]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/media/dvb/dvb-core/dvb_ca_en50221.c
V4L/DVB (5837): stv0299: Fix signedness warning (gcc 4.1.1, kernel 2.6.22)
[mirror_ubuntu-bionic-kernel.git] / drivers / media / dvb / dvb-core / dvb_ca_en50221.c
CommitLineData
1da177e4
LT
1/*
2 * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces
3 *
4 * Copyright (C) 2004 Andrew de Quincey
5 *
6 * Parts of this file were based on sources as follows:
7 *
8 * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de>
9 *
10 * based on code:
11 *
12 * Copyright (C) 1999-2002 Ralph Metzler
13 * & Marcus Metzler for convergence integrated media GmbH
14 *
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.
19 *
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.
24 *
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
29 */
30
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>
ded92846 38#include <linux/spinlock.h>
4e57b681 39#include <linux/sched.h>
1da177e4
LT
40
41#include "dvb_ca_en50221.h"
42#include "dvb_ringbuffer.h"
43
44static int dvb_ca_en50221_debug;
45
46module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
47MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
48
49#define dprintk if (dvb_ca_en50221_debug) printk
50
50b447d5 51#define INIT_TIMEOUT_SECS 10
1da177e4
LT
52
53#define HOST_LINK_BUF_SIZE 0x200
54
55#define RX_BUFFER_SIZE 65535
56
57#define MAX_RX_PACKETS_PER_ITERATION 10
58
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
64
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)
72
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 */
78
79
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
88
89
90/* Information on a CA slot */
91struct dvb_ca_slot {
92
93 /* current state of the CAM */
94 int slot_state;
95
96 /* Number of CAMCHANGES that have occurred since last processing */
97 atomic_t camchange_count;
98
99 /* Type of last CAMCHANGE */
100 int camchange_type;
101
102 /* base address of CAM config */
103 u32 config_base;
104
105 /* value to write into Config Control register */
106 u8 config_option;
107
108 /* if 1, the CAM supports DA IRQs */
109 u8 da_irq_supported:1;
110
111 /* size of the buffer to use when talking to the CAM */
112 int link_buf_size;
113
1da177e4
LT
114 /* buffer for incoming packets */
115 struct dvb_ringbuffer rx_buffer;
116
117 /* timer used during various states of the slot */
118 unsigned long timeout;
119};
120
121/* Private CA-interface information */
122struct dvb_ca_private {
123
124 /* pointer back to the public data structure */
125 struct dvb_ca_en50221 *pub;
126
127 /* the DVB device */
128 struct dvb_device *dvbdev;
129
130 /* Flags describing the interface (DVB_CA_FLAG_*) */
131 u32 flags;
132
133 /* number of slots supported by this CA interface */
134 unsigned int slot_count;
135
136 /* information on each slot */
137 struct dvb_ca_slot *slot_info;
138
139 /* wait queues for read() and write() operations */
140 wait_queue_head_t wait_queue;
141
142 /* PID of the monitoring thread */
143 pid_t thread_pid;
144
145 /* Wait queue used when shutting thread down */
146 wait_queue_head_t thread_queue;
147
148 /* Flag indicating when thread should exit */
149 unsigned int exit:1;
150
151 /* Flag indicating if the CA device is open */
152 unsigned int open:1;
153
154 /* Flag indicating the thread should wake up now */
155 unsigned int wakeup:1;
156
157 /* Delay the main thread should use */
158 unsigned long delay;
159
160 /* Slot to start looking for data to read from in the next user-space read operation */
161 int next_read_slot;
162};
163
164static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
165static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
166static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
167
168
169/**
170 * Safely find needle in haystack.
171 *
172 * @param haystack Buffer to look in.
173 * @param hlen Number of bytes in haystack.
174 * @param needle Buffer to find.
175 * @param nlen Number of bytes in needle.
176 * @return Pointer into haystack needle was found at, or NULL if not found.
177 */
178static u8 *findstr(u8 * haystack, int hlen, u8 * needle, int nlen)
179{
180 int i;
181
182 if (hlen < nlen)
183 return NULL;
184
185 for (i = 0; i <= hlen - nlen; i++) {
186 if (!strncmp(haystack + i, needle, nlen))
187 return haystack + i;
188 }
189
190 return NULL;
191}
192
193
194
195/* ******************************************************************************** */
196/* EN50221 physical interface functions */
197
198
199/**
200 * Check CAM status.
201 */
202static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
203{
204 int slot_status;
205 int cam_present_now;
206 int cam_changed;
207
208 /* IRQ mode */
209 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
210 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
211 }
212
213 /* poll mode */
214 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
215
216 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
217 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
218 if (!cam_changed) {
219 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
220 cam_changed = (cam_present_now != cam_present_old);
221 }
222
223 if (cam_changed) {
224 if (!cam_present_now) {
225 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
226 } else {
227 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
228 }
229 atomic_set(&ca->slot_info[slot].camchange_count, 1);
230 } else {
231 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
232 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
233 // move to validate state if reset is completed
234 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
235 }
236 }
237
238 return cam_changed;
239}
240
241
242/**
243 * Wait for flags to become set on the STATUS register on a CAM interface,
244 * checking for errors and timeout.
245 *
246 * @param ca CA instance.
247 * @param slot Slot on interface.
248 * @param waitfor Flags to wait for.
249 * @param timeout_ms Timeout in milliseconds.
250 *
251 * @return 0 on success, nonzero on error.
252 */
253static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
254 u8 waitfor, int timeout_hz)
255{
256 unsigned long timeout;
257 unsigned long start;
258
259 dprintk("%s\n", __FUNCTION__);
260
261 /* loop until timeout elapsed */
262 start = jiffies;
263 timeout = jiffies + timeout_hz;
264 while (1) {
265 /* read the status and check for error */
266 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
267 if (res < 0)
268 return -EIO;
269
270 /* if we got the flags, it was successful! */
271 if (res & waitfor) {
272 dprintk("%s succeeded timeout:%lu\n", __FUNCTION__, jiffies - start);
273 return 0;
274 }
275
276 /* check for timeout */
277 if (time_after(jiffies, timeout)) {
278 break;
279 }
280
281 /* wait for a bit */
282 msleep(1);
283 }
284
285 dprintk("%s failed timeout:%lu\n", __FUNCTION__, jiffies - start);
286
287 /* if we get here, we've timed out */
288 return -ETIMEDOUT;
289}
290
291
292/**
293 * Initialise the link layer connection to a CAM.
294 *
295 * @param ca CA instance.
296 * @param slot Slot id.
297 *
298 * @return 0 on success, nonzero on failure.
299 */
300static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
301{
302 int ret;
303 int buf_size;
304 u8 buf[2];
305
306 dprintk("%s\n", __FUNCTION__);
307
308 /* we'll be determining these during this function */
309 ca->slot_info[slot].da_irq_supported = 0;
310
311 /* set the host link buffer size temporarily. it will be overwritten with the
312 * real negotiated size later. */
313 ca->slot_info[slot].link_buf_size = 2;
314
315 /* read the buffer size from the CAM */
316 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
317 return ret;
318 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
319 return ret;
320 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
321 return -EIO;
322 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
323 return ret;
324
325 /* store it, and choose the minimum of our buffer and the CAM's buffer size */
326 buf_size = (buf[0] << 8) | buf[1];
327 if (buf_size > HOST_LINK_BUF_SIZE)
328 buf_size = HOST_LINK_BUF_SIZE;
329 ca->slot_info[slot].link_buf_size = buf_size;
330 buf[0] = buf_size >> 8;
331 buf[1] = buf_size & 0xff;
332 dprintk("Chosen link buffer size of %i\n", buf_size);
333
334 /* write the buffer size to the CAM */
335 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
336 return ret;
337 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
338 return ret;
339 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
340 return -EIO;
341 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
342 return ret;
343
344 /* success */
345 return 0;
346}
347
348/**
349 * Read a tuple from attribute memory.
350 *
351 * @param ca CA instance.
352 * @param slot Slot id.
353 * @param address Address to read from. Updated.
354 * @param tupleType Tuple id byte. Updated.
355 * @param tupleLength Tuple length. Updated.
356 * @param tuple Dest buffer for tuple (must be 256 bytes). Updated.
357 *
358 * @return 0 on success, nonzero on error.
359 */
360static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
361 int *address, int *tupleType, int *tupleLength, u8 * tuple)
362{
363 int i;
364 int _tupleType;
365 int _tupleLength;
366 int _address = *address;
367
368 /* grab the next tuple length and type */
369 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
370 return _tupleType;
371 if (_tupleType == 0xff) {
372 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
373 *address += 2;
374 *tupleType = _tupleType;
375 *tupleLength = 0;
376 return 0;
377 }
378 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
379 return _tupleLength;
380 _address += 4;
381
382 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
383
384 /* read in the whole tuple */
385 for (i = 0; i < _tupleLength; i++) {
386 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2));
387 dprintk(" 0x%02x: 0x%02x %c\n",
388 i, tuple[i] & 0xff,
389 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
390 }
391 _address += (_tupleLength * 2);
392
393 // success
394 *tupleType = _tupleType;
395 *tupleLength = _tupleLength;
396 *address = _address;
397 return 0;
398}
399
400
401/**
402 * Parse attribute memory of a CAM module, extracting Config register, and checking
403 * it is a DVB CAM module.
404 *
405 * @param ca CA instance.
406 * @param slot Slot id.
407 *
408 * @return 0 on success, <0 on failure.
409 */
410static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
411{
412 int address = 0;
413 int tupleLength;
414 int tupleType;
415 u8 tuple[257];
416 char *dvb_str;
417 int rasz;
418 int status;
419 int got_cftableentry = 0;
420 int end_chain = 0;
421 int i;
422 u16 manfid = 0;
423 u16 devid = 0;
424
425
426 // CISTPL_DEVICE_0A
427 if ((status =
428 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
429 return status;
430 if (tupleType != 0x1D)
431 return -EINVAL;
432
433
434
435 // CISTPL_DEVICE_0C
436 if ((status =
437 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
438 return status;
439 if (tupleType != 0x1C)
440 return -EINVAL;
441
442
443
444 // CISTPL_VERS_1
445 if ((status =
446 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
447 return status;
448 if (tupleType != 0x15)
449 return -EINVAL;
450
451
452
453 // CISTPL_MANFID
454 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
455 &tupleLength, tuple)) < 0)
456 return status;
457 if (tupleType != 0x20)
458 return -EINVAL;
459 if (tupleLength != 4)
460 return -EINVAL;
461 manfid = (tuple[1] << 8) | tuple[0];
462 devid = (tuple[3] << 8) | tuple[2];
463
464
465
466 // CISTPL_CONFIG
467 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
468 &tupleLength, tuple)) < 0)
469 return status;
470 if (tupleType != 0x1A)
471 return -EINVAL;
472 if (tupleLength < 3)
473 return -EINVAL;
474
475 /* extract the configbase */
476 rasz = tuple[0] & 3;
477 if (tupleLength < (3 + rasz + 14))
478 return -EINVAL;
479 ca->slot_info[slot].config_base = 0;
480 for (i = 0; i < rasz + 1; i++) {
481 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i));
482 }
483
484 /* check it contains the correct DVB string */
485 dvb_str = findstr(tuple, tupleLength, "DVB_CI_V", 8);
486 if (dvb_str == NULL)
487 return -EINVAL;
488 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
489 return -EINVAL;
490
491 /* is it a version we support? */
492 if (strncmp(dvb_str + 8, "1.00", 4)) {
493 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
494 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]);
495 return -EINVAL;
496 }
497
498 /* process the CFTABLE_ENTRY tuples, and any after those */
499 while ((!end_chain) && (address < 0x1000)) {
500 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
afd1a0c9 501 &tupleLength, tuple)) < 0)
1da177e4
LT
502 return status;
503 switch (tupleType) {
504 case 0x1B: // CISTPL_CFTABLE_ENTRY
505 if (tupleLength < (2 + 11 + 17))
506 break;
507
508 /* if we've already parsed one, just use it */
509 if (got_cftableentry)
510 break;
511
512 /* get the config option */
513 ca->slot_info[slot].config_option = tuple[0] & 0x3f;
514
515 /* OK, check it contains the correct strings */
516 if ((findstr(tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
517 (findstr(tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
518 break;
519
520 got_cftableentry = 1;
521 break;
522
523 case 0x14: // CISTPL_NO_LINK
524 break;
525
526 case 0xFF: // CISTPL_END
527 end_chain = 1;
528 break;
529
530 default: /* Unknown tuple type - just skip this tuple and move to the next one */
531 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType,
532 tupleLength);
533 break;
534 }
535 }
536
537 if ((address > 0x1000) || (!got_cftableentry))
538 return -EINVAL;
539
540 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
541 manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option);
542
543 // success!
544 return 0;
545}
546
547
548/**
549 * Set CAM's configoption correctly.
550 *
551 * @param ca CA instance.
552 * @param slot Slot containing the CAM.
553 */
554static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
555{
556 int configoption;
557
558 dprintk("%s\n", __FUNCTION__);
559
560 /* set the config option */
561 ca->pub->write_attribute_mem(ca->pub, slot,
562 ca->slot_info[slot].config_base,
563 ca->slot_info[slot].config_option);
564
565 /* check it */
566 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
567 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
568 ca->slot_info[slot].config_option, configoption & 0x3f);
569
570 /* fine! */
571 return 0;
572
573}
574
575
576/**
577 * This function talks to an EN50221 CAM control interface. It reads a buffer of
578 * data from the CAM. The data can either be stored in a supplied buffer, or
579 * automatically be added to the slot's rx_buffer.
580 *
581 * @param ca CA instance.
582 * @param slot Slot to read from.
583 * @param ebuf If non-NULL, the data will be written to this buffer. If NULL,
584 * the data will be added into the buffering system as a normal fragment.
585 * @param ecount Size of ebuf. Ignored if ebuf is NULL.
586 *
587 * @return Number of bytes read, or < 0 on error
588 */
589static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount)
590{
591 int bytes_read;
592 int status;
593 u8 buf[HOST_LINK_BUF_SIZE];
594 int i;
595
596 dprintk("%s\n", __FUNCTION__);
597
598 /* check if we have space for a link buf in the rx_buffer */
599 if (ebuf == NULL) {
600 int buf_free;
601
1da177e4 602 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1da177e4
LT
603 status = -EIO;
604 goto exit;
605 }
606 buf_free = dvb_ringbuffer_free(&ca->slot_info[slot].rx_buffer);
1da177e4
LT
607
608 if (buf_free < (ca->slot_info[slot].link_buf_size + DVB_RINGBUFFER_PKTHDRSIZE)) {
609 status = -EAGAIN;
610 goto exit;
611 }
612 }
613
614 /* check if there is data available */
615 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
616 goto exit;
617 if (!(status & STATUSREG_DA)) {
618 /* no data */
619 status = 0;
620 goto exit;
621 }
622
623 /* read the amount of data */
624 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH)) < 0)
625 goto exit;
626 bytes_read = status << 8;
627 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW)) < 0)
628 goto exit;
629 bytes_read |= status;
630
631 /* check it will fit */
632 if (ebuf == NULL) {
633 if (bytes_read > ca->slot_info[slot].link_buf_size) {
634 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
635 ca->dvbdev->adapter->num, bytes_read, ca->slot_info[slot].link_buf_size);
636 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
637 status = -EIO;
638 goto exit;
639 }
640 if (bytes_read < 2) {
641 printk("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
642 ca->dvbdev->adapter->num);
643 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
644 status = -EIO;
645 goto exit;
646 }
647 } else {
648 if (bytes_read > ecount) {
649 printk("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
650 ca->dvbdev->adapter->num);
651 status = -EIO;
652 goto exit;
653 }
654 }
655
656 /* fill the buffer */
657 for (i = 0; i < bytes_read; i++) {
658 /* read byte and check */
659 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
660 goto exit;
661
662 /* OK, store it in the buffer */
663 buf[i] = status;
664 }
665
666 /* check for read error (RE should now be 0) */
667 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
668 goto exit;
669 if (status & STATUSREG_RE) {
670 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
671 status = -EIO;
672 goto exit;
673 }
674
675 /* OK, add it to the receive buffer, or copy into external buffer if supplied */
676 if (ebuf == NULL) {
1da177e4 677 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1da177e4
LT
678 status = -EIO;
679 goto exit;
680 }
681 dvb_ringbuffer_pkt_write(&ca->slot_info[slot].rx_buffer, buf, bytes_read);
1da177e4
LT
682 } else {
683 memcpy(ebuf, buf, bytes_read);
684 }
685
686 dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
687 buf[0], (buf[1] & 0x80) == 0, bytes_read);
688
689 /* wake up readers when a last_fragment is received */
690 if ((buf[1] & 0x80) == 0x00) {
691 wake_up_interruptible(&ca->wait_queue);
692 }
693 status = bytes_read;
694
695exit:
696 return status;
697}
698
699
700/**
701 * This function talks to an EN50221 CAM control interface. It writes a buffer of data
702 * to a CAM.
703 *
704 * @param ca CA instance.
705 * @param slot Slot to write to.
706 * @param ebuf The data in this buffer is treated as a complete link-level packet to
707 * be written.
708 * @param count Size of ebuf.
709 *
710 * @return Number of bytes written, or < 0 on error.
711 */
712static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * buf, int bytes_write)
713{
714 int status;
715 int i;
716
717 dprintk("%s\n", __FUNCTION__);
718
719
720 // sanity check
721 if (bytes_write > ca->slot_info[slot].link_buf_size)
722 return -EINVAL;
723
724 /* check if interface is actually waiting for us to read from it, or if a read is in progress */
725 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
726 goto exitnowrite;
727 if (status & (STATUSREG_DA | STATUSREG_RE)) {
728 status = -EAGAIN;
729 goto exitnowrite;
730 }
731
732 /* OK, set HC bit */
733 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
734 IRQEN | CMDREG_HC)) != 0)
735 goto exit;
736
737 /* check if interface is still free */
738 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
739 goto exit;
740 if (!(status & STATUSREG_FR)) {
741 /* it wasn't free => try again later */
742 status = -EAGAIN;
743 goto exit;
744 }
745
746 /* send the amount of data */
747 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
748 goto exit;
749 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
750 bytes_write & 0xff)) != 0)
751 goto exit;
752
753 /* send the buffer */
754 for (i = 0; i < bytes_write; i++) {
755 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
756 goto exit;
757 }
758
759 /* check for write error (WE should now be 0) */
760 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
761 goto exit;
762 if (status & STATUSREG_WE) {
763 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
764 status = -EIO;
765 goto exit;
766 }
767 status = bytes_write;
768
769 dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
770 buf[0], (buf[1] & 0x80) == 0, bytes_write);
771
772exit:
773 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
774
775exitnowrite:
776 return status;
777}
778EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
779
780
781
782/* ******************************************************************************** */
783/* EN50221 higher level functions */
784
785
786/**
787 * A CAM has been removed => shut it down.
788 *
789 * @param ca CA instance.
790 * @param slot Slot to shut down.
791 */
792static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
793{
794 dprintk("%s\n", __FUNCTION__);
795
1da177e4
LT
796 ca->pub->slot_shutdown(ca->pub, slot);
797 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1da177e4
LT
798
799 /* need to wake up all processes to check if they're now
800 trying to write to a defunct CAM */
801 wake_up_interruptible(&ca->wait_queue);
802
803 dprintk("Slot %i shutdown\n", slot);
804
805 /* success */
806 return 0;
807}
808EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
809
810
811/**
812 * A CAMCHANGE IRQ has occurred.
813 *
814 * @param ca CA instance.
815 * @param slot Slot concerned.
816 * @param change_type One of the DVB_CA_CAMCHANGE_* values.
817 */
818void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
819{
0c53c70f 820 struct dvb_ca_private *ca = pubca->private;
1da177e4
LT
821
822 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
823
824 switch (change_type) {
825 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
826 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
827 break;
828
829 default:
830 return;
831 }
832
833 ca->slot_info[slot].camchange_type = change_type;
834 atomic_inc(&ca->slot_info[slot].camchange_count);
835 dvb_ca_en50221_thread_wakeup(ca);
836}
837EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
838
839
840/**
841 * A CAMREADY IRQ has occurred.
842 *
843 * @param ca CA instance.
844 * @param slot Slot concerned.
845 */
846void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
847{
0c53c70f 848 struct dvb_ca_private *ca = pubca->private;
1da177e4
LT
849
850 dprintk("CAMREADY IRQ slot:%i\n", slot);
851
852 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
853 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
854 dvb_ca_en50221_thread_wakeup(ca);
855 }
856}
857
858
859/**
860 * An FR or DA IRQ has occurred.
861 *
862 * @param ca CA instance.
863 * @param slot Slot concerned.
864 */
865void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
866{
0c53c70f 867 struct dvb_ca_private *ca = pubca->private;
1da177e4
LT
868 int flags;
869
870 dprintk("FR/DA IRQ slot:%i\n", slot);
871
872 switch (ca->slot_info[slot].slot_state) {
873 case DVB_CA_SLOTSTATE_LINKINIT:
874 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
875 if (flags & STATUSREG_DA) {
876 dprintk("CAM supports DA IRQ\n");
877 ca->slot_info[slot].da_irq_supported = 1;
878 }
879 break;
880
881 case DVB_CA_SLOTSTATE_RUNNING:
882 if (ca->open)
ded92846 883 dvb_ca_en50221_thread_wakeup(ca);
1da177e4
LT
884 break;
885 }
886}
887
888
889
890/* ******************************************************************************** */
891/* EN50221 thread functions */
892
893/**
894 * Wake up the DVB CA thread
895 *
896 * @param ca CA instance.
897 */
898static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
899{
900
901 dprintk("%s\n", __FUNCTION__);
902
903 ca->wakeup = 1;
904 mb();
905 wake_up_interruptible(&ca->thread_queue);
906}
907
908/**
909 * Used by the CA thread to determine if an early wakeup is necessary
910 *
911 * @param ca CA instance.
912 */
913static int dvb_ca_en50221_thread_should_wakeup(struct dvb_ca_private *ca)
914{
915 if (ca->wakeup) {
916 ca->wakeup = 0;
917 return 1;
918 }
919 if (ca->exit)
920 return 1;
921
922 return 0;
923}
924
925
926/**
927 * Update the delay used by the thread.
928 *
929 * @param ca CA instance.
930 */
931static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
932{
933 int delay;
934 int curdelay = 100000000;
935 int slot;
936
937 for (slot = 0; slot < ca->slot_count; slot++) {
938 switch (ca->slot_info[slot].slot_state) {
939 default:
940 case DVB_CA_SLOTSTATE_NONE:
941 case DVB_CA_SLOTSTATE_INVALID:
942 delay = HZ * 60;
943 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
944 delay = HZ / 10;
945 }
946 break;
947
948 case DVB_CA_SLOTSTATE_UNINITIALISED:
949 case DVB_CA_SLOTSTATE_WAITREADY:
950 case DVB_CA_SLOTSTATE_VALIDATE:
951 case DVB_CA_SLOTSTATE_WAITFR:
952 case DVB_CA_SLOTSTATE_LINKINIT:
953 delay = HZ / 10;
954 break;
955
956 case DVB_CA_SLOTSTATE_RUNNING:
957 delay = HZ * 60;
958 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) {
959 delay = HZ / 10;
960 }
961 if (ca->open) {
962 if ((!ca->slot_info[slot].da_irq_supported) ||
963 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA))) {
964 delay = HZ / 10;
965 }
966 }
967 break;
968 }
969
970 if (delay < curdelay)
971 curdelay = delay;
972 }
973
974 ca->delay = curdelay;
975}
976
977
978
979/**
980 * Kernel thread which monitors CA slots for CAM changes, and performs data transfers.
981 */
982static int dvb_ca_en50221_thread(void *data)
983{
0c53c70f 984 struct dvb_ca_private *ca = data;
1da177e4
LT
985 char name[15];
986 int slot;
987 int flags;
988 int status;
989 int pktcount;
990 void *rxbuf;
991
992 dprintk("%s\n", __FUNCTION__);
993
994 /* setup kernel thread */
995 snprintf(name, sizeof(name), "kdvb-ca-%i:%i", ca->dvbdev->adapter->num, ca->dvbdev->id);
996
997 lock_kernel();
998 daemonize(name);
999 sigfillset(&current->blocked);
1000 unlock_kernel();
1001
1002 /* choose the correct initial delay */
1003 dvb_ca_en50221_thread_update_delay(ca);
1004
1005 /* main loop */
1006 while (!ca->exit) {
1007 /* sleep for a bit */
1008 if (!ca->wakeup) {
1009 flags = wait_event_interruptible_timeout(ca->thread_queue,
1010 dvb_ca_en50221_thread_should_wakeup(ca),
1011 ca->delay);
1012 if ((flags == -ERESTARTSYS) || ca->exit) {
1013 /* got signal or quitting */
1014 break;
1015 }
1016 }
1017 ca->wakeup = 0;
1018
1019 /* go through all the slots processing them */
1020 for (slot = 0; slot < ca->slot_count; slot++) {
1021
1022 // check the cam status + deal with CAMCHANGEs
1023 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1024 /* clear down an old CI slot if necessary */
1025 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
1026 dvb_ca_en50221_slot_shutdown(ca, slot);
1027
1028 /* if a CAM is NOW present, initialise it */
1029 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
1030 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1031 }
1032
1033 /* we've handled one CAMCHANGE */
1034 dvb_ca_en50221_thread_update_delay(ca);
1035 atomic_dec(&ca->slot_info[slot].camchange_count);
1036 }
1037
1038 // CAM state machine
1039 switch (ca->slot_info[slot].slot_state) {
1040 case DVB_CA_SLOTSTATE_NONE:
1041 case DVB_CA_SLOTSTATE_INVALID:
1042 // no action needed
1043 break;
1044
1045 case DVB_CA_SLOTSTATE_UNINITIALISED:
1046 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1047 ca->pub->slot_reset(ca->pub, slot);
1048 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1049 break;
1050
1051 case DVB_CA_SLOTSTATE_WAITREADY:
1052 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1053 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1054 ca->dvbdev->adapter->num);
1055 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1056 dvb_ca_en50221_thread_update_delay(ca);
1057 break;
1058 }
1059 // no other action needed; will automatically change state when ready
1060 break;
1061
1062 case DVB_CA_SLOTSTATE_VALIDATE:
5c1208ba
AQ
1063 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1064 /* we need this extra check for annoying interfaces like the budget-av */
1065 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1066 (ca->pub->poll_slot_status)) {
1067 int status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1068 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1069 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1070 dvb_ca_en50221_thread_update_delay(ca);
1071 break;
1072 }
1073 }
1074
1da177e4
LT
1075 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1076 ca->dvbdev->adapter->num);
1077 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1078 dvb_ca_en50221_thread_update_delay(ca);
1079 break;
1080 }
1081 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1082 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1083 ca->dvbdev->adapter->num);
1084 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1085 dvb_ca_en50221_thread_update_delay(ca);
1086 break;
1087 }
1088 if (ca->pub->write_cam_control(ca->pub, slot,
1089 CTRLIF_COMMAND, CMDREG_RS) != 0) {
1090 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1091 ca->dvbdev->adapter->num);
1092 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1093 dvb_ca_en50221_thread_update_delay(ca);
1094 break;
1095 }
1096 dprintk("DVB CAM validated successfully\n");
1097
1098 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1099 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
1100 ca->wakeup = 1;
1101 break;
1102
1103 case DVB_CA_SLOTSTATE_WAITFR:
1104 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1105 printk("dvb_ca adapter %d: DVB CAM did not respond :(\n",
1106 ca->dvbdev->adapter->num);
1107 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1108 dvb_ca_en50221_thread_update_delay(ca);
1109 break;
1110 }
1111
1112 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1113 if (flags & STATUSREG_FR) {
1114 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1115 ca->wakeup = 1;
1116 }
1117 break;
1118
1119 case DVB_CA_SLOTSTATE_LINKINIT:
1120 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
5c1208ba
AQ
1121 /* we need this extra check for annoying interfaces like the budget-av */
1122 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1123 (ca->pub->poll_slot_status)) {
1124 int status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1125 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1126 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1127 dvb_ca_en50221_thread_update_delay(ca);
1128 break;
1129 }
1130 }
1131
1da177e4
LT
1132 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
1133 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1134 dvb_ca_en50221_thread_update_delay(ca);
1135 break;
1136 }
1137
ded92846
AQ
1138 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1139 rxbuf = vmalloc(RX_BUFFER_SIZE);
1140 if (rxbuf == NULL) {
1141 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", 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;
1145 }
1146 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
1da177e4 1147 }
1da177e4
LT
1148
1149 ca->pub->slot_ts_enable(ca->pub, slot);
1150 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
1151 dvb_ca_en50221_thread_update_delay(ca);
1152 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num);
1153 break;
1154
1155 case DVB_CA_SLOTSTATE_RUNNING:
1156 if (!ca->open)
1157 continue;
1158
ded92846 1159 // poll slots for data
1da177e4
LT
1160 pktcount = 0;
1161 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1162 if (!ca->open)
1163 break;
1164
1165 /* if a CAMCHANGE occurred at some point, do not do any more processing of this slot */
1166 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1167 // we dont want to sleep on the next iteration so we can handle the cam change
1168 ca->wakeup = 1;
1169 break;
1170 }
1171
1172 /* check if we've hit our limit this time */
1173 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1174 // dont sleep; there is likely to be more data to read
1175 ca->wakeup = 1;
1176 break;
1177 }
1178 }
1179 break;
1180 }
1181 }
1182 }
1183
1184 /* completed */
1185 ca->thread_pid = 0;
1186 mb();
1187 wake_up_interruptible(&ca->thread_queue);
1188 return 0;
1189}
1190
1191
1192
1193/* ******************************************************************************** */
1194/* EN50221 IO interface functions */
1195
1196/**
1197 * Real ioctl implementation.
1198 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
1199 *
1200 * @param inode Inode concerned.
1201 * @param file File concerned.
1202 * @param cmd IOCTL command.
1203 * @param arg Associated argument.
1204 *
1205 * @return 0 on success, <0 on error.
1206 */
1207static int dvb_ca_en50221_io_do_ioctl(struct inode *inode, struct file *file,
1208 unsigned int cmd, void *parg)
1209{
0c53c70f
JS
1210 struct dvb_device *dvbdev = file->private_data;
1211 struct dvb_ca_private *ca = dvbdev->priv;
1da177e4
LT
1212 int err = 0;
1213 int slot;
1214
1215 dprintk("%s\n", __FUNCTION__);
1216
1217 switch (cmd) {
1218 case CA_RESET:
1219 for (slot = 0; slot < ca->slot_count; slot++) {
1220 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
1221 dvb_ca_en50221_slot_shutdown(ca, slot);
1222 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1223 dvb_ca_en50221_camchange_irq(ca->pub,
1224 slot,
1225 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1226 }
1227 }
1228 ca->next_read_slot = 0;
1229 dvb_ca_en50221_thread_wakeup(ca);
1230 break;
1231
1232 case CA_GET_CAP: {
0c53c70f 1233 struct ca_caps *caps = parg;
1da177e4
LT
1234
1235 caps->slot_num = ca->slot_count;
1236 caps->slot_type = CA_CI_LINK;
1237 caps->descr_num = 0;
1238 caps->descr_type = 0;
1239 break;
1240 }
1241
1242 case CA_GET_SLOT_INFO: {
0c53c70f 1243 struct ca_slot_info *info = parg;
1da177e4
LT
1244
1245 if ((info->num > ca->slot_count) || (info->num < 0))
1246 return -EINVAL;
1247
1248 info->type = CA_CI_LINK;
1249 info->flags = 0;
1250 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
1251 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1252 info->flags = CA_CI_MODULE_PRESENT;
1253 }
1254 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1255 info->flags |= CA_CI_MODULE_READY;
1256 }
1257 break;
1258 }
1259
1260 default:
1261 err = -EINVAL;
1262 break;
1263 }
1264
1265 return err;
1266}
1267
1268
1269/**
1270 * Wrapper for ioctl implementation.
1271 *
1272 * @param inode Inode concerned.
1273 * @param file File concerned.
1274 * @param cmd IOCTL command.
1275 * @param arg Associated argument.
1276 *
1277 * @return 0 on success, <0 on error.
1278 */
1279static int dvb_ca_en50221_io_ioctl(struct inode *inode, struct file *file,
1280 unsigned int cmd, unsigned long arg)
1281{
1282 return dvb_usercopy(inode, file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1283}
1284
1285
1286/**
1287 * Implementation of write() syscall.
1288 *
1289 * @param file File structure.
1290 * @param buf Source buffer.
1291 * @param count Size of source buffer.
1292 * @param ppos Position in file (ignored).
1293 *
1294 * @return Number of bytes read, or <0 on error.
1295 */
1296static ssize_t dvb_ca_en50221_io_write(struct file *file,
1297 const char __user * buf, size_t count, loff_t * ppos)
1298{
0c53c70f
JS
1299 struct dvb_device *dvbdev = file->private_data;
1300 struct dvb_ca_private *ca = dvbdev->priv;
1da177e4
LT
1301 u8 slot, connection_id;
1302 int status;
1303 char fragbuf[HOST_LINK_BUF_SIZE];
1304 int fragpos = 0;
1305 int fraglen;
1306 unsigned long timeout;
1307 int written;
1308
1309 dprintk("%s\n", __FUNCTION__);
1310
1311 /* Incoming packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1312 if (count < 2)
1313 return -EINVAL;
1314
1315 /* extract slot & connection id */
1316 if (copy_from_user(&slot, buf, 1))
1317 return -EFAULT;
1318 if (copy_from_user(&connection_id, buf + 1, 1))
1319 return -EFAULT;
1320 buf += 2;
1321 count -= 2;
1322
1323 /* check if the slot is actually running */
1324 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1325 return -EINVAL;
1326
1327 /* fragment the packets & store in the buffer */
1328 while (fragpos < count) {
1329 fraglen = ca->slot_info[slot].link_buf_size - 2;
1330 if ((count - fragpos) < fraglen)
1331 fraglen = count - fragpos;
1332
1333 fragbuf[0] = connection_id;
1334 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1335 if ((status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen)) != 0)
1336 goto exit;
1337
1338 timeout = jiffies + HZ / 2;
1339 written = 0;
1340 while (!time_after(jiffies, timeout)) {
1341 /* check the CAM hasn't been removed/reset in the meantime */
1342 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1343 status = -EIO;
1344 goto exit;
1345 }
1346
1347 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
1348 if (status == (fraglen + 2)) {
1349 written = 1;
1350 break;
1351 }
1352 if (status != -EAGAIN)
1353 goto exit;
1354
1355 msleep(1);
1356 }
1357 if (!written) {
1358 status = -EIO;
1359 goto exit;
1360 }
1361
1362 fragpos += fraglen;
1363 }
1364 status = count + 2;
1365
1366exit:
1367 return status;
1368}
1369
1370
1371/**
1372 * Condition for waking up in dvb_ca_en50221_io_read_condition
1373 */
ded92846
AQ
1374static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
1375 int *result, int *_slot)
1da177e4
LT
1376{
1377 int slot;
1378 int slot_count = 0;
1379 int idx;
ded92846 1380 size_t fraglen;
1da177e4
LT
1381 int connection_id = -1;
1382 int found = 0;
1383 u8 hdr[2];
1384
1385 slot = ca->next_read_slot;
1386 while ((slot_count < ca->slot_count) && (!found)) {
1387 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1388 goto nextslot;
1389
1da177e4 1390 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1da177e4
LT
1391 return 0;
1392 }
1393
1394 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1395 while (idx != -1) {
1396 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2, 0);
1397 if (connection_id == -1)
1398 connection_id = hdr[0];
1399 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
1400 *_slot = slot;
1401 found = 1;
1402 break;
1403 }
1404
1405 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1406 }
1407
ded92846 1408nextslot:
1da177e4
LT
1409 slot = (slot + 1) % ca->slot_count;
1410 slot_count++;
1411 }
1412
1413 ca->next_read_slot = slot;
1414 return found;
1415}
1416
1417
1418/**
1419 * Implementation of read() syscall.
1420 *
1421 * @param file File structure.
1422 * @param buf Destination buffer.
1423 * @param count Size of destination buffer.
1424 * @param ppos Position in file (ignored).
1425 *
1426 * @return Number of bytes read, or <0 on error.
1427 */
1428static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
1429 size_t count, loff_t * ppos)
1430{
0c53c70f
JS
1431 struct dvb_device *dvbdev = file->private_data;
1432 struct dvb_ca_private *ca = dvbdev->priv;
1da177e4
LT
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;
1443
1444 dprintk("%s\n", __FUNCTION__);
1445
1446 /* Outgoing packet has a 2 byte header. hdr[0] = slot_id, hdr[1] = connection_id */
1447 if (count < 2)
1448 return -EINVAL;
1449
1450 /* wait for some data */
1451 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
1452
1453 /* if we're in nonblocking mode, exit immediately */
1454 if (file->f_flags & O_NONBLOCK)
1455 return -EWOULDBLOCK;
1456
1457 /* wait for some data */
1458 status = wait_event_interruptible(ca->wait_queue,
1459 dvb_ca_en50221_io_read_condition
1460 (ca, &result, &slot));
1461 }
1462 if ((status < 0) || (result < 0)) {
1463 if (result)
1464 return result;
1465 return status;
1466 }
1467
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;
1475 }
1476
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;
1486 }
1487
1488 if ((status = dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 2,
1489 buf + pktlen, fraglen, 1)) < 0) {
1490 goto exit;
1491 }
1492 pktlen += fraglen;
1493 }
1494
1495 if ((hdr[1] & 0x80) == 0)
1496 last_fragment = 1;
1497 dispose = 1;
1498 }
1499
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);
1506
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;
1512
ded92846 1513exit:
1da177e4
LT
1514 return status;
1515}
1516
1517
1518/**
1519 * Implementation of file open syscall.
1520 *
1521 * @param inode Inode concerned.
1522 * @param file File concerned.
1523 *
1524 * @return 0 on success, <0 on failure.
1525 */
1526static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1527{
0c53c70f
JS
1528 struct dvb_device *dvbdev = file->private_data;
1529 struct dvb_ca_private *ca = dvbdev->priv;
1da177e4
LT
1530 int err;
1531 int i;
1532
1533 dprintk("%s\n", __FUNCTION__);
1534
1535 if (!try_module_get(ca->pub->owner))
1536 return -EIO;
1537
1538 err = dvb_generic_open(inode, file);
1539 if (err < 0)
1540 return err;
1541
1542 for (i = 0; i < ca->slot_count; i++) {
1543
1544 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1da177e4 1545 if (ca->slot_info[i].rx_buffer.data != NULL) {
ded92846
AQ
1546 /* it is safe to call this here without locks because
1547 * ca->open == 0. Data is not read in this case */
1da177e4
LT
1548 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
1549 }
1da177e4
LT
1550 }
1551 }
1552
1553 ca->open = 1;
1554 dvb_ca_en50221_thread_update_delay(ca);
1555 dvb_ca_en50221_thread_wakeup(ca);
1556
1557 return 0;
1558}
1559
1560
1561/**
1562 * Implementation of file close syscall.
1563 *
1564 * @param inode Inode concerned.
1565 * @param file File concerned.
1566 *
1567 * @return 0 on success, <0 on failure.
1568 */
1569static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1570{
0c53c70f
JS
1571 struct dvb_device *dvbdev = file->private_data;
1572 struct dvb_ca_private *ca = dvbdev->priv;
1da177e4
LT
1573 int err = 0;
1574
1575 dprintk("%s\n", __FUNCTION__);
1576
1577 /* mark the CA device as closed */
1578 ca->open = 0;
1579 dvb_ca_en50221_thread_update_delay(ca);
1580
1581 err = dvb_generic_release(inode, file);
1582
1583 module_put(ca->pub->owner);
1584
1585 return 0;
1586}
1587
1588
1589/**
1590 * Implementation of poll() syscall.
1591 *
1592 * @param file File concerned.
1593 * @param wait poll wait table.
1594 *
1595 * @return Standard poll mask.
1596 */
1597static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
1598{
0c53c70f
JS
1599 struct dvb_device *dvbdev = file->private_data;
1600 struct dvb_ca_private *ca = dvbdev->priv;
1da177e4
LT
1601 unsigned int mask = 0;
1602 int slot;
1603 int result = 0;
1604
1605 dprintk("%s\n", __FUNCTION__);
1606
1607 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1da177e4
LT
1608 mask |= POLLIN;
1609 }
1610
1611 /* if there is something, return now */
1612 if (mask)
1613 return mask;
1614
1615 /* wait for something to happen */
1616 poll_wait(file, &ca->wait_queue, wait);
1617
1618 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1da177e4
LT
1619 mask |= POLLIN;
1620 }
1621
1622 return mask;
1623}
1624EXPORT_SYMBOL(dvb_ca_en50221_init);
1625
1626
1627static struct file_operations dvb_ca_fops = {
1628 .owner = THIS_MODULE,
1629 .read = dvb_ca_en50221_io_read,
1630 .write = dvb_ca_en50221_io_write,
1631 .ioctl = dvb_ca_en50221_io_ioctl,
1632 .open = dvb_ca_en50221_io_open,
1633 .release = dvb_ca_en50221_io_release,
1634 .poll = dvb_ca_en50221_io_poll,
1635};
1636
1637static struct dvb_device dvbdev_ca = {
1638 .priv = NULL,
1639 .users = 1,
1640 .readers = 1,
1641 .writers = 1,
1642 .fops = &dvb_ca_fops,
1643};
1644
1645
1646/* ******************************************************************************** */
1647/* Initialisation/shutdown functions */
1648
1649
1650/**
1651 * Initialise a new DVB CA EN50221 interface device.
1652 *
1653 * @param dvb_adapter DVB adapter to attach the new CA device to.
1654 * @param ca The dvb_ca instance.
1655 * @param flags Flags describing the CA device (DVB_CA_FLAG_*).
1656 * @param slot_count Number of slots supported.
1657 *
1658 * @return 0 on success, nonzero on failure
1659 */
1660int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1661 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1662{
1663 int ret;
1664 struct dvb_ca_private *ca = NULL;
1665 int i;
1666
1667 dprintk("%s\n", __FUNCTION__);
1668
1669 if (slot_count < 1)
1670 return -EINVAL;
1671
1672 /* initialise the system data */
7408187d 1673 if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
1da177e4
LT
1674 ret = -ENOMEM;
1675 goto error;
1676 }
1da177e4
LT
1677 ca->pub = pubca;
1678 ca->flags = flags;
1679 ca->slot_count = slot_count;
7408187d 1680 if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
1da177e4
LT
1681 ret = -ENOMEM;
1682 goto error;
1683 }
1da177e4
LT
1684 init_waitqueue_head(&ca->wait_queue);
1685 ca->thread_pid = 0;
1686 init_waitqueue_head(&ca->thread_queue);
1687 ca->exit = 0;
1688 ca->open = 0;
1689 ca->wakeup = 0;
1690 ca->next_read_slot = 0;
1691 pubca->private = ca;
1692
1693 /* register the DVB device */
1694 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA);
1695 if (ret)
1696 goto error;
1697
1698 /* now initialise each slot */
1699 for (i = 0; i < slot_count; i++) {
1700 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
1701 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
1702 atomic_set(&ca->slot_info[i].camchange_count, 0);
1703 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1da177e4
LT
1704 }
1705
1706 if (signal_pending(current)) {
1707 ret = -EINTR;
1708 goto error;
1709 }
1710 mb();
1711
1712 /* create a kthread for monitoring this CA device */
1713
1714 ret = kernel_thread(dvb_ca_en50221_thread, ca, 0);
1715
1716 if (ret < 0) {
1717 printk("dvb_ca_init: failed to start kernel_thread (%d)\n", ret);
1718 goto error;
1719 }
1720 ca->thread_pid = ret;
1721 return 0;
1722
ded92846 1723error:
1da177e4
LT
1724 if (ca != NULL) {
1725 if (ca->dvbdev != NULL)
1726 dvb_unregister_device(ca->dvbdev);
1727 kfree(ca->slot_info);
1728 kfree(ca);
1729 }
1730 pubca->private = NULL;
1731 return ret;
1732}
1733EXPORT_SYMBOL(dvb_ca_en50221_release);
1734
1735
1736
1737/**
1738 * Release a DVB CA EN50221 interface device.
1739 *
1740 * @param ca_dev The dvb_device_t instance for the CA device.
1741 * @param ca The associated dvb_ca instance.
1742 */
1743void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1744{
0c53c70f 1745 struct dvb_ca_private *ca = pubca->private;
1da177e4
LT
1746 int i;
1747
1748 dprintk("%s\n", __FUNCTION__);
1749
1750 /* shutdown the thread if there was one */
1751 if (ca->thread_pid) {
1752 if (kill_proc(ca->thread_pid, 0, 1) == -ESRCH) {
1753 printk("dvb_ca_release adapter %d: thread PID %d already died\n",
1754 ca->dvbdev->adapter->num, ca->thread_pid);
1755 } else {
1756 ca->exit = 1;
1757 mb();
1758 dvb_ca_en50221_thread_wakeup(ca);
1759 wait_event_interruptible(ca->thread_queue, ca->thread_pid == 0);
1760 }
1761 }
1762
1763 for (i = 0; i < ca->slot_count; i++) {
1764 dvb_ca_en50221_slot_shutdown(ca, i);
35dc0fef 1765 vfree(ca->slot_info[i].rx_buffer.data);
1da177e4
LT
1766 }
1767 kfree(ca->slot_info);
1768 dvb_unregister_device(ca->dvbdev);
1769 kfree(ca);
1770 pubca->private = NULL;
1771}