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/moduleparam.h>
36 #include <linux/vmalloc.h>
37 #include <linux/delay.h>
38 #include <linux/rwsem.h>
40 #include "dvb_ca_en50221.h"
41 #include "dvb_ringbuffer.h"
43 static int dvb_ca_en50221_debug
;
45 module_param_named(cam_debug
, dvb_ca_en50221_debug
, int, 0644);
46 MODULE_PARM_DESC(cam_debug
, "enable verbose debug messages");
48 #define dprintk if (dvb_ca_en50221_debug) printk
50 #define INIT_TIMEOUT_SECS 5
52 #define HOST_LINK_BUF_SIZE 0x200
54 #define RX_BUFFER_SIZE 65535
56 #define MAX_RX_PACKETS_PER_ITERATION 10
59 #define CTRLIF_COMMAND 1
60 #define CTRLIF_STATUS 1
61 #define CTRLIF_SIZE_LOW 2
62 #define CTRLIF_SIZE_HIGH 3
64 #define CMDREG_HC 1 /* Host control */
65 #define CMDREG_SW 2 /* Size write */
66 #define CMDREG_SR 4 /* Size read */
67 #define CMDREG_RS 8 /* Reset interface */
68 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
69 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
70 #define IRQEN (CMDREG_DAIE)
72 #define STATUSREG_RE 1 /* read error */
73 #define STATUSREG_WE 2 /* write error */
74 #define STATUSREG_FR 0x40 /* module free */
75 #define STATUSREG_DA 0x80 /* data available */
76 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
79 #define DVB_CA_SLOTSTATE_NONE 0
80 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
81 #define DVB_CA_SLOTSTATE_RUNNING 2
82 #define DVB_CA_SLOTSTATE_INVALID 3
83 #define DVB_CA_SLOTSTATE_WAITREADY 4
84 #define DVB_CA_SLOTSTATE_VALIDATE 5
85 #define DVB_CA_SLOTSTATE_WAITFR 6
86 #define DVB_CA_SLOTSTATE_LINKINIT 7
89 /* Information on a CA slot */
92 /* current state of the CAM */
95 /* Number of CAMCHANGES that have occurred since last processing */
96 atomic_t camchange_count
;
98 /* Type of last CAMCHANGE */
101 /* base address of CAM config */
104 /* value to write into Config Control register */
107 /* if 1, the CAM supports DA IRQs */
108 u8 da_irq_supported
:1;
110 /* size of the buffer to use when talking to the CAM */
113 /* semaphore for syncing access to slot structure */
114 struct rw_semaphore sem
;
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
{
126 /* pointer back to the public data structure */
127 struct dvb_ca_en50221
*pub
;
130 struct dvb_device
*dvbdev
;
132 /* Flags describing the interface (DVB_CA_FLAG_*) */
135 /* number of slots supported by this CA interface */
136 unsigned int slot_count
;
138 /* information on each slot */
139 struct dvb_ca_slot
*slot_info
;
141 /* wait queues for read() and write() operations */
142 wait_queue_head_t wait_queue
;
144 /* PID of the monitoring thread */
147 /* Wait queue used when shutting thread down */
148 wait_queue_head_t thread_queue
;
150 /* Flag indicating when thread should exit */
153 /* Flag indicating if the CA device is open */
156 /* Flag indicating the thread should wake up now */
157 unsigned int wakeup
:1;
159 /* Delay the main thread should use */
162 /* Slot to start looking for data to read from in the next user-space read operation */
166 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
167 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
168 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
172 * Safely find needle in haystack.
174 * @param haystack Buffer to look in.
175 * @param hlen Number of bytes in haystack.
176 * @param needle Buffer to find.
177 * @param nlen Number of bytes in needle.
178 * @return Pointer into haystack needle was found at, or NULL if not found.
180 static u8
*findstr(u8
* haystack
, int hlen
, u8
* needle
, int nlen
)
187 for (i
= 0; i
<= hlen
- nlen
; i
++) {
188 if (!strncmp(haystack
+ i
, needle
, nlen
))
197 /* ******************************************************************************** */
198 /* EN50221 physical interface functions */
204 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private
*ca
, int slot
)
211 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
) {
212 return (atomic_read(&ca
->slot_info
[slot
].camchange_count
) != 0);
216 slot_status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, ca
->open
);
218 cam_present_now
= (slot_status
& DVB_CA_EN50221_POLL_CAM_PRESENT
) ? 1 : 0;
219 cam_changed
= (slot_status
& DVB_CA_EN50221_POLL_CAM_CHANGED
) ? 1 : 0;
221 int cam_present_old
= (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
);
222 cam_changed
= (cam_present_now
!= cam_present_old
);
226 if (!cam_present_now
) {
227 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
229 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
231 atomic_set(&ca
->slot_info
[slot
].camchange_count
, 1);
233 if ((ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) &&
234 (slot_status
& DVB_CA_EN50221_POLL_CAM_READY
)) {
235 // move to validate state if reset is completed
236 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
245 * Wait for flags to become set on the STATUS register on a CAM interface,
246 * checking for errors and timeout.
248 * @param ca CA instance.
249 * @param slot Slot on interface.
250 * @param waitfor Flags to wait for.
251 * @param timeout_ms Timeout in milliseconds.
253 * @return 0 on success, nonzero on error.
255 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
256 u8 waitfor
, int timeout_hz
)
258 unsigned long timeout
;
261 dprintk("%s\n", __FUNCTION__
);
263 /* loop until timeout elapsed */
265 timeout
= jiffies
+ timeout_hz
;
267 /* read the status and check for error */
268 int res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
272 /* if we got the flags, it was successful! */
274 dprintk("%s succeeded timeout:%lu\n", __FUNCTION__
, jiffies
- start
);
278 /* check for timeout */
279 if (time_after(jiffies
, timeout
)) {
287 dprintk("%s failed timeout:%lu\n", __FUNCTION__
, jiffies
- start
);
289 /* if we get here, we've timed out */
295 * Initialise the link layer connection to a CAM.
297 * @param ca CA instance.
298 * @param slot Slot id.
300 * @return 0 on success, nonzero on failure.
302 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
308 dprintk("%s\n", __FUNCTION__
);
310 /* we'll be determining these during this function */
311 ca
->slot_info
[slot
].da_irq_supported
= 0;
313 /* set the host link buffer size temporarily. it will be overwritten with the
314 * real negotiated size later. */
315 ca
->slot_info
[slot
].link_buf_size
= 2;
317 /* read the buffer size from the CAM */
318 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SR
)) != 0)
320 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
/ 10)) != 0)
322 if ((ret
= dvb_ca_en50221_read_data(ca
, slot
, buf
, 2)) != 2)
324 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
327 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
328 buf_size
= (buf
[0] << 8) | buf
[1];
329 if (buf_size
> HOST_LINK_BUF_SIZE
)
330 buf_size
= HOST_LINK_BUF_SIZE
;
331 ca
->slot_info
[slot
].link_buf_size
= buf_size
;
332 buf
[0] = buf_size
>> 8;
333 buf
[1] = buf_size
& 0xff;
334 dprintk("Chosen link buffer size of %i\n", buf_size
);
336 /* write the buffer size to the CAM */
337 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SW
)) != 0)
339 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_FR
, HZ
/ 10)) != 0)
341 if ((ret
= dvb_ca_en50221_write_data(ca
, slot
, buf
, 2)) != 2)
343 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
351 * Read a tuple from attribute memory.
353 * @param ca CA instance.
354 * @param slot Slot id.
355 * @param address Address to read from. Updated.
356 * @param tupleType Tuple id byte. Updated.
357 * @param tupleLength Tuple length. Updated.
358 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
360 * @return 0 on success, nonzero on error.
362 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private
*ca
, int slot
,
363 int *address
, int *tupleType
, int *tupleLength
, u8
* tuple
)
368 int _address
= *address
;
370 /* grab the next tuple length and type */
371 if ((_tupleType
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
)) < 0)
373 if (_tupleType
== 0xff) {
374 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType
);
376 *tupleType
= _tupleType
;
380 if ((_tupleLength
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ 2)) < 0)
384 dprintk("TUPLE type:0x%x length:%i\n", _tupleType
, _tupleLength
);
386 /* read in the whole tuple */
387 for (i
= 0; i
< _tupleLength
; i
++) {
388 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ (i
* 2));
389 dprintk(" 0x%02x: 0x%02x %c\n",
391 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
393 _address
+= (_tupleLength
* 2);
396 *tupleType
= _tupleType
;
397 *tupleLength
= _tupleLength
;
404 * Parse attribute memory of a CAM module, extracting Config register, and checking
405 * it is a DVB CAM module.
407 * @param ca CA instance.
408 * @param slot Slot id.
410 * @return 0 on success, <0 on failure.
412 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
421 int got_cftableentry
= 0;
430 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
432 if (tupleType
!= 0x1D)
439 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
441 if (tupleType
!= 0x1C)
448 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
450 if (tupleType
!= 0x15)
456 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
457 &tupleLength
, tuple
)) < 0)
459 if (tupleType
!= 0x20)
461 if (tupleLength
!= 4)
463 manfid
= (tuple
[1] << 8) | tuple
[0];
464 devid
= (tuple
[3] << 8) | tuple
[2];
469 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
470 &tupleLength
, tuple
)) < 0)
472 if (tupleType
!= 0x1A)
477 /* extract the configbase */
479 if (tupleLength
< (3 + rasz
+ 14))
481 ca
->slot_info
[slot
].config_base
= 0;
482 for (i
= 0; i
< rasz
+ 1; i
++) {
483 ca
->slot_info
[slot
].config_base
|= (tuple
[2 + i
] << (8 * i
));
486 /* check it contains the correct DVB string */
487 dvb_str
= findstr(tuple
, tupleLength
, "DVB_CI_V", 8);
490 if (tupleLength
< ((dvb_str
- (char *) tuple
) + 12))
493 /* is it a version we support? */
494 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
495 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
496 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9], dvb_str
[10], dvb_str
[11]);
500 /* process the CFTABLE_ENTRY tuples, and any after those */
501 while ((!end_chain
) && (address
< 0x1000)) {
502 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
503 &tupleLength
, tuple
)) < 0)
506 case 0x1B: // CISTPL_CFTABLE_ENTRY
507 if (tupleLength
< (2 + 11 + 17))
510 /* if we've already parsed one, just use it */
511 if (got_cftableentry
)
514 /* get the config option */
515 ca
->slot_info
[slot
].config_option
= tuple
[0] & 0x3f;
517 /* OK, check it contains the correct strings */
518 if ((findstr(tuple
, tupleLength
, "DVB_HOST", 8) == NULL
) ||
519 (findstr(tuple
, tupleLength
, "DVB_CI_MODULE", 13) == NULL
))
522 got_cftableentry
= 1;
525 case 0x14: // CISTPL_NO_LINK
528 case 0xFF: // CISTPL_END
532 default: /* Unknown tuple type - just skip this tuple and move to the next one */
533 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType
,
539 if ((address
> 0x1000) || (!got_cftableentry
))
542 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
543 manfid
, devid
, ca
->slot_info
[slot
].config_base
, ca
->slot_info
[slot
].config_option
);
551 * Set CAM's configoption correctly.
553 * @param ca CA instance.
554 * @param slot Slot containing the CAM.
556 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
560 dprintk("%s\n", __FUNCTION__
);
562 /* set the config option */
563 ca
->pub
->write_attribute_mem(ca
->pub
, slot
,
564 ca
->slot_info
[slot
].config_base
,
565 ca
->slot_info
[slot
].config_option
);
568 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, ca
->slot_info
[slot
].config_base
);
569 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
570 ca
->slot_info
[slot
].config_option
, configoption
& 0x3f);
579 * This function talks to an EN50221 CAM control interface. It reads a buffer of
580 * data from the CAM. The data can either be stored in a supplied buffer, or
581 * automatically be added to the slot's rx_buffer.
583 * @param ca CA instance.
584 * @param slot Slot to read from.
585 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
586 * the data will be added into the buffering system as a normal fragment.
587 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
589 * @return Number of bytes read, or < 0 on error
591 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
)
595 u8 buf
[HOST_LINK_BUF_SIZE
];
598 dprintk("%s\n", __FUNCTION__
);
600 /* check if we have space for a link buf in the rx_buffer */
604 down_read(&ca
->slot_info
[slot
].sem
);
605 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
606 up_read(&ca
->slot_info
[slot
].sem
);
610 buf_free
= dvb_ringbuffer_free(&ca
->slot_info
[slot
].rx_buffer
);
611 up_read(&ca
->slot_info
[slot
].sem
);
613 if (buf_free
< (ca
->slot_info
[slot
].link_buf_size
+ DVB_RINGBUFFER_PKTHDRSIZE
)) {
619 /* check if there is data available */
620 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
622 if (!(status
& STATUSREG_DA
)) {
628 /* read the amount of data */
629 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
)) < 0)
631 bytes_read
= status
<< 8;
632 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
)) < 0)
634 bytes_read
|= status
;
636 /* check it will fit */
638 if (bytes_read
> ca
->slot_info
[slot
].link_buf_size
) {
639 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
640 ca
->dvbdev
->adapter
->num
, bytes_read
, ca
->slot_info
[slot
].link_buf_size
);
641 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
645 if (bytes_read
< 2) {
646 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
647 ca
->dvbdev
->adapter
->num
);
648 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
653 if (bytes_read
> ecount
) {
654 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
655 ca
->dvbdev
->adapter
->num
);
661 /* fill the buffer */
662 for (i
= 0; i
< bytes_read
; i
++) {
663 /* read byte and check */
664 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_DATA
)) < 0)
667 /* OK, store it in the buffer */
671 /* check for read error (RE should now be 0) */
672 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
674 if (status
& STATUSREG_RE
) {
675 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
680 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
682 down_read(&ca
->slot_info
[slot
].sem
);
683 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
684 up_read(&ca
->slot_info
[slot
].sem
);
688 dvb_ringbuffer_pkt_write(&ca
->slot_info
[slot
].rx_buffer
, buf
, bytes_read
);
689 up_read(&ca
->slot_info
[slot
].sem
);
691 memcpy(ebuf
, buf
, bytes_read
);
694 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
695 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
697 /* wake up readers when a last_fragment is received */
698 if ((buf
[1] & 0x80) == 0x00) {
699 wake_up_interruptible(&ca
->wait_queue
);
709 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
712 * @param ca CA instance.
713 * @param slot Slot to write to.
714 * @param ebuf The data in this buffer is treated as a complete link-level packet to
716 * @param count Size of ebuf.
718 * @return Number of bytes written, or < 0 on error.
720 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* buf
, int bytes_write
)
725 dprintk("%s\n", __FUNCTION__
);
729 if (bytes_write
> ca
->slot_info
[slot
].link_buf_size
)
732 /* check if interface is actually waiting for us to read from it, or if a read is in progress */
733 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
735 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
741 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
742 IRQEN
| CMDREG_HC
)) != 0)
745 /* check if interface is still free */
746 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
748 if (!(status
& STATUSREG_FR
)) {
749 /* it wasn't free => try again later */
754 /* send the amount of data */
755 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
, bytes_write
>> 8)) != 0)
757 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
758 bytes_write
& 0xff)) != 0)
761 /* send the buffer */
762 for (i
= 0; i
< bytes_write
; i
++) {
763 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
, buf
[i
])) != 0)
767 /* check for write error (WE should now be 0) */
768 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
770 if (status
& STATUSREG_WE
) {
771 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
775 status
= bytes_write
;
777 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
778 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
781 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
786 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
790 /* ******************************************************************************** */
791 /* EN50221 higher level functions */
795 * A CAM has been removed => shut it down.
797 * @param ca CA instance.
798 * @param slot Slot to shut down.
800 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
802 dprintk("%s\n", __FUNCTION__
);
804 down_write(&ca
->slot_info
[slot
].sem
);
805 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
806 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
807 vfree(ca
->slot_info
[slot
].rx_buffer
.data
);
808 ca
->slot_info
[slot
].rx_buffer
.data
= NULL
;
809 up_write(&ca
->slot_info
[slot
].sem
);
811 /* need to wake up all processes to check if they're now
812 trying to write to a defunct CAM */
813 wake_up_interruptible(&ca
->wait_queue
);
815 dprintk("Slot %i shutdown\n", slot
);
820 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
824 * A CAMCHANGE IRQ has occurred.
826 * @param ca CA instance.
827 * @param slot Slot concerned.
828 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
830 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
, int change_type
)
832 struct dvb_ca_private
*ca
= pubca
->private;
834 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
836 switch (change_type
) {
837 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
838 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
845 ca
->slot_info
[slot
].camchange_type
= change_type
;
846 atomic_inc(&ca
->slot_info
[slot
].camchange_count
);
847 dvb_ca_en50221_thread_wakeup(ca
);
849 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
853 * A CAMREADY IRQ has occurred.
855 * @param ca CA instance.
856 * @param slot Slot concerned.
858 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
860 struct dvb_ca_private
*ca
= pubca
->private;
862 dprintk("CAMREADY IRQ slot:%i\n", slot
);
864 if (ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
865 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
866 dvb_ca_en50221_thread_wakeup(ca
);
872 * An FR or DA IRQ has occurred.
874 * @param ca CA instance.
875 * @param slot Slot concerned.
877 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
879 struct dvb_ca_private
*ca
= pubca
->private;
882 dprintk("FR/DA IRQ slot:%i\n", slot
);
884 switch (ca
->slot_info
[slot
].slot_state
) {
885 case DVB_CA_SLOTSTATE_LINKINIT
:
886 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
887 if (flags
& STATUSREG_DA
) {
888 dprintk("CAM supports DA IRQ\n");
889 ca
->slot_info
[slot
].da_irq_supported
= 1;
893 case DVB_CA_SLOTSTATE_RUNNING
:
895 dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0);
902 /* ******************************************************************************** */
903 /* EN50221 thread functions */
906 * Wake up the DVB CA thread
908 * @param ca CA instance.
910 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
913 dprintk("%s\n", __FUNCTION__
);
917 wake_up_interruptible(&ca
->thread_queue
);
921 * Used by the CA thread to determine if an early wakeup is necessary
923 * @param ca CA instance.
925 static int dvb_ca_en50221_thread_should_wakeup(struct dvb_ca_private
*ca
)
939 * Update the delay used by the thread.
941 * @param ca CA instance.
943 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
946 int curdelay
= 100000000;
949 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
950 switch (ca
->slot_info
[slot
].slot_state
) {
952 case DVB_CA_SLOTSTATE_NONE
:
953 case DVB_CA_SLOTSTATE_INVALID
:
955 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) {
960 case DVB_CA_SLOTSTATE_UNINITIALISED
:
961 case DVB_CA_SLOTSTATE_WAITREADY
:
962 case DVB_CA_SLOTSTATE_VALIDATE
:
963 case DVB_CA_SLOTSTATE_WAITFR
:
964 case DVB_CA_SLOTSTATE_LINKINIT
:
968 case DVB_CA_SLOTSTATE_RUNNING
:
970 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) {
974 if ((!ca
->slot_info
[slot
].da_irq_supported
) ||
975 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
))) {
982 if (delay
< curdelay
)
986 ca
->delay
= curdelay
;
992 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
994 static int dvb_ca_en50221_thread(void *data
)
996 struct dvb_ca_private
*ca
= data
;
1004 dprintk("%s\n", __FUNCTION__
);
1006 /* setup kernel thread */
1007 snprintf(name
, sizeof(name
), "kdvb-ca-%i:%i", ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
1011 sigfillset(¤t
->blocked
);
1014 /* choose the correct initial delay */
1015 dvb_ca_en50221_thread_update_delay(ca
);
1019 /* sleep for a bit */
1021 flags
= wait_event_interruptible_timeout(ca
->thread_queue
,
1022 dvb_ca_en50221_thread_should_wakeup(ca
),
1024 if ((flags
== -ERESTARTSYS
) || ca
->exit
) {
1025 /* got signal or quitting */
1031 /* go through all the slots processing them */
1032 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1034 // check the cam status + deal with CAMCHANGEs
1035 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1036 /* clear down an old CI slot if necessary */
1037 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1038 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1040 /* if a CAM is NOW present, initialise it */
1041 if (ca
->slot_info
[slot
].camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
) {
1042 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1045 /* we've handled one CAMCHANGE */
1046 dvb_ca_en50221_thread_update_delay(ca
);
1047 atomic_dec(&ca
->slot_info
[slot
].camchange_count
);
1050 // CAM state machine
1051 switch (ca
->slot_info
[slot
].slot_state
) {
1052 case DVB_CA_SLOTSTATE_NONE
:
1053 case DVB_CA_SLOTSTATE_INVALID
:
1057 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1058 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1059 ca
->pub
->slot_reset(ca
->pub
, slot
);
1060 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1063 case DVB_CA_SLOTSTATE_WAITREADY
:
1064 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1065 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1066 ca
->dvbdev
->adapter
->num
);
1067 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1068 dvb_ca_en50221_thread_update_delay(ca
);
1071 // no other action needed; will automatically change state when ready
1074 case DVB_CA_SLOTSTATE_VALIDATE
:
1075 if (dvb_ca_en50221_parse_attributes(ca
, slot
)
1077 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1078 ca
->dvbdev
->adapter
->num
);
1079 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1080 dvb_ca_en50221_thread_update_delay(ca
);
1083 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1084 printk("dvb_ca adapter %d: Unable to initialise CAM :(\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
);
1090 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1091 CTRLIF_COMMAND
, CMDREG_RS
) != 0) {
1092 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1093 ca
->dvbdev
->adapter
->num
);
1094 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1095 dvb_ca_en50221_thread_update_delay(ca
);
1098 dprintk("DVB CAM validated successfully\n");
1100 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1101 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1105 case DVB_CA_SLOTSTATE_WAITFR
:
1106 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1107 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1108 ca
->dvbdev
->adapter
->num
);
1109 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1110 dvb_ca_en50221_thread_update_delay(ca
);
1114 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1115 if (flags
& STATUSREG_FR
) {
1116 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1121 case DVB_CA_SLOTSTATE_LINKINIT
:
1122 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1123 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca
->dvbdev
->adapter
->num
);
1124 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1125 dvb_ca_en50221_thread_update_delay(ca
);
1129 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1130 if (rxbuf
== NULL
) {
1131 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca
->dvbdev
->adapter
->num
);
1132 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1133 dvb_ca_en50221_thread_update_delay(ca
);
1136 down_write(&ca
->slot_info
[slot
].sem
);
1137 dvb_ringbuffer_init(&ca
->slot_info
[slot
].rx_buffer
, rxbuf
, RX_BUFFER_SIZE
);
1138 up_write(&ca
->slot_info
[slot
].sem
);
1140 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1141 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1142 dvb_ca_en50221_thread_update_delay(ca
);
1143 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca
->dvbdev
->adapter
->num
);
1146 case DVB_CA_SLOTSTATE_RUNNING
:
1150 // no need to poll if the CAM supports IRQs
1151 if (ca
->slot_info
[slot
].da_irq_supported
)
1156 while ((status
= dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0)) > 0) {
1160 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1161 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1162 // we dont want to sleep on the next iteration so we can handle the cam change
1167 /* check if we've hit our limit this time */
1168 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1169 // dont sleep; there is likely to be more data to read
1182 wake_up_interruptible(&ca
->thread_queue
);
1188 /* ******************************************************************************** */
1189 /* EN50221 IO interface functions */
1192 * Real ioctl implementation.
1193 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1195 * @param inode Inode concerned.
1196 * @param file File concerned.
1197 * @param cmd IOCTL command.
1198 * @param arg Associated argument.
1200 * @return 0 on success, <0 on error.
1202 static int dvb_ca_en50221_io_do_ioctl(struct inode
*inode
, struct file
*file
,
1203 unsigned int cmd
, void *parg
)
1205 struct dvb_device
*dvbdev
= file
->private_data
;
1206 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1210 dprintk("%s\n", __FUNCTION__
);
1214 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1215 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1216 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1217 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1218 dvb_ca_en50221_camchange_irq(ca
->pub
,
1220 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1223 ca
->next_read_slot
= 0;
1224 dvb_ca_en50221_thread_wakeup(ca
);
1228 struct ca_caps
*caps
= parg
;
1230 caps
->slot_num
= ca
->slot_count
;
1231 caps
->slot_type
= CA_CI_LINK
;
1232 caps
->descr_num
= 0;
1233 caps
->descr_type
= 0;
1237 case CA_GET_SLOT_INFO
: {
1238 struct ca_slot_info
*info
= parg
;
1240 if ((info
->num
> ca
->slot_count
) || (info
->num
< 0))
1243 info
->type
= CA_CI_LINK
;
1245 if ((ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1246 && (ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_INVALID
)) {
1247 info
->flags
= CA_CI_MODULE_PRESENT
;
1249 if (ca
->slot_info
[info
->num
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1250 info
->flags
|= CA_CI_MODULE_READY
;
1265 * Wrapper for ioctl implementation.
1267 * @param inode Inode concerned.
1268 * @param file File concerned.
1269 * @param cmd IOCTL command.
1270 * @param arg Associated argument.
1272 * @return 0 on success, <0 on error.
1274 static int dvb_ca_en50221_io_ioctl(struct inode
*inode
, struct file
*file
,
1275 unsigned int cmd
, unsigned long arg
)
1277 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_ca_en50221_io_do_ioctl
);
1282 * Implementation of write() syscall.
1284 * @param file File structure.
1285 * @param buf Source buffer.
1286 * @param count Size of source buffer.
1287 * @param ppos Position in file (ignored).
1289 * @return Number of bytes read, or <0 on error.
1291 static ssize_t
dvb_ca_en50221_io_write(struct file
*file
,
1292 const char __user
* buf
, size_t count
, loff_t
* ppos
)
1294 struct dvb_device
*dvbdev
= file
->private_data
;
1295 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1296 u8 slot
, connection_id
;
1298 char fragbuf
[HOST_LINK_BUF_SIZE
];
1301 unsigned long timeout
;
1304 dprintk("%s\n", __FUNCTION__
);
1306 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1310 /* extract slot & connection id */
1311 if (copy_from_user(&slot
, buf
, 1))
1313 if (copy_from_user(&connection_id
, buf
+ 1, 1))
1318 /* check if the slot is actually running */
1319 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1322 /* fragment the packets & store in the buffer */
1323 while (fragpos
< count
) {
1324 fraglen
= ca
->slot_info
[slot
].link_buf_size
- 2;
1325 if ((count
- fragpos
) < fraglen
)
1326 fraglen
= count
- fragpos
;
1328 fragbuf
[0] = connection_id
;
1329 fragbuf
[1] = ((fragpos
+ fraglen
) < count
) ? 0x80 : 0x00;
1330 if ((status
= copy_from_user(fragbuf
+ 2, buf
+ fragpos
, fraglen
)) != 0)
1333 timeout
= jiffies
+ HZ
/ 2;
1335 while (!time_after(jiffies
, timeout
)) {
1336 /* check the CAM hasn't been removed/reset in the meantime */
1337 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
) {
1342 status
= dvb_ca_en50221_write_data(ca
, slot
, fragbuf
, fraglen
+ 2);
1343 if (status
== (fraglen
+ 2)) {
1347 if (status
!= -EAGAIN
)
1367 * Condition for waking up in dvb_ca_en50221_io_read_condition
1369 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private
*ca
, int *result
, int *_slot
)
1375 int connection_id
= -1;
1379 slot
= ca
->next_read_slot
;
1380 while ((slot_count
< ca
->slot_count
) && (!found
)) {
1381 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1384 down_read(&ca
->slot_info
[slot
].sem
);
1386 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1387 up_read(&ca
->slot_info
[slot
].sem
);
1391 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1393 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2, 0);
1394 if (connection_id
== -1)
1395 connection_id
= hdr
[0];
1396 if ((hdr
[0] == connection_id
) && ((hdr
[1] & 0x80) == 0)) {
1402 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1406 up_read(&ca
->slot_info
[slot
].sem
);
1409 slot
= (slot
+ 1) % ca
->slot_count
;
1413 ca
->next_read_slot
= slot
;
1419 * Implementation of read() syscall.
1421 * @param file File structure.
1422 * @param buf Destination buffer.
1423 * @param count Size of destination buffer.
1424 * @param ppos Position in file (ignored).
1426 * @return Number of bytes read, or <0 on error.
1428 static ssize_t
dvb_ca_en50221_io_read(struct file
*file
, char __user
* buf
,
1429 size_t count
, loff_t
* ppos
)
1431 struct dvb_device
*dvbdev
= file
->private_data
;
1432 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1437 int connection_id
= -1;
1439 int last_fragment
= 0;
1444 dprintk("%s\n", __FUNCTION__
);
1446 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1450 /* wait for some data */
1451 if ((status
= dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
)) == 0) {
1453 /* if we're in nonblocking mode, exit immediately */
1454 if (file
->f_flags
& O_NONBLOCK
)
1455 return -EWOULDBLOCK
;
1457 /* wait for some data */
1458 status
= wait_event_interruptible(ca
->wait_queue
,
1459 dvb_ca_en50221_io_read_condition
1460 (ca
, &result
, &slot
));
1462 if ((status
< 0) || (result
< 0)) {
1468 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1472 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca
->dvbdev
->adapter
->num
);
1477 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2, 0);
1478 if (connection_id
== -1)
1479 connection_id
= hdr
[0];
1480 if (hdr
[0] == connection_id
) {
1481 if (pktlen
< count
) {
1482 if ((pktlen
+ fraglen
- 2) > count
) {
1483 fraglen
= count
- pktlen
;
1488 if ((status
= dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 2,
1489 buf
+ pktlen
, fraglen
, 1)) < 0) {
1495 if ((hdr
[1] & 0x80) == 0)
1500 idx2
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1502 dvb_ringbuffer_pkt_dispose(&ca
->slot_info
[slot
].rx_buffer
, idx
);
1505 } while (!last_fragment
);
1508 hdr
[1] = connection_id
;
1509 if ((status
= copy_to_user(buf
, hdr
, 2)) != 0)
1514 up_read(&ca
->slot_info
[slot
].sem
);
1520 * Implementation of file open syscall.
1522 * @param inode Inode concerned.
1523 * @param file File concerned.
1525 * @return 0 on success, <0 on failure.
1527 static int dvb_ca_en50221_io_open(struct inode
*inode
, struct file
*file
)
1529 struct dvb_device
*dvbdev
= file
->private_data
;
1530 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1534 dprintk("%s\n", __FUNCTION__
);
1536 if (!try_module_get(ca
->pub
->owner
))
1539 err
= dvb_generic_open(inode
, file
);
1543 for (i
= 0; i
< ca
->slot_count
; i
++) {
1545 if (ca
->slot_info
[i
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1546 down_write(&ca
->slot_info
[i
].sem
);
1547 if (ca
->slot_info
[i
].rx_buffer
.data
!= NULL
) {
1548 dvb_ringbuffer_flush(&ca
->slot_info
[i
].rx_buffer
);
1550 up_write(&ca
->slot_info
[i
].sem
);
1555 dvb_ca_en50221_thread_update_delay(ca
);
1556 dvb_ca_en50221_thread_wakeup(ca
);
1563 * Implementation of file close syscall.
1565 * @param inode Inode concerned.
1566 * @param file File concerned.
1568 * @return 0 on success, <0 on failure.
1570 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1572 struct dvb_device
*dvbdev
= file
->private_data
;
1573 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1576 dprintk("%s\n", __FUNCTION__
);
1578 /* mark the CA device as closed */
1580 dvb_ca_en50221_thread_update_delay(ca
);
1582 err
= dvb_generic_release(inode
, file
);
1584 module_put(ca
->pub
->owner
);
1591 * Implementation of poll() syscall.
1593 * @param file File concerned.
1594 * @param wait poll wait table.
1596 * @return Standard poll mask.
1598 static unsigned int dvb_ca_en50221_io_poll(struct file
*file
, poll_table
* wait
)
1600 struct dvb_device
*dvbdev
= file
->private_data
;
1601 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1602 unsigned int mask
= 0;
1606 dprintk("%s\n", __FUNCTION__
);
1608 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1609 up_read(&ca
->slot_info
[slot
].sem
);
1613 /* if there is something, return now */
1617 /* wait for something to happen */
1618 poll_wait(file
, &ca
->wait_queue
, wait
);
1620 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1621 up_read(&ca
->slot_info
[slot
].sem
);
1627 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1630 static struct file_operations dvb_ca_fops
= {
1631 .owner
= THIS_MODULE
,
1632 .read
= dvb_ca_en50221_io_read
,
1633 .write
= dvb_ca_en50221_io_write
,
1634 .ioctl
= dvb_ca_en50221_io_ioctl
,
1635 .open
= dvb_ca_en50221_io_open
,
1636 .release
= dvb_ca_en50221_io_release
,
1637 .poll
= dvb_ca_en50221_io_poll
,
1640 static struct dvb_device dvbdev_ca
= {
1645 .fops
= &dvb_ca_fops
,
1649 /* ******************************************************************************** */
1650 /* Initialisation/shutdown functions */
1654 * Initialise a new DVB CA EN50221 interface device.
1656 * @param dvb_adapter DVB adapter to attach the new CA device to.
1657 * @param ca The dvb_ca instance.
1658 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1659 * @param slot_count Number of slots supported.
1661 * @return 0 on success, nonzero on failure
1663 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1664 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1667 struct dvb_ca_private
*ca
= NULL
;
1670 dprintk("%s\n", __FUNCTION__
);
1675 /* initialise the system data */
1677 (struct dvb_ca_private
*) kmalloc(sizeof(struct dvb_ca_private
),
1678 GFP_KERNEL
)) == NULL
) {
1682 memset(ca
, 0, sizeof(struct dvb_ca_private
));
1685 ca
->slot_count
= slot_count
;
1686 if ((ca
->slot_info
= kmalloc(sizeof(struct dvb_ca_slot
) * slot_count
, GFP_KERNEL
)) == NULL
) {
1690 memset(ca
->slot_info
, 0, sizeof(struct dvb_ca_slot
) * slot_count
);
1691 init_waitqueue_head(&ca
->wait_queue
);
1693 init_waitqueue_head(&ca
->thread_queue
);
1697 ca
->next_read_slot
= 0;
1698 pubca
->private = ca
;
1700 /* register the DVB device */
1701 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
, DVB_DEVICE_CA
);
1705 /* now initialise each slot */
1706 for (i
= 0; i
< slot_count
; i
++) {
1707 memset(&ca
->slot_info
[i
], 0, sizeof(struct dvb_ca_slot
));
1708 ca
->slot_info
[i
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1709 atomic_set(&ca
->slot_info
[i
].camchange_count
, 0);
1710 ca
->slot_info
[i
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1711 init_rwsem(&ca
->slot_info
[i
].sem
);
1714 if (signal_pending(current
)) {
1720 /* create a kthread for monitoring this CA device */
1722 ret
= kernel_thread(dvb_ca_en50221_thread
, ca
, 0);
1725 printk("dvb_ca_init: failed to start kernel_thread (%d)\n", ret
);
1728 ca
->thread_pid
= ret
;
1733 if (ca
->dvbdev
!= NULL
)
1734 dvb_unregister_device(ca
->dvbdev
);
1735 kfree(ca
->slot_info
);
1738 pubca
->private = NULL
;
1741 EXPORT_SYMBOL(dvb_ca_en50221_release
);
1746 * Release a DVB CA EN50221 interface device.
1748 * @param ca_dev The dvb_device_t instance for the CA device.
1749 * @param ca The associated dvb_ca instance.
1751 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1753 struct dvb_ca_private
*ca
= pubca
->private;
1756 dprintk("%s\n", __FUNCTION__
);
1758 /* shutdown the thread if there was one */
1759 if (ca
->thread_pid
) {
1760 if (kill_proc(ca
->thread_pid
, 0, 1) == -ESRCH
) {
1761 printk("dvb_ca_release adapter %d: thread PID %d already died\n",
1762 ca
->dvbdev
->adapter
->num
, ca
->thread_pid
);
1766 dvb_ca_en50221_thread_wakeup(ca
);
1767 wait_event_interruptible(ca
->thread_queue
, ca
->thread_pid
== 0);
1771 for (i
= 0; i
< ca
->slot_count
; i
++) {
1772 dvb_ca_en50221_slot_shutdown(ca
, i
);
1774 kfree(ca
->slot_info
);
1775 dvb_unregister_device(ca
->dvbdev
);
1777 pubca
->private = NULL
;