[XFRM]: skb_cow_data() does not set proper owner for new skbs.
[linux-2.6/verdex.git] / drivers / media / dvb / dvb-core / dvb_ca_en50221.c
blob0eb9aa711fb0335f44cce5d96faca15de40eb75d
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/moduleparam.h>
36 #include <linux/vmalloc.h>
37 #include <linux/delay.h>
38 #include <linux/rwsem.h>
40 #include "dvb_ca_en50221.h"
41 #include "dvb_ringbuffer.h"
43 static int dvb_ca_en50221_debug;
45 module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
46 MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
48 #define dprintk if (dvb_ca_en50221_debug) printk
50 #define INIT_TIMEOUT_SECS 5
52 #define HOST_LINK_BUF_SIZE 0x200
54 #define RX_BUFFER_SIZE 65535
56 #define MAX_RX_PACKETS_PER_ITERATION 10
58 #define CTRLIF_DATA 0
59 #define CTRLIF_COMMAND 1
60 #define CTRLIF_STATUS 1
61 #define CTRLIF_SIZE_LOW 2
62 #define CTRLIF_SIZE_HIGH 3
64 #define CMDREG_HC 1 /* Host control */
65 #define CMDREG_SW 2 /* Size write */
66 #define CMDREG_SR 4 /* Size read */
67 #define CMDREG_RS 8 /* Reset interface */
68 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
69 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
70 #define IRQEN (CMDREG_DAIE)
72 #define STATUSREG_RE 1 /* read error */
73 #define STATUSREG_WE 2 /* write error */
74 #define STATUSREG_FR 0x40 /* module free */
75 #define STATUSREG_DA 0x80 /* data available */
76 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
79 #define DVB_CA_SLOTSTATE_NONE 0
80 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
81 #define DVB_CA_SLOTSTATE_RUNNING 2
82 #define DVB_CA_SLOTSTATE_INVALID 3
83 #define DVB_CA_SLOTSTATE_WAITREADY 4
84 #define DVB_CA_SLOTSTATE_VALIDATE 5
85 #define DVB_CA_SLOTSTATE_WAITFR 6
86 #define DVB_CA_SLOTSTATE_LINKINIT 7
89 /* Information on a CA slot */
90 struct dvb_ca_slot {
92 /* current state of the CAM */
93 int slot_state;
95 /* Number of CAMCHANGES that have occurred since last processing */
96 atomic_t camchange_count;
98 /* Type of last CAMCHANGE */
99 int camchange_type;
101 /* base address of CAM config */
102 u32 config_base;
104 /* value to write into Config Control register */
105 u8 config_option;
107 /* if 1, the CAM supports DA IRQs */
108 u8 da_irq_supported:1;
110 /* size of the buffer to use when talking to the CAM */
111 int link_buf_size;
113 /* semaphore for syncing access to slot structure */
114 struct rw_semaphore sem;
116 /* buffer for incoming packets */
117 struct dvb_ringbuffer rx_buffer;
119 /* timer used during various states of the slot */
120 unsigned long timeout;
123 /* Private CA-interface information */
124 struct dvb_ca_private {
126 /* pointer back to the public data structure */
127 struct dvb_ca_en50221 *pub;
129 /* the DVB device */
130 struct dvb_device *dvbdev;
132 /* Flags describing the interface (DVB_CA_FLAG_*) */
133 u32 flags;
135 /* number of slots supported by this CA interface */
136 unsigned int slot_count;
138 /* information on each slot */
139 struct dvb_ca_slot *slot_info;
141 /* wait queues for read() and write() operations */
142 wait_queue_head_t wait_queue;
144 /* PID of the monitoring thread */
145 pid_t thread_pid;
147 /* Wait queue used when shutting thread down */
148 wait_queue_head_t thread_queue;
150 /* Flag indicating when thread should exit */
151 unsigned int exit:1;
153 /* Flag indicating if the CA device is open */
154 unsigned int open:1;
156 /* Flag indicating the thread should wake up now */
157 unsigned int wakeup:1;
159 /* Delay the main thread should use */
160 unsigned long delay;
162 /* Slot to start looking for data to read from in the next user-space read operation */
163 int next_read_slot;
166 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
167 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
168 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
172 * Safely find needle in haystack.
174 * @param haystack Buffer to look in.
175 * @param hlen Number of bytes in haystack.
176 * @param needle Buffer to find.
177 * @param nlen Number of bytes in needle.
178 * @return Pointer into haystack needle was found at, or NULL if not found.
180 static u8 *findstr(u8 * haystack, int hlen, u8 * needle, int nlen)
182 int i;
184 if (hlen < nlen)
185 return NULL;
187 for (i = 0; i <= hlen - nlen; i++) {
188 if (!strncmp(haystack + i, needle, nlen))
189 return haystack + i;
192 return NULL;
197 /* ******************************************************************************** */
198 /* EN50221 physical interface functions */
202 * Check CAM status.
204 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
206 int slot_status;
207 int cam_present_now;
208 int cam_changed;
210 /* IRQ mode */
211 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
212 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
215 /* poll mode */
216 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
218 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
219 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
220 if (!cam_changed) {
221 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
222 cam_changed = (cam_present_now != cam_present_old);
225 if (cam_changed) {
226 if (!cam_present_now) {
227 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
228 } else {
229 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
231 atomic_set(&ca->slot_info[slot].camchange_count, 1);
232 } else {
233 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
234 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
235 // move to validate state if reset is completed
236 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
240 return cam_changed;
245 * Wait for flags to become set on the STATUS register on a CAM interface,
246 * checking for errors and timeout.
248 * @param ca CA instance.
249 * @param slot Slot on interface.
250 * @param waitfor Flags to wait for.
251 * @param timeout_ms Timeout in milliseconds.
253 * @return 0 on success, nonzero on error.
255 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
256 u8 waitfor, int timeout_hz)
258 unsigned long timeout;
259 unsigned long start;
261 dprintk("%s\n", __FUNCTION__);
263 /* loop until timeout elapsed */
264 start = jiffies;
265 timeout = jiffies + timeout_hz;
266 while (1) {
267 /* read the status and check for error */
268 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
269 if (res < 0)
270 return -EIO;
272 /* if we got the flags, it was successful! */
273 if (res & waitfor) {
274 dprintk("%s succeeded timeout:%lu\n", __FUNCTION__, jiffies - start);
275 return 0;
278 /* check for timeout */
279 if (time_after(jiffies, timeout)) {
280 break;
283 /* wait for a bit */
284 msleep(1);
287 dprintk("%s failed timeout:%lu\n", __FUNCTION__, jiffies - start);
289 /* if we get here, we've timed out */
290 return -ETIMEDOUT;
295 * Initialise the link layer connection to a CAM.
297 * @param ca CA instance.
298 * @param slot Slot id.
300 * @return 0 on success, nonzero on failure.
302 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
304 int ret;
305 int buf_size;
306 u8 buf[2];
308 dprintk("%s\n", __FUNCTION__);
310 /* we'll be determining these during this function */
311 ca->slot_info[slot].da_irq_supported = 0;
313 /* set the host link buffer size temporarily. it will be overwritten with the
314 * real negotiated size later. */
315 ca->slot_info[slot].link_buf_size = 2;
317 /* read the buffer size from the CAM */
318 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
319 return ret;
320 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
321 return ret;
322 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
323 return -EIO;
324 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
325 return ret;
327 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
328 buf_size = (buf[0] << 8) | buf[1];
329 if (buf_size > HOST_LINK_BUF_SIZE)
330 buf_size = HOST_LINK_BUF_SIZE;
331 ca->slot_info[slot].link_buf_size = buf_size;
332 buf[0] = buf_size >> 8;
333 buf[1] = buf_size & 0xff;
334 dprintk("Chosen link buffer size of %i\n", buf_size);
336 /* write the buffer size to the CAM */
337 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
338 return ret;
339 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
340 return ret;
341 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
342 return -EIO;
343 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
344 return ret;
346 /* success */
347 return 0;
351 * Read a tuple from attribute memory.
353 * @param ca CA instance.
354 * @param slot Slot id.
355 * @param address Address to read from. Updated.
356 * @param tupleType Tuple id byte. Updated.
357 * @param tupleLength Tuple length. Updated.
358 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
360 * @return 0 on success, nonzero on error.
362 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
363 int *address, int *tupleType, int *tupleLength, u8 * tuple)
365 int i;
366 int _tupleType;
367 int _tupleLength;
368 int _address = *address;
370 /* grab the next tuple length and type */
371 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
372 return _tupleType;
373 if (_tupleType == 0xff) {
374 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
375 *address += 2;
376 *tupleType = _tupleType;
377 *tupleLength = 0;
378 return 0;
380 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
381 return _tupleLength;
382 _address += 4;
384 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
386 /* read in the whole tuple */
387 for (i = 0; i < _tupleLength; i++) {
388 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2));
389 dprintk(" 0x%02x: 0x%02x %c\n",
390 i, tuple[i] & 0xff,
391 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
393 _address += (_tupleLength * 2);
395 // success
396 *tupleType = _tupleType;
397 *tupleLength = _tupleLength;
398 *address = _address;
399 return 0;
404 * Parse attribute memory of a CAM module, extracting Config register, and checking
405 * it is a DVB CAM module.
407 * @param ca CA instance.
408 * @param slot Slot id.
410 * @return 0 on success, <0 on failure.
412 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
414 int address = 0;
415 int tupleLength;
416 int tupleType;
417 u8 tuple[257];
418 char *dvb_str;
419 int rasz;
420 int status;
421 int got_cftableentry = 0;
422 int end_chain = 0;
423 int i;
424 u16 manfid = 0;
425 u16 devid = 0;
428 // CISTPL_DEVICE_0A
429 if ((status =
430 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
431 return status;
432 if (tupleType != 0x1D)
433 return -EINVAL;
437 // CISTPL_DEVICE_0C
438 if ((status =
439 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
440 return status;
441 if (tupleType != 0x1C)
442 return -EINVAL;
446 // CISTPL_VERS_1
447 if ((status =
448 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
449 return status;
450 if (tupleType != 0x15)
451 return -EINVAL;
455 // CISTPL_MANFID
456 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
457 &tupleLength, tuple)) < 0)
458 return status;
459 if (tupleType != 0x20)
460 return -EINVAL;
461 if (tupleLength != 4)
462 return -EINVAL;
463 manfid = (tuple[1] << 8) | tuple[0];
464 devid = (tuple[3] << 8) | tuple[2];
468 // CISTPL_CONFIG
469 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
470 &tupleLength, tuple)) < 0)
471 return status;
472 if (tupleType != 0x1A)
473 return -EINVAL;
474 if (tupleLength < 3)
475 return -EINVAL;
477 /* extract the configbase */
478 rasz = tuple[0] & 3;
479 if (tupleLength < (3 + rasz + 14))
480 return -EINVAL;
481 ca->slot_info[slot].config_base = 0;
482 for (i = 0; i < rasz + 1; i++) {
483 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i));
486 /* check it contains the correct DVB string */
487 dvb_str = findstr(tuple, tupleLength, "DVB_CI_V", 8);
488 if (dvb_str == NULL)
489 return -EINVAL;
490 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
491 return -EINVAL;
493 /* is it a version we support? */
494 if (strncmp(dvb_str + 8, "1.00", 4)) {
495 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
496 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]);
497 return -EINVAL;
500 /* process the CFTABLE_ENTRY tuples, and any after those */
501 while ((!end_chain) && (address < 0x1000)) {
502 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
503 &tupleLength, tuple)) < 0)
504 return status;
505 switch (tupleType) {
506 case 0x1B: // CISTPL_CFTABLE_ENTRY
507 if (tupleLength < (2 + 11 + 17))
508 break;
510 /* if we've already parsed one, just use it */
511 if (got_cftableentry)
512 break;
514 /* get the config option */
515 ca->slot_info[slot].config_option = tuple[0] & 0x3f;
517 /* OK, check it contains the correct strings */
518 if ((findstr(tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
519 (findstr(tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
520 break;
522 got_cftableentry = 1;
523 break;
525 case 0x14: // CISTPL_NO_LINK
526 break;
528 case 0xFF: // CISTPL_END
529 end_chain = 1;
530 break;
532 default: /* Unknown tuple type - just skip this tuple and move to the next one */
533 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType,
534 tupleLength);
535 break;
539 if ((address > 0x1000) || (!got_cftableentry))
540 return -EINVAL;
542 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
543 manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option);
545 // success!
546 return 0;
551 * Set CAM's configoption correctly.
553 * @param ca CA instance.
554 * @param slot Slot containing the CAM.
556 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
558 int configoption;
560 dprintk("%s\n", __FUNCTION__);
562 /* set the config option */
563 ca->pub->write_attribute_mem(ca->pub, slot,
564 ca->slot_info[slot].config_base,
565 ca->slot_info[slot].config_option);
567 /* check it */
568 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
569 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
570 ca->slot_info[slot].config_option, configoption & 0x3f);
572 /* fine! */
573 return 0;
579 * This function talks to an EN50221 CAM control interface. It reads a buffer of
580 * data from the CAM. The data can either be stored in a supplied buffer, or
581 * automatically be added to the slot's rx_buffer.
583 * @param ca CA instance.
584 * @param slot Slot to read from.
585 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
586 * the data will be added into the buffering system as a normal fragment.
587 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
589 * @return Number of bytes read, or < 0 on error
591 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount)
593 int bytes_read;
594 int status;
595 u8 buf[HOST_LINK_BUF_SIZE];
596 int i;
598 dprintk("%s\n", __FUNCTION__);
600 /* check if we have space for a link buf in the rx_buffer */
601 if (ebuf == NULL) {
602 int buf_free;
604 down_read(&ca->slot_info[slot].sem);
605 if (ca->slot_info[slot].rx_buffer.data == NULL) {
606 up_read(&ca->slot_info[slot].sem);
607 status = -EIO;
608 goto exit;
610 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
611 up_read(&ca->slot_info[slot].sem);
613 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
614 status = -EAGAIN;
615 goto exit;
619 /* check if there is data available */
620 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
621 goto exit;
622 if (!(status & STATUSREG_DA)) {
623 /* no data */
624 status = 0;
625 goto exit;
628 /* read the amount of data */
629 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
630 goto exit;
631 bytes_read = status << 8;
632 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
633 goto exit;
634 bytes_read |= status;
636 /* check it will fit */
637 if (ebuf == NULL) {
638 if (bytes_read > ca->slot_info[slot].link_buf_size) {
639 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
640 ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size);
641 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
642 status = -EIO;
643 goto exit;
645 if (bytes_read < 2) {
646 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
647 ca->dvbdev->adapter->num);
648 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
649 status = -EIO;
650 goto exit;
652 } else {
653 if (bytes_read > ecount) {
654 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
655 ca->dvbdev->adapter->num);
656 status = -EIO;
657 goto exit;
661 /* fill the buffer */
662 for (i = 0; i < bytes_read; i++) {
663 /* read byte and check */
664 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
665 goto exit;
667 /* OK, store it in the buffer */
668 buf[i] = status;
671 /* check for read error (RE should now be 0) */
672 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
673 goto exit;
674 if (status & STATUSREG_RE) {
675 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
676 status = -EIO;
677 goto exit;
680 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
681 if (ebuf == NULL) {
682 down_read(&ca->slot_info[slot].sem);
683 if (ca->slot_info[slot].rx_buffer.data == NULL) {
684 up_read(&ca->slot_info[slot].sem);
685 status = -EIO;
686 goto exit;
688 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
689 up_read(&ca->slot_info[slot].sem);
690 } else {
691 memcpy(ebuf, buf, bytes_read);
694 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
695 buf[0], (buf[1] & 0x80) == 0, bytes_read);
697 /* wake up readers when a last_fragment is received */
698 if ((buf[1] & 0x80) == 0x00) {
699 wake_up_interruptible(&ca->wait_queue);
701 status = bytes_read;
703 exit:
704 return status;
709 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
710 * to a CAM.
712 * @param ca CA instance.
713 * @param slot Slot to write to.
714 * @param ebuf The data in this buffer is treated as a complete link-level packet to
715 * be written.
716 * @param count Size of ebuf.
718 * @return Number of bytes written, or < 0 on error.
720 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write)
722 int status;
723 int i;
725 dprintk("%s\n", __FUNCTION__);
728 // sanity check
729 if (bytes_write > ca->slot_info[slot].link_buf_size)
730 return -EINVAL;
732 /* check if interface is actually waiting for us to read from it, or if a read is in progress */
733 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
734 goto exitnowrite;
735 if (status & (STATUSREG_DA | STATUSREG_RE)) {
736 status = -EAGAIN;
737 goto exitnowrite;
740 /* OK, set HC bit */
741 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
742 IRQEN | CMDREG_HC)) != 0)
743 goto exit;
745 /* check if interface is still free */
746 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
747 goto exit;
748 if (!(status & STATUSREG_FR)) {
749 /* it wasn't free => try again later */
750 status = -EAGAIN;
751 goto exit;
754 /* send the amount of data */
755 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
756 goto exit;
757 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
758 bytes_write & 0xff)) != 0)
759 goto exit;
761 /* send the buffer */
762 for (i = 0; i < bytes_write; i++) {
763 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
764 goto exit;
767 /* check for write error (WE should now be 0) */
768 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
769 goto exit;
770 if (status & STATUSREG_WE) {
771 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
772 status = -EIO;
773 goto exit;
775 status = bytes_write;
777 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
778 buf[0], (buf[1] & 0x80) == 0, bytes_write);
780 exit:
781 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
783 exitnowrite:
784 return status;
786 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
790 /* ******************************************************************************** */
791 /* EN50221 higher level functions */
795 * A CAM has been removed => shut it down.
797 * @param ca CA instance.
798 * @param slot Slot to shut down.
800 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
802 dprintk("%s\n", __FUNCTION__);
804 down_write(&ca->slot_info[slot].sem);
805 ca->pub->slot_shutdown(ca->pub, slot);
806 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
807 vfree(ca->slot_info[slot].rx_buffer.data);
808 ca->slot_info[slot].rx_buffer.data = NULL;
809 up_write(&ca->slot_info[slot].sem);
811 /* need to wake up all processes to check if they're now
812 trying to write to a defunct CAM */
813 wake_up_interruptible(&ca->wait_queue);
815 dprintk("Slot %i shutdown\n", slot);
817 /* success */
818 return 0;
820 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
824 * A CAMCHANGE IRQ has occurred.
826 * @param ca CA instance.
827 * @param slot Slot concerned.
828 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
830 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
832 struct dvb_ca_private *ca = pubca->private;
834 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
836 switch (change_type) {
837 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
838 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
839 break;
841 default:
842 return;
845 ca->slot_info[slot].camchange_type = change_type;
846 atomic_inc(&ca->slot_info[slot].camchange_count);
847 dvb_ca_en50221_thread_wakeup(ca);
849 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
853 * A CAMREADY IRQ has occurred.
855 * @param ca CA instance.
856 * @param slot Slot concerned.
858 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
860 struct dvb_ca_private *ca = pubca->private;
862 dprintk("CAMREADY IRQ slot:%i\n", slot);
864 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
865 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
866 dvb_ca_en50221_thread_wakeup(ca);
872 * An FR or DA IRQ has occurred.
874 * @param ca CA instance.
875 * @param slot Slot concerned.
877 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
879 struct dvb_ca_private *ca = pubca->private;
880 int flags;
882 dprintk("FR/DA IRQ slot:%i\n", slot);
884 switch (ca->slot_info[slot].slot_state) {
885 case DVB_CA_SLOTSTATE_LINKINIT:
886 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
887 if (flags & STATUSREG_DA) {
888 dprintk("CAM supports DA IRQ\n");
889 ca->slot_info[slot].da_irq_supported = 1;
891 break;
893 case DVB_CA_SLOTSTATE_RUNNING:
894 if (ca->open)
895 dvb_ca_en50221_read_data(ca, slot, NULL, 0);
896 break;
902 /* ******************************************************************************** */
903 /* EN50221 thread functions */
906 * Wake up the DVB CA thread
908 * @param ca CA instance.
910 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
913 dprintk("%s\n", __FUNCTION__);
915 ca->wakeup = 1;
916 mb();
917 wake_up_interruptible(&ca->thread_queue);
921 * Used by the CA thread to determine if an early wakeup is necessary
923 * @param ca CA instance.
925 static int dvb_ca_en50221_thread_should_wakeup(struct dvb_ca_private *ca)
927 if (ca->wakeup) {
928 ca->wakeup = 0;
929 return 1;
931 if (ca->exit)
932 return 1;
934 return 0;
939 * Update the delay used by the thread.
941 * @param ca CA instance.
943 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
945 int delay;
946 int curdelay = 100000000;
947 int slot;
949 for (slot = 0; slot < ca->slot_count; slot++) {
950 switch (ca->slot_info[slot].slot_state) {
951 default:
952 case DVB_CA_SLOTSTATE_NONE:
953 case DVB_CA_SLOTSTATE_INVALID:
954 delay = HZ * 60;
955 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
956 delay = HZ / 10;
958 break;
960 case DVB_CA_SLOTSTATE_UNINITIALISED:
961 case DVB_CA_SLOTSTATE_WAITREADY:
962 case DVB_CA_SLOTSTATE_VALIDATE:
963 case DVB_CA_SLOTSTATE_WAITFR:
964 case DVB_CA_SLOTSTATE_LINKINIT:
965 delay = HZ / 10;
966 break;
968 case DVB_CA_SLOTSTATE_RUNNING:
969 delay = HZ * 60;
970 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
971 delay = HZ / 10;
973 if (ca->open) {
974 if ((!ca->slot_info[slot].da_irq_supported) ||
975 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA))) {
976 delay = HZ / 10;
979 break;
982 if (delay < curdelay)
983 curdelay = delay;
986 ca->delay = curdelay;
992 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
994 static int dvb_ca_en50221_thread(void *data)
996 struct dvb_ca_private *ca = data;
997 char name[15];
998 int slot;
999 int flags;
1000 int status;
1001 int pktcount;
1002 void *rxbuf;
1004 dprintk("%s\n", __FUNCTION__);
1006 /* setup kernel thread */
1007 snprintf(name, sizeof(name), "kdvb-ca-%i:%i", ca->dvbdev->adapter->num, ca->dvbdev->id);
1009 lock_kernel();
1010 daemonize(name);
1011 sigfillset(&current->blocked);
1012 unlock_kernel();
1014 /* choose the correct initial delay */
1015 dvb_ca_en50221_thread_update_delay(ca);
1017 /* main loop */
1018 while (!ca->exit) {
1019 /* sleep for a bit */
1020 if (!ca->wakeup) {
1021 flags = wait_event_interruptible_timeout(ca->thread_queue,
1022 dvb_ca_en50221_thread_should_wakeup(ca),
1023 ca->delay);
1024 if ((flags == -ERESTARTSYS) || ca->exit) {
1025 /* got signal or quitting */
1026 break;
1029 ca->wakeup = 0;
1031 /* go through all the slots processing them */
1032 for (slot = 0; slot < ca->slot_count; slot++) {
1034 // check the cam status + deal with CAMCHANGEs
1035 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1036 /* clear down an old CI slot if necessary */
1037 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
1038 dvb_ca_en50221_slot_shutdown(ca, slot);
1040 /* if a CAM is NOW present, initialise it */
1041 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
1042 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1045 /* we've handled one CAMCHANGE */
1046 dvb_ca_en50221_thread_update_delay(ca);
1047 atomic_dec(&ca->slot_info[slot].camchange_count);
1050 // CAM state machine
1051 switch (ca->slot_info[slot].slot_state) {
1052 case DVB_CA_SLOTSTATE_NONE:
1053 case DVB_CA_SLOTSTATE_INVALID:
1054 // no action needed
1055 break;
1057 case DVB_CA_SLOTSTATE_UNINITIALISED:
1058 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1059 ca->pub->slot_reset(ca->pub, slot);
1060 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1061 break;
1063 case DVB_CA_SLOTSTATE_WAITREADY:
1064 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1065 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1066 ca->dvbdev->adapter->num);
1067 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1068 dvb_ca_en50221_thread_update_delay(ca);
1069 break;
1071 // no other action needed; will automatically change state when ready
1072 break;
1074 case DVB_CA_SLOTSTATE_VALIDATE:
1075 if (dvb_ca_en50221_parse_attributes(ca, slot)
1076 != 0) {
1077 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1078 ca->dvbdev->adapter->num);
1079 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1080 dvb_ca_en50221_thread_update_delay(ca);
1081 break;
1083 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1084 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1085 ca->dvbdev->adapter->num);
1086 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1087 dvb_ca_en50221_thread_update_delay(ca);
1088 break;
1090 if (ca->pub->write_cam_control(ca->pub, slot,
1091 CTRLIF_COMMAND, CMDREG_RS) != 0) {
1092 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1093 ca->dvbdev->adapter->num);
1094 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1095 dvb_ca_en50221_thread_update_delay(ca);
1096 break;
1098 dprintk("DVB CAM validated successfully\n");
1100 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1101 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
1102 ca->wakeup = 1;
1103 break;
1105 case DVB_CA_SLOTSTATE_WAITFR:
1106 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1107 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1108 ca->dvbdev->adapter->num);
1109 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1110 dvb_ca_en50221_thread_update_delay(ca);
1111 break;
1114 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1115 if (flags & STATUSREG_FR) {
1116 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1117 ca->wakeup = 1;
1119 break;
1121 case DVB_CA_SLOTSTATE_LINKINIT:
1122 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1123 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
1124 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1125 dvb_ca_en50221_thread_update_delay(ca);
1126 break;
1129 rxbuf = vmalloc(RX_BUFFER_SIZE);
1130 if (rxbuf == NULL) {
1131 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
1132 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1133 dvb_ca_en50221_thread_update_delay(ca);
1134 break;
1136 down_write(&ca->slot_info[slot].sem);
1137 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
1138 up_write(&ca->slot_info[slot].sem);
1140 ca->pub->slot_ts_enable(ca->pub, slot);
1141 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
1142 dvb_ca_en50221_thread_update_delay(ca);
1143 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num);
1144 break;
1146 case DVB_CA_SLOTSTATE_RUNNING:
1147 if (!ca->open)
1148 continue;
1150 // no need to poll if the CAM supports IRQs
1151 if (ca->slot_info[slot].da_irq_supported)
1152 break;
1154 // poll mode
1155 pktcount = 0;
1156 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1157 if (!ca->open)
1158 break;
1160 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1161 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1162 // we dont want to sleep on the next iteration so we can handle the cam change
1163 ca->wakeup = 1;
1164 break;
1167 /* check if we've hit our limit this time */
1168 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1169 // dont sleep; there is likely to be more data to read
1170 ca->wakeup = 1;
1171 break;
1174 break;
1179 /* completed */
1180 ca->thread_pid = 0;
1181 mb();
1182 wake_up_interruptible(&ca->thread_queue);
1183 return 0;
1188 /* ******************************************************************************** */
1189 /* EN50221 IO interface functions */
1192 * Real ioctl implementation.
1193 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1195 * @param inode Inode concerned.
1196 * @param file File concerned.
1197 * @param cmd IOCTL command.
1198 * @param arg Associated argument.
1200 * @return 0 on success, <0 on error.
1202 static int dvb_ca_en50221_io_do_ioctl(struct inode *inode, struct file *file,
1203 unsigned int cmd, void *parg)
1205 struct dvb_device *dvbdev = file->private_data;
1206 struct dvb_ca_private *ca = dvbdev->priv;
1207 int err = 0;
1208 int slot;
1210 dprintk("%s\n", __FUNCTION__);
1212 switch (cmd) {
1213 case CA_RESET:
1214 for (slot = 0; slot < ca->slot_count; slot++) {
1215 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
1216 dvb_ca_en50221_slot_shutdown(ca, slot);
1217 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1218 dvb_ca_en50221_camchange_irq(ca->pub,
1219 slot,
1220 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1223 ca->next_read_slot = 0;
1224 dvb_ca_en50221_thread_wakeup(ca);
1225 break;
1227 case CA_GET_CAP: {
1228 struct ca_caps *caps = parg;
1230 caps->slot_num = ca->slot_count;
1231 caps->slot_type = CA_CI_LINK;
1232 caps->descr_num = 0;
1233 caps->descr_type = 0;
1234 break;
1237 case CA_GET_SLOT_INFO: {
1238 struct ca_slot_info *info = parg;
1240 if ((info->num > ca->slot_count) || (info->num < 0))
1241 return -EINVAL;
1243 info->type = CA_CI_LINK;
1244 info->flags = 0;
1245 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
1246 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1247 info->flags = CA_CI_MODULE_PRESENT;
1249 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1250 info->flags |= CA_CI_MODULE_READY;
1252 break;
1255 default:
1256 err = -EINVAL;
1257 break;
1260 return err;
1265 * Wrapper for ioctl implementation.
1267 * @param inode Inode concerned.
1268 * @param file File concerned.
1269 * @param cmd IOCTL command.
1270 * @param arg Associated argument.
1272 * @return 0 on success, <0 on error.
1274 static int dvb_ca_en50221_io_ioctl(struct inode *inode, struct file *file,
1275 unsigned int cmd, unsigned long arg)
1277 return dvb_usercopy(inode, file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1282 * Implementation of write() syscall.
1284 * @param file File structure.
1285 * @param buf Source buffer.
1286 * @param count Size of source buffer.
1287 * @param ppos Position in file (ignored).
1289 * @return Number of bytes read, or <0 on error.
1291 static ssize_t dvb_ca_en50221_io_write(struct file *file,
1292 const char __user * buf, size_t count, loff_t * ppos)
1294 struct dvb_device *dvbdev = file->private_data;
1295 struct dvb_ca_private *ca = dvbdev->priv;
1296 u8 slot, connection_id;
1297 int status;
1298 char fragbuf[HOST_LINK_BUF_SIZE];
1299 int fragpos = 0;
1300 int fraglen;
1301 unsigned long timeout;
1302 int written;
1304 dprintk("%s\n", __FUNCTION__);
1306 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1307 if (count < 2)
1308 return -EINVAL;
1310 /* extract slot & connection id */
1311 if (copy_from_user(&slot, buf, 1))
1312 return -EFAULT;
1313 if (copy_from_user(&connection_id, buf + 1, 1))
1314 return -EFAULT;
1315 buf += 2;
1316 count -= 2;
1318 /* check if the slot is actually running */
1319 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1320 return -EINVAL;
1322 /* fragment the packets & store in the buffer */
1323 while (fragpos < count) {
1324 fraglen = ca->slot_info[slot].link_buf_size - 2;
1325 if ((count - fragpos) < fraglen)
1326 fraglen = count - fragpos;
1328 fragbuf[0] = connection_id;
1329 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1330 if ((status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen)) != 0)
1331 goto exit;
1333 timeout = jiffies + HZ / 2;
1334 written = 0;
1335 while (!time_after(jiffies, timeout)) {
1336 /* check the CAM hasn't been removed/reset in the meantime */
1337 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1338 status = -EIO;
1339 goto exit;
1342 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
1343 if (status == (fraglen + 2)) {
1344 written = 1;
1345 break;
1347 if (status != -EAGAIN)
1348 goto exit;
1350 msleep(1);
1352 if (!written) {
1353 status = -EIO;
1354 goto exit;
1357 fragpos += fraglen;
1359 status = count + 2;
1361 exit:
1362 return status;
1367 * Condition for waking up in dvb_ca_en50221_io_read_condition
1369 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca, int *result, int *_slot)
1371 int slot;
1372 int slot_count = 0;
1373 int idx;
1374 int fraglen;
1375 int connection_id = -1;
1376 int found = 0;
1377 u8 hdr[2];
1379 slot = ca->next_read_slot;
1380 while ((slot_count < ca->slot_count) && (!found)) {
1381 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1382 goto nextslot;
1384 down_read(&ca->slot_info[slot].sem);
1386 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1387 up_read(&ca->slot_info[slot].sem);
1388 return 0;
1391 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1392 while (idx != -1) {
1393 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2, 0);
1394 if (connection_id == -1)
1395 connection_id = hdr[0];
1396 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
1397 *_slot = slot;
1398 found = 1;
1399 break;
1402 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1405 if (!found)
1406 up_read(&ca->slot_info[slot].sem);
1408 nextslot:
1409 slot = (slot + 1) % ca->slot_count;
1410 slot_count++;
1413 ca->next_read_slot = slot;
1414 return found;
1419 * Implementation of read() syscall.
1421 * @param file File structure.
1422 * @param buf Destination buffer.
1423 * @param count Size of destination buffer.
1424 * @param ppos Position in file (ignored).
1426 * @return Number of bytes read, or <0 on error.
1428 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
1429 size_t count, loff_t * ppos)
1431 struct dvb_device *dvbdev = file->private_data;
1432 struct dvb_ca_private *ca = dvbdev->priv;
1433 int status;
1434 int result = 0;
1435 u8 hdr[2];
1436 int slot;
1437 int connection_id = -1;
1438 size_t idx, idx2;
1439 int last_fragment = 0;
1440 size_t fraglen;
1441 int pktlen;
1442 int dispose = 0;
1444 dprintk("%s\n", __FUNCTION__);
1446 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1447 if (count < 2)
1448 return -EINVAL;
1450 /* wait for some data */
1451 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
1453 /* if we're in nonblocking mode, exit immediately */
1454 if (file->f_flags & O_NONBLOCK)
1455 return -EWOULDBLOCK;
1457 /* wait for some data */
1458 status = wait_event_interruptible(ca->wait_queue,
1459 dvb_ca_en50221_io_read_condition
1460 (ca, &result, &slot));
1462 if ((status < 0) || (result < 0)) {
1463 if (result)
1464 return result;
1465 return status;
1468 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1469 pktlen = 2;
1470 do {
1471 if (idx == -1) {
1472 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
1473 status = -EIO;
1474 goto exit;
1477 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2, 0);
1478 if (connection_id == -1)
1479 connection_id = hdr[0];
1480 if (hdr[0] == connection_id) {
1481 if (pktlen < count) {
1482 if ((pktlen + fraglen - 2) > count) {
1483 fraglen = count - pktlen;
1484 } else {
1485 fraglen -= 2;
1488 if ((status = dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 2,
1489 buf + pktlen, fraglen, 1)) < 0) {
1490 goto exit;
1492 pktlen += fraglen;
1495 if ((hdr[1] & 0x80) == 0)
1496 last_fragment = 1;
1497 dispose = 1;
1500 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1501 if (dispose)
1502 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
1503 idx = idx2;
1504 dispose = 0;
1505 } while (!last_fragment);
1507 hdr[0] = slot;
1508 hdr[1] = connection_id;
1509 if ((status = copy_to_user(buf, hdr, 2)) != 0)
1510 goto exit;
1511 status = pktlen;
1513 exit:
1514 up_read(&ca->slot_info[slot].sem);
1515 return status;
1520 * Implementation of file open syscall.
1522 * @param inode Inode concerned.
1523 * @param file File concerned.
1525 * @return 0 on success, <0 on failure.
1527 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1529 struct dvb_device *dvbdev = file->private_data;
1530 struct dvb_ca_private *ca = dvbdev->priv;
1531 int err;
1532 int i;
1534 dprintk("%s\n", __FUNCTION__);
1536 if (!try_module_get(ca->pub->owner))
1537 return -EIO;
1539 err = dvb_generic_open(inode, file);
1540 if (err < 0)
1541 return err;
1543 for (i = 0; i < ca->slot_count; i++) {
1545 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1546 down_write(&ca->slot_info[i].sem);
1547 if (ca->slot_info[i].rx_buffer.data != NULL) {
1548 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
1550 up_write(&ca->slot_info[i].sem);
1554 ca->open = 1;
1555 dvb_ca_en50221_thread_update_delay(ca);
1556 dvb_ca_en50221_thread_wakeup(ca);
1558 return 0;
1563 * Implementation of file close syscall.
1565 * @param inode Inode concerned.
1566 * @param file File concerned.
1568 * @return 0 on success, <0 on failure.
1570 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1572 struct dvb_device *dvbdev = file->private_data;
1573 struct dvb_ca_private *ca = dvbdev->priv;
1574 int err = 0;
1576 dprintk("%s\n", __FUNCTION__);
1578 /* mark the CA device as closed */
1579 ca->open = 0;
1580 dvb_ca_en50221_thread_update_delay(ca);
1582 err = dvb_generic_release(inode, file);
1584 module_put(ca->pub->owner);
1586 return 0;
1591 * Implementation of poll() syscall.
1593 * @param file File concerned.
1594 * @param wait poll wait table.
1596 * @return Standard poll mask.
1598 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
1600 struct dvb_device *dvbdev = file->private_data;
1601 struct dvb_ca_private *ca = dvbdev->priv;
1602 unsigned int mask = 0;
1603 int slot;
1604 int result = 0;
1606 dprintk("%s\n", __FUNCTION__);
1608 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1609 up_read(&ca->slot_info[slot].sem);
1610 mask |= POLLIN;
1613 /* if there is something, return now */
1614 if (mask)
1615 return mask;
1617 /* wait for something to happen */
1618 poll_wait(file, &ca->wait_queue, wait);
1620 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1621 up_read(&ca->slot_info[slot].sem);
1622 mask |= POLLIN;
1625 return mask;
1627 EXPORT_SYMBOL(dvb_ca_en50221_init);
1630 static struct file_operations dvb_ca_fops = {
1631 .owner = THIS_MODULE,
1632 .read = dvb_ca_en50221_io_read,
1633 .write = dvb_ca_en50221_io_write,
1634 .ioctl = dvb_ca_en50221_io_ioctl,
1635 .open = dvb_ca_en50221_io_open,
1636 .release = dvb_ca_en50221_io_release,
1637 .poll = dvb_ca_en50221_io_poll,
1640 static struct dvb_device dvbdev_ca = {
1641 .priv = NULL,
1642 .users = 1,
1643 .readers = 1,
1644 .writers = 1,
1645 .fops = &dvb_ca_fops,
1649 /* ******************************************************************************** */
1650 /* Initialisation/shutdown functions */
1654 * Initialise a new DVB CA EN50221 interface device.
1656 * @param dvb_adapter DVB adapter to attach the new CA device to.
1657 * @param ca The dvb_ca instance.
1658 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1659 * @param slot_count Number of slots supported.
1661 * @return 0 on success, nonzero on failure
1663 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1664 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1666 int ret;
1667 struct dvb_ca_private *ca = NULL;
1668 int i;
1670 dprintk("%s\n", __FUNCTION__);
1672 if (slot_count < 1)
1673 return -EINVAL;
1675 /* initialise the system data */
1676 if ((ca =
1677 (struct dvb_ca_private *) kmalloc(sizeof(struct dvb_ca_private),
1678 GFP_KERNEL)) == NULL) {
1679 ret = -ENOMEM;
1680 goto error;
1682 memset(ca, 0, sizeof(struct dvb_ca_private));
1683 ca->pub = pubca;
1684 ca->flags = flags;
1685 ca->slot_count = slot_count;
1686 if ((ca->slot_info = kmalloc(sizeof(struct dvb_ca_slot) * slot_count, GFP_KERNEL)) == NULL) {
1687 ret = -ENOMEM;
1688 goto error;
1690 memset(ca->slot_info, 0, sizeof(struct dvb_ca_slot) * slot_count);
1691 init_waitqueue_head(&ca->wait_queue);
1692 ca->thread_pid = 0;
1693 init_waitqueue_head(&ca->thread_queue);
1694 ca->exit = 0;
1695 ca->open = 0;
1696 ca->wakeup = 0;
1697 ca->next_read_slot = 0;
1698 pubca->private = ca;
1700 /* register the DVB device */
1701 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA);
1702 if (ret)
1703 goto error;
1705 /* now initialise each slot */
1706 for (i = 0; i < slot_count; i++) {
1707 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
1708 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
1709 atomic_set(&ca->slot_info[i].camchange_count, 0);
1710 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1711 init_rwsem(&ca->slot_info[i].sem);
1714 if (signal_pending(current)) {
1715 ret = -EINTR;
1716 goto error;
1718 mb();
1720 /* create a kthread for monitoring this CA device */
1722 ret = kernel_thread(dvb_ca_en50221_thread, ca, 0);
1724 if (ret < 0) {
1725 printk("dvb_ca_init: failed to start kernel_thread (%d)\n", ret);
1726 goto error;
1728 ca->thread_pid = ret;
1729 return 0;
1731 error:
1732 if (ca != NULL) {
1733 if (ca->dvbdev != NULL)
1734 dvb_unregister_device(ca->dvbdev);
1735 kfree(ca->slot_info);
1736 kfree(ca);
1738 pubca->private = NULL;
1739 return ret;
1741 EXPORT_SYMBOL(dvb_ca_en50221_release);
1746 * Release a DVB CA EN50221 interface device.
1748 * @param ca_dev The dvb_device_t instance for the CA device.
1749 * @param ca The associated dvb_ca instance.
1751 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1753 struct dvb_ca_private *ca = pubca->private;
1754 int i;
1756 dprintk("%s\n", __FUNCTION__);
1758 /* shutdown the thread if there was one */
1759 if (ca->thread_pid) {
1760 if (kill_proc(ca->thread_pid, 0, 1) == -ESRCH) {
1761 printk("dvb_ca_release adapter %d: thread PID %d already died\n",
1762 ca->dvbdev->adapter->num, ca->thread_pid);
1763 } else {
1764 ca->exit = 1;
1765 mb();
1766 dvb_ca_en50221_thread_wakeup(ca);
1767 wait_event_interruptible(ca->thread_queue, ca->thread_pid == 0);
1771 for (i = 0; i < ca->slot_count; i++) {
1772 dvb_ca_en50221_slot_shutdown(ca, i);
1774 kfree(ca->slot_info);
1775 dvb_unregister_device(ca->dvbdev);
1776 kfree(ca);
1777 pubca->private = NULL;