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