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/nospec.h>
35 #include <linux/vmalloc.h>
36 #include <linux/delay.h>
37 #include <linux/spinlock.h>
38 #include <linux/sched/signal.h>
39 #include <linux/kthread.h>
41 #include <media/dvb_ca_en50221.h>
42 #include <media/dvb_ringbuffer.h>
44 static int dvb_ca_en50221_debug
;
46 module_param_named(cam_debug
, dvb_ca_en50221_debug
, int, 0644);
47 MODULE_PARM_DESC(cam_debug
, "enable verbose debug messages");
49 #define dprintk(fmt, arg...) do { \
50 if (dvb_ca_en50221_debug) \
51 printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg);\
54 #define INIT_TIMEOUT_SECS 10
56 #define HOST_LINK_BUF_SIZE 0x200
58 #define RX_BUFFER_SIZE 65535
60 #define MAX_RX_PACKETS_PER_ITERATION 10
63 #define CTRLIF_COMMAND 1
64 #define CTRLIF_STATUS 1
65 #define CTRLIF_SIZE_LOW 2
66 #define CTRLIF_SIZE_HIGH 3
68 #define CMDREG_HC 1 /* Host control */
69 #define CMDREG_SW 2 /* Size write */
70 #define CMDREG_SR 4 /* Size read */
71 #define CMDREG_RS 8 /* Reset interface */
72 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
73 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
74 #define IRQEN (CMDREG_DAIE)
76 #define STATUSREG_RE 1 /* read error */
77 #define STATUSREG_WE 2 /* write error */
78 #define STATUSREG_FR 0x40 /* module free */
79 #define STATUSREG_DA 0x80 /* data available */
81 #define DVB_CA_SLOTSTATE_NONE 0
82 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
83 #define DVB_CA_SLOTSTATE_RUNNING 2
84 #define DVB_CA_SLOTSTATE_INVALID 3
85 #define DVB_CA_SLOTSTATE_WAITREADY 4
86 #define DVB_CA_SLOTSTATE_VALIDATE 5
87 #define DVB_CA_SLOTSTATE_WAITFR 6
88 #define DVB_CA_SLOTSTATE_LINKINIT 7
90 /* Information on a CA slot */
92 /* current state of the CAM */
95 /* mutex used for serializing access to one CI slot */
96 struct mutex slot_lock
;
98 /* Number of CAMCHANGES that have occurred since last processing */
99 atomic_t camchange_count
;
101 /* Type of last CAMCHANGE */
104 /* base address of CAM config */
107 /* value to write into Config Control register */
110 /* if 1, the CAM supports DA IRQs */
111 u8 da_irq_supported
:1;
113 /* size of the buffer to use when talking to the CAM */
116 /* buffer for incoming packets */
117 struct dvb_ringbuffer rx_buffer
;
119 /* timer used during various states of the slot */
120 unsigned long timeout
;
123 /* Private CA-interface information */
124 struct dvb_ca_private
{
125 struct kref refcount
;
127 /* pointer back to the public data structure */
128 struct dvb_ca_en50221
*pub
;
131 struct dvb_device
*dvbdev
;
133 /* Flags describing the interface (DVB_CA_FLAG_*) */
136 /* number of slots supported by this CA interface */
137 unsigned int slot_count
;
139 /* information on each slot */
140 struct dvb_ca_slot
*slot_info
;
142 /* wait queues for read() and write() operations */
143 wait_queue_head_t wait_queue
;
145 /* PID of the monitoring thread */
146 struct task_struct
*thread
;
148 /* Flag indicating if the CA device is open */
151 /* Flag indicating the thread should wake up now */
152 unsigned int wakeup
:1;
154 /* Delay the main thread should use */
158 * Slot to start looking for data to read from in the next user-space
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
;
183 ca
= container_of(ref
, struct dvb_ca_private
, refcount
);
184 dvb_ca_private_free(ca
);
187 static void dvb_ca_private_get(struct dvb_ca_private
*ca
)
189 kref_get(&ca
->refcount
);
192 static void dvb_ca_private_put(struct dvb_ca_private
*ca
)
194 kref_put(&ca
->refcount
, dvb_ca_private_release
);
197 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
198 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
,
199 u8
*ebuf
, int ecount
);
200 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
,
201 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
))
227 /* ************************************************************************** */
228 /* 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
)
235 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
241 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
242 return (atomic_read(&sl
->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
= (sl
->slot_state
!= DVB_CA_SLOTSTATE_NONE
);
252 cam_changed
= (cam_present_now
!= cam_present_old
);
256 if (!cam_present_now
)
257 sl
->camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
259 sl
->camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
260 atomic_set(&sl
->camchange_count
, 1);
262 if ((sl
->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 sl
->slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
273 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
274 * register on a CAM interface, checking for errors and timeout.
277 * @slot: Slot on interface.
278 * @waitfor: Flags to wait for.
279 * @timeout_hz: Timeout in milliseconds.
281 * return: 0 on success, nonzero on error.
283 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
284 u8 waitfor
, int timeout_hz
)
286 unsigned long timeout
;
289 dprintk("%s\n", __func__
);
291 /* loop until timeout elapsed */
293 timeout
= jiffies
+ timeout_hz
;
297 /* read the status and check for error */
298 res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
302 /* if we got the flags, it was successful! */
304 dprintk("%s succeeded timeout:%lu\n",
305 __func__
, jiffies
- start
);
309 /* check for timeout */
310 if (time_after(jiffies
, timeout
))
314 usleep_range(1000, 1100);
317 dprintk("%s failed timeout:%lu\n", __func__
, jiffies
- start
);
319 /* if we get here, we've timed out */
324 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
329 * return: 0 on success, nonzero on failure.
331 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
333 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
338 dprintk("%s\n", __func__
);
340 /* we'll be determining these during this function */
341 sl
->da_irq_supported
= 0;
344 * set the host link buffer size temporarily. it will be overwritten
345 * with the real negotiated size later.
347 sl
->link_buf_size
= 2;
349 /* read the buffer size from the CAM */
350 ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
354 ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
);
357 ret
= dvb_ca_en50221_read_data(ca
, slot
, buf
, 2);
360 ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
365 * store it, and choose the minimum of our buffer and the CAM's buffer
368 buf_size
= (buf
[0] << 8) | buf
[1];
369 if (buf_size
> HOST_LINK_BUF_SIZE
)
370 buf_size
= HOST_LINK_BUF_SIZE
;
371 sl
->link_buf_size
= buf_size
;
372 buf
[0] = buf_size
>> 8;
373 buf
[1] = buf_size
& 0xff;
374 dprintk("Chosen link buffer size of %i\n", buf_size
);
376 /* write the buffer size to the CAM */
377 ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
381 ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_FR
, HZ
/ 10);
384 ret
= dvb_ca_en50221_write_data(ca
, slot
, buf
, 2);
387 ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
396 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
400 * @address: Address to read from. Updated.
401 * @tuple_type: Tuple id byte. Updated.
402 * @tuple_length: Tuple length. Updated.
403 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
405 * return: 0 on success, nonzero on error.
407 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private
*ca
, int slot
,
408 int *address
, int *tuple_type
,
409 int *tuple_length
, u8
*tuple
)
414 int _address
= *address
;
416 /* grab the next tuple length and type */
417 _tuple_type
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
);
420 if (_tuple_type
== 0xff) {
421 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tuple_type
);
423 *tuple_type
= _tuple_type
;
427 _tuple_length
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
,
429 if (_tuple_length
< 0)
430 return _tuple_length
;
433 dprintk("TUPLE type:0x%x length:%i\n", _tuple_type
, _tuple_length
);
435 /* read in the whole tuple */
436 for (i
= 0; i
< _tuple_length
; i
++) {
437 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
,
439 dprintk(" 0x%02x: 0x%02x %c\n",
441 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
443 _address
+= (_tuple_length
* 2);
446 *tuple_type
= _tuple_type
;
447 *tuple_length
= _tuple_length
;
453 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
454 * extracting Config register, and checking it is a DVB CAM module.
459 * return: 0 on success, <0 on failure.
461 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
463 struct dvb_ca_slot
*sl
;
471 int got_cftableentry
= 0;
477 /* CISTPL_DEVICE_0A */
478 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
479 &tuple_length
, tuple
);
482 if (tuple_type
!= 0x1D)
485 /* CISTPL_DEVICE_0C */
486 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
487 &tuple_length
, tuple
);
490 if (tuple_type
!= 0x1C)
494 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
495 &tuple_length
, tuple
);
498 if (tuple_type
!= 0x15)
502 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
503 &tuple_length
, tuple
);
506 if (tuple_type
!= 0x20)
508 if (tuple_length
!= 4)
510 manfid
= (tuple
[1] << 8) | tuple
[0];
511 devid
= (tuple
[3] << 8) | tuple
[2];
514 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tuple_type
,
515 &tuple_length
, tuple
);
518 if (tuple_type
!= 0x1A)
520 if (tuple_length
< 3)
523 /* extract the configbase */
525 if (tuple_length
< (3 + rasz
+ 14))
527 sl
= &ca
->slot_info
[slot
];
529 for (i
= 0; i
< rasz
+ 1; i
++)
530 sl
->config_base
|= (tuple
[2 + i
] << (8 * i
));
532 /* check it contains the correct DVB string */
533 dvb_str
= findstr((char *)tuple
, tuple_length
, "DVB_CI_V", 8);
536 if (tuple_length
< ((dvb_str
- (char *)tuple
) + 12))
539 /* is it a version we support? */
540 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
541 pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
542 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9],
543 dvb_str
[10], dvb_str
[11]);
547 /* process the CFTABLE_ENTRY tuples, and any after those */
548 while ((!end_chain
) && (address
< 0x1000)) {
549 status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
,
550 &tuple_type
, &tuple_length
,
554 switch (tuple_type
) {
555 case 0x1B: /* CISTPL_CFTABLE_ENTRY */
556 if (tuple_length
< (2 + 11 + 17))
559 /* if we've already parsed one, just use it */
560 if (got_cftableentry
)
563 /* get the config option */
564 sl
->config_option
= tuple
[0] & 0x3f;
566 /* OK, check it contains the correct strings */
567 if (!findstr((char *)tuple
, tuple_length
,
569 !findstr((char *)tuple
, tuple_length
,
570 "DVB_CI_MODULE", 13))
573 got_cftableentry
= 1;
576 case 0x14: /* CISTPL_NO_LINK */
579 case 0xFF: /* CISTPL_END */
583 default: /* Unknown tuple type - just skip this tuple */
584 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
585 tuple_type
, tuple_length
);
590 if ((address
> 0x1000) || (!got_cftableentry
))
593 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
594 manfid
, devid
, sl
->config_base
, sl
->config_option
);
601 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
604 * @slot: Slot containing the CAM.
606 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
608 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
611 dprintk("%s\n", __func__
);
613 /* set the config option */
614 ca
->pub
->write_attribute_mem(ca
->pub
, slot
, sl
->config_base
,
618 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
,
620 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
621 sl
->config_option
, configoption
& 0x3f);
628 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
629 * interface. It reads a buffer of data from the CAM. The data can either
630 * be stored in a supplied buffer, or automatically be added to the slot's
634 * @slot: Slot to read from.
635 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
636 * the data will be added into the buffering system as a normal
638 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
640 * return: Number of bytes read, or < 0 on error
642 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
,
643 u8
*ebuf
, int ecount
)
645 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
648 u8 buf
[HOST_LINK_BUF_SIZE
];
651 dprintk("%s\n", __func__
);
653 /* check if we have space for a link buf in the rx_buffer */
657 if (!sl
->rx_buffer
.data
) {
661 buf_free
= dvb_ringbuffer_free(&sl
->rx_buffer
);
663 if (buf_free
< (sl
->link_buf_size
+
664 DVB_RINGBUFFER_PKTHDRSIZE
)) {
670 if (ca
->pub
->read_data
&&
671 (sl
->slot_state
!= DVB_CA_SLOTSTATE_LINKINIT
)) {
673 status
= ca
->pub
->read_data(ca
->pub
, slot
, buf
,
676 status
= ca
->pub
->read_data(ca
->pub
, slot
, buf
, ecount
);
683 /* check if there is data available */
684 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
688 if (!(status
& STATUSREG_DA
)) {
694 /* read the amount of data */
695 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
699 bytes_read
= status
<< 8;
700 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
704 bytes_read
|= status
;
706 /* check it will fit */
708 if (bytes_read
> sl
->link_buf_size
) {
709 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
710 ca
->dvbdev
->adapter
->num
, bytes_read
,
712 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
716 if (bytes_read
< 2) {
717 pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
718 ca
->dvbdev
->adapter
->num
);
719 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
724 if (bytes_read
> ecount
) {
725 pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
726 ca
->dvbdev
->adapter
->num
);
732 /* fill the buffer */
733 for (i
= 0; i
< bytes_read
; i
++) {
734 /* read byte and check */
735 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
740 /* OK, store it in the buffer */
744 /* check for read error (RE should now be 0) */
745 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
,
749 if (status
& STATUSREG_RE
) {
750 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
757 * OK, add it to the receive buffer, or copy into external buffer if
761 if (!sl
->rx_buffer
.data
) {
765 dvb_ringbuffer_pkt_write(&sl
->rx_buffer
, buf
, bytes_read
);
767 memcpy(ebuf
, buf
, bytes_read
);
770 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
771 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
773 /* wake up readers when a last_fragment is received */
774 if ((buf
[1] & 0x80) == 0x00)
775 wake_up_interruptible(&ca
->wait_queue
);
784 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
785 * interface. It writes a buffer of data to a CAM.
788 * @slot: Slot to write to.
789 * @buf: The data in this buffer is treated as a complete link-level packet to
791 * @bytes_write: Size of ebuf.
793 * return: Number of bytes written, or < 0 on error.
795 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
,
796 u8
*buf
, int bytes_write
)
798 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
802 dprintk("%s\n", __func__
);
805 if (bytes_write
> sl
->link_buf_size
)
808 if (ca
->pub
->write_data
&&
809 (sl
->slot_state
!= DVB_CA_SLOTSTATE_LINKINIT
))
810 return ca
->pub
->write_data(ca
->pub
, slot
, buf
, bytes_write
);
813 * it is possible we are dealing with a single buffer implementation,
814 * thus if there is data available for read or if there is even a read
815 * already in progress, we do nothing but awake the kernel thread to
816 * process the data if necessary.
818 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
821 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
822 if (status
& STATUSREG_DA
)
823 dvb_ca_en50221_thread_wakeup(ca
);
830 status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
835 /* check if interface is still free */
836 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
839 if (!(status
& STATUSREG_FR
)) {
840 /* it wasn't free => try again later */
846 * It may need some time for the CAM to settle down, or there might
847 * be a race condition between the CAM, writing HC and our last
848 * check for DA. This happens, if the CAM asserts DA, just after
849 * checking DA before we are setting HC. In this case it might be
850 * a bug in the CAM to keep the FR bit, the lower layer/HW
851 * communication requires a longer timeout or the CAM needs more
852 * time internally. But this happens in reality!
853 * We need to read the status from the HW again and do the same
854 * we did for the previous check for DA
856 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
860 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
861 if (status
& STATUSREG_DA
)
862 dvb_ca_en50221_thread_wakeup(ca
);
868 /* send the amount of data */
869 status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
,
873 status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
878 /* send the buffer */
879 for (i
= 0; i
< bytes_write
; i
++) {
880 status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
,
886 /* check for write error (WE should now be 0) */
887 status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
890 if (status
& STATUSREG_WE
) {
891 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
895 status
= bytes_write
;
897 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
898 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
901 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
907 /* ************************************************************************** */
908 /* EN50221 higher level functions */
911 * dvb_ca_en50221_slot_shutdown - A CAM has been removed => shut it down.
914 * @slot: Slot to shut down.
916 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
918 dprintk("%s\n", __func__
);
920 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
921 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
924 * need to wake up all processes to check if they're now trying to
925 * write to a defunct CAM
927 wake_up_interruptible(&ca
->wait_queue
);
929 dprintk("Slot %i shutdown\n", slot
);
936 * dvb_ca_en50221_camchange_irq - A CAMCHANGE IRQ has occurred.
938 * @pubca: CA instance.
939 * @slot: Slot concerned.
940 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
942 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
,
945 struct dvb_ca_private
*ca
= pubca
->private;
946 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
948 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
950 switch (change_type
) {
951 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
952 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
959 sl
->camchange_type
= change_type
;
960 atomic_inc(&sl
->camchange_count
);
961 dvb_ca_en50221_thread_wakeup(ca
);
963 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
966 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
968 * @pubca: CA instance.
969 * @slot: Slot concerned.
971 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
973 struct dvb_ca_private
*ca
= pubca
->private;
974 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
976 dprintk("CAMREADY IRQ slot:%i\n", slot
);
978 if (sl
->slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
979 sl
->slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
980 dvb_ca_en50221_thread_wakeup(ca
);
983 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
986 * dvb_ca_en50221_frda_irq - An FR or DA IRQ has occurred.
988 * @pubca: CA instance.
989 * @slot: Slot concerned.
991 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
993 struct dvb_ca_private
*ca
= pubca
->private;
994 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
997 dprintk("FR/DA IRQ slot:%i\n", slot
);
999 switch (sl
->slot_state
) {
1000 case DVB_CA_SLOTSTATE_LINKINIT
:
1001 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
1002 if (flags
& STATUSREG_DA
) {
1003 dprintk("CAM supports DA IRQ\n");
1004 sl
->da_irq_supported
= 1;
1008 case DVB_CA_SLOTSTATE_RUNNING
:
1010 dvb_ca_en50221_thread_wakeup(ca
);
1014 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
1016 /* ************************************************************************** */
1017 /* EN50221 thread functions */
1020 * Wake up the DVB CA thread
1024 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
1026 dprintk("%s\n", __func__
);
1030 wake_up_process(ca
->thread
);
1034 * Update the delay used by the thread.
1038 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
1041 int curdelay
= 100000000;
1045 * Beware of too high polling frequency, because one polling
1046 * call might take several hundred milliseconds until timeout!
1048 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1049 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
1051 switch (sl
->slot_state
) {
1053 case DVB_CA_SLOTSTATE_NONE
:
1054 delay
= HZ
* 60; /* 60s */
1055 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
1056 delay
= HZ
* 5; /* 5s */
1058 case DVB_CA_SLOTSTATE_INVALID
:
1059 delay
= HZ
* 60; /* 60s */
1060 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
1061 delay
= HZ
/ 10; /* 100ms */
1064 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1065 case DVB_CA_SLOTSTATE_WAITREADY
:
1066 case DVB_CA_SLOTSTATE_VALIDATE
:
1067 case DVB_CA_SLOTSTATE_WAITFR
:
1068 case DVB_CA_SLOTSTATE_LINKINIT
:
1069 delay
= HZ
/ 10; /* 100ms */
1072 case DVB_CA_SLOTSTATE_RUNNING
:
1073 delay
= HZ
* 60; /* 60s */
1074 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
1075 delay
= HZ
/ 10; /* 100ms */
1077 if ((!sl
->da_irq_supported
) ||
1078 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
)))
1079 delay
= HZ
/ 10; /* 100ms */
1084 if (delay
< curdelay
)
1088 ca
->delay
= curdelay
;
1092 * Poll if the CAM is gone.
1095 * @slot: Slot to process.
1096 * return:: 0 .. no change
1097 * 1 .. CAM state changed
1100 static int dvb_ca_en50221_poll_cam_gone(struct dvb_ca_private
*ca
, int slot
)
1106 * we need this extra check for annoying interfaces like the
1109 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1110 (ca
->pub
->poll_slot_status
)) {
1111 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1113 DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1114 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1115 dvb_ca_en50221_thread_update_delay(ca
);
1123 * Thread state machine for one CA slot to perform the data transfer.
1126 * @slot: Slot to process.
1128 static void dvb_ca_en50221_thread_state_machine(struct dvb_ca_private
*ca
,
1131 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
1136 mutex_lock(&sl
->slot_lock
);
1138 /* check the cam status + deal with CAMCHANGEs */
1139 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1140 /* clear down an old CI slot if necessary */
1141 if (sl
->slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1142 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1144 /* if a CAM is NOW present, initialise it */
1145 if (sl
->camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
)
1146 sl
->slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1148 /* we've handled one CAMCHANGE */
1149 dvb_ca_en50221_thread_update_delay(ca
);
1150 atomic_dec(&sl
->camchange_count
);
1153 /* CAM state machine */
1154 switch (sl
->slot_state
) {
1155 case DVB_CA_SLOTSTATE_NONE
:
1156 case DVB_CA_SLOTSTATE_INVALID
:
1157 /* no action needed */
1160 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1161 sl
->slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1162 ca
->pub
->slot_reset(ca
->pub
, slot
);
1163 sl
->timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1166 case DVB_CA_SLOTSTATE_WAITREADY
:
1167 if (time_after(jiffies
, sl
->timeout
)) {
1168 pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
1169 ca
->dvbdev
->adapter
->num
);
1170 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1171 dvb_ca_en50221_thread_update_delay(ca
);
1175 * no other action needed; will automatically change state when
1180 case DVB_CA_SLOTSTATE_VALIDATE
:
1181 if (dvb_ca_en50221_parse_attributes(ca
, slot
) != 0) {
1182 if (dvb_ca_en50221_poll_cam_gone(ca
, slot
))
1185 pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1186 ca
->dvbdev
->adapter
->num
);
1187 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1188 dvb_ca_en50221_thread_update_delay(ca
);
1191 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1192 pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1193 ca
->dvbdev
->adapter
->num
);
1194 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1195 dvb_ca_en50221_thread_update_delay(ca
);
1198 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1201 pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
1202 ca
->dvbdev
->adapter
->num
);
1203 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1204 dvb_ca_en50221_thread_update_delay(ca
);
1207 dprintk("DVB CAM validated successfully\n");
1209 sl
->timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1210 sl
->slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1214 case DVB_CA_SLOTSTATE_WAITFR
:
1215 if (time_after(jiffies
, sl
->timeout
)) {
1216 pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1217 ca
->dvbdev
->adapter
->num
);
1218 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1219 dvb_ca_en50221_thread_update_delay(ca
);
1223 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1224 if (flags
& STATUSREG_FR
) {
1225 sl
->slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1230 case DVB_CA_SLOTSTATE_LINKINIT
:
1231 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1232 if (dvb_ca_en50221_poll_cam_gone(ca
, slot
))
1235 pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
1236 ca
->dvbdev
->adapter
->num
);
1237 sl
->slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1238 dvb_ca_en50221_thread_update_delay(ca
);
1242 if (!sl
->rx_buffer
.data
) {
1243 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1245 pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1246 ca
->dvbdev
->adapter
->num
);
1247 sl
->slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1248 dvb_ca_en50221_thread_update_delay(ca
);
1251 dvb_ringbuffer_init(&sl
->rx_buffer
, rxbuf
,
1255 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1256 sl
->slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1257 dvb_ca_en50221_thread_update_delay(ca
);
1258 pr_info("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
1259 ca
->dvbdev
->adapter
->num
);
1262 case DVB_CA_SLOTSTATE_RUNNING
:
1266 /* poll slots for data */
1268 while (dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0) > 0) {
1273 * if a CAMCHANGE occurred at some point, do not do any
1274 * more processing of this slot
1276 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1278 * we don't want to sleep on the next iteration
1279 * so we can handle the cam change
1285 /* check if we've hit our limit this time */
1286 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1288 * don't sleep; there is likely to be more data
1298 mutex_unlock(&sl
->slot_lock
);
1302 * Kernel thread which monitors CA slots for CAM changes, and performs data
1305 static int dvb_ca_en50221_thread(void *data
)
1307 struct dvb_ca_private
*ca
= data
;
1310 dprintk("%s\n", __func__
);
1312 /* choose the correct initial delay */
1313 dvb_ca_en50221_thread_update_delay(ca
);
1316 while (!kthread_should_stop()) {
1317 /* sleep for a bit */
1319 set_current_state(TASK_INTERRUPTIBLE
);
1320 schedule_timeout(ca
->delay
);
1321 if (kthread_should_stop())
1326 /* go through all the slots processing them */
1327 for (slot
= 0; slot
< ca
->slot_count
; slot
++)
1328 dvb_ca_en50221_thread_state_machine(ca
, slot
);
1334 /* ************************************************************************** */
1335 /* EN50221 IO interface functions */
1338 * Real ioctl implementation.
1339 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1341 * @file: File concerned.
1342 * @cmd: IOCTL command.
1343 * @parg: Associated argument.
1345 * return: 0 on success, <0 on error.
1347 static int dvb_ca_en50221_io_do_ioctl(struct file
*file
,
1348 unsigned int cmd
, void *parg
)
1350 struct dvb_device
*dvbdev
= file
->private_data
;
1351 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1355 dprintk("%s\n", __func__
);
1357 if (mutex_lock_interruptible(&ca
->ioctl_mutex
))
1358 return -ERESTARTSYS
;
1362 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1363 struct dvb_ca_slot
*sl
= &ca
->slot_info
[slot
];
1365 mutex_lock(&sl
->slot_lock
);
1366 if (sl
->slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1367 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1368 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1369 dvb_ca_en50221_camchange_irq(ca
->pub
,
1371 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1373 mutex_unlock(&sl
->slot_lock
);
1375 ca
->next_read_slot
= 0;
1376 dvb_ca_en50221_thread_wakeup(ca
);
1380 struct ca_caps
*caps
= parg
;
1382 caps
->slot_num
= ca
->slot_count
;
1383 caps
->slot_type
= CA_CI_LINK
;
1384 caps
->descr_num
= 0;
1385 caps
->descr_type
= 0;
1389 case CA_GET_SLOT_INFO
: {
1390 struct ca_slot_info
*info
= parg
;
1391 struct dvb_ca_slot
*sl
;
1394 if ((slot
>= ca
->slot_count
) || (slot
< 0)) {
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 * 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 * 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 * 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 * 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 if (!try_module_get(ca
->pub
->owner
))
1723 err
= dvb_generic_open(inode
, file
);
1725 module_put(ca
->pub
->owner
);
1729 for (i
= 0; i
< ca
->slot_count
; i
++) {
1730 struct dvb_ca_slot
*sl
= &ca
->slot_info
[i
];
1732 if (sl
->slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1733 if (!sl
->rx_buffer
.data
) {
1735 * it is safe to call this here without locks
1736 * because ca->open == 0. Data is not read in
1739 dvb_ringbuffer_flush(&sl
->rx_buffer
);
1745 dvb_ca_en50221_thread_update_delay(ca
);
1746 dvb_ca_en50221_thread_wakeup(ca
);
1748 dvb_ca_private_get(ca
);
1754 * Implementation of file close syscall.
1756 * @inode: Inode concerned.
1757 * @file: File concerned.
1759 * return: 0 on success, <0 on failure.
1761 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1763 struct dvb_device
*dvbdev
= file
->private_data
;
1764 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1767 dprintk("%s\n", __func__
);
1769 /* mark the CA device as closed */
1771 dvb_ca_en50221_thread_update_delay(ca
);
1773 err
= dvb_generic_release(inode
, file
);
1775 module_put(ca
->pub
->owner
);
1777 dvb_ca_private_put(ca
);
1783 * Implementation of poll() syscall.
1785 * @file: File concerned.
1786 * @wait: poll wait table.
1788 * return: Standard poll mask.
1790 static __poll_t
dvb_ca_en50221_io_poll(struct file
*file
, poll_table
*wait
)
1792 struct dvb_device
*dvbdev
= file
->private_data
;
1793 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1798 dprintk("%s\n", __func__
);
1800 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1)
1803 /* if there is something, return now */
1807 /* wait for something to happen */
1808 poll_wait(file
, &ca
->wait_queue
, wait
);
1810 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1)
1816 static const struct file_operations dvb_ca_fops
= {
1817 .owner
= THIS_MODULE
,
1818 .read
= dvb_ca_en50221_io_read
,
1819 .write
= dvb_ca_en50221_io_write
,
1820 .unlocked_ioctl
= dvb_ca_en50221_io_ioctl
,
1821 .open
= dvb_ca_en50221_io_open
,
1822 .release
= dvb_ca_en50221_io_release
,
1823 .poll
= dvb_ca_en50221_io_poll
,
1824 .llseek
= noop_llseek
,
1827 static const struct dvb_device dvbdev_ca
= {
1832 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1833 .name
= "dvb-ca-en50221",
1835 .fops
= &dvb_ca_fops
,
1838 /* ************************************************************************** */
1839 /* Initialisation/shutdown functions */
1842 * Initialise a new DVB CA EN50221 interface device.
1844 * @dvb_adapter: DVB adapter to attach the new CA device to.
1845 * @pubca: The dvb_ca instance.
1846 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1847 * @slot_count: Number of slots supported.
1849 * return: 0 on success, nonzero on failure
1851 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1852 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1855 struct dvb_ca_private
*ca
= NULL
;
1858 dprintk("%s\n", __func__
);
1863 /* initialise the system data */
1864 ca
= kzalloc(sizeof(*ca
), GFP_KERNEL
);
1869 kref_init(&ca
->refcount
);
1872 ca
->slot_count
= slot_count
;
1873 ca
->slot_info
= kcalloc(slot_count
, sizeof(struct dvb_ca_slot
),
1875 if (!ca
->slot_info
) {
1879 init_waitqueue_head(&ca
->wait_queue
);
1882 ca
->next_read_slot
= 0;
1883 pubca
->private = ca
;
1885 /* register the DVB device */
1886 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
,
1889 goto free_slot_info
;
1891 /* now initialise each slot */
1892 for (i
= 0; i
< slot_count
; i
++) {
1893 struct dvb_ca_slot
*sl
= &ca
->slot_info
[i
];
1895 memset(sl
, 0, sizeof(struct dvb_ca_slot
));
1896 sl
->slot_state
= DVB_CA_SLOTSTATE_NONE
;
1897 atomic_set(&sl
->camchange_count
, 0);
1898 sl
->camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1899 mutex_init(&sl
->slot_lock
);
1902 mutex_init(&ca
->ioctl_mutex
);
1904 if (signal_pending(current
)) {
1906 goto unregister_device
;
1910 /* create a kthread for monitoring this CA device */
1911 ca
->thread
= kthread_run(dvb_ca_en50221_thread
, ca
, "kdvb-ca-%i:%i",
1912 ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
1913 if (IS_ERR(ca
->thread
)) {
1914 ret
= PTR_ERR(ca
->thread
);
1915 pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
1917 goto unregister_device
;
1922 dvb_unregister_device(ca
->dvbdev
);
1924 kfree(ca
->slot_info
);
1928 pubca
->private = NULL
;
1931 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1934 * Release a DVB CA EN50221 interface device.
1936 * @pubca: The associated dvb_ca instance.
1938 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1940 struct dvb_ca_private
*ca
= pubca
->private;
1943 dprintk("%s\n", __func__
);
1945 /* shutdown the thread if there was one */
1946 kthread_stop(ca
->thread
);
1948 for (i
= 0; i
< ca
->slot_count
; i
++)
1949 dvb_ca_en50221_slot_shutdown(ca
, i
);
1951 dvb_remove_device(ca
->dvbdev
);
1952 dvb_ca_private_put(ca
);
1953 pubca
->private = NULL
;
1955 EXPORT_SYMBOL(dvb_ca_en50221_release
);