1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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/nospec.h>
36#include <linux/vmalloc.h>
37#include <linux/delay.h>
38#include <linux/spinlock.h>
39#include <linux/sched.h>
40#include <linux/kthread.h>
41
42#include "dvb_ca_en50221.h"
43#include "dvb_ringbuffer.h"
44
45static int dvb_ca_en50221_debug;
46
47module_param_named(cam_debug, dvb_ca_en50221_debug, int, 0644);
48MODULE_PARM_DESC(cam_debug, "enable verbose debug messages");
49
50#define dprintk if (dvb_ca_en50221_debug) printk
51
52#define INIT_TIMEOUT_SECS 10
53
54#define HOST_LINK_BUF_SIZE 0x200
55
56#define RX_BUFFER_SIZE 65535
57
58#define MAX_RX_PACKETS_PER_ITERATION 10
59
60#define CTRLIF_DATA 0
61#define CTRLIF_COMMAND 1
62#define CTRLIF_STATUS 1
63#define CTRLIF_SIZE_LOW 2
64#define CTRLIF_SIZE_HIGH 3
65
66#define CMDREG_HC 1
67#define CMDREG_SW 2
68#define CMDREG_SR 4
69#define CMDREG_RS 8
70#define CMDREG_FRIE 0x40
71#define CMDREG_DAIE 0x80
72#define IRQEN (CMDREG_DAIE)
73
74#define STATUSREG_RE 1
75#define STATUSREG_WE 2
76#define STATUSREG_FR 0x40
77#define STATUSREG_DA 0x80
78#define STATUSREG_TXERR (STATUSREG_RE|STATUSREG_WE)
79
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
91
92struct dvb_ca_slot {
93
94
95 int slot_state;
96
97
98 struct mutex slot_lock;
99
100
101 atomic_t camchange_count;
102
103
104 int camchange_type;
105
106
107 u32 config_base;
108
109
110 u8 config_option;
111
112
113 u8 da_irq_supported:1;
114
115
116 int link_buf_size;
117
118
119 struct dvb_ringbuffer rx_buffer;
120
121
122 unsigned long timeout;
123};
124
125
126struct dvb_ca_private {
127
128
129 struct dvb_ca_en50221 *pub;
130
131
132 struct dvb_device *dvbdev;
133
134
135 u32 flags;
136
137
138 unsigned int slot_count;
139
140
141 struct dvb_ca_slot *slot_info;
142
143
144 wait_queue_head_t wait_queue;
145
146
147 struct task_struct *thread;
148
149
150 unsigned int open:1;
151
152
153 unsigned int wakeup:1;
154
155
156 unsigned long delay;
157
158
159 int next_read_slot;
160
161
162 struct mutex ioctl_mutex;
163};
164
165static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca);
166static int dvb_ca_en50221_read_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
167static int dvb_ca_en50221_write_data(struct dvb_ca_private *ca, int slot, u8 * ebuf, int ecount);
168
169
170
171
172
173
174
175
176
177
178
179static char *findstr(char * haystack, int hlen, char * needle, int nlen)
180{
181 int i;
182
183 if (hlen < nlen)
184 return NULL;
185
186 for (i = 0; i <= hlen - nlen; i++) {
187 if (!strncmp(haystack + i, needle, nlen))
188 return haystack + i;
189 }
190
191 return NULL;
192}
193
194
195
196
197
198
199
200
201
202
203static int dvb_ca_en50221_check_camstatus(struct dvb_ca_private *ca, int slot)
204{
205 int slot_status;
206 int cam_present_now;
207 int cam_changed;
208
209
210 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE) {
211 return (atomic_read(&ca->slot_info[slot].camchange_count) != 0);
212 }
213
214
215 slot_status = ca->pub->poll_slot_status(ca->pub, slot, ca->open);
216
217 cam_present_now = (slot_status & DVB_CA_EN50221_POLL_CAM_PRESENT) ? 1 : 0;
218 cam_changed = (slot_status & DVB_CA_EN50221_POLL_CAM_CHANGED) ? 1 : 0;
219 if (!cam_changed) {
220 int cam_present_old = (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE);
221 cam_changed = (cam_present_now != cam_present_old);
222 }
223
224 if (cam_changed) {
225 if (!cam_present_now) {
226 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
227 } else {
228 ca->slot_info[slot].camchange_type = DVB_CA_EN50221_CAMCHANGE_INSERTED;
229 }
230 atomic_set(&ca->slot_info[slot].camchange_count, 1);
231 } else {
232 if ((ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) &&
233 (slot_status & DVB_CA_EN50221_POLL_CAM_READY)) {
234
235 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
236 }
237 }
238
239 return cam_changed;
240}
241
242
243
244
245
246
247
248
249
250
251
252
253
254static int dvb_ca_en50221_wait_if_status(struct dvb_ca_private *ca, int slot,
255 u8 waitfor, int timeout_hz)
256{
257 unsigned long timeout;
258 unsigned long start;
259
260 dprintk("%s\n", __func__);
261
262
263 start = jiffies;
264 timeout = jiffies + timeout_hz;
265 while (1) {
266
267 int res = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
268 if (res < 0)
269 return -EIO;
270
271
272 if (res & waitfor) {
273 dprintk("%s succeeded timeout:%lu\n", __func__, jiffies - start);
274 return 0;
275 }
276
277
278 if (time_after(jiffies, timeout)) {
279 break;
280 }
281
282
283 msleep(1);
284 }
285
286 dprintk("%s failed timeout:%lu\n", __func__, jiffies - start);
287
288
289 return -ETIMEDOUT;
290}
291
292
293
294
295
296
297
298
299
300
301static int dvb_ca_en50221_link_init(struct dvb_ca_private *ca, int slot)
302{
303 int ret;
304 int buf_size;
305 u8 buf[2];
306
307 dprintk("%s\n", __func__);
308
309
310 ca->slot_info[slot].da_irq_supported = 0;
311
312
313
314 ca->slot_info[slot].link_buf_size = 2;
315
316
317 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SR)) != 0)
318 return ret;
319 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_DA, HZ / 10)) != 0)
320 return ret;
321 if ((ret = dvb_ca_en50221_read_data(ca, slot, buf, 2)) != 2)
322 return -EIO;
323 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
324 return ret;
325
326
327 buf_size = (buf[0] << 8) | buf[1];
328 if (buf_size > HOST_LINK_BUF_SIZE)
329 buf_size = HOST_LINK_BUF_SIZE;
330 ca->slot_info[slot].link_buf_size = buf_size;
331 buf[0] = buf_size >> 8;
332 buf[1] = buf_size & 0xff;
333 dprintk("Chosen link buffer size of %i\n", buf_size);
334
335
336 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN | CMDREG_SW)) != 0)
337 return ret;
338 if ((ret = dvb_ca_en50221_wait_if_status(ca, slot, STATUSREG_FR, HZ / 10)) != 0)
339 return ret;
340 if ((ret = dvb_ca_en50221_write_data(ca, slot, buf, 2)) != 2)
341 return -EIO;
342 if ((ret = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN)) != 0)
343 return ret;
344
345
346 return 0;
347}
348
349
350
351
352
353
354
355
356
357
358
359
360
361static int dvb_ca_en50221_read_tuple(struct dvb_ca_private *ca, int slot,
362 int *address, int *tupleType, int *tupleLength, u8 * tuple)
363{
364 int i;
365 int _tupleType;
366 int _tupleLength;
367 int _address = *address;
368
369
370 if ((_tupleType = ca->pub->read_attribute_mem(ca->pub, slot, _address)) < 0)
371 return _tupleType;
372 if (_tupleType == 0xff) {
373 dprintk("END OF CHAIN TUPLE type:0x%x\n", _tupleType);
374 *address += 2;
375 *tupleType = _tupleType;
376 *tupleLength = 0;
377 return 0;
378 }
379 if ((_tupleLength = ca->pub->read_attribute_mem(ca->pub, slot, _address + 2)) < 0)
380 return _tupleLength;
381 _address += 4;
382
383 dprintk("TUPLE type:0x%x length:%i\n", _tupleType, _tupleLength);
384
385
386 for (i = 0; i < _tupleLength; i++) {
387 tuple[i] = ca->pub->read_attribute_mem(ca->pub, slot, _address + (i * 2));
388 dprintk(" 0x%02x: 0x%02x %c\n",
389 i, tuple[i] & 0xff,
390 ((tuple[i] > 31) && (tuple[i] < 127)) ? tuple[i] : '.');
391 }
392 _address += (_tupleLength * 2);
393
394
395 *tupleType = _tupleType;
396 *tupleLength = _tupleLength;
397 *address = _address;
398 return 0;
399}
400
401
402
403
404
405
406
407
408
409
410
411static int dvb_ca_en50221_parse_attributes(struct dvb_ca_private *ca, int slot)
412{
413 int address = 0;
414 int tupleLength;
415 int tupleType;
416 u8 tuple[257];
417 char *dvb_str;
418 int rasz;
419 int status;
420 int got_cftableentry = 0;
421 int end_chain = 0;
422 int i;
423 u16 manfid = 0;
424 u16 devid = 0;
425
426
427
428 if ((status =
429 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
430 return status;
431 if (tupleType != 0x1D)
432 return -EINVAL;
433
434
435
436
437 if ((status =
438 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
439 return status;
440 if (tupleType != 0x1C)
441 return -EINVAL;
442
443
444
445
446 if ((status =
447 dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType, &tupleLength, tuple)) < 0)
448 return status;
449 if (tupleType != 0x15)
450 return -EINVAL;
451
452
453
454
455 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
456 &tupleLength, tuple)) < 0)
457 return status;
458 if (tupleType != 0x20)
459 return -EINVAL;
460 if (tupleLength != 4)
461 return -EINVAL;
462 manfid = (tuple[1] << 8) | tuple[0];
463 devid = (tuple[3] << 8) | tuple[2];
464
465
466
467
468 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
469 &tupleLength, tuple)) < 0)
470 return status;
471 if (tupleType != 0x1A)
472 return -EINVAL;
473 if (tupleLength < 3)
474 return -EINVAL;
475
476
477 rasz = tuple[0] & 3;
478 if (tupleLength < (3 + rasz + 14))
479 return -EINVAL;
480 ca->slot_info[slot].config_base = 0;
481 for (i = 0; i < rasz + 1; i++) {
482 ca->slot_info[slot].config_base |= (tuple[2 + i] << (8 * i));
483 }
484
485
486 dvb_str = findstr((char *)tuple, tupleLength, "DVB_CI_V", 8);
487 if (dvb_str == NULL)
488 return -EINVAL;
489 if (tupleLength < ((dvb_str - (char *) tuple) + 12))
490 return -EINVAL;
491
492
493 if (strncmp(dvb_str + 8, "1.00", 4)) {
494 printk("dvb_ca adapter %d: Unsupported DVB CAM module version %c%c%c%c\n",
495 ca->dvbdev->adapter->num, dvb_str[8], dvb_str[9], dvb_str[10], dvb_str[11]);
496 return -EINVAL;
497 }
498
499
500 while ((!end_chain) && (address < 0x1000)) {
501 if ((status = dvb_ca_en50221_read_tuple(ca, slot, &address, &tupleType,
502 &tupleLength, tuple)) < 0)
503 return status;
504 switch (tupleType) {
505 case 0x1B:
506 if (tupleLength < (2 + 11 + 17))
507 break;
508
509
510 if (got_cftableentry)
511 break;
512
513
514 ca->slot_info[slot].config_option = tuple[0] & 0x3f;
515
516
517 if ((findstr((char *)tuple, tupleLength, "DVB_HOST", 8) == NULL) ||
518 (findstr((char *)tuple, tupleLength, "DVB_CI_MODULE", 13) == NULL))
519 break;
520
521 got_cftableentry = 1;
522 break;
523
524 case 0x14:
525 break;
526
527 case 0xFF:
528 end_chain = 1;
529 break;
530
531 default:
532 dprintk("dvb_ca: Skipping unknown tuple type:0x%x length:0x%x\n", tupleType,
533 tupleLength);
534 break;
535 }
536 }
537
538 if ((address > 0x1000) || (!got_cftableentry))
539 return -EINVAL;
540
541 dprintk("Valid DVB CAM detected MANID:%x DEVID:%x CONFIGBASE:0x%x CONFIGOPTION:0x%x\n",
542 manfid, devid, ca->slot_info[slot].config_base, ca->slot_info[slot].config_option);
543
544
545 return 0;
546}
547
548
549
550
551
552
553
554
555static int dvb_ca_en50221_set_configoption(struct dvb_ca_private *ca, int slot)
556{
557 int configoption;
558
559 dprintk("%s\n", __func__);
560
561
562 ca->pub->write_attribute_mem(ca->pub, slot,
563 ca->slot_info[slot].config_base,
564 ca->slot_info[slot].config_option);
565
566
567 configoption = ca->pub->read_attribute_mem(ca->pub, slot, ca->slot_info[slot].config_base);
568 dprintk("Set configoption 0x%x, read configoption 0x%x\n",
569 ca->slot_info[slot].config_option, configoption & 0x3f);
570
571
572 return 0;
573
574}
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590static 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
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
616 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
617 goto exit;
618 if (!(status & STATUSREG_DA)) {
619
620 status = 0;
621 goto exit;
622 }
623
624
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
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
658 for (i = 0; i < bytes_read; i++) {
659
660 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_DATA)) < 0)
661 goto exit;
662
663
664 buf[i] = status;
665 }
666
667
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
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
691 if ((buf[1] & 0x80) == 0x00) {
692 wake_up_interruptible(&ca->wait_queue);
693 }
694 status = bytes_read;
695
696exit:
697 return status;
698}
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713static 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
722 if (bytes_write > ca->slot_info[slot].link_buf_size)
723 return -EINVAL;
724
725
726
727
728
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
740 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND,
741 IRQEN | CMDREG_HC)) != 0)
742 goto exit;
743
744
745 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
746 goto exit;
747 if (!(status & STATUSREG_FR)) {
748
749 status = -EAGAIN;
750 goto exit;
751 }
752
753
754 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_HIGH, bytes_write >> 8)) != 0)
755 goto exit;
756 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_SIZE_LOW,
757 bytes_write & 0xff)) != 0)
758 goto exit;
759
760
761 for (i = 0; i < bytes_write; i++) {
762 if ((status = ca->pub->write_cam_control(ca->pub, slot, CTRLIF_DATA, buf[i])) != 0)
763 goto exit;
764 }
765
766
767 if ((status = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS)) < 0)
768 goto exit;
769 if (status & STATUSREG_WE) {
770 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
771 status = -EIO;
772 goto exit;
773 }
774 status = bytes_write;
775
776 dprintk("Wrote 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_write);
778
779exit:
780 ca->pub->write_cam_control(ca->pub, slot, CTRLIF_COMMAND, IRQEN);
781
782exitnowrite:
783 return status;
784}
785EXPORT_SYMBOL(dvb_ca_en50221_camchange_irq);
786
787
788
789
790
791
792
793
794
795
796
797
798
799static int dvb_ca_en50221_slot_shutdown(struct dvb_ca_private *ca, int slot)
800{
801 dprintk("%s\n", __func__);
802
803 ca->pub->slot_shutdown(ca->pub, slot);
804 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
805
806
807
808 wake_up_interruptible(&ca->wait_queue);
809
810 dprintk("Slot %i shutdown\n", slot);
811
812
813 return 0;
814}
815EXPORT_SYMBOL(dvb_ca_en50221_camready_irq);
816
817
818
819
820
821
822
823
824
825void dvb_ca_en50221_camchange_irq(struct dvb_ca_en50221 *pubca, int slot, int change_type)
826{
827 struct dvb_ca_private *ca = pubca->private;
828
829 dprintk("CAMCHANGE IRQ slot:%i change_type:%i\n", slot, change_type);
830
831 switch (change_type) {
832 case DVB_CA_EN50221_CAMCHANGE_REMOVED:
833 case DVB_CA_EN50221_CAMCHANGE_INSERTED:
834 break;
835
836 default:
837 return;
838 }
839
840 ca->slot_info[slot].camchange_type = change_type;
841 atomic_inc(&ca->slot_info[slot].camchange_count);
842 dvb_ca_en50221_thread_wakeup(ca);
843}
844EXPORT_SYMBOL(dvb_ca_en50221_frda_irq);
845
846
847
848
849
850
851
852
853void dvb_ca_en50221_camready_irq(struct dvb_ca_en50221 *pubca, int slot)
854{
855 struct dvb_ca_private *ca = pubca->private;
856
857 dprintk("CAMREADY IRQ slot:%i\n", slot);
858
859 if (ca->slot_info[slot].slot_state == DVB_CA_SLOTSTATE_WAITREADY) {
860 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_VALIDATE;
861 dvb_ca_en50221_thread_wakeup(ca);
862 }
863}
864
865
866
867
868
869
870
871
872void dvb_ca_en50221_frda_irq(struct dvb_ca_en50221 *pubca, int slot)
873{
874 struct dvb_ca_private *ca = pubca->private;
875 int flags;
876
877 dprintk("FR/DA IRQ slot:%i\n", slot);
878
879 switch (ca->slot_info[slot].slot_state) {
880 case DVB_CA_SLOTSTATE_LINKINIT:
881 flags = ca->pub->read_cam_control(pubca, slot, CTRLIF_STATUS);
882 if (flags & STATUSREG_DA) {
883 dprintk("CAM supports DA IRQ\n");
884 ca->slot_info[slot].da_irq_supported = 1;
885 }
886 break;
887
888 case DVB_CA_SLOTSTATE_RUNNING:
889 if (ca->open)
890 dvb_ca_en50221_thread_wakeup(ca);
891 break;
892 }
893}
894
895
896
897
898
899
900
901
902
903
904
905static void dvb_ca_en50221_thread_wakeup(struct dvb_ca_private *ca)
906{
907
908 dprintk("%s\n", __func__);
909
910 ca->wakeup = 1;
911 mb();
912 wake_up_process(ca->thread);
913}
914
915
916
917
918
919
920static void dvb_ca_en50221_thread_update_delay(struct dvb_ca_private *ca)
921{
922 int delay;
923 int curdelay = 100000000;
924 int slot;
925
926
927
928
929 for (slot = 0; slot < ca->slot_count; slot++) {
930 switch (ca->slot_info[slot].slot_state) {
931 default:
932 case DVB_CA_SLOTSTATE_NONE:
933 delay = HZ * 60;
934 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
935 delay = HZ * 5;
936 break;
937 case DVB_CA_SLOTSTATE_INVALID:
938 delay = HZ * 60;
939 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
940 delay = HZ / 10;
941 break;
942
943 case DVB_CA_SLOTSTATE_UNINITIALISED:
944 case DVB_CA_SLOTSTATE_WAITREADY:
945 case DVB_CA_SLOTSTATE_VALIDATE:
946 case DVB_CA_SLOTSTATE_WAITFR:
947 case DVB_CA_SLOTSTATE_LINKINIT:
948 delay = HZ / 10;
949 break;
950
951 case DVB_CA_SLOTSTATE_RUNNING:
952 delay = HZ * 60;
953 if (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE))
954 delay = HZ / 10;
955 if (ca->open) {
956 if ((!ca->slot_info[slot].da_irq_supported) ||
957 (!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_DA)))
958 delay = HZ / 10;
959 }
960 break;
961 }
962
963 if (delay < curdelay)
964 curdelay = delay;
965 }
966
967 ca->delay = curdelay;
968}
969
970
971
972
973
974
975static int dvb_ca_en50221_thread(void *data)
976{
977 struct dvb_ca_private *ca = data;
978 int slot;
979 int flags;
980 int status;
981 int pktcount;
982 void *rxbuf;
983
984 dprintk("%s\n", __func__);
985
986
987 dvb_ca_en50221_thread_update_delay(ca);
988
989
990 while (!kthread_should_stop()) {
991
992 if (!ca->wakeup) {
993 set_current_state(TASK_INTERRUPTIBLE);
994 schedule_timeout(ca->delay);
995 if (kthread_should_stop())
996 return 0;
997 }
998 ca->wakeup = 0;
999
1000
1001 for (slot = 0; slot < ca->slot_count; slot++) {
1002
1003 mutex_lock(&ca->slot_info[slot].slot_lock);
1004
1005
1006 while (dvb_ca_en50221_check_camstatus(ca, slot)) {
1007
1008 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE)
1009 dvb_ca_en50221_slot_shutdown(ca, slot);
1010
1011
1012 if (ca->slot_info[slot].camchange_type == DVB_CA_EN50221_CAMCHANGE_INSERTED) {
1013 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_UNINITIALISED;
1014 }
1015
1016
1017 dvb_ca_en50221_thread_update_delay(ca);
1018 atomic_dec(&ca->slot_info[slot].camchange_count);
1019 }
1020
1021
1022 switch (ca->slot_info[slot].slot_state) {
1023 case DVB_CA_SLOTSTATE_NONE:
1024 case DVB_CA_SLOTSTATE_INVALID:
1025
1026 break;
1027
1028 case DVB_CA_SLOTSTATE_UNINITIALISED:
1029 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITREADY;
1030 ca->pub->slot_reset(ca->pub, slot);
1031 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1032 break;
1033
1034 case DVB_CA_SLOTSTATE_WAITREADY:
1035 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1036 printk("dvb_ca adaptor %d: PC card did not respond :(\n",
1037 ca->dvbdev->adapter->num);
1038 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1039 dvb_ca_en50221_thread_update_delay(ca);
1040 break;
1041 }
1042
1043 break;
1044
1045 case DVB_CA_SLOTSTATE_VALIDATE:
1046 if (dvb_ca_en50221_parse_attributes(ca, slot) != 0) {
1047
1048 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1049 (ca->pub->poll_slot_status)) {
1050 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1051 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1052 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1053 dvb_ca_en50221_thread_update_delay(ca);
1054 break;
1055 }
1056 }
1057
1058 printk("dvb_ca adapter %d: Invalid PC card inserted :(\n",
1059 ca->dvbdev->adapter->num);
1060 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1061 dvb_ca_en50221_thread_update_delay(ca);
1062 break;
1063 }
1064 if (dvb_ca_en50221_set_configoption(ca, slot) != 0) {
1065 printk("dvb_ca adapter %d: Unable to initialise CAM :(\n",
1066 ca->dvbdev->adapter->num);
1067 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1068 dvb_ca_en50221_thread_update_delay(ca);
1069 break;
1070 }
1071 if (ca->pub->write_cam_control(ca->pub, slot,
1072 CTRLIF_COMMAND, CMDREG_RS) != 0) {
1073 printk("dvb_ca adapter %d: Unable to reset CAM IF\n",
1074 ca->dvbdev->adapter->num);
1075 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1076 dvb_ca_en50221_thread_update_delay(ca);
1077 break;
1078 }
1079 dprintk("DVB CAM validated successfully\n");
1080
1081 ca->slot_info[slot].timeout = jiffies + (INIT_TIMEOUT_SECS * HZ);
1082 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_WAITFR;
1083 ca->wakeup = 1;
1084 break;
1085
1086 case DVB_CA_SLOTSTATE_WAITFR:
1087 if (time_after(jiffies, ca->slot_info[slot].timeout)) {
1088 printk("dvb_ca adapter %d: DVB CAM did not respond :(\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
1095 flags = ca->pub->read_cam_control(ca->pub, slot, CTRLIF_STATUS);
1096 if (flags & STATUSREG_FR) {
1097 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_LINKINIT;
1098 ca->wakeup = 1;
1099 }
1100 break;
1101
1102 case DVB_CA_SLOTSTATE_LINKINIT:
1103 if (dvb_ca_en50221_link_init(ca, slot) != 0) {
1104
1105 if ((!(ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)) &&
1106 (ca->pub->poll_slot_status)) {
1107 status = ca->pub->poll_slot_status(ca->pub, slot, 0);
1108 if (!(status & DVB_CA_EN50221_POLL_CAM_PRESENT)) {
1109 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_NONE;
1110 dvb_ca_en50221_thread_update_delay(ca);
1111 break;
1112 }
1113 }
1114
1115 printk("dvb_ca adapter %d: DVB CAM link initialisation failed :(\n", ca->dvbdev->adapter->num);
1116 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1117 dvb_ca_en50221_thread_update_delay(ca);
1118 break;
1119 }
1120
1121 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1122 rxbuf = vmalloc(RX_BUFFER_SIZE);
1123 if (rxbuf == NULL) {
1124 printk("dvb_ca adapter %d: Unable to allocate CAM rx buffer :(\n", ca->dvbdev->adapter->num);
1125 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_INVALID;
1126 dvb_ca_en50221_thread_update_delay(ca);
1127 break;
1128 }
1129 dvb_ringbuffer_init(&ca->slot_info[slot].rx_buffer, rxbuf, RX_BUFFER_SIZE);
1130 }
1131
1132 ca->pub->slot_ts_enable(ca->pub, slot);
1133 ca->slot_info[slot].slot_state = DVB_CA_SLOTSTATE_RUNNING;
1134 dvb_ca_en50221_thread_update_delay(ca);
1135 printk("dvb_ca adapter %d: DVB CAM detected and initialised successfully\n", ca->dvbdev->adapter->num);
1136 break;
1137
1138 case DVB_CA_SLOTSTATE_RUNNING:
1139 if (!ca->open)
1140 break;
1141
1142
1143 pktcount = 0;
1144 while ((status = dvb_ca_en50221_read_data(ca, slot, NULL, 0)) > 0) {
1145 if (!ca->open)
1146 break;
1147
1148
1149 if (dvb_ca_en50221_check_camstatus(ca, slot)) {
1150
1151 ca->wakeup = 1;
1152 break;
1153 }
1154
1155
1156 if (++pktcount >= MAX_RX_PACKETS_PER_ITERATION) {
1157
1158 ca->wakeup = 1;
1159 break;
1160 }
1161 }
1162 break;
1163 }
1164
1165 mutex_unlock(&ca->slot_info[slot].slot_lock);
1166 }
1167 }
1168
1169 return 0;
1170}
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188static int dvb_ca_en50221_io_do_ioctl(struct file *file,
1189 unsigned int cmd, void *parg)
1190{
1191 struct dvb_device *dvbdev = file->private_data;
1192 struct dvb_ca_private *ca = dvbdev->priv;
1193 int err = 0;
1194 int slot;
1195
1196 dprintk("%s\n", __func__);
1197
1198 if (mutex_lock_interruptible(&ca->ioctl_mutex))
1199 return -ERESTARTSYS;
1200
1201 switch (cmd) {
1202 case CA_RESET:
1203 for (slot = 0; slot < ca->slot_count; slot++) {
1204 mutex_lock(&ca->slot_info[slot].slot_lock);
1205 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_NONE) {
1206 dvb_ca_en50221_slot_shutdown(ca, slot);
1207 if (ca->flags & DVB_CA_EN50221_FLAG_IRQ_CAMCHANGE)
1208 dvb_ca_en50221_camchange_irq(ca->pub,
1209 slot,
1210 DVB_CA_EN50221_CAMCHANGE_INSERTED);
1211 }
1212 mutex_unlock(&ca->slot_info[slot].slot_lock);
1213 }
1214 ca->next_read_slot = 0;
1215 dvb_ca_en50221_thread_wakeup(ca);
1216 break;
1217
1218 case CA_GET_CAP: {
1219 struct ca_caps *caps = parg;
1220
1221 caps->slot_num = ca->slot_count;
1222 caps->slot_type = CA_CI_LINK;
1223 caps->descr_num = 0;
1224 caps->descr_type = 0;
1225 break;
1226 }
1227
1228 case CA_GET_SLOT_INFO: {
1229 struct ca_slot_info *info = parg;
1230
1231 if ((info->num > ca->slot_count) || (info->num < 0)) {
1232 err = -EINVAL;
1233 goto out_unlock;
1234 }
1235
1236 info->type = CA_CI_LINK;
1237 info->flags = 0;
1238 if ((ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_NONE)
1239 && (ca->slot_info[info->num].slot_state != DVB_CA_SLOTSTATE_INVALID)) {
1240 info->flags = CA_CI_MODULE_PRESENT;
1241 }
1242 if (ca->slot_info[info->num].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1243 info->flags |= CA_CI_MODULE_READY;
1244 }
1245 break;
1246 }
1247
1248 default:
1249 err = -EINVAL;
1250 break;
1251 }
1252
1253out_unlock:
1254 mutex_unlock(&ca->ioctl_mutex);
1255 return err;
1256}
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269static long dvb_ca_en50221_io_ioctl(struct file *file,
1270 unsigned int cmd, unsigned long arg)
1271{
1272 return dvb_usercopy(file, cmd, arg, dvb_ca_en50221_io_do_ioctl);
1273}
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286static ssize_t dvb_ca_en50221_io_write(struct file *file,
1287 const char __user * buf, size_t count, loff_t * ppos)
1288{
1289 struct dvb_device *dvbdev = file->private_data;
1290 struct dvb_ca_private *ca = dvbdev->priv;
1291 u8 slot, connection_id;
1292 int status;
1293 u8 fragbuf[HOST_LINK_BUF_SIZE];
1294 int fragpos = 0;
1295 int fraglen;
1296 unsigned long timeout;
1297 int written;
1298
1299 dprintk("%s\n", __func__);
1300
1301
1302 if (count < 2)
1303 return -EINVAL;
1304
1305
1306 if (copy_from_user(&slot, buf, 1))
1307 return -EFAULT;
1308 if (copy_from_user(&connection_id, buf + 1, 1))
1309 return -EFAULT;
1310 buf += 2;
1311 count -= 2;
1312
1313 if (slot >= ca->slot_count)
1314 return -EINVAL;
1315 slot = array_index_nospec(slot, ca->slot_count);
1316
1317
1318 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1319 return -EINVAL;
1320
1321
1322 while (fragpos < count) {
1323 fraglen = ca->slot_info[slot].link_buf_size - 2;
1324 if (fraglen < 0)
1325 break;
1326 if (fraglen > HOST_LINK_BUF_SIZE - 2)
1327 fraglen = HOST_LINK_BUF_SIZE - 2;
1328 if ((count - fragpos) < fraglen)
1329 fraglen = count - fragpos;
1330
1331 fragbuf[0] = connection_id;
1332 fragbuf[1] = ((fragpos + fraglen) < count) ? 0x80 : 0x00;
1333 status = copy_from_user(fragbuf + 2, buf + fragpos, fraglen);
1334 if (status) {
1335 status = -EFAULT;
1336 goto exit;
1337 }
1338
1339 timeout = jiffies + HZ / 2;
1340 written = 0;
1341 while (!time_after(jiffies, timeout)) {
1342
1343 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING) {
1344 status = -EIO;
1345 goto exit;
1346 }
1347
1348 mutex_lock(&ca->slot_info[slot].slot_lock);
1349 status = dvb_ca_en50221_write_data(ca, slot, fragbuf, fraglen + 2);
1350 mutex_unlock(&ca->slot_info[slot].slot_lock);
1351 if (status == (fraglen + 2)) {
1352 written = 1;
1353 break;
1354 }
1355 if (status != -EAGAIN)
1356 goto exit;
1357
1358 msleep(1);
1359 }
1360 if (!written) {
1361 status = -EIO;
1362 goto exit;
1363 }
1364
1365 fragpos += fraglen;
1366 }
1367 status = count + 2;
1368
1369exit:
1370 return status;
1371}
1372
1373
1374
1375
1376
1377static int dvb_ca_en50221_io_read_condition(struct dvb_ca_private *ca,
1378 int *result, int *_slot)
1379{
1380 int slot;
1381 int slot_count = 0;
1382 int idx;
1383 size_t fraglen;
1384 int connection_id = -1;
1385 int found = 0;
1386 u8 hdr[2];
1387
1388 slot = ca->next_read_slot;
1389 while ((slot_count < ca->slot_count) && (!found)) {
1390 if (ca->slot_info[slot].slot_state != DVB_CA_SLOTSTATE_RUNNING)
1391 goto nextslot;
1392
1393 if (ca->slot_info[slot].rx_buffer.data == NULL) {
1394 return 0;
1395 }
1396
1397 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1398 while (idx != -1) {
1399 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1400 if (connection_id == -1)
1401 connection_id = hdr[0];
1402 if ((hdr[0] == connection_id) && ((hdr[1] & 0x80) == 0)) {
1403 *_slot = slot;
1404 found = 1;
1405 break;
1406 }
1407
1408 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1409 }
1410
1411nextslot:
1412 slot = (slot + 1) % ca->slot_count;
1413 slot_count++;
1414 }
1415
1416 ca->next_read_slot = slot;
1417 return found;
1418}
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431static ssize_t dvb_ca_en50221_io_read(struct file *file, char __user * buf,
1432 size_t count, loff_t * ppos)
1433{
1434 struct dvb_device *dvbdev = file->private_data;
1435 struct dvb_ca_private *ca = dvbdev->priv;
1436 int status;
1437 int result = 0;
1438 u8 hdr[2];
1439 int slot;
1440 int connection_id = -1;
1441 size_t idx, idx2;
1442 int last_fragment = 0;
1443 size_t fraglen;
1444 int pktlen;
1445 int dispose = 0;
1446
1447 dprintk("%s\n", __func__);
1448
1449
1450 if (count < 2)
1451 return -EINVAL;
1452
1453
1454 if ((status = dvb_ca_en50221_io_read_condition(ca, &result, &slot)) == 0) {
1455
1456
1457 if (file->f_flags & O_NONBLOCK)
1458 return -EWOULDBLOCK;
1459
1460
1461 status = wait_event_interruptible(ca->wait_queue,
1462 dvb_ca_en50221_io_read_condition
1463 (ca, &result, &slot));
1464 }
1465 if ((status < 0) || (result < 0)) {
1466 if (result)
1467 return result;
1468 return status;
1469 }
1470
1471 idx = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, -1, &fraglen);
1472 pktlen = 2;
1473 do {
1474 if (idx == -1) {
1475 printk("dvb_ca adapter %d: BUG: read packet ended before last_fragment encountered\n", ca->dvbdev->adapter->num);
1476 status = -EIO;
1477 goto exit;
1478 }
1479
1480 dvb_ringbuffer_pkt_read(&ca->slot_info[slot].rx_buffer, idx, 0, hdr, 2);
1481 if (connection_id == -1)
1482 connection_id = hdr[0];
1483 if (hdr[0] == connection_id) {
1484 if (pktlen < count) {
1485 if ((pktlen + fraglen - 2) > count) {
1486 fraglen = count - pktlen;
1487 } else {
1488 fraglen -= 2;
1489 }
1490
1491 if ((status = dvb_ringbuffer_pkt_read_user(&ca->slot_info[slot].rx_buffer, idx, 2,
1492 buf + pktlen, fraglen)) < 0) {
1493 goto exit;
1494 }
1495 pktlen += fraglen;
1496 }
1497
1498 if ((hdr[1] & 0x80) == 0)
1499 last_fragment = 1;
1500 dispose = 1;
1501 }
1502
1503 idx2 = dvb_ringbuffer_pkt_next(&ca->slot_info[slot].rx_buffer, idx, &fraglen);
1504 if (dispose)
1505 dvb_ringbuffer_pkt_dispose(&ca->slot_info[slot].rx_buffer, idx);
1506 idx = idx2;
1507 dispose = 0;
1508 } while (!last_fragment);
1509
1510 hdr[0] = slot;
1511 hdr[1] = connection_id;
1512 status = copy_to_user(buf, hdr, 2);
1513 if (status) {
1514 status = -EFAULT;
1515 goto exit;
1516 }
1517 status = pktlen;
1518
1519exit:
1520 return status;
1521}
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532static int dvb_ca_en50221_io_open(struct inode *inode, struct file *file)
1533{
1534 struct dvb_device *dvbdev = file->private_data;
1535 struct dvb_ca_private *ca = dvbdev->priv;
1536 int err;
1537 int i;
1538
1539 dprintk("%s\n", __func__);
1540
1541 if (!try_module_get(ca->pub->owner))
1542 return -EIO;
1543
1544 err = dvb_generic_open(inode, file);
1545 if (err < 0) {
1546 module_put(ca->pub->owner);
1547 return err;
1548 }
1549
1550 for (i = 0; i < ca->slot_count; i++) {
1551
1552 if (ca->slot_info[i].slot_state == DVB_CA_SLOTSTATE_RUNNING) {
1553 if (ca->slot_info[i].rx_buffer.data != NULL) {
1554
1555
1556 dvb_ringbuffer_flush(&ca->slot_info[i].rx_buffer);
1557 }
1558 }
1559 }
1560
1561 ca->open = 1;
1562 dvb_ca_en50221_thread_update_delay(ca);
1563 dvb_ca_en50221_thread_wakeup(ca);
1564
1565 return 0;
1566}
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577static int dvb_ca_en50221_io_release(struct inode *inode, struct file *file)
1578{
1579 struct dvb_device *dvbdev = file->private_data;
1580 struct dvb_ca_private *ca = dvbdev->priv;
1581 int err;
1582
1583 dprintk("%s\n", __func__);
1584
1585
1586 ca->open = 0;
1587 dvb_ca_en50221_thread_update_delay(ca);
1588
1589 err = dvb_generic_release(inode, file);
1590
1591 module_put(ca->pub->owner);
1592
1593 return err;
1594}
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605static unsigned int dvb_ca_en50221_io_poll(struct file *file, poll_table * wait)
1606{
1607 struct dvb_device *dvbdev = file->private_data;
1608 struct dvb_ca_private *ca = dvbdev->priv;
1609 unsigned int mask = 0;
1610 int slot;
1611 int result = 0;
1612
1613 dprintk("%s\n", __func__);
1614
1615 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1616 mask |= POLLIN;
1617 }
1618
1619
1620 if (mask)
1621 return mask;
1622
1623
1624 poll_wait(file, &ca->wait_queue, wait);
1625
1626 if (dvb_ca_en50221_io_read_condition(ca, &result, &slot) == 1) {
1627 mask |= POLLIN;
1628 }
1629
1630 return mask;
1631}
1632EXPORT_SYMBOL(dvb_ca_en50221_init);
1633
1634
1635static const struct file_operations dvb_ca_fops = {
1636 .owner = THIS_MODULE,
1637 .read = dvb_ca_en50221_io_read,
1638 .write = dvb_ca_en50221_io_write,
1639 .unlocked_ioctl = dvb_ca_en50221_io_ioctl,
1640 .open = dvb_ca_en50221_io_open,
1641 .release = dvb_ca_en50221_io_release,
1642 .poll = dvb_ca_en50221_io_poll,
1643 .llseek = noop_llseek,
1644};
1645
1646static struct dvb_device dvbdev_ca = {
1647 .priv = NULL,
1648 .users = 1,
1649 .readers = 1,
1650 .writers = 1,
1651 .fops = &dvb_ca_fops,
1652};
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669int dvb_ca_en50221_init(struct dvb_adapter *dvb_adapter,
1670 struct dvb_ca_en50221 *pubca, int flags, int slot_count)
1671{
1672 int ret;
1673 struct dvb_ca_private *ca = NULL;
1674 int i;
1675
1676 dprintk("%s\n", __func__);
1677
1678 if (slot_count < 1)
1679 return -EINVAL;
1680
1681
1682 if ((ca = kzalloc(sizeof(struct dvb_ca_private), GFP_KERNEL)) == NULL) {
1683 ret = -ENOMEM;
1684 goto error;
1685 }
1686 ca->pub = pubca;
1687 ca->flags = flags;
1688 ca->slot_count = slot_count;
1689 if ((ca->slot_info = kcalloc(slot_count, sizeof(struct dvb_ca_slot), GFP_KERNEL)) == NULL) {
1690 ret = -ENOMEM;
1691 goto error;
1692 }
1693 init_waitqueue_head(&ca->wait_queue);
1694 ca->open = 0;
1695 ca->wakeup = 0;
1696 ca->next_read_slot = 0;
1697 pubca->private = ca;
1698
1699
1700 ret = dvb_register_device(dvb_adapter, &ca->dvbdev, &dvbdev_ca, ca, DVB_DEVICE_CA);
1701 if (ret)
1702 goto error;
1703
1704
1705 for (i = 0; i < slot_count; i++) {
1706 memset(&ca->slot_info[i], 0, sizeof(struct dvb_ca_slot));
1707 ca->slot_info[i].slot_state = DVB_CA_SLOTSTATE_NONE;
1708 atomic_set(&ca->slot_info[i].camchange_count, 0);
1709 ca->slot_info[i].camchange_type = DVB_CA_EN50221_CAMCHANGE_REMOVED;
1710 mutex_init(&ca->slot_info[i].slot_lock);
1711 }
1712
1713 mutex_init(&ca->ioctl_mutex);
1714
1715 if (signal_pending(current)) {
1716 ret = -EINTR;
1717 goto error;
1718 }
1719 mb();
1720
1721
1722 ca->thread = kthread_run(dvb_ca_en50221_thread, ca, "kdvb-ca-%i:%i",
1723 ca->dvbdev->adapter->num, ca->dvbdev->id);
1724 if (IS_ERR(ca->thread)) {
1725 ret = PTR_ERR(ca->thread);
1726 printk("dvb_ca_init: failed to start kernel_thread (%d)\n",
1727 ret);
1728 goto error;
1729 }
1730 return 0;
1731
1732error:
1733 if (ca != NULL) {
1734 if (ca->dvbdev != NULL)
1735 dvb_unregister_device(ca->dvbdev);
1736 kfree(ca->slot_info);
1737 kfree(ca);
1738 }
1739 pubca->private = NULL;
1740 return ret;
1741}
1742EXPORT_SYMBOL(dvb_ca_en50221_release);
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752void dvb_ca_en50221_release(struct dvb_ca_en50221 *pubca)
1753{
1754 struct dvb_ca_private *ca = pubca->private;
1755 int i;
1756
1757 dprintk("%s\n", __func__);
1758
1759
1760 kthread_stop(ca->thread);
1761
1762 for (i = 0; i < ca->slot_count; i++) {
1763 dvb_ca_en50221_slot_shutdown(ca, i);
1764 vfree(ca->slot_info[i].rx_buffer.data);
1765 }
1766 kfree(ca->slot_info);
1767 dvb_unregister_device(ca->dvbdev);
1768 kfree(ca);
1769 pubca->private = NULL;
1770}
1771