2 * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces
4 * Copyright (C) 2004 Andrew de Quincey
6 * Parts of this file were based on sources as follows:
8 * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de>
12 * Copyright (C) 1999-2002 Ralph Metzler
13 * & Marcus Metzler for convergence integrated media GmbH
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 * To obtain the license, point your browser to
25 * http://www.gnu.org/copyleft/gpl.html
28 #define pr_fmt(fmt) "dvb_ca_en50221: " fmt
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/list.h>
33 #include <linux/module.h>
34 #include <linux/vmalloc.h>
35 #include <linux/delay.h>
36 #include <linux/spinlock.h>
37 #include <linux/sched/signal.h>
38 #include <linux/kthread.h>
40 #include "dvb_ca_en50221.h"
41 #include "dvb_ringbuffer.h"
43 static int dvb_ca_en50221_debug
;
45 module_param_named(cam_debug
, dvb_ca_en50221_debug
, int, 0644);
46 MODULE_PARM_DESC(cam_debug
, "enable verbose debug messages");
48 #define dprintk(fmt, arg...) do { \
49 if (dvb_ca_en50221_debug) \
50 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg);\
53 #define INIT_TIMEOUT_SECS 10
55 #define HOST_LINK_BUF_SIZE 0x200
57 #define RX_BUFFER_SIZE 65535
59 #define MAX_RX_PACKETS_PER_ITERATION 10
62 #define CTRLIF_COMMAND 1
63 #define CTRLIF_STATUS 1
64 #define CTRLIF_SIZE_LOW 2
65 #define CTRLIF_SIZE_HIGH 3
67 #define CMDREG_HC 1 /* Host control */
68 #define CMDREG_SW 2 /* Size write */
69 #define CMDREG_SR 4 /* Size read */
70 #define CMDREG_RS 8 /* Reset interface */
71 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
72 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
73 #define IRQEN (CMDREG_DAIE)
75 #define STATUSREG_RE 1 /* read error */
76 #define STATUSREG_WE 2 /* write error */
77 #define STATUSREG_FR 0x40 /* module free */
78 #define STATUSREG_DA 0x80 /* data available */
79 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
82 #define DVB_CA_SLOTSTATE_NONE 0
83 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
84 #define DVB_CA_SLOTSTATE_RUNNING 2
85 #define DVB_CA_SLOTSTATE_INVALID 3
86 #define DVB_CA_SLOTSTATE_WAITREADY 4
87 #define DVB_CA_SLOTSTATE_VALIDATE 5
88 #define DVB_CA_SLOTSTATE_WAITFR 6
89 #define DVB_CA_SLOTSTATE_LINKINIT 7
92 /* Information on a CA slot */
95 /* current state of the CAM */
98 /* mutex used for serializing access to one CI slot */
99 struct mutex slot_lock
;
101 /* Number of CAMCHANGES that have occurred since last processing */
102 atomic_t camchange_count
;
104 /* Type of last CAMCHANGE */
107 /* base address of CAM config */
110 /* value to write into Config Control register */
113 /* if 1, the CAM supports DA IRQs */
114 u8 da_irq_supported
:1;
116 /* size of the buffer to use when talking to the CAM */
119 /* buffer for incoming packets */
120 struct dvb_ringbuffer rx_buffer
;
122 /* timer used during various states of the slot */
123 unsigned long timeout
;
126 /* Private CA-interface information */
127 struct dvb_ca_private
{
128 struct kref refcount
;
130 /* pointer back to the public data structure */
131 struct dvb_ca_en50221
*pub
;
134 struct dvb_device
*dvbdev
;
136 /* Flags describing the interface (DVB_CA_FLAG_*) */
139 /* number of slots supported by this CA interface */
140 unsigned int slot_count
;
142 /* information on each slot */
143 struct dvb_ca_slot
*slot_info
;
145 /* wait queues for read() and write() operations */
146 wait_queue_head_t wait_queue
;
148 /* PID of the monitoring thread */
149 struct task_struct
*thread
;
151 /* Flag indicating if the CA device is open */
154 /* Flag indicating the thread should wake up now */
155 unsigned int wakeup
:1;
157 /* Delay the main thread should use */
160 /* Slot to start looking for data to read from in the next user-space read operation */
163 /* mutex serializing ioctls */
164 struct mutex ioctl_mutex
;
167 static void dvb_ca_private_free(struct dvb_ca_private
*ca
)
171 dvb_free_device(ca
->dvbdev
);
172 for (i
= 0; i
< ca
->slot_count
; i
++)
173 vfree(ca
->slot_info
[i
].rx_buffer
.data
);
175 kfree(ca
->slot_info
);
179 static void dvb_ca_private_release(struct kref
*ref
)
181 struct dvb_ca_private
*ca
= container_of(ref
, struct dvb_ca_private
, refcount
);
182 dvb_ca_private_free(ca
);
185 static void dvb_ca_private_get(struct dvb_ca_private
*ca
)
187 kref_get(&ca
->refcount
);
190 static void dvb_ca_private_put(struct dvb_ca_private
*ca
)
192 kref_put(&ca
->refcount
, dvb_ca_private_release
);
195 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
196 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
,
197 u8
*ebuf
, int ecount
);
198 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
,
199 u8
*ebuf
, int ecount
);
203 * Safely find needle in haystack.
205 * @haystack: Buffer to look in.
206 * @hlen: Number of bytes in haystack.
207 * @needle: Buffer to find.
208 * @nlen: Number of bytes in needle.
209 * @return Pointer into haystack needle was found at, or NULL if not found.
211 static char *findstr(char *haystack
, int hlen
, char *needle
, int nlen
)
218 for (i
= 0; i
<= hlen
- nlen
; i
++) {
219 if (!strncmp(haystack
+ i
, needle
, nlen
))
228 /* ******************************************************************************** */
229 /* EN50221 physical interface functions */
233 * dvb_ca_en50221_check_camstatus - Check CAM status.
235 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private
*ca
, int slot
)
242 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
) {
243 return (atomic_read(&ca
->slot_info
[slot
].camchange_count
) != 0);
247 slot_status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, ca
->open
);
249 cam_present_now
= (slot_status
& DVB_CA_EN50221_POLL_CAM_PRESENT
) ? 1 : 0;
250 cam_changed
= (slot_status
& DVB_CA_EN50221_POLL_CAM_CHANGED
) ? 1 : 0;
252 int cam_present_old
= (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
);
253 cam_changed
= (cam_present_now
!= cam_present_old
);
257 if (!cam_present_now
) {
258 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
260 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
262 atomic_set(&ca
->slot_info
[slot
].camchange_count
, 1);
264 if ((ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) &&
265 (slot_status
& DVB_CA_EN50221_POLL_CAM_READY
)) {
266 // move to validate state if reset is completed
267 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
276 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
277 * register on a CAM interface, checking for errors and timeout.
280 * @slot: Slot on interface.
281 * @waitfor: Flags to wait for.
282 * @timeout_ms: Timeout in milliseconds.
284 * @return 0 on success, nonzero on error.
286 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
287 u8 waitfor
, int timeout_hz
)
289 unsigned long timeout
;
292 dprintk("%s\n", __func__
);
294 /* loop until timeout elapsed */
296 timeout
= jiffies
+ timeout_hz
;
298 /* read the status and check for error */
299 int res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
303 /* if we got the flags, it was successful! */
305 dprintk("%s succeeded timeout:%lu\n",
306 __func__
, jiffies
- start
);
310 /* check for timeout */
311 if (time_after(jiffies
, timeout
)) {
319 dprintk("%s failed timeout:%lu\n", __func__
, jiffies
- start
);
321 /* if we get here, we've timed out */
327 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
332 * @return 0 on success, nonzero on failure.
334 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
340 dprintk("%s\n", __func__
);
342 /* we'll be determining these during this function */
343 ca
->slot_info
[slot
].da_irq_supported
= 0;
345 /* set the host link buffer size temporarily. it will be overwritten with the
346 * real negotiated size later. */
347 ca
->slot_info
[slot
].link_buf_size
= 2;
349 /* read the buffer size from the CAM */
350 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SR
)) != 0)
352 ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
);
355 if ((ret
= dvb_ca_en50221_read_data(ca
, slot
, buf
, 2)) != 2)
357 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
360 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
361 buf_size
= (buf
[0] << 8) | buf
[1];
362 if (buf_size
> HOST_LINK_BUF_SIZE
)
363 buf_size
= HOST_LINK_BUF_SIZE
;
364 ca
->slot_info
[slot
].link_buf_size
= buf_size
;
365 buf
[0] = buf_size
>> 8;
366 buf
[1] = buf_size
& 0xff;
367 dprintk("Chosen link buffer size of %i\n", buf_size
);
369 /* write the buffer size to the CAM */
370 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SW
)) != 0)
372 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_FR
, HZ
/ 10)) != 0)
374 if ((ret
= dvb_ca_en50221_write_data(ca
, slot
, buf
, 2)) != 2)
376 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
384 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
388 * @address: Address to read from. Updated.
389 * @tupleType: Tuple id byte. Updated.
390 * @tupleLength: Tuple length. Updated.
391 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
393 * @return 0 on success, nonzero on error.
395 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private
*ca
, int slot
,
396 int *address
, int *tupleType
,
397 int *tupleLength
, u8
*tuple
)
402 int _address
= *address
;
404 /* grab the next tuple length and type */
405 if ((_tupleType
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
)) < 0)
407 if (_tupleType
== 0xff) {
408 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType
);
410 *tupleType
= _tupleType
;
414 if ((_tupleLength
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ 2)) < 0)
418 dprintk("TUPLE type:0x%x length:%i\n", _tupleType
, _tupleLength
);
420 /* read in the whole tuple */
421 for (i
= 0; i
< _tupleLength
; i
++) {
422 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ (i
* 2));
423 dprintk(" 0x%02x: 0x%02x %c\n",
425 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
427 _address
+= (_tupleLength
* 2);
430 *tupleType
= _tupleType
;
431 *tupleLength
= _tupleLength
;
438 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
439 * extracting Config register, and checking it is a DVB CAM module.
444 * @return 0 on success, <0 on failure.
446 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
455 int got_cftableentry
= 0;
464 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
466 if (tupleType
!= 0x1D)
473 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
475 if (tupleType
!= 0x1C)
482 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
484 if (tupleType
!= 0x15)
490 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
491 &tupleLength
, tuple
)) < 0)
493 if (tupleType
!= 0x20)
495 if (tupleLength
!= 4)
497 manfid
= (tuple
[1] << 8) | tuple
[0];
498 devid
= (tuple
[3] << 8) | tuple
[2];
503 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
504 &tupleLength
, tuple
)) < 0)
506 if (tupleType
!= 0x1A)
511 /* extract the configbase */
513 if (tupleLength
< (3 + rasz
+ 14))
515 ca
->slot_info
[slot
].config_base
= 0;
516 for (i
= 0; i
< rasz
+ 1; i
++) {
517 ca
->slot_info
[slot
].config_base
|= (tuple
[2 + i
] << (8 * i
));
520 /* check it contains the correct DVB string */
521 dvb_str
= findstr((char *)tuple
, tupleLength
, "DVB_CI_V", 8);
524 if (tupleLength
< ((dvb_str
- (char *) tuple
) + 12))
527 /* is it a version we support? */
528 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
529 pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
530 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9],
531 dvb_str
[10], dvb_str
[11]);
535 /* process the CFTABLE_ENTRY tuples, and any after those */
536 while ((!end_chain
) && (address
< 0x1000)) {
537 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
538 &tupleLength
, tuple
)) < 0)
541 case 0x1B: // CISTPL_CFTABLE_ENTRY
542 if (tupleLength
< (2 + 11 + 17))
545 /* if we've already parsed one, just use it */
546 if (got_cftableentry
)
549 /* get the config option */
550 ca
->slot_info
[slot
].config_option
= tuple
[0] & 0x3f;
552 /* OK, check it contains the correct strings */
553 if ((findstr((char *)tuple
, tupleLength
, "DVB_HOST", 8) == NULL
) ||
554 (findstr((char *)tuple
, tupleLength
, "DVB_CI_MODULE", 13) == NULL
))
557 got_cftableentry
= 1;
560 case 0x14: // CISTPL_NO_LINK
563 case 0xFF: // CISTPL_END
567 default: /* Unknown tuple type - just skip this tuple and move to the next one */
568 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
569 tupleType
, tupleLength
);
574 if ((address
> 0x1000) || (!got_cftableentry
))
577 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
578 manfid
, devid
, ca
->slot_info
[slot
].config_base
,
579 ca
->slot_info
[slot
].config_option
);
587 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
590 * @slot: Slot containing the CAM.
592 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
596 dprintk("%s\n", __func__
);
598 /* set the config option */
599 ca
->pub
->write_attribute_mem(ca
->pub
, slot
,
600 ca
->slot_info
[slot
].config_base
,
601 ca
->slot_info
[slot
].config_option
);
604 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, ca
->slot_info
[slot
].config_base
);
605 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
606 ca
->slot_info
[slot
].config_option
, configoption
& 0x3f);
615 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
616 * interface. It reads a buffer of data from the CAM. The data can either
617 * be stored in a supplied buffer, or automatically be added to the slot's
621 * @slot: Slot to read from.
622 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
623 * the data will be added into the buffering system as a normal fragment.
624 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
626 * @return Number of bytes read, or < 0 on error
628 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
,
629 u8
*ebuf
, int ecount
)
633 u8 buf
[HOST_LINK_BUF_SIZE
];
636 dprintk("%s\n", __func__
);
638 /* check if we have space for a link buf in the rx_buffer */
642 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
646 buf_free
= dvb_ringbuffer_free(&ca
->slot_info
[slot
].rx_buffer
);
648 if (buf_free
< (ca
->slot_info
[slot
].link_buf_size
+
649 DVB_RINGBUFFER_PKTHDRSIZE
)) {
655 if (ca
->pub
->read_data
&&
656 (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_LINKINIT
)) {
658 status
= ca
->pub
->read_data(ca
->pub
, slot
, buf
,
661 status
= ca
->pub
->read_data(ca
->pub
, slot
, buf
, ecount
);
669 /* check if there is data available */
670 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
674 if (!(status
& STATUSREG_DA
)) {
680 /* read the amount of data */
681 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
685 bytes_read
= status
<< 8;
686 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
690 bytes_read
|= status
;
692 /* check it will fit */
694 if (bytes_read
> ca
->slot_info
[slot
].link_buf_size
) {
695 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
696 ca
->dvbdev
->adapter
->num
, bytes_read
,
697 ca
->slot_info
[slot
].link_buf_size
);
698 ca
->slot_info
[slot
].slot_state
=
699 DVB_CA_SLOTSTATE_LINKINIT
;
703 if (bytes_read
< 2) {
704 pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
705 ca
->dvbdev
->adapter
->num
);
706 ca
->slot_info
[slot
].slot_state
=
707 DVB_CA_SLOTSTATE_LINKINIT
;
712 if (bytes_read
> ecount
) {
713 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
714 ca
->dvbdev
->adapter
->num
);
720 /* fill the buffer */
721 for (i
= 0; i
< bytes_read
; i
++) {
722 /* read byte and check */
723 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
728 /* OK, store it in the buffer */
732 /* check for read error (RE should now be 0) */
733 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
737 if (status
& STATUSREG_RE
) {
738 ca
->slot_info
[slot
].slot_state
=
739 DVB_CA_SLOTSTATE_LINKINIT
;
745 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
747 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
751 dvb_ringbuffer_pkt_write(&ca
->slot_info
[slot
].rx_buffer
, buf
, bytes_read
);
753 memcpy(ebuf
, buf
, bytes_read
);
756 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
757 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
759 /* wake up readers when a last_fragment is received */
760 if ((buf
[1] & 0x80) == 0x00) {
761 wake_up_interruptible(&ca
->wait_queue
);
771 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
772 * interface. It writes a buffer of data to a CAM.
775 * @slot: Slot to write to.
776 * @ebuf: The data in this buffer is treated as a complete link-level packet to
778 * @count: Size of ebuf.
780 * @return Number of bytes written, or < 0 on error.
782 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
,
783 u8
*buf
, int bytes_write
)
788 dprintk("%s\n", __func__
);
792 if (bytes_write
> ca
->slot_info
[slot
].link_buf_size
)
795 if (ca
->pub
->write_data
&&
796 (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_LINKINIT
))
797 return ca
->pub
->write_data(ca
->pub
, slot
, buf
, bytes_write
);
799 /* it is possible we are dealing with a single buffer implementation,
800 thus if there is data available for read or if there is even a read
801 already in progress, we do nothing but awake the kernel thread to
802 process the data if necessary. */
803 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
805 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
806 if (status
& STATUSREG_DA
)
807 dvb_ca_en50221_thread_wakeup(ca
);
814 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
815 IRQEN
| CMDREG_HC
)) != 0)
818 /* check if interface is still free */
819 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
821 if (!(status
& STATUSREG_FR
)) {
822 /* it wasn't free => try again later */
828 * It may need some time for the CAM to settle down, or there might
829 * be a race condition between the CAM, writing HC and our last
830 * check for DA. This happens, if the CAM asserts DA, just after
831 * checking DA before we are setting HC. In this case it might be
832 * a bug in the CAM to keep the FR bit, the lower layer/HW
833 * communication requires a longer timeout or the CAM needs more
834 * time internally. But this happens in reality!
835 * We need to read the status from the HW again and do the same
836 * we did for the previous check for DA
838 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
842 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
843 if (status
& STATUSREG_DA
)
844 dvb_ca_en50221_thread_wakeup(ca
);
850 /* send the amount of data */
851 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
, bytes_write
>> 8)) != 0)
853 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
854 bytes_write
& 0xff)) != 0)
857 /* send the buffer */
858 for (i
= 0; i
< bytes_write
; i
++) {
859 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
, buf
[i
])) != 0)
863 /* check for write error (WE should now be 0) */
864 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
866 if (status
& STATUSREG_WE
) {
867 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
871 status
= bytes_write
;
873 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
874 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
877 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
885 /* ******************************************************************************** */
886 /* EN50221 higher level functions */
890 * dvb_ca_en50221_slot_shutdown - A CAM has been removed => shut it down.
893 * @slot: Slot to shut down.
895 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
897 dprintk("%s\n", __func__
);
899 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
900 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
902 /* need to wake up all processes to check if they're now
903 trying to write to a defunct CAM */
904 wake_up_interruptible(&ca
->wait_queue
);
906 dprintk("Slot %i shutdown\n", slot
);
914 * dvb_ca_en50221_camchange_irq - A CAMCHANGE IRQ has occurred.
917 * @slot: Slot concerned.
918 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
920 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
, int change_type
)
922 struct dvb_ca_private
*ca
= pubca
->private;
924 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
926 switch (change_type
) {
927 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
928 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
935 ca
->slot_info
[slot
].camchange_type
= change_type
;
936 atomic_inc(&ca
->slot_info
[slot
].camchange_count
);
937 dvb_ca_en50221_thread_wakeup(ca
);
939 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
943 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
946 * @slot: Slot concerned.
948 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
950 struct dvb_ca_private
*ca
= pubca
->private;
952 dprintk("CAMREADY IRQ slot:%i\n", slot
);
954 if (ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
955 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
956 dvb_ca_en50221_thread_wakeup(ca
);
959 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
963 * dvb_ca_en50221_frda_irq - An FR or DA IRQ has occurred.
966 * @slot: Slot concerned.
968 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
970 struct dvb_ca_private
*ca
= pubca
->private;
973 dprintk("FR/DA IRQ slot:%i\n", slot
);
975 switch (ca
->slot_info
[slot
].slot_state
) {
976 case DVB_CA_SLOTSTATE_LINKINIT
:
977 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
978 if (flags
& STATUSREG_DA
) {
979 dprintk("CAM supports DA IRQ\n");
980 ca
->slot_info
[slot
].da_irq_supported
= 1;
984 case DVB_CA_SLOTSTATE_RUNNING
:
986 dvb_ca_en50221_thread_wakeup(ca
);
990 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
993 /* ******************************************************************************** */
994 /* EN50221 thread functions */
997 * Wake up the DVB CA thread
1001 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
1004 dprintk("%s\n", __func__
);
1008 wake_up_process(ca
->thread
);
1012 * Update the delay used by the thread.
1016 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
1019 int curdelay
= 100000000;
1022 /* Beware of too high polling frequency, because one polling
1023 * call might take several hundred milliseconds until timeout!
1025 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1026 switch (ca
->slot_info
[slot
].slot_state
) {
1028 case DVB_CA_SLOTSTATE_NONE
:
1029 delay
= HZ
* 60; /* 60s */
1030 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
1031 delay
= HZ
* 5; /* 5s */
1033 case DVB_CA_SLOTSTATE_INVALID
:
1034 delay
= HZ
* 60; /* 60s */
1035 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
1036 delay
= HZ
/ 10; /* 100ms */
1039 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1040 case DVB_CA_SLOTSTATE_WAITREADY
:
1041 case DVB_CA_SLOTSTATE_VALIDATE
:
1042 case DVB_CA_SLOTSTATE_WAITFR
:
1043 case DVB_CA_SLOTSTATE_LINKINIT
:
1044 delay
= HZ
/ 10; /* 100ms */
1047 case DVB_CA_SLOTSTATE_RUNNING
:
1048 delay
= HZ
* 60; /* 60s */
1049 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
1050 delay
= HZ
/ 10; /* 100ms */
1052 if ((!ca
->slot_info
[slot
].da_irq_supported
) ||
1053 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
)))
1054 delay
= HZ
/ 10; /* 100ms */
1059 if (delay
< curdelay
)
1063 ca
->delay
= curdelay
;
1069 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
1071 static int dvb_ca_en50221_thread(void *data
)
1073 struct dvb_ca_private
*ca
= data
;
1080 dprintk("%s\n", __func__
);
1082 /* choose the correct initial delay */
1083 dvb_ca_en50221_thread_update_delay(ca
);
1086 while (!kthread_should_stop()) {
1087 /* sleep for a bit */
1089 set_current_state(TASK_INTERRUPTIBLE
);
1090 schedule_timeout(ca
->delay
);
1091 if (kthread_should_stop())
1096 /* go through all the slots processing them */
1097 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1099 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1101 // check the cam status + deal with CAMCHANGEs
1102 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1103 /* clear down an old CI slot if necessary */
1104 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1105 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1107 /* if a CAM is NOW present, initialise it */
1108 if (ca
->slot_info
[slot
].camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
) {
1109 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1112 /* we've handled one CAMCHANGE */
1113 dvb_ca_en50221_thread_update_delay(ca
);
1114 atomic_dec(&ca
->slot_info
[slot
].camchange_count
);
1117 // CAM state machine
1118 switch (ca
->slot_info
[slot
].slot_state
) {
1119 case DVB_CA_SLOTSTATE_NONE
:
1120 case DVB_CA_SLOTSTATE_INVALID
:
1124 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1125 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1126 ca
->pub
->slot_reset(ca
->pub
, slot
);
1127 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1130 case DVB_CA_SLOTSTATE_WAITREADY
:
1131 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1132 pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
1133 ca
->dvbdev
->adapter
->num
);
1134 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1135 dvb_ca_en50221_thread_update_delay(ca
);
1138 // no other action needed; will automatically change state when ready
1141 case DVB_CA_SLOTSTATE_VALIDATE
:
1142 if (dvb_ca_en50221_parse_attributes(ca
, slot
) != 0) {
1143 /* we need this extra check for annoying interfaces like the budget-av */
1144 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1145 (ca
->pub
->poll_slot_status
)) {
1146 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1147 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1148 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1149 dvb_ca_en50221_thread_update_delay(ca
);
1154 pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1155 ca
->dvbdev
->adapter
->num
);
1156 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1157 dvb_ca_en50221_thread_update_delay(ca
);
1160 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1161 pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1162 ca
->dvbdev
->adapter
->num
);
1163 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1164 dvb_ca_en50221_thread_update_delay(ca
);
1167 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1168 CTRLIF_COMMAND
, CMDREG_RS
) != 0) {
1169 pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
1170 ca
->dvbdev
->adapter
->num
);
1171 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1172 dvb_ca_en50221_thread_update_delay(ca
);
1175 dprintk("DVB CAM validated successfully\n");
1177 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1178 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1182 case DVB_CA_SLOTSTATE_WAITFR
:
1183 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1184 pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1185 ca
->dvbdev
->adapter
->num
);
1186 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1187 dvb_ca_en50221_thread_update_delay(ca
);
1191 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1192 if (flags
& STATUSREG_FR
) {
1193 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1198 case DVB_CA_SLOTSTATE_LINKINIT
:
1199 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1200 /* we need this extra check for annoying interfaces like the budget-av */
1201 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1202 (ca
->pub
->poll_slot_status
)) {
1203 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1204 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1205 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1206 dvb_ca_en50221_thread_update_delay(ca
);
1211 pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
1212 ca
->dvbdev
->adapter
->num
);
1213 ca
->slot_info
[slot
].slot_state
=
1214 DVB_CA_SLOTSTATE_UNINITIALISED
;
1215 dvb_ca_en50221_thread_update_delay(ca
);
1219 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1220 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1221 if (rxbuf
== NULL
) {
1222 pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1223 ca
->dvbdev
->adapter
->num
);
1224 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1225 dvb_ca_en50221_thread_update_delay(ca
);
1228 dvb_ringbuffer_init(&ca
->slot_info
[slot
].rx_buffer
, rxbuf
, RX_BUFFER_SIZE
);
1231 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1232 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1233 dvb_ca_en50221_thread_update_delay(ca
);
1234 pr_err("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
1235 ca
->dvbdev
->adapter
->num
);
1238 case DVB_CA_SLOTSTATE_RUNNING
:
1242 // poll slots for data
1244 while ((status
= dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0)) > 0) {
1248 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1249 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1250 // we dont want to sleep on the next iteration so we can handle the cam change
1255 /* check if we've hit our limit this time */
1256 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1257 // dont sleep; there is likely to be more data to read
1265 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1274 /* ******************************************************************************** */
1275 /* EN50221 IO interface functions */
1278 * Real ioctl implementation.
1279 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1281 * @inode: Inode concerned.
1282 * @file: File concerned.
1283 * @cmd: IOCTL command.
1284 * @arg: Associated argument.
1286 * @return 0 on success, <0 on error.
1288 static int dvb_ca_en50221_io_do_ioctl(struct file
*file
,
1289 unsigned int cmd
, void *parg
)
1291 struct dvb_device
*dvbdev
= file
->private_data
;
1292 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1296 dprintk("%s\n", __func__
);
1298 if (mutex_lock_interruptible(&ca
->ioctl_mutex
))
1299 return -ERESTARTSYS
;
1303 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1304 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1305 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1306 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1307 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1308 dvb_ca_en50221_camchange_irq(ca
->pub
,
1310 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1312 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1314 ca
->next_read_slot
= 0;
1315 dvb_ca_en50221_thread_wakeup(ca
);
1319 struct ca_caps
*caps
= parg
;
1321 caps
->slot_num
= ca
->slot_count
;
1322 caps
->slot_type
= CA_CI_LINK
;
1323 caps
->descr_num
= 0;
1324 caps
->descr_type
= 0;
1328 case CA_GET_SLOT_INFO
: {
1329 struct ca_slot_info
*info
= parg
;
1331 if ((info
->num
> ca
->slot_count
) || (info
->num
< 0)) {
1336 info
->type
= CA_CI_LINK
;
1338 if ((ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1339 && (ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_INVALID
)) {
1340 info
->flags
= CA_CI_MODULE_PRESENT
;
1342 if (ca
->slot_info
[info
->num
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1343 info
->flags
|= CA_CI_MODULE_READY
;
1354 mutex_unlock(&ca
->ioctl_mutex
);
1360 * Wrapper for ioctl implementation.
1362 * @inode: Inode concerned.
1363 * @file: File concerned.
1364 * @cmd: IOCTL command.
1365 * @arg: Associated argument.
1367 * @return 0 on success, <0 on error.
1369 static long dvb_ca_en50221_io_ioctl(struct file
*file
,
1370 unsigned int cmd
, unsigned long arg
)
1372 return dvb_usercopy(file
, cmd
, arg
, dvb_ca_en50221_io_do_ioctl
);
1377 * Implementation of write() syscall.
1379 * @file: File structure.
1380 * @buf: Source buffer.
1381 * @count: Size of source buffer.
1382 * @ppos: Position in file (ignored).
1384 * @return Number of bytes read, or <0 on error.
1386 static ssize_t
dvb_ca_en50221_io_write(struct file
*file
,
1387 const char __user
*buf
, size_t count
,
1390 struct dvb_device
*dvbdev
= file
->private_data
;
1391 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1392 u8 slot
, connection_id
;
1394 u8 fragbuf
[HOST_LINK_BUF_SIZE
];
1397 unsigned long timeout
;
1400 dprintk("%s\n", __func__
);
1402 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1406 /* extract slot & connection id */
1407 if (copy_from_user(&slot
, buf
, 1))
1409 if (copy_from_user(&connection_id
, buf
+ 1, 1))
1414 /* check if the slot is actually running */
1415 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1418 /* fragment the packets & store in the buffer */
1419 while (fragpos
< count
) {
1420 fraglen
= ca
->slot_info
[slot
].link_buf_size
- 2;
1423 if (fraglen
> HOST_LINK_BUF_SIZE
- 2)
1424 fraglen
= HOST_LINK_BUF_SIZE
- 2;
1425 if ((count
- fragpos
) < fraglen
)
1426 fraglen
= count
- fragpos
;
1428 fragbuf
[0] = connection_id
;
1429 fragbuf
[1] = ((fragpos
+ fraglen
) < count
) ? 0x80 : 0x00;
1430 status
= copy_from_user(fragbuf
+ 2, buf
+ fragpos
, fraglen
);
1436 timeout
= jiffies
+ HZ
/ 2;
1438 while (!time_after(jiffies
, timeout
)) {
1439 /* check the CAM hasn't been removed/reset in the meantime */
1440 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
) {
1445 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1446 status
= dvb_ca_en50221_write_data(ca
, slot
, fragbuf
, fraglen
+ 2);
1447 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1448 if (status
== (fraglen
+ 2)) {
1452 if (status
!= -EAGAIN
)
1472 * Condition for waking up in dvb_ca_en50221_io_read_condition
1474 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private
*ca
,
1475 int *result
, int *_slot
)
1481 int connection_id
= -1;
1485 slot
= ca
->next_read_slot
;
1486 while ((slot_count
< ca
->slot_count
) && (!found
)) {
1487 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1490 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1494 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1496 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1497 if (connection_id
== -1)
1498 connection_id
= hdr
[0];
1499 if ((hdr
[0] == connection_id
) && ((hdr
[1] & 0x80) == 0)) {
1505 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1509 slot
= (slot
+ 1) % ca
->slot_count
;
1513 ca
->next_read_slot
= slot
;
1519 * Implementation of read() syscall.
1521 * @file: File structure.
1522 * @buf: Destination buffer.
1523 * @count: Size of destination buffer.
1524 * @ppos: Position in file (ignored).
1526 * @return Number of bytes read, or <0 on error.
1528 static ssize_t
dvb_ca_en50221_io_read(struct file
*file
, char __user
*buf
,
1529 size_t count
, loff_t
*ppos
)
1531 struct dvb_device
*dvbdev
= file
->private_data
;
1532 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1537 int connection_id
= -1;
1539 int last_fragment
= 0;
1544 dprintk("%s\n", __func__
);
1546 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1550 /* wait for some data */
1551 if ((status
= dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
)) == 0) {
1553 /* if we're in nonblocking mode, exit immediately */
1554 if (file
->f_flags
& O_NONBLOCK
)
1555 return -EWOULDBLOCK
;
1557 /* wait for some data */
1558 status
= wait_event_interruptible(ca
->wait_queue
,
1559 dvb_ca_en50221_io_read_condition
1560 (ca
, &result
, &slot
));
1562 if ((status
< 0) || (result
< 0)) {
1568 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1572 pr_err("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n",
1573 ca
->dvbdev
->adapter
->num
);
1578 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1579 if (connection_id
== -1)
1580 connection_id
= hdr
[0];
1581 if (hdr
[0] == connection_id
) {
1582 if (pktlen
< count
) {
1583 if ((pktlen
+ fraglen
- 2) > count
) {
1584 fraglen
= count
- pktlen
;
1589 if ((status
= dvb_ringbuffer_pkt_read_user(&ca
->slot_info
[slot
].rx_buffer
, idx
, 2,
1590 buf
+ pktlen
, fraglen
)) < 0) {
1596 if ((hdr
[1] & 0x80) == 0)
1601 idx2
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1603 dvb_ringbuffer_pkt_dispose(&ca
->slot_info
[slot
].rx_buffer
, idx
);
1606 } while (!last_fragment
);
1609 hdr
[1] = connection_id
;
1610 status
= copy_to_user(buf
, hdr
, 2);
1623 * Implementation of file open syscall.
1625 * @inode: Inode concerned.
1626 * @file: File concerned.
1628 * @return 0 on success, <0 on failure.
1630 static int dvb_ca_en50221_io_open(struct inode
*inode
, struct file
*file
)
1632 struct dvb_device
*dvbdev
= file
->private_data
;
1633 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1637 dprintk("%s\n", __func__
);
1639 if (!try_module_get(ca
->pub
->owner
))
1642 err
= dvb_generic_open(inode
, file
);
1644 module_put(ca
->pub
->owner
);
1648 for (i
= 0; i
< ca
->slot_count
; i
++) {
1650 if (ca
->slot_info
[i
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1651 if (ca
->slot_info
[i
].rx_buffer
.data
!= NULL
) {
1652 /* it is safe to call this here without locks because
1653 * ca->open == 0. Data is not read in this case */
1654 dvb_ringbuffer_flush(&ca
->slot_info
[i
].rx_buffer
);
1660 dvb_ca_en50221_thread_update_delay(ca
);
1661 dvb_ca_en50221_thread_wakeup(ca
);
1663 dvb_ca_private_get(ca
);
1670 * Implementation of file close syscall.
1672 * @inode: Inode concerned.
1673 * @file: File concerned.
1675 * @return 0 on success, <0 on failure.
1677 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1679 struct dvb_device
*dvbdev
= file
->private_data
;
1680 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1683 dprintk("%s\n", __func__
);
1685 /* mark the CA device as closed */
1687 dvb_ca_en50221_thread_update_delay(ca
);
1689 err
= dvb_generic_release(inode
, file
);
1691 module_put(ca
->pub
->owner
);
1693 dvb_ca_private_put(ca
);
1700 * Implementation of poll() syscall.
1702 * @file: File concerned.
1703 * @wait: poll wait table.
1705 * @return Standard poll mask.
1707 static unsigned int dvb_ca_en50221_io_poll(struct file
*file
, poll_table
*wait
)
1709 struct dvb_device
*dvbdev
= file
->private_data
;
1710 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1711 unsigned int mask
= 0;
1715 dprintk("%s\n", __func__
);
1717 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1721 /* if there is something, return now */
1725 /* wait for something to happen */
1726 poll_wait(file
, &ca
->wait_queue
, wait
);
1728 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1734 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1737 static const struct file_operations dvb_ca_fops
= {
1738 .owner
= THIS_MODULE
,
1739 .read
= dvb_ca_en50221_io_read
,
1740 .write
= dvb_ca_en50221_io_write
,
1741 .unlocked_ioctl
= dvb_ca_en50221_io_ioctl
,
1742 .open
= dvb_ca_en50221_io_open
,
1743 .release
= dvb_ca_en50221_io_release
,
1744 .poll
= dvb_ca_en50221_io_poll
,
1745 .llseek
= noop_llseek
,
1748 static const struct dvb_device dvbdev_ca
= {
1753 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1754 .name
= "dvb-ca-en50221",
1756 .fops
= &dvb_ca_fops
,
1759 /* ******************************************************************************** */
1760 /* Initialisation/shutdown functions */
1764 * Initialise a new DVB CA EN50221 interface device.
1766 * @dvb_adapter: DVB adapter to attach the new CA device to.
1767 * @ca: The dvb_ca instance.
1768 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1769 * @slot_count: Number of slots supported.
1771 * @return 0 on success, nonzero on failure
1773 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1774 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1777 struct dvb_ca_private
*ca
= NULL
;
1780 dprintk("%s\n", __func__
);
1785 /* initialise the system data */
1786 if ((ca
= kzalloc(sizeof(struct dvb_ca_private
), GFP_KERNEL
)) == NULL
) {
1790 kref_init(&ca
->refcount
);
1793 ca
->slot_count
= slot_count
;
1794 if ((ca
->slot_info
= kcalloc(slot_count
, sizeof(struct dvb_ca_slot
), GFP_KERNEL
)) == NULL
) {
1798 init_waitqueue_head(&ca
->wait_queue
);
1801 ca
->next_read_slot
= 0;
1802 pubca
->private = ca
;
1804 /* register the DVB device */
1805 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
, DVB_DEVICE_CA
, 0);
1807 goto free_slot_info
;
1809 /* now initialise each slot */
1810 for (i
= 0; i
< slot_count
; i
++) {
1811 memset(&ca
->slot_info
[i
], 0, sizeof(struct dvb_ca_slot
));
1812 ca
->slot_info
[i
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1813 atomic_set(&ca
->slot_info
[i
].camchange_count
, 0);
1814 ca
->slot_info
[i
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1815 mutex_init(&ca
->slot_info
[i
].slot_lock
);
1818 mutex_init(&ca
->ioctl_mutex
);
1820 if (signal_pending(current
)) {
1822 goto unregister_device
;
1826 /* create a kthread for monitoring this CA device */
1827 ca
->thread
= kthread_run(dvb_ca_en50221_thread
, ca
, "kdvb-ca-%i:%i",
1828 ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
1829 if (IS_ERR(ca
->thread
)) {
1830 ret
= PTR_ERR(ca
->thread
);
1831 pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
1833 goto unregister_device
;
1838 dvb_unregister_device(ca
->dvbdev
);
1840 kfree(ca
->slot_info
);
1844 pubca
->private = NULL
;
1847 EXPORT_SYMBOL(dvb_ca_en50221_release
);
1852 * Release a DVB CA EN50221 interface device.
1854 * @ca_dev: The dvb_device_t instance for the CA device.
1855 * @ca: The associated dvb_ca instance.
1857 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1859 struct dvb_ca_private
*ca
= pubca
->private;
1862 dprintk("%s\n", __func__
);
1864 /* shutdown the thread if there was one */
1865 kthread_stop(ca
->thread
);
1867 for (i
= 0; i
< ca
->slot_count
; i
++) {
1868 dvb_ca_en50221_slot_shutdown(ca
, i
);
1870 dvb_remove_device(ca
->dvbdev
);
1871 dvb_ca_private_put(ca
);
1872 pubca
->private = NULL
;