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 /* Number of CAMCHANGES that have occurred since last processing */
97 atomic_t camchange_count
;
99 /* Type of last CAMCHANGE */
102 /* base address of CAM config */
105 /* value to write into Config Control register */
108 /* if 1, the CAM supports DA IRQs */
109 u8 da_irq_supported
:1;
111 /* size of the buffer to use when talking to the CAM */
114 /* buffer for incoming packets */
115 struct dvb_ringbuffer rx_buffer
;
117 /* timer used during various states of the slot */
118 unsigned long timeout
;
121 /* Private CA-interface information */
122 struct dvb_ca_private
{
124 /* pointer back to the public data structure */
125 struct dvb_ca_en50221
*pub
;
128 struct dvb_device
*dvbdev
;
130 /* Flags describing the interface (DVB_CA_FLAG_*) */
133 /* number of slots supported by this CA interface */
134 unsigned int slot_count
;
136 /* information on each slot */
137 struct dvb_ca_slot
*slot_info
;
139 /* wait queues for read() and write() operations */
140 wait_queue_head_t wait_queue
;
142 /* PID of the monitoring thread */
143 struct task_struct
*thread
;
145 /* Flag indicating if the CA device is open */
148 /* Flag indicating the thread should wake up now */
149 unsigned int wakeup
:1;
151 /* Delay the main thread should use */
154 /* Slot to start looking for data to read from in the next user-space read operation */
158 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
);
159 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
160 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
);
164 * Safely find needle in haystack.
166 * @param haystack Buffer to look in.
167 * @param hlen Number of bytes in haystack.
168 * @param needle Buffer to find.
169 * @param nlen Number of bytes in needle.
170 * @return Pointer into haystack needle was found at, or NULL if not found.
172 static char *findstr(char * haystack
, int hlen
, char * needle
, int nlen
)
179 for (i
= 0; i
<= hlen
- nlen
; i
++) {
180 if (!strncmp(haystack
+ i
, needle
, nlen
))
189 /* ******************************************************************************** */
190 /* EN50221 physical interface functions */
196 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private
*ca
, int slot
)
203 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
) {
204 return (atomic_read(&ca
->slot_info
[slot
].camchange_count
) != 0);
208 slot_status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, ca
->open
);
210 cam_present_now
= (slot_status
& DVB_CA_EN50221_POLL_CAM_PRESENT
) ? 1 : 0;
211 cam_changed
= (slot_status
& DVB_CA_EN50221_POLL_CAM_CHANGED
) ? 1 : 0;
213 int cam_present_old
= (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
);
214 cam_changed
= (cam_present_now
!= cam_present_old
);
218 if (!cam_present_now
) {
219 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
221 ca
->slot_info
[slot
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_INSERTED
;
223 atomic_set(&ca
->slot_info
[slot
].camchange_count
, 1);
225 if ((ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) &&
226 (slot_status
& DVB_CA_EN50221_POLL_CAM_READY
)) {
227 // move to validate state if reset is completed
228 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
237 * Wait for flags to become set on the STATUS register on a CAM interface,
238 * checking for errors and timeout.
240 * @param ca CA instance.
241 * @param slot Slot on interface.
242 * @param waitfor Flags to wait for.
243 * @param timeout_ms Timeout in milliseconds.
245 * @return 0 on success, nonzero on error.
247 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private
*ca
, int slot
,
248 u8 waitfor
, int timeout_hz
)
250 unsigned long timeout
;
253 dprintk("%s\n", __FUNCTION__
);
255 /* loop until timeout elapsed */
257 timeout
= jiffies
+ timeout_hz
;
259 /* read the status and check for error */
260 int res
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
264 /* if we got the flags, it was successful! */
266 dprintk("%s succeeded timeout:%lu\n", __FUNCTION__
, jiffies
- start
);
270 /* check for timeout */
271 if (time_after(jiffies
, timeout
)) {
279 dprintk("%s failed timeout:%lu\n", __FUNCTION__
, jiffies
- start
);
281 /* if we get here, we've timed out */
287 * Initialise the link layer connection to a CAM.
289 * @param ca CA instance.
290 * @param slot Slot id.
292 * @return 0 on success, nonzero on failure.
294 static int dvb_ca_en50221_link_init(struct dvb_ca_private
*ca
, int slot
)
300 dprintk("%s\n", __FUNCTION__
);
302 /* we'll be determining these during this function */
303 ca
->slot_info
[slot
].da_irq_supported
= 0;
305 /* set the host link buffer size temporarily. it will be overwritten with the
306 * real negotiated size later. */
307 ca
->slot_info
[slot
].link_buf_size
= 2;
309 /* read the buffer size from the CAM */
310 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SR
)) != 0)
312 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_DA
, HZ
/ 10)) != 0)
314 if ((ret
= dvb_ca_en50221_read_data(ca
, slot
, buf
, 2)) != 2)
316 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
319 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
320 buf_size
= (buf
[0] << 8) | buf
[1];
321 if (buf_size
> HOST_LINK_BUF_SIZE
)
322 buf_size
= HOST_LINK_BUF_SIZE
;
323 ca
->slot_info
[slot
].link_buf_size
= buf_size
;
324 buf
[0] = buf_size
>> 8;
325 buf
[1] = buf_size
& 0xff;
326 dprintk("Chosen link buffer size of %i\n", buf_size
);
328 /* write the buffer size to the CAM */
329 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
| CMDREG_SW
)) != 0)
331 if ((ret
= dvb_ca_en50221_wait_if_status(ca
, slot
, STATUSREG_FR
, HZ
/ 10)) != 0)
333 if ((ret
= dvb_ca_en50221_write_data(ca
, slot
, buf
, 2)) != 2)
335 if ((ret
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
)) != 0)
343 * Read a tuple from attribute memory.
345 * @param ca CA instance.
346 * @param slot Slot id.
347 * @param address Address to read from. Updated.
348 * @param tupleType Tuple id byte. Updated.
349 * @param tupleLength Tuple length. Updated.
350 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
352 * @return 0 on success, nonzero on error.
354 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private
*ca
, int slot
,
355 int *address
, int *tupleType
, int *tupleLength
, u8
* tuple
)
360 int _address
= *address
;
362 /* grab the next tuple length and type */
363 if ((_tupleType
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
)) < 0)
365 if (_tupleType
== 0xff) {
366 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType
);
368 *tupleType
= _tupleType
;
372 if ((_tupleLength
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ 2)) < 0)
376 dprintk("TUPLE type:0x%x length:%i\n", _tupleType
, _tupleLength
);
378 /* read in the whole tuple */
379 for (i
= 0; i
< _tupleLength
; i
++) {
380 tuple
[i
] = ca
->pub
->read_attribute_mem(ca
->pub
, slot
, _address
+ (i
* 2));
381 dprintk(" 0x%02x: 0x%02x %c\n",
383 ((tuple
[i
] > 31) && (tuple
[i
] < 127)) ? tuple
[i
] : '.');
385 _address
+= (_tupleLength
* 2);
388 *tupleType
= _tupleType
;
389 *tupleLength
= _tupleLength
;
396 * Parse attribute memory of a CAM module, extracting Config register, and checking
397 * it is a DVB CAM module.
399 * @param ca CA instance.
400 * @param slot Slot id.
402 * @return 0 on success, <0 on failure.
404 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private
*ca
, int slot
)
413 int got_cftableentry
= 0;
422 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
424 if (tupleType
!= 0x1D)
431 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
433 if (tupleType
!= 0x1C)
440 dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
, &tupleLength
, tuple
)) < 0)
442 if (tupleType
!= 0x15)
448 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
449 &tupleLength
, tuple
)) < 0)
451 if (tupleType
!= 0x20)
453 if (tupleLength
!= 4)
455 manfid
= (tuple
[1] << 8) | tuple
[0];
456 devid
= (tuple
[3] << 8) | tuple
[2];
461 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
462 &tupleLength
, tuple
)) < 0)
464 if (tupleType
!= 0x1A)
469 /* extract the configbase */
471 if (tupleLength
< (3 + rasz
+ 14))
473 ca
->slot_info
[slot
].config_base
= 0;
474 for (i
= 0; i
< rasz
+ 1; i
++) {
475 ca
->slot_info
[slot
].config_base
|= (tuple
[2 + i
] << (8 * i
));
478 /* check it contains the correct DVB string */
479 dvb_str
= findstr((char *)tuple
, tupleLength
, "DVB_CI_V", 8);
482 if (tupleLength
< ((dvb_str
- (char *) tuple
) + 12))
485 /* is it a version we support? */
486 if (strncmp(dvb_str
+ 8, "1.00", 4)) {
487 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
488 ca
->dvbdev
->adapter
->num
, dvb_str
[8], dvb_str
[9], dvb_str
[10], dvb_str
[11]);
492 /* process the CFTABLE_ENTRY tuples, and any after those */
493 while ((!end_chain
) && (address
< 0x1000)) {
494 if ((status
= dvb_ca_en50221_read_tuple(ca
, slot
, &address
, &tupleType
,
495 &tupleLength
, tuple
)) < 0)
498 case 0x1B: // CISTPL_CFTABLE_ENTRY
499 if (tupleLength
< (2 + 11 + 17))
502 /* if we've already parsed one, just use it */
503 if (got_cftableentry
)
506 /* get the config option */
507 ca
->slot_info
[slot
].config_option
= tuple
[0] & 0x3f;
509 /* OK, check it contains the correct strings */
510 if ((findstr((char *)tuple
, tupleLength
, "DVB_HOST", 8) == NULL
) ||
511 (findstr((char *)tuple
, tupleLength
, "DVB_CI_MODULE", 13) == NULL
))
514 got_cftableentry
= 1;
517 case 0x14: // CISTPL_NO_LINK
520 case 0xFF: // CISTPL_END
524 default: /* Unknown tuple type - just skip this tuple and move to the next one */
525 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType
,
531 if ((address
> 0x1000) || (!got_cftableentry
))
534 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
535 manfid
, devid
, ca
->slot_info
[slot
].config_base
, ca
->slot_info
[slot
].config_option
);
543 * Set CAM's configoption correctly.
545 * @param ca CA instance.
546 * @param slot Slot containing the CAM.
548 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private
*ca
, int slot
)
552 dprintk("%s\n", __FUNCTION__
);
554 /* set the config option */
555 ca
->pub
->write_attribute_mem(ca
->pub
, slot
,
556 ca
->slot_info
[slot
].config_base
,
557 ca
->slot_info
[slot
].config_option
);
560 configoption
= ca
->pub
->read_attribute_mem(ca
->pub
, slot
, ca
->slot_info
[slot
].config_base
);
561 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
562 ca
->slot_info
[slot
].config_option
, configoption
& 0x3f);
571 * This function talks to an EN50221 CAM control interface. It reads a buffer of
572 * data from the CAM. The data can either be stored in a supplied buffer, or
573 * automatically be added to the slot's rx_buffer.
575 * @param ca CA instance.
576 * @param slot Slot to read from.
577 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
578 * the data will be added into the buffering system as a normal fragment.
579 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
581 * @return Number of bytes read, or < 0 on error
583 static int dvb_ca_en50221_read_data(struct dvb_ca_private
*ca
, int slot
, u8
* ebuf
, int ecount
)
587 u8 buf
[HOST_LINK_BUF_SIZE
];
590 dprintk("%s\n", __FUNCTION__
);
592 /* check if we have space for a link buf in the rx_buffer */
596 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
600 buf_free
= dvb_ringbuffer_free(&ca
->slot_info
[slot
].rx_buffer
);
602 if (buf_free
< (ca
->slot_info
[slot
].link_buf_size
+ DVB_RINGBUFFER_PKTHDRSIZE
)) {
608 /* check if there is data available */
609 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
611 if (!(status
& STATUSREG_DA
)) {
617 /* read the amount of data */
618 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
)) < 0)
620 bytes_read
= status
<< 8;
621 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
)) < 0)
623 bytes_read
|= status
;
625 /* check it will fit */
627 if (bytes_read
> ca
->slot_info
[slot
].link_buf_size
) {
628 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
629 ca
->dvbdev
->adapter
->num
, bytes_read
, ca
->slot_info
[slot
].link_buf_size
);
630 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
634 if (bytes_read
< 2) {
635 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
636 ca
->dvbdev
->adapter
->num
);
637 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
642 if (bytes_read
> ecount
) {
643 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
644 ca
->dvbdev
->adapter
->num
);
650 /* fill the buffer */
651 for (i
= 0; i
< bytes_read
; i
++) {
652 /* read byte and check */
653 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_DATA
)) < 0)
656 /* OK, store it in the buffer */
660 /* check for read error (RE should now be 0) */
661 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
663 if (status
& STATUSREG_RE
) {
664 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
669 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
671 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
675 dvb_ringbuffer_pkt_write(&ca
->slot_info
[slot
].rx_buffer
, buf
, bytes_read
);
677 memcpy(ebuf
, buf
, bytes_read
);
680 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot
,
681 buf
[0], (buf
[1] & 0x80) == 0, bytes_read
);
683 /* wake up readers when a last_fragment is received */
684 if ((buf
[1] & 0x80) == 0x00) {
685 wake_up_interruptible(&ca
->wait_queue
);
695 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
698 * @param ca CA instance.
699 * @param slot Slot to write to.
700 * @param ebuf The data in this buffer is treated as a complete link-level packet to
702 * @param count Size of ebuf.
704 * @return Number of bytes written, or < 0 on error.
706 static int dvb_ca_en50221_write_data(struct dvb_ca_private
*ca
, int slot
, u8
* buf
, int bytes_write
)
711 dprintk("%s\n", __FUNCTION__
);
715 if (bytes_write
> ca
->slot_info
[slot
].link_buf_size
)
718 /* check if interface is actually waiting for us to read from it, or if a read is in progress */
719 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
721 if (status
& (STATUSREG_DA
| STATUSREG_RE
)) {
727 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
,
728 IRQEN
| CMDREG_HC
)) != 0)
731 /* check if interface is still free */
732 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
734 if (!(status
& STATUSREG_FR
)) {
735 /* it wasn't free => try again later */
740 /* send the amount of data */
741 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_HIGH
, bytes_write
>> 8)) != 0)
743 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_SIZE_LOW
,
744 bytes_write
& 0xff)) != 0)
747 /* send the buffer */
748 for (i
= 0; i
< bytes_write
; i
++) {
749 if ((status
= ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_DATA
, buf
[i
])) != 0)
753 /* check for write error (WE should now be 0) */
754 if ((status
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
)) < 0)
756 if (status
& STATUSREG_WE
) {
757 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
761 status
= bytes_write
;
763 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot
,
764 buf
[0], (buf
[1] & 0x80) == 0, bytes_write
);
767 ca
->pub
->write_cam_control(ca
->pub
, slot
, CTRLIF_COMMAND
, IRQEN
);
772 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq
);
776 /* ******************************************************************************** */
777 /* EN50221 higher level functions */
781 * A CAM has been removed => shut it down.
783 * @param ca CA instance.
784 * @param slot Slot to shut down.
786 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private
*ca
, int slot
)
788 dprintk("%s\n", __FUNCTION__
);
790 ca
->pub
->slot_shutdown(ca
->pub
, slot
);
791 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
793 /* need to wake up all processes to check if they're now
794 trying to write to a defunct CAM */
795 wake_up_interruptible(&ca
->wait_queue
);
797 dprintk("Slot %i shutdown\n", slot
);
802 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq
);
806 * A CAMCHANGE IRQ has occurred.
808 * @param ca CA instance.
809 * @param slot Slot concerned.
810 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
812 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221
*pubca
, int slot
, int change_type
)
814 struct dvb_ca_private
*ca
= pubca
->private;
816 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot
, change_type
);
818 switch (change_type
) {
819 case DVB_CA_EN50221_CAMCHANGE_REMOVED
:
820 case DVB_CA_EN50221_CAMCHANGE_INSERTED
:
827 ca
->slot_info
[slot
].camchange_type
= change_type
;
828 atomic_inc(&ca
->slot_info
[slot
].camchange_count
);
829 dvb_ca_en50221_thread_wakeup(ca
);
831 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq
);
835 * A CAMREADY IRQ has occurred.
837 * @param ca CA instance.
838 * @param slot Slot concerned.
840 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221
*pubca
, int slot
)
842 struct dvb_ca_private
*ca
= pubca
->private;
844 dprintk("CAMREADY IRQ slot:%i\n", slot
);
846 if (ca
->slot_info
[slot
].slot_state
== DVB_CA_SLOTSTATE_WAITREADY
) {
847 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_VALIDATE
;
848 dvb_ca_en50221_thread_wakeup(ca
);
854 * An FR or DA IRQ has occurred.
856 * @param ca CA instance.
857 * @param slot Slot concerned.
859 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221
*pubca
, int slot
)
861 struct dvb_ca_private
*ca
= pubca
->private;
864 dprintk("FR/DA IRQ slot:%i\n", slot
);
866 switch (ca
->slot_info
[slot
].slot_state
) {
867 case DVB_CA_SLOTSTATE_LINKINIT
:
868 flags
= ca
->pub
->read_cam_control(pubca
, slot
, CTRLIF_STATUS
);
869 if (flags
& STATUSREG_DA
) {
870 dprintk("CAM supports DA IRQ\n");
871 ca
->slot_info
[slot
].da_irq_supported
= 1;
875 case DVB_CA_SLOTSTATE_RUNNING
:
877 dvb_ca_en50221_thread_wakeup(ca
);
884 /* ******************************************************************************** */
885 /* EN50221 thread functions */
888 * Wake up the DVB CA thread
890 * @param ca CA instance.
892 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private
*ca
)
895 dprintk("%s\n", __FUNCTION__
);
899 wake_up_process(ca
->thread
);
903 * Update the delay used by the thread.
905 * @param ca CA instance.
907 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private
*ca
)
910 int curdelay
= 100000000;
913 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
914 switch (ca
->slot_info
[slot
].slot_state
) {
916 case DVB_CA_SLOTSTATE_NONE
:
917 case DVB_CA_SLOTSTATE_INVALID
:
919 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) {
924 case DVB_CA_SLOTSTATE_UNINITIALISED
:
925 case DVB_CA_SLOTSTATE_WAITREADY
:
926 case DVB_CA_SLOTSTATE_VALIDATE
:
927 case DVB_CA_SLOTSTATE_WAITFR
:
928 case DVB_CA_SLOTSTATE_LINKINIT
:
932 case DVB_CA_SLOTSTATE_RUNNING
:
934 if (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) {
938 if ((!ca
->slot_info
[slot
].da_irq_supported
) ||
939 (!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_DA
))) {
946 if (delay
< curdelay
)
950 ca
->delay
= curdelay
;
956 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
958 static int dvb_ca_en50221_thread(void *data
)
960 struct dvb_ca_private
*ca
= data
;
967 dprintk("%s\n", __FUNCTION__
);
969 /* choose the correct initial delay */
970 dvb_ca_en50221_thread_update_delay(ca
);
973 while (!kthread_should_stop()) {
974 /* sleep for a bit */
976 set_current_state(TASK_INTERRUPTIBLE
);
977 schedule_timeout(ca
->delay
);
978 if (kthread_should_stop())
983 /* go through all the slots processing them */
984 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
986 // check the cam status + deal with CAMCHANGEs
987 while (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
988 /* clear down an old CI slot if necessary */
989 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
990 dvb_ca_en50221_slot_shutdown(ca
, slot
);
992 /* if a CAM is NOW present, initialise it */
993 if (ca
->slot_info
[slot
].camchange_type
== DVB_CA_EN50221_CAMCHANGE_INSERTED
) {
994 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_UNINITIALISED
;
997 /* we've handled one CAMCHANGE */
998 dvb_ca_en50221_thread_update_delay(ca
);
999 atomic_dec(&ca
->slot_info
[slot
].camchange_count
);
1002 // CAM state machine
1003 switch (ca
->slot_info
[slot
].slot_state
) {
1004 case DVB_CA_SLOTSTATE_NONE
:
1005 case DVB_CA_SLOTSTATE_INVALID
:
1009 case DVB_CA_SLOTSTATE_UNINITIALISED
:
1010 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITREADY
;
1011 ca
->pub
->slot_reset(ca
->pub
, slot
);
1012 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1015 case DVB_CA_SLOTSTATE_WAITREADY
:
1016 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1017 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1018 ca
->dvbdev
->adapter
->num
);
1019 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1020 dvb_ca_en50221_thread_update_delay(ca
);
1023 // no other action needed; will automatically change state when ready
1026 case DVB_CA_SLOTSTATE_VALIDATE
:
1027 if (dvb_ca_en50221_parse_attributes(ca
, slot
) != 0) {
1028 /* we need this extra check for annoying interfaces like the budget-av */
1029 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1030 (ca
->pub
->poll_slot_status
)) {
1031 int status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1032 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1033 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1034 dvb_ca_en50221_thread_update_delay(ca
);
1039 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1040 ca
->dvbdev
->adapter
->num
);
1041 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1042 dvb_ca_en50221_thread_update_delay(ca
);
1045 if (dvb_ca_en50221_set_configoption(ca
, slot
) != 0) {
1046 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1047 ca
->dvbdev
->adapter
->num
);
1048 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1049 dvb_ca_en50221_thread_update_delay(ca
);
1052 if (ca
->pub
->write_cam_control(ca
->pub
, slot
,
1053 CTRLIF_COMMAND
, CMDREG_RS
) != 0) {
1054 printk("dvb_ca adapter %d: Unable to reset CAM IF\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 dprintk("DVB CAM validated successfully\n");
1062 ca
->slot_info
[slot
].timeout
= jiffies
+ (INIT_TIMEOUT_SECS
* HZ
);
1063 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_WAITFR
;
1067 case DVB_CA_SLOTSTATE_WAITFR
:
1068 if (time_after(jiffies
, ca
->slot_info
[slot
].timeout
)) {
1069 printk("dvb_ca adapter %d: DVB CAM did not respond :(\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
);
1076 flags
= ca
->pub
->read_cam_control(ca
->pub
, slot
, CTRLIF_STATUS
);
1077 if (flags
& STATUSREG_FR
) {
1078 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_LINKINIT
;
1083 case DVB_CA_SLOTSTATE_LINKINIT
:
1084 if (dvb_ca_en50221_link_init(ca
, slot
) != 0) {
1085 /* we need this extra check for annoying interfaces like the budget-av */
1086 if ((!(ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)) &&
1087 (ca
->pub
->poll_slot_status
)) {
1088 int status
= ca
->pub
->poll_slot_status(ca
->pub
, slot
, 0);
1089 if (!(status
& DVB_CA_EN50221_POLL_CAM_PRESENT
)) {
1090 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1091 dvb_ca_en50221_thread_update_delay(ca
);
1096 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca
->dvbdev
->adapter
->num
);
1097 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1098 dvb_ca_en50221_thread_update_delay(ca
);
1102 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1103 rxbuf
= vmalloc(RX_BUFFER_SIZE
);
1104 if (rxbuf
== NULL
) {
1105 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca
->dvbdev
->adapter
->num
);
1106 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_INVALID
;
1107 dvb_ca_en50221_thread_update_delay(ca
);
1110 dvb_ringbuffer_init(&ca
->slot_info
[slot
].rx_buffer
, rxbuf
, RX_BUFFER_SIZE
);
1113 ca
->pub
->slot_ts_enable(ca
->pub
, slot
);
1114 ca
->slot_info
[slot
].slot_state
= DVB_CA_SLOTSTATE_RUNNING
;
1115 dvb_ca_en50221_thread_update_delay(ca
);
1116 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca
->dvbdev
->adapter
->num
);
1119 case DVB_CA_SLOTSTATE_RUNNING
:
1123 // poll slots for data
1125 while ((status
= dvb_ca_en50221_read_data(ca
, slot
, NULL
, 0)) > 0) {
1129 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1130 if (dvb_ca_en50221_check_camstatus(ca
, slot
)) {
1131 // we dont want to sleep on the next iteration so we can handle the cam change
1136 /* check if we've hit our limit this time */
1137 if (++pktcount
>= MAX_RX_PACKETS_PER_ITERATION
) {
1138 // dont sleep; there is likely to be more data to read
1153 /* ******************************************************************************** */
1154 /* EN50221 IO interface functions */
1157 * Real ioctl implementation.
1158 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1160 * @param inode Inode concerned.
1161 * @param file File concerned.
1162 * @param cmd IOCTL command.
1163 * @param arg Associated argument.
1165 * @return 0 on success, <0 on error.
1167 static int dvb_ca_en50221_io_do_ioctl(struct inode
*inode
, struct file
*file
,
1168 unsigned int cmd
, void *parg
)
1170 struct dvb_device
*dvbdev
= file
->private_data
;
1171 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1175 dprintk("%s\n", __FUNCTION__
);
1179 for (slot
= 0; slot
< ca
->slot_count
; slot
++) {
1180 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_NONE
) {
1181 dvb_ca_en50221_slot_shutdown(ca
, slot
);
1182 if (ca
->flags
& DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE
)
1183 dvb_ca_en50221_camchange_irq(ca
->pub
,
1185 DVB_CA_EN50221_CAMCHANGE_INSERTED
);
1188 ca
->next_read_slot
= 0;
1189 dvb_ca_en50221_thread_wakeup(ca
);
1193 struct ca_caps
*caps
= parg
;
1195 caps
->slot_num
= ca
->slot_count
;
1196 caps
->slot_type
= CA_CI_LINK
;
1197 caps
->descr_num
= 0;
1198 caps
->descr_type
= 0;
1202 case CA_GET_SLOT_INFO
: {
1203 struct ca_slot_info
*info
= parg
;
1205 if ((info
->num
> ca
->slot_count
) || (info
->num
< 0))
1208 info
->type
= CA_CI_LINK
;
1210 if ((ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_NONE
)
1211 && (ca
->slot_info
[info
->num
].slot_state
!= DVB_CA_SLOTSTATE_INVALID
)) {
1212 info
->flags
= CA_CI_MODULE_PRESENT
;
1214 if (ca
->slot_info
[info
->num
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1215 info
->flags
|= CA_CI_MODULE_READY
;
1230 * Wrapper for ioctl implementation.
1232 * @param inode Inode concerned.
1233 * @param file File concerned.
1234 * @param cmd IOCTL command.
1235 * @param arg Associated argument.
1237 * @return 0 on success, <0 on error.
1239 static int dvb_ca_en50221_io_ioctl(struct inode
*inode
, struct file
*file
,
1240 unsigned int cmd
, unsigned long arg
)
1242 return dvb_usercopy(inode
, file
, cmd
, arg
, dvb_ca_en50221_io_do_ioctl
);
1247 * Implementation of write() syscall.
1249 * @param file File structure.
1250 * @param buf Source buffer.
1251 * @param count Size of source buffer.
1252 * @param ppos Position in file (ignored).
1254 * @return Number of bytes read, or <0 on error.
1256 static ssize_t
dvb_ca_en50221_io_write(struct file
*file
,
1257 const char __user
* buf
, size_t count
, loff_t
* ppos
)
1259 struct dvb_device
*dvbdev
= file
->private_data
;
1260 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1261 u8 slot
, connection_id
;
1263 u8 fragbuf
[HOST_LINK_BUF_SIZE
];
1266 unsigned long timeout
;
1269 dprintk("%s\n", __FUNCTION__
);
1271 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1275 /* extract slot & connection id */
1276 if (copy_from_user(&slot
, buf
, 1))
1278 if (copy_from_user(&connection_id
, buf
+ 1, 1))
1283 /* check if the slot is actually running */
1284 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1287 /* fragment the packets & store in the buffer */
1288 while (fragpos
< count
) {
1289 fraglen
= ca
->slot_info
[slot
].link_buf_size
- 2;
1290 if ((count
- fragpos
) < fraglen
)
1291 fraglen
= count
- fragpos
;
1293 fragbuf
[0] = connection_id
;
1294 fragbuf
[1] = ((fragpos
+ fraglen
) < count
) ? 0x80 : 0x00;
1295 if ((status
= copy_from_user(fragbuf
+ 2, buf
+ fragpos
, fraglen
)) != 0)
1298 timeout
= jiffies
+ HZ
/ 2;
1300 while (!time_after(jiffies
, timeout
)) {
1301 /* check the CAM hasn't been removed/reset in the meantime */
1302 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
) {
1307 status
= dvb_ca_en50221_write_data(ca
, slot
, fragbuf
, fraglen
+ 2);
1308 if (status
== (fraglen
+ 2)) {
1312 if (status
!= -EAGAIN
)
1332 * Condition for waking up in dvb_ca_en50221_io_read_condition
1334 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private
*ca
,
1335 int *result
, int *_slot
)
1341 int connection_id
= -1;
1345 slot
= ca
->next_read_slot
;
1346 while ((slot_count
< ca
->slot_count
) && (!found
)) {
1347 if (ca
->slot_info
[slot
].slot_state
!= DVB_CA_SLOTSTATE_RUNNING
)
1350 if (ca
->slot_info
[slot
].rx_buffer
.data
== NULL
) {
1354 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1356 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2, 0);
1357 if (connection_id
== -1)
1358 connection_id
= hdr
[0];
1359 if ((hdr
[0] == connection_id
) && ((hdr
[1] & 0x80) == 0)) {
1365 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1369 slot
= (slot
+ 1) % ca
->slot_count
;
1373 ca
->next_read_slot
= slot
;
1379 * Implementation of read() syscall.
1381 * @param file File structure.
1382 * @param buf Destination buffer.
1383 * @param count Size of destination buffer.
1384 * @param ppos Position in file (ignored).
1386 * @return Number of bytes read, or <0 on error.
1388 static ssize_t
dvb_ca_en50221_io_read(struct file
*file
, char __user
* buf
,
1389 size_t count
, loff_t
* ppos
)
1391 struct dvb_device
*dvbdev
= file
->private_data
;
1392 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1397 int connection_id
= -1;
1399 int last_fragment
= 0;
1404 dprintk("%s\n", __FUNCTION__
);
1406 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1410 /* wait for some data */
1411 if ((status
= dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
)) == 0) {
1413 /* if we're in nonblocking mode, exit immediately */
1414 if (file
->f_flags
& O_NONBLOCK
)
1415 return -EWOULDBLOCK
;
1417 /* wait for some data */
1418 status
= wait_event_interruptible(ca
->wait_queue
,
1419 dvb_ca_en50221_io_read_condition
1420 (ca
, &result
, &slot
));
1422 if ((status
< 0) || (result
< 0)) {
1428 idx
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, -1, &fraglen
);
1432 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca
->dvbdev
->adapter
->num
);
1437 dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 0, hdr
, 2, 0);
1438 if (connection_id
== -1)
1439 connection_id
= hdr
[0];
1440 if (hdr
[0] == connection_id
) {
1441 if (pktlen
< count
) {
1442 if ((pktlen
+ fraglen
- 2) > count
) {
1443 fraglen
= count
- pktlen
;
1448 if ((status
= dvb_ringbuffer_pkt_read(&ca
->slot_info
[slot
].rx_buffer
, idx
, 2,
1449 (u8
*)buf
+ pktlen
, fraglen
, 1)) < 0) {
1455 if ((hdr
[1] & 0x80) == 0)
1460 idx2
= dvb_ringbuffer_pkt_next(&ca
->slot_info
[slot
].rx_buffer
, idx
, &fraglen
);
1462 dvb_ringbuffer_pkt_dispose(&ca
->slot_info
[slot
].rx_buffer
, idx
);
1465 } while (!last_fragment
);
1468 hdr
[1] = connection_id
;
1469 if ((status
= copy_to_user(buf
, hdr
, 2)) != 0)
1479 * Implementation of file open syscall.
1481 * @param inode Inode concerned.
1482 * @param file File concerned.
1484 * @return 0 on success, <0 on failure.
1486 static int dvb_ca_en50221_io_open(struct inode
*inode
, struct file
*file
)
1488 struct dvb_device
*dvbdev
= file
->private_data
;
1489 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1493 dprintk("%s\n", __FUNCTION__
);
1495 if (!try_module_get(ca
->pub
->owner
))
1498 err
= dvb_generic_open(inode
, file
);
1500 module_put(ca
->pub
->owner
);
1504 for (i
= 0; i
< ca
->slot_count
; i
++) {
1506 if (ca
->slot_info
[i
].slot_state
== DVB_CA_SLOTSTATE_RUNNING
) {
1507 if (ca
->slot_info
[i
].rx_buffer
.data
!= NULL
) {
1508 /* it is safe to call this here without locks because
1509 * ca->open == 0. Data is not read in this case */
1510 dvb_ringbuffer_flush(&ca
->slot_info
[i
].rx_buffer
);
1516 dvb_ca_en50221_thread_update_delay(ca
);
1517 dvb_ca_en50221_thread_wakeup(ca
);
1524 * Implementation of file close syscall.
1526 * @param inode Inode concerned.
1527 * @param file File concerned.
1529 * @return 0 on success, <0 on failure.
1531 static int dvb_ca_en50221_io_release(struct inode
*inode
, struct file
*file
)
1533 struct dvb_device
*dvbdev
= file
->private_data
;
1534 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1537 dprintk("%s\n", __FUNCTION__
);
1539 /* mark the CA device as closed */
1541 dvb_ca_en50221_thread_update_delay(ca
);
1543 err
= dvb_generic_release(inode
, file
);
1545 module_put(ca
->pub
->owner
);
1552 * Implementation of poll() syscall.
1554 * @param file File concerned.
1555 * @param wait poll wait table.
1557 * @return Standard poll mask.
1559 static unsigned int dvb_ca_en50221_io_poll(struct file
*file
, poll_table
* wait
)
1561 struct dvb_device
*dvbdev
= file
->private_data
;
1562 struct dvb_ca_private
*ca
= dvbdev
->priv
;
1563 unsigned int mask
= 0;
1567 dprintk("%s\n", __FUNCTION__
);
1569 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1573 /* if there is something, return now */
1577 /* wait for something to happen */
1578 poll_wait(file
, &ca
->wait_queue
, wait
);
1580 if (dvb_ca_en50221_io_read_condition(ca
, &result
, &slot
) == 1) {
1586 EXPORT_SYMBOL(dvb_ca_en50221_init
);
1589 static struct file_operations dvb_ca_fops
= {
1590 .owner
= THIS_MODULE
,
1591 .read
= dvb_ca_en50221_io_read
,
1592 .write
= dvb_ca_en50221_io_write
,
1593 .ioctl
= dvb_ca_en50221_io_ioctl
,
1594 .open
= dvb_ca_en50221_io_open
,
1595 .release
= dvb_ca_en50221_io_release
,
1596 .poll
= dvb_ca_en50221_io_poll
,
1599 static struct dvb_device dvbdev_ca
= {
1604 .fops
= &dvb_ca_fops
,
1608 /* ******************************************************************************** */
1609 /* Initialisation/shutdown functions */
1613 * Initialise a new DVB CA EN50221 interface device.
1615 * @param dvb_adapter DVB adapter to attach the new CA device to.
1616 * @param ca The dvb_ca instance.
1617 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1618 * @param slot_count Number of slots supported.
1620 * @return 0 on success, nonzero on failure
1622 int dvb_ca_en50221_init(struct dvb_adapter
*dvb_adapter
,
1623 struct dvb_ca_en50221
*pubca
, int flags
, int slot_count
)
1626 struct dvb_ca_private
*ca
= NULL
;
1629 dprintk("%s\n", __FUNCTION__
);
1634 /* initialise the system data */
1635 if ((ca
= kzalloc(sizeof(struct dvb_ca_private
), GFP_KERNEL
)) == NULL
) {
1641 ca
->slot_count
= slot_count
;
1642 if ((ca
->slot_info
= kcalloc(slot_count
, sizeof(struct dvb_ca_slot
), GFP_KERNEL
)) == NULL
) {
1646 init_waitqueue_head(&ca
->wait_queue
);
1649 ca
->next_read_slot
= 0;
1650 pubca
->private = ca
;
1652 /* register the DVB device */
1653 ret
= dvb_register_device(dvb_adapter
, &ca
->dvbdev
, &dvbdev_ca
, ca
, DVB_DEVICE_CA
);
1657 /* now initialise each slot */
1658 for (i
= 0; i
< slot_count
; i
++) {
1659 memset(&ca
->slot_info
[i
], 0, sizeof(struct dvb_ca_slot
));
1660 ca
->slot_info
[i
].slot_state
= DVB_CA_SLOTSTATE_NONE
;
1661 atomic_set(&ca
->slot_info
[i
].camchange_count
, 0);
1662 ca
->slot_info
[i
].camchange_type
= DVB_CA_EN50221_CAMCHANGE_REMOVED
;
1665 if (signal_pending(current
)) {
1671 /* create a kthread for monitoring this CA device */
1672 ca
->thread
= kthread_run(dvb_ca_en50221_thread
, ca
, "kdvb-ca-%i:%i",
1673 ca
->dvbdev
->adapter
->num
, ca
->dvbdev
->id
);
1674 if (IS_ERR(ca
->thread
)) {
1675 ret
= PTR_ERR(ca
->thread
);
1676 printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
1684 if (ca
->dvbdev
!= NULL
)
1685 dvb_unregister_device(ca
->dvbdev
);
1686 kfree(ca
->slot_info
);
1689 pubca
->private = NULL
;
1692 EXPORT_SYMBOL(dvb_ca_en50221_release
);
1697 * Release a DVB CA EN50221 interface device.
1699 * @param ca_dev The dvb_device_t instance for the CA device.
1700 * @param ca The associated dvb_ca instance.
1702 void dvb_ca_en50221_release(struct dvb_ca_en50221
*pubca
)
1704 struct dvb_ca_private
*ca
= pubca
->private;
1707 dprintk("%s\n", __FUNCTION__
);
1709 /* shutdown the thread if there was one */
1710 kthread_stop(ca
->thread
);
1712 for (i
= 0; i
< ca
->slot_count
; i
++) {
1713 dvb_ca_en50221_slot_shutdown(ca
, i
);
1714 vfree(ca
->slot_info
[i
].rx_buffer
.data
);
1716 kfree(ca
->slot_info
);
1717 dvb_unregister_device(ca
->dvbdev
);
1719 pubca
->private = NULL
;