2 * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces
4 * Copyright (C) 2004 Andrew de Quincey
6 * Parts of this file were based on sources as follows:
8 * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de>
12 * Copyright (C) 1999-2002 Ralph Metzler
13 * & Marcus Metzler for convergence integrated media GmbH
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <linux/list.h>
34 #include <linux/module.h>
35 #include <linux/vmalloc.h>
36 #include <linux/delay.h>
37 #include <linux/spinlock.h>
38 #include <linux/sched.h>
39 #include <linux/kthread.h>
41 #include "dvb_ca_en50221.h"
42 #include "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 if (dvb_ca_en50221_debug) printk
51 #define INIT_TIMEOUT_SECS 10
53 #define HOST_LINK_BUF_SIZE 0x200
55 #define RX_BUFFER_SIZE 65535
57 #define MAX_RX_PACKETS_PER_ITERATION 10
60 #define CTRLIF_COMMAND 1
61 #define CTRLIF_STATUS 1
62 #define CTRLIF_SIZE_LOW 2
63 #define CTRLIF_SIZE_HIGH 3
65 #define CMDREG_HC 1 /* Host control */
66 #define CMDREG_SW 2 /* Size write */
67 #define CMDREG_SR 4 /* Size read */
68 #define CMDREG_RS 8 /* Reset interface */
69 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
70 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
71 #define IRQEN (CMDREG_DAIE)
73 #define STATUSREG_RE 1 /* read error */
74 #define STATUSREG_WE 2 /* write error */
75 #define STATUSREG_FR 0x40 /* module free */
76 #define STATUSREG_DA 0x80 /* data available */
77 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
80 #define DVB_CA_SLOTSTATE_NONE 0
81 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
82 #define DVB_CA_SLOTSTATE_RUNNING 2
83 #define DVB_CA_SLOTSTATE_INVALID 3
84 #define DVB_CA_SLOTSTATE_WAITREADY 4
85 #define DVB_CA_SLOTSTATE_VALIDATE 5
86 #define DVB_CA_SLOTSTATE_WAITFR 6
87 #define DVB_CA_SLOTSTATE_LINKINIT 7
90 /* Information on a CA slot */
93 /* current state of the CAM */
96 /* mutex used for serializing access to one CI slot */
97 struct mutex slot_lock
;
99 /* Number of CAMCHANGES that have occurred since last processing */
100 atomic_t camchange_count
;
102 /* Type of last CAMCHANGE */
105 /* base address of CAM config */
108 /* value to write into Config Control register */
111 /* if 1, the CAM supports DA IRQs */
112 u8 da_irq_supported
:1;
114 /* size of the buffer to use when talking to the CAM */
117 /* buffer for incoming packets */
118 struct dvb_ringbuffer rx_buffer
;
120 /* timer used during various states of the slot */
121 unsigned long timeout
;
124 /* Private CA-interface information */
125 struct dvb_ca_private
{
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 */
157 /* Slot to start looking for data to read from in the next user-space read operation */
161 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
162 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
163 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
167 * Safely find needle in haystack.
169 * @param haystack Buffer to look in.
170 * @param hlen Number of bytes in haystack.
171 * @param needle Buffer to find.
172 * @param nlen Number of bytes in needle.
173 * @return Pointer into haystack needle was found at, or NULL if not found.
175 static char *findstr(char * haystack
, int hlen
, char * needle
, int nlen
)
182 for (i
= 0; i
<= hlen
- nlen
; i
++) {
183 if (!strncmp(haystack
+ i
, needle
, nlen
))
192 /* ******************************************************************************** */
193 /* EN50221 physical interface functions */
199 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private
*ca
, int slot
)
206 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
) {
207 return (atomic_read(&ca
->slot_info
[slot
].camchange_count
) != 0);
211 slot_status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, ca
->open
);
213 cam_present_now
= (slot_status
& DVB_CA_EN50221_POLL_CAM_PRESENT
) ? 1 : 0;
214 cam_changed
= (slot_status
& DVB_CA_EN50221_POLL_CAM_CHANGED
) ? 1 : 0;
216 int cam_present_old
= (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
);
217 cam_changed
= (cam_present_now
!= cam_present_old
);
221 if (!cam_present_now
) {
222 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
224 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
226 atomic_set(&ca
->slot_info
[slot
].camchange_count
, 1);
228 if ((ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) &&
229 (slot_status
& DVB_CA_EN50221_POLL_CAM_READY
)) {
230 // move to validate state if reset is completed
231 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
240 * Wait for flags to become set on the STATUS register on a CAM interface,
241 * checking for errors and timeout.
243 * @param ca CA instance.
244 * @param slot Slot on interface.
245 * @param waitfor Flags to wait for.
246 * @param timeout_ms Timeout in milliseconds.
248 * @return 0 on success, nonzero on error.
250 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
251 u8 waitfor
, int timeout_hz
)
253 unsigned long timeout
;
256 dprintk("%s\n", __func__
);
258 /* loop until timeout elapsed */
260 timeout
= jiffies
+ timeout_hz
;
262 /* read the status and check for error */
263 int res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
267 /* if we got the flags, it was successful! */
269 dprintk("%s succeeded timeout:%lu\n", __func__
, jiffies
- start
);
273 /* check for timeout */
274 if (time_after(jiffies
, timeout
)) {
282 dprintk("%s failed timeout:%lu\n", __func__
, jiffies
- start
);
284 /* if we get here, we've timed out */
290 * Initialise the link layer connection to a CAM.
292 * @param ca CA instance.
293 * @param slot Slot id.
295 * @return 0 on success, nonzero on failure.
297 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
303 dprintk("%s\n", __func__
);
305 /* we'll be determining these during this function */
306 ca
->slot_info
[slot
].da_irq_supported
= 0;
308 /* set the host link buffer size temporarily. it will be overwritten with the
309 * real negotiated size later. */
310 ca
->slot_info
[slot
].link_buf_size
= 2;
312 /* read the buffer size from the CAM */
313 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SR
)) != 0)
315 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
/ 10)) != 0)
317 if ((ret
= dvb_ca_en50221_read_data(ca
, slot
, buf
, 2)) != 2)
319 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
322 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
323 buf_size
= (buf
[0] << 8) | buf
[1];
324 if (buf_size
> HOST_LINK_BUF_SIZE
)
325 buf_size
= HOST_LINK_BUF_SIZE
;
326 ca
->slot_info
[slot
].link_buf_size
= buf_size
;
327 buf
[0] = buf_size
>> 8;
328 buf
[1] = buf_size
& 0xff;
329 dprintk("Chosen link buffer size of %i\n", buf_size
);
331 /* write the buffer size to the CAM */
332 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SW
)) != 0)
334 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_FR
, HZ
/ 10)) != 0)
336 if ((ret
= dvb_ca_en50221_write_data(ca
, slot
, buf
, 2)) != 2)
338 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
346 * Read a tuple from attribute memory.
348 * @param ca CA instance.
349 * @param slot Slot id.
350 * @param address Address to read from. Updated.
351 * @param tupleType Tuple id byte. Updated.
352 * @param tupleLength Tuple length. Updated.
353 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
355 * @return 0 on success, nonzero on error.
357 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private
*ca
, int slot
,
358 int *address
, int *tupleType
, int *tupleLength
, u8
* tuple
)
363 int _address
= *address
;
365 /* grab the next tuple length and type */
366 if ((_tupleType
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
)) < 0)
368 if (_tupleType
== 0xff) {
369 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType
);
371 *tupleType
= _tupleType
;
375 if ((_tupleLength
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ 2)) < 0)
379 dprintk("TUPLE type:0x%x length:%i\n", _tupleType
, _tupleLength
);
381 /* read in the whole tuple */
382 for (i
= 0; i
< _tupleLength
; i
++) {
383 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ (i
* 2));
384 dprintk(" 0x%02x: 0x%02x %c\n",
386 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
388 _address
+= (_tupleLength
* 2);
391 *tupleType
= _tupleType
;
392 *tupleLength
= _tupleLength
;
399 * Parse attribute memory of a CAM module, extracting Config register, and checking
400 * it is a DVB CAM module.
402 * @param ca CA instance.
403 * @param slot Slot id.
405 * @return 0 on success, <0 on failure.
407 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
416 int got_cftableentry
= 0;
425 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
427 if (tupleType
!= 0x1D)
434 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
436 if (tupleType
!= 0x1C)
443 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
445 if (tupleType
!= 0x15)
451 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
452 &tupleLength
, tuple
)) < 0)
454 if (tupleType
!= 0x20)
456 if (tupleLength
!= 4)
458 manfid
= (tuple
[1] << 8) | tuple
[0];
459 devid
= (tuple
[3] << 8) | tuple
[2];
464 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
465 &tupleLength
, tuple
)) < 0)
467 if (tupleType
!= 0x1A)
472 /* extract the configbase */
474 if (tupleLength
< (3 + rasz
+ 14))
476 ca
->slot_info
[slot
].config_base
= 0;
477 for (i
= 0; i
< rasz
+ 1; i
++) {
478 ca
->slot_info
[slot
].config_base
|= (tuple
[2 + i
] << (8 * i
));
481 /* check it contains the correct DVB string */
482 dvb_str
= findstr((char *)tuple
, tupleLength
, "DVB_CI_V", 8);
485 if (tupleLength
< ((dvb_str
- (char *) tuple
) + 12))
488 /* is it a version we support? */
489 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
490 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
491 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9], dvb_str
[10], dvb_str
[11]);
495 /* process the CFTABLE_ENTRY tuples, and any after those */
496 while ((!end_chain
) && (address
< 0x1000)) {
497 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
498 &tupleLength
, tuple
)) < 0)
501 case 0x1B: // CISTPL_CFTABLE_ENTRY
502 if (tupleLength
< (2 + 11 + 17))
505 /* if we've already parsed one, just use it */
506 if (got_cftableentry
)
509 /* get the config option */
510 ca
->slot_info
[slot
].config_option
= tuple
[0] & 0x3f;
512 /* OK, check it contains the correct strings */
513 if ((findstr((char *)tuple
, tupleLength
, "DVB_HOST", 8) == NULL
) ||
514 (findstr((char *)tuple
, tupleLength
, "DVB_CI_MODULE", 13) == NULL
))
517 got_cftableentry
= 1;
520 case 0x14: // CISTPL_NO_LINK
523 case 0xFF: // CISTPL_END
527 default: /* Unknown tuple type - just skip this tuple and move to the next one */
528 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType
,
534 if ((address
> 0x1000) || (!got_cftableentry
))
537 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
538 manfid
, devid
, ca
->slot_info
[slot
].config_base
, ca
->slot_info
[slot
].config_option
);
546 * Set CAM's configoption correctly.
548 * @param ca CA instance.
549 * @param slot Slot containing the CAM.
551 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
555 dprintk("%s\n", __func__
);
557 /* set the config option */
558 ca
->pub
->write_attribute_mem(ca
->pub
, slot
,
559 ca
->slot_info
[slot
].config_base
,
560 ca
->slot_info
[slot
].config_option
);
563 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, ca
->slot_info
[slot
].config_base
);
564 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
565 ca
->slot_info
[slot
].config_option
, configoption
& 0x3f);
574 * This function talks to an EN50221 CAM control interface. It reads a buffer of
575 * data from the CAM. The data can either be stored in a supplied buffer, or
576 * automatically be added to the slot's rx_buffer.
578 * @param ca CA instance.
579 * @param slot Slot to read from.
580 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
581 * the data will be added into the buffering system as a normal fragment.
582 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
584 * @return Number of bytes read, or < 0 on error
586 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
)
590 u8 buf
[HOST_LINK_BUF_SIZE
];
593 dprintk("%s\n", __func__
);
595 /* check if we have space for a link buf in the rx_buffer */
599 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
603 buf_free
= dvb_ringbuffer_free(&ca
->slot_info
[slot
].rx_buffer
);
605 if (buf_free
< (ca
->slot_info
[slot
].link_buf_size
+ DVB_RINGBUFFER_PKTHDRSIZE
)) {
611 /* check if there is data available */
612 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
614 if (!(status
& STATUSREG_DA
)) {
620 /* read the amount of data */
621 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
)) < 0)
623 bytes_read
= status
<< 8;
624 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
)) < 0)
626 bytes_read
|= status
;
628 /* check it will fit */
630 if (bytes_read
> ca
->slot_info
[slot
].link_buf_size
) {
631 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
632 ca
->dvbdev
->adapter
->num
, bytes_read
, ca
->slot_info
[slot
].link_buf_size
);
633 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
637 if (bytes_read
< 2) {
638 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
639 ca
->dvbdev
->adapter
->num
);
640 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
645 if (bytes_read
> ecount
) {
646 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
647 ca
->dvbdev
->adapter
->num
);
653 /* fill the buffer */
654 for (i
= 0; i
< bytes_read
; i
++) {
655 /* read byte and check */
656 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_DATA
)) < 0)
659 /* OK, store it in the buffer */
663 /* check for read error (RE should now be 0) */
664 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
666 if (status
& STATUSREG_RE
) {
667 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
672 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
674 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
678 dvb_ringbuffer_pkt_write(&ca
->slot_info
[slot
].rx_buffer
, buf
, bytes_read
);
680 memcpy(ebuf
, buf
, bytes_read
);
683 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
684 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
686 /* wake up readers when a last_fragment is received */
687 if ((buf
[1] & 0x80) == 0x00) {
688 wake_up_interruptible(&ca
->wait_queue
);
698 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
701 * @param ca CA instance.
702 * @param slot Slot to write to.
703 * @param ebuf The data in this buffer is treated as a complete link-level packet to
705 * @param count Size of ebuf.
707 * @return Number of bytes written, or < 0 on error.
709 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* buf
, int bytes_write
)
714 dprintk("%s\n", __func__
);
718 if (bytes_write
> ca
->slot_info
[slot
].link_buf_size
)
721 /* it is possible we are dealing with a single buffer implementation,
722 thus if there is data available for read or if there is even a read
723 already in progress, we do nothing but awake the kernel thread to
724 process the data if necessary. */
725 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
727 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
728 if (status
& STATUSREG_DA
)
729 dvb_ca_en50221_thread_wakeup(ca
);
736 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
737 IRQEN
| CMDREG_HC
)) != 0)
740 /* check if interface is still free */
741 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
743 if (!(status
& STATUSREG_FR
)) {
744 /* it wasn't free => try again later */
749 /* send the amount of data */
750 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
, bytes_write
>> 8)) != 0)
752 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
753 bytes_write
& 0xff)) != 0)
756 /* send the buffer */
757 for (i
= 0; i
< bytes_write
; i
++) {
758 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
, buf
[i
])) != 0)
762 /* check for write error (WE should now be 0) */
763 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
765 if (status
& STATUSREG_WE
) {
766 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
770 status
= bytes_write
;
772 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
773 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
776 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
781 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
785 /* ******************************************************************************** */
786 /* EN50221 higher level functions */
790 * A CAM has been removed => shut it down.
792 * @param ca CA instance.
793 * @param slot Slot to shut down.
795 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
797 dprintk("%s\n", __func__
);
799 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
800 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
802 /* need to wake up all processes to check if they're now
803 trying to write to a defunct CAM */
804 wake_up_interruptible(&ca
->wait_queue
);
806 dprintk("Slot %i shutdown\n", slot
);
811 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
815 * A CAMCHANGE IRQ has occurred.
817 * @param ca CA instance.
818 * @param slot Slot concerned.
819 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
821 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
, int change_type
)
823 struct dvb_ca_private
*ca
= pubca
->private;
825 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
827 switch (change_type
) {
828 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
829 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
836 ca
->slot_info
[slot
].camchange_type
= change_type
;
837 atomic_inc(&ca
->slot_info
[slot
].camchange_count
);
838 dvb_ca_en50221_thread_wakeup(ca
);
840 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
844 * A CAMREADY IRQ has occurred.
846 * @param ca CA instance.
847 * @param slot Slot concerned.
849 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
851 struct dvb_ca_private
*ca
= pubca
->private;
853 dprintk("CAMREADY IRQ slot:%i\n", slot
);
855 if (ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
856 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
857 dvb_ca_en50221_thread_wakeup(ca
);
863 * An FR or DA IRQ has occurred.
865 * @param ca CA instance.
866 * @param slot Slot concerned.
868 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
870 struct dvb_ca_private
*ca
= pubca
->private;
873 dprintk("FR/DA IRQ slot:%i\n", slot
);
875 switch (ca
->slot_info
[slot
].slot_state
) {
876 case DVB_CA_SLOTSTATE_LINKINIT
:
877 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
878 if (flags
& STATUSREG_DA
) {
879 dprintk("CAM supports DA IRQ\n");
880 ca
->slot_info
[slot
].da_irq_supported
= 1;
884 case DVB_CA_SLOTSTATE_RUNNING
:
886 dvb_ca_en50221_thread_wakeup(ca
);
893 /* ******************************************************************************** */
894 /* EN50221 thread functions */
897 * Wake up the DVB CA thread
899 * @param ca CA instance.
901 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
904 dprintk("%s\n", __func__
);
908 wake_up_process(ca
->thread
);
912 * Update the delay used by the thread.
914 * @param ca CA instance.
916 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
919 int curdelay
= 100000000;
922 /* Beware of too high polling frequency, because one polling
923 * call might take several hundred milliseconds until timeout!
925 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
926 switch (ca
->slot_info
[slot
].slot_state
) {
928 case DVB_CA_SLOTSTATE_NONE
:
929 delay
= HZ
* 60; /* 60s */
930 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
931 delay
= HZ
* 5; /* 5s */
933 case DVB_CA_SLOTSTATE_INVALID
:
934 delay
= HZ
* 60; /* 60s */
935 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
936 delay
= HZ
/ 10; /* 100ms */
939 case DVB_CA_SLOTSTATE_UNINITIALISED
:
940 case DVB_CA_SLOTSTATE_WAITREADY
:
941 case DVB_CA_SLOTSTATE_VALIDATE
:
942 case DVB_CA_SLOTSTATE_WAITFR
:
943 case DVB_CA_SLOTSTATE_LINKINIT
:
944 delay
= HZ
/ 10; /* 100ms */
947 case DVB_CA_SLOTSTATE_RUNNING
:
948 delay
= HZ
* 60; /* 60s */
949 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
))
950 delay
= HZ
/ 10; /* 100ms */
952 if ((!ca
->slot_info
[slot
].da_irq_supported
) ||
953 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
)))
954 delay
= HZ
/ 10; /* 100ms */
959 if (delay
< curdelay
)
963 ca
->delay
= curdelay
;
969 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
971 static int dvb_ca_en50221_thread(void *data
)
973 struct dvb_ca_private
*ca
= data
;
980 dprintk("%s\n", __func__
);
982 /* choose the correct initial delay */
983 dvb_ca_en50221_thread_update_delay(ca
);
986 while (!kthread_should_stop()) {
987 /* sleep for a bit */
989 set_current_state(TASK_INTERRUPTIBLE
);
990 schedule_timeout(ca
->delay
);
991 if (kthread_should_stop())
996 /* go through all the slots processing them */
997 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
999 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1001 // check the cam status + deal with CAMCHANGEs
1002 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1003 /* clear down an old CI slot if necessary */
1004 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1005 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1007 /* if a CAM is NOW present, initialise it */
1008 if (ca
->slot_info
[slot
].camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
) {
1009 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1012 /* we've handled one CAMCHANGE */
1013 dvb_ca_en50221_thread_update_delay(ca
);
1014 atomic_dec(&ca
->slot_info
[slot
].camchange_count
);
1017 // CAM state machine
1018 switch (ca
->slot_info
[slot
].slot_state
) {
1019 case DVB_CA_SLOTSTATE_NONE
:
1020 case DVB_CA_SLOTSTATE_INVALID
:
1024 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1025 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1026 ca
->pub
->slot_reset(ca
->pub
, slot
);
1027 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1030 case DVB_CA_SLOTSTATE_WAITREADY
:
1031 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1032 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1033 ca
->dvbdev
->adapter
->num
);
1034 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1035 dvb_ca_en50221_thread_update_delay(ca
);
1038 // no other action needed; will automatically change state when ready
1041 case DVB_CA_SLOTSTATE_VALIDATE
:
1042 if (dvb_ca_en50221_parse_attributes(ca
, slot
) != 0) {
1043 /* we need this extra check for annoying interfaces like the budget-av */
1044 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1045 (ca
->pub
->poll_slot_status
)) {
1046 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1047 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1048 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1049 dvb_ca_en50221_thread_update_delay(ca
);
1054 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1055 ca
->dvbdev
->adapter
->num
);
1056 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1057 dvb_ca_en50221_thread_update_delay(ca
);
1060 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1061 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1062 ca
->dvbdev
->adapter
->num
);
1063 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1064 dvb_ca_en50221_thread_update_delay(ca
);
1067 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1068 CTRLIF_COMMAND
, CMDREG_RS
) != 0) {
1069 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1070 ca
->dvbdev
->adapter
->num
);
1071 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1072 dvb_ca_en50221_thread_update_delay(ca
);
1075 dprintk("DVB CAM validated successfully\n");
1077 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1078 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1082 case DVB_CA_SLOTSTATE_WAITFR
:
1083 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1084 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1085 ca
->dvbdev
->adapter
->num
);
1086 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1087 dvb_ca_en50221_thread_update_delay(ca
);
1091 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1092 if (flags
& STATUSREG_FR
) {
1093 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1098 case DVB_CA_SLOTSTATE_LINKINIT
:
1099 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1100 /* we need this extra check for annoying interfaces like the budget-av */
1101 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1102 (ca
->pub
->poll_slot_status
)) {
1103 status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1104 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1105 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1106 dvb_ca_en50221_thread_update_delay(ca
);
1111 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca
->dvbdev
->adapter
->num
);
1112 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1113 dvb_ca_en50221_thread_update_delay(ca
);
1117 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1118 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1119 if (rxbuf
== NULL
) {
1120 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca
->dvbdev
->adapter
->num
);
1121 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1122 dvb_ca_en50221_thread_update_delay(ca
);
1125 dvb_ringbuffer_init(&ca
->slot_info
[slot
].rx_buffer
, rxbuf
, RX_BUFFER_SIZE
);
1128 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1129 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1130 dvb_ca_en50221_thread_update_delay(ca
);
1131 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca
->dvbdev
->adapter
->num
);
1134 case DVB_CA_SLOTSTATE_RUNNING
:
1138 // poll slots for data
1140 while ((status
= dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0)) > 0) {
1144 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1145 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1146 // we dont want to sleep on the next iteration so we can handle the cam change
1151 /* check if we've hit our limit this time */
1152 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1153 // dont sleep; there is likely to be more data to read
1161 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1170 /* ******************************************************************************** */
1171 /* EN50221 IO interface functions */
1174 * Real ioctl implementation.
1175 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1177 * @param inode Inode concerned.
1178 * @param file File concerned.
1179 * @param cmd IOCTL command.
1180 * @param arg Associated argument.
1182 * @return 0 on success, <0 on error.
1184 static int dvb_ca_en50221_io_do_ioctl(struct inode
*inode
, struct file
*file
,
1185 unsigned int cmd
, void *parg
)
1187 struct dvb_device
*dvbdev
= file
->private_data
;
1188 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1192 dprintk("%s\n", __func__
);
1196 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1197 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1198 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1199 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1200 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1201 dvb_ca_en50221_camchange_irq(ca
->pub
,
1203 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1205 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1207 ca
->next_read_slot
= 0;
1208 dvb_ca_en50221_thread_wakeup(ca
);
1212 struct ca_caps
*caps
= parg
;
1214 caps
->slot_num
= ca
->slot_count
;
1215 caps
->slot_type
= CA_CI_LINK
;
1216 caps
->descr_num
= 0;
1217 caps
->descr_type
= 0;
1221 case CA_GET_SLOT_INFO
: {
1222 struct ca_slot_info
*info
= parg
;
1224 if ((info
->num
> ca
->slot_count
) || (info
->num
< 0))
1227 info
->type
= CA_CI_LINK
;
1229 if ((ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1230 && (ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_INVALID
)) {
1231 info
->flags
= CA_CI_MODULE_PRESENT
;
1233 if (ca
->slot_info
[info
->num
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1234 info
->flags
|= CA_CI_MODULE_READY
;
1249 * Wrapper for ioctl implementation.
1251 * @param inode Inode concerned.
1252 * @param file File concerned.
1253 * @param cmd IOCTL command.
1254 * @param arg Associated argument.
1256 * @return 0 on success, <0 on error.
1258 static int dvb_ca_en50221_io_ioctl(struct inode
*inode
, struct file
*file
,
1259 unsigned int cmd
, unsigned long arg
)
1261 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_ca_en50221_io_do_ioctl
);
1266 * Implementation of write() syscall.
1268 * @param file File structure.
1269 * @param buf Source buffer.
1270 * @param count Size of source buffer.
1271 * @param ppos Position in file (ignored).
1273 * @return Number of bytes read, or <0 on error.
1275 static ssize_t
dvb_ca_en50221_io_write(struct file
*file
,
1276 const char __user
* buf
, size_t count
, loff_t
* ppos
)
1278 struct dvb_device
*dvbdev
= file
->private_data
;
1279 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1280 u8 slot
, connection_id
;
1282 u8 fragbuf
[HOST_LINK_BUF_SIZE
];
1285 unsigned long timeout
;
1288 dprintk("%s\n", __func__
);
1290 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1294 /* extract slot & connection id */
1295 if (copy_from_user(&slot
, buf
, 1))
1297 if (copy_from_user(&connection_id
, buf
+ 1, 1))
1302 /* check if the slot is actually running */
1303 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1306 /* fragment the packets & store in the buffer */
1307 while (fragpos
< count
) {
1308 fraglen
= ca
->slot_info
[slot
].link_buf_size
- 2;
1309 if ((count
- fragpos
) < fraglen
)
1310 fraglen
= count
- fragpos
;
1312 fragbuf
[0] = connection_id
;
1313 fragbuf
[1] = ((fragpos
+ fraglen
) < count
) ? 0x80 : 0x00;
1314 if ((status
= copy_from_user(fragbuf
+ 2, buf
+ fragpos
, fraglen
)) != 0)
1317 timeout
= jiffies
+ HZ
/ 2;
1319 while (!time_after(jiffies
, timeout
)) {
1320 /* check the CAM hasn't been removed/reset in the meantime */
1321 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
) {
1326 mutex_lock(&ca
->slot_info
[slot
].slot_lock
);
1327 status
= dvb_ca_en50221_write_data(ca
, slot
, fragbuf
, fraglen
+ 2);
1328 mutex_unlock(&ca
->slot_info
[slot
].slot_lock
);
1329 if (status
== (fraglen
+ 2)) {
1333 if (status
!= -EAGAIN
)
1353 * Condition for waking up in dvb_ca_en50221_io_read_condition
1355 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private
*ca
,
1356 int *result
, int *_slot
)
1362 int connection_id
= -1;
1366 slot
= ca
->next_read_slot
;
1367 while ((slot_count
< ca
->slot_count
) && (!found
)) {
1368 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1371 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1375 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1377 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1378 if (connection_id
== -1)
1379 connection_id
= hdr
[0];
1380 if ((hdr
[0] == connection_id
) && ((hdr
[1] & 0x80) == 0)) {
1386 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1390 slot
= (slot
+ 1) % ca
->slot_count
;
1394 ca
->next_read_slot
= slot
;
1400 * Implementation of read() syscall.
1402 * @param file File structure.
1403 * @param buf Destination buffer.
1404 * @param count Size of destination buffer.
1405 * @param ppos Position in file (ignored).
1407 * @return Number of bytes read, or <0 on error.
1409 static ssize_t
dvb_ca_en50221_io_read(struct file
*file
, char __user
* buf
,
1410 size_t count
, loff_t
* ppos
)
1412 struct dvb_device
*dvbdev
= file
->private_data
;
1413 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1418 int connection_id
= -1;
1420 int last_fragment
= 0;
1425 dprintk("%s\n", __func__
);
1427 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1431 /* wait for some data */
1432 if ((status
= dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
)) == 0) {
1434 /* if we're in nonblocking mode, exit immediately */
1435 if (file
->f_flags
& O_NONBLOCK
)
1436 return -EWOULDBLOCK
;
1438 /* wait for some data */
1439 status
= wait_event_interruptible(ca
->wait_queue
,
1440 dvb_ca_en50221_io_read_condition
1441 (ca
, &result
, &slot
));
1443 if ((status
< 0) || (result
< 0)) {
1449 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1453 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca
->dvbdev
->adapter
->num
);
1458 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2);
1459 if (connection_id
== -1)
1460 connection_id
= hdr
[0];
1461 if (hdr
[0] == connection_id
) {
1462 if (pktlen
< count
) {
1463 if ((pktlen
+ fraglen
- 2) > count
) {
1464 fraglen
= count
- pktlen
;
1469 if ((status
= dvb_ringbuffer_pkt_read_user(&ca
->slot_info
[slot
].rx_buffer
, idx
, 2,
1470 buf
+ pktlen
, fraglen
)) < 0) {
1476 if ((hdr
[1] & 0x80) == 0)
1481 idx2
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1483 dvb_ringbuffer_pkt_dispose(&ca
->slot_info
[slot
].rx_buffer
, idx
);
1486 } while (!last_fragment
);
1489 hdr
[1] = connection_id
;
1490 if ((status
= copy_to_user(buf
, hdr
, 2)) != 0)
1500 * Implementation of file open syscall.
1502 * @param inode Inode concerned.
1503 * @param file File concerned.
1505 * @return 0 on success, <0 on failure.
1507 static int dvb_ca_en50221_io_open(struct inode
*inode
, struct file
*file
)
1509 struct dvb_device
*dvbdev
= file
->private_data
;
1510 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1514 dprintk("%s\n", __func__
);
1516 if (!try_module_get(ca
->pub
->owner
))
1519 err
= dvb_generic_open(inode
, file
);
1521 module_put(ca
->pub
->owner
);
1525 for (i
= 0; i
< ca
->slot_count
; i
++) {
1527 if (ca
->slot_info
[i
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1528 if (ca
->slot_info
[i
].rx_buffer
.data
!= NULL
) {
1529 /* it is safe to call this here without locks because
1530 * ca->open == 0. Data is not read in this case */
1531 dvb_ringbuffer_flush(&ca
->slot_info
[i
].rx_buffer
);
1537 dvb_ca_en50221_thread_update_delay(ca
);
1538 dvb_ca_en50221_thread_wakeup(ca
);
1545 * Implementation of file close syscall.
1547 * @param inode Inode concerned.
1548 * @param file File concerned.
1550 * @return 0 on success, <0 on failure.
1552 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1554 struct dvb_device
*dvbdev
= file
->private_data
;
1555 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1558 dprintk("%s\n", __func__
);
1560 /* mark the CA device as closed */
1562 dvb_ca_en50221_thread_update_delay(ca
);
1564 err
= dvb_generic_release(inode
, file
);
1566 module_put(ca
->pub
->owner
);
1573 * Implementation of poll() syscall.
1575 * @param file File concerned.
1576 * @param wait poll wait table.
1578 * @return Standard poll mask.
1580 static unsigned int dvb_ca_en50221_io_poll(struct file
*file
, poll_table
* wait
)
1582 struct dvb_device
*dvbdev
= file
->private_data
;
1583 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1584 unsigned int mask
= 0;
1588 dprintk("%s\n", __func__
);
1590 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1594 /* if there is something, return now */
1598 /* wait for something to happen */
1599 poll_wait(file
, &ca
->wait_queue
, wait
);
1601 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1607 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1610 static const struct file_operations dvb_ca_fops
= {
1611 .owner
= THIS_MODULE
,
1612 .read
= dvb_ca_en50221_io_read
,
1613 .write
= dvb_ca_en50221_io_write
,
1614 .ioctl
= dvb_ca_en50221_io_ioctl
,
1615 .open
= dvb_ca_en50221_io_open
,
1616 .release
= dvb_ca_en50221_io_release
,
1617 .poll
= dvb_ca_en50221_io_poll
,
1620 static struct dvb_device dvbdev_ca
= {
1625 .fops
= &dvb_ca_fops
,
1629 /* ******************************************************************************** */
1630 /* Initialisation/shutdown functions */
1634 * Initialise a new DVB CA EN50221 interface device.
1636 * @param dvb_adapter DVB adapter to attach the new CA device to.
1637 * @param ca The dvb_ca instance.
1638 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1639 * @param slot_count Number of slots supported.
1641 * @return 0 on success, nonzero on failure
1643 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1644 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1647 struct dvb_ca_private
*ca
= NULL
;
1650 dprintk("%s\n", __func__
);
1655 /* initialise the system data */
1656 if ((ca
= kzalloc(sizeof(struct dvb_ca_private
), GFP_KERNEL
)) == NULL
) {
1662 ca
->slot_count
= slot_count
;
1663 if ((ca
->slot_info
= kcalloc(slot_count
, sizeof(struct dvb_ca_slot
), GFP_KERNEL
)) == NULL
) {
1667 init_waitqueue_head(&ca
->wait_queue
);
1670 ca
->next_read_slot
= 0;
1671 pubca
->private = ca
;
1673 /* register the DVB device */
1674 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
, DVB_DEVICE_CA
);
1678 /* now initialise each slot */
1679 for (i
= 0; i
< slot_count
; i
++) {
1680 memset(&ca
->slot_info
[i
], 0, sizeof(struct dvb_ca_slot
));
1681 ca
->slot_info
[i
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1682 atomic_set(&ca
->slot_info
[i
].camchange_count
, 0);
1683 ca
->slot_info
[i
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1684 mutex_init(&ca
->slot_info
[i
].slot_lock
);
1687 if (signal_pending(current
)) {
1693 /* create a kthread for monitoring this CA device */
1694 ca
->thread
= kthread_run(dvb_ca_en50221_thread
, ca
, "kdvb-ca-%i:%i",
1695 ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
1696 if (IS_ERR(ca
->thread
)) {
1697 ret
= PTR_ERR(ca
->thread
);
1698 printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
1706 if (ca
->dvbdev
!= NULL
)
1707 dvb_unregister_device(ca
->dvbdev
);
1708 kfree(ca
->slot_info
);
1711 pubca
->private = NULL
;
1714 EXPORT_SYMBOL(dvb_ca_en50221_release
);
1719 * Release a DVB CA EN50221 interface device.
1721 * @param ca_dev The dvb_device_t instance for the CA device.
1722 * @param ca The associated dvb_ca instance.
1724 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1726 struct dvb_ca_private
*ca
= pubca
->private;
1729 dprintk("%s\n", __func__
);
1731 /* shutdown the thread if there was one */
1732 kthread_stop(ca
->thread
);
1734 for (i
= 0; i
< ca
->slot_count
; i
++) {
1735 dvb_ca_en50221_slot_shutdown(ca
, i
);
1736 vfree(ca
->slot_info
[i
].rx_buffer
.data
);
1738 kfree(ca
->slot_info
);
1739 dvb_unregister_device(ca
->dvbdev
);
1741 pubca
->private = NULL
;