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.h>
38 #include <linux/kthread.h>
40 #include "dvb_ca_en50221.h"
41 #include "dvb_ringbuffer.h"
43 static int dvb_ca_en50221_debug
;
45 module_param_named(cam_debug
, dvb_ca_en50221_debug
, int, 0644);
46 MODULE_PARM_DESC(cam_debug
, "enable verbose debug messages");
48 #define dprintk(fmt, arg...) do { \
49 if (dvb_ca_en50221_debug) \
50 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg);\
53 #define INIT_TIMEOUT_SECS 10
55 #define HOST_LINK_BUF_SIZE 0x200
57 #define RX_BUFFER_SIZE 65535
59 #define MAX_RX_PACKETS_PER_ITERATION 10
62 #define CTRLIF_COMMAND 1
63 #define CTRLIF_STATUS 1
64 #define CTRLIF_SIZE_LOW 2
65 #define CTRLIF_SIZE_HIGH 3
67 #define CMDREG_HC 1 /* Host control */
68 #define CMDREG_SW 2 /* Size write */
69 #define CMDREG_SR 4 /* Size read */
70 #define CMDREG_RS 8 /* Reset interface */
71 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
72 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
73 #define IRQEN (CMDREG_DAIE)
75 #define STATUSREG_RE 1 /* read error */
76 #define STATUSREG_WE 2 /* write error */
77 #define STATUSREG_FR 0x40 /* module free */
78 #define STATUSREG_DA 0x80 /* data available */
79 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
82 #define DVB_CA_SLOTSTATE_NONE 0
83 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
84 #define DVB_CA_SLOTSTATE_RUNNING 2
85 #define DVB_CA_SLOTSTATE_INVALID 3
86 #define DVB_CA_SLOTSTATE_WAITREADY 4
87 #define DVB_CA_SLOTSTATE_VALIDATE 5
88 #define DVB_CA_SLOTSTATE_WAITFR 6
89 #define DVB_CA_SLOTSTATE_LINKINIT 7
92 /* Information on a CA slot */
95 /* current state of the CAM */
98 /* mutex used for serializing access to one CI slot */
99 struct mutex slot_lock
;
101 /* Number of CAMCHANGES that have occurred since last processing */
102 atomic_t camchange_count
;
104 /* Type of last CAMCHANGE */
107 /* base address of CAM config */
110 /* value to write into Config Control register */
113 /* if 1, the CAM supports DA IRQs */
114 u8 da_irq_supported
:1;
116 /* size of the buffer to use when talking to the CAM */
119 /* buffer for incoming packets */
120 struct dvb_ringbuffer rx_buffer
;
122 /* timer used during various states of the slot */
123 unsigned long timeout
;
126 /* Private CA-interface information */
127 struct dvb_ca_private
{
128 struct kref refcount
;
130 /* pointer back to the public data structure */
131 struct dvb_ca_en50221
*pub
;
134 struct dvb_device
*dvbdev
;
136 /* Flags describing the interface (DVB_CA_FLAG_*) */
139 /* number of slots supported by this CA interface */
140 unsigned int slot_count
;
142 /* information on each slot */
143 struct dvb_ca_slot
*slot_info
;
145 /* wait queues for read() and write() operations */
146 wait_queue_head_t wait_queue
;
148 /* PID of the monitoring thread */
149 struct task_struct
*thread
;
151 /* Flag indicating if the CA device is open */
154 /* Flag indicating the thread should wake up now */
155 unsigned int wakeup
:1;
157 /* Delay the main thread should use */
160 /* Slot to start looking for data to read from in the next user-space read operation */
163 /* mutex serializing ioctls */
164 struct mutex ioctl_mutex
;
167 static void dvb_ca_private_free(struct dvb_ca_private
*ca
)
171 dvb_free_device(ca
->dvbdev
);
172 for (i
= 0; i
< ca
->slot_count
; i
++)
173 vfree(ca
->slot_info
[i
].rx_buffer
.data
);
175 kfree(ca
->slot_info
);
179 static void dvb_ca_private_release(struct kref
*ref
)
181 struct dvb_ca_private
*ca
= container_of(ref
, struct dvb_ca_private
, refcount
);
182 dvb_ca_private_free(ca
);
185 static void dvb_ca_private_get(struct dvb_ca_private
*ca
)
187 kref_get(&ca
->refcount
);
190 static void dvb_ca_private_put(struct dvb_ca_private
*ca
)
192 kref_put(&ca
->refcount
, dvb_ca_private_release
);
195 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
196 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
197 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
201 * Safely find needle in haystack.
203 * @haystack: Buffer to look in.
204 * @hlen: Number of bytes in haystack.
205 * @needle: Buffer to find.
206 * @nlen: Number of bytes in needle.
207 * @return Pointer into haystack needle was found at, or NULL if not found.
209 static char *findstr(char * haystack
, int hlen
, char * needle
, int nlen
)
216 for (i
= 0; i
<= hlen
- nlen
; i
++) {
217 if (!strncmp(haystack
+ i
, needle
, nlen
))
226 /* ******************************************************************************** */
227 /* EN50221 physical interface functions */
231 * dvb_ca_en50221_check_camstatus - Check CAM status.
233 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private
*ca
, int slot
)
240 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
) {
241 return (atomic_read(&ca
->slot_info
[slot
].camchange_count
) != 0);
245 slot_status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, ca
->open
);
247 cam_present_now
= (slot_status
& DVB_CA_EN50221_POLL_CAM_PRESENT
) ? 1 : 0;
248 cam_changed
= (slot_status
& DVB_CA_EN50221_POLL_CAM_CHANGED
) ? 1 : 0;
250 int cam_present_old
= (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
);
251 cam_changed
= (cam_present_now
!= cam_present_old
);
255 if (!cam_present_now
) {
256 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
258 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
260 atomic_set(&ca
->slot_info
[slot
].camchange_count
, 1);
262 if ((ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) &&
263 (slot_status
& DVB_CA_EN50221_POLL_CAM_READY
)) {
264 // move to validate state if reset is completed
265 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
274 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
275 * register on a CAM interface, checking for errors and timeout.
278 * @slot: Slot on interface.
279 * @waitfor: Flags to wait for.
280 * @timeout_ms: Timeout in milliseconds.
282 * @return 0 on success, nonzero on error.
284 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
285 u8 waitfor
, int timeout_hz
)
287 unsigned long timeout
;
290 dprintk("%s\n", __func__
);
292 /* loop until timeout elapsed */
294 timeout
= jiffies
+ timeout_hz
;
296 /* read the status and check for error */
297 int res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
301 /* if we got the flags, it was successful! */
303 dprintk("%s succeeded timeout:%lu\n",
304 __func__
, jiffies
- start
);
308 /* check for timeout */
309 if (time_after(jiffies
, timeout
)) {
317 dprintk("%s failed timeout:%lu\n", __func__
, jiffies
- start
);
319 /* if we get here, we've timed out */
325 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
330 * @return 0 on success, nonzero on failure.
332 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
338 dprintk("%s\n", __func__
);
340 /* we'll be determining these during this function */
341 ca
->slot_info
[slot
].da_irq_supported
= 0;
343 /* set the host link buffer size temporarily. it will be overwritten with the
344 * real negotiated size later. */
345 ca
->slot_info
[slot
].link_buf_size
= 2;
347 /* read the buffer size from the CAM */
348 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SR
)) != 0)
350 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
/ 10)) != 0)
352 if ((ret
= dvb_ca_en50221_read_data(ca
, slot
, buf
, 2)) != 2)
354 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
357 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
358 buf_size
= (buf
[0] << 8) | buf
[1];
359 if (buf_size
> HOST_LINK_BUF_SIZE
)
360 buf_size
= HOST_LINK_BUF_SIZE
;
361 ca
->slot_info
[slot
].link_buf_size
= buf_size
;
362 buf
[0] = buf_size
>> 8;
363 buf
[1] = buf_size
& 0xff;
364 dprintk("Chosen link buffer size of %i\n", buf_size
);
366 /* write the buffer size to the CAM */
367 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SW
)) != 0)
369 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_FR
, HZ
/ 10)) != 0)
371 if ((ret
= dvb_ca_en50221_write_data(ca
, slot
, buf
, 2)) != 2)
373 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
381 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
385 * @address: Address to read from. Updated.
386 * @tupleType: Tuple id byte. Updated.
387 * @tupleLength: Tuple length. Updated.
388 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
390 * @return 0 on success, nonzero on error.
392 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private
*ca
, int slot
,
393 int *address
, int *tupleType
, int *tupleLength
, u8
* tuple
)
398 int _address
= *address
;
400 /* grab the next tuple length and type */
401 if ((_tupleType
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
)) < 0)
403 if (_tupleType
== 0xff) {
404 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType
);
406 *tupleType
= _tupleType
;
410 if ((_tupleLength
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ 2)) < 0)
414 dprintk("TUPLE type:0x%x length:%i\n", _tupleType
, _tupleLength
);
416 /* read in the whole tuple */
417 for (i
= 0; i
< _tupleLength
; i
++) {
418 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ (i
* 2));
419 dprintk(" 0x%02x: 0x%02x %c\n",
421 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
423 _address
+= (_tupleLength
* 2);
426 *tupleType
= _tupleType
;
427 *tupleLength
= _tupleLength
;
434 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
435 * extracting Config register, and checking it is a DVB CAM module.
440 * @return 0 on success, <0 on failure.
442 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
451 int got_cftableentry
= 0;
460 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
462 if (tupleType
!= 0x1D)
469 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
471 if (tupleType
!= 0x1C)
478 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
480 if (tupleType
!= 0x15)
486 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
487 &tupleLength
, tuple
)) < 0)
489 if (tupleType
!= 0x20)
491 if (tupleLength
!= 4)
493 manfid
= (tuple
[1] << 8) | tuple
[0];
494 devid
= (tuple
[3] << 8) | tuple
[2];
499 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
500 &tupleLength
, tuple
)) < 0)
502 if (tupleType
!= 0x1A)
507 /* extract the configbase */
509 if (tupleLength
< (3 + rasz
+ 14))
511 ca
->slot_info
[slot
].config_base
= 0;
512 for (i
= 0; i
< rasz
+ 1; i
++) {
513 ca
->slot_info
[slot
].config_base
|= (tuple
[2 + i
] << (8 * i
));
516 /* check it contains the correct DVB string */
517 dvb_str
= findstr((char *)tuple
, tupleLength
, "DVB_CI_V", 8);
520 if (tupleLength
< ((dvb_str
- (char *) tuple
) + 12))
523 /* is it a version we support? */
524 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
525 pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
526 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9],
527 dvb_str
[10], dvb_str
[11]);
531 /* process the CFTABLE_ENTRY tuples, and any after those */
532 while ((!end_chain
) && (address
< 0x1000)) {
533 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
534 &tupleLength
, tuple
)) < 0)
537 case 0x1B: // CISTPL_CFTABLE_ENTRY
538 if (tupleLength
< (2 + 11 + 17))
541 /* if we've already parsed one, just use it */
542 if (got_cftableentry
)
545 /* get the config option */
546 ca
->slot_info
[slot
].config_option
= tuple
[0] & 0x3f;
548 /* OK, check it contains the correct strings */
549 if ((findstr((char *)tuple
, tupleLength
, "DVB_HOST", 8) == NULL
) ||
550 (findstr((char *)tuple
, tupleLength
, "DVB_CI_MODULE", 13) == NULL
))
553 got_cftableentry
= 1;
556 case 0x14: // CISTPL_NO_LINK
559 case 0xFF: // CISTPL_END
563 default: /* Unknown tuple type - just skip this tuple and move to the next one */
564 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
565 tupleType
, tupleLength
);
570 if ((address
> 0x1000) || (!got_cftableentry
))
573 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
574 manfid
, devid
, ca
->slot_info
[slot
].config_base
,
575 ca
->slot_info
[slot
].config_option
);
583 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
586 * @slot: Slot containing the CAM.
588 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
592 dprintk("%s\n", __func__
);
594 /* set the config option */
595 ca
->pub
->write_attribute_mem(ca
->pub
, slot
,
596 ca
->slot_info
[slot
].config_base
,
597 ca
->slot_info
[slot
].config_option
);
600 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, ca
->slot_info
[slot
].config_base
);
601 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
602 ca
->slot_info
[slot
].config_option
, configoption
& 0x3f);
611 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
612 * interface. It reads a buffer of data from the CAM. The data can either
613 * be stored in a supplied buffer, or automatically be added to the slot's
617 * @slot: Slot to read from.
618 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
619 * the data will be added into the buffering system as a normal fragment.
620 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
622 * @return Number of bytes read, or < 0 on error
624 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
)
628 u8 buf
[HOST_LINK_BUF_SIZE
];
631 dprintk("%s\n", __func__
);
633 /* check if we have space for a link buf in the rx_buffer */
637 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
641 buf_free
= dvb_ringbuffer_free(&ca
->slot_info
[slot
].rx_buffer
);
643 if (buf_free
< (ca
->slot_info
[slot
].link_buf_size
+ DVB_RINGBUFFER_PKTHDRSIZE
)) {
649 /* check if there is data available */
650 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
652 if (!(status
& STATUSREG_DA
)) {
658 /* read the amount of data */
659 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
)) < 0)
661 bytes_read
= status
<< 8;
662 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
)) < 0)
664 bytes_read
|= status
;
666 /* check it will fit */
668 if (bytes_read
> ca
->slot_info
[slot
].link_buf_size
) {
669 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
670 ca
->dvbdev
->adapter
->num
, bytes_read
,
671 ca
->slot_info
[slot
].link_buf_size
);
672 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
676 if (bytes_read
< 2) {
677 pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
678 ca
->dvbdev
->adapter
->num
);
679 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
684 if (bytes_read
> ecount
) {
685 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
686 ca
->dvbdev
->adapter
->num
);
692 /* fill the buffer */
693 for (i
= 0; i
< bytes_read
; i
++) {
694 /* read byte and check */
695 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_DATA
)) < 0)
698 /* OK, store it in the buffer */
702 /* check for read error (RE should now be 0) */
703 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
705 if (status
& STATUSREG_RE
) {
706 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
711 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
713 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
717 dvb_ringbuffer_pkt_write(&ca
->slot_info
[slot
].rx_buffer
, buf
, bytes_read
);
719 memcpy(ebuf
, buf
, bytes_read
);
722 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
723 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
725 /* wake up readers when a last_fragment is received */
726 if ((buf
[1] & 0x80) == 0x00) {
727 wake_up_interruptible(&ca
->wait_queue
);
737 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
738 * interface. It writes a buffer of data to a CAM.
741 * @slot: Slot to write to.
742 * @ebuf: The data in this buffer is treated as a complete link-level packet to
744 * @count: Size of ebuf.
746 * @return Number of bytes written, or < 0 on error.
748 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* buf
, int bytes_write
)
753 dprintk("%s\n", __func__
);
757 if (bytes_write
> ca
->slot_info
[slot
].link_buf_size
)
760 /* it is possible we are dealing with a single buffer implementation,
761 thus if there is data available for read or if there is even a read
762 already in progress, we do nothing but awake the kernel thread to
763 process the data if necessary. */
764 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
766 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
767 if (status
& STATUSREG_DA
)
768 dvb_ca_en50221_thread_wakeup(ca
);
775 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
776 IRQEN
| CMDREG_HC
)) != 0)
779 /* check if interface is still free */
780 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
782 if (!(status
& STATUSREG_FR
)) {
783 /* it wasn't free => try again later */
788 /* send the amount of data */
789 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
, bytes_write
>> 8)) != 0)
791 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
792 bytes_write
& 0xff)) != 0)
795 /* send the buffer */
796 for (i
= 0; i
< bytes_write
; i
++) {
797 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
, buf
[i
])) != 0)
801 /* check for write error (WE should now be 0) */
802 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
804 if (status
& STATUSREG_WE
) {
805 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
809 status
= bytes_write
;
811 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
812 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
815 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
820 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
824 /* ******************************************************************************** */
825 /* EN50221 higher level functions */
829 * dvb_ca_en50221_camready_irq - A CAM has been removed => shut it down.
832 * @slot: Slot to shut down.
834 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
836 dprintk("%s\n", __func__
);
838 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
839 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
841 /* need to wake up all processes to check if they're now
842 trying to write to a defunct CAM */
843 wake_up_interruptible(&ca
->wait_queue
);
845 dprintk("Slot %i shutdown\n", slot
);
850 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
854 * dvb_ca_en50221_camready_irq - A CAMCHANGE IRQ has occurred.
857 * @slot: Slot concerned.
858 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
860 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
, int change_type
)
862 struct dvb_ca_private
*ca
= pubca
->private;
864 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
866 switch (change_type
) {
867 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
868 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
875 ca
->slot_info
[slot
].camchange_type
= change_type
;
876 atomic_inc(&ca
->slot_info
[slot
].camchange_count
);
877 dvb_ca_en50221_thread_wakeup(ca
);
879 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
883 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
886 * @slot: Slot concerned.
888 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
890 struct dvb_ca_private
*ca
= pubca
->private;
892 dprintk("CAMREADY IRQ slot:%i\n", slot
);
894 if (ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
895 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
896 dvb_ca_en50221_thread_wakeup(ca
);
902 * An FR or DA IRQ has occurred.
905 * @slot: Slot concerned.
907 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
909 struct dvb_ca_private
*ca
= pubca
->private;
912 dprintk("FR/DA IRQ slot:%i\n", slot
);
914 switch (ca
->slot_info
[slot
].slot_state
) {
915 case DVB_CA_SLOTSTATE_LINKINIT
:
916 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
917 if (flags
& STATUSREG_DA
) {
918 dprintk("CAM supports DA IRQ\n");
919 ca
->slot_info
[slot
].da_irq_supported
= 1;
923 case DVB_CA_SLOTSTATE_RUNNING
:
925 dvb_ca_en50221_thread_wakeup(ca
);
932 /* ******************************************************************************** */
933 /* EN50221 thread functions */
936 * Wake up the DVB CA thread
940 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
943 dprintk("%s\n", __func__
);
947 wake_up_process(ca
->thread
);
951 * Update the delay used by the thread.
955 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
958 int curdelay
= 100000000;
961 /* Beware of too high polling frequency, because one polling
962 * call might take several hundred milliseconds until timeout!
964 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
965 switch (ca
->slot_info
[slot
].slot_state
) {
967 case DVB_CA_SLOTSTATE_NONE
:
968 delay
= HZ
* 60; /* 60s */
969 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
970 delay
= HZ
* 5; /* 5s */
972 case DVB_CA_SLOTSTATE_INVALID
:
973 delay
= HZ
* 60; /* 60s */
974 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
975 delay
= HZ
/ 10; /* 100ms */
978 case DVB_CA_SLOTSTATE_UNINITIALISED
:
979 case DVB_CA_SLOTSTATE_WAITREADY
:
980 case DVB_CA_SLOTSTATE_VALIDATE
:
981 case DVB_CA_SLOTSTATE_WAITFR
:
982 case DVB_CA_SLOTSTATE_LINKINIT
:
983 delay
= HZ
/ 10; /* 100ms */
986 case DVB_CA_SLOTSTATE_RUNNING
:
987 delay
= HZ
* 60; /* 60s */
988 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
989 delay
= HZ
/ 10; /* 100ms */
991 if ((!ca
->slot_info
[slot
].da_irq_supported
) ||
992 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
)))
993 delay
= HZ
/ 10; /* 100ms */
998 if (delay
< curdelay
)
1002 ca
->delay
= curdelay
;
1008 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
1010 static int dvb_ca_en50221_thread(void *data
)
1012 struct dvb_ca_private
*ca
= data
;
1019 dprintk("%s\n", __func__
);
1021 /* choose the correct initial delay */
1022 dvb_ca_en50221_thread_update_delay(ca
);
1025 while (!kthread_should_stop()) {
1026 /* sleep for a bit */
1028 set_current_state(TASK_INTERRUPTIBLE
);
1029 schedule_timeout(ca
->delay
);
1030 if (kthread_should_stop())
1035 /* go through all the slots processing them */
1036 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1038 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1040 // check the cam status + deal with CAMCHANGEs
1041 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1042 /* clear down an old CI slot if necessary */
1043 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1044 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1046 /* if a CAM is NOW present, initialise it */
1047 if (ca
->slot_info
[slot
].camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
) {
1048 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1051 /* we've handled one CAMCHANGE */
1052 dvb_ca_en50221_thread_update_delay(ca
);
1053 atomic_dec(&ca
->slot_info
[slot
].camchange_count
);
1056 // CAM state machine
1057 switch (ca
->slot_info
[slot
].slot_state
) {
1058 case DVB_CA_SLOTSTATE_NONE
:
1059 case DVB_CA_SLOTSTATE_INVALID
:
1063 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1064 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1065 ca
->pub
->slot_reset(ca
->pub
, slot
);
1066 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1069 case DVB_CA_SLOTSTATE_WAITREADY
:
1070 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1071 pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
1072 ca
->dvbdev
->adapter
->num
);
1073 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1074 dvb_ca_en50221_thread_update_delay(ca
);
1077 // no other action needed; will automatically change state when ready
1080 case DVB_CA_SLOTSTATE_VALIDATE
:
1081 if (dvb_ca_en50221_parse_attributes(ca
, slot
) != 0) {
1082 /* we need this extra check for annoying interfaces like the budget-av */
1083 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1084 (ca
->pub
->poll_slot_status
)) {
1085 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1086 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1087 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1088 dvb_ca_en50221_thread_update_delay(ca
);
1093 pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1094 ca
->dvbdev
->adapter
->num
);
1095 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1096 dvb_ca_en50221_thread_update_delay(ca
);
1099 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1100 pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1101 ca
->dvbdev
->adapter
->num
);
1102 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1103 dvb_ca_en50221_thread_update_delay(ca
);
1106 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1107 CTRLIF_COMMAND
, CMDREG_RS
) != 0) {
1108 pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
1109 ca
->dvbdev
->adapter
->num
);
1110 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1111 dvb_ca_en50221_thread_update_delay(ca
);
1114 dprintk("DVB CAM validated successfully\n");
1116 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1117 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1121 case DVB_CA_SLOTSTATE_WAITFR
:
1122 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1123 pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1124 ca
->dvbdev
->adapter
->num
);
1125 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1126 dvb_ca_en50221_thread_update_delay(ca
);
1130 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1131 if (flags
& STATUSREG_FR
) {
1132 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1137 case DVB_CA_SLOTSTATE_LINKINIT
:
1138 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1139 /* we need this extra check for annoying interfaces like the budget-av */
1140 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1141 (ca
->pub
->poll_slot_status
)) {
1142 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1143 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1144 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1145 dvb_ca_en50221_thread_update_delay(ca
);
1150 pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
1151 ca
->dvbdev
->adapter
->num
);
1152 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1153 dvb_ca_en50221_thread_update_delay(ca
);
1157 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1158 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1159 if (rxbuf
== NULL
) {
1160 pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1161 ca
->dvbdev
->adapter
->num
);
1162 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1163 dvb_ca_en50221_thread_update_delay(ca
);
1166 dvb_ringbuffer_init(&ca
->slot_info
[slot
].rx_buffer
, rxbuf
, RX_BUFFER_SIZE
);
1169 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1170 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1171 dvb_ca_en50221_thread_update_delay(ca
);
1172 pr_err("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
1173 ca
->dvbdev
->adapter
->num
);
1176 case DVB_CA_SLOTSTATE_RUNNING
:
1180 // poll slots for data
1182 while ((status
= dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0)) > 0) {
1186 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1187 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1188 // we dont want to sleep on the next iteration so we can handle the cam change
1193 /* check if we've hit our limit this time */
1194 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1195 // dont sleep; there is likely to be more data to read
1203 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1212 /* ******************************************************************************** */
1213 /* EN50221 IO interface functions */
1216 * Real ioctl implementation.
1217 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1219 * @inode: Inode concerned.
1220 * @file: File concerned.
1221 * @cmd: IOCTL command.
1222 * @arg: Associated argument.
1224 * @return 0 on success, <0 on error.
1226 static int dvb_ca_en50221_io_do_ioctl(struct file
*file
,
1227 unsigned int cmd
, void *parg
)
1229 struct dvb_device
*dvbdev
= file
->private_data
;
1230 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1234 dprintk("%s\n", __func__
);
1236 if (mutex_lock_interruptible(&ca
->ioctl_mutex
))
1237 return -ERESTARTSYS
;
1241 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1242 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1243 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1244 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1245 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1246 dvb_ca_en50221_camchange_irq(ca
->pub
,
1248 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1250 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1252 ca
->next_read_slot
= 0;
1253 dvb_ca_en50221_thread_wakeup(ca
);
1257 struct ca_caps
*caps
= parg
;
1259 caps
->slot_num
= ca
->slot_count
;
1260 caps
->slot_type
= CA_CI_LINK
;
1261 caps
->descr_num
= 0;
1262 caps
->descr_type
= 0;
1266 case CA_GET_SLOT_INFO
: {
1267 struct ca_slot_info
*info
= parg
;
1269 if ((info
->num
> ca
->slot_count
) || (info
->num
< 0)) {
1274 info
->type
= CA_CI_LINK
;
1276 if ((ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1277 && (ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_INVALID
)) {
1278 info
->flags
= CA_CI_MODULE_PRESENT
;
1280 if (ca
->slot_info
[info
->num
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1281 info
->flags
|= CA_CI_MODULE_READY
;
1292 mutex_unlock(&ca
->ioctl_mutex
);
1298 * Wrapper for ioctl implementation.
1300 * @inode: Inode concerned.
1301 * @file: File concerned.
1302 * @cmd: IOCTL command.
1303 * @arg: Associated argument.
1305 * @return 0 on success, <0 on error.
1307 static long dvb_ca_en50221_io_ioctl(struct file
*file
,
1308 unsigned int cmd
, unsigned long arg
)
1310 return dvb_usercopy(file
, cmd
, arg
, dvb_ca_en50221_io_do_ioctl
);
1315 * Implementation of write() syscall.
1317 * @file: File structure.
1318 * @buf: Source buffer.
1319 * @count: Size of source buffer.
1320 * @ppos: Position in file (ignored).
1322 * @return Number of bytes read, or <0 on error.
1324 static ssize_t
dvb_ca_en50221_io_write(struct file
*file
,
1325 const char __user
* buf
, size_t count
, loff_t
* ppos
)
1327 struct dvb_device
*dvbdev
= file
->private_data
;
1328 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1329 u8 slot
, connection_id
;
1331 u8 fragbuf
[HOST_LINK_BUF_SIZE
];
1334 unsigned long timeout
;
1337 dprintk("%s\n", __func__
);
1339 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1343 /* extract slot & connection id */
1344 if (copy_from_user(&slot
, buf
, 1))
1346 if (copy_from_user(&connection_id
, buf
+ 1, 1))
1351 /* check if the slot is actually running */
1352 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1355 /* fragment the packets & store in the buffer */
1356 while (fragpos
< count
) {
1357 fraglen
= ca
->slot_info
[slot
].link_buf_size
- 2;
1360 if (fraglen
> HOST_LINK_BUF_SIZE
- 2)
1361 fraglen
= HOST_LINK_BUF_SIZE
- 2;
1362 if ((count
- fragpos
) < fraglen
)
1363 fraglen
= count
- fragpos
;
1365 fragbuf
[0] = connection_id
;
1366 fragbuf
[1] = ((fragpos
+ fraglen
) < count
) ? 0x80 : 0x00;
1367 status
= copy_from_user(fragbuf
+ 2, buf
+ fragpos
, fraglen
);
1373 timeout
= jiffies
+ HZ
/ 2;
1375 while (!time_after(jiffies
, timeout
)) {
1376 /* check the CAM hasn't been removed/reset in the meantime */
1377 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
) {
1382 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1383 status
= dvb_ca_en50221_write_data(ca
, slot
, fragbuf
, fraglen
+ 2);
1384 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1385 if (status
== (fraglen
+ 2)) {
1389 if (status
!= -EAGAIN
)
1409 * Condition for waking up in dvb_ca_en50221_io_read_condition
1411 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private
*ca
,
1412 int *result
, int *_slot
)
1418 int connection_id
= -1;
1422 slot
= ca
->next_read_slot
;
1423 while ((slot_count
< ca
->slot_count
) && (!found
)) {
1424 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1427 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1431 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1433 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1434 if (connection_id
== -1)
1435 connection_id
= hdr
[0];
1436 if ((hdr
[0] == connection_id
) && ((hdr
[1] & 0x80) == 0)) {
1442 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1446 slot
= (slot
+ 1) % ca
->slot_count
;
1450 ca
->next_read_slot
= slot
;
1456 * Implementation of read() syscall.
1458 * @file: File structure.
1459 * @buf: Destination buffer.
1460 * @count: Size of destination buffer.
1461 * @ppos: Position in file (ignored).
1463 * @return Number of bytes read, or <0 on error.
1465 static ssize_t
dvb_ca_en50221_io_read(struct file
*file
, char __user
* buf
,
1466 size_t count
, loff_t
* ppos
)
1468 struct dvb_device
*dvbdev
= file
->private_data
;
1469 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1474 int connection_id
= -1;
1476 int last_fragment
= 0;
1481 dprintk("%s\n", __func__
);
1483 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1487 /* wait for some data */
1488 if ((status
= dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
)) == 0) {
1490 /* if we're in nonblocking mode, exit immediately */
1491 if (file
->f_flags
& O_NONBLOCK
)
1492 return -EWOULDBLOCK
;
1494 /* wait for some data */
1495 status
= wait_event_interruptible(ca
->wait_queue
,
1496 dvb_ca_en50221_io_read_condition
1497 (ca
, &result
, &slot
));
1499 if ((status
< 0) || (result
< 0)) {
1505 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1509 pr_err("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n",
1510 ca
->dvbdev
->adapter
->num
);
1515 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1516 if (connection_id
== -1)
1517 connection_id
= hdr
[0];
1518 if (hdr
[0] == connection_id
) {
1519 if (pktlen
< count
) {
1520 if ((pktlen
+ fraglen
- 2) > count
) {
1521 fraglen
= count
- pktlen
;
1526 if ((status
= dvb_ringbuffer_pkt_read_user(&ca
->slot_info
[slot
].rx_buffer
, idx
, 2,
1527 buf
+ pktlen
, fraglen
)) < 0) {
1533 if ((hdr
[1] & 0x80) == 0)
1538 idx2
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1540 dvb_ringbuffer_pkt_dispose(&ca
->slot_info
[slot
].rx_buffer
, idx
);
1543 } while (!last_fragment
);
1546 hdr
[1] = connection_id
;
1547 status
= copy_to_user(buf
, hdr
, 2);
1560 * Implementation of file open syscall.
1562 * @inode: Inode concerned.
1563 * @file: File concerned.
1565 * @return 0 on success, <0 on failure.
1567 static int dvb_ca_en50221_io_open(struct inode
*inode
, struct file
*file
)
1569 struct dvb_device
*dvbdev
= file
->private_data
;
1570 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1574 dprintk("%s\n", __func__
);
1576 if (!try_module_get(ca
->pub
->owner
))
1579 err
= dvb_generic_open(inode
, file
);
1581 module_put(ca
->pub
->owner
);
1585 for (i
= 0; i
< ca
->slot_count
; i
++) {
1587 if (ca
->slot_info
[i
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1588 if (ca
->slot_info
[i
].rx_buffer
.data
!= NULL
) {
1589 /* it is safe to call this here without locks because
1590 * ca->open == 0. Data is not read in this case */
1591 dvb_ringbuffer_flush(&ca
->slot_info
[i
].rx_buffer
);
1597 dvb_ca_en50221_thread_update_delay(ca
);
1598 dvb_ca_en50221_thread_wakeup(ca
);
1600 dvb_ca_private_get(ca
);
1607 * Implementation of file close syscall.
1609 * @inode: Inode concerned.
1610 * @file: File concerned.
1612 * @return 0 on success, <0 on failure.
1614 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1616 struct dvb_device
*dvbdev
= file
->private_data
;
1617 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1620 dprintk("%s\n", __func__
);
1622 /* mark the CA device as closed */
1624 dvb_ca_en50221_thread_update_delay(ca
);
1626 err
= dvb_generic_release(inode
, file
);
1628 module_put(ca
->pub
->owner
);
1630 dvb_ca_private_put(ca
);
1637 * Implementation of poll() syscall.
1639 * @file: File concerned.
1640 * @wait: poll wait table.
1642 * @return Standard poll mask.
1644 static unsigned int dvb_ca_en50221_io_poll(struct file
*file
, poll_table
* wait
)
1646 struct dvb_device
*dvbdev
= file
->private_data
;
1647 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1648 unsigned int mask
= 0;
1652 dprintk("%s\n", __func__
);
1654 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1658 /* if there is something, return now */
1662 /* wait for something to happen */
1663 poll_wait(file
, &ca
->wait_queue
, wait
);
1665 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1671 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1674 static const struct file_operations dvb_ca_fops
= {
1675 .owner
= THIS_MODULE
,
1676 .read
= dvb_ca_en50221_io_read
,
1677 .write
= dvb_ca_en50221_io_write
,
1678 .unlocked_ioctl
= dvb_ca_en50221_io_ioctl
,
1679 .open
= dvb_ca_en50221_io_open
,
1680 .release
= dvb_ca_en50221_io_release
,
1681 .poll
= dvb_ca_en50221_io_poll
,
1682 .llseek
= noop_llseek
,
1685 static const struct dvb_device dvbdev_ca
= {
1690 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1691 .name
= "dvb-ca-en50221",
1693 .fops
= &dvb_ca_fops
,
1696 /* ******************************************************************************** */
1697 /* Initialisation/shutdown functions */
1701 * Initialise a new DVB CA EN50221 interface device.
1703 * @dvb_adapter: DVB adapter to attach the new CA device to.
1704 * @ca: The dvb_ca instance.
1705 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1706 * @slot_count: Number of slots supported.
1708 * @return 0 on success, nonzero on failure
1710 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1711 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1714 struct dvb_ca_private
*ca
= NULL
;
1717 dprintk("%s\n", __func__
);
1722 /* initialise the system data */
1723 if ((ca
= kzalloc(sizeof(struct dvb_ca_private
), GFP_KERNEL
)) == NULL
) {
1727 kref_init(&ca
->refcount
);
1730 ca
->slot_count
= slot_count
;
1731 if ((ca
->slot_info
= kcalloc(slot_count
, sizeof(struct dvb_ca_slot
), GFP_KERNEL
)) == NULL
) {
1735 init_waitqueue_head(&ca
->wait_queue
);
1738 ca
->next_read_slot
= 0;
1739 pubca
->private = ca
;
1741 /* register the DVB device */
1742 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
, DVB_DEVICE_CA
, 0);
1744 goto free_slot_info
;
1746 /* now initialise each slot */
1747 for (i
= 0; i
< slot_count
; i
++) {
1748 memset(&ca
->slot_info
[i
], 0, sizeof(struct dvb_ca_slot
));
1749 ca
->slot_info
[i
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1750 atomic_set(&ca
->slot_info
[i
].camchange_count
, 0);
1751 ca
->slot_info
[i
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1752 mutex_init(&ca
->slot_info
[i
].slot_lock
);
1755 mutex_init(&ca
->ioctl_mutex
);
1757 if (signal_pending(current
)) {
1759 goto unregister_device
;
1763 /* create a kthread for monitoring this CA device */
1764 ca
->thread
= kthread_run(dvb_ca_en50221_thread
, ca
, "kdvb-ca-%i:%i",
1765 ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
1766 if (IS_ERR(ca
->thread
)) {
1767 ret
= PTR_ERR(ca
->thread
);
1768 pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
1770 goto unregister_device
;
1775 dvb_unregister_device(ca
->dvbdev
);
1777 kfree(ca
->slot_info
);
1781 pubca
->private = NULL
;
1784 EXPORT_SYMBOL(dvb_ca_en50221_release
);
1789 * Release a DVB CA EN50221 interface device.
1791 * @ca_dev: The dvb_device_t instance for the CA device.
1792 * @ca: The associated dvb_ca instance.
1794 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1796 struct dvb_ca_private
*ca
= pubca
->private;
1799 dprintk("%s\n", __func__
);
1801 /* shutdown the thread if there was one */
1802 kthread_stop(ca
->thread
);
1804 for (i
= 0; i
< ca
->slot_count
; i
++) {
1805 dvb_ca_en50221_slot_shutdown(ca
, i
);
1807 dvb_remove_device(ca
->dvbdev
);
1808 dvb_ca_private_put(ca
);
1809 pubca
->private = NULL
;