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