r8152: fix tx packets accounting
[linux/fpc-iii.git] / drivers / media / dvb-core / dvb_ca_en50221.c
blob42ce88ceac66d914cee62a1a68b67f33db139181
1 /*
2 * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces
4 * Copyright (C) 2004 Andrew de Quincey
6 * Parts of this file were based on sources as follows:
8 * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de>
10 * based on code:
12 * Copyright (C) 1999-2002 Ralph Metzler
13 * & Marcus Metzler for convergence integrated media GmbH
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
31 #include <linux/errno.h>
32 #include <linux/slab.h>
33 #include <linux/list.h>
34 #include <linux/module.h>
35 #include <linux/vmalloc.h>
36 #include <linux/delay.h>
37 #include <linux/spinlock.h>
38 #include <linux/sched.h>
39 #include <linux/kthread.h>
41 #include "dvb_ca_en50221.h"
42 #include "dvb_ringbuffer.h"
44 static int dvb_ca_en50221_debug;
46 module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
47 MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
49 #define dprintk if (dvb_ca_en50221_debug) printk
51 #define INIT_TIMEOUT_SECS 10
53 #define HOST_LINK_BUF_SIZE 0x200
55 #define RX_BUFFER_SIZE 65535
57 #define MAX_RX_PACKETS_PER_ITERATION 10
59 #define CTRLIF_DATA 0
60 #define CTRLIF_COMMAND 1
61 #define CTRLIF_STATUS 1
62 #define CTRLIF_SIZE_LOW 2
63 #define CTRLIF_SIZE_HIGH 3
65 #define CMDREG_HC 1 /* Host control */
66 #define CMDREG_SW 2 /* Size write */
67 #define CMDREG_SR 4 /* Size read */
68 #define CMDREG_RS 8 /* Reset interface */
69 #define CMDREG_FRIE 0x40 /* Enable FR interrupt */
70 #define CMDREG_DAIE 0x80 /* Enable DA interrupt */
71 #define IRQEN (CMDREG_DAIE)
73 #define STATUSREG_RE 1 /* read error */
74 #define STATUSREG_WE 2 /* write error */
75 #define STATUSREG_FR 0x40 /* module free */
76 #define STATUSREG_DA 0x80 /* data available */
77 #define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE) /* general transfer error */
80 #define DVB_CA_SLOTSTATE_NONE 0
81 #define DVB_CA_SLOTSTATE_UNINITIALISED 1
82 #define DVB_CA_SLOTSTATE_RUNNING 2
83 #define DVB_CA_SLOTSTATE_INVALID 3
84 #define DVB_CA_SLOTSTATE_WAITREADY 4
85 #define DVB_CA_SLOTSTATE_VALIDATE 5
86 #define DVB_CA_SLOTSTATE_WAITFR 6
87 #define DVB_CA_SLOTSTATE_LINKINIT 7
90 /* Information on a CA slot */
91 struct dvb_ca_slot {
93 /* current state of the CAM */
94 int slot_state;
96 /* mutex used for serializing access to one CI slot */
97 struct mutex slot_lock;
99 /* Number of CAMCHANGES that have occurred since last processing */
100 atomic_t camchange_count;
102 /* Type of last CAMCHANGE */
103 int camchange_type;
105 /* base address of CAM config */
106 u32 config_base;
108 /* value to write into Config Control register */
109 u8 config_option;
111 /* if 1, the CAM supports DA IRQs */
112 u8 da_irq_supported:1;
114 /* size of the buffer to use when talking to the CAM */
115 int link_buf_size;
117 /* buffer for incoming packets */
118 struct dvb_ringbuffer rx_buffer;
120 /* timer used during various states of the slot */
121 unsigned long timeout;
124 /* Private CA-interface information */
125 struct dvb_ca_private {
126 struct kref refcount;
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;
161 /* mutex serializing ioctls */
162 struct mutex ioctl_mutex;
165 static void dvb_ca_private_free(struct dvb_ca_private *ca)
167 unsigned int i;
169 dvb_unregister_device(ca->dvbdev);
170 for (i = 0; i < ca->slot_count; i++)
171 vfree(ca->slot_info[i].rx_buffer.data);
173 kfree(ca->slot_info);
174 kfree(ca);
177 static void dvb_ca_private_release(struct kref *ref)
179 struct dvb_ca_private *ca = container_of(ref, struct dvb_ca_private, refcount);
180 dvb_ca_private_free(ca);
183 static void dvb_ca_private_get(struct dvb_ca_private *ca)
185 kref_get(&ca->refcount);
188 static void dvb_ca_private_put(struct dvb_ca_private *ca)
190 kref_put(&ca->refcount, dvb_ca_private_release);
193 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
194 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
195 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
199 * Safely find needle in haystack.
201 * @haystack: Buffer to look in.
202 * @hlen: Number of bytes in haystack.
203 * @needle: Buffer to find.
204 * @nlen: Number of bytes in needle.
205 * @return Pointer into haystack needle was found at, or NULL if not found.
207 static char *findstr(char * haystack, int hlen, char * needle, int nlen)
209 int i;
211 if (hlen < nlen)
212 return NULL;
214 for (i = 0; i <= hlen - nlen; i++) {
215 if (!strncmp(haystack + i, needle, nlen))
216 return haystack + i;
219 return NULL;
224 /* ******************************************************************************** */
225 /* EN50221 physical interface functions */
229 * dvb_ca_en50221_check_camstatus - Check CAM status.
231 static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
233 int slot_status;
234 int cam_present_now;
235 int cam_changed;
237 /* IRQ mode */
238 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
239 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
242 /* poll mode */
243 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
245 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
246 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
247 if (!cam_changed) {
248 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
249 cam_changed = (cam_present_now != cam_present_old);
252 if (cam_changed) {
253 if (!cam_present_now) {
254 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
255 } else {
256 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
258 atomic_set(&ca->slot_info[slot].camchange_count, 1);
259 } else {
260 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
261 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
262 // move to validate state if reset is completed
263 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
267 return cam_changed;
272 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
273 * register on a CAM interface, checking for errors and timeout.
275 * @ca: CA instance.
276 * @slot: Slot on interface.
277 * @waitfor: Flags to wait for.
278 * @timeout_ms: Timeout in milliseconds.
280 * @return 0 on success, nonzero on error.
282 static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
283 u8 waitfor, int timeout_hz)
285 unsigned long timeout;
286 unsigned long start;
288 dprintk("%s\n", __func__);
290 /* loop until timeout elapsed */
291 start = jiffies;
292 timeout = jiffies + timeout_hz;
293 while (1) {
294 /* read the status and check for error */
295 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
296 if (res < 0)
297 return -EIO;
299 /* if we got the flags, it was successful! */
300 if (res & waitfor) {
301 dprintk("%s succeeded timeout:%lu\n", __func__, jiffies - start);
302 return 0;
305 /* check for timeout */
306 if (time_after(jiffies, timeout)) {
307 break;
310 /* wait for a bit */
311 msleep(1);
314 dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
316 /* if we get here, we've timed out */
317 return -ETIMEDOUT;
322 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
324 * @ca: CA instance.
325 * @slot: Slot id.
327 * @return 0 on success, nonzero on failure.
329 static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
331 int ret;
332 int buf_size;
333 u8 buf[2];
335 dprintk("%s\n", __func__);
337 /* we'll be determining these during this function */
338 ca->slot_info[slot].da_irq_supported = 0;
340 /* set the host link buffer size temporarily. it will be overwritten with the
341 * real negotiated size later. */
342 ca->slot_info[slot].link_buf_size = 2;
344 /* read the buffer size from the CAM */
345 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
346 return ret;
347 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
348 return ret;
349 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
350 return -EIO;
351 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
352 return ret;
354 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
355 buf_size = (buf[0] << 8) | buf[1];
356 if (buf_size > HOST_LINK_BUF_SIZE)
357 buf_size = HOST_LINK_BUF_SIZE;
358 ca->slot_info[slot].link_buf_size = buf_size;
359 buf[0] = buf_size >> 8;
360 buf[1] = buf_size & 0xff;
361 dprintk("Chosen link buffer size of %i\n", buf_size);
363 /* write the buffer size to the CAM */
364 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
365 return ret;
366 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
367 return ret;
368 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
369 return -EIO;
370 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
371 return ret;
373 /* success */
374 return 0;
378 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
380 * @ca: CA instance.
381 * @slot: Slot id.
382 * @address: Address to read from. Updated.
383 * @tupleType: Tuple id byte. Updated.
384 * @tupleLength: Tuple length. Updated.
385 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
387 * @return 0 on success, nonzero on error.
389 static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
390 int *address, int *tupleType, int *tupleLength, u8 * tuple)
392 int i;
393 int _tupleType;
394 int _tupleLength;
395 int _address = *address;
397 /* grab the next tuple length and type */
398 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
399 return _tupleType;
400 if (_tupleType == 0xff) {
401 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
402 *address += 2;
403 *tupleType = _tupleType;
404 *tupleLength = 0;
405 return 0;
407 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
408 return _tupleLength;
409 _address += 4;
411 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
413 /* read in the whole tuple */
414 for (i = 0; i < _tupleLength; i++) {
415 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2));
416 dprintk(" 0x%02x: 0x%02x %c\n",
417 i, tuple[i] & 0xff,
418 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
420 _address += (_tupleLength * 2);
422 // success
423 *tupleType = _tupleType;
424 *tupleLength = _tupleLength;
425 *address = _address;
426 return 0;
431 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
432 * extracting Config register, and checking it is a DVB CAM module.
434 * @ca: CA instance.
435 * @slot: Slot id.
437 * @return 0 on success, <0 on failure.
439 static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
441 int address = 0;
442 int tupleLength;
443 int tupleType;
444 u8 tuple[257];
445 char *dvb_str;
446 int rasz;
447 int status;
448 int got_cftableentry = 0;
449 int end_chain = 0;
450 int i;
451 u16 manfid = 0;
452 u16 devid = 0;
455 // CISTPL_DEVICE_0A
456 if ((status =
457 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
458 return status;
459 if (tupleType != 0x1D)
460 return -EINVAL;
464 // CISTPL_DEVICE_0C
465 if ((status =
466 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
467 return status;
468 if (tupleType != 0x1C)
469 return -EINVAL;
473 // CISTPL_VERS_1
474 if ((status =
475 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
476 return status;
477 if (tupleType != 0x15)
478 return -EINVAL;
482 // CISTPL_MANFID
483 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
484 &tupleLength, tuple)) < 0)
485 return status;
486 if (tupleType != 0x20)
487 return -EINVAL;
488 if (tupleLength != 4)
489 return -EINVAL;
490 manfid = (tuple[1] << 8) | tuple[0];
491 devid = (tuple[3] << 8) | tuple[2];
495 // CISTPL_CONFIG
496 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
497 &tupleLength, tuple)) < 0)
498 return status;
499 if (tupleType != 0x1A)
500 return -EINVAL;
501 if (tupleLength < 3)
502 return -EINVAL;
504 /* extract the configbase */
505 rasz = tuple[0] & 3;
506 if (tupleLength < (3 + rasz + 14))
507 return -EINVAL;
508 ca->slot_info[slot].config_base = 0;
509 for (i = 0; i < rasz + 1; i++) {
510 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i));
513 /* check it contains the correct DVB string */
514 dvb_str = findstr((char *)tuple, tupleLength, "DVB_CI_V", 8);
515 if (dvb_str == NULL)
516 return -EINVAL;
517 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
518 return -EINVAL;
520 /* is it a version we support? */
521 if (strncmp(dvb_str + 8, "1.00", 4)) {
522 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
523 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]);
524 return -EINVAL;
527 /* process the CFTABLE_ENTRY tuples, and any after those */
528 while ((!end_chain) && (address < 0x1000)) {
529 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
530 &tupleLength, tuple)) < 0)
531 return status;
532 switch (tupleType) {
533 case 0x1B: // CISTPL_CFTABLE_ENTRY
534 if (tupleLength < (2 + 11 + 17))
535 break;
537 /* if we've already parsed one, just use it */
538 if (got_cftableentry)
539 break;
541 /* get the config option */
542 ca->slot_info[slot].config_option = tuple[0] & 0x3f;
544 /* OK, check it contains the correct strings */
545 if ((findstr((char *)tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
546 (findstr((char *)tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
547 break;
549 got_cftableentry = 1;
550 break;
552 case 0x14: // CISTPL_NO_LINK
553 break;
555 case 0xFF: // CISTPL_END
556 end_chain = 1;
557 break;
559 default: /* Unknown tuple type - just skip this tuple and move to the next one */
560 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType,
561 tupleLength);
562 break;
566 if ((address > 0x1000) || (!got_cftableentry))
567 return -EINVAL;
569 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
570 manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option);
572 // success!
573 return 0;
578 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
580 * @ca: CA instance.
581 * @slot: Slot containing the CAM.
583 static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
585 int configoption;
587 dprintk("%s\n", __func__);
589 /* set the config option */
590 ca->pub->write_attribute_mem(ca->pub, slot,
591 ca->slot_info[slot].config_base,
592 ca->slot_info[slot].config_option);
594 /* check it */
595 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
596 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
597 ca->slot_info[slot].config_option, configoption & 0x3f);
599 /* fine! */
600 return 0;
606 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
607 * interface. It reads a buffer of data from the CAM. The data can either
608 * be stored in a supplied buffer, or automatically be added to the slot's
609 * rx_buffer.
611 * @ca: CA instance.
612 * @slot: Slot to read from.
613 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
614 * the data will be added into the buffering system as a normal fragment.
615 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
617 * @return Number of bytes read, or < 0 on error
619 static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount)
621 int bytes_read;
622 int status;
623 u8 buf[HOST_LINK_BUF_SIZE];
624 int i;
626 dprintk("%s\n", __func__);
628 /* check if we have space for a link buf in the rx_buffer */
629 if (ebuf == NULL) {
630 int buf_free;
632 if (ca->slot_info[slot].rx_buffer.data == NULL) {
633 status = -EIO;
634 goto exit;
636 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
638 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
639 status = -EAGAIN;
640 goto exit;
644 /* check if there is data available */
645 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
646 goto exit;
647 if (!(status & STATUSREG_DA)) {
648 /* no data */
649 status = 0;
650 goto exit;
653 /* read the amount of data */
654 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
655 goto exit;
656 bytes_read = status << 8;
657 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
658 goto exit;
659 bytes_read |= status;
661 /* check it will fit */
662 if (ebuf == NULL) {
663 if (bytes_read > ca->slot_info[slot].link_buf_size) {
664 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
665 ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size);
666 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
667 status = -EIO;
668 goto exit;
670 if (bytes_read < 2) {
671 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
672 ca->dvbdev->adapter->num);
673 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
674 status = -EIO;
675 goto exit;
677 } else {
678 if (bytes_read > ecount) {
679 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
680 ca->dvbdev->adapter->num);
681 status = -EIO;
682 goto exit;
686 /* fill the buffer */
687 for (i = 0; i < bytes_read; i++) {
688 /* read byte and check */
689 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
690 goto exit;
692 /* OK, store it in the buffer */
693 buf[i] = status;
696 /* check for read error (RE should now be 0) */
697 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
698 goto exit;
699 if (status & STATUSREG_RE) {
700 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
701 status = -EIO;
702 goto exit;
705 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
706 if (ebuf == NULL) {
707 if (ca->slot_info[slot].rx_buffer.data == NULL) {
708 status = -EIO;
709 goto exit;
711 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
712 } else {
713 memcpy(ebuf, buf, bytes_read);
716 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
717 buf[0], (buf[1] & 0x80) == 0, bytes_read);
719 /* wake up readers when a last_fragment is received */
720 if ((buf[1] & 0x80) == 0x00) {
721 wake_up_interruptible(&ca->wait_queue);
723 status = bytes_read;
725 exit:
726 return status;
731 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
732 * interface. It writes a buffer of data to a CAM.
734 * @ca: CA instance.
735 * @slot: Slot to write to.
736 * @ebuf: The data in this buffer is treated as a complete link-level packet to
737 * be written.
738 * @count: Size of ebuf.
740 * @return Number of bytes written, or < 0 on error.
742 static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write)
744 int status;
745 int i;
747 dprintk("%s\n", __func__);
750 /* sanity check */
751 if (bytes_write > ca->slot_info[slot].link_buf_size)
752 return -EINVAL;
754 /* it is possible we are dealing with a single buffer implementation,
755 thus if there is data available for read or if there is even a read
756 already in progress, we do nothing but awake the kernel thread to
757 process the data if necessary. */
758 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
759 goto exitnowrite;
760 if (status & (STATUSREG_DA | STATUSREG_RE)) {
761 if (status & STATUSREG_DA)
762 dvb_ca_en50221_thread_wakeup(ca);
764 status = -EAGAIN;
765 goto exitnowrite;
768 /* OK, set HC bit */
769 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
770 IRQEN | CMDREG_HC)) != 0)
771 goto exit;
773 /* check if interface is still free */
774 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
775 goto exit;
776 if (!(status & STATUSREG_FR)) {
777 /* it wasn't free => try again later */
778 status = -EAGAIN;
779 goto exit;
783 * It may need some time for the CAM to settle down, or there might
784 * be a race condition between the CAM, writing HC and our last
785 * check for DA. This happens, if the CAM asserts DA, just after
786 * checking DA before we are setting HC. In this case it might be
787 * a bug in the CAM to keep the FR bit, the lower layer/HW
788 * communication requires a longer timeout or the CAM needs more
789 * time internally. But this happens in reality!
790 * We need to read the status from the HW again and do the same
791 * we did for the previous check for DA
793 status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
794 if (status < 0)
795 goto exit;
797 if (status & (STATUSREG_DA | STATUSREG_RE)) {
798 if (status & STATUSREG_DA)
799 dvb_ca_en50221_thread_wakeup(ca);
801 status = -EAGAIN;
802 goto exit;
805 /* send the amount of data */
806 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
807 goto exit;
808 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
809 bytes_write & 0xff)) != 0)
810 goto exit;
812 /* send the buffer */
813 for (i = 0; i < bytes_write; i++) {
814 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
815 goto exit;
818 /* check for write error (WE should now be 0) */
819 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
820 goto exit;
821 if (status & STATUSREG_WE) {
822 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
823 status = -EIO;
824 goto exit;
826 status = bytes_write;
828 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
829 buf[0], (buf[1] & 0x80) == 0, bytes_write);
831 exit:
832 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
834 exitnowrite:
835 return status;
837 EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
841 /* ******************************************************************************** */
842 /* EN50221 higher level functions */
846 * dvb_ca_en50221_camready_irq - A CAM has been removed => shut it down.
848 * @ca: CA instance.
849 * @slot: Slot to shut down.
851 static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
853 dprintk("%s\n", __func__);
855 ca->pub->slot_shutdown(ca->pub, slot);
856 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
858 /* need to wake up all processes to check if they're now
859 trying to write to a defunct CAM */
860 wake_up_interruptible(&ca->wait_queue);
862 dprintk("Slot %i shutdown\n", slot);
864 /* success */
865 return 0;
867 EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
871 * dvb_ca_en50221_camready_irq - A CAMCHANGE IRQ has occurred.
873 * @ca: CA instance.
874 * @slot: Slot concerned.
875 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
877 void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
879 struct dvb_ca_private *ca = pubca->private;
881 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
883 switch (change_type) {
884 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
885 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
886 break;
888 default:
889 return;
892 ca->slot_info[slot].camchange_type = change_type;
893 atomic_inc(&ca->slot_info[slot].camchange_count);
894 dvb_ca_en50221_thread_wakeup(ca);
896 EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
900 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
902 * @ca: CA instance.
903 * @slot: Slot concerned.
905 void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
907 struct dvb_ca_private *ca = pubca->private;
909 dprintk("CAMREADY IRQ slot:%i\n", slot);
911 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
912 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
913 dvb_ca_en50221_thread_wakeup(ca);
919 * An FR or DA IRQ has occurred.
921 * @ca: CA instance.
922 * @slot: Slot concerned.
924 void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
926 struct dvb_ca_private *ca = pubca->private;
927 int flags;
929 dprintk("FR/DA IRQ slot:%i\n", slot);
931 switch (ca->slot_info[slot].slot_state) {
932 case DVB_CA_SLOTSTATE_LINKINIT:
933 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
934 if (flags & STATUSREG_DA) {
935 dprintk("CAM supports DA IRQ\n");
936 ca->slot_info[slot].da_irq_supported = 1;
938 break;
940 case DVB_CA_SLOTSTATE_RUNNING:
941 if (ca->open)
942 dvb_ca_en50221_thread_wakeup(ca);
943 break;
949 /* ******************************************************************************** */
950 /* EN50221 thread functions */
953 * Wake up the DVB CA thread
955 * @ca: CA instance.
957 static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
960 dprintk("%s\n", __func__);
962 ca->wakeup = 1;
963 mb();
964 wake_up_process(ca->thread);
968 * Update the delay used by the thread.
970 * @ca: CA instance.
972 static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
974 int delay;
975 int curdelay = 100000000;
976 int slot;
978 /* Beware of too high polling frequency, because one polling
979 * call might take several hundred milliseconds until timeout!
981 for (slot = 0; slot < ca->slot_count; slot++) {
982 switch (ca->slot_info[slot].slot_state) {
983 default:
984 case DVB_CA_SLOTSTATE_NONE:
985 delay = HZ * 60; /* 60s */
986 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
987 delay = HZ * 5; /* 5s */
988 break;
989 case DVB_CA_SLOTSTATE_INVALID:
990 delay = HZ * 60; /* 60s */
991 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
992 delay = HZ / 10; /* 100ms */
993 break;
995 case DVB_CA_SLOTSTATE_UNINITIALISED:
996 case DVB_CA_SLOTSTATE_WAITREADY:
997 case DVB_CA_SLOTSTATE_VALIDATE:
998 case DVB_CA_SLOTSTATE_WAITFR:
999 case DVB_CA_SLOTSTATE_LINKINIT:
1000 delay = HZ / 10; /* 100ms */
1001 break;
1003 case DVB_CA_SLOTSTATE_RUNNING:
1004 delay = HZ * 60; /* 60s */
1005 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
1006 delay = HZ / 10; /* 100ms */
1007 if (ca->open) {
1008 if ((!ca->slot_info[slot].da_irq_supported) ||
1009 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
1010 delay = HZ / 10; /* 100ms */
1012 break;
1015 if (delay < curdelay)
1016 curdelay = delay;
1019 ca->delay = curdelay;
1025 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
1027 static int dvb_ca_en50221_thread(void *data)
1029 struct dvb_ca_private *ca = data;
1030 int slot;
1031 int flags;
1032 int status;
1033 int pktcount;
1034 void *rxbuf;
1036 dprintk("%s\n", __func__);
1038 /* choose the correct initial delay */
1039 dvb_ca_en50221_thread_update_delay(ca);
1041 /* main loop */
1042 while (!kthread_should_stop()) {
1043 /* sleep for a bit */
1044 if (!ca->wakeup) {
1045 set_current_state(TASK_INTERRUPTIBLE);
1046 schedule_timeout(ca->delay);
1047 if (kthread_should_stop())
1048 return 0;
1050 ca->wakeup = 0;
1052 /* go through all the slots processing them */
1053 for (slot = 0; slot < ca->slot_count; slot++) {
1055 mutex_lock(&ca->slot_info[slot].slot_lock);
1057 // check the cam status + deal with CAMCHANGEs
1058 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1059 /* clear down an old CI slot if necessary */
1060 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
1061 dvb_ca_en50221_slot_shutdown(ca, slot);
1063 /* if a CAM is NOW present, initialise it */
1064 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
1065 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1068 /* we've handled one CAMCHANGE */
1069 dvb_ca_en50221_thread_update_delay(ca);
1070 atomic_dec(&ca->slot_info[slot].camchange_count);
1073 // CAM state machine
1074 switch (ca->slot_info[slot].slot_state) {
1075 case DVB_CA_SLOTSTATE_NONE:
1076 case DVB_CA_SLOTSTATE_INVALID:
1077 // no action needed
1078 break;
1080 case DVB_CA_SLOTSTATE_UNINITIALISED:
1081 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1082 ca->pub->slot_reset(ca->pub, slot);
1083 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1084 break;
1086 case DVB_CA_SLOTSTATE_WAITREADY:
1087 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1088 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1089 ca->dvbdev->adapter->num);
1090 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1091 dvb_ca_en50221_thread_update_delay(ca);
1092 break;
1094 // no other action needed; will automatically change state when ready
1095 break;
1097 case DVB_CA_SLOTSTATE_VALIDATE:
1098 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1099 /* we need this extra check for annoying interfaces like the budget-av */
1100 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1101 (ca->pub->poll_slot_status)) {
1102 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1103 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1104 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1105 dvb_ca_en50221_thread_update_delay(ca);
1106 break;
1110 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1111 ca->dvbdev->adapter->num);
1112 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1113 dvb_ca_en50221_thread_update_delay(ca);
1114 break;
1116 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1117 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1118 ca->dvbdev->adapter->num);
1119 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1120 dvb_ca_en50221_thread_update_delay(ca);
1121 break;
1123 if (ca->pub->write_cam_control(ca->pub, slot,
1124 CTRLIF_COMMAND, CMDREG_RS) != 0) {
1125 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1126 ca->dvbdev->adapter->num);
1127 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1128 dvb_ca_en50221_thread_update_delay(ca);
1129 break;
1131 dprintk("DVB CAM validated successfully\n");
1133 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1134 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
1135 ca->wakeup = 1;
1136 break;
1138 case DVB_CA_SLOTSTATE_WAITFR:
1139 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1140 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1141 ca->dvbdev->adapter->num);
1142 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1143 dvb_ca_en50221_thread_update_delay(ca);
1144 break;
1147 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1148 if (flags & STATUSREG_FR) {
1149 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1150 ca->wakeup = 1;
1152 break;
1154 case DVB_CA_SLOTSTATE_LINKINIT:
1155 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1156 /* we need this extra check for annoying interfaces like the budget-av */
1157 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1158 (ca->pub->poll_slot_status)) {
1159 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1160 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1161 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1162 dvb_ca_en50221_thread_update_delay(ca);
1163 break;
1167 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
1168 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1169 dvb_ca_en50221_thread_update_delay(ca);
1170 break;
1173 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1174 rxbuf = vmalloc(RX_BUFFER_SIZE);
1175 if (rxbuf == NULL) {
1176 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
1177 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1178 dvb_ca_en50221_thread_update_delay(ca);
1179 break;
1181 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
1184 ca->pub->slot_ts_enable(ca->pub, slot);
1185 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
1186 dvb_ca_en50221_thread_update_delay(ca);
1187 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num);
1188 break;
1190 case DVB_CA_SLOTSTATE_RUNNING:
1191 if (!ca->open)
1192 break;
1194 // poll slots for data
1195 pktcount = 0;
1196 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1197 if (!ca->open)
1198 break;
1200 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1201 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1202 // we dont want to sleep on the next iteration so we can handle the cam change
1203 ca->wakeup = 1;
1204 break;
1207 /* check if we've hit our limit this time */
1208 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1209 // dont sleep; there is likely to be more data to read
1210 ca->wakeup = 1;
1211 break;
1214 break;
1217 mutex_unlock(&ca->slot_info[slot].slot_lock);
1221 return 0;
1226 /* ******************************************************************************** */
1227 /* EN50221 IO interface functions */
1230 * Real ioctl implementation.
1231 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1233 * @inode: Inode concerned.
1234 * @file: File concerned.
1235 * @cmd: IOCTL command.
1236 * @arg: Associated argument.
1238 * @return 0 on success, <0 on error.
1240 static int dvb_ca_en50221_io_do_ioctl(struct file *file,
1241 unsigned int cmd, void *parg)
1243 struct dvb_device *dvbdev = file->private_data;
1244 struct dvb_ca_private *ca = dvbdev->priv;
1245 int err = 0;
1246 int slot;
1248 dprintk("%s\n", __func__);
1250 if (mutex_lock_interruptible(&ca->ioctl_mutex))
1251 return -ERESTARTSYS;
1253 switch (cmd) {
1254 case CA_RESET:
1255 for (slot = 0; slot < ca->slot_count; slot++) {
1256 mutex_lock(&ca->slot_info[slot].slot_lock);
1257 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
1258 dvb_ca_en50221_slot_shutdown(ca, slot);
1259 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1260 dvb_ca_en50221_camchange_irq(ca->pub,
1261 slot,
1262 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1264 mutex_unlock(&ca->slot_info[slot].slot_lock);
1266 ca->next_read_slot = 0;
1267 dvb_ca_en50221_thread_wakeup(ca);
1268 break;
1270 case CA_GET_CAP: {
1271 struct ca_caps *caps = parg;
1273 caps->slot_num = ca->slot_count;
1274 caps->slot_type = CA_CI_LINK;
1275 caps->descr_num = 0;
1276 caps->descr_type = 0;
1277 break;
1280 case CA_GET_SLOT_INFO: {
1281 struct ca_slot_info *info = parg;
1283 if ((info->num > ca->slot_count) || (info->num < 0)) {
1284 err = -EINVAL;
1285 goto out_unlock;
1288 info->type = CA_CI_LINK;
1289 info->flags = 0;
1290 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
1291 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1292 info->flags = CA_CI_MODULE_PRESENT;
1294 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1295 info->flags |= CA_CI_MODULE_READY;
1297 break;
1300 default:
1301 err = -EINVAL;
1302 break;
1305 out_unlock:
1306 mutex_unlock(&ca->ioctl_mutex);
1307 return err;
1312 * Wrapper for ioctl implementation.
1314 * @inode: Inode concerned.
1315 * @file: File concerned.
1316 * @cmd: IOCTL command.
1317 * @arg: Associated argument.
1319 * @return 0 on success, <0 on error.
1321 static long dvb_ca_en50221_io_ioctl(struct file *file,
1322 unsigned int cmd, unsigned long arg)
1324 return dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1329 * Implementation of write() syscall.
1331 * @file: File structure.
1332 * @buf: Source buffer.
1333 * @count: Size of source buffer.
1334 * @ppos: Position in file (ignored).
1336 * @return Number of bytes read, or <0 on error.
1338 static ssize_t dvb_ca_en50221_io_write(struct file *file,
1339 const char __user * buf, size_t count, loff_t * ppos)
1341 struct dvb_device *dvbdev = file->private_data;
1342 struct dvb_ca_private *ca = dvbdev->priv;
1343 u8 slot, connection_id;
1344 int status;
1345 u8 fragbuf[HOST_LINK_BUF_SIZE];
1346 int fragpos = 0;
1347 int fraglen;
1348 unsigned long timeout;
1349 int written;
1351 dprintk("%s\n", __func__);
1353 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1354 if (count < 2)
1355 return -EINVAL;
1357 /* extract slot & connection id */
1358 if (copy_from_user(&slot, buf, 1))
1359 return -EFAULT;
1360 if (copy_from_user(&connection_id, buf + 1, 1))
1361 return -EFAULT;
1362 buf += 2;
1363 count -= 2;
1365 /* check if the slot is actually running */
1366 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1367 return -EINVAL;
1369 /* fragment the packets & store in the buffer */
1370 while (fragpos < count) {
1371 fraglen = ca->slot_info[slot].link_buf_size - 2;
1372 if (fraglen < 0)
1373 break;
1374 if (fraglen > HOST_LINK_BUF_SIZE - 2)
1375 fraglen = HOST_LINK_BUF_SIZE - 2;
1376 if ((count - fragpos) < fraglen)
1377 fraglen = count - fragpos;
1379 fragbuf[0] = connection_id;
1380 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1381 status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
1382 if (status) {
1383 status = -EFAULT;
1384 goto exit;
1387 timeout = jiffies + HZ / 2;
1388 written = 0;
1389 while (!time_after(jiffies, timeout)) {
1390 /* check the CAM hasn't been removed/reset in the meantime */
1391 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1392 status = -EIO;
1393 goto exit;
1396 mutex_lock(&ca->slot_info[slot].slot_lock);
1397 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
1398 mutex_unlock(&ca->slot_info[slot].slot_lock);
1399 if (status == (fraglen + 2)) {
1400 written = 1;
1401 break;
1403 if (status != -EAGAIN)
1404 goto exit;
1406 msleep(1);
1408 if (!written) {
1409 status = -EIO;
1410 goto exit;
1413 fragpos += fraglen;
1415 status = count + 2;
1417 exit:
1418 return status;
1423 * Condition for waking up in dvb_ca_en50221_io_read_condition
1425 static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
1426 int *result, int *_slot)
1428 int slot;
1429 int slot_count = 0;
1430 int idx;
1431 size_t fraglen;
1432 int connection_id = -1;
1433 int found = 0;
1434 u8 hdr[2];
1436 slot = ca->next_read_slot;
1437 while ((slot_count < ca->slot_count) && (!found)) {
1438 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1439 goto nextslot;
1441 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1442 return 0;
1445 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1446 while (idx != -1) {
1447 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1448 if (connection_id == -1)
1449 connection_id = hdr[0];
1450 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
1451 *_slot = slot;
1452 found = 1;
1453 break;
1456 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1459 nextslot:
1460 slot = (slot + 1) % ca->slot_count;
1461 slot_count++;
1464 ca->next_read_slot = slot;
1465 return found;
1470 * Implementation of read() syscall.
1472 * @file: File structure.
1473 * @buf: Destination buffer.
1474 * @count: Size of destination buffer.
1475 * @ppos: Position in file (ignored).
1477 * @return Number of bytes read, or <0 on error.
1479 static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
1480 size_t count, loff_t * ppos)
1482 struct dvb_device *dvbdev = file->private_data;
1483 struct dvb_ca_private *ca = dvbdev->priv;
1484 int status;
1485 int result = 0;
1486 u8 hdr[2];
1487 int slot;
1488 int connection_id = -1;
1489 size_t idx, idx2;
1490 int last_fragment = 0;
1491 size_t fraglen;
1492 int pktlen;
1493 int dispose = 0;
1495 dprintk("%s\n", __func__);
1497 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1498 if (count < 2)
1499 return -EINVAL;
1501 /* wait for some data */
1502 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
1504 /* if we're in nonblocking mode, exit immediately */
1505 if (file->f_flags & O_NONBLOCK)
1506 return -EWOULDBLOCK;
1508 /* wait for some data */
1509 status = wait_event_interruptible(ca->wait_queue,
1510 dvb_ca_en50221_io_read_condition
1511 (ca, &result, &slot));
1513 if ((status < 0) || (result < 0)) {
1514 if (result)
1515 return result;
1516 return status;
1519 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1520 pktlen = 2;
1521 do {
1522 if (idx == -1) {
1523 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
1524 status = -EIO;
1525 goto exit;
1528 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1529 if (connection_id == -1)
1530 connection_id = hdr[0];
1531 if (hdr[0] == connection_id) {
1532 if (pktlen < count) {
1533 if ((pktlen + fraglen - 2) > count) {
1534 fraglen = count - pktlen;
1535 } else {
1536 fraglen -= 2;
1539 if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
1540 buf + pktlen, fraglen)) < 0) {
1541 goto exit;
1543 pktlen += fraglen;
1546 if ((hdr[1] & 0x80) == 0)
1547 last_fragment = 1;
1548 dispose = 1;
1551 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1552 if (dispose)
1553 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
1554 idx = idx2;
1555 dispose = 0;
1556 } while (!last_fragment);
1558 hdr[0] = slot;
1559 hdr[1] = connection_id;
1560 status = copy_to_user(buf, hdr, 2);
1561 if (status) {
1562 status = -EFAULT;
1563 goto exit;
1565 status = pktlen;
1567 exit:
1568 return status;
1573 * Implementation of file open syscall.
1575 * @inode: Inode concerned.
1576 * @file: File concerned.
1578 * @return 0 on success, <0 on failure.
1580 static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1582 struct dvb_device *dvbdev = file->private_data;
1583 struct dvb_ca_private *ca = dvbdev->priv;
1584 int err;
1585 int i;
1587 dprintk("%s\n", __func__);
1589 if (!try_module_get(ca->pub->owner))
1590 return -EIO;
1592 err = dvb_generic_open(inode, file);
1593 if (err < 0) {
1594 module_put(ca->pub->owner);
1595 return err;
1598 for (i = 0; i < ca->slot_count; i++) {
1600 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1601 if (ca->slot_info[i].rx_buffer.data != NULL) {
1602 /* it is safe to call this here without locks because
1603 * ca->open == 0. Data is not read in this case */
1604 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
1609 ca->open = 1;
1610 dvb_ca_en50221_thread_update_delay(ca);
1611 dvb_ca_en50221_thread_wakeup(ca);
1613 dvb_ca_private_get(ca);
1615 return 0;
1620 * Implementation of file close syscall.
1622 * @inode: Inode concerned.
1623 * @file: File concerned.
1625 * @return 0 on success, <0 on failure.
1627 static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1629 struct dvb_device *dvbdev = file->private_data;
1630 struct dvb_ca_private *ca = dvbdev->priv;
1631 int err;
1633 dprintk("%s\n", __func__);
1635 /* mark the CA device as closed */
1636 ca->open = 0;
1637 dvb_ca_en50221_thread_update_delay(ca);
1639 err = dvb_generic_release(inode, file);
1641 module_put(ca->pub->owner);
1643 dvb_ca_private_put(ca);
1645 return err;
1650 * Implementation of poll() syscall.
1652 * @file: File concerned.
1653 * @wait: poll wait table.
1655 * @return Standard poll mask.
1657 static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
1659 struct dvb_device *dvbdev = file->private_data;
1660 struct dvb_ca_private *ca = dvbdev->priv;
1661 unsigned int mask = 0;
1662 int slot;
1663 int result = 0;
1665 dprintk("%s\n", __func__);
1667 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1668 mask |= POLLIN;
1671 /* if there is something, return now */
1672 if (mask)
1673 return mask;
1675 /* wait for something to happen */
1676 poll_wait(file, &ca->wait_queue, wait);
1678 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1679 mask |= POLLIN;
1682 return mask;
1684 EXPORT_SYMBOL(dvb_ca_en50221_init);
1687 static const struct file_operations dvb_ca_fops = {
1688 .owner = THIS_MODULE,
1689 .read = dvb_ca_en50221_io_read,
1690 .write = dvb_ca_en50221_io_write,
1691 .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
1692 .open = dvb_ca_en50221_io_open,
1693 .release = dvb_ca_en50221_io_release,
1694 .poll = dvb_ca_en50221_io_poll,
1695 .llseek = noop_llseek,
1698 static const struct dvb_device dvbdev_ca = {
1699 .priv = NULL,
1700 .users = 1,
1701 .readers = 1,
1702 .writers = 1,
1703 #if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1704 .name = "dvb-ca-en50221",
1705 #endif
1706 .fops = &dvb_ca_fops,
1709 /* ******************************************************************************** */
1710 /* Initialisation/shutdown functions */
1714 * Initialise a new DVB CA EN50221 interface device.
1716 * @dvb_adapter: DVB adapter to attach the new CA device to.
1717 * @ca: The dvb_ca instance.
1718 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
1719 * @slot_count: Number of slots supported.
1721 * @return 0 on success, nonzero on failure
1723 int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1724 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1726 int ret;
1727 struct dvb_ca_private *ca = NULL;
1728 int i;
1730 dprintk("%s\n", __func__);
1732 if (slot_count < 1)
1733 return -EINVAL;
1735 /* initialise the system data */
1736 if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
1737 ret = -ENOMEM;
1738 goto exit;
1740 kref_init(&ca->refcount);
1741 ca->pub = pubca;
1742 ca->flags = flags;
1743 ca->slot_count = slot_count;
1744 if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
1745 ret = -ENOMEM;
1746 goto free_ca;
1748 init_waitqueue_head(&ca->wait_queue);
1749 ca->open = 0;
1750 ca->wakeup = 0;
1751 ca->next_read_slot = 0;
1752 pubca->private = ca;
1754 /* register the DVB device */
1755 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA, 0);
1756 if (ret)
1757 goto free_slot_info;
1759 /* now initialise each slot */
1760 for (i = 0; i < slot_count; i++) {
1761 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
1762 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
1763 atomic_set(&ca->slot_info[i].camchange_count, 0);
1764 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1765 mutex_init(&ca->slot_info[i].slot_lock);
1768 mutex_init(&ca->ioctl_mutex);
1770 if (signal_pending(current)) {
1771 ret = -EINTR;
1772 goto unregister_device;
1774 mb();
1776 /* create a kthread for monitoring this CA device */
1777 ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
1778 ca->dvbdev->adapter->num, ca->dvbdev->id);
1779 if (IS_ERR(ca->thread)) {
1780 ret = PTR_ERR(ca->thread);
1781 printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
1782 ret);
1783 goto unregister_device;
1785 return 0;
1787 unregister_device:
1788 dvb_unregister_device(ca->dvbdev);
1789 free_slot_info:
1790 kfree(ca->slot_info);
1791 free_ca:
1792 kfree(ca);
1793 exit:
1794 pubca->private = NULL;
1795 return ret;
1797 EXPORT_SYMBOL(dvb_ca_en50221_release);
1802 * Release a DVB CA EN50221 interface device.
1804 * @ca_dev: The dvb_device_t instance for the CA device.
1805 * @ca: The associated dvb_ca instance.
1807 void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1809 struct dvb_ca_private *ca = pubca->private;
1810 int i;
1812 dprintk("%s\n", __func__);
1814 /* shutdown the thread if there was one */
1815 kthread_stop(ca->thread);
1817 for (i = 0; i < ca->slot_count; i++) {
1818 dvb_ca_en50221_slot_shutdown(ca, i);
1820 dvb_ca_private_put(ca);
1821 pubca->private = NULL;