Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / media / dvb / dvb-core / dvb_ca_en50221.c
blob89437fdab8befcd393578c57f8c44d9434787612
1 /*
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>
10 * based on code:
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
59 #define CTRLIF_DATA 0
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 */
91 struct dvb_ca_slot {
93 /* current state of the CAM */
94 int slot_state;
96 /* Number of CAMCHANGES that have occurred since last processing */
97 atomic_t camchange_count;
99 /* Type of last CAMCHANGE */
100 int camchange_type;
102 /* base address of CAM config */
103 u32 config_base;
105 /* value to write into Config Control register */
106 u8 config_option;
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 */
112 int link_buf_size;
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;
127 /* the DVB device */
128 struct dvb_device *dvbdev;
130 /* Flags describing the interface (DVB_CA_FLAG_*) */
131 u32 flags;
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 */
146 unsigned int open:1;
148 /* Flag indicating the thread should wake up now */
149 unsigned int wakeup:1;
151 /* Delay the main thread should use */
152 unsigned long delay;
154 /* Slot to start looking for data to read from in the next user-space read operation */
155 int next_read_slot;
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)
174 int i;
176 if (hlen < nlen)
177 return NULL;
179 for (i = 0; i <= hlen - nlen; i++) {
180 if (!strncmp(haystack + i, needle, nlen))
181 return haystack + i;
184 return NULL;
189 /* ******************************************************************************** */
190 /* EN50221 physical interface functions */
194 * Check CAM status.
196 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
198 int slot_status;
199 int cam_present_now;
200 int cam_changed;
202 /* IRQ mode */
203 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
204 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
207 /* poll mode */
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;
212 if (!cam_changed) {
213 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
214 cam_changed = (cam_present_now != cam_present_old);
217 if (cam_changed) {
218 if (!cam_present_now) {
219 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
220 } else {
221 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
223 atomic_set(&ca->slot_info[slot].camchange_count, 1);
224 } else {
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;
232 return cam_changed;
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;
251 unsigned long start;
253 dprintk("%s\n", __FUNCTION__);
255 /* loop until timeout elapsed */
256 start = jiffies;
257 timeout = jiffies + timeout_hz;
258 while (1) {
259 /* read the status and check for error */
260 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
261 if (res < 0)
262 return -EIO;
264 /* if we got the flags, it was successful! */
265 if (res & waitfor) {
266 dprintk("%s succeeded timeout:%lu\n", __FUNCTION__, jiffies - start);
267 return 0;
270 /* check for timeout */
271 if (time_after(jiffies, timeout)) {
272 break;
275 /* wait for a bit */
276 msleep(1);
279 dprintk("%s failed timeout:%lu\n", __FUNCTION__, jiffies - start);
281 /* if we get here, we've timed out */
282 return -ETIMEDOUT;
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)
296 int ret;
297 int buf_size;
298 u8 buf[2];
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)
311 return ret;
312 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
313 return ret;
314 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
315 return -EIO;
316 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
317 return ret;
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)
330 return ret;
331 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
332 return ret;
333 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
334 return -EIO;
335 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
336 return ret;
338 /* success */
339 return 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)
357 int i;
358 int _tupleType;
359 int _tupleLength;
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)
364 return _tupleType;
365 if (_tupleType == 0xff) {
366 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
367 *address += 2;
368 *tupleType = _tupleType;
369 *tupleLength = 0;
370 return 0;
372 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
373 return _tupleLength;
374 _address += 4;
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",
382 i, tuple[i] & 0xff,
383 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
385 _address += (_tupleLength * 2);
387 // success
388 *tupleType = _tupleType;
389 *tupleLength = _tupleLength;
390 *address = _address;
391 return 0;
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)
406 int address = 0;
407 int tupleLength;
408 int tupleType;
409 u8 tuple[257];
410 char *dvb_str;
411 int rasz;
412 int status;
413 int got_cftableentry = 0;
414 int end_chain = 0;
415 int i;
416 u16 manfid = 0;
417 u16 devid = 0;
420 // CISTPL_DEVICE_0A
421 if ((status =
422 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
423 return status;
424 if (tupleType != 0x1D)
425 return -EINVAL;
429 // CISTPL_DEVICE_0C
430 if ((status =
431 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
432 return status;
433 if (tupleType != 0x1C)
434 return -EINVAL;
438 // CISTPL_VERS_1
439 if ((status =
440 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
441 return status;
442 if (tupleType != 0x15)
443 return -EINVAL;
447 // CISTPL_MANFID
448 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
449 &tupleLength, tuple)) < 0)
450 return status;
451 if (tupleType != 0x20)
452 return -EINVAL;
453 if (tupleLength != 4)
454 return -EINVAL;
455 manfid = (tuple[1] << 8) | tuple[0];
456 devid = (tuple[3] << 8) | tuple[2];
460 // CISTPL_CONFIG
461 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
462 &tupleLength, tuple)) < 0)
463 return status;
464 if (tupleType != 0x1A)
465 return -EINVAL;
466 if (tupleLength < 3)
467 return -EINVAL;
469 /* extract the configbase */
470 rasz = tuple[0] & 3;
471 if (tupleLength < (3 + rasz + 14))
472 return -EINVAL;
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);
480 if (dvb_str == NULL)
481 return -EINVAL;
482 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
483 return -EINVAL;
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]);
489 return -EINVAL;
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)
496 return status;
497 switch (tupleType) {
498 case 0x1B: // CISTPL_CFTABLE_ENTRY
499 if (tupleLength < (2 + 11 + 17))
500 break;
502 /* if we've already parsed one, just use it */
503 if (got_cftableentry)
504 break;
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))
512 break;
514 got_cftableentry = 1;
515 break;
517 case 0x14: // CISTPL_NO_LINK
518 break;
520 case 0xFF: // CISTPL_END
521 end_chain = 1;
522 break;
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,
526 tupleLength);
527 break;
531 if ((address > 0x1000) || (!got_cftableentry))
532 return -EINVAL;
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);
537 // success!
538 return 0;
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)
550 int configoption;
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);
559 /* check it */
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);
564 /* fine! */
565 return 0;
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)
585 int bytes_read;
586 int status;
587 u8 buf[HOST_LINK_BUF_SIZE];
588 int i;
590 dprintk("%s\n", __FUNCTION__);
592 /* check if we have space for a link buf in the rx_buffer */
593 if (ebuf == NULL) {
594 int buf_free;
596 if (ca->slot_info[slot].rx_buffer.data == NULL) {
597 status = -EIO;
598 goto exit;
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)) {
603 status = -EAGAIN;
604 goto exit;
608 /* check if there is data available */
609 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
610 goto exit;
611 if (!(status & STATUSREG_DA)) {
612 /* no data */
613 status = 0;
614 goto exit;
617 /* read the amount of data */
618 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
619 goto exit;
620 bytes_read = status << 8;
621 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
622 goto exit;
623 bytes_read |= status;
625 /* check it will fit */
626 if (ebuf == NULL) {
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;
631 status = -EIO;
632 goto exit;
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;
638 status = -EIO;
639 goto exit;
641 } else {
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);
645 status = -EIO;
646 goto exit;
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)
654 goto exit;
656 /* OK, store it in the buffer */
657 buf[i] = status;
660 /* check for read error (RE should now be 0) */
661 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
662 goto exit;
663 if (status & STATUSREG_RE) {
664 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
665 status = -EIO;
666 goto exit;
669 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
670 if (ebuf == NULL) {
671 if (ca->slot_info[slot].rx_buffer.data == NULL) {
672 status = -EIO;
673 goto exit;
675 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
676 } else {
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);
687 status = bytes_read;
689 exit:
690 return status;
695 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
696 * to a CAM.
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
701 * be written.
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)
708 int status;
709 int i;
711 dprintk("%s\n", __FUNCTION__);
714 // sanity check
715 if (bytes_write > ca->slot_info[slot].link_buf_size)
716 return -EINVAL;
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)
720 goto exitnowrite;
721 if (status & (STATUSREG_DA | STATUSREG_RE)) {
722 status = -EAGAIN;
723 goto exitnowrite;
726 /* OK, set HC bit */
727 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
728 IRQEN | CMDREG_HC)) != 0)
729 goto exit;
731 /* check if interface is still free */
732 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
733 goto exit;
734 if (!(status & STATUSREG_FR)) {
735 /* it wasn't free => try again later */
736 status = -EAGAIN;
737 goto exit;
740 /* send the amount of data */
741 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
742 goto exit;
743 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
744 bytes_write & 0xff)) != 0)
745 goto exit;
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)
750 goto exit;
753 /* check for write error (WE should now be 0) */
754 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
755 goto exit;
756 if (status & STATUSREG_WE) {
757 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
758 status = -EIO;
759 goto exit;
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);
766 exit:
767 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
769 exitnowrite:
770 return status;
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);
799 /* success */
800 return 0;
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:
821 break;
823 default:
824 return;
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;
862 int flags;
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;
873 break;
875 case DVB_CA_SLOTSTATE_RUNNING:
876 if (ca->open)
877 dvb_ca_en50221_thread_wakeup(ca);
878 break;
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__);
897 ca->wakeup = 1;
898 mb();
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)
909 int delay;
910 int curdelay = 100000000;
911 int slot;
913 for (slot = 0; slot < ca->slot_count; slot++) {
914 switch (ca->slot_info[slot].slot_state) {
915 default:
916 case DVB_CA_SLOTSTATE_NONE:
917 case DVB_CA_SLOTSTATE_INVALID:
918 delay = HZ * 60;
919 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
920 delay = HZ / 10;
922 break;
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:
929 delay = HZ / 10;
930 break;
932 case DVB_CA_SLOTSTATE_RUNNING:
933 delay = HZ * 60;
934 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
935 delay = HZ / 10;
937 if (ca->open) {
938 if ((!ca->slot_info[slot].da_irq_supported) ||
939 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA))) {
940 delay = HZ / 10;
943 break;
946 if (delay < curdelay)
947 curdelay = delay;
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;
961 int slot;
962 int flags;
963 int status;
964 int pktcount;
965 void *rxbuf;
967 dprintk("%s\n", __FUNCTION__);
969 /* choose the correct initial delay */
970 dvb_ca_en50221_thread_update_delay(ca);
972 /* main loop */
973 while (!kthread_should_stop()) {
974 /* sleep for a bit */
975 if (!ca->wakeup) {
976 set_current_state(TASK_INTERRUPTIBLE);
977 schedule_timeout(ca->delay);
978 if (kthread_should_stop())
979 return 0;
981 ca->wakeup = 0;
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:
1006 // no action needed
1007 break;
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);
1013 break;
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);
1021 break;
1023 // no other action needed; will automatically change state when ready
1024 break;
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);
1035 break;
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);
1043 break;
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);
1050 break;
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);
1058 break;
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;
1064 ca->wakeup = 1;
1065 break;
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);
1073 break;
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;
1079 ca->wakeup = 1;
1081 break;
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);
1092 break;
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);
1099 break;
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);
1108 break;
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);
1117 break;
1119 case DVB_CA_SLOTSTATE_RUNNING:
1120 if (!ca->open)
1121 continue;
1123 // poll slots for data
1124 pktcount = 0;
1125 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1126 if (!ca->open)
1127 break;
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
1132 ca->wakeup = 1;
1133 break;
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
1139 ca->wakeup = 1;
1140 break;
1143 break;
1148 return 0;
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;
1172 int err = 0;
1173 int slot;
1175 dprintk("%s\n", __FUNCTION__);
1177 switch (cmd) {
1178 case CA_RESET:
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,
1184 slot,
1185 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1188 ca->next_read_slot = 0;
1189 dvb_ca_en50221_thread_wakeup(ca);
1190 break;
1192 case CA_GET_CAP: {
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;
1199 break;
1202 case CA_GET_SLOT_INFO: {
1203 struct ca_slot_info *info = parg;
1205 if ((info->num > ca->slot_count) || (info->num < 0))
1206 return -EINVAL;
1208 info->type = CA_CI_LINK;
1209 info->flags = 0;
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;
1217 break;
1220 default:
1221 err = -EINVAL;
1222 break;
1225 return err;
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;
1262 int status;
1263 u8 fragbuf[HOST_LINK_BUF_SIZE];
1264 int fragpos = 0;
1265 int fraglen;
1266 unsigned long timeout;
1267 int written;
1269 dprintk("%s\n", __FUNCTION__);
1271 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1272 if (count < 2)
1273 return -EINVAL;
1275 /* extract slot & connection id */
1276 if (copy_from_user(&slot, buf, 1))
1277 return -EFAULT;
1278 if (copy_from_user(&connection_id, buf + 1, 1))
1279 return -EFAULT;
1280 buf += 2;
1281 count -= 2;
1283 /* check if the slot is actually running */
1284 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1285 return -EINVAL;
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)
1296 goto exit;
1298 timeout = jiffies + HZ / 2;
1299 written = 0;
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) {
1303 status = -EIO;
1304 goto exit;
1307 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
1308 if (status == (fraglen + 2)) {
1309 written = 1;
1310 break;
1312 if (status != -EAGAIN)
1313 goto exit;
1315 msleep(1);
1317 if (!written) {
1318 status = -EIO;
1319 goto exit;
1322 fragpos += fraglen;
1324 status = count + 2;
1326 exit:
1327 return status;
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)
1337 int slot;
1338 int slot_count = 0;
1339 int idx;
1340 size_t fraglen;
1341 int connection_id = -1;
1342 int found = 0;
1343 u8 hdr[2];
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)
1348 goto nextslot;
1350 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1351 return 0;
1354 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1355 while (idx != -1) {
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)) {
1360 *_slot = slot;
1361 found = 1;
1362 break;
1365 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1368 nextslot:
1369 slot = (slot + 1) % ca->slot_count;
1370 slot_count++;
1373 ca->next_read_slot = slot;
1374 return found;
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;
1393 int status;
1394 int result = 0;
1395 u8 hdr[2];
1396 int slot;
1397 int connection_id = -1;
1398 size_t idx, idx2;
1399 int last_fragment = 0;
1400 size_t fraglen;
1401 int pktlen;
1402 int dispose = 0;
1404 dprintk("%s\n", __FUNCTION__);
1406 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1407 if (count < 2)
1408 return -EINVAL;
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)) {
1423 if (result)
1424 return result;
1425 return status;
1428 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1429 pktlen = 2;
1430 do {
1431 if (idx == -1) {
1432 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
1433 status = -EIO;
1434 goto exit;
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;
1444 } else {
1445 fraglen -= 2;
1448 if ((status = dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 2,
1449 (u8 *)buf + pktlen, fraglen, 1)) < 0) {
1450 goto exit;
1452 pktlen += fraglen;
1455 if ((hdr[1] & 0x80) == 0)
1456 last_fragment = 1;
1457 dispose = 1;
1460 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1461 if (dispose)
1462 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
1463 idx = idx2;
1464 dispose = 0;
1465 } while (!last_fragment);
1467 hdr[0] = slot;
1468 hdr[1] = connection_id;
1469 if ((status = copy_to_user(buf, hdr, 2)) != 0)
1470 goto exit;
1471 status = pktlen;
1473 exit:
1474 return status;
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;
1490 int err;
1491 int i;
1493 dprintk("%s\n", __FUNCTION__);
1495 if (!try_module_get(ca->pub->owner))
1496 return -EIO;
1498 err = dvb_generic_open(inode, file);
1499 if (err < 0) {
1500 module_put(ca->pub->owner);
1501 return err;
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);
1515 ca->open = 1;
1516 dvb_ca_en50221_thread_update_delay(ca);
1517 dvb_ca_en50221_thread_wakeup(ca);
1519 return 0;
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;
1535 int err;
1537 dprintk("%s\n", __FUNCTION__);
1539 /* mark the CA device as closed */
1540 ca->open = 0;
1541 dvb_ca_en50221_thread_update_delay(ca);
1543 err = dvb_generic_release(inode, file);
1545 module_put(ca->pub->owner);
1547 return err;
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;
1564 int slot;
1565 int result = 0;
1567 dprintk("%s\n", __FUNCTION__);
1569 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1570 mask |= POLLIN;
1573 /* if there is something, return now */
1574 if (mask)
1575 return mask;
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) {
1581 mask |= POLLIN;
1584 return mask;
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 = {
1600 .priv = NULL,
1601 .users = 1,
1602 .readers = 1,
1603 .writers = 1,
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)
1625 int ret;
1626 struct dvb_ca_private *ca = NULL;
1627 int i;
1629 dprintk("%s\n", __FUNCTION__);
1631 if (slot_count < 1)
1632 return -EINVAL;
1634 /* initialise the system data */
1635 if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
1636 ret = -ENOMEM;
1637 goto error;
1639 ca->pub = pubca;
1640 ca->flags = flags;
1641 ca->slot_count = slot_count;
1642 if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
1643 ret = -ENOMEM;
1644 goto error;
1646 init_waitqueue_head(&ca->wait_queue);
1647 ca->open = 0;
1648 ca->wakeup = 0;
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);
1654 if (ret)
1655 goto error;
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)) {
1666 ret = -EINTR;
1667 goto error;
1669 mb();
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",
1677 ret);
1678 goto error;
1680 return 0;
1682 error:
1683 if (ca != NULL) {
1684 if (ca->dvbdev != NULL)
1685 dvb_unregister_device(ca->dvbdev);
1686 kfree(ca->slot_info);
1687 kfree(ca);
1689 pubca->private = NULL;
1690 return ret;
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;
1705 int i;
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);
1718 kfree(ca);
1719 pubca->private = NULL;