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