1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces
5 * Copyright (C) 2004 Andrew de Quincey
7 * Parts of this file were based on sources as follows:
9 * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de>
13 * Copyright (C) 1999-2002 Ralph Metzler
14 * & Marcus Metzler for convergence integrated media GmbH
17 #define pr_fmt(fmt) "dvb_ca_en50221: " fmt
19 #include <linux/errno.h>
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/nospec.h>
24 #include <linux/vmalloc.h>
25 #include <linux/delay.h>
26 #include <linux/spinlock.h>
27 #include <linux/sched/signal.h>
28 #include <linux/kthread.h>
30 #include <media/dvb_ca_en50221.h>
31 #include <media/dvb_ringbuffer.h>
33 static int dvb_ca_en50221_debug
;
35 module_param_named(cam_debug
, dvb_ca_en50221_debug
, int, 0644);
36 MODULE_PARM_DESC(cam_debug
, "enable verbose debug messages");
38 #define dprintk(fmt, arg...) do { \
39 if (dvb_ca_en50221_debug) \
40 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg);\
43 #define INIT_TIMEOUT_SECS 10
45 #define HOST_LINK_BUF_SIZE 0x200
47 #define RX_BUFFER_SIZE 65535
49 #define MAX_RX_PACKETS_PER_ITERATION 10
52 #define CTRLIF_COMMAND 1
53 #define CTRLIF_STATUS 1
54 #define CTRLIF_SIZE_LOW 2
55 #define CTRLIF_SIZE_HIGH 3
57 #define CMDREG_HC 1 /* Host control */
58 #define CMDREG_SW 2 /* Size write */
59 #define CMDREG_SR 4 /* Size read */
60 #define CMDREG_RS 8 /* Reset interface */
61 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
62 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
63 #define IRQEN (CMDREG_DAIE)
65 #define STATUSREG_RE 1 /* read error */
66 #define STATUSREG_WE 2 /* write error */
67 #define STATUSREG_FR 0x40 /* module free */
68 #define STATUSREG_DA 0x80 /* data available */
70 #define DVB_CA_SLOTSTATE_NONE 0
71 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
72 #define DVB_CA_SLOTSTATE_RUNNING 2
73 #define DVB_CA_SLOTSTATE_INVALID 3
74 #define DVB_CA_SLOTSTATE_WAITREADY 4
75 #define DVB_CA_SLOTSTATE_VALIDATE 5
76 #define DVB_CA_SLOTSTATE_WAITFR 6
77 #define DVB_CA_SLOTSTATE_LINKINIT 7
79 /* Information on a CA slot */
81 /* current state of the CAM */
84 /* mutex used for serializing access to one CI slot */
85 struct mutex slot_lock
;
87 /* Number of CAMCHANGES that have occurred since last processing */
88 atomic_t camchange_count
;
90 /* Type of last CAMCHANGE */
93 /* base address of CAM config */
96 /* value to write into Config Control register */
99 /* if 1, the CAM supports DA IRQs */
100 u8 da_irq_supported
:1;
102 /* size of the buffer to use when talking to the CAM */
105 /* buffer for incoming packets */
106 struct dvb_ringbuffer rx_buffer
;
108 /* timer used during various states of the slot */
109 unsigned long timeout
;
112 /* Private CA-interface information */
113 struct dvb_ca_private
{
114 struct kref refcount
;
116 /* pointer back to the public data structure */
117 struct dvb_ca_en50221
*pub
;
120 struct dvb_device
*dvbdev
;
122 /* Flags describing the interface (DVB_CA_FLAG_*) */
125 /* number of slots supported by this CA interface */
126 unsigned int slot_count
;
128 /* information on each slot */
129 struct dvb_ca_slot
*slot_info
;
131 /* wait queues for read() and write() operations */
132 wait_queue_head_t wait_queue
;
134 /* PID of the monitoring thread */
135 struct task_struct
*thread
;
137 /* Flag indicating if the CA device is open */
140 /* Flag indicating the thread should wake up now */
141 unsigned int wakeup
:1;
143 /* Delay the main thread should use */
147 * Slot to start looking for data to read from in the next user-space
152 /* mutex serializing ioctls */
153 struct mutex ioctl_mutex
;
155 /* A mutex used when a device is disconnected */
156 struct mutex remove_mutex
;
158 /* Whether the device is disconnected */
162 static void dvb_ca_private_free(struct dvb_ca_private
*ca
)
166 dvb_device_put(ca
->dvbdev
);
167 for (i
= 0; i
< ca
->slot_count
; i
++)
168 vfree(ca
->slot_info
[i
].rx_buffer
.data
);
170 kfree(ca
->slot_info
);
174 static void dvb_ca_private_release(struct kref
*ref
)
176 struct dvb_ca_private
*ca
;
178 ca
= container_of(ref
, struct dvb_ca_private
, refcount
);
179 dvb_ca_private_free(ca
);
182 static void dvb_ca_private_get(struct dvb_ca_private
*ca
)
184 kref_get(&ca
->refcount
);
187 static void dvb_ca_private_put(struct dvb_ca_private
*ca
)
189 kref_put(&ca
->refcount
, dvb_ca_private_release
);
192 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
193 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
,
194 u8
*ebuf
, int ecount
);
195 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
,
196 u8
*ebuf
, int ecount
, int size_write_flag
);
199 * findstr - Safely find needle in haystack.
201 * @haystack: Buffer to look in.
202 * @hlen: Number of bytes in haystack.
203 * @needle: Buffer to find.
204 * @nlen: Number of bytes in needle.
205 * return: Pointer into haystack needle was found at, or NULL if not found.
207 static char *findstr(char *haystack
, int hlen
, char *needle
, int nlen
)
214 for (i
= 0; i
<= hlen
- nlen
; i
++) {
215 if (!strncmp(haystack
+ i
, needle
, nlen
))
222 /* ************************************************************************** */
223 /* EN50221 physical interface functions */
226 * dvb_ca_en50221_check_camstatus - Check CAM status.
228 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private
*ca
, int slot
)
230 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
236 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
237 return (atomic_read(&sl
->camchange_count
) != 0);
240 slot_status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, ca
->open
);
242 cam_present_now
= (slot_status
& DVB_CA_EN50221_POLL_CAM_PRESENT
) ? 1 : 0;
243 cam_changed
= (slot_status
& DVB_CA_EN50221_POLL_CAM_CHANGED
) ? 1 : 0;
245 int cam_present_old
= (sl
->slot_state
!= DVB_CA_SLOTSTATE_NONE
);
247 cam_changed
= (cam_present_now
!= cam_present_old
);
251 if (!cam_present_now
)
252 sl
->camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
254 sl
->camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
255 atomic_set(&sl
->camchange_count
, 1);
257 if ((sl
->slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) &&
258 (slot_status
& DVB_CA_EN50221_POLL_CAM_READY
)) {
259 /* move to validate state if reset is completed */
260 sl
->slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
268 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
269 * register on a CAM interface, checking for errors and timeout.
272 * @slot: Slot on interface.
273 * @waitfor: Flags to wait for.
274 * @timeout_hz: Timeout in milliseconds.
276 * return: 0 on success, nonzero on error.
278 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
279 u8 waitfor
, int timeout_hz
)
281 unsigned long timeout
;
284 dprintk("%s\n", __func__
);
286 /* loop until timeout elapsed */
288 timeout
= jiffies
+ timeout_hz
;
292 /* read the status and check for error */
293 res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
297 /* if we got the flags, it was successful! */
299 dprintk("%s succeeded timeout:%lu\n",
300 __func__
, jiffies
- start
);
304 /* check for timeout */
305 if (time_after(jiffies
, timeout
))
309 usleep_range(1000, 1100);
312 dprintk("%s failed timeout:%lu\n", __func__
, jiffies
- start
);
314 /* if we get here, we've timed out */
319 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
324 * return: 0 on success, nonzero on failure.
326 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
328 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
333 dprintk("%s\n", __func__
);
335 /* we'll be determining these during this function */
336 sl
->da_irq_supported
= 0;
339 * set the host link buffer size temporarily. it will be overwritten
340 * with the real negotiated size later.
342 sl
->link_buf_size
= 2;
344 /* read the buffer size from the CAM */
345 ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
349 ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
);
352 ret
= dvb_ca_en50221_read_data(ca
, slot
, buf
, 2);
355 ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
360 * store it, and choose the minimum of our buffer and the CAM's buffer
363 buf_size
= (buf
[0] << 8) | buf
[1];
364 if (buf_size
> HOST_LINK_BUF_SIZE
)
365 buf_size
= HOST_LINK_BUF_SIZE
;
366 sl
->link_buf_size
= buf_size
;
367 buf
[0] = buf_size
>> 8;
368 buf
[1] = buf_size
& 0xff;
369 dprintk("Chosen link buffer size of %i\n", buf_size
);
371 /* write the buffer size to the CAM */
372 ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
376 ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_FR
, HZ
/ 10);
379 ret
= dvb_ca_en50221_write_data(ca
, slot
, buf
, 2, CMDREG_SW
);
382 ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
391 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
395 * @address: Address to read from. Updated.
396 * @tuple_type: Tuple id byte. Updated.
397 * @tuple_length: Tuple length. Updated.
398 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
400 * return: 0 on success, nonzero on error.
402 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private
*ca
, int slot
,
403 int *address
, int *tuple_type
,
404 int *tuple_length
, u8
*tuple
)
409 int _address
= *address
;
411 /* grab the next tuple length and type */
412 _tuple_type
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
);
415 if (_tuple_type
== 0xff) {
416 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tuple_type
);
418 *tuple_type
= _tuple_type
;
422 _tuple_length
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
,
424 if (_tuple_length
< 0)
425 return _tuple_length
;
428 dprintk("TUPLE type:0x%x length:%i\n", _tuple_type
, _tuple_length
);
430 /* read in the whole tuple */
431 for (i
= 0; i
< _tuple_length
; i
++) {
432 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
,
434 dprintk(" 0x%02x: 0x%02x %c\n",
436 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
438 _address
+= (_tuple_length
* 2);
441 *tuple_type
= _tuple_type
;
442 *tuple_length
= _tuple_length
;
448 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
449 * extracting Config register, and checking it is a DVB CAM module.
454 * return: 0 on success, <0 on failure.
456 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
458 struct dvb_ca_slot
*sl
;
466 int got_cftableentry
= 0;
472 /* CISTPL_DEVICE_0A */
473 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
474 &tuple_length
, tuple
);
477 if (tuple_type
!= 0x1D)
480 /* CISTPL_DEVICE_0C */
481 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
482 &tuple_length
, tuple
);
485 if (tuple_type
!= 0x1C)
489 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
490 &tuple_length
, tuple
);
493 if (tuple_type
!= 0x15)
497 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
498 &tuple_length
, tuple
);
501 if (tuple_type
!= 0x20)
503 if (tuple_length
!= 4)
505 manfid
= (tuple
[1] << 8) | tuple
[0];
506 devid
= (tuple
[3] << 8) | tuple
[2];
509 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
510 &tuple_length
, tuple
);
513 if (tuple_type
!= 0x1A)
515 if (tuple_length
< 3)
518 /* extract the configbase */
520 if (tuple_length
< (3 + rasz
+ 14))
522 sl
= &ca
->slot_info
[slot
];
524 for (i
= 0; i
< rasz
+ 1; i
++)
525 sl
->config_base
|= (tuple
[2 + i
] << (8 * i
));
527 /* check it contains the correct DVB string */
528 dvb_str
= findstr((char *)tuple
, tuple_length
, "DVB_CI_V", 8);
531 if (tuple_length
< ((dvb_str
- (char *)tuple
) + 12))
534 /* is it a version we support? */
535 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
536 pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
537 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9],
538 dvb_str
[10], dvb_str
[11]);
542 /* process the CFTABLE_ENTRY tuples, and any after those */
543 while ((!end_chain
) && (address
< 0x1000)) {
544 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
,
545 &tuple_type
, &tuple_length
,
549 switch (tuple_type
) {
550 case 0x1B: /* CISTPL_CFTABLE_ENTRY */
551 if (tuple_length
< (2 + 11 + 17))
554 /* if we've already parsed one, just use it */
555 if (got_cftableentry
)
558 /* get the config option */
559 sl
->config_option
= tuple
[0] & 0x3f;
561 /* OK, check it contains the correct strings */
562 if (!findstr((char *)tuple
, tuple_length
,
564 !findstr((char *)tuple
, tuple_length
,
565 "DVB_CI_MODULE", 13))
568 got_cftableentry
= 1;
571 case 0x14: /* CISTPL_NO_LINK */
574 case 0xFF: /* CISTPL_END */
578 default: /* Unknown tuple type - just skip this tuple */
579 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
580 tuple_type
, tuple_length
);
585 if ((address
> 0x1000) || (!got_cftableentry
))
588 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
589 manfid
, devid
, sl
->config_base
, sl
->config_option
);
596 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
599 * @slot: Slot containing the CAM.
601 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
603 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
606 dprintk("%s\n", __func__
);
608 /* set the config option */
609 ca
->pub
->write_attribute_mem(ca
->pub
, slot
, sl
->config_base
,
613 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
,
615 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
616 sl
->config_option
, configoption
& 0x3f);
623 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
624 * interface. It reads a buffer of data from the CAM. The data can either
625 * be stored in a supplied buffer, or automatically be added to the slot's
629 * @slot: Slot to read from.
630 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
631 * the data will be added into the buffering system as a normal
633 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
635 * return: Number of bytes read, or < 0 on error
637 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
,
638 u8
*ebuf
, int ecount
)
640 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
643 u8 buf
[HOST_LINK_BUF_SIZE
];
646 dprintk("%s\n", __func__
);
648 /* check if we have space for a link buf in the rx_buffer */
652 if (!sl
->rx_buffer
.data
) {
656 buf_free
= dvb_ringbuffer_free(&sl
->rx_buffer
);
658 if (buf_free
< (sl
->link_buf_size
+
659 DVB_RINGBUFFER_PKTHDRSIZE
)) {
665 if (ca
->pub
->read_data
&&
666 (sl
->slot_state
!= DVB_CA_SLOTSTATE_LINKINIT
)) {
668 status
= ca
->pub
->read_data(ca
->pub
, slot
, buf
,
671 status
= ca
->pub
->read_data(ca
->pub
, slot
, buf
, ecount
);
678 /* check if there is data available */
679 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
683 if (!(status
& STATUSREG_DA
)) {
689 /* read the amount of data */
690 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
694 bytes_read
= status
<< 8;
695 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
699 bytes_read
|= status
;
701 /* check it will fit */
703 if (bytes_read
> sl
->link_buf_size
) {
704 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
705 ca
->dvbdev
->adapter
->num
, bytes_read
,
707 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
711 if (bytes_read
< 2) {
712 pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
713 ca
->dvbdev
->adapter
->num
);
714 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
719 if (bytes_read
> ecount
) {
720 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
721 ca
->dvbdev
->adapter
->num
);
727 /* fill the buffer */
728 for (i
= 0; i
< bytes_read
; i
++) {
729 /* read byte and check */
730 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
735 /* OK, store it in the buffer */
739 /* check for read error (RE should now be 0) */
740 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
744 if (status
& STATUSREG_RE
) {
745 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
752 * OK, add it to the receive buffer, or copy into external buffer if
756 if (!sl
->rx_buffer
.data
) {
760 dvb_ringbuffer_pkt_write(&sl
->rx_buffer
, buf
, bytes_read
);
762 memcpy(ebuf
, buf
, bytes_read
);
765 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
766 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
768 /* wake up readers when a last_fragment is received */
769 if ((buf
[1] & 0x80) == 0x00)
770 wake_up_interruptible(&ca
->wait_queue
);
779 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
780 * interface. It writes a buffer of data to a CAM.
783 * @slot: Slot to write to.
784 * @buf: The data in this buffer is treated as a complete link-level packet to
786 * @bytes_write: Size of ebuf.
787 * @size_write_flag: A flag on Command Register which says whether the link size
788 * information will be writen or not.
790 * return: Number of bytes written, or < 0 on error.
792 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
,
793 u8
*buf
, int bytes_write
, int size_write_flag
)
795 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
799 dprintk("%s\n", __func__
);
802 if (bytes_write
> sl
->link_buf_size
)
805 if (ca
->pub
->write_data
&&
806 (sl
->slot_state
!= DVB_CA_SLOTSTATE_LINKINIT
))
807 return ca
->pub
->write_data(ca
->pub
, slot
, buf
, bytes_write
);
810 * it is possible we are dealing with a single buffer implementation,
811 * thus if there is data available for read or if there is even a read
812 * already in progress, we do nothing but awake the kernel thread to
813 * process the data if necessary.
815 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
818 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
819 if (status
& STATUSREG_DA
)
820 dvb_ca_en50221_thread_wakeup(ca
);
827 status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
828 IRQEN
| CMDREG_HC
| size_write_flag
);
832 /* check if interface is still free */
833 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
836 if (!(status
& STATUSREG_FR
)) {
837 /* it wasn't free => try again later */
843 * It may need some time for the CAM to settle down, or there might
844 * be a race condition between the CAM, writing HC and our last
845 * check for DA. This happens, if the CAM asserts DA, just after
846 * checking DA before we are setting HC. In this case it might be
847 * a bug in the CAM to keep the FR bit, the lower layer/HW
848 * communication requires a longer timeout or the CAM needs more
849 * time internally. But this happens in reality!
850 * We need to read the status from the HW again and do the same
851 * we did for the previous check for DA
853 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
857 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
858 if (status
& STATUSREG_DA
)
859 dvb_ca_en50221_thread_wakeup(ca
);
865 /* send the amount of data */
866 status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
,
870 status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
875 /* send the buffer */
876 for (i
= 0; i
< bytes_write
; i
++) {
877 status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
,
883 /* check for write error (WE should now be 0) */
884 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
887 if (status
& STATUSREG_WE
) {
888 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
892 status
= bytes_write
;
894 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
895 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
898 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
904 /* ************************************************************************** */
905 /* EN50221 higher level functions */
908 * dvb_ca_en50221_slot_shutdown - A CAM has been removed => shut it down.
911 * @slot: Slot to shut down.
913 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
915 dprintk("%s\n", __func__
);
917 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
918 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
921 * need to wake up all processes to check if they're now trying to
922 * write to a defunct CAM
924 wake_up_interruptible(&ca
->wait_queue
);
926 dprintk("Slot %i shutdown\n", slot
);
933 * dvb_ca_en50221_camchange_irq - A CAMCHANGE IRQ has occurred.
935 * @pubca: CA instance.
936 * @slot: Slot concerned.
937 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
939 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
,
942 struct dvb_ca_private
*ca
= pubca
->private;
943 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
945 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
947 switch (change_type
) {
948 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
949 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
956 sl
->camchange_type
= change_type
;
957 atomic_inc(&sl
->camchange_count
);
958 dvb_ca_en50221_thread_wakeup(ca
);
960 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
963 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
965 * @pubca: CA instance.
966 * @slot: Slot concerned.
968 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
970 struct dvb_ca_private
*ca
= pubca
->private;
971 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
973 dprintk("CAMREADY IRQ slot:%i\n", slot
);
975 if (sl
->slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
976 sl
->slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
977 dvb_ca_en50221_thread_wakeup(ca
);
980 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
983 * dvb_ca_en50221_frda_irq - An FR or DA IRQ has occurred.
985 * @pubca: CA instance.
986 * @slot: Slot concerned.
988 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
990 struct dvb_ca_private
*ca
= pubca
->private;
991 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
994 dprintk("FR/DA IRQ slot:%i\n", slot
);
996 switch (sl
->slot_state
) {
997 case DVB_CA_SLOTSTATE_LINKINIT
:
998 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
999 if (flags
& STATUSREG_DA
) {
1000 dprintk("CAM supports DA IRQ\n");
1001 sl
->da_irq_supported
= 1;
1005 case DVB_CA_SLOTSTATE_RUNNING
:
1007 dvb_ca_en50221_thread_wakeup(ca
);
1011 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
1013 /* ************************************************************************** */
1014 /* EN50221 thread functions */
1017 * dvb_ca_en50221_thread_wakeup - Wake up the DVB CA thread
1021 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
1023 dprintk("%s\n", __func__
);
1027 wake_up_process(ca
->thread
);
1031 * dvb_ca_en50221_thread_update_delay - Update the delay used by the thread.
1035 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
1038 int curdelay
= 100000000;
1042 * Beware of too high polling frequency, because one polling
1043 * call might take several hundred milliseconds until timeout!
1045 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1046 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
1048 switch (sl
->slot_state
) {
1050 case DVB_CA_SLOTSTATE_NONE
:
1051 delay
= HZ
* 60; /* 60s */
1052 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
1053 delay
= HZ
* 5; /* 5s */
1055 case DVB_CA_SLOTSTATE_INVALID
:
1056 delay
= HZ
* 60; /* 60s */
1057 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
1058 delay
= HZ
/ 10; /* 100ms */
1061 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1062 case DVB_CA_SLOTSTATE_WAITREADY
:
1063 case DVB_CA_SLOTSTATE_VALIDATE
:
1064 case DVB_CA_SLOTSTATE_WAITFR
:
1065 case DVB_CA_SLOTSTATE_LINKINIT
:
1066 delay
= HZ
/ 10; /* 100ms */
1069 case DVB_CA_SLOTSTATE_RUNNING
:
1070 delay
= HZ
* 60; /* 60s */
1071 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
1072 delay
= HZ
/ 10; /* 100ms */
1074 if ((!sl
->da_irq_supported
) ||
1075 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
)))
1076 delay
= HZ
/ 10; /* 100ms */
1081 if (delay
< curdelay
)
1085 ca
->delay
= curdelay
;
1089 * dvb_ca_en50221_poll_cam_gone - Poll if the CAM is gone.
1092 * @slot: Slot to process.
1093 * return:: 0 .. no change
1094 * 1 .. CAM state changed
1097 static int dvb_ca_en50221_poll_cam_gone(struct dvb_ca_private
*ca
, int slot
)
1103 * we need this extra check for annoying interfaces like the
1106 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1107 (ca
->pub
->poll_slot_status
)) {
1108 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1110 DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1111 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1112 dvb_ca_en50221_thread_update_delay(ca
);
1120 * dvb_ca_en50221_thread_state_machine - Thread state machine for one CA slot
1121 * to perform the data transfer.
1124 * @slot: Slot to process.
1126 static void dvb_ca_en50221_thread_state_machine(struct dvb_ca_private
*ca
,
1129 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
1134 mutex_lock(&sl
->slot_lock
);
1136 /* check the cam status + deal with CAMCHANGEs */
1137 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1138 /* clear down an old CI slot if necessary */
1139 if (sl
->slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1140 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1142 /* if a CAM is NOW present, initialise it */
1143 if (sl
->camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
)
1144 sl
->slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1146 /* we've handled one CAMCHANGE */
1147 dvb_ca_en50221_thread_update_delay(ca
);
1148 atomic_dec(&sl
->camchange_count
);
1151 /* CAM state machine */
1152 switch (sl
->slot_state
) {
1153 case DVB_CA_SLOTSTATE_NONE
:
1154 case DVB_CA_SLOTSTATE_INVALID
:
1155 /* no action needed */
1158 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1159 sl
->slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1160 ca
->pub
->slot_reset(ca
->pub
, slot
);
1161 sl
->timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1164 case DVB_CA_SLOTSTATE_WAITREADY
:
1165 if (time_after(jiffies
, sl
->timeout
)) {
1166 pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
1167 ca
->dvbdev
->adapter
->num
);
1168 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1169 dvb_ca_en50221_thread_update_delay(ca
);
1173 * no other action needed; will automatically change state when
1178 case DVB_CA_SLOTSTATE_VALIDATE
:
1179 if (dvb_ca_en50221_parse_attributes(ca
, slot
) != 0) {
1180 if (dvb_ca_en50221_poll_cam_gone(ca
, slot
))
1183 pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1184 ca
->dvbdev
->adapter
->num
);
1185 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1186 dvb_ca_en50221_thread_update_delay(ca
);
1189 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1190 pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1191 ca
->dvbdev
->adapter
->num
);
1192 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1193 dvb_ca_en50221_thread_update_delay(ca
);
1196 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1199 pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
1200 ca
->dvbdev
->adapter
->num
);
1201 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1202 dvb_ca_en50221_thread_update_delay(ca
);
1205 dprintk("DVB CAM validated successfully\n");
1207 sl
->timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1208 sl
->slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1212 case DVB_CA_SLOTSTATE_WAITFR
:
1213 if (time_after(jiffies
, sl
->timeout
)) {
1214 pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1215 ca
->dvbdev
->adapter
->num
);
1216 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1217 dvb_ca_en50221_thread_update_delay(ca
);
1221 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1222 if (flags
& STATUSREG_FR
) {
1223 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1228 case DVB_CA_SLOTSTATE_LINKINIT
:
1229 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1230 if (dvb_ca_en50221_poll_cam_gone(ca
, slot
))
1233 pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
1234 ca
->dvbdev
->adapter
->num
);
1235 sl
->slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1236 dvb_ca_en50221_thread_update_delay(ca
);
1240 if (!sl
->rx_buffer
.data
) {
1241 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1243 pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1244 ca
->dvbdev
->adapter
->num
);
1245 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1246 dvb_ca_en50221_thread_update_delay(ca
);
1249 dvb_ringbuffer_init(&sl
->rx_buffer
, rxbuf
,
1253 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1254 sl
->slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1255 dvb_ca_en50221_thread_update_delay(ca
);
1256 pr_info("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
1257 ca
->dvbdev
->adapter
->num
);
1260 case DVB_CA_SLOTSTATE_RUNNING
:
1264 /* poll slots for data */
1266 while (dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0) > 0) {
1271 * if a CAMCHANGE occurred at some point, do not do any
1272 * more processing of this slot
1274 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1276 * we don't want to sleep on the next iteration
1277 * so we can handle the cam change
1283 /* check if we've hit our limit this time */
1284 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1286 * don't sleep; there is likely to be more data
1296 mutex_unlock(&sl
->slot_lock
);
1300 * Kernel thread which monitors CA slots for CAM changes, and performs data
1303 static int dvb_ca_en50221_thread(void *data
)
1305 struct dvb_ca_private
*ca
= data
;
1308 dprintk("%s\n", __func__
);
1310 /* choose the correct initial delay */
1311 dvb_ca_en50221_thread_update_delay(ca
);
1314 while (!kthread_should_stop()) {
1315 /* sleep for a bit */
1317 set_current_state(TASK_INTERRUPTIBLE
);
1318 schedule_timeout(ca
->delay
);
1319 if (kthread_should_stop())
1324 /* go through all the slots processing them */
1325 for (slot
= 0; slot
< ca
->slot_count
; slot
++)
1326 dvb_ca_en50221_thread_state_machine(ca
, slot
);
1332 /* ************************************************************************** */
1333 /* EN50221 IO interface functions */
1336 * dvb_ca_en50221_io_do_ioctl - Real ioctl implementation.
1338 * @file: File concerned.
1339 * @cmd: IOCTL command.
1340 * @parg: Associated argument.
1342 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1344 * return: 0 on success, <0 on error.
1346 static int dvb_ca_en50221_io_do_ioctl(struct file
*file
,
1347 unsigned int cmd
, void *parg
)
1349 struct dvb_device
*dvbdev
= file
->private_data
;
1350 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1354 dprintk("%s\n", __func__
);
1356 if (mutex_lock_interruptible(&ca
->ioctl_mutex
))
1357 return -ERESTARTSYS
;
1361 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1362 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
1364 mutex_lock(&sl
->slot_lock
);
1365 if (sl
->slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1366 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1367 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1368 dvb_ca_en50221_camchange_irq(ca
->pub
,
1370 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1372 mutex_unlock(&sl
->slot_lock
);
1374 ca
->next_read_slot
= 0;
1375 dvb_ca_en50221_thread_wakeup(ca
);
1379 struct ca_caps
*caps
= parg
;
1381 caps
->slot_num
= ca
->slot_count
;
1382 caps
->slot_type
= CA_CI_LINK
;
1383 caps
->descr_num
= 0;
1384 caps
->descr_type
= 0;
1388 case CA_GET_SLOT_INFO
: {
1389 struct ca_slot_info
*info
= parg
;
1390 struct dvb_ca_slot
*sl
;
1393 if ((slot
>= ca
->slot_count
) || (slot
< 0)) {
1397 slot
= array_index_nospec(slot
, ca
->slot_count
);
1399 info
->type
= CA_CI_LINK
;
1401 sl
= &ca
->slot_info
[slot
];
1402 if ((sl
->slot_state
!= DVB_CA_SLOTSTATE_NONE
) &&
1403 (sl
->slot_state
!= DVB_CA_SLOTSTATE_INVALID
)) {
1404 info
->flags
= CA_CI_MODULE_PRESENT
;
1406 if (sl
->slot_state
== DVB_CA_SLOTSTATE_RUNNING
)
1407 info
->flags
|= CA_CI_MODULE_READY
;
1417 mutex_unlock(&ca
->ioctl_mutex
);
1422 * dvb_ca_en50221_io_ioctl - Wrapper for ioctl implementation.
1424 * @file: File concerned.
1425 * @cmd: IOCTL command.
1426 * @arg: Associated argument.
1428 * return: 0 on success, <0 on error.
1430 static long dvb_ca_en50221_io_ioctl(struct file
*file
,
1431 unsigned int cmd
, unsigned long arg
)
1433 return dvb_usercopy(file
, cmd
, arg
, dvb_ca_en50221_io_do_ioctl
);
1437 * dvb_ca_en50221_io_write - Implementation of write() syscall.
1439 * @file: File structure.
1440 * @buf: Source buffer.
1441 * @count: Size of source buffer.
1442 * @ppos: Position in file (ignored).
1444 * return: Number of bytes read, or <0 on error.
1446 static ssize_t
dvb_ca_en50221_io_write(struct file
*file
,
1447 const char __user
*buf
, size_t count
,
1450 struct dvb_device
*dvbdev
= file
->private_data
;
1451 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1452 struct dvb_ca_slot
*sl
;
1453 u8 slot
, connection_id
;
1455 u8 fragbuf
[HOST_LINK_BUF_SIZE
];
1458 unsigned long timeout
;
1461 dprintk("%s\n", __func__
);
1464 * Incoming packet has a 2 byte header.
1465 * hdr[0] = slot_id, hdr[1] = connection_id
1470 /* extract slot & connection id */
1471 if (copy_from_user(&slot
, buf
, 1))
1473 if (copy_from_user(&connection_id
, buf
+ 1, 1))
1478 if (slot
>= ca
->slot_count
)
1480 slot
= array_index_nospec(slot
, ca
->slot_count
);
1481 sl
= &ca
->slot_info
[slot
];
1483 /* check if the slot is actually running */
1484 if (sl
->slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1487 /* fragment the packets & store in the buffer */
1488 while (fragpos
< count
) {
1489 fraglen
= sl
->link_buf_size
- 2;
1492 if (fraglen
> HOST_LINK_BUF_SIZE
- 2)
1493 fraglen
= HOST_LINK_BUF_SIZE
- 2;
1494 if ((count
- fragpos
) < fraglen
)
1495 fraglen
= count
- fragpos
;
1497 fragbuf
[0] = connection_id
;
1498 fragbuf
[1] = ((fragpos
+ fraglen
) < count
) ? 0x80 : 0x00;
1499 status
= copy_from_user(fragbuf
+ 2, buf
+ fragpos
, fraglen
);
1505 timeout
= jiffies
+ HZ
/ 2;
1507 while (!time_after(jiffies
, timeout
)) {
1509 * check the CAM hasn't been removed/reset in the
1512 if (sl
->slot_state
!= DVB_CA_SLOTSTATE_RUNNING
) {
1517 mutex_lock(&sl
->slot_lock
);
1518 status
= dvb_ca_en50221_write_data(ca
, slot
, fragbuf
,
1520 mutex_unlock(&sl
->slot_lock
);
1521 if (status
== (fraglen
+ 2)) {
1525 if (status
!= -EAGAIN
)
1528 usleep_range(1000, 1100);
1544 * Condition for waking up in dvb_ca_en50221_io_read_condition
1546 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private
*ca
,
1547 int *result
, int *_slot
)
1553 int connection_id
= -1;
1557 slot
= ca
->next_read_slot
;
1558 while ((slot_count
< ca
->slot_count
) && (!found
)) {
1559 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
1561 if (sl
->slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1564 if (!sl
->rx_buffer
.data
)
1567 idx
= dvb_ringbuffer_pkt_next(&sl
->rx_buffer
, -1, &fraglen
);
1569 dvb_ringbuffer_pkt_read(&sl
->rx_buffer
, idx
, 0, hdr
, 2);
1570 if (connection_id
== -1)
1571 connection_id
= hdr
[0];
1572 if ((hdr
[0] == connection_id
) &&
1573 ((hdr
[1] & 0x80) == 0)) {
1579 idx
= dvb_ringbuffer_pkt_next(&sl
->rx_buffer
, idx
,
1584 slot
= (slot
+ 1) % ca
->slot_count
;
1588 ca
->next_read_slot
= slot
;
1593 * dvb_ca_en50221_io_read - Implementation of read() syscall.
1595 * @file: File structure.
1596 * @buf: Destination buffer.
1597 * @count: Size of destination buffer.
1598 * @ppos: Position in file (ignored).
1600 * return: Number of bytes read, or <0 on error.
1602 static ssize_t
dvb_ca_en50221_io_read(struct file
*file
, char __user
*buf
,
1603 size_t count
, loff_t
*ppos
)
1605 struct dvb_device
*dvbdev
= file
->private_data
;
1606 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1607 struct dvb_ca_slot
*sl
;
1612 int connection_id
= -1;
1614 int last_fragment
= 0;
1619 dprintk("%s\n", __func__
);
1622 * Outgoing packet has a 2 byte header.
1623 * hdr[0] = slot_id, hdr[1] = connection_id
1628 /* wait for some data */
1629 status
= dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
);
1631 /* if we're in nonblocking mode, exit immediately */
1632 if (file
->f_flags
& O_NONBLOCK
)
1633 return -EWOULDBLOCK
;
1635 /* wait for some data */
1636 status
= wait_event_interruptible(ca
->wait_queue
,
1637 dvb_ca_en50221_io_read_condition
1638 (ca
, &result
, &slot
));
1640 if ((status
< 0) || (result
< 0)) {
1646 sl
= &ca
->slot_info
[slot
];
1647 idx
= dvb_ringbuffer_pkt_next(&sl
->rx_buffer
, -1, &fraglen
);
1651 pr_err("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n",
1652 ca
->dvbdev
->adapter
->num
);
1657 dvb_ringbuffer_pkt_read(&sl
->rx_buffer
, idx
, 0, hdr
, 2);
1658 if (connection_id
== -1)
1659 connection_id
= hdr
[0];
1660 if (hdr
[0] == connection_id
) {
1661 if (pktlen
< count
) {
1662 if ((pktlen
+ fraglen
- 2) > count
)
1663 fraglen
= count
- pktlen
;
1668 dvb_ringbuffer_pkt_read_user(&sl
->rx_buffer
,
1678 if ((hdr
[1] & 0x80) == 0)
1683 idx2
= dvb_ringbuffer_pkt_next(&sl
->rx_buffer
, idx
, &fraglen
);
1685 dvb_ringbuffer_pkt_dispose(&sl
->rx_buffer
, idx
);
1688 } while (!last_fragment
);
1691 hdr
[1] = connection_id
;
1692 status
= copy_to_user(buf
, hdr
, 2);
1704 * dvb_ca_en50221_io_open - Implementation of file open syscall.
1706 * @inode: Inode concerned.
1707 * @file: File concerned.
1709 * return: 0 on success, <0 on failure.
1711 static int dvb_ca_en50221_io_open(struct inode
*inode
, struct file
*file
)
1713 struct dvb_device
*dvbdev
= file
->private_data
;
1714 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1718 dprintk("%s\n", __func__
);
1720 mutex_lock(&ca
->remove_mutex
);
1723 mutex_unlock(&ca
->remove_mutex
);
1727 if (!try_module_get(ca
->pub
->owner
)) {
1728 mutex_unlock(&ca
->remove_mutex
);
1732 err
= dvb_generic_open(inode
, file
);
1734 module_put(ca
->pub
->owner
);
1735 mutex_unlock(&ca
->remove_mutex
);
1739 for (i
= 0; i
< ca
->slot_count
; i
++) {
1740 struct dvb_ca_slot
*sl
= &ca
->slot_info
[i
];
1742 if (sl
->slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1743 if (!sl
->rx_buffer
.data
) {
1745 * it is safe to call this here without locks
1746 * because ca->open == 0. Data is not read in
1749 dvb_ringbuffer_flush(&sl
->rx_buffer
);
1755 dvb_ca_en50221_thread_update_delay(ca
);
1756 dvb_ca_en50221_thread_wakeup(ca
);
1758 dvb_ca_private_get(ca
);
1760 mutex_unlock(&ca
->remove_mutex
);
1765 * dvb_ca_en50221_io_release - Implementation of file close syscall.
1767 * @inode: Inode concerned.
1768 * @file: File concerned.
1770 * return: 0 on success, <0 on failure.
1772 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1774 struct dvb_device
*dvbdev
= file
->private_data
;
1775 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1778 dprintk("%s\n", __func__
);
1780 mutex_lock(&ca
->remove_mutex
);
1782 /* mark the CA device as closed */
1784 dvb_ca_en50221_thread_update_delay(ca
);
1786 err
= dvb_generic_release(inode
, file
);
1788 module_put(ca
->pub
->owner
);
1790 dvb_ca_private_put(ca
);
1792 if (dvbdev
->users
== 1 && ca
->exit
== 1) {
1793 mutex_unlock(&ca
->remove_mutex
);
1794 wake_up(&dvbdev
->wait_queue
);
1796 mutex_unlock(&ca
->remove_mutex
);
1803 * dvb_ca_en50221_io_poll - Implementation of poll() syscall.
1805 * @file: File concerned.
1806 * @wait: poll wait table.
1808 * return: Standard poll mask.
1810 static __poll_t
dvb_ca_en50221_io_poll(struct file
*file
, poll_table
*wait
)
1812 struct dvb_device
*dvbdev
= file
->private_data
;
1813 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1818 dprintk("%s\n", __func__
);
1820 poll_wait(file
, &ca
->wait_queue
, wait
);
1822 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1)
1825 /* if there is something, return now */
1829 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1)
1835 static const struct file_operations dvb_ca_fops
= {
1836 .owner
= THIS_MODULE
,
1837 .read
= dvb_ca_en50221_io_read
,
1838 .write
= dvb_ca_en50221_io_write
,
1839 .unlocked_ioctl
= dvb_ca_en50221_io_ioctl
,
1840 .open
= dvb_ca_en50221_io_open
,
1841 .release
= dvb_ca_en50221_io_release
,
1842 .poll
= dvb_ca_en50221_io_poll
,
1843 .llseek
= noop_llseek
,
1846 static const struct dvb_device dvbdev_ca
= {
1851 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1852 .name
= "dvb-ca-en50221",
1854 .fops
= &dvb_ca_fops
,
1857 /* ************************************************************************** */
1858 /* Initialisation/shutdown functions */
1861 * dvb_ca_en50221_init - Initialise a new DVB CA EN50221 interface device.
1863 * @dvb_adapter: DVB adapter to attach the new CA device to.
1864 * @pubca: The dvb_ca instance.
1865 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1866 * @slot_count: Number of slots supported.
1868 * return: 0 on success, nonzero on failure
1870 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1871 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1874 struct dvb_ca_private
*ca
= NULL
;
1877 dprintk("%s\n", __func__
);
1882 /* initialise the system data */
1883 ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
1888 kref_init(&ca
->refcount
);
1891 ca
->slot_count
= slot_count
;
1892 ca
->slot_info
= kcalloc(slot_count
, sizeof(struct dvb_ca_slot
),
1894 if (!ca
->slot_info
) {
1898 init_waitqueue_head(&ca
->wait_queue
);
1901 ca
->next_read_slot
= 0;
1902 pubca
->private = ca
;
1904 /* register the DVB device */
1905 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
,
1908 goto free_slot_info
;
1910 /* now initialise each slot */
1911 for (i
= 0; i
< slot_count
; i
++) {
1912 struct dvb_ca_slot
*sl
= &ca
->slot_info
[i
];
1914 memset(sl
, 0, sizeof(struct dvb_ca_slot
));
1915 sl
->slot_state
= DVB_CA_SLOTSTATE_NONE
;
1916 atomic_set(&sl
->camchange_count
, 0);
1917 sl
->camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1918 mutex_init(&sl
->slot_lock
);
1921 mutex_init(&ca
->ioctl_mutex
);
1922 mutex_init(&ca
->remove_mutex
);
1924 if (signal_pending(current
)) {
1926 goto unregister_device
;
1930 /* create a kthread for monitoring this CA device */
1931 ca
->thread
= kthread_run(dvb_ca_en50221_thread
, ca
, "kdvb-ca-%i:%i",
1932 ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
1933 if (IS_ERR(ca
->thread
)) {
1934 ret
= PTR_ERR(ca
->thread
);
1935 pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
1937 goto unregister_device
;
1942 dvb_unregister_device(ca
->dvbdev
);
1944 kfree(ca
->slot_info
);
1948 pubca
->private = NULL
;
1951 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1954 * dvb_ca_en50221_release - Release a DVB CA EN50221 interface device.
1956 * @pubca: The associated dvb_ca instance.
1958 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1960 struct dvb_ca_private
*ca
= pubca
->private;
1963 dprintk("%s\n", __func__
);
1965 mutex_lock(&ca
->remove_mutex
);
1967 mutex_unlock(&ca
->remove_mutex
);
1969 if (ca
->dvbdev
->users
< 1)
1970 wait_event(ca
->dvbdev
->wait_queue
,
1971 ca
->dvbdev
->users
== 1);
1973 /* shutdown the thread if there was one */
1974 kthread_stop(ca
->thread
);
1976 for (i
= 0; i
< ca
->slot_count
; i
++)
1977 dvb_ca_en50221_slot_shutdown(ca
, i
);
1979 dvb_remove_device(ca
->dvbdev
);
1980 dvb_ca_private_put(ca
);
1981 pubca
->private = NULL
;
1983 EXPORT_SYMBOL(dvb_ca_en50221_release
);