dvb_ca_en50221.c 47.7 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * dvb_ca.c: generic DVB functions for EN50221 CAM interfaces
 *
 * Copyright (C) 2004 Andrew de Quincey
 *
 * Parts of this file were based on sources as follows:
 *
 * Copyright (C) 2003 Ralph Metzler <rjkm@metzlerbros.de>
 *
 * based on code:
 *
 * Copyright (C) 1999-2002 Ralph  Metzler
 *                       & Marcus Metzler for convergence integrated media GmbH
 */

17 18
#define pr_fmt(fmt) "dvb_ca_en50221: " fmt

L
Linus Torvalds 已提交
19 20 21 22
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/module.h>
23
#include <linux/nospec.h>
L
Linus Torvalds 已提交
24 25
#include <linux/vmalloc.h>
#include <linux/delay.h>
26
#include <linux/spinlock.h>
27
#include <linux/sched/signal.h>
28
#include <linux/kthread.h>
L
Linus Torvalds 已提交
29

30 31
#include <media/dvb_ca_en50221.h>
#include <media/dvb_ringbuffer.h>
L
Linus Torvalds 已提交
32 33 34 35 36 37

static int dvb_ca_en50221_debug;

module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");

38 39 40 41
#define dprintk(fmt, arg...) do {					\
	if (dvb_ca_en50221_debug)					\
		printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg);\
} while (0)
L
Linus Torvalds 已提交
42

43
#define INIT_TIMEOUT_SECS 10
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

#define HOST_LINK_BUF_SIZE 0x200

#define RX_BUFFER_SIZE 65535

#define MAX_RX_PACKETS_PER_ITERATION 10

#define CTRLIF_DATA      0
#define CTRLIF_COMMAND   1
#define CTRLIF_STATUS    1
#define CTRLIF_SIZE_LOW  2
#define CTRLIF_SIZE_HIGH 3

#define CMDREG_HC        1	/* Host control */
#define CMDREG_SW        2	/* Size write */
#define CMDREG_SR        4	/* Size read */
#define CMDREG_RS        8	/* Reset interface */
#define CMDREG_FRIE   0x40	/* Enable FR interrupt */
#define CMDREG_DAIE   0x80	/* Enable DA interrupt */
#define IRQEN (CMDREG_DAIE)

#define STATUSREG_RE     1	/* read error */
#define STATUSREG_WE     2	/* write error */
#define STATUSREG_FR  0x40	/* module free */
#define STATUSREG_DA  0x80	/* data available */

#define DVB_CA_SLOTSTATE_NONE           0
#define DVB_CA_SLOTSTATE_UNINITIALISED  1
#define DVB_CA_SLOTSTATE_RUNNING        2
#define DVB_CA_SLOTSTATE_INVALID        3
#define DVB_CA_SLOTSTATE_WAITREADY      4
#define DVB_CA_SLOTSTATE_VALIDATE       5
#define DVB_CA_SLOTSTATE_WAITFR         6
#define DVB_CA_SLOTSTATE_LINKINIT       7

/* Information on a CA slot */
struct dvb_ca_slot {
	/* current state of the CAM */
	int slot_state;

84 85 86
	/* mutex used for serializing access to one CI slot */
	struct mutex slot_lock;

L
Linus Torvalds 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	/* Number of CAMCHANGES that have occurred since last processing */
	atomic_t camchange_count;

	/* Type of last CAMCHANGE */
	int camchange_type;

	/* base address of CAM config */
	u32 config_base;

	/* value to write into Config Control register */
	u8 config_option;

	/* if 1, the CAM supports DA IRQs */
	u8 da_irq_supported:1;

	/* size of the buffer to use when talking to the CAM */
	int link_buf_size;

	/* buffer for incoming packets */
	struct dvb_ringbuffer rx_buffer;

	/* timer used during various states of the slot */
	unsigned long timeout;
};

/* Private CA-interface information */
struct dvb_ca_private {
114
	struct kref refcount;
L
Linus Torvalds 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

	/* pointer back to the public data structure */
	struct dvb_ca_en50221 *pub;

	/* the DVB device */
	struct dvb_device *dvbdev;

	/* Flags describing the interface (DVB_CA_FLAG_*) */
	u32 flags;

	/* number of slots supported by this CA interface */
	unsigned int slot_count;

	/* information on each slot */
	struct dvb_ca_slot *slot_info;

	/* wait queues for read() and write() operations */
	wait_queue_head_t wait_queue;

	/* PID of the monitoring thread */
135
	struct task_struct *thread;
L
Linus Torvalds 已提交
136 137 138 139 140 141 142 143 144 145

	/* Flag indicating if the CA device is open */
	unsigned int open:1;

	/* Flag indicating the thread should wake up now */
	unsigned int wakeup:1;

	/* Delay the main thread should use */
	unsigned long delay;

146 147 148 149
	/*
	 * Slot to start looking for data to read from in the next user-space
	 * read operation
	 */
L
Linus Torvalds 已提交
150
	int next_read_slot;
151 152 153

	/* mutex serializing ioctls */
	struct mutex ioctl_mutex;
154 155 156 157 158 159

	/* A mutex used when a device is disconnected */
	struct mutex remove_mutex;

	/* Whether the device is disconnected */
	int exit;
L
Linus Torvalds 已提交
160 161
};

162 163 164 165
static void dvb_ca_private_free(struct dvb_ca_private *ca)
{
	unsigned int i;

L
Lin Ma 已提交
166
	dvb_device_put(ca->dvbdev);
167 168 169 170 171 172 173
	for (i = 0; i < ca->slot_count; i++)
		vfree(ca->slot_info[i].rx_buffer.data);

	kfree(ca->slot_info);
	kfree(ca);
}

174 175
static void dvb_ca_private_release(struct kref *ref)
{
176 177 178
	struct dvb_ca_private *ca;

	ca = container_of(ref, struct dvb_ca_private, refcount);
179 180 181 182 183 184 185 186 187 188 189 190 191
	dvb_ca_private_free(ca);
}

static void dvb_ca_private_get(struct dvb_ca_private *ca)
{
	kref_get(&ca->refcount);
}

static void dvb_ca_private_put(struct dvb_ca_private *ca)
{
	kref_put(&ca->refcount, dvb_ca_private_release);
}

L
Linus Torvalds 已提交
192
static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
193 194 195
static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot,
				    u8 *ebuf, int ecount);
static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot,
196
				     u8 *ebuf, int ecount, int size_write_flag);
L
Linus Torvalds 已提交
197 198

/**
199
 * findstr - Safely find needle in haystack.
L
Linus Torvalds 已提交
200
 *
201 202 203 204
 * @haystack: Buffer to look in.
 * @hlen: Number of bytes in haystack.
 * @needle: Buffer to find.
 * @nlen: Number of bytes in needle.
205
 * return: Pointer into haystack needle was found at, or NULL if not found.
L
Linus Torvalds 已提交
206
 */
207
static char *findstr(char *haystack, int hlen, char *needle, int nlen)
L
Linus Torvalds 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221
{
	int i;

	if (hlen < nlen)
		return NULL;

	for (i = 0; i <= hlen - nlen; i++) {
		if (!strncmp(haystack + i, needle, nlen))
			return haystack + i;
	}

	return NULL;
}

222
/* ************************************************************************** */
L
Linus Torvalds 已提交
223 224
/* EN50221 physical interface functions */

225
/*
226
 * dvb_ca_en50221_check_camstatus - Check CAM status.
L
Linus Torvalds 已提交
227 228 229
 */
static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
{
230
	struct dvb_ca_slot *sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
231 232 233 234 235
	int slot_status;
	int cam_present_now;
	int cam_changed;

	/* IRQ mode */
236
	if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
237
		return (atomic_read(&sl->camchange_count) != 0);
L
Linus Torvalds 已提交
238 239 240 241 242 243 244

	/* poll mode */
	slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);

	cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
	cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
	if (!cam_changed) {
245 246
		int cam_present_old = (sl->slot_state != DVB_CA_SLOTSTATE_NONE);

L
Linus Torvalds 已提交
247 248 249 250
		cam_changed = (cam_present_now != cam_present_old);
	}

	if (cam_changed) {
251
		if (!cam_present_now)
252
			sl->camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
253
		else
254 255
			sl->camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
		atomic_set(&sl->camchange_count, 1);
L
Linus Torvalds 已提交
256
	} else {
257
		if ((sl->slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
L
Linus Torvalds 已提交
258
		    (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
259
			/* move to validate state if reset is completed */
260
			sl->slot_state = DVB_CA_SLOTSTATE_VALIDATE;
L
Linus Torvalds 已提交
261 262 263 264 265 266 267
		}
	}

	return cam_changed;
}

/**
268 269
 * dvb_ca_en50221_wait_if_status - Wait for flags to become set on the STATUS
 *	 register on a CAM interface, checking for errors and timeout.
L
Linus Torvalds 已提交
270
 *
271 272 273
 * @ca: CA instance.
 * @slot: Slot on interface.
 * @waitfor: Flags to wait for.
274
 * @timeout_hz: Timeout in milliseconds.
L
Linus Torvalds 已提交
275
 *
276
 * return: 0 on success, nonzero on error.
L
Linus Torvalds 已提交
277 278 279 280 281 282 283
 */
static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
					 u8 waitfor, int timeout_hz)
{
	unsigned long timeout;
	unsigned long start;

284
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
285 286 287 288 289

	/* loop until timeout elapsed */
	start = jiffies;
	timeout = jiffies + timeout_hz;
	while (1) {
290 291
		int res;

L
Linus Torvalds 已提交
292
		/* read the status and check for error */
293
		res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
L
Linus Torvalds 已提交
294 295 296 297 298
		if (res < 0)
			return -EIO;

		/* if we got the flags, it was successful! */
		if (res & waitfor) {
299 300
			dprintk("%s succeeded timeout:%lu\n",
				__func__, jiffies - start);
L
Linus Torvalds 已提交
301 302 303 304
			return 0;
		}

		/* check for timeout */
305
		if (time_after(jiffies, timeout))
L
Linus Torvalds 已提交
306 307 308
			break;

		/* wait for a bit */
309
		usleep_range(1000, 1100);
L
Linus Torvalds 已提交
310 311
	}

312
	dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
L
Linus Torvalds 已提交
313 314 315 316 317 318

	/* if we get here, we've timed out */
	return -ETIMEDOUT;
}

/**
319
 * dvb_ca_en50221_link_init - Initialise the link layer connection to a CAM.
L
Linus Torvalds 已提交
320
 *
321 322
 * @ca: CA instance.
 * @slot: Slot id.
L
Linus Torvalds 已提交
323
 *
324
 * return: 0 on success, nonzero on failure.
L
Linus Torvalds 已提交
325 326 327
 */
static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
{
328
	struct dvb_ca_slot *sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
329 330 331 332
	int ret;
	int buf_size;
	u8 buf[2];

333
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
334 335

	/* we'll be determining these during this function */
336
	sl->da_irq_supported = 0;
L
Linus Torvalds 已提交
337

338 339 340 341
	/*
	 * set the host link buffer size temporarily. it will be overwritten
	 * with the real negotiated size later.
	 */
342
	sl->link_buf_size = 2;
L
Linus Torvalds 已提交
343 344

	/* read the buffer size from the CAM */
345 346 347
	ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
					 IRQEN | CMDREG_SR);
	if (ret)
L
Linus Torvalds 已提交
348
		return ret;
349
	ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ);
350
	if (ret)
L
Linus Torvalds 已提交
351
		return ret;
352 353
	ret = dvb_ca_en50221_read_data(ca, slot, buf, 2);
	if (ret != 2)
L
Linus Torvalds 已提交
354
		return -EIO;
355 356
	ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
	if (ret)
L
Linus Torvalds 已提交
357 358
		return ret;

359 360 361 362
	/*
	 * store it, and choose the minimum of our buffer and the CAM's buffer
	 * size
	 */
L
Linus Torvalds 已提交
363 364 365
	buf_size = (buf[0] << 8) | buf[1];
	if (buf_size > HOST_LINK_BUF_SIZE)
		buf_size = HOST_LINK_BUF_SIZE;
366
	sl->link_buf_size = buf_size;
L
Linus Torvalds 已提交
367 368 369 370 371
	buf[0] = buf_size >> 8;
	buf[1] = buf_size & 0xff;
	dprintk("Chosen link buffer size of %i\n", buf_size);

	/* write the buffer size to the CAM */
372 373 374
	ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
					 IRQEN | CMDREG_SW);
	if (ret)
L
Linus Torvalds 已提交
375
		return ret;
376 377
	ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10);
	if (ret)
L
Linus Torvalds 已提交
378
		return ret;
379
	ret = dvb_ca_en50221_write_data(ca, slot, buf, 2, CMDREG_SW);
380
	if (ret != 2)
L
Linus Torvalds 已提交
381
		return -EIO;
382 383
	ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
	if (ret)
L
Linus Torvalds 已提交
384 385 386 387 388 389 390
		return ret;

	/* success */
	return 0;
}

/**
391
 * dvb_ca_en50221_read_tuple - Read a tuple from attribute memory.
L
Linus Torvalds 已提交
392
 *
393 394 395
 * @ca: CA instance.
 * @slot: Slot id.
 * @address: Address to read from. Updated.
396 397
 * @tuple_type: Tuple id byte. Updated.
 * @tuple_length: Tuple length. Updated.
398
 * @tuple: Dest buffer for tuple (must be 256 bytes). Updated.
L
Linus Torvalds 已提交
399
 *
400
 * return: 0 on success, nonzero on error.
L
Linus Torvalds 已提交
401 402
 */
static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
403 404
				     int *address, int *tuple_type,
				     int *tuple_length, u8 *tuple)
L
Linus Torvalds 已提交
405 406
{
	int i;
407 408
	int _tuple_type;
	int _tuple_length;
L
Linus Torvalds 已提交
409 410 411
	int _address = *address;

	/* grab the next tuple length and type */
412 413 414 415 416
	_tuple_type = ca->pub->read_attribute_mem(ca->pub, slot, _address);
	if (_tuple_type < 0)
		return _tuple_type;
	if (_tuple_type == 0xff) {
		dprintk("END OF CHAIN TUPLE type:0x%x\n", _tuple_type);
L
Linus Torvalds 已提交
417
		*address += 2;
418 419
		*tuple_type = _tuple_type;
		*tuple_length = 0;
L
Linus Torvalds 已提交
420 421
		return 0;
	}
422 423 424 425
	_tuple_length = ca->pub->read_attribute_mem(ca->pub, slot,
						    _address + 2);
	if (_tuple_length < 0)
		return _tuple_length;
L
Linus Torvalds 已提交
426 427
	_address += 4;

428
	dprintk("TUPLE type:0x%x length:%i\n", _tuple_type, _tuple_length);
L
Linus Torvalds 已提交
429 430

	/* read in the whole tuple */
431
	for (i = 0; i < _tuple_length; i++) {
432 433
		tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot,
						       _address + (i * 2));
L
Linus Torvalds 已提交
434 435 436 437
		dprintk("  0x%02x: 0x%02x %c\n",
			i, tuple[i] & 0xff,
			((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
	}
438
	_address += (_tuple_length * 2);
L
Linus Torvalds 已提交
439

440
	/* success */
441 442
	*tuple_type = _tuple_type;
	*tuple_length = _tuple_length;
L
Linus Torvalds 已提交
443 444 445 446 447
	*address = _address;
	return 0;
}

/**
448 449
 * dvb_ca_en50221_parse_attributes - Parse attribute memory of a CAM module,
 *	extracting Config register, and checking it is a DVB CAM module.
L
Linus Torvalds 已提交
450
 *
451 452
 * @ca: CA instance.
 * @slot: Slot id.
L
Linus Torvalds 已提交
453
 *
454
 * return: 0 on success, <0 on failure.
L
Linus Torvalds 已提交
455 456 457
 */
static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
{
458
	struct dvb_ca_slot *sl;
L
Linus Torvalds 已提交
459
	int address = 0;
460 461
	int tuple_length;
	int tuple_type;
L
Linus Torvalds 已提交
462 463 464 465 466 467 468 469 470 471
	u8 tuple[257];
	char *dvb_str;
	int rasz;
	int status;
	int got_cftableentry = 0;
	int end_chain = 0;
	int i;
	u16 manfid = 0;
	u16 devid = 0;

472
	/* CISTPL_DEVICE_0A */
473 474 475
	status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
					   &tuple_length, tuple);
	if (status < 0)
L
Linus Torvalds 已提交
476
		return status;
477
	if (tuple_type != 0x1D)
L
Linus Torvalds 已提交
478 479
		return -EINVAL;

480
	/* CISTPL_DEVICE_0C */
481 482 483
	status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
					   &tuple_length, tuple);
	if (status < 0)
L
Linus Torvalds 已提交
484
		return status;
485
	if (tuple_type != 0x1C)
L
Linus Torvalds 已提交
486 487
		return -EINVAL;

488
	/* CISTPL_VERS_1 */
489 490 491
	status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
					   &tuple_length, tuple);
	if (status < 0)
L
Linus Torvalds 已提交
492
		return status;
493
	if (tuple_type != 0x15)
L
Linus Torvalds 已提交
494 495
		return -EINVAL;

496
	/* CISTPL_MANFID */
497 498 499
	status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
					   &tuple_length, tuple);
	if (status < 0)
L
Linus Torvalds 已提交
500
		return status;
501
	if (tuple_type != 0x20)
L
Linus Torvalds 已提交
502
		return -EINVAL;
503
	if (tuple_length != 4)
L
Linus Torvalds 已提交
504 505 506 507
		return -EINVAL;
	manfid = (tuple[1] << 8) | tuple[0];
	devid = (tuple[3] << 8) | tuple[2];

508
	/* CISTPL_CONFIG */
509 510 511
	status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tuple_type,
					   &tuple_length, tuple);
	if (status < 0)
L
Linus Torvalds 已提交
512
		return status;
513
	if (tuple_type != 0x1A)
L
Linus Torvalds 已提交
514
		return -EINVAL;
515
	if (tuple_length < 3)
L
Linus Torvalds 已提交
516 517 518 519
		return -EINVAL;

	/* extract the configbase */
	rasz = tuple[0] & 3;
520
	if (tuple_length < (3 + rasz + 14))
L
Linus Torvalds 已提交
521
		return -EINVAL;
522 523 524 525
	sl = &ca->slot_info[slot];
	sl->config_base = 0;
	for (i = 0; i < rasz + 1; i++)
		sl->config_base |= (tuple[2 + i] << (8 * i));
L
Linus Torvalds 已提交
526 527

	/* check it contains the correct DVB string */
528
	dvb_str = findstr((char *)tuple, tuple_length, "DVB_CI_V", 8);
529
	if (!dvb_str)
L
Linus Torvalds 已提交
530
		return -EINVAL;
531
	if (tuple_length < ((dvb_str - (char *)tuple) + 12))
L
Linus Torvalds 已提交
532 533 534 535
		return -EINVAL;

	/* is it a version we support? */
	if (strncmp(dvb_str + 8, "1.00", 4)) {
536 537 538
		pr_err("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
		       ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9],
		       dvb_str[10], dvb_str[11]);
L
Linus Torvalds 已提交
539 540 541 542 543
		return -EINVAL;
	}

	/* process the CFTABLE_ENTRY tuples, and any after those */
	while ((!end_chain) && (address < 0x1000)) {
544 545 546 547
		status = dvb_ca_en50221_read_tuple(ca, slot, &address,
						   &tuple_type, &tuple_length,
						   tuple);
		if (status < 0)
L
Linus Torvalds 已提交
548
			return status;
549
		switch (tuple_type) {
550
		case 0x1B:	/* CISTPL_CFTABLE_ENTRY */
551
			if (tuple_length < (2 + 11 + 17))
L
Linus Torvalds 已提交
552 553 554 555 556 557 558
				break;

			/* if we've already parsed one, just use it */
			if (got_cftableentry)
				break;

			/* get the config option */
559
			sl->config_option = tuple[0] & 0x3f;
L
Linus Torvalds 已提交
560 561

			/* OK, check it contains the correct strings */
562 563 564 565
			if (!findstr((char *)tuple, tuple_length,
				     "DVB_HOST", 8) ||
			    !findstr((char *)tuple, tuple_length,
				     "DVB_CI_MODULE", 13))
L
Linus Torvalds 已提交
566 567 568 569 570
				break;

			got_cftableentry = 1;
			break;

571
		case 0x14:	/* CISTPL_NO_LINK */
L
Linus Torvalds 已提交
572 573
			break;

574
		case 0xFF:	/* CISTPL_END */
L
Linus Torvalds 已提交
575 576 577
			end_chain = 1;
			break;

578
		default:	/* Unknown tuple type - just skip this tuple */
579
			dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n",
580
				tuple_type, tuple_length);
L
Linus Torvalds 已提交
581 582 583 584 585 586 587 588
			break;
		}
	}

	if ((address > 0x1000) || (!got_cftableentry))
		return -EINVAL;

	dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
589
		manfid, devid, sl->config_base, sl->config_option);
L
Linus Torvalds 已提交
590

591
	/* success! */
L
Linus Torvalds 已提交
592 593 594 595
	return 0;
}

/**
596
 * dvb_ca_en50221_set_configoption - Set CAM's configoption correctly.
L
Linus Torvalds 已提交
597
 *
598 599
 * @ca: CA instance.
 * @slot: Slot containing the CAM.
L
Linus Torvalds 已提交
600 601 602
 */
static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
{
603
	struct dvb_ca_slot *sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
604 605
	int configoption;

606
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
607 608

	/* set the config option */
609 610
	ca->pub->write_attribute_mem(ca->pub, slot, sl->config_base,
				     sl->config_option);
L
Linus Torvalds 已提交
611 612

	/* check it */
613 614
	configoption = ca->pub->read_attribute_mem(ca->pub, slot,
						   sl->config_base);
L
Linus Torvalds 已提交
615
	dprintk("Set configoption 0x%x, read configoption 0x%x\n",
616
		sl->config_option, configoption & 0x3f);
L
Linus Torvalds 已提交
617 618 619 620 621 622

	/* fine! */
	return 0;
}

/**
623 624 625 626
 * dvb_ca_en50221_read_data - This function talks to an EN50221 CAM control
 *	interface. It reads a buffer of data from the CAM. The data can either
 *	be stored in a supplied buffer, or automatically be added to the slot's
 *	rx_buffer.
L
Linus Torvalds 已提交
627
 *
628 629 630
 * @ca: CA instance.
 * @slot: Slot to read from.
 * @ebuf: If non-NULL, the data will be written to this buffer. If NULL,
631 632
 *	  the data will be added into the buffering system as a normal
 *	  fragment.
633
 * @ecount: Size of ebuf. Ignored if ebuf is NULL.
L
Linus Torvalds 已提交
634
 *
635
 * return: Number of bytes read, or < 0 on error
L
Linus Torvalds 已提交
636
 */
637 638
static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot,
				    u8 *ebuf, int ecount)
L
Linus Torvalds 已提交
639
{
640
	struct dvb_ca_slot *sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
641 642 643 644 645
	int bytes_read;
	int status;
	u8 buf[HOST_LINK_BUF_SIZE];
	int i;

646
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
647 648

	/* check if we have space for a link buf in the rx_buffer */
649
	if (!ebuf) {
L
Linus Torvalds 已提交
650 651
		int buf_free;

652
		if (!sl->rx_buffer.data) {
L
Linus Torvalds 已提交
653 654 655
			status = -EIO;
			goto exit;
		}
656
		buf_free = dvb_ringbuffer_free(&sl->rx_buffer);
L
Linus Torvalds 已提交
657

658
		if (buf_free < (sl->link_buf_size +
659
				DVB_RINGBUFFER_PKTHDRSIZE)) {
L
Linus Torvalds 已提交
660 661 662 663 664
			status = -EAGAIN;
			goto exit;
		}
	}

665
	if (ca->pub->read_data &&
666
	    (sl->slot_state != DVB_CA_SLOTSTATE_LINKINIT)) {
667
		if (!ebuf)
668 669 670 671 672 673 674 675 676 677 678 679 680 681
			status = ca->pub->read_data(ca->pub, slot, buf,
						    sizeof(buf));
		else
			status = ca->pub->read_data(ca->pub, slot, buf, ecount);
		if (status < 0)
			return status;
		bytes_read =  status;
		if (status == 0)
			goto exit;
	} else {
		/* check if there is data available */
		status = ca->pub->read_cam_control(ca->pub, slot,
						   CTRLIF_STATUS);
		if (status < 0)
L
Linus Torvalds 已提交
682
			goto exit;
683 684 685
		if (!(status & STATUSREG_DA)) {
			/* no data */
			status = 0;
L
Linus Torvalds 已提交
686 687
			goto exit;
		}
688 689 690 691 692 693 694 695 696 697

		/* read the amount of data */
		status = ca->pub->read_cam_control(ca->pub, slot,
						   CTRLIF_SIZE_HIGH);
		if (status < 0)
			goto exit;
		bytes_read = status << 8;
		status = ca->pub->read_cam_control(ca->pub, slot,
						   CTRLIF_SIZE_LOW);
		if (status < 0)
L
Linus Torvalds 已提交
698
			goto exit;
699 700 701
		bytes_read |= status;

		/* check it will fit */
702
		if (!ebuf) {
703
			if (bytes_read > sl->link_buf_size) {
704 705
				pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the link buffer size (%i > %i)!\n",
				       ca->dvbdev->adapter->num, bytes_read,
706 707
				       sl->link_buf_size);
				sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
708 709 710 711 712 713
				status = -EIO;
				goto exit;
			}
			if (bytes_read < 2) {
				pr_err("dvb_ca adapter %d: CAM sent a buffer that was less than 2 bytes!\n",
				       ca->dvbdev->adapter->num);
714
				sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
715 716 717 718 719 720 721 722 723 724
				status = -EIO;
				goto exit;
			}
		} else {
			if (bytes_read > ecount) {
				pr_err("dvb_ca adapter %d: CAM tried to send a buffer larger than the ecount size!\n",
				       ca->dvbdev->adapter->num);
				status = -EIO;
				goto exit;
			}
L
Linus Torvalds 已提交
725 726
		}

727 728 729 730 731 732 733
		/* fill the buffer */
		for (i = 0; i < bytes_read; i++) {
			/* read byte and check */
			status = ca->pub->read_cam_control(ca->pub, slot,
							   CTRLIF_DATA);
			if (status < 0)
				goto exit;
L
Linus Torvalds 已提交
734

735 736 737
			/* OK, store it in the buffer */
			buf[i] = status;
		}
L
Linus Torvalds 已提交
738

739 740 741 742 743 744
		/* check for read error (RE should now be 0) */
		status = ca->pub->read_cam_control(ca->pub, slot,
						   CTRLIF_STATUS);
		if (status < 0)
			goto exit;
		if (status & STATUSREG_RE) {
745
			sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
746 747 748
			status = -EIO;
			goto exit;
		}
L
Linus Torvalds 已提交
749 750
	}

751 752 753 754
	/*
	 * OK, add it to the receive buffer, or copy into external buffer if
	 * supplied
	 */
755
	if (!ebuf) {
756
		if (!sl->rx_buffer.data) {
L
Linus Torvalds 已提交
757 758 759
			status = -EIO;
			goto exit;
		}
760
		dvb_ringbuffer_pkt_write(&sl->rx_buffer, buf, bytes_read);
L
Linus Torvalds 已提交
761 762 763 764 765 766 767 768
	} else {
		memcpy(ebuf, buf, bytes_read);
	}

	dprintk("Received CA packet for slot %i connection id 0x%x last_frag:%i size:0x%x\n", slot,
		buf[0], (buf[1] & 0x80) == 0, bytes_read);

	/* wake up readers when a last_fragment is received */
769
	if ((buf[1] & 0x80) == 0x00)
L
Linus Torvalds 已提交
770
		wake_up_interruptible(&ca->wait_queue);
771

L
Linus Torvalds 已提交
772 773 774 775 776 777 778
	status = bytes_read;

exit:
	return status;
}

/**
779 780
 * dvb_ca_en50221_write_data - This function talks to an EN50221 CAM control
 *				interface. It writes a buffer of data to a CAM.
L
Linus Torvalds 已提交
781
 *
782 783
 * @ca: CA instance.
 * @slot: Slot to write to.
784
 * @buf: The data in this buffer is treated as a complete link-level packet to
785
 *	 be written.
786
 * @bytes_write: Size of ebuf.
787 788
 * @size_write_flag: A flag on Command Register which says whether the link size
 * information will be writen or not.
L
Linus Torvalds 已提交
789
 *
790
 * return: Number of bytes written, or < 0 on error.
L
Linus Torvalds 已提交
791
 */
792
static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot,
793
				     u8 *buf, int bytes_write, int size_write_flag)
L
Linus Torvalds 已提交
794
{
795
	struct dvb_ca_slot *sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
796 797 798
	int status;
	int i;

799
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
800

801
	/* sanity check */
802
	if (bytes_write > sl->link_buf_size)
L
Linus Torvalds 已提交
803 804
		return -EINVAL;

805
	if (ca->pub->write_data &&
806
	    (sl->slot_state != DVB_CA_SLOTSTATE_LINKINIT))
807 808
		return ca->pub->write_data(ca->pub, slot, buf, bytes_write);

809 810 811 812 813 814
	/*
	 * it is possible we are dealing with a single buffer implementation,
	 * thus if there is data available for read or if there is even a read
	 * already in progress, we do nothing but awake the kernel thread to
	 * process the data if necessary.
	 */
815 816
	status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
	if (status < 0)
L
Linus Torvalds 已提交
817 818
		goto exitnowrite;
	if (status & (STATUSREG_DA | STATUSREG_RE)) {
819 820 821
		if (status & STATUSREG_DA)
			dvb_ca_en50221_thread_wakeup(ca);

L
Linus Torvalds 已提交
822 823 824 825 826
		status = -EAGAIN;
		goto exitnowrite;
	}

	/* OK, set HC bit */
827
	status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
828
					    IRQEN | CMDREG_HC | size_write_flag);
829
	if (status)
L
Linus Torvalds 已提交
830 831 832
		goto exit;

	/* check if interface is still free */
833 834
	status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
	if (status < 0)
L
Linus Torvalds 已提交
835 836 837 838 839 840 841
		goto exit;
	if (!(status & STATUSREG_FR)) {
		/* it wasn't free => try again later */
		status = -EAGAIN;
		goto exit;
	}

842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
	/*
	 * It may need some time for the CAM to settle down, or there might
	 * be a race condition between the CAM, writing HC and our last
	 * check for DA. This happens, if the CAM asserts DA, just after
	 * checking DA before we are setting HC. In this case it might be
	 * a bug in the CAM to keep the FR bit, the lower layer/HW
	 * communication requires a longer timeout or the CAM needs more
	 * time internally. But this happens in reality!
	 * We need to read the status from the HW again and do the same
	 * we did for the previous check for DA
	 */
	status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
	if (status < 0)
		goto exit;

	if (status & (STATUSREG_DA | STATUSREG_RE)) {
		if (status & STATUSREG_DA)
			dvb_ca_en50221_thread_wakeup(ca);

		status = -EAGAIN;
		goto exit;
	}

L
Linus Torvalds 已提交
865
	/* send the amount of data */
866 867 868
	status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH,
					    bytes_write >> 8);
	if (status)
L
Linus Torvalds 已提交
869
		goto exit;
870 871 872
	status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
					    bytes_write & 0xff);
	if (status)
L
Linus Torvalds 已提交
873 874 875 876
		goto exit;

	/* send the buffer */
	for (i = 0; i < bytes_write; i++) {
877 878 879
		status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA,
						    buf[i]);
		if (status)
L
Linus Torvalds 已提交
880 881 882 883
			goto exit;
	}

	/* check for write error (WE should now be 0) */
884 885
	status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
	if (status < 0)
L
Linus Torvalds 已提交
886 887
		goto exit;
	if (status & STATUSREG_WE) {
888
		sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
L
Linus Torvalds 已提交
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
		status = -EIO;
		goto exit;
	}
	status = bytes_write;

	dprintk("Wrote CA packet for slot %i, connection id 0x%x last_frag:%i size:0x%x\n", slot,
		buf[0], (buf[1] & 0x80) == 0, bytes_write);

exit:
	ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);

exitnowrite:
	return status;
}

904
/* ************************************************************************** */
L
Linus Torvalds 已提交
905 906 907
/* EN50221 higher level functions */

/**
908
 * dvb_ca_en50221_slot_shutdown - A CAM has been removed => shut it down.
L
Linus Torvalds 已提交
909
 *
910 911
 * @ca: CA instance.
 * @slot: Slot to shut down.
L
Linus Torvalds 已提交
912 913 914
 */
static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
{
915
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
916 917 918 919

	ca->pub->slot_shutdown(ca->pub, slot);
	ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;

920 921 922 923
	/*
	 * need to wake up all processes to check if they're now trying to
	 * write to a defunct CAM
	 */
L
Linus Torvalds 已提交
924 925 926 927 928 929 930 931 932
	wake_up_interruptible(&ca->wait_queue);

	dprintk("Slot %i shutdown\n", slot);

	/* success */
	return 0;
}

/**
933
 * dvb_ca_en50221_camchange_irq - A CAMCHANGE IRQ has occurred.
L
Linus Torvalds 已提交
934
 *
935
 * @pubca: CA instance.
936 937
 * @slot: Slot concerned.
 * @change_type: One of the DVB_CA_CAMCHANGE_* values.
L
Linus Torvalds 已提交
938
 */
939 940
void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot,
				  int change_type)
L
Linus Torvalds 已提交
941
{
942
	struct dvb_ca_private *ca = pubca->private;
943
	struct dvb_ca_slot *sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
944 945 946 947 948 949 950 951 952 953 954 955

	dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);

	switch (change_type) {
	case DVB_CA_EN50221_CAMCHANGE_REMOVED:
	case DVB_CA_EN50221_CAMCHANGE_INSERTED:
		break;

	default:
		return;
	}

956 957
	sl->camchange_type = change_type;
	atomic_inc(&sl->camchange_count);
L
Linus Torvalds 已提交
958 959
	dvb_ca_en50221_thread_wakeup(ca);
}
960
EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
L
Linus Torvalds 已提交
961 962

/**
963
 * dvb_ca_en50221_camready_irq - A CAMREADY IRQ has occurred.
L
Linus Torvalds 已提交
964
 *
965
 * @pubca: CA instance.
966
 * @slot: Slot concerned.
L
Linus Torvalds 已提交
967 968 969
 */
void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
{
970
	struct dvb_ca_private *ca = pubca->private;
971
	struct dvb_ca_slot *sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
972 973 974

	dprintk("CAMREADY IRQ slot:%i\n", slot);

975 976
	if (sl->slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
		sl->slot_state = DVB_CA_SLOTSTATE_VALIDATE;
L
Linus Torvalds 已提交
977 978 979
		dvb_ca_en50221_thread_wakeup(ca);
	}
}
980
EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
L
Linus Torvalds 已提交
981 982

/**
983
 * dvb_ca_en50221_frda_irq - An FR or DA IRQ has occurred.
L
Linus Torvalds 已提交
984
 *
985
 * @pubca: CA instance.
986
 * @slot: Slot concerned.
L
Linus Torvalds 已提交
987 988 989
 */
void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
{
990
	struct dvb_ca_private *ca = pubca->private;
991
	struct dvb_ca_slot *sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
992 993 994 995
	int flags;

	dprintk("FR/DA IRQ slot:%i\n", slot);

996
	switch (sl->slot_state) {
L
Linus Torvalds 已提交
997 998 999 1000
	case DVB_CA_SLOTSTATE_LINKINIT:
		flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
		if (flags & STATUSREG_DA) {
			dprintk("CAM supports DA IRQ\n");
1001
			sl->da_irq_supported = 1;
L
Linus Torvalds 已提交
1002 1003 1004 1005 1006
		}
		break;

	case DVB_CA_SLOTSTATE_RUNNING:
		if (ca->open)
1007
			dvb_ca_en50221_thread_wakeup(ca);
L
Linus Torvalds 已提交
1008 1009 1010
		break;
	}
}
1011
EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
L
Linus Torvalds 已提交
1012

1013
/* ************************************************************************** */
L
Linus Torvalds 已提交
1014 1015 1016
/* EN50221 thread functions */

/**
1017
 * dvb_ca_en50221_thread_wakeup - Wake up the DVB CA thread
L
Linus Torvalds 已提交
1018
 *
1019
 * @ca: CA instance.
L
Linus Torvalds 已提交
1020 1021 1022
 */
static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
{
1023
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1024 1025 1026

	ca->wakeup = 1;
	mb();
1027
	wake_up_process(ca->thread);
L
Linus Torvalds 已提交
1028 1029 1030
}

/**
1031
 * dvb_ca_en50221_thread_update_delay - Update the delay used by the thread.
L
Linus Torvalds 已提交
1032
 *
1033
 * @ca: CA instance.
L
Linus Torvalds 已提交
1034 1035 1036 1037 1038 1039 1040
 */
static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
{
	int delay;
	int curdelay = 100000000;
	int slot;

1041 1042
	/*
	 * Beware of too high polling frequency, because one polling
1043 1044
	 * call might take several hundred milliseconds until timeout!
	 */
L
Linus Torvalds 已提交
1045
	for (slot = 0; slot < ca->slot_count; slot++) {
1046 1047 1048
		struct dvb_ca_slot *sl = &ca->slot_info[slot];

		switch (sl->slot_state) {
L
Linus Torvalds 已提交
1049 1050
		default:
		case DVB_CA_SLOTSTATE_NONE:
1051 1052 1053 1054
			delay = HZ * 60;  /* 60s */
			if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
				delay = HZ * 5;  /* 5s */
			break;
L
Linus Torvalds 已提交
1055
		case DVB_CA_SLOTSTATE_INVALID:
1056 1057 1058
			delay = HZ * 60;  /* 60s */
			if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
				delay = HZ / 10;  /* 100ms */
L
Linus Torvalds 已提交
1059 1060 1061 1062 1063 1064 1065
			break;

		case DVB_CA_SLOTSTATE_UNINITIALISED:
		case DVB_CA_SLOTSTATE_WAITREADY:
		case DVB_CA_SLOTSTATE_VALIDATE:
		case DVB_CA_SLOTSTATE_WAITFR:
		case DVB_CA_SLOTSTATE_LINKINIT:
1066
			delay = HZ / 10;  /* 100ms */
L
Linus Torvalds 已提交
1067 1068 1069
			break;

		case DVB_CA_SLOTSTATE_RUNNING:
1070 1071 1072
			delay = HZ * 60;  /* 60s */
			if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
				delay = HZ / 10;  /* 100ms */
L
Linus Torvalds 已提交
1073
			if (ca->open) {
1074
				if ((!sl->da_irq_supported) ||
1075 1076
				    (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
					delay = HZ / 10;  /* 100ms */
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
			}
			break;
		}

		if (delay < curdelay)
			curdelay = delay;
	}

	ca->delay = curdelay;
}

1088
/**
1089
 * dvb_ca_en50221_poll_cam_gone - Poll if the CAM is gone.
1090 1091 1092
 *
 * @ca: CA instance.
 * @slot: Slot to process.
1093
 * return:: 0 .. no change
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
 *          1 .. CAM state changed
 */

static int dvb_ca_en50221_poll_cam_gone(struct dvb_ca_private *ca, int slot)
{
	int changed = 0;
	int status;

	/*
	 * we need this extra check for annoying interfaces like the
	 * budget-av
	 */
	if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
	    (ca->pub->poll_slot_status)) {
		status = ca->pub->poll_slot_status(ca->pub, slot, 0);
		if (!(status &
			DVB_CA_EN50221_POLL_CAM_PRESENT)) {
			ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
			dvb_ca_en50221_thread_update_delay(ca);
			changed = 1;
		}
	}
	return changed;
}

L
Linus Torvalds 已提交
1119
/**
1120 1121
 * dvb_ca_en50221_thread_state_machine - Thread state machine for one CA slot
 *	to perform the data transfer.
1122 1123 1124
 *
 * @ca: CA instance.
 * @slot: Slot to process.
L
Linus Torvalds 已提交
1125
 */
1126 1127
static void dvb_ca_en50221_thread_state_machine(struct dvb_ca_private *ca,
						int slot)
L
Linus Torvalds 已提交
1128
{
1129
	struct dvb_ca_slot *sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
1130 1131 1132 1133
	int flags;
	int pktcount;
	void *rxbuf;

1134
	mutex_lock(&sl->slot_lock);
L
Linus Torvalds 已提交
1135

1136 1137 1138 1139 1140
	/* check the cam status + deal with CAMCHANGEs */
	while (dvb_ca_en50221_check_camstatus(ca, slot)) {
		/* clear down an old CI slot if necessary */
		if (sl->slot_state != DVB_CA_SLOTSTATE_NONE)
			dvb_ca_en50221_slot_shutdown(ca, slot);
L
Linus Torvalds 已提交
1141

1142 1143 1144
		/* if a CAM is NOW present, initialise it */
		if (sl->camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED)
			sl->slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
L
Linus Torvalds 已提交
1145

1146 1147 1148 1149
		/* we've handled one CAMCHANGE */
		dvb_ca_en50221_thread_update_delay(ca);
		atomic_dec(&sl->camchange_count);
	}
L
Linus Torvalds 已提交
1150

1151 1152 1153 1154 1155 1156
	/* CAM state machine */
	switch (sl->slot_state) {
	case DVB_CA_SLOTSTATE_NONE:
	case DVB_CA_SLOTSTATE_INVALID:
		/* no action needed */
		break;
L
Linus Torvalds 已提交
1157

1158 1159 1160 1161 1162
	case DVB_CA_SLOTSTATE_UNINITIALISED:
		sl->slot_state = DVB_CA_SLOTSTATE_WAITREADY;
		ca->pub->slot_reset(ca->pub, slot);
		sl->timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
		break;
L
Linus Torvalds 已提交
1163

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
	case DVB_CA_SLOTSTATE_WAITREADY:
		if (time_after(jiffies, sl->timeout)) {
			pr_err("dvb_ca adaptor %d: PC card did not respond :(\n",
			       ca->dvbdev->adapter->num);
			sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
			dvb_ca_en50221_thread_update_delay(ca);
			break;
		}
		/*
		 * no other action needed; will automatically change state when
		 * ready
		 */
		break;
L
Linus Torvalds 已提交
1177

1178 1179
	case DVB_CA_SLOTSTATE_VALIDATE:
		if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1180 1181
			if (dvb_ca_en50221_poll_cam_gone(ca, slot))
				break;
L
Linus Torvalds 已提交
1182

1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
			pr_err("dvb_ca adapter %d: Invalid PC card inserted :(\n",
			       ca->dvbdev->adapter->num);
			sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
			dvb_ca_en50221_thread_update_delay(ca);
			break;
		}
		if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
			pr_err("dvb_ca adapter %d: Unable to initialise CAM :(\n",
			       ca->dvbdev->adapter->num);
			sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
			dvb_ca_en50221_thread_update_delay(ca);
			break;
		}
		if (ca->pub->write_cam_control(ca->pub, slot,
					       CTRLIF_COMMAND,
					       CMDREG_RS) != 0) {
			pr_err("dvb_ca adapter %d: Unable to reset CAM IF\n",
			       ca->dvbdev->adapter->num);
			sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
			dvb_ca_en50221_thread_update_delay(ca);
			break;
		}
		dprintk("DVB CAM validated successfully\n");
L
Linus Torvalds 已提交
1206

1207 1208 1209 1210
		sl->timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
		sl->slot_state = DVB_CA_SLOTSTATE_WAITFR;
		ca->wakeup = 1;
		break;
L
Linus Torvalds 已提交
1211

1212 1213 1214 1215 1216 1217 1218 1219
	case DVB_CA_SLOTSTATE_WAITFR:
		if (time_after(jiffies, sl->timeout)) {
			pr_err("dvb_ca adapter %d: DVB CAM did not respond :(\n",
			       ca->dvbdev->adapter->num);
			sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
			dvb_ca_en50221_thread_update_delay(ca);
			break;
		}
L
Linus Torvalds 已提交
1220

1221 1222 1223 1224 1225 1226
		flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
		if (flags & STATUSREG_FR) {
			sl->slot_state = DVB_CA_SLOTSTATE_LINKINIT;
			ca->wakeup = 1;
		}
		break;
L
Linus Torvalds 已提交
1227

1228 1229
	case DVB_CA_SLOTSTATE_LINKINIT:
		if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1230 1231
			if (dvb_ca_en50221_poll_cam_gone(ca, slot))
				break;
L
Linus Torvalds 已提交
1232

1233 1234 1235 1236 1237 1238
			pr_err("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n",
			       ca->dvbdev->adapter->num);
			sl->slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
			dvb_ca_en50221_thread_update_delay(ca);
			break;
		}
L
Linus Torvalds 已提交
1239

1240 1241 1242 1243
		if (!sl->rx_buffer.data) {
			rxbuf = vmalloc(RX_BUFFER_SIZE);
			if (!rxbuf) {
				pr_err("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n",
1244
				       ca->dvbdev->adapter->num);
1245 1246
				sl->slot_state = DVB_CA_SLOTSTATE_INVALID;
				dvb_ca_en50221_thread_update_delay(ca);
L
Linus Torvalds 已提交
1247
				break;
1248 1249 1250 1251
			}
			dvb_ringbuffer_init(&sl->rx_buffer, rxbuf,
					    RX_BUFFER_SIZE);
		}
L
Linus Torvalds 已提交
1252

1253 1254 1255
		ca->pub->slot_ts_enable(ca->pub, slot);
		sl->slot_state = DVB_CA_SLOTSTATE_RUNNING;
		dvb_ca_en50221_thread_update_delay(ca);
1256 1257
		pr_info("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n",
			ca->dvbdev->adapter->num);
1258
		break;
L
Linus Torvalds 已提交
1259

1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
	case DVB_CA_SLOTSTATE_RUNNING:
		if (!ca->open)
			break;

		/* poll slots for data */
		pktcount = 0;
		while (dvb_ca_en50221_read_data(ca, slot, NULL, 0) > 0) {
			if (!ca->open)
				break;

			/*
			 * if a CAMCHANGE occurred at some point, do not do any
			 * more processing of this slot
			 */
			if (dvb_ca_en50221_check_camstatus(ca, slot)) {
				/*
1276
				 * we don't want to sleep on the next iteration
1277 1278 1279
				 * so we can handle the cam change
				 */
				ca->wakeup = 1;
L
Linus Torvalds 已提交
1280 1281
				break;
			}
1282

1283 1284 1285
			/* check if we've hit our limit this time */
			if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
				/*
1286
				 * don't sleep; there is likely to be more data
1287 1288 1289 1290 1291
				 * to read
				 */
				ca->wakeup = 1;
				break;
			}
L
Linus Torvalds 已提交
1292
		}
1293 1294 1295 1296 1297 1298
		break;
	}

	mutex_unlock(&sl->slot_lock);
}

1299
/*
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326
 * Kernel thread which monitors CA slots for CAM changes, and performs data
 * transfers.
 */
static int dvb_ca_en50221_thread(void *data)
{
	struct dvb_ca_private *ca = data;
	int slot;

	dprintk("%s\n", __func__);

	/* choose the correct initial delay */
	dvb_ca_en50221_thread_update_delay(ca);

	/* main loop */
	while (!kthread_should_stop()) {
		/* sleep for a bit */
		if (!ca->wakeup) {
			set_current_state(TASK_INTERRUPTIBLE);
			schedule_timeout(ca->delay);
			if (kthread_should_stop())
				return 0;
		}
		ca->wakeup = 0;

		/* go through all the slots processing them */
		for (slot = 0; slot < ca->slot_count; slot++)
			dvb_ca_en50221_thread_state_machine(ca, slot);
L
Linus Torvalds 已提交
1327 1328 1329 1330 1331
	}

	return 0;
}

1332
/* ************************************************************************** */
L
Linus Torvalds 已提交
1333 1334 1335
/* EN50221 IO interface functions */

/**
1336
 * dvb_ca_en50221_io_do_ioctl - Real ioctl implementation.
L
Linus Torvalds 已提交
1337
 *
1338 1339
 * @file: File concerned.
 * @cmd: IOCTL command.
1340
 * @parg: Associated argument.
L
Linus Torvalds 已提交
1341
 *
1342 1343
 * NOTE: CA_SEND_MSG/CA_GET_MSG ioctls have userspace buffers passed to them.
 *
1344
 * return: 0 on success, <0 on error.
L
Linus Torvalds 已提交
1345
 */
1346
static int dvb_ca_en50221_io_do_ioctl(struct file *file,
L
Linus Torvalds 已提交
1347 1348
				      unsigned int cmd, void *parg)
{
1349 1350
	struct dvb_device *dvbdev = file->private_data;
	struct dvb_ca_private *ca = dvbdev->priv;
L
Linus Torvalds 已提交
1351 1352 1353
	int err = 0;
	int slot;

1354
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1355

1356 1357 1358
	if (mutex_lock_interruptible(&ca->ioctl_mutex))
		return -ERESTARTSYS;

L
Linus Torvalds 已提交
1359 1360 1361
	switch (cmd) {
	case CA_RESET:
		for (slot = 0; slot < ca->slot_count; slot++) {
1362 1363 1364 1365
			struct dvb_ca_slot *sl = &ca->slot_info[slot];

			mutex_lock(&sl->slot_lock);
			if (sl->slot_state != DVB_CA_SLOTSTATE_NONE) {
L
Linus Torvalds 已提交
1366 1367 1368 1369 1370 1371
				dvb_ca_en50221_slot_shutdown(ca, slot);
				if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
					dvb_ca_en50221_camchange_irq(ca->pub,
								     slot,
								     DVB_CA_EN50221_CAMCHANGE_INSERTED);
			}
1372
			mutex_unlock(&sl->slot_lock);
L
Linus Torvalds 已提交
1373 1374 1375 1376 1377 1378
		}
		ca->next_read_slot = 0;
		dvb_ca_en50221_thread_wakeup(ca);
		break;

	case CA_GET_CAP: {
1379
		struct ca_caps *caps = parg;
L
Linus Torvalds 已提交
1380 1381 1382 1383 1384 1385 1386 1387 1388

		caps->slot_num = ca->slot_count;
		caps->slot_type = CA_CI_LINK;
		caps->descr_num = 0;
		caps->descr_type = 0;
		break;
	}

	case CA_GET_SLOT_INFO: {
1389
		struct ca_slot_info *info = parg;
1390
		struct dvb_ca_slot *sl;
L
Linus Torvalds 已提交
1391

1392
		slot = info->num;
1393
		if ((slot >= ca->slot_count) || (slot < 0)) {
1394 1395 1396
			err = -EINVAL;
			goto out_unlock;
		}
1397
		slot = array_index_nospec(slot, ca->slot_count);
L
Linus Torvalds 已提交
1398 1399 1400

		info->type = CA_CI_LINK;
		info->flags = 0;
1401 1402 1403
		sl = &ca->slot_info[slot];
		if ((sl->slot_state != DVB_CA_SLOTSTATE_NONE) &&
		    (sl->slot_state != DVB_CA_SLOTSTATE_INVALID)) {
L
Linus Torvalds 已提交
1404 1405
			info->flags = CA_CI_MODULE_PRESENT;
		}
1406
		if (sl->slot_state == DVB_CA_SLOTSTATE_RUNNING)
L
Linus Torvalds 已提交
1407 1408 1409 1410 1411 1412 1413 1414 1415
			info->flags |= CA_CI_MODULE_READY;
		break;
	}

	default:
		err = -EINVAL;
		break;
	}

1416
out_unlock:
1417
	mutex_unlock(&ca->ioctl_mutex);
L
Linus Torvalds 已提交
1418 1419 1420 1421
	return err;
}

/**
1422
 * dvb_ca_en50221_io_ioctl - Wrapper for ioctl implementation.
L
Linus Torvalds 已提交
1423
 *
1424 1425 1426
 * @file: File concerned.
 * @cmd: IOCTL command.
 * @arg: Associated argument.
L
Linus Torvalds 已提交
1427
 *
1428
 * return: 0 on success, <0 on error.
L
Linus Torvalds 已提交
1429
 */
1430 1431
static long dvb_ca_en50221_io_ioctl(struct file *file,
				    unsigned int cmd, unsigned long arg)
L
Linus Torvalds 已提交
1432
{
A
Arnd Bergmann 已提交
1433
	return dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
L
Linus Torvalds 已提交
1434 1435 1436
}

/**
1437
 * dvb_ca_en50221_io_write - Implementation of write() syscall.
L
Linus Torvalds 已提交
1438
 *
1439 1440 1441 1442
 * @file: File structure.
 * @buf: Source buffer.
 * @count: Size of source buffer.
 * @ppos: Position in file (ignored).
L
Linus Torvalds 已提交
1443
 *
1444
 * return: Number of bytes read, or <0 on error.
L
Linus Torvalds 已提交
1445 1446
 */
static ssize_t dvb_ca_en50221_io_write(struct file *file,
1447 1448
				       const char __user *buf, size_t count,
				       loff_t *ppos)
L
Linus Torvalds 已提交
1449
{
1450 1451
	struct dvb_device *dvbdev = file->private_data;
	struct dvb_ca_private *ca = dvbdev->priv;
1452
	struct dvb_ca_slot *sl;
L
Linus Torvalds 已提交
1453 1454
	u8 slot, connection_id;
	int status;
1455
	u8 fragbuf[HOST_LINK_BUF_SIZE];
L
Linus Torvalds 已提交
1456 1457 1458 1459 1460
	int fragpos = 0;
	int fraglen;
	unsigned long timeout;
	int written;

1461
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1462

1463 1464 1465 1466
	/*
	 * Incoming packet has a 2 byte header.
	 * hdr[0] = slot_id, hdr[1] = connection_id
	 */
L
Linus Torvalds 已提交
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
	if (count < 2)
		return -EINVAL;

	/* extract slot & connection id */
	if (copy_from_user(&slot, buf, 1))
		return -EFAULT;
	if (copy_from_user(&connection_id, buf + 1, 1))
		return -EFAULT;
	buf += 2;
	count -= 2;
1477 1478 1479

	if (slot >= ca->slot_count)
		return -EINVAL;
1480
	slot = array_index_nospec(slot, ca->slot_count);
1481
	sl = &ca->slot_info[slot];
L
Linus Torvalds 已提交
1482 1483

	/* check if the slot is actually running */
1484
	if (sl->slot_state != DVB_CA_SLOTSTATE_RUNNING)
L
Linus Torvalds 已提交
1485 1486 1487 1488
		return -EINVAL;

	/* fragment the packets & store in the buffer */
	while (fragpos < count) {
1489
		fraglen = sl->link_buf_size - 2;
1490 1491 1492 1493
		if (fraglen < 0)
			break;
		if (fraglen > HOST_LINK_BUF_SIZE - 2)
			fraglen = HOST_LINK_BUF_SIZE - 2;
L
Linus Torvalds 已提交
1494 1495 1496 1497 1498
		if ((count - fragpos) < fraglen)
			fraglen = count - fragpos;

		fragbuf[0] = connection_id;
		fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1499 1500 1501
		status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
		if (status) {
			status = -EFAULT;
L
Linus Torvalds 已提交
1502
			goto exit;
1503
		}
L
Linus Torvalds 已提交
1504 1505 1506 1507

		timeout = jiffies + HZ / 2;
		written = 0;
		while (!time_after(jiffies, timeout)) {
1508 1509 1510 1511
			/*
			 * check the CAM hasn't been removed/reset in the
			 * meantime
			 */
1512
			if (sl->slot_state != DVB_CA_SLOTSTATE_RUNNING) {
L
Linus Torvalds 已提交
1513 1514 1515 1516
				status = -EIO;
				goto exit;
			}

1517
			mutex_lock(&sl->slot_lock);
1518
			status = dvb_ca_en50221_write_data(ca, slot, fragbuf,
1519
							   fraglen + 2, 0);
1520
			mutex_unlock(&sl->slot_lock);
L
Linus Torvalds 已提交
1521 1522 1523 1524 1525 1526 1527
			if (status == (fraglen + 2)) {
				written = 1;
				break;
			}
			if (status != -EAGAIN)
				goto exit;

1528
			usleep_range(1000, 1100);
L
Linus Torvalds 已提交
1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
		}
		if (!written) {
			status = -EIO;
			goto exit;
		}

		fragpos += fraglen;
	}
	status = count + 2;

exit:
	return status;
}

1543
/*
L
Linus Torvalds 已提交
1544 1545
 * Condition for waking up in dvb_ca_en50221_io_read_condition
 */
1546 1547
static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
					    int *result, int *_slot)
L
Linus Torvalds 已提交
1548 1549 1550 1551
{
	int slot;
	int slot_count = 0;
	int idx;
1552
	size_t fraglen;
L
Linus Torvalds 已提交
1553 1554 1555 1556 1557 1558
	int connection_id = -1;
	int found = 0;
	u8 hdr[2];

	slot = ca->next_read_slot;
	while ((slot_count < ca->slot_count) && (!found)) {
1559 1560 1561
		struct dvb_ca_slot *sl = &ca->slot_info[slot];

		if (sl->slot_state != DVB_CA_SLOTSTATE_RUNNING)
L
Linus Torvalds 已提交
1562 1563
			goto nextslot;

1564
		if (!sl->rx_buffer.data)
L
Linus Torvalds 已提交
1565 1566
			return 0;

1567
		idx = dvb_ringbuffer_pkt_next(&sl->rx_buffer, -1, &fraglen);
L
Linus Torvalds 已提交
1568
		while (idx != -1) {
1569
			dvb_ringbuffer_pkt_read(&sl->rx_buffer, idx, 0, hdr, 2);
L
Linus Torvalds 已提交
1570 1571
			if (connection_id == -1)
				connection_id = hdr[0];
1572 1573
			if ((hdr[0] == connection_id) &&
			    ((hdr[1] & 0x80) == 0)) {
L
Linus Torvalds 已提交
1574 1575 1576 1577 1578
				*_slot = slot;
				found = 1;
				break;
			}

1579 1580
			idx = dvb_ringbuffer_pkt_next(&sl->rx_buffer, idx,
						      &fraglen);
L
Linus Torvalds 已提交
1581 1582
		}

1583
nextslot:
L
Linus Torvalds 已提交
1584 1585 1586 1587 1588 1589 1590 1591 1592
		slot = (slot + 1) % ca->slot_count;
		slot_count++;
	}

	ca->next_read_slot = slot;
	return found;
}

/**
1593
 * dvb_ca_en50221_io_read - Implementation of read() syscall.
L
Linus Torvalds 已提交
1594
 *
1595 1596 1597 1598
 * @file: File structure.
 * @buf: Destination buffer.
 * @count: Size of destination buffer.
 * @ppos: Position in file (ignored).
L
Linus Torvalds 已提交
1599
 *
1600
 * return: Number of bytes read, or <0 on error.
L
Linus Torvalds 已提交
1601
 */
1602 1603
static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user *buf,
				      size_t count, loff_t *ppos)
L
Linus Torvalds 已提交
1604
{
1605 1606
	struct dvb_device *dvbdev = file->private_data;
	struct dvb_ca_private *ca = dvbdev->priv;
1607
	struct dvb_ca_slot *sl;
L
Linus Torvalds 已提交
1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
	int status;
	int result = 0;
	u8 hdr[2];
	int slot;
	int connection_id = -1;
	size_t idx, idx2;
	int last_fragment = 0;
	size_t fraglen;
	int pktlen;
	int dispose = 0;

1619
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1620

1621 1622 1623 1624
	/*
	 * Outgoing packet has a 2 byte header.
	 * hdr[0] = slot_id, hdr[1] = connection_id
	 */
L
Linus Torvalds 已提交
1625 1626 1627 1628
	if (count < 2)
		return -EINVAL;

	/* wait for some data */
1629 1630
	status = dvb_ca_en50221_io_read_condition(ca, &result, &slot);
	if (status == 0) {
L
Linus Torvalds 已提交
1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645
		/* if we're in nonblocking mode, exit immediately */
		if (file->f_flags & O_NONBLOCK)
			return -EWOULDBLOCK;

		/* wait for some data */
		status = wait_event_interruptible(ca->wait_queue,
						  dvb_ca_en50221_io_read_condition
						  (ca, &result, &slot));
	}
	if ((status < 0) || (result < 0)) {
		if (result)
			return result;
		return status;
	}

1646 1647
	sl = &ca->slot_info[slot];
	idx = dvb_ringbuffer_pkt_next(&sl->rx_buffer, -1, &fraglen);
L
Linus Torvalds 已提交
1648 1649 1650
	pktlen = 2;
	do {
		if (idx == -1) {
1651 1652
			pr_err("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n",
			       ca->dvbdev->adapter->num);
L
Linus Torvalds 已提交
1653 1654 1655 1656
			status = -EIO;
			goto exit;
		}

1657
		dvb_ringbuffer_pkt_read(&sl->rx_buffer, idx, 0, hdr, 2);
L
Linus Torvalds 已提交
1658 1659 1660 1661
		if (connection_id == -1)
			connection_id = hdr[0];
		if (hdr[0] == connection_id) {
			if (pktlen < count) {
1662
				if ((pktlen + fraglen - 2) > count)
L
Linus Torvalds 已提交
1663
					fraglen = count - pktlen;
1664
				else
L
Linus Torvalds 已提交
1665 1666
					fraglen -= 2;

1667 1668 1669 1670 1671 1672
				status =
				   dvb_ringbuffer_pkt_read_user(&sl->rx_buffer,
								idx, 2,
								buf + pktlen,
								fraglen);
				if (status < 0)
L
Linus Torvalds 已提交
1673
					goto exit;
1674

L
Linus Torvalds 已提交
1675 1676 1677 1678 1679 1680 1681 1682
				pktlen += fraglen;
			}

			if ((hdr[1] & 0x80) == 0)
				last_fragment = 1;
			dispose = 1;
		}

1683
		idx2 = dvb_ringbuffer_pkt_next(&sl->rx_buffer, idx, &fraglen);
L
Linus Torvalds 已提交
1684
		if (dispose)
1685
			dvb_ringbuffer_pkt_dispose(&sl->rx_buffer, idx);
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690 1691
		idx = idx2;
		dispose = 0;
	} while (!last_fragment);

	hdr[0] = slot;
	hdr[1] = connection_id;
1692 1693 1694
	status = copy_to_user(buf, hdr, 2);
	if (status) {
		status = -EFAULT;
L
Linus Torvalds 已提交
1695
		goto exit;
1696
	}
L
Linus Torvalds 已提交
1697 1698
	status = pktlen;

1699
exit:
L
Linus Torvalds 已提交
1700 1701 1702 1703
	return status;
}

/**
1704
 * dvb_ca_en50221_io_open - Implementation of file open syscall.
L
Linus Torvalds 已提交
1705
 *
1706 1707
 * @inode: Inode concerned.
 * @file: File concerned.
L
Linus Torvalds 已提交
1708
 *
1709
 * return: 0 on success, <0 on failure.
L
Linus Torvalds 已提交
1710 1711 1712
 */
static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
{
1713 1714
	struct dvb_device *dvbdev = file->private_data;
	struct dvb_ca_private *ca = dvbdev->priv;
L
Linus Torvalds 已提交
1715 1716 1717
	int err;
	int i;

1718
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1719

1720 1721 1722 1723 1724 1725 1726 1727 1728
	mutex_lock(&ca->remove_mutex);

	if (ca->exit) {
		mutex_unlock(&ca->remove_mutex);
		return -ENODEV;
	}

	if (!try_module_get(ca->pub->owner)) {
		mutex_unlock(&ca->remove_mutex);
L
Linus Torvalds 已提交
1729
		return -EIO;
1730
	}
L
Linus Torvalds 已提交
1731 1732

	err = dvb_generic_open(inode, file);
1733 1734
	if (err < 0) {
		module_put(ca->pub->owner);
1735
		mutex_unlock(&ca->remove_mutex);
L
Linus Torvalds 已提交
1736
		return err;
1737
	}
L
Linus Torvalds 已提交
1738 1739

	for (i = 0; i < ca->slot_count; i++) {
1740
		struct dvb_ca_slot *sl = &ca->slot_info[i];
L
Linus Torvalds 已提交
1741

1742 1743
		if (sl->slot_state == DVB_CA_SLOTSTATE_RUNNING) {
			if (!sl->rx_buffer.data) {
1744 1745 1746 1747 1748
				/*
				 * it is safe to call this here without locks
				 * because ca->open == 0. Data is not read in
				 * this case
				 */
1749
				dvb_ringbuffer_flush(&sl->rx_buffer);
L
Linus Torvalds 已提交
1750 1751 1752 1753 1754 1755 1756 1757
			}
		}
	}

	ca->open = 1;
	dvb_ca_en50221_thread_update_delay(ca);
	dvb_ca_en50221_thread_wakeup(ca);

1758 1759
	dvb_ca_private_get(ca);

1760
	mutex_unlock(&ca->remove_mutex);
L
Linus Torvalds 已提交
1761 1762 1763 1764
	return 0;
}

/**
1765
 * dvb_ca_en50221_io_release - Implementation of file close syscall.
L
Linus Torvalds 已提交
1766
 *
1767 1768
 * @inode: Inode concerned.
 * @file: File concerned.
L
Linus Torvalds 已提交
1769
 *
1770
 * return: 0 on success, <0 on failure.
L
Linus Torvalds 已提交
1771 1772 1773
 */
static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
{
1774 1775
	struct dvb_device *dvbdev = file->private_data;
	struct dvb_ca_private *ca = dvbdev->priv;
1776
	int err;
L
Linus Torvalds 已提交
1777

1778
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1779

1780 1781
	mutex_lock(&ca->remove_mutex);

L
Linus Torvalds 已提交
1782 1783 1784 1785 1786 1787 1788 1789
	/* mark the CA device as closed */
	ca->open = 0;
	dvb_ca_en50221_thread_update_delay(ca);

	err = dvb_generic_release(inode, file);

	module_put(ca->pub->owner);

1790 1791
	dvb_ca_private_put(ca);

1792 1793 1794 1795 1796 1797 1798
	if (dvbdev->users == 1 && ca->exit == 1) {
		mutex_unlock(&ca->remove_mutex);
		wake_up(&dvbdev->wait_queue);
	} else {
		mutex_unlock(&ca->remove_mutex);
	}

1799
	return err;
L
Linus Torvalds 已提交
1800 1801 1802
}

/**
1803
 * dvb_ca_en50221_io_poll - Implementation of poll() syscall.
L
Linus Torvalds 已提交
1804
 *
1805 1806
 * @file: File concerned.
 * @wait: poll wait table.
L
Linus Torvalds 已提交
1807
 *
1808
 * return: Standard poll mask.
L
Linus Torvalds 已提交
1809
 */
A
Al Viro 已提交
1810
static __poll_t dvb_ca_en50221_io_poll(struct file *file, poll_table *wait)
L
Linus Torvalds 已提交
1811
{
1812 1813
	struct dvb_device *dvbdev = file->private_data;
	struct dvb_ca_private *ca = dvbdev->priv;
A
Al Viro 已提交
1814
	__poll_t mask = 0;
L
Linus Torvalds 已提交
1815 1816 1817
	int slot;
	int result = 0;

1818
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1819

1820 1821
	poll_wait(file, &ca->wait_queue, wait);

1822
	if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1)
1823
		mask |= EPOLLIN;
L
Linus Torvalds 已提交
1824 1825 1826 1827 1828

	/* if there is something, return now */
	if (mask)
		return mask;

1829
	if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1)
1830
		mask |= EPOLLIN;
L
Linus Torvalds 已提交
1831 1832 1833 1834

	return mask;
}

1835
static const struct file_operations dvb_ca_fops = {
L
Linus Torvalds 已提交
1836 1837 1838
	.owner = THIS_MODULE,
	.read = dvb_ca_en50221_io_read,
	.write = dvb_ca_en50221_io_write,
1839
	.unlocked_ioctl = dvb_ca_en50221_io_ioctl,
L
Linus Torvalds 已提交
1840 1841 1842
	.open = dvb_ca_en50221_io_open,
	.release = dvb_ca_en50221_io_release,
	.poll = dvb_ca_en50221_io_poll,
1843
	.llseek = noop_llseek,
L
Linus Torvalds 已提交
1844 1845
};

1846
static const struct dvb_device dvbdev_ca = {
L
Linus Torvalds 已提交
1847 1848 1849 1850
	.priv = NULL,
	.users = 1,
	.readers = 1,
	.writers = 1,
1851
#if defined(CONFIG_MEDIA_CONTROLLER_DVB)
1852
	.name = "dvb-ca-en50221",
1853
#endif
L
Linus Torvalds 已提交
1854 1855 1856
	.fops = &dvb_ca_fops,
};

1857
/* ************************************************************************** */
L
Linus Torvalds 已提交
1858 1859 1860
/* Initialisation/shutdown functions */

/**
1861
 * dvb_ca_en50221_init - Initialise a new DVB CA EN50221 interface device.
L
Linus Torvalds 已提交
1862
 *
1863
 * @dvb_adapter: DVB adapter to attach the new CA device to.
1864
 * @pubca: The dvb_ca instance.
1865 1866
 * @flags: Flags describing the CA device (DVB_CA_FLAG_*).
 * @slot_count: Number of slots supported.
L
Linus Torvalds 已提交
1867
 *
1868
 * return: 0 on success, nonzero on failure
L
Linus Torvalds 已提交
1869 1870 1871 1872 1873 1874 1875 1876
 */
int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
			struct dvb_ca_en50221 *pubca, int flags, int slot_count)
{
	int ret;
	struct dvb_ca_private *ca = NULL;
	int i;

1877
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1878 1879 1880 1881 1882

	if (slot_count < 1)
		return -EINVAL;

	/* initialise the system data */
1883 1884
	ca = kzalloc(sizeof(*ca), GFP_KERNEL);
	if (!ca) {
L
Linus Torvalds 已提交
1885
		ret = -ENOMEM;
1886
		goto exit;
L
Linus Torvalds 已提交
1887
	}
1888
	kref_init(&ca->refcount);
L
Linus Torvalds 已提交
1889 1890 1891
	ca->pub = pubca;
	ca->flags = flags;
	ca->slot_count = slot_count;
1892 1893 1894
	ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot),
				GFP_KERNEL);
	if (!ca->slot_info) {
L
Linus Torvalds 已提交
1895
		ret = -ENOMEM;
1896
		goto free_ca;
L
Linus Torvalds 已提交
1897 1898 1899 1900 1901 1902 1903 1904
	}
	init_waitqueue_head(&ca->wait_queue);
	ca->open = 0;
	ca->wakeup = 0;
	ca->next_read_slot = 0;
	pubca->private = ca;

	/* register the DVB device */
1905 1906
	ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca,
				  DVB_DEVICE_CA, 0);
L
Linus Torvalds 已提交
1907
	if (ret)
1908
		goto free_slot_info;
L
Linus Torvalds 已提交
1909 1910 1911

	/* now initialise each slot */
	for (i = 0; i < slot_count; i++) {
1912 1913 1914 1915 1916 1917 1918
		struct dvb_ca_slot *sl = &ca->slot_info[i];

		memset(sl, 0, sizeof(struct dvb_ca_slot));
		sl->slot_state = DVB_CA_SLOTSTATE_NONE;
		atomic_set(&sl->camchange_count, 0);
		sl->camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
		mutex_init(&sl->slot_lock);
L
Linus Torvalds 已提交
1919 1920
	}

1921
	mutex_init(&ca->ioctl_mutex);
1922
	mutex_init(&ca->remove_mutex);
1923

L
Linus Torvalds 已提交
1924 1925
	if (signal_pending(current)) {
		ret = -EINTR;
1926
		goto unregister_device;
L
Linus Torvalds 已提交
1927 1928 1929 1930
	}
	mb();

	/* create a kthread for monitoring this CA device */
1931 1932 1933 1934
	ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
				 ca->dvbdev->adapter->num, ca->dvbdev->id);
	if (IS_ERR(ca->thread)) {
		ret = PTR_ERR(ca->thread);
1935 1936
		pr_err("dvb_ca_init: failed to start kernel_thread (%d)\n",
		       ret);
1937
		goto unregister_device;
L
Linus Torvalds 已提交
1938 1939 1940
	}
	return 0;

1941 1942 1943 1944 1945 1946 1947
unregister_device:
	dvb_unregister_device(ca->dvbdev);
free_slot_info:
	kfree(ca->slot_info);
free_ca:
	kfree(ca);
exit:
L
Linus Torvalds 已提交
1948 1949 1950
	pubca->private = NULL;
	return ret;
}
1951
EXPORT_SYMBOL(dvb_ca_en50221_init);
L
Linus Torvalds 已提交
1952 1953

/**
1954
 * dvb_ca_en50221_release - Release a DVB CA EN50221 interface device.
L
Linus Torvalds 已提交
1955
 *
1956
 * @pubca: The associated dvb_ca instance.
L
Linus Torvalds 已提交
1957 1958 1959
 */
void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
{
1960
	struct dvb_ca_private *ca = pubca->private;
L
Linus Torvalds 已提交
1961 1962
	int i;

1963
	dprintk("%s\n", __func__);
L
Linus Torvalds 已提交
1964

1965 1966 1967 1968 1969 1970 1971 1972
	mutex_lock(&ca->remove_mutex);
	ca->exit = 1;
	mutex_unlock(&ca->remove_mutex);

	if (ca->dvbdev->users < 1)
		wait_event(ca->dvbdev->wait_queue,
				ca->dvbdev->users == 1);

L
Linus Torvalds 已提交
1973
	/* shutdown the thread if there was one */
1974
	kthread_stop(ca->thread);
L
Linus Torvalds 已提交
1975

1976
	for (i = 0; i < ca->slot_count; i++)
L
Linus Torvalds 已提交
1977
		dvb_ca_en50221_slot_shutdown(ca, i);
1978

1979
	dvb_remove_device(ca->dvbdev);
1980
	dvb_ca_private_put(ca);
L
Linus Torvalds 已提交
1981 1982
	pubca->private = NULL;
}
1983
EXPORT_SYMBOL(dvb_ca_en50221_release);