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>
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 10
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 /* buffer for incoming packets */
114 struct dvb_ringbuffer rx_buffer
;
116 /* timer used during various states of the slot */
117 unsigned long timeout
;
120 /* Private CA-interface information */
121 struct dvb_ca_private
{
123 /* pointer back to the public data structure */
124 struct dvb_ca_en50221
*pub
;
127 struct dvb_device
*dvbdev
;
129 /* Flags describing the interface (DVB_CA_FLAG_*) */
132 /* number of slots supported by this CA interface */
133 unsigned int slot_count
;
135 /* information on each slot */
136 struct dvb_ca_slot
*slot_info
;
138 /* wait queues for read() and write() operations */
139 wait_queue_head_t wait_queue
;
141 /* PID of the monitoring thread */
144 /* Wait queue used when shutting thread down */
145 wait_queue_head_t thread_queue
;
147 /* Flag indicating when thread should exit */
150 /* Flag indicating if the CA device is open */
153 /* Flag indicating the thread should wake up now */
154 unsigned int wakeup
:1;
156 /* Delay the main thread should use */
159 /* Slot to start looking for data to read from in the next user-space read operation */
163 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
164 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
165 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
169 * Safely find needle in haystack.
171 * @param haystack Buffer to look in.
172 * @param hlen Number of bytes in haystack.
173 * @param needle Buffer to find.
174 * @param nlen Number of bytes in needle.
175 * @return Pointer into haystack needle was found at, or NULL if not found.
177 static char *findstr(char * haystack
, int hlen
, char * needle
, int nlen
)
184 for (i
= 0; i
<= hlen
- nlen
; i
++) {
185 if (!strncmp(haystack
+ i
, needle
, nlen
))
194 /* ******************************************************************************** */
195 /* EN50221 physical interface functions */
201 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private
*ca
, int slot
)
208 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
) {
209 return (atomic_read(&ca
->slot_info
[slot
].camchange_count
) != 0);
213 slot_status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, ca
->open
);
215 cam_present_now
= (slot_status
& DVB_CA_EN50221_POLL_CAM_PRESENT
) ? 1 : 0;
216 cam_changed
= (slot_status
& DVB_CA_EN50221_POLL_CAM_CHANGED
) ? 1 : 0;
218 int cam_present_old
= (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
);
219 cam_changed
= (cam_present_now
!= cam_present_old
);
223 if (!cam_present_now
) {
224 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
226 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
228 atomic_set(&ca
->slot_info
[slot
].camchange_count
, 1);
230 if ((ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) &&
231 (slot_status
& DVB_CA_EN50221_POLL_CAM_READY
)) {
232 // move to validate state if reset is completed
233 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
242 * Wait for flags to become set on the STATUS register on a CAM interface,
243 * checking for errors and timeout.
245 * @param ca CA instance.
246 * @param slot Slot on interface.
247 * @param waitfor Flags to wait for.
248 * @param timeout_ms Timeout in milliseconds.
250 * @return 0 on success, nonzero on error.
252 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
253 u8 waitfor
, int timeout_hz
)
255 unsigned long timeout
;
258 dprintk("%s\n", __FUNCTION__
);
260 /* loop until timeout elapsed */
262 timeout
= jiffies
+ timeout_hz
;
264 /* read the status and check for error */
265 int res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
269 /* if we got the flags, it was successful! */
271 dprintk("%s succeeded timeout:%lu\n", __FUNCTION__
, jiffies
- start
);
275 /* check for timeout */
276 if (time_after(jiffies
, timeout
)) {
284 dprintk("%s failed timeout:%lu\n", __FUNCTION__
, jiffies
- start
);
286 /* if we get here, we've timed out */
292 * Initialise the link layer connection to a CAM.
294 * @param ca CA instance.
295 * @param slot Slot id.
297 * @return 0 on success, nonzero on failure.
299 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
305 dprintk("%s\n", __FUNCTION__
);
307 /* we'll be determining these during this function */
308 ca
->slot_info
[slot
].da_irq_supported
= 0;
310 /* set the host link buffer size temporarily. it will be overwritten with the
311 * real negotiated size later. */
312 ca
->slot_info
[slot
].link_buf_size
= 2;
314 /* read the buffer size from the CAM */
315 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SR
)) != 0)
317 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
/ 10)) != 0)
319 if ((ret
= dvb_ca_en50221_read_data(ca
, slot
, buf
, 2)) != 2)
321 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
324 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
325 buf_size
= (buf
[0] << 8) | buf
[1];
326 if (buf_size
> HOST_LINK_BUF_SIZE
)
327 buf_size
= HOST_LINK_BUF_SIZE
;
328 ca
->slot_info
[slot
].link_buf_size
= buf_size
;
329 buf
[0] = buf_size
>> 8;
330 buf
[1] = buf_size
& 0xff;
331 dprintk("Chosen link buffer size of %i\n", buf_size
);
333 /* write the buffer size to the CAM */
334 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SW
)) != 0)
336 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_FR
, HZ
/ 10)) != 0)
338 if ((ret
= dvb_ca_en50221_write_data(ca
, slot
, buf
, 2)) != 2)
340 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
348 * Read a tuple from attribute memory.
350 * @param ca CA instance.
351 * @param slot Slot id.
352 * @param address Address to read from. Updated.
353 * @param tupleType Tuple id byte. Updated.
354 * @param tupleLength Tuple length. Updated.
355 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
357 * @return 0 on success, nonzero on error.
359 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private
*ca
, int slot
,
360 int *address
, int *tupleType
, int *tupleLength
, u8
* tuple
)
365 int _address
= *address
;
367 /* grab the next tuple length and type */
368 if ((_tupleType
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
)) < 0)
370 if (_tupleType
== 0xff) {
371 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType
);
373 *tupleType
= _tupleType
;
377 if ((_tupleLength
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ 2)) < 0)
381 dprintk("TUPLE type:0x%x length:%i\n", _tupleType
, _tupleLength
);
383 /* read in the whole tuple */
384 for (i
= 0; i
< _tupleLength
; i
++) {
385 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ (i
* 2));
386 dprintk(" 0x%02x: 0x%02x %c\n",
388 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
390 _address
+= (_tupleLength
* 2);
393 *tupleType
= _tupleType
;
394 *tupleLength
= _tupleLength
;
401 * Parse attribute memory of a CAM module, extracting Config register, and checking
402 * it is a DVB CAM module.
404 * @param ca CA instance.
405 * @param slot Slot id.
407 * @return 0 on success, <0 on failure.
409 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
418 int got_cftableentry
= 0;
427 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
429 if (tupleType
!= 0x1D)
436 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
438 if (tupleType
!= 0x1C)
445 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
447 if (tupleType
!= 0x15)
453 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
454 &tupleLength
, tuple
)) < 0)
456 if (tupleType
!= 0x20)
458 if (tupleLength
!= 4)
460 manfid
= (tuple
[1] << 8) | tuple
[0];
461 devid
= (tuple
[3] << 8) | tuple
[2];
466 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
467 &tupleLength
, tuple
)) < 0)
469 if (tupleType
!= 0x1A)
474 /* extract the configbase */
476 if (tupleLength
< (3 + rasz
+ 14))
478 ca
->slot_info
[slot
].config_base
= 0;
479 for (i
= 0; i
< rasz
+ 1; i
++) {
480 ca
->slot_info
[slot
].config_base
|= (tuple
[2 + i
] << (8 * i
));
483 /* check it contains the correct DVB string */
484 dvb_str
= findstr((char *)tuple
, tupleLength
, "DVB_CI_V", 8);
487 if (tupleLength
< ((dvb_str
- (char *) tuple
) + 12))
490 /* is it a version we support? */
491 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
492 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
493 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9], dvb_str
[10], dvb_str
[11]);
497 /* process the CFTABLE_ENTRY tuples, and any after those */
498 while ((!end_chain
) && (address
< 0x1000)) {
499 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
500 &tupleLength
, tuple
)) < 0)
503 case 0x1B: // CISTPL_CFTABLE_ENTRY
504 if (tupleLength
< (2 + 11 + 17))
507 /* if we've already parsed one, just use it */
508 if (got_cftableentry
)
511 /* get the config option */
512 ca
->slot_info
[slot
].config_option
= tuple
[0] & 0x3f;
514 /* OK, check it contains the correct strings */
515 if ((findstr((char *)tuple
, tupleLength
, "DVB_HOST", 8) == NULL
) ||
516 (findstr((char *)tuple
, tupleLength
, "DVB_CI_MODULE", 13) == NULL
))
519 got_cftableentry
= 1;
522 case 0x14: // CISTPL_NO_LINK
525 case 0xFF: // CISTPL_END
529 default: /* Unknown tuple type - just skip this tuple and move to the next one */
530 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType
,
536 if ((address
> 0x1000) || (!got_cftableentry
))
539 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
540 manfid
, devid
, ca
->slot_info
[slot
].config_base
, ca
->slot_info
[slot
].config_option
);
548 * Set CAM's configoption correctly.
550 * @param ca CA instance.
551 * @param slot Slot containing the CAM.
553 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
557 dprintk("%s\n", __FUNCTION__
);
559 /* set the config option */
560 ca
->pub
->write_attribute_mem(ca
->pub
, slot
,
561 ca
->slot_info
[slot
].config_base
,
562 ca
->slot_info
[slot
].config_option
);
565 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, ca
->slot_info
[slot
].config_base
);
566 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
567 ca
->slot_info
[slot
].config_option
, configoption
& 0x3f);
576 * This function talks to an EN50221 CAM control interface. It reads a buffer of
577 * data from the CAM. The data can either be stored in a supplied buffer, or
578 * automatically be added to the slot's rx_buffer.
580 * @param ca CA instance.
581 * @param slot Slot to read from.
582 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
583 * the data will be added into the buffering system as a normal fragment.
584 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
586 * @return Number of bytes read, or < 0 on error
588 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
)
592 u8 buf
[HOST_LINK_BUF_SIZE
];
595 dprintk("%s\n", __FUNCTION__
);
597 /* check if we have space for a link buf in the rx_buffer */
601 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
605 buf_free
= dvb_ringbuffer_free(&ca
->slot_info
[slot
].rx_buffer
);
607 if (buf_free
< (ca
->slot_info
[slot
].link_buf_size
+ DVB_RINGBUFFER_PKTHDRSIZE
)) {
613 /* check if there is data available */
614 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
616 if (!(status
& STATUSREG_DA
)) {
622 /* read the amount of data */
623 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
)) < 0)
625 bytes_read
= status
<< 8;
626 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
)) < 0)
628 bytes_read
|= status
;
630 /* check it will fit */
632 if (bytes_read
> ca
->slot_info
[slot
].link_buf_size
) {
633 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
634 ca
->dvbdev
->adapter
->num
, bytes_read
, ca
->slot_info
[slot
].link_buf_size
);
635 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
639 if (bytes_read
< 2) {
640 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
641 ca
->dvbdev
->adapter
->num
);
642 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
647 if (bytes_read
> ecount
) {
648 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
649 ca
->dvbdev
->adapter
->num
);
655 /* fill the buffer */
656 for (i
= 0; i
< bytes_read
; i
++) {
657 /* read byte and check */
658 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_DATA
)) < 0)
661 /* OK, store it in the buffer */
665 /* check for read error (RE should now be 0) */
666 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
668 if (status
& STATUSREG_RE
) {
669 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
674 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
676 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
680 dvb_ringbuffer_pkt_write(&ca
->slot_info
[slot
].rx_buffer
, buf
, bytes_read
);
682 memcpy(ebuf
, buf
, bytes_read
);
685 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
686 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
688 /* wake up readers when a last_fragment is received */
689 if ((buf
[1] & 0x80) == 0x00) {
690 wake_up_interruptible(&ca
->wait_queue
);
700 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
703 * @param ca CA instance.
704 * @param slot Slot to write to.
705 * @param ebuf The data in this buffer is treated as a complete link-level packet to
707 * @param count Size of ebuf.
709 * @return Number of bytes written, or < 0 on error.
711 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* buf
, int bytes_write
)
716 dprintk("%s\n", __FUNCTION__
);
720 if (bytes_write
> ca
->slot_info
[slot
].link_buf_size
)
723 /* check if interface is actually waiting for us to read from it, or if a read is in progress */
724 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
726 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
732 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
733 IRQEN
| CMDREG_HC
)) != 0)
736 /* check if interface is still free */
737 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
739 if (!(status
& STATUSREG_FR
)) {
740 /* it wasn't free => try again later */
745 /* send the amount of data */
746 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
, bytes_write
>> 8)) != 0)
748 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
749 bytes_write
& 0xff)) != 0)
752 /* send the buffer */
753 for (i
= 0; i
< bytes_write
; i
++) {
754 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
, buf
[i
])) != 0)
758 /* check for write error (WE should now be 0) */
759 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
761 if (status
& STATUSREG_WE
) {
762 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
766 status
= bytes_write
;
768 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
769 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
772 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
777 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
781 /* ******************************************************************************** */
782 /* EN50221 higher level functions */
786 * A CAM has been removed => shut it down.
788 * @param ca CA instance.
789 * @param slot Slot to shut down.
791 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
793 dprintk("%s\n", __FUNCTION__
);
795 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
796 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
798 /* need to wake up all processes to check if they're now
799 trying to write to a defunct CAM */
800 wake_up_interruptible(&ca
->wait_queue
);
802 dprintk("Slot %i shutdown\n", slot
);
807 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
811 * A CAMCHANGE IRQ has occurred.
813 * @param ca CA instance.
814 * @param slot Slot concerned.
815 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
817 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
, int change_type
)
819 struct dvb_ca_private
*ca
= pubca
->private;
821 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
823 switch (change_type
) {
824 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
825 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
832 ca
->slot_info
[slot
].camchange_type
= change_type
;
833 atomic_inc(&ca
->slot_info
[slot
].camchange_count
);
834 dvb_ca_en50221_thread_wakeup(ca
);
836 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
840 * A CAMREADY IRQ has occurred.
842 * @param ca CA instance.
843 * @param slot Slot concerned.
845 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
847 struct dvb_ca_private
*ca
= pubca
->private;
849 dprintk("CAMREADY IRQ slot:%i\n", slot
);
851 if (ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
852 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
853 dvb_ca_en50221_thread_wakeup(ca
);
859 * An FR or DA IRQ has occurred.
861 * @param ca CA instance.
862 * @param slot Slot concerned.
864 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
866 struct dvb_ca_private
*ca
= pubca
->private;
869 dprintk("FR/DA IRQ slot:%i\n", slot
);
871 switch (ca
->slot_info
[slot
].slot_state
) {
872 case DVB_CA_SLOTSTATE_LINKINIT
:
873 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
874 if (flags
& STATUSREG_DA
) {
875 dprintk("CAM supports DA IRQ\n");
876 ca
->slot_info
[slot
].da_irq_supported
= 1;
880 case DVB_CA_SLOTSTATE_RUNNING
:
882 dvb_ca_en50221_thread_wakeup(ca
);
889 /* ******************************************************************************** */
890 /* EN50221 thread functions */
893 * Wake up the DVB CA thread
895 * @param ca CA instance.
897 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
900 dprintk("%s\n", __FUNCTION__
);
904 wake_up_interruptible(&ca
->thread_queue
);
908 * Used by the CA thread to determine if an early wakeup is necessary
910 * @param ca CA instance.
912 static int dvb_ca_en50221_thread_should_wakeup(struct dvb_ca_private
*ca
)
926 * Update the delay used by the thread.
928 * @param ca CA instance.
930 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
933 int curdelay
= 100000000;
936 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
937 switch (ca
->slot_info
[slot
].slot_state
) {
939 case DVB_CA_SLOTSTATE_NONE
:
940 case DVB_CA_SLOTSTATE_INVALID
:
942 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) {
947 case DVB_CA_SLOTSTATE_UNINITIALISED
:
948 case DVB_CA_SLOTSTATE_WAITREADY
:
949 case DVB_CA_SLOTSTATE_VALIDATE
:
950 case DVB_CA_SLOTSTATE_WAITFR
:
951 case DVB_CA_SLOTSTATE_LINKINIT
:
955 case DVB_CA_SLOTSTATE_RUNNING
:
957 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) {
961 if ((!ca
->slot_info
[slot
].da_irq_supported
) ||
962 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
))) {
969 if (delay
< curdelay
)
973 ca
->delay
= curdelay
;
979 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
981 static int dvb_ca_en50221_thread(void *data
)
983 struct dvb_ca_private
*ca
= data
;
991 dprintk("%s\n", __FUNCTION__
);
993 /* setup kernel thread */
994 snprintf(name
, sizeof(name
), "kdvb-ca-%i:%i", ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
998 sigfillset(¤t
->blocked
);
1001 /* choose the correct initial delay */
1002 dvb_ca_en50221_thread_update_delay(ca
);
1006 /* sleep for a bit */
1008 flags
= wait_event_interruptible_timeout(ca
->thread_queue
,
1009 dvb_ca_en50221_thread_should_wakeup(ca
),
1011 if ((flags
== -ERESTARTSYS
) || ca
->exit
) {
1012 /* got signal or quitting */
1018 /* go through all the slots processing them */
1019 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1021 // check the cam status + deal with CAMCHANGEs
1022 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1023 /* clear down an old CI slot if necessary */
1024 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1025 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1027 /* if a CAM is NOW present, initialise it */
1028 if (ca
->slot_info
[slot
].camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
) {
1029 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
1032 /* we've handled one CAMCHANGE */
1033 dvb_ca_en50221_thread_update_delay(ca
);
1034 atomic_dec(&ca
->slot_info
[slot
].camchange_count
);
1037 // CAM state machine
1038 switch (ca
->slot_info
[slot
].slot_state
) {
1039 case DVB_CA_SLOTSTATE_NONE
:
1040 case DVB_CA_SLOTSTATE_INVALID
:
1044 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1045 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1046 ca
->pub
->slot_reset(ca
->pub
, slot
);
1047 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1050 case DVB_CA_SLOTSTATE_WAITREADY
:
1051 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1052 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1053 ca
->dvbdev
->adapter
->num
);
1054 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1055 dvb_ca_en50221_thread_update_delay(ca
);
1058 // no other action needed; will automatically change state when ready
1061 case DVB_CA_SLOTSTATE_VALIDATE
:
1062 if (dvb_ca_en50221_parse_attributes(ca
, slot
) != 0) {
1063 /* we need this extra check for annoying interfaces like the budget-av */
1064 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1065 (ca
->pub
->poll_slot_status
)) {
1066 int status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1067 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1068 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1069 dvb_ca_en50221_thread_update_delay(ca
);
1074 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1075 ca
->dvbdev
->adapter
->num
);
1076 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1077 dvb_ca_en50221_thread_update_delay(ca
);
1080 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1081 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1082 ca
->dvbdev
->adapter
->num
);
1083 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1084 dvb_ca_en50221_thread_update_delay(ca
);
1087 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1088 CTRLIF_COMMAND
, CMDREG_RS
) != 0) {
1089 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1090 ca
->dvbdev
->adapter
->num
);
1091 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1092 dvb_ca_en50221_thread_update_delay(ca
);
1095 dprintk("DVB CAM validated successfully\n");
1097 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1098 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1102 case DVB_CA_SLOTSTATE_WAITFR
:
1103 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1104 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1105 ca
->dvbdev
->adapter
->num
);
1106 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1107 dvb_ca_en50221_thread_update_delay(ca
);
1111 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1112 if (flags
& STATUSREG_FR
) {
1113 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1118 case DVB_CA_SLOTSTATE_LINKINIT
:
1119 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1120 /* we need this extra check for annoying interfaces like the budget-av */
1121 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1122 (ca
->pub
->poll_slot_status
)) {
1123 int status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1124 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1125 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1126 dvb_ca_en50221_thread_update_delay(ca
);
1131 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca
->dvbdev
->adapter
->num
);
1132 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1133 dvb_ca_en50221_thread_update_delay(ca
);
1137 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1138 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1139 if (rxbuf
== NULL
) {
1140 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca
->dvbdev
->adapter
->num
);
1141 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1142 dvb_ca_en50221_thread_update_delay(ca
);
1145 dvb_ringbuffer_init(&ca
->slot_info
[slot
].rx_buffer
, rxbuf
, RX_BUFFER_SIZE
);
1148 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1149 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1150 dvb_ca_en50221_thread_update_delay(ca
);
1151 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca
->dvbdev
->adapter
->num
);
1154 case DVB_CA_SLOTSTATE_RUNNING
:
1158 // poll slots for data
1160 while ((status
= dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0)) > 0) {
1164 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1165 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1166 // we dont want to sleep on the next iteration so we can handle the cam change
1171 /* check if we've hit our limit this time */
1172 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1173 // dont sleep; there is likely to be more data to read
1186 wake_up_interruptible(&ca
->thread_queue
);
1192 /* ******************************************************************************** */
1193 /* EN50221 IO interface functions */
1196 * Real ioctl implementation.
1197 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1199 * @param inode Inode concerned.
1200 * @param file File concerned.
1201 * @param cmd IOCTL command.
1202 * @param arg Associated argument.
1204 * @return 0 on success, <0 on error.
1206 static int dvb_ca_en50221_io_do_ioctl(struct inode
*inode
, struct file
*file
,
1207 unsigned int cmd
, void *parg
)
1209 struct dvb_device
*dvbdev
= file
->private_data
;
1210 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1214 dprintk("%s\n", __FUNCTION__
);
1218 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1219 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1220 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1221 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1222 dvb_ca_en50221_camchange_irq(ca
->pub
,
1224 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1227 ca
->next_read_slot
= 0;
1228 dvb_ca_en50221_thread_wakeup(ca
);
1232 struct ca_caps
*caps
= parg
;
1234 caps
->slot_num
= ca
->slot_count
;
1235 caps
->slot_type
= CA_CI_LINK
;
1236 caps
->descr_num
= 0;
1237 caps
->descr_type
= 0;
1241 case CA_GET_SLOT_INFO
: {
1242 struct ca_slot_info
*info
= parg
;
1244 if ((info
->num
> ca
->slot_count
) || (info
->num
< 0))
1247 info
->type
= CA_CI_LINK
;
1249 if ((ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1250 && (ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_INVALID
)) {
1251 info
->flags
= CA_CI_MODULE_PRESENT
;
1253 if (ca
->slot_info
[info
->num
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1254 info
->flags
|= CA_CI_MODULE_READY
;
1269 * Wrapper for ioctl implementation.
1271 * @param inode Inode concerned.
1272 * @param file File concerned.
1273 * @param cmd IOCTL command.
1274 * @param arg Associated argument.
1276 * @return 0 on success, <0 on error.
1278 static int dvb_ca_en50221_io_ioctl(struct inode
*inode
, struct file
*file
,
1279 unsigned int cmd
, unsigned long arg
)
1281 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_ca_en50221_io_do_ioctl
);
1286 * Implementation of write() syscall.
1288 * @param file File structure.
1289 * @param buf Source buffer.
1290 * @param count Size of source buffer.
1291 * @param ppos Position in file (ignored).
1293 * @return Number of bytes read, or <0 on error.
1295 static ssize_t
dvb_ca_en50221_io_write(struct file
*file
,
1296 const char __user
* buf
, size_t count
, loff_t
* ppos
)
1298 struct dvb_device
*dvbdev
= file
->private_data
;
1299 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1300 u8 slot
, connection_id
;
1302 u8 fragbuf
[HOST_LINK_BUF_SIZE
];
1305 unsigned long timeout
;
1308 dprintk("%s\n", __FUNCTION__
);
1310 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1314 /* extract slot & connection id */
1315 if (copy_from_user(&slot
, buf
, 1))
1317 if (copy_from_user(&connection_id
, buf
+ 1, 1))
1322 /* check if the slot is actually running */
1323 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1326 /* fragment the packets & store in the buffer */
1327 while (fragpos
< count
) {
1328 fraglen
= ca
->slot_info
[slot
].link_buf_size
- 2;
1329 if ((count
- fragpos
) < fraglen
)
1330 fraglen
= count
- fragpos
;
1332 fragbuf
[0] = connection_id
;
1333 fragbuf
[1] = ((fragpos
+ fraglen
) < count
) ? 0x80 : 0x00;
1334 if ((status
= copy_from_user(fragbuf
+ 2, buf
+ fragpos
, fraglen
)) != 0)
1337 timeout
= jiffies
+ HZ
/ 2;
1339 while (!time_after(jiffies
, timeout
)) {
1340 /* check the CAM hasn't been removed/reset in the meantime */
1341 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
) {
1346 status
= dvb_ca_en50221_write_data(ca
, slot
, fragbuf
, fraglen
+ 2);
1347 if (status
== (fraglen
+ 2)) {
1351 if (status
!= -EAGAIN
)
1371 * Condition for waking up in dvb_ca_en50221_io_read_condition
1373 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private
*ca
,
1374 int *result
, int *_slot
)
1380 int connection_id
= -1;
1384 slot
= ca
->next_read_slot
;
1385 while ((slot_count
< ca
->slot_count
) && (!found
)) {
1386 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1389 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1393 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1395 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2, 0);
1396 if (connection_id
== -1)
1397 connection_id
= hdr
[0];
1398 if ((hdr
[0] == connection_id
) && ((hdr
[1] & 0x80) == 0)) {
1404 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1408 slot
= (slot
+ 1) % ca
->slot_count
;
1412 ca
->next_read_slot
= slot
;
1418 * Implementation of read() syscall.
1420 * @param file File structure.
1421 * @param buf Destination buffer.
1422 * @param count Size of destination buffer.
1423 * @param ppos Position in file (ignored).
1425 * @return Number of bytes read, or <0 on error.
1427 static ssize_t
dvb_ca_en50221_io_read(struct file
*file
, char __user
* buf
,
1428 size_t count
, loff_t
* ppos
)
1430 struct dvb_device
*dvbdev
= file
->private_data
;
1431 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1436 int connection_id
= -1;
1438 int last_fragment
= 0;
1443 dprintk("%s\n", __FUNCTION__
);
1445 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1449 /* wait for some data */
1450 if ((status
= dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
)) == 0) {
1452 /* if we're in nonblocking mode, exit immediately */
1453 if (file
->f_flags
& O_NONBLOCK
)
1454 return -EWOULDBLOCK
;
1456 /* wait for some data */
1457 status
= wait_event_interruptible(ca
->wait_queue
,
1458 dvb_ca_en50221_io_read_condition
1459 (ca
, &result
, &slot
));
1461 if ((status
< 0) || (result
< 0)) {
1467 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1471 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca
->dvbdev
->adapter
->num
);
1476 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2, 0);
1477 if (connection_id
== -1)
1478 connection_id
= hdr
[0];
1479 if (hdr
[0] == connection_id
) {
1480 if (pktlen
< count
) {
1481 if ((pktlen
+ fraglen
- 2) > count
) {
1482 fraglen
= count
- pktlen
;
1487 if ((status
= dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 2,
1488 (u8
*)buf
+ pktlen
, fraglen
, 1)) < 0) {
1494 if ((hdr
[1] & 0x80) == 0)
1499 idx2
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1501 dvb_ringbuffer_pkt_dispose(&ca
->slot_info
[slot
].rx_buffer
, idx
);
1504 } while (!last_fragment
);
1507 hdr
[1] = connection_id
;
1508 if ((status
= copy_to_user(buf
, hdr
, 2)) != 0)
1518 * Implementation of file open syscall.
1520 * @param inode Inode concerned.
1521 * @param file File concerned.
1523 * @return 0 on success, <0 on failure.
1525 static int dvb_ca_en50221_io_open(struct inode
*inode
, struct file
*file
)
1527 struct dvb_device
*dvbdev
= file
->private_data
;
1528 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1532 dprintk("%s\n", __FUNCTION__
);
1534 if (!try_module_get(ca
->pub
->owner
))
1537 err
= dvb_generic_open(inode
, file
);
1541 for (i
= 0; i
< ca
->slot_count
; i
++) {
1543 if (ca
->slot_info
[i
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1544 if (ca
->slot_info
[i
].rx_buffer
.data
!= NULL
) {
1545 /* it is safe to call this here without locks because
1546 * ca->open == 0. Data is not read in this case */
1547 dvb_ringbuffer_flush(&ca
->slot_info
[i
].rx_buffer
);
1553 dvb_ca_en50221_thread_update_delay(ca
);
1554 dvb_ca_en50221_thread_wakeup(ca
);
1561 * Implementation of file close syscall.
1563 * @param inode Inode concerned.
1564 * @param file File concerned.
1566 * @return 0 on success, <0 on failure.
1568 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1570 struct dvb_device
*dvbdev
= file
->private_data
;
1571 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1574 dprintk("%s\n", __FUNCTION__
);
1576 /* mark the CA device as closed */
1578 dvb_ca_en50221_thread_update_delay(ca
);
1580 err
= dvb_generic_release(inode
, file
);
1582 module_put(ca
->pub
->owner
);
1589 * Implementation of poll() syscall.
1591 * @param file File concerned.
1592 * @param wait poll wait table.
1594 * @return Standard poll mask.
1596 static unsigned int dvb_ca_en50221_io_poll(struct file
*file
, poll_table
* wait
)
1598 struct dvb_device
*dvbdev
= file
->private_data
;
1599 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1600 unsigned int mask
= 0;
1604 dprintk("%s\n", __FUNCTION__
);
1606 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1610 /* if there is something, return now */
1614 /* wait for something to happen */
1615 poll_wait(file
, &ca
->wait_queue
, wait
);
1617 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1623 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1626 static struct file_operations dvb_ca_fops
= {
1627 .owner
= THIS_MODULE
,
1628 .read
= dvb_ca_en50221_io_read
,
1629 .write
= dvb_ca_en50221_io_write
,
1630 .ioctl
= dvb_ca_en50221_io_ioctl
,
1631 .open
= dvb_ca_en50221_io_open
,
1632 .release
= dvb_ca_en50221_io_release
,
1633 .poll
= dvb_ca_en50221_io_poll
,
1636 static struct dvb_device dvbdev_ca
= {
1641 .fops
= &dvb_ca_fops
,
1645 /* ******************************************************************************** */
1646 /* Initialisation/shutdown functions */
1650 * Initialise a new DVB CA EN50221 interface device.
1652 * @param dvb_adapter DVB adapter to attach the new CA device to.
1653 * @param ca The dvb_ca instance.
1654 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1655 * @param slot_count Number of slots supported.
1657 * @return 0 on success, nonzero on failure
1659 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1660 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1663 struct dvb_ca_private
*ca
= NULL
;
1666 dprintk("%s\n", __FUNCTION__
);
1671 /* initialise the system data */
1672 if ((ca
= kzalloc(sizeof(struct dvb_ca_private
), GFP_KERNEL
)) == NULL
) {
1678 ca
->slot_count
= slot_count
;
1679 if ((ca
->slot_info
= kcalloc(slot_count
, sizeof(struct dvb_ca_slot
), GFP_KERNEL
)) == NULL
) {
1683 init_waitqueue_head(&ca
->wait_queue
);
1685 init_waitqueue_head(&ca
->thread_queue
);
1689 ca
->next_read_slot
= 0;
1690 pubca
->private = ca
;
1692 /* register the DVB device */
1693 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
, DVB_DEVICE_CA
);
1697 /* now initialise each slot */
1698 for (i
= 0; i
< slot_count
; i
++) {
1699 memset(&ca
->slot_info
[i
], 0, sizeof(struct dvb_ca_slot
));
1700 ca
->slot_info
[i
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1701 atomic_set(&ca
->slot_info
[i
].camchange_count
, 0);
1702 ca
->slot_info
[i
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1705 if (signal_pending(current
)) {
1711 /* create a kthread for monitoring this CA device */
1713 ret
= kernel_thread(dvb_ca_en50221_thread
, ca
, 0);
1716 printk("dvb_ca_init: failed to start kernel_thread (%d)\n", ret
);
1719 ca
->thread_pid
= ret
;
1724 if (ca
->dvbdev
!= NULL
)
1725 dvb_unregister_device(ca
->dvbdev
);
1726 kfree(ca
->slot_info
);
1729 pubca
->private = NULL
;
1732 EXPORT_SYMBOL(dvb_ca_en50221_release
);
1737 * Release a DVB CA EN50221 interface device.
1739 * @param ca_dev The dvb_device_t instance for the CA device.
1740 * @param ca The associated dvb_ca instance.
1742 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1744 struct dvb_ca_private
*ca
= pubca
->private;
1747 dprintk("%s\n", __FUNCTION__
);
1749 /* shutdown the thread if there was one */
1750 if (ca
->thread_pid
) {
1751 if (kill_proc(ca
->thread_pid
, 0, 1) == -ESRCH
) {
1752 printk("dvb_ca_release adapter %d: thread PID %d already died\n",
1753 ca
->dvbdev
->adapter
->num
, ca
->thread_pid
);
1757 dvb_ca_en50221_thread_wakeup(ca
);
1758 wait_event_interruptible(ca
->thread_queue
, ca
->thread_pid
== 0);
1762 for (i
= 0; i
< ca
->slot_count
; i
++) {
1763 dvb_ca_en50221_slot_shutdown(ca
, i
);
1764 vfree(ca
->slot_info
[i
].rx_buffer
.data
);
1766 kfree(ca
->slot_info
);
1767 dvb_unregister_device(ca
->dvbdev
);
1769 pubca
->private = NULL
;