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 #define pr_fmt(fmt) "dvb_ca_en50221: " fmt
33 #include <linux/errno.h>
34 #include <linux/slab.h>
35 #include <linux/list.h>
36 #include <linux/module.h>
37 #include <linux/vmalloc.h>
38 #include <linux/delay.h>
39 #include <linux/spinlock.h>
40 #include <linux/sched.h>
41 #include <linux/kthread.h>
43 #include "dvb_ca_en50221.h"
44 #include "dvb_ringbuffer.h"
46 static int dvb_ca_en50221_debug
;
48 module_param_named(cam_debug
, dvb_ca_en50221_debug
, int, 0644);
49 MODULE_PARM_DESC(cam_debug
, "enable verbose debug messages");
51 #define dprintk(fmt, arg...) do { \
52 if (dvb_ca_en50221_debug) \
53 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg);\
56 #define INIT_TIMEOUT_SECS 10
58 #define HOST_LINK_BUF_SIZE 0x200
60 #define RX_BUFFER_SIZE 65535
62 #define MAX_RX_PACKETS_PER_ITERATION 10
65 #define CTRLIF_COMMAND 1
66 #define CTRLIF_STATUS 1
67 #define CTRLIF_SIZE_LOW 2
68 #define CTRLIF_SIZE_HIGH 3
70 #define CMDREG_HC 1 /* Host control */
71 #define CMDREG_SW 2 /* Size write */
72 #define CMDREG_SR 4 /* Size read */
73 #define CMDREG_RS 8 /* Reset interface */
74 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
75 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
76 #define IRQEN (CMDREG_DAIE)
78 #define STATUSREG_RE 1 /* read error */
79 #define STATUSREG_WE 2 /* write error */
80 #define STATUSREG_FR 0x40 /* module free */
81 #define STATUSREG_DA 0x80 /* data available */
82 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
85 #define DVB_CA_SLOTSTATE_NONE 0
86 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
87 #define DVB_CA_SLOTSTATE_RUNNING 2
88 #define DVB_CA_SLOTSTATE_INVALID 3
89 #define DVB_CA_SLOTSTATE_WAITREADY 4
90 #define DVB_CA_SLOTSTATE_VALIDATE 5
91 #define DVB_CA_SLOTSTATE_WAITFR 6
92 #define DVB_CA_SLOTSTATE_LINKINIT 7
95 /* Information on a CA slot */
98 /* current state of the CAM */
101 /* mutex used for serializing access to one CI slot */
102 struct mutex slot_lock
;
104 /* Number of CAMCHANGES that have occurred since last processing */
105 atomic_t camchange_count
;
107 /* Type of last CAMCHANGE */
110 /* base address of CAM config */
113 /* value to write into Config Control register */
116 /* if 1, the CAM supports DA IRQs */
117 u8 da_irq_supported
:1;
119 /* size of the buffer to use when talking to the CAM */
122 /* buffer for incoming packets */
123 struct dvb_ringbuffer rx_buffer
;
125 /* timer used during various states of the slot */
126 unsigned long timeout
;
129 /* Private CA-interface information */
130 struct dvb_ca_private
{
131 struct kref refcount
;
133 /* pointer back to the public data structure */
134 struct dvb_ca_en50221
*pub
;
137 struct dvb_device
*dvbdev
;
139 /* Flags describing the interface (DVB_CA_FLAG_*) */
142 /* number of slots supported by this CA interface */
143 unsigned int slot_count
;
145 /* information on each slot */
146 struct dvb_ca_slot
*slot_info
;
148 /* wait queues for read() and write() operations */
149 wait_queue_head_t wait_queue
;
151 /* PID of the monitoring thread */
152 struct task_struct
*thread
;
154 /* Flag indicating if the CA device is open */
157 /* Flag indicating the thread should wake up now */
158 unsigned int wakeup
:1;
160 /* Delay the main thread should use */
163 /* Slot to start looking for data to read from in the next user-space read operation */
166 /* mutex serializing ioctls */
167 struct mutex ioctl_mutex
;
170 static void dvb_ca_private_free(struct dvb_ca_private
*ca
)
174 dvb_free_device(ca
->dvbdev
);
175 for (i
= 0; i
< ca
->slot_count
; i
++)
176 vfree(ca
->slot_info
[i
].rx_buffer
.data
);
178 kfree(ca
->slot_info
);
182 static void dvb_ca_private_release(struct kref
*ref
)
184 struct dvb_ca_private
*ca
= container_of(ref
, struct dvb_ca_private
, refcount
);
185 dvb_ca_private_free(ca
);
188 static void dvb_ca_private_get(struct dvb_ca_private
*ca
)
190 kref_get(&ca
->refcount
);
193 static void dvb_ca_private_put(struct dvb_ca_private
*ca
)
195 kref_put(&ca
->refcount
, dvb_ca_private_release
);
198 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
199 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
200 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
204 * Safely find needle in haystack.
206 * @haystack: Buffer to look in.
207 * @hlen: Number of bytes in haystack.
208 * @needle: Buffer to find.
209 * @nlen: Number of bytes in needle.
210 * @return Pointer into haystack needle was found at, or NULL if not found.
212 static char *findstr(char * haystack
, int hlen
, char * needle
, int nlen
)
219 for (i
= 0; i
<= hlen
- nlen
; i
++) {
220 if (!strncmp(haystack
+ i
, needle
, nlen
))
229 /* ******************************************************************************** */
230 /* EN50221 physical interface functions */
234 * dvb_ca_en50221_check_camstatus - Check CAM status.
236 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private
*ca
, int slot
)
243 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
) {
244 return (atomic_read(&ca
->slot_info
[slot
].camchange_count
) != 0);
248 slot_status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, ca
->open
);
250 cam_present_now
= (slot_status
& DVB_CA_EN50221_POLL_CAM_PRESENT
) ? 1 : 0;
251 cam_changed
= (slot_status
& DVB_CA_EN50221_POLL_CAM_CHANGED
) ? 1 : 0;
253 int cam_present_old
= (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
);
254 cam_changed
= (cam_present_now
!= cam_present_old
);
258 if (!cam_present_now
) {
259 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
261 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
263 atomic_set(&ca
->slot_info
[slot
].camchange_count
, 1);
265 if ((ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) &&
266 (slot_status
& DVB_CA_EN50221_POLL_CAM_READY
)) {
267 // move to validate state if reset is completed
268 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
277 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
278 * register on a CAM interface, checking for errors and timeout.
281 * @slot: Slot on interface.
282 * @waitfor: Flags to wait for.
283 * @timeout_ms: Timeout in milliseconds.
285 * @return 0 on success, nonzero on error.
287 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
288 u8 waitfor
, int timeout_hz
)
290 unsigned long timeout
;
293 dprintk("%s\n", __func__
);
295 /* loop until timeout elapsed */
297 timeout
= jiffies
+ timeout_hz
;
299 /* read the status and check for error */
300 int res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
304 /* if we got the flags, it was successful! */
306 dprintk("%s succeeded timeout:%lu\n",
307 __func__
, jiffies
- start
);
311 /* check for timeout */
312 if (time_after(jiffies
, timeout
)) {
320 dprintk("%s failed timeout:%lu\n", __func__
, jiffies
- start
);
322 /* if we get here, we've timed out */
328 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
333 * @return 0 on success, nonzero on failure.
335 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
341 dprintk("%s\n", __func__
);
343 /* we'll be determining these during this function */
344 ca
->slot_info
[slot
].da_irq_supported
= 0;
346 /* set the host link buffer size temporarily. it will be overwritten with the
347 * real negotiated size later. */
348 ca
->slot_info
[slot
].link_buf_size
= 2;
350 /* read the buffer size from the CAM */
351 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SR
)) != 0)
353 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
/ 10)) != 0)
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
, int *tupleLength
, u8
* tuple
)
401 int _address
= *address
;
403 /* grab the next tuple length and type */
404 if ((_tupleType
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
)) < 0)
406 if (_tupleType
== 0xff) {
407 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType
);
409 *tupleType
= _tupleType
;
413 if ((_tupleLength
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ 2)) < 0)
417 dprintk("TUPLE type:0x%x length:%i\n", _tupleType
, _tupleLength
);
419 /* read in the whole tuple */
420 for (i
= 0; i
< _tupleLength
; i
++) {
421 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ (i
* 2));
422 dprintk(" 0x%02x: 0x%02x %c\n",
424 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
426 _address
+= (_tupleLength
* 2);
429 *tupleType
= _tupleType
;
430 *tupleLength
= _tupleLength
;
437 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
438 * extracting Config register, and checking it is a DVB CAM module.
443 * @return 0 on success, <0 on failure.
445 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
454 int got_cftableentry
= 0;
463 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
465 if (tupleType
!= 0x1D)
472 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
474 if (tupleType
!= 0x1C)
481 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
483 if (tupleType
!= 0x15)
489 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
490 &tupleLength
, tuple
)) < 0)
492 if (tupleType
!= 0x20)
494 if (tupleLength
!= 4)
496 manfid
= (tuple
[1] << 8) | tuple
[0];
497 devid
= (tuple
[3] << 8) | tuple
[2];
502 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
503 &tupleLength
, tuple
)) < 0)
505 if (tupleType
!= 0x1A)
510 /* extract the configbase */
512 if (tupleLength
< (3 + rasz
+ 14))
514 ca
->slot_info
[slot
].config_base
= 0;
515 for (i
= 0; i
< rasz
+ 1; i
++) {
516 ca
->slot_info
[slot
].config_base
|= (tuple
[2 + i
] << (8 * i
));
519 /* check it contains the correct DVB string */
520 dvb_str
= findstr((char *)tuple
, tupleLength
, "DVB_CI_V", 8);
523 if (tupleLength
< ((dvb_str
- (char *) tuple
) + 12))
526 /* is it a version we support? */
527 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
528 pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
529 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9],
530 dvb_str
[10], dvb_str
[11]);
534 /* process the CFTABLE_ENTRY tuples, and any after those */
535 while ((!end_chain
) && (address
< 0x1000)) {
536 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
537 &tupleLength
, tuple
)) < 0)
540 case 0x1B: // CISTPL_CFTABLE_ENTRY
541 if (tupleLength
< (2 + 11 + 17))
544 /* if we've already parsed one, just use it */
545 if (got_cftableentry
)
548 /* get the config option */
549 ca
->slot_info
[slot
].config_option
= tuple
[0] & 0x3f;
551 /* OK, check it contains the correct strings */
552 if ((findstr((char *)tuple
, tupleLength
, "DVB_HOST", 8) == NULL
) ||
553 (findstr((char *)tuple
, tupleLength
, "DVB_CI_MODULE", 13) == NULL
))
556 got_cftableentry
= 1;
559 case 0x14: // CISTPL_NO_LINK
562 case 0xFF: // CISTPL_END
566 default: /* Unknown tuple type - just skip this tuple and move to the next one */
567 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
568 tupleType
, tupleLength
);
573 if ((address
> 0x1000) || (!got_cftableentry
))
576 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
577 manfid
, devid
, ca
->slot_info
[slot
].config_base
,
578 ca
->slot_info
[slot
].config_option
);
586 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
589 * @slot: Slot containing the CAM.
591 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
595 dprintk("%s\n", __func__
);
597 /* set the config option */
598 ca
->pub
->write_attribute_mem(ca
->pub
, slot
,
599 ca
->slot_info
[slot
].config_base
,
600 ca
->slot_info
[slot
].config_option
);
603 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, ca
->slot_info
[slot
].config_base
);
604 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
605 ca
->slot_info
[slot
].config_option
, configoption
& 0x3f);
614 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
615 * interface. It reads a buffer of data from the CAM. The data can either
616 * be stored in a supplied buffer, or automatically be added to the slot's
620 * @slot: Slot to read from.
621 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
622 * the data will be added into the buffering system as a normal fragment.
623 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
625 * @return Number of bytes read, or < 0 on error
627 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
)
631 u8 buf
[HOST_LINK_BUF_SIZE
];
634 dprintk("%s\n", __func__
);
636 /* check if we have space for a link buf in the rx_buffer */
640 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
644 buf_free
= dvb_ringbuffer_free(&ca
->slot_info
[slot
].rx_buffer
);
646 if (buf_free
< (ca
->slot_info
[slot
].link_buf_size
+ DVB_RINGBUFFER_PKTHDRSIZE
)) {
652 /* check if there is data available */
653 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
655 if (!(status
& STATUSREG_DA
)) {
661 /* read the amount of data */
662 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
)) < 0)
664 bytes_read
= status
<< 8;
665 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
)) < 0)
667 bytes_read
|= status
;
669 /* check it will fit */
671 if (bytes_read
> ca
->slot_info
[slot
].link_buf_size
) {
672 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
673 ca
->dvbdev
->adapter
->num
, bytes_read
,
674 ca
->slot_info
[slot
].link_buf_size
);
675 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
679 if (bytes_read
< 2) {
680 pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
681 ca
->dvbdev
->adapter
->num
);
682 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
687 if (bytes_read
> ecount
) {
688 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
689 ca
->dvbdev
->adapter
->num
);
695 /* fill the buffer */
696 for (i
= 0; i
< bytes_read
; i
++) {
697 /* read byte and check */
698 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_DATA
)) < 0)
701 /* OK, store it in the buffer */
705 /* check for read error (RE should now be 0) */
706 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
708 if (status
& STATUSREG_RE
) {
709 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
714 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
716 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
720 dvb_ringbuffer_pkt_write(&ca
->slot_info
[slot
].rx_buffer
, buf
, bytes_read
);
722 memcpy(ebuf
, buf
, bytes_read
);
725 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
726 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
728 /* wake up readers when a last_fragment is received */
729 if ((buf
[1] & 0x80) == 0x00) {
730 wake_up_interruptible(&ca
->wait_queue
);
740 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
741 * interface. It writes a buffer of data to a CAM.
744 * @slot: Slot to write to.
745 * @ebuf: The data in this buffer is treated as a complete link-level packet to
747 * @count: Size of ebuf.
749 * @return Number of bytes written, or < 0 on error.
751 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* buf
, int bytes_write
)
756 dprintk("%s\n", __func__
);
760 if (bytes_write
> ca
->slot_info
[slot
].link_buf_size
)
763 /* it is possible we are dealing with a single buffer implementation,
764 thus if there is data available for read or if there is even a read
765 already in progress, we do nothing but awake the kernel thread to
766 process the data if necessary. */
767 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
769 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
770 if (status
& STATUSREG_DA
)
771 dvb_ca_en50221_thread_wakeup(ca
);
778 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
779 IRQEN
| CMDREG_HC
)) != 0)
782 /* check if interface is still free */
783 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
785 if (!(status
& STATUSREG_FR
)) {
786 /* it wasn't free => try again later */
791 /* send the amount of data */
792 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
, bytes_write
>> 8)) != 0)
794 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
795 bytes_write
& 0xff)) != 0)
798 /* send the buffer */
799 for (i
= 0; i
< bytes_write
; i
++) {
800 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
, buf
[i
])) != 0)
804 /* check for write error (WE should now be 0) */
805 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
807 if (status
& STATUSREG_WE
) {
808 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
812 status
= bytes_write
;
814 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
815 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
818 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
823 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
827 /* ******************************************************************************** */
828 /* EN50221 higher level functions */
832 * dvb_ca_en50221_camready_irq - A CAM has been removed => shut it down.
835 * @slot: Slot to shut down.
837 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
839 dprintk("%s\n", __func__
);
841 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
842 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
844 /* need to wake up all processes to check if they're now
845 trying to write to a defunct CAM */
846 wake_up_interruptible(&ca
->wait_queue
);
848 dprintk("Slot %i shutdown\n", slot
);
853 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
857 * dvb_ca_en50221_camready_irq - A CAMCHANGE IRQ has occurred.
860 * @slot: Slot concerned.
861 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
863 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
, int change_type
)
865 struct dvb_ca_private
*ca
= pubca
->private;
867 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
869 switch (change_type
) {
870 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
871 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
878 ca
->slot_info
[slot
].camchange_type
= change_type
;
879 atomic_inc(&ca
->slot_info
[slot
].camchange_count
);
880 dvb_ca_en50221_thread_wakeup(ca
);
882 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
886 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
889 * @slot: Slot concerned.
891 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
893 struct dvb_ca_private
*ca
= pubca
->private;
895 dprintk("CAMREADY IRQ slot:%i\n", slot
);
897 if (ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
898 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
899 dvb_ca_en50221_thread_wakeup(ca
);
905 * An FR or DA IRQ has occurred.
908 * @slot: Slot concerned.
910 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
912 struct dvb_ca_private
*ca
= pubca
->private;
915 dprintk("FR/DA IRQ slot:%i\n", slot
);
917 switch (ca
->slot_info
[slot
].slot_state
) {
918 case DVB_CA_SLOTSTATE_LINKINIT
:
919 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
920 if (flags
& STATUSREG_DA
) {
921 dprintk("CAM supports DA IRQ\n");
922 ca
->slot_info
[slot
].da_irq_supported
= 1;
926 case DVB_CA_SLOTSTATE_RUNNING
:
928 dvb_ca_en50221_thread_wakeup(ca
);
935 /* ******************************************************************************** */
936 /* EN50221 thread functions */
939 * Wake up the DVB CA thread
943 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
946 dprintk("%s\n", __func__
);
950 wake_up_process(ca
->thread
);
954 * Update the delay used by the thread.
958 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
961 int curdelay
= 100000000;
964 /* Beware of too high polling frequency, because one polling
965 * call might take several hundred milliseconds until timeout!
967 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
968 switch (ca
->slot_info
[slot
].slot_state
) {
970 case DVB_CA_SLOTSTATE_NONE
:
971 delay
= HZ
* 60; /* 60s */
972 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
973 delay
= HZ
* 5; /* 5s */
975 case DVB_CA_SLOTSTATE_INVALID
:
976 delay
= HZ
* 60; /* 60s */
977 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
978 delay
= HZ
/ 10; /* 100ms */
981 case DVB_CA_SLOTSTATE_UNINITIALISED
:
982 case DVB_CA_SLOTSTATE_WAITREADY
:
983 case DVB_CA_SLOTSTATE_VALIDATE
:
984 case DVB_CA_SLOTSTATE_WAITFR
:
985 case DVB_CA_SLOTSTATE_LINKINIT
:
986 delay
= HZ
/ 10; /* 100ms */
989 case DVB_CA_SLOTSTATE_RUNNING
:
990 delay
= HZ
* 60; /* 60s */
991 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
992 delay
= HZ
/ 10; /* 100ms */
994 if ((!ca
->slot_info
[slot
].da_irq_supported
) ||
995 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
)))
996 delay
= HZ
/ 10; /* 100ms */
1001 if (delay
< curdelay
)
1005 ca
->delay
= curdelay
;
1011 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
1013 static int dvb_ca_en50221_thread(void *data
)
1015 struct dvb_ca_private
*ca
= data
;
1022 dprintk("%s\n", __func__
);
1024 /* choose the correct initial delay */
1025 dvb_ca_en50221_thread_update_delay(ca
);
1028 while (!kthread_should_stop()) {
1029 /* sleep for a bit */
1031 set_current_state(TASK_INTERRUPTIBLE
);
1032 schedule_timeout(ca
->delay
);
1033 if (kthread_should_stop())
1038 /* go through all the slots processing them */
1039 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1041 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1043 // check the cam status + deal with CAMCHANGEs
1044 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1045 /* clear down an old CI slot if necessary */
1046 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1047 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1049 /* if a CAM is NOW present, initialise it */
1050 if (ca
->slot_info
[slot
].camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
) {
1051 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1054 /* we've handled one CAMCHANGE */
1055 dvb_ca_en50221_thread_update_delay(ca
);
1056 atomic_dec(&ca
->slot_info
[slot
].camchange_count
);
1059 // CAM state machine
1060 switch (ca
->slot_info
[slot
].slot_state
) {
1061 case DVB_CA_SLOTSTATE_NONE
:
1062 case DVB_CA_SLOTSTATE_INVALID
:
1066 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1067 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1068 ca
->pub
->slot_reset(ca
->pub
, slot
);
1069 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1072 case DVB_CA_SLOTSTATE_WAITREADY
:
1073 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1074 pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
1075 ca
->dvbdev
->adapter
->num
);
1076 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1077 dvb_ca_en50221_thread_update_delay(ca
);
1080 // no other action needed; will automatically change state when ready
1083 case DVB_CA_SLOTSTATE_VALIDATE
:
1084 if (dvb_ca_en50221_parse_attributes(ca
, slot
) != 0) {
1085 /* we need this extra check for annoying interfaces like the budget-av */
1086 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1087 (ca
->pub
->poll_slot_status
)) {
1088 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1089 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1090 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1091 dvb_ca_en50221_thread_update_delay(ca
);
1096 pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1097 ca
->dvbdev
->adapter
->num
);
1098 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1099 dvb_ca_en50221_thread_update_delay(ca
);
1102 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1103 pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1104 ca
->dvbdev
->adapter
->num
);
1105 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1106 dvb_ca_en50221_thread_update_delay(ca
);
1109 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1110 CTRLIF_COMMAND
, CMDREG_RS
) != 0) {
1111 pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
1112 ca
->dvbdev
->adapter
->num
);
1113 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1114 dvb_ca_en50221_thread_update_delay(ca
);
1117 dprintk("DVB CAM validated successfully\n");
1119 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1120 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1124 case DVB_CA_SLOTSTATE_WAITFR
:
1125 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1126 pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1127 ca
->dvbdev
->adapter
->num
);
1128 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1129 dvb_ca_en50221_thread_update_delay(ca
);
1133 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1134 if (flags
& STATUSREG_FR
) {
1135 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1140 case DVB_CA_SLOTSTATE_LINKINIT
:
1141 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1142 /* we need this extra check for annoying interfaces like the budget-av */
1143 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1144 (ca
->pub
->poll_slot_status
)) {
1145 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1146 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1147 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1148 dvb_ca_en50221_thread_update_delay(ca
);
1153 pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
1154 ca
->dvbdev
->adapter
->num
);
1155 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1156 dvb_ca_en50221_thread_update_delay(ca
);
1160 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1161 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1162 if (rxbuf
== NULL
) {
1163 pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1164 ca
->dvbdev
->adapter
->num
);
1165 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1166 dvb_ca_en50221_thread_update_delay(ca
);
1169 dvb_ringbuffer_init(&ca
->slot_info
[slot
].rx_buffer
, rxbuf
, RX_BUFFER_SIZE
);
1172 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1173 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1174 dvb_ca_en50221_thread_update_delay(ca
);
1175 pr_err("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
1176 ca
->dvbdev
->adapter
->num
);
1179 case DVB_CA_SLOTSTATE_RUNNING
:
1183 // poll slots for data
1185 while ((status
= dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0)) > 0) {
1189 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1190 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1191 // we dont want to sleep on the next iteration so we can handle the cam change
1196 /* check if we've hit our limit this time */
1197 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1198 // dont sleep; there is likely to be more data to read
1206 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1215 /* ******************************************************************************** */
1216 /* EN50221 IO interface functions */
1219 * Real ioctl implementation.
1220 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1222 * @inode: Inode concerned.
1223 * @file: File concerned.
1224 * @cmd: IOCTL command.
1225 * @arg: Associated argument.
1227 * @return 0 on success, <0 on error.
1229 static int dvb_ca_en50221_io_do_ioctl(struct file
*file
,
1230 unsigned int cmd
, void *parg
)
1232 struct dvb_device
*dvbdev
= file
->private_data
;
1233 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1237 dprintk("%s\n", __func__
);
1239 if (mutex_lock_interruptible(&ca
->ioctl_mutex
))
1240 return -ERESTARTSYS
;
1244 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1245 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1246 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1247 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1248 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1249 dvb_ca_en50221_camchange_irq(ca
->pub
,
1251 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1253 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1255 ca
->next_read_slot
= 0;
1256 dvb_ca_en50221_thread_wakeup(ca
);
1260 struct ca_caps
*caps
= parg
;
1262 caps
->slot_num
= ca
->slot_count
;
1263 caps
->slot_type
= CA_CI_LINK
;
1264 caps
->descr_num
= 0;
1265 caps
->descr_type
= 0;
1269 case CA_GET_SLOT_INFO
: {
1270 struct ca_slot_info
*info
= parg
;
1272 if ((info
->num
> ca
->slot_count
) || (info
->num
< 0)) {
1277 info
->type
= CA_CI_LINK
;
1279 if ((ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1280 && (ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_INVALID
)) {
1281 info
->flags
= CA_CI_MODULE_PRESENT
;
1283 if (ca
->slot_info
[info
->num
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1284 info
->flags
|= CA_CI_MODULE_READY
;
1295 mutex_unlock(&ca
->ioctl_mutex
);
1301 * Wrapper for ioctl implementation.
1303 * @inode: Inode concerned.
1304 * @file: File concerned.
1305 * @cmd: IOCTL command.
1306 * @arg: Associated argument.
1308 * @return 0 on success, <0 on error.
1310 static long dvb_ca_en50221_io_ioctl(struct file
*file
,
1311 unsigned int cmd
, unsigned long arg
)
1313 return dvb_usercopy(file
, cmd
, arg
, dvb_ca_en50221_io_do_ioctl
);
1318 * Implementation of write() syscall.
1320 * @file: File structure.
1321 * @buf: Source buffer.
1322 * @count: Size of source buffer.
1323 * @ppos: Position in file (ignored).
1325 * @return Number of bytes read, or <0 on error.
1327 static ssize_t
dvb_ca_en50221_io_write(struct file
*file
,
1328 const char __user
* buf
, size_t count
, loff_t
* ppos
)
1330 struct dvb_device
*dvbdev
= file
->private_data
;
1331 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1332 u8 slot
, connection_id
;
1334 u8 fragbuf
[HOST_LINK_BUF_SIZE
];
1337 unsigned long timeout
;
1340 dprintk("%s\n", __func__
);
1342 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1346 /* extract slot & connection id */
1347 if (copy_from_user(&slot
, buf
, 1))
1349 if (copy_from_user(&connection_id
, buf
+ 1, 1))
1354 /* check if the slot is actually running */
1355 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1358 /* fragment the packets & store in the buffer */
1359 while (fragpos
< count
) {
1360 fraglen
= ca
->slot_info
[slot
].link_buf_size
- 2;
1363 if (fraglen
> HOST_LINK_BUF_SIZE
- 2)
1364 fraglen
= HOST_LINK_BUF_SIZE
- 2;
1365 if ((count
- fragpos
) < fraglen
)
1366 fraglen
= count
- fragpos
;
1368 fragbuf
[0] = connection_id
;
1369 fragbuf
[1] = ((fragpos
+ fraglen
) < count
) ? 0x80 : 0x00;
1370 status
= copy_from_user(fragbuf
+ 2, buf
+ fragpos
, fraglen
);
1376 timeout
= jiffies
+ HZ
/ 2;
1378 while (!time_after(jiffies
, timeout
)) {
1379 /* check the CAM hasn't been removed/reset in the meantime */
1380 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
) {
1385 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1386 status
= dvb_ca_en50221_write_data(ca
, slot
, fragbuf
, fraglen
+ 2);
1387 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1388 if (status
== (fraglen
+ 2)) {
1392 if (status
!= -EAGAIN
)
1412 * Condition for waking up in dvb_ca_en50221_io_read_condition
1414 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private
*ca
,
1415 int *result
, int *_slot
)
1421 int connection_id
= -1;
1425 slot
= ca
->next_read_slot
;
1426 while ((slot_count
< ca
->slot_count
) && (!found
)) {
1427 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1430 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1434 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1436 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1437 if (connection_id
== -1)
1438 connection_id
= hdr
[0];
1439 if ((hdr
[0] == connection_id
) && ((hdr
[1] & 0x80) == 0)) {
1445 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1449 slot
= (slot
+ 1) % ca
->slot_count
;
1453 ca
->next_read_slot
= slot
;
1459 * Implementation of read() syscall.
1461 * @file: File structure.
1462 * @buf: Destination buffer.
1463 * @count: Size of destination buffer.
1464 * @ppos: Position in file (ignored).
1466 * @return Number of bytes read, or <0 on error.
1468 static ssize_t
dvb_ca_en50221_io_read(struct file
*file
, char __user
* buf
,
1469 size_t count
, loff_t
* ppos
)
1471 struct dvb_device
*dvbdev
= file
->private_data
;
1472 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1477 int connection_id
= -1;
1479 int last_fragment
= 0;
1484 dprintk("%s\n", __func__
);
1486 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1490 /* wait for some data */
1491 if ((status
= dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
)) == 0) {
1493 /* if we're in nonblocking mode, exit immediately */
1494 if (file
->f_flags
& O_NONBLOCK
)
1495 return -EWOULDBLOCK
;
1497 /* wait for some data */
1498 status
= wait_event_interruptible(ca
->wait_queue
,
1499 dvb_ca_en50221_io_read_condition
1500 (ca
, &result
, &slot
));
1502 if ((status
< 0) || (result
< 0)) {
1508 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1512 pr_err("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n",
1513 ca
->dvbdev
->adapter
->num
);
1518 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1519 if (connection_id
== -1)
1520 connection_id
= hdr
[0];
1521 if (hdr
[0] == connection_id
) {
1522 if (pktlen
< count
) {
1523 if ((pktlen
+ fraglen
- 2) > count
) {
1524 fraglen
= count
- pktlen
;
1529 if ((status
= dvb_ringbuffer_pkt_read_user(&ca
->slot_info
[slot
].rx_buffer
, idx
, 2,
1530 buf
+ pktlen
, fraglen
)) < 0) {
1536 if ((hdr
[1] & 0x80) == 0)
1541 idx2
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1543 dvb_ringbuffer_pkt_dispose(&ca
->slot_info
[slot
].rx_buffer
, idx
);
1546 } while (!last_fragment
);
1549 hdr
[1] = connection_id
;
1550 status
= copy_to_user(buf
, hdr
, 2);
1563 * Implementation of file open syscall.
1565 * @inode: Inode concerned.
1566 * @file: File concerned.
1568 * @return 0 on success, <0 on failure.
1570 static int dvb_ca_en50221_io_open(struct inode
*inode
, struct file
*file
)
1572 struct dvb_device
*dvbdev
= file
->private_data
;
1573 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1577 dprintk("%s\n", __func__
);
1579 if (!try_module_get(ca
->pub
->owner
))
1582 err
= dvb_generic_open(inode
, file
);
1584 module_put(ca
->pub
->owner
);
1588 for (i
= 0; i
< ca
->slot_count
; i
++) {
1590 if (ca
->slot_info
[i
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1591 if (ca
->slot_info
[i
].rx_buffer
.data
!= NULL
) {
1592 /* it is safe to call this here without locks because
1593 * ca->open == 0. Data is not read in this case */
1594 dvb_ringbuffer_flush(&ca
->slot_info
[i
].rx_buffer
);
1600 dvb_ca_en50221_thread_update_delay(ca
);
1601 dvb_ca_en50221_thread_wakeup(ca
);
1603 dvb_ca_private_get(ca
);
1610 * Implementation of file close syscall.
1612 * @inode: Inode concerned.
1613 * @file: File concerned.
1615 * @return 0 on success, <0 on failure.
1617 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1619 struct dvb_device
*dvbdev
= file
->private_data
;
1620 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1623 dprintk("%s\n", __func__
);
1625 /* mark the CA device as closed */
1627 dvb_ca_en50221_thread_update_delay(ca
);
1629 err
= dvb_generic_release(inode
, file
);
1631 module_put(ca
->pub
->owner
);
1633 dvb_ca_private_put(ca
);
1640 * Implementation of poll() syscall.
1642 * @file: File concerned.
1643 * @wait: poll wait table.
1645 * @return Standard poll mask.
1647 static unsigned int dvb_ca_en50221_io_poll(struct file
*file
, poll_table
* wait
)
1649 struct dvb_device
*dvbdev
= file
->private_data
;
1650 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1651 unsigned int mask
= 0;
1655 dprintk("%s\n", __func__
);
1657 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1661 /* if there is something, return now */
1665 /* wait for something to happen */
1666 poll_wait(file
, &ca
->wait_queue
, wait
);
1668 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1674 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1677 static const struct file_operations dvb_ca_fops
= {
1678 .owner
= THIS_MODULE
,
1679 .read
= dvb_ca_en50221_io_read
,
1680 .write
= dvb_ca_en50221_io_write
,
1681 .unlocked_ioctl
= dvb_ca_en50221_io_ioctl
,
1682 .open
= dvb_ca_en50221_io_open
,
1683 .release
= dvb_ca_en50221_io_release
,
1684 .poll
= dvb_ca_en50221_io_poll
,
1685 .llseek
= noop_llseek
,
1688 static const struct dvb_device dvbdev_ca
= {
1693 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1694 .name
= "dvb-ca-en50221",
1696 .fops
= &dvb_ca_fops
,
1699 /* ******************************************************************************** */
1700 /* Initialisation/shutdown functions */
1704 * Initialise a new DVB CA EN50221 interface device.
1706 * @dvb_adapter: DVB adapter to attach the new CA device to.
1707 * @ca: The dvb_ca instance.
1708 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1709 * @slot_count: Number of slots supported.
1711 * @return 0 on success, nonzero on failure
1713 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1714 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1717 struct dvb_ca_private
*ca
= NULL
;
1720 dprintk("%s\n", __func__
);
1725 /* initialise the system data */
1726 if ((ca
= kzalloc(sizeof(struct dvb_ca_private
), GFP_KERNEL
)) == NULL
) {
1730 kref_init(&ca
->refcount
);
1733 ca
->slot_count
= slot_count
;
1734 if ((ca
->slot_info
= kcalloc(slot_count
, sizeof(struct dvb_ca_slot
), GFP_KERNEL
)) == NULL
) {
1738 init_waitqueue_head(&ca
->wait_queue
);
1741 ca
->next_read_slot
= 0;
1742 pubca
->private = ca
;
1744 /* register the DVB device */
1745 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
, DVB_DEVICE_CA
, 0);
1747 goto free_slot_info
;
1749 /* now initialise each slot */
1750 for (i
= 0; i
< slot_count
; i
++) {
1751 memset(&ca
->slot_info
[i
], 0, sizeof(struct dvb_ca_slot
));
1752 ca
->slot_info
[i
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1753 atomic_set(&ca
->slot_info
[i
].camchange_count
, 0);
1754 ca
->slot_info
[i
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1755 mutex_init(&ca
->slot_info
[i
].slot_lock
);
1758 mutex_init(&ca
->ioctl_mutex
);
1760 if (signal_pending(current
)) {
1762 goto unregister_device
;
1766 /* create a kthread for monitoring this CA device */
1767 ca
->thread
= kthread_run(dvb_ca_en50221_thread
, ca
, "kdvb-ca-%i:%i",
1768 ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
1769 if (IS_ERR(ca
->thread
)) {
1770 ret
= PTR_ERR(ca
->thread
);
1771 pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
1773 goto unregister_device
;
1778 dvb_unregister_device(ca
->dvbdev
);
1780 kfree(ca
->slot_info
);
1784 pubca
->private = NULL
;
1787 EXPORT_SYMBOL(dvb_ca_en50221_release
);
1792 * Release a DVB CA EN50221 interface device.
1794 * @ca_dev: The dvb_device_t instance for the CA device.
1795 * @ca: The associated dvb_ca instance.
1797 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1799 struct dvb_ca_private
*ca
= pubca
->private;
1802 dprintk("%s\n", __func__
);
1804 /* shutdown the thread if there was one */
1805 kthread_stop(ca
->thread
);
1807 for (i
= 0; i
< ca
->slot_count
; i
++) {
1808 dvb_ca_en50221_slot_shutdown(ca
, i
);
1810 dvb_remove_device(ca
->dvbdev
);
1811 dvb_ca_private_put(ca
);
1812 pubca
->private = NULL
;