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