GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / media / dvb / dvb-core / dvb_ca_en50221.c
blobcb97e6b8543295971d30f8b21c233791a725f26e
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/smp_lock.h>
40 #include <linux/kthread.h>
42 #include "dvb_ca_en50221.h"
43 #include "dvb_ringbuffer.h"
45 static int dvb_ca_en50221_debug;
47 module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
48 MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
50 #define dprintk if (dvb_ca_en50221_debug) printk
52 #define INIT_TIMEOUT_SECS 10
54 #define HOST_LINK_BUF_SIZE 0x200
56 #define RX_BUFFER_SIZE 65535
58 #define MAX_RX_PACKETS_PER_ITERATION 10
60 #define CTRLIF_DATA 0
61 #define CTRLIF_COMMAND 1
62 #define CTRLIF_STATUS 1
63 #define CTRLIF_SIZE_LOW 2
64 #define CTRLIF_SIZE_HIGH 3
66 #define CMDREG_HC 1 /* Host control */
67 #define CMDREG_SW 2 /* Size write */
68 #define CMDREG_SR 4 /* Size read */
69 #define CMDREG_RS 8 /* Reset interface */
70 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
71 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
72 #define IRQEN (CMDREG_DAIE)
74 #define STATUSREG_RE 1 /* read error */
75 #define STATUSREG_WE 2 /* write error */
76 #define STATUSREG_FR 0x40 /* module free */
77 #define STATUSREG_DA 0x80 /* data available */
78 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
81 #define DVB_CA_SLOTSTATE_NONE 0
82 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
83 #define DVB_CA_SLOTSTATE_RUNNING 2
84 #define DVB_CA_SLOTSTATE_INVALID 3
85 #define DVB_CA_SLOTSTATE_WAITREADY 4
86 #define DVB_CA_SLOTSTATE_VALIDATE 5
87 #define DVB_CA_SLOTSTATE_WAITFR 6
88 #define DVB_CA_SLOTSTATE_LINKINIT 7
91 /* Information on a CA slot */
92 struct dvb_ca_slot {
94 /* current state of the CAM */
95 int slot_state;
97 /* mutex used for serializing access to one CI slot */
98 struct mutex slot_lock;
100 /* Number of CAMCHANGES that have occurred since last processing */
101 atomic_t camchange_count;
103 /* Type of last CAMCHANGE */
104 int camchange_type;
106 /* base address of CAM config */
107 u32 config_base;
109 /* value to write into Config Control register */
110 u8 config_option;
112 /* if 1, the CAM supports DA IRQs */
113 u8 da_irq_supported:1;
115 /* size of the buffer to use when talking to the CAM */
116 int link_buf_size;
118 /* buffer for incoming packets */
119 struct dvb_ringbuffer rx_buffer;
121 /* timer used during various states of the slot */
122 unsigned long timeout;
125 /* Private CA-interface information */
126 struct dvb_ca_private {
128 /* pointer back to the public data structure */
129 struct dvb_ca_en50221 *pub;
131 /* the DVB device */
132 struct dvb_device *dvbdev;
134 /* Flags describing the interface (DVB_CA_FLAG_*) */
135 u32 flags;
137 /* number of slots supported by this CA interface */
138 unsigned int slot_count;
140 /* information on each slot */
141 struct dvb_ca_slot *slot_info;
143 /* wait queues for read() and write() operations */
144 wait_queue_head_t wait_queue;
146 /* PID of the monitoring thread */
147 struct task_struct *thread;
149 /* Flag indicating if the CA device is open */
150 unsigned int open:1;
152 /* Flag indicating the thread should wake up now */
153 unsigned int wakeup:1;
155 /* Delay the main thread should use */
156 unsigned long delay;
158 /* Slot to start looking for data to read from in the next user-space read operation */
159 int next_read_slot;
162 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
163 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
164 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
168 * Safely find needle in haystack.
170 * @param haystack Buffer to look in.
171 * @param hlen Number of bytes in haystack.
172 * @param needle Buffer to find.
173 * @param nlen Number of bytes in needle.
174 * @return Pointer into haystack needle was found at, or NULL if not found.
176 static char *findstr(char * haystack, int hlen, char * needle, int nlen)
178 int i;
180 if (hlen < nlen)
181 return NULL;
183 for (i = 0; i <= hlen - nlen; i++) {
184 if (!strncmp(haystack + i, needle, nlen))
185 return haystack + i;
188 return NULL;
193 /* ******************************************************************************** */
194 /* EN50221 physical interface functions */
198 * Check CAM status.
200 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
202 int slot_status;
203 int cam_present_now;
204 int cam_changed;
206 /* IRQ mode */
207 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
208 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
211 /* poll mode */
212 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
214 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
215 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
216 if (!cam_changed) {
217 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
218 cam_changed = (cam_present_now != cam_present_old);
221 if (cam_changed) {
222 if (!cam_present_now) {
223 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
224 } else {
225 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
227 atomic_set(&ca->slot_info[slot].camchange_count, 1);
228 } else {
229 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
230 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
231 // move to validate state if reset is completed
232 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
236 return cam_changed;
241 * Wait for flags to become set on the STATUS register on a CAM interface,
242 * checking for errors and timeout.
244 * @param ca CA instance.
245 * @param slot Slot on interface.
246 * @param waitfor Flags to wait for.
247 * @param timeout_ms Timeout in milliseconds.
249 * @return 0 on success, nonzero on error.
251 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
252 u8 waitfor, int timeout_hz)
254 unsigned long timeout;
255 unsigned long start;
257 dprintk("%s\n", __func__);
259 /* loop until timeout elapsed */
260 start = jiffies;
261 timeout = jiffies + timeout_hz;
262 while (1) {
263 /* read the status and check for error */
264 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
265 if (res < 0)
266 return -EIO;
268 /* if we got the flags, it was successful! */
269 if (res & waitfor) {
270 dprintk("%s succeeded timeout:%lu\n", __func__, jiffies - start);
271 return 0;
274 /* check for timeout */
275 if (time_after(jiffies, timeout)) {
276 break;
279 /* wait for a bit */
280 msleep(1);
283 dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
285 /* if we get here, we've timed out */
286 return -ETIMEDOUT;
291 * Initialise the link layer connection to a CAM.
293 * @param ca CA instance.
294 * @param slot Slot id.
296 * @return 0 on success, nonzero on failure.
298 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
300 int ret;
301 int buf_size;
302 u8 buf[2];
304 dprintk("%s\n", __func__);
306 /* we'll be determining these during this function */
307 ca->slot_info[slot].da_irq_supported = 0;
309 /* set the host link buffer size temporarily. it will be overwritten with the
310 * real negotiated size later. */
311 ca->slot_info[slot].link_buf_size = 2;
313 /* read the buffer size from the CAM */
314 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
315 return ret;
316 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
317 return ret;
318 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
319 return -EIO;
320 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
321 return ret;
323 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
324 buf_size = (buf[0] << 8) | buf[1];
325 if (buf_size > HOST_LINK_BUF_SIZE)
326 buf_size = HOST_LINK_BUF_SIZE;
327 ca->slot_info[slot].link_buf_size = buf_size;
328 buf[0] = buf_size >> 8;
329 buf[1] = buf_size & 0xff;
330 dprintk("Chosen link buffer size of %i\n", buf_size);
332 /* write the buffer size to the CAM */
333 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
334 return ret;
335 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
336 return ret;
337 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
338 return -EIO;
339 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
340 return ret;
342 /* success */
343 return 0;
347 * Read a tuple from attribute memory.
349 * @param ca CA instance.
350 * @param slot Slot id.
351 * @param address Address to read from. Updated.
352 * @param tupleType Tuple id byte. Updated.
353 * @param tupleLength Tuple length. Updated.
354 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
356 * @return 0 on success, nonzero on error.
358 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
359 int *address, int *tupleType, int *tupleLength, u8 * tuple)
361 int i;
362 int _tupleType;
363 int _tupleLength;
364 int _address = *address;
366 /* grab the next tuple length and type */
367 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
368 return _tupleType;
369 if (_tupleType == 0xff) {
370 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
371 *address += 2;
372 *tupleType = _tupleType;
373 *tupleLength = 0;
374 return 0;
376 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
377 return _tupleLength;
378 _address += 4;
380 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
382 /* read in the whole tuple */
383 for (i = 0; i < _tupleLength; i++) {
384 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2));
385 dprintk(" 0x%02x: 0x%02x %c\n",
386 i, tuple[i] & 0xff,
387 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
389 _address += (_tupleLength * 2);
391 // success
392 *tupleType = _tupleType;
393 *tupleLength = _tupleLength;
394 *address = _address;
395 return 0;
400 * Parse attribute memory of a CAM module, extracting Config register, and checking
401 * it is a DVB CAM module.
403 * @param ca CA instance.
404 * @param slot Slot id.
406 * @return 0 on success, <0 on failure.
408 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
410 int address = 0;
411 int tupleLength;
412 int tupleType;
413 u8 tuple[257];
414 char *dvb_str;
415 int rasz;
416 int status;
417 int got_cftableentry = 0;
418 int end_chain = 0;
419 int i;
420 u16 manfid = 0;
421 u16 devid = 0;
424 // CISTPL_DEVICE_0A
425 if ((status =
426 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
427 return status;
428 if (tupleType != 0x1D)
429 return -EINVAL;
433 // CISTPL_DEVICE_0C
434 if ((status =
435 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
436 return status;
437 if (tupleType != 0x1C)
438 return -EINVAL;
442 // CISTPL_VERS_1
443 if ((status =
444 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
445 return status;
446 if (tupleType != 0x15)
447 return -EINVAL;
451 // CISTPL_MANFID
452 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
453 &tupleLength, tuple)) < 0)
454 return status;
455 if (tupleType != 0x20)
456 return -EINVAL;
457 if (tupleLength != 4)
458 return -EINVAL;
459 manfid = (tuple[1] << 8) | tuple[0];
460 devid = (tuple[3] << 8) | tuple[2];
464 // CISTPL_CONFIG
465 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
466 &tupleLength, tuple)) < 0)
467 return status;
468 if (tupleType != 0x1A)
469 return -EINVAL;
470 if (tupleLength < 3)
471 return -EINVAL;
473 /* extract the configbase */
474 rasz = tuple[0] & 3;
475 if (tupleLength < (3 + rasz + 14))
476 return -EINVAL;
477 ca->slot_info[slot].config_base = 0;
478 for (i = 0; i < rasz + 1; i++) {
479 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i));
482 /* check it contains the correct DVB string */
483 dvb_str = findstr((char *)tuple, tupleLength, "DVB_CI_V", 8);
484 if (dvb_str == NULL)
485 return -EINVAL;
486 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
487 return -EINVAL;
489 /* is it a version we support? */
490 if (strncmp(dvb_str + 8, "1.00", 4)) {
491 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
492 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]);
493 return -EINVAL;
496 /* process the CFTABLE_ENTRY tuples, and any after those */
497 while ((!end_chain) && (address < 0x1000)) {
498 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
499 &tupleLength, tuple)) < 0)
500 return status;
501 switch (tupleType) {
502 case 0x1B: // CISTPL_CFTABLE_ENTRY
503 if (tupleLength < (2 + 11 + 17))
504 break;
506 /* if we've already parsed one, just use it */
507 if (got_cftableentry)
508 break;
510 /* get the config option */
511 ca->slot_info[slot].config_option = tuple[0] & 0x3f;
513 /* OK, check it contains the correct strings */
514 if ((findstr((char *)tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
515 (findstr((char *)tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
516 break;
518 got_cftableentry = 1;
519 break;
521 case 0x14: // CISTPL_NO_LINK
522 break;
524 case 0xFF: // CISTPL_END
525 end_chain = 1;
526 break;
528 default: /* Unknown tuple type - just skip this tuple and move to the next one */
529 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType,
530 tupleLength);
531 break;
535 if ((address > 0x1000) || (!got_cftableentry))
536 return -EINVAL;
538 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
539 manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option);
541 // success!
542 return 0;
547 * Set CAM's configoption correctly.
549 * @param ca CA instance.
550 * @param slot Slot containing the CAM.
552 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
554 int configoption;
556 dprintk("%s\n", __func__);
558 /* set the config option */
559 ca->pub->write_attribute_mem(ca->pub, slot,
560 ca->slot_info[slot].config_base,
561 ca->slot_info[slot].config_option);
563 /* check it */
564 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
565 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
566 ca->slot_info[slot].config_option, configoption & 0x3f);
568 /* fine! */
569 return 0;
575 * This function talks to an EN50221 CAM control interface. It reads a buffer of
576 * data from the CAM. The data can either be stored in a supplied buffer, or
577 * automatically be added to the slot's rx_buffer.
579 * @param ca CA instance.
580 * @param slot Slot to read from.
581 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
582 * the data will be added into the buffering system as a normal fragment.
583 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
585 * @return Number of bytes read, or < 0 on error
587 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount)
589 int bytes_read;
590 int status;
591 u8 buf[HOST_LINK_BUF_SIZE];
592 int i;
594 dprintk("%s\n", __func__);
596 /* check if we have space for a link buf in the rx_buffer */
597 if (ebuf == NULL) {
598 int buf_free;
600 if (ca->slot_info[slot].rx_buffer.data == NULL) {
601 status = -EIO;
602 goto exit;
604 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
606 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
607 status = -EAGAIN;
608 goto exit;
612 /* check if there is data available */
613 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
614 goto exit;
615 if (!(status & STATUSREG_DA)) {
616 /* no data */
617 status = 0;
618 goto exit;
621 /* read the amount of data */
622 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
623 goto exit;
624 bytes_read = status << 8;
625 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
626 goto exit;
627 bytes_read |= status;
629 /* check it will fit */
630 if (ebuf == NULL) {
631 if (bytes_read > ca->slot_info[slot].link_buf_size) {
632 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
633 ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size);
634 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
635 status = -EIO;
636 goto exit;
638 if (bytes_read < 2) {
639 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
640 ca->dvbdev->adapter->num);
641 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
642 status = -EIO;
643 goto exit;
645 } else {
646 if (bytes_read > ecount) {
647 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
648 ca->dvbdev->adapter->num);
649 status = -EIO;
650 goto exit;
654 /* fill the buffer */
655 for (i = 0; i < bytes_read; i++) {
656 /* read byte and check */
657 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
658 goto exit;
660 /* OK, store it in the buffer */
661 buf[i] = status;
664 /* check for read error (RE should now be 0) */
665 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
666 goto exit;
667 if (status & STATUSREG_RE) {
668 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
669 status = -EIO;
670 goto exit;
673 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
674 if (ebuf == NULL) {
675 if (ca->slot_info[slot].rx_buffer.data == NULL) {
676 status = -EIO;
677 goto exit;
679 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
680 } else {
681 memcpy(ebuf, buf, bytes_read);
684 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
685 buf[0], (buf[1] & 0x80) == 0, bytes_read);
687 /* wake up readers when a last_fragment is received */
688 if ((buf[1] & 0x80) == 0x00) {
689 wake_up_interruptible(&ca->wait_queue);
691 status = bytes_read;
693 exit:
694 return status;
699 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
700 * to a CAM.
702 * @param ca CA instance.
703 * @param slot Slot to write to.
704 * @param ebuf The data in this buffer is treated as a complete link-level packet to
705 * be written.
706 * @param count Size of ebuf.
708 * @return Number of bytes written, or < 0 on error.
710 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write)
712 int status;
713 int i;
715 dprintk("%s\n", __func__);
718 /* sanity check */
719 if (bytes_write > ca->slot_info[slot].link_buf_size)
720 return -EINVAL;
722 /* it is possible we are dealing with a single buffer implementation,
723 thus if there is data available for read or if there is even a read
724 already in progress, we do nothing but awake the kernel thread to
725 process the data if necessary. */
726 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
727 goto exitnowrite;
728 if (status & (STATUSREG_DA | STATUSREG_RE)) {
729 if (status & STATUSREG_DA)
730 dvb_ca_en50221_thread_wakeup(ca);
732 status = -EAGAIN;
733 goto exitnowrite;
736 /* OK, set HC bit */
737 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
738 IRQEN | CMDREG_HC)) != 0)
739 goto exit;
741 /* check if interface is still free */
742 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
743 goto exit;
744 if (!(status & STATUSREG_FR)) {
745 /* it wasn't free => try again later */
746 status = -EAGAIN;
747 goto exit;
750 /* send the amount of data */
751 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
752 goto exit;
753 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
754 bytes_write & 0xff)) != 0)
755 goto exit;
757 /* send the buffer */
758 for (i = 0; i < bytes_write; i++) {
759 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
760 goto exit;
763 /* check for write error (WE should now be 0) */
764 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
765 goto exit;
766 if (status & STATUSREG_WE) {
767 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
768 status = -EIO;
769 goto exit;
771 status = bytes_write;
773 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
774 buf[0], (buf[1] & 0x80) == 0, bytes_write);
776 exit:
777 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
779 exitnowrite:
780 return status;
782 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
786 /* ******************************************************************************** */
787 /* EN50221 higher level functions */
791 * A CAM has been removed => shut it down.
793 * @param ca CA instance.
794 * @param slot Slot to shut down.
796 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
798 dprintk("%s\n", __func__);
800 ca->pub->slot_shutdown(ca->pub, slot);
801 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
803 /* need to wake up all processes to check if they're now
804 trying to write to a defunct CAM */
805 wake_up_interruptible(&ca->wait_queue);
807 dprintk("Slot %i shutdown\n", slot);
809 /* success */
810 return 0;
812 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
816 * A CAMCHANGE IRQ has occurred.
818 * @param ca CA instance.
819 * @param slot Slot concerned.
820 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
822 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
824 struct dvb_ca_private *ca = pubca->private;
826 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
828 switch (change_type) {
829 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
830 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
831 break;
833 default:
834 return;
837 ca->slot_info[slot].camchange_type = change_type;
838 atomic_inc(&ca->slot_info[slot].camchange_count);
839 dvb_ca_en50221_thread_wakeup(ca);
841 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
845 * A CAMREADY IRQ has occurred.
847 * @param ca CA instance.
848 * @param slot Slot concerned.
850 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
852 struct dvb_ca_private *ca = pubca->private;
854 dprintk("CAMREADY IRQ slot:%i\n", slot);
856 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
857 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
858 dvb_ca_en50221_thread_wakeup(ca);
864 * An FR or DA IRQ has occurred.
866 * @param ca CA instance.
867 * @param slot Slot concerned.
869 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
871 struct dvb_ca_private *ca = pubca->private;
872 int flags;
874 dprintk("FR/DA IRQ slot:%i\n", slot);
876 switch (ca->slot_info[slot].slot_state) {
877 case DVB_CA_SLOTSTATE_LINKINIT:
878 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
879 if (flags & STATUSREG_DA) {
880 dprintk("CAM supports DA IRQ\n");
881 ca->slot_info[slot].da_irq_supported = 1;
883 break;
885 case DVB_CA_SLOTSTATE_RUNNING:
886 if (ca->open)
887 dvb_ca_en50221_thread_wakeup(ca);
888 break;
894 /* ******************************************************************************** */
895 /* EN50221 thread functions */
898 * Wake up the DVB CA thread
900 * @param ca CA instance.
902 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
905 dprintk("%s\n", __func__);
907 ca->wakeup = 1;
908 mb();
909 wake_up_process(ca->thread);
913 * Update the delay used by the thread.
915 * @param ca CA instance.
917 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
919 int delay;
920 int curdelay = 100000000;
921 int slot;
923 /* Beware of too high polling frequency, because one polling
924 * call might take several hundred milliseconds until timeout!
926 for (slot = 0; slot < ca->slot_count; slot++) {
927 switch (ca->slot_info[slot].slot_state) {
928 default:
929 case DVB_CA_SLOTSTATE_NONE:
930 delay = HZ * 60; /* 60s */
931 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
932 delay = HZ * 5; /* 5s */
933 break;
934 case DVB_CA_SLOTSTATE_INVALID:
935 delay = HZ * 60; /* 60s */
936 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
937 delay = HZ / 10; /* 100ms */
938 break;
940 case DVB_CA_SLOTSTATE_UNINITIALISED:
941 case DVB_CA_SLOTSTATE_WAITREADY:
942 case DVB_CA_SLOTSTATE_VALIDATE:
943 case DVB_CA_SLOTSTATE_WAITFR:
944 case DVB_CA_SLOTSTATE_LINKINIT:
945 delay = HZ / 10; /* 100ms */
946 break;
948 case DVB_CA_SLOTSTATE_RUNNING:
949 delay = HZ * 60; /* 60s */
950 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
951 delay = HZ / 10; /* 100ms */
952 if (ca->open) {
953 if ((!ca->slot_info[slot].da_irq_supported) ||
954 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
955 delay = HZ / 10; /* 100ms */
957 break;
960 if (delay < curdelay)
961 curdelay = delay;
964 ca->delay = curdelay;
970 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
972 static int dvb_ca_en50221_thread(void *data)
974 struct dvb_ca_private *ca = data;
975 int slot;
976 int flags;
977 int status;
978 int pktcount;
979 void *rxbuf;
981 dprintk("%s\n", __func__);
983 /* choose the correct initial delay */
984 dvb_ca_en50221_thread_update_delay(ca);
986 /* main loop */
987 while (!kthread_should_stop()) {
988 /* sleep for a bit */
989 if (!ca->wakeup) {
990 set_current_state(TASK_INTERRUPTIBLE);
991 schedule_timeout(ca->delay);
992 if (kthread_should_stop())
993 return 0;
995 ca->wakeup = 0;
997 /* go through all the slots processing them */
998 for (slot = 0; slot < ca->slot_count; slot++) {
1000 mutex_lock(&ca->slot_info[slot].slot_lock);
1002 // check the cam status + deal with CAMCHANGEs
1003 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1004 /* clear down an old CI slot if necessary */
1005 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
1006 dvb_ca_en50221_slot_shutdown(ca, slot);
1008 /* if a CAM is NOW present, initialise it */
1009 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
1010 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1013 /* we've handled one CAMCHANGE */
1014 dvb_ca_en50221_thread_update_delay(ca);
1015 atomic_dec(&ca->slot_info[slot].camchange_count);
1018 // CAM state machine
1019 switch (ca->slot_info[slot].slot_state) {
1020 case DVB_CA_SLOTSTATE_NONE:
1021 case DVB_CA_SLOTSTATE_INVALID:
1022 // no action needed
1023 break;
1025 case DVB_CA_SLOTSTATE_UNINITIALISED:
1026 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1027 ca->pub->slot_reset(ca->pub, slot);
1028 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1029 break;
1031 case DVB_CA_SLOTSTATE_WAITREADY:
1032 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1033 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1034 ca->dvbdev->adapter->num);
1035 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1036 dvb_ca_en50221_thread_update_delay(ca);
1037 break;
1039 // no other action needed; will automatically change state when ready
1040 break;
1042 case DVB_CA_SLOTSTATE_VALIDATE:
1043 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1044 /* we need this extra check for annoying interfaces like the budget-av */
1045 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1046 (ca->pub->poll_slot_status)) {
1047 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1048 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1049 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1050 dvb_ca_en50221_thread_update_delay(ca);
1051 break;
1055 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1056 ca->dvbdev->adapter->num);
1057 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1058 dvb_ca_en50221_thread_update_delay(ca);
1059 break;
1061 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1062 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1063 ca->dvbdev->adapter->num);
1064 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1065 dvb_ca_en50221_thread_update_delay(ca);
1066 break;
1068 if (ca->pub->write_cam_control(ca->pub, slot,
1069 CTRLIF_COMMAND, CMDREG_RS) != 0) {
1070 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1071 ca->dvbdev->adapter->num);
1072 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1073 dvb_ca_en50221_thread_update_delay(ca);
1074 break;
1076 dprintk("DVB CAM validated successfully\n");
1078 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1079 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
1080 ca->wakeup = 1;
1081 break;
1083 case DVB_CA_SLOTSTATE_WAITFR:
1084 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1085 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1086 ca->dvbdev->adapter->num);
1087 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1088 dvb_ca_en50221_thread_update_delay(ca);
1089 break;
1092 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1093 if (flags & STATUSREG_FR) {
1094 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1095 ca->wakeup = 1;
1097 break;
1099 case DVB_CA_SLOTSTATE_LINKINIT:
1100 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1101 /* we need this extra check for annoying interfaces like the budget-av */
1102 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1103 (ca->pub->poll_slot_status)) {
1104 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1105 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1106 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1107 dvb_ca_en50221_thread_update_delay(ca);
1108 break;
1112 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
1113 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1114 dvb_ca_en50221_thread_update_delay(ca);
1115 break;
1118 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1119 rxbuf = vmalloc(RX_BUFFER_SIZE);
1120 if (rxbuf == NULL) {
1121 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
1122 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1123 dvb_ca_en50221_thread_update_delay(ca);
1124 break;
1126 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
1129 ca->pub->slot_ts_enable(ca->pub, slot);
1130 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
1131 dvb_ca_en50221_thread_update_delay(ca);
1132 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num);
1133 break;
1135 case DVB_CA_SLOTSTATE_RUNNING:
1136 if (!ca->open)
1137 break;
1139 // poll slots for data
1140 pktcount = 0;
1141 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1142 if (!ca->open)
1143 break;
1145 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1146 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1147 // we dont want to sleep on the next iteration so we can handle the cam change
1148 ca->wakeup = 1;
1149 break;
1152 /* check if we've hit our limit this time */
1153 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1154 // dont sleep; there is likely to be more data to read
1155 ca->wakeup = 1;
1156 break;
1159 break;
1162 mutex_unlock(&ca->slot_info[slot].slot_lock);
1166 return 0;
1171 /* ******************************************************************************** */
1172 /* EN50221 IO interface functions */
1175 * Real ioctl implementation.
1176 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1178 * @param inode Inode concerned.
1179 * @param file File concerned.
1180 * @param cmd IOCTL command.
1181 * @param arg Associated argument.
1183 * @return 0 on success, <0 on error.
1185 static int dvb_ca_en50221_io_do_ioctl(struct file *file,
1186 unsigned int cmd, void *parg)
1188 struct dvb_device *dvbdev = file->private_data;
1189 struct dvb_ca_private *ca = dvbdev->priv;
1190 int err = 0;
1191 int slot;
1193 dprintk("%s\n", __func__);
1195 switch (cmd) {
1196 case CA_RESET:
1197 for (slot = 0; slot < ca->slot_count; slot++) {
1198 mutex_lock(&ca->slot_info[slot].slot_lock);
1199 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
1200 dvb_ca_en50221_slot_shutdown(ca, slot);
1201 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1202 dvb_ca_en50221_camchange_irq(ca->pub,
1203 slot,
1204 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1206 mutex_unlock(&ca->slot_info[slot].slot_lock);
1208 ca->next_read_slot = 0;
1209 dvb_ca_en50221_thread_wakeup(ca);
1210 break;
1212 case CA_GET_CAP: {
1213 struct ca_caps *caps = parg;
1215 caps->slot_num = ca->slot_count;
1216 caps->slot_type = CA_CI_LINK;
1217 caps->descr_num = 0;
1218 caps->descr_type = 0;
1219 break;
1222 case CA_GET_SLOT_INFO: {
1223 struct ca_slot_info *info = parg;
1225 if ((info->num > ca->slot_count) || (info->num < 0))
1226 return -EINVAL;
1228 info->type = CA_CI_LINK;
1229 info->flags = 0;
1230 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
1231 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1232 info->flags = CA_CI_MODULE_PRESENT;
1234 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1235 info->flags |= CA_CI_MODULE_READY;
1237 break;
1240 default:
1241 err = -EINVAL;
1242 break;
1245 return err;
1250 * Wrapper for ioctl implementation.
1252 * @param inode Inode concerned.
1253 * @param file File concerned.
1254 * @param cmd IOCTL command.
1255 * @param arg Associated argument.
1257 * @return 0 on success, <0 on error.
1259 static long dvb_ca_en50221_io_ioctl(struct file *file,
1260 unsigned int cmd, unsigned long arg)
1262 int ret;
1264 lock_kernel();
1265 ret = dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1266 unlock_kernel();
1268 return ret;
1273 * Implementation of write() syscall.
1275 * @param file File structure.
1276 * @param buf Source buffer.
1277 * @param count Size of source buffer.
1278 * @param ppos Position in file (ignored).
1280 * @return Number of bytes read, or <0 on error.
1282 static ssize_t dvb_ca_en50221_io_write(struct file *file,
1283 const char __user * buf, size_t count, loff_t * ppos)
1285 struct dvb_device *dvbdev = file->private_data;
1286 struct dvb_ca_private *ca = dvbdev->priv;
1287 u8 slot, connection_id;
1288 int status;
1289 u8 fragbuf[HOST_LINK_BUF_SIZE];
1290 int fragpos = 0;
1291 int fraglen;
1292 unsigned long timeout;
1293 int written;
1295 dprintk("%s\n", __func__);
1297 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1298 if (count < 2)
1299 return -EINVAL;
1301 /* extract slot & connection id */
1302 if (copy_from_user(&slot, buf, 1))
1303 return -EFAULT;
1304 if (copy_from_user(&connection_id, buf + 1, 1))
1305 return -EFAULT;
1306 buf += 2;
1307 count -= 2;
1309 /* check if the slot is actually running */
1310 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1311 return -EINVAL;
1313 /* fragment the packets & store in the buffer */
1314 while (fragpos < count) {
1315 fraglen = ca->slot_info[slot].link_buf_size - 2;
1316 if ((count - fragpos) < fraglen)
1317 fraglen = count - fragpos;
1319 fragbuf[0] = connection_id;
1320 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1321 status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
1322 if (status) {
1323 status = -EFAULT;
1324 goto exit;
1327 timeout = jiffies + HZ / 2;
1328 written = 0;
1329 while (!time_after(jiffies, timeout)) {
1330 /* check the CAM hasn't been removed/reset in the meantime */
1331 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1332 status = -EIO;
1333 goto exit;
1336 mutex_lock(&ca->slot_info[slot].slot_lock);
1337 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
1338 mutex_unlock(&ca->slot_info[slot].slot_lock);
1339 if (status == (fraglen + 2)) {
1340 written = 1;
1341 break;
1343 if (status != -EAGAIN)
1344 goto exit;
1346 msleep(1);
1348 if (!written) {
1349 status = -EIO;
1350 goto exit;
1353 fragpos += fraglen;
1355 status = count + 2;
1357 exit:
1358 return status;
1363 * Condition for waking up in dvb_ca_en50221_io_read_condition
1365 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
1366 int *result, int *_slot)
1368 int slot;
1369 int slot_count = 0;
1370 int idx;
1371 size_t fraglen;
1372 int connection_id = -1;
1373 int found = 0;
1374 u8 hdr[2];
1376 slot = ca->next_read_slot;
1377 while ((slot_count < ca->slot_count) && (!found)) {
1378 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1379 goto nextslot;
1381 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1382 return 0;
1385 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1386 while (idx != -1) {
1387 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1388 if (connection_id == -1)
1389 connection_id = hdr[0];
1390 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
1391 *_slot = slot;
1392 found = 1;
1393 break;
1396 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1399 nextslot:
1400 slot = (slot + 1) % ca->slot_count;
1401 slot_count++;
1404 ca->next_read_slot = slot;
1405 return found;
1410 * Implementation of read() syscall.
1412 * @param file File structure.
1413 * @param buf Destination buffer.
1414 * @param count Size of destination buffer.
1415 * @param ppos Position in file (ignored).
1417 * @return Number of bytes read, or <0 on error.
1419 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
1420 size_t count, loff_t * ppos)
1422 struct dvb_device *dvbdev = file->private_data;
1423 struct dvb_ca_private *ca = dvbdev->priv;
1424 int status;
1425 int result = 0;
1426 u8 hdr[2];
1427 int slot;
1428 int connection_id = -1;
1429 size_t idx, idx2;
1430 int last_fragment = 0;
1431 size_t fraglen;
1432 int pktlen;
1433 int dispose = 0;
1435 dprintk("%s\n", __func__);
1437 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1438 if (count < 2)
1439 return -EINVAL;
1441 /* wait for some data */
1442 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
1444 /* if we're in nonblocking mode, exit immediately */
1445 if (file->f_flags & O_NONBLOCK)
1446 return -EWOULDBLOCK;
1448 /* wait for some data */
1449 status = wait_event_interruptible(ca->wait_queue,
1450 dvb_ca_en50221_io_read_condition
1451 (ca, &result, &slot));
1453 if ((status < 0) || (result < 0)) {
1454 if (result)
1455 return result;
1456 return status;
1459 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1460 pktlen = 2;
1461 do {
1462 if (idx == -1) {
1463 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
1464 status = -EIO;
1465 goto exit;
1468 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1469 if (connection_id == -1)
1470 connection_id = hdr[0];
1471 if (hdr[0] == connection_id) {
1472 if (pktlen < count) {
1473 if ((pktlen + fraglen - 2) > count) {
1474 fraglen = count - pktlen;
1475 } else {
1476 fraglen -= 2;
1479 if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
1480 buf + pktlen, fraglen)) < 0) {
1481 goto exit;
1483 pktlen += fraglen;
1486 if ((hdr[1] & 0x80) == 0)
1487 last_fragment = 1;
1488 dispose = 1;
1491 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1492 if (dispose)
1493 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
1494 idx = idx2;
1495 dispose = 0;
1496 } while (!last_fragment);
1498 hdr[0] = slot;
1499 hdr[1] = connection_id;
1500 status = copy_to_user(buf, hdr, 2);
1501 if (status) {
1502 status = -EFAULT;
1503 goto exit;
1505 status = pktlen;
1507 exit:
1508 return status;
1513 * Implementation of file open syscall.
1515 * @param inode Inode concerned.
1516 * @param file File concerned.
1518 * @return 0 on success, <0 on failure.
1520 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1522 struct dvb_device *dvbdev = file->private_data;
1523 struct dvb_ca_private *ca = dvbdev->priv;
1524 int err;
1525 int i;
1527 dprintk("%s\n", __func__);
1529 if (!try_module_get(ca->pub->owner))
1530 return -EIO;
1532 err = dvb_generic_open(inode, file);
1533 if (err < 0) {
1534 module_put(ca->pub->owner);
1535 return err;
1538 for (i = 0; i < ca->slot_count; i++) {
1540 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1541 if (ca->slot_info[i].rx_buffer.data != NULL) {
1542 /* it is safe to call this here without locks because
1543 * ca->open == 0. Data is not read in this case */
1544 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
1549 ca->open = 1;
1550 dvb_ca_en50221_thread_update_delay(ca);
1551 dvb_ca_en50221_thread_wakeup(ca);
1553 return 0;
1558 * Implementation of file close syscall.
1560 * @param inode Inode concerned.
1561 * @param file File concerned.
1563 * @return 0 on success, <0 on failure.
1565 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1567 struct dvb_device *dvbdev = file->private_data;
1568 struct dvb_ca_private *ca = dvbdev->priv;
1569 int err;
1571 dprintk("%s\n", __func__);
1573 /* mark the CA device as closed */
1574 ca->open = 0;
1575 dvb_ca_en50221_thread_update_delay(ca);
1577 err = dvb_generic_release(inode, file);
1579 module_put(ca->pub->owner);
1581 return err;
1586 * Implementation of poll() syscall.
1588 * @param file File concerned.
1589 * @param wait poll wait table.
1591 * @return Standard poll mask.
1593 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
1595 struct dvb_device *dvbdev = file->private_data;
1596 struct dvb_ca_private *ca = dvbdev->priv;
1597 unsigned int mask = 0;
1598 int slot;
1599 int result = 0;
1601 dprintk("%s\n", __func__);
1603 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1604 mask |= POLLIN;
1607 /* if there is something, return now */
1608 if (mask)
1609 return mask;
1611 /* wait for something to happen */
1612 poll_wait(file, &ca->wait_queue, wait);
1614 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1615 mask |= POLLIN;
1618 return mask;
1620 EXPORT_SYMBOL(dvb_ca_en50221_init);
1623 static const struct file_operations dvb_ca_fops = {
1624 .owner = THIS_MODULE,
1625 .read = dvb_ca_en50221_io_read,
1626 .write = dvb_ca_en50221_io_write,
1627 .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
1628 .open = dvb_ca_en50221_io_open,
1629 .release = dvb_ca_en50221_io_release,
1630 .poll = dvb_ca_en50221_io_poll,
1633 static struct dvb_device dvbdev_ca = {
1634 .priv = NULL,
1635 .users = 1,
1636 .readers = 1,
1637 .writers = 1,
1638 .fops = &dvb_ca_fops,
1642 /* ******************************************************************************** */
1643 /* Initialisation/shutdown functions */
1647 * Initialise a new DVB CA EN50221 interface device.
1649 * @param dvb_adapter DVB adapter to attach the new CA device to.
1650 * @param ca The dvb_ca instance.
1651 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1652 * @param slot_count Number of slots supported.
1654 * @return 0 on success, nonzero on failure
1656 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1657 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1659 int ret;
1660 struct dvb_ca_private *ca = NULL;
1661 int i;
1663 dprintk("%s\n", __func__);
1665 if (slot_count < 1)
1666 return -EINVAL;
1668 /* initialise the system data */
1669 if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
1670 ret = -ENOMEM;
1671 goto error;
1673 ca->pub = pubca;
1674 ca->flags = flags;
1675 ca->slot_count = slot_count;
1676 if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
1677 ret = -ENOMEM;
1678 goto error;
1680 init_waitqueue_head(&ca->wait_queue);
1681 ca->open = 0;
1682 ca->wakeup = 0;
1683 ca->next_read_slot = 0;
1684 pubca->private = ca;
1686 /* register the DVB device */
1687 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA);
1688 if (ret)
1689 goto error;
1691 /* now initialise each slot */
1692 for (i = 0; i < slot_count; i++) {
1693 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
1694 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
1695 atomic_set(&ca->slot_info[i].camchange_count, 0);
1696 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1697 mutex_init(&ca->slot_info[i].slot_lock);
1700 if (signal_pending(current)) {
1701 ret = -EINTR;
1702 goto error;
1704 mb();
1706 /* create a kthread for monitoring this CA device */
1707 ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
1708 ca->dvbdev->adapter->num, ca->dvbdev->id);
1709 if (IS_ERR(ca->thread)) {
1710 ret = PTR_ERR(ca->thread);
1711 printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
1712 ret);
1713 goto error;
1715 return 0;
1717 error:
1718 if (ca != NULL) {
1719 if (ca->dvbdev != NULL)
1720 dvb_unregister_device(ca->dvbdev);
1721 kfree(ca->slot_info);
1722 kfree(ca);
1724 pubca->private = NULL;
1725 return ret;
1727 EXPORT_SYMBOL(dvb_ca_en50221_release);
1732 * Release a DVB CA EN50221 interface device.
1734 * @param ca_dev The dvb_device_t instance for the CA device.
1735 * @param ca The associated dvb_ca instance.
1737 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1739 struct dvb_ca_private *ca = pubca->private;
1740 int i;
1742 dprintk("%s\n", __func__);
1744 /* shutdown the thread if there was one */
1745 kthread_stop(ca->thread);
1747 for (i = 0; i < ca->slot_count; i++) {
1748 dvb_ca_en50221_slot_shutdown(ca, i);
1749 vfree(ca->slot_info[i].rx_buffer.data);
1751 kfree(ca->slot_info);
1752 dvb_unregister_device(ca->dvbdev);
1753 kfree(ca);
1754 pubca->private = NULL;