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