1 | #ifdef COOL
|
---|
2 | /*
|
---|
3 | ifd_cool.c
|
---|
4 | This module provides IFD handling functions for Coolstream internal reader.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include <stdio.h>
|
---|
8 | #include <time.h>
|
---|
9 | #include <string.h>
|
---|
10 | #include"ifd_cool.h"
|
---|
11 | #include"../globals.h"
|
---|
12 | #include"icc_async.h"
|
---|
13 |
|
---|
14 | struct s_coolstream_reader {
|
---|
15 | void *handle; //device handle for coolstream
|
---|
16 | char cardbuffer[256];
|
---|
17 | int32_t cardbuflen;
|
---|
18 | };
|
---|
19 |
|
---|
20 | #define specdev() \
|
---|
21 | ((struct s_coolstream_reader *)cur_client()->reader->spec_dev)
|
---|
22 |
|
---|
23 | int32_t Cool_Init (char *device)
|
---|
24 | {
|
---|
25 | cnxt_smc_init (NULL); //not sure whether this should be in coolapi_open_all
|
---|
26 | int32_t reader_nb = 0;
|
---|
27 | // this is to stay compatible with older config.
|
---|
28 | if(strlen(device))
|
---|
29 | reader_nb=atoi((const char *)device);
|
---|
30 | if(reader_nb>1) {
|
---|
31 | // there are only 2 readers in the coolstream : 0 or 1
|
---|
32 | cs_log("Coolstream reader device can only be 0 or 1");
|
---|
33 | return FALSE;
|
---|
34 | }
|
---|
35 | cur_client()->reader->spec_dev=malloc(sizeof(struct s_coolstream_reader));
|
---|
36 | if (cnxt_smc_open (&specdev()->handle, &reader_nb))
|
---|
37 | return FALSE;
|
---|
38 | specdev()->cardbuflen = 0;
|
---|
39 | return OK;
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | int32_t Cool_GetStatus (int32_t * in)
|
---|
44 | {
|
---|
45 | int32_t state;
|
---|
46 | int32_t ret = cnxt_smc_get_state(specdev()->handle, &state);
|
---|
47 | if (ret) {
|
---|
48 | cs_log("COOLSTREAM return code = %i", ret);
|
---|
49 | return ERROR;
|
---|
50 | }
|
---|
51 | //state = 0 no card, 1 = not ready, 2 = ready
|
---|
52 | if (state)
|
---|
53 | *in = 1; //CARD, even if not ready report card is in, or it will never get activated
|
---|
54 | else
|
---|
55 | *in = 0; //NOCARD
|
---|
56 | return OK;
|
---|
57 | }
|
---|
58 |
|
---|
59 | int32_t Cool_Reset (ATR * atr)
|
---|
60 | {
|
---|
61 | call (Cool_SetClockrate(357));
|
---|
62 |
|
---|
63 | //reset card
|
---|
64 | int32_t timeout = 5000; // Timout in ms?
|
---|
65 | call (cnxt_smc_reset_card (specdev()->handle, ATR_TIMEOUT, NULL, NULL));
|
---|
66 | cs_sleepms(50);
|
---|
67 | int32_t n = 40;
|
---|
68 | unsigned char buf[40];
|
---|
69 | call (cnxt_smc_get_atr (specdev()->handle, buf, &n));
|
---|
70 |
|
---|
71 | call (!ATR_InitFromArray (atr, buf, n) == ATR_OK);
|
---|
72 | {
|
---|
73 | cs_sleepms(50);
|
---|
74 | return OK;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | int32_t Cool_Transmit (BYTE * sent, uint32_t size)
|
---|
79 | {
|
---|
80 | specdev()->cardbuflen = 256;//it needs to know max buffer size to respond?
|
---|
81 | call (cnxt_smc_read_write(specdev()->handle, FALSE, sent, size, specdev()->cardbuffer, &specdev()->cardbuflen, 50, 0));
|
---|
82 | //call (cnxt_smc_read_write(specdev()->handle, FALSE, sent, size, specdev()->cardbuffer, &specdev()->cardbuflen, read_timeout, 0));
|
---|
83 | cs_ddump_mask(D_DEVICE, sent, size, "COOL IO: Transmit: ");
|
---|
84 | return OK;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int32_t Cool_Receive (BYTE * data, uint32_t size)
|
---|
88 | {
|
---|
89 | if (size > specdev()->cardbuflen)
|
---|
90 | size = specdev()->cardbuflen; //never read past end of buffer
|
---|
91 | memcpy(data, specdev()->cardbuffer, size);
|
---|
92 | specdev()->cardbuflen -= size;
|
---|
93 | memmove(specdev()->cardbuffer, specdev()->cardbuffer+size, specdev()->cardbuflen);
|
---|
94 | cs_ddump_mask(D_DEVICE, data, size, "COOL IO: Receive: ");
|
---|
95 | return OK;
|
---|
96 | }
|
---|
97 |
|
---|
98 | int32_t Cool_SetClockrate (int32_t mhz)
|
---|
99 | {
|
---|
100 | uint32_t clk;
|
---|
101 | clk = mhz * 10000;
|
---|
102 | call (cnxt_smc_set_clock_freq (specdev()->handle, clk));
|
---|
103 | cs_debug_mask(D_DEVICE, "COOL: Clock succesfully set to %i0 kHz", mhz);
|
---|
104 | return OK;
|
---|
105 | }
|
---|
106 |
|
---|
107 | int32_t Cool_WriteSettings (uint32_t BWT, uint32_t CWT, uint32_t EGT, uint32_t BGT)
|
---|
108 | {
|
---|
109 | //this code worked with old cnxt_lnx.ko, but prevented nagra cards from working with new cnxt_lnx.ko
|
---|
110 | /* struct
|
---|
111 | {
|
---|
112 | unsigned short CardActTime; //card activation time (in clock cycles = 1/54Mhz)
|
---|
113 | unsigned short CardDeactTime; //card deactivation time (in clock cycles = 1/54Mhz)
|
---|
114 | unsigned short ATRSTime; //ATR first char timeout in clock cycles (1/f)
|
---|
115 | unsigned short ATRDTime; //ATR duration in ETU
|
---|
116 | unsigned long BWT;
|
---|
117 | unsigned long CWT;
|
---|
118 | unsigned char EGT;
|
---|
119 | unsigned char BGT;
|
---|
120 | } params;
|
---|
121 | params.BWT = BWT;
|
---|
122 | params.CWT = CWT;
|
---|
123 | params.EGT = EGT;
|
---|
124 | params.BGT = BGT;
|
---|
125 | call (cnxt_smc_set_config_timeout(specdev()->handle, params));
|
---|
126 | cs_debug_mask(D_DEVICE, "COOL WriteSettings OK");*/
|
---|
127 | return OK;
|
---|
128 | }
|
---|
129 |
|
---|
130 | int32_t Cool_FastReset ()
|
---|
131 | {
|
---|
132 | int32_t n = 40;
|
---|
133 | unsigned char buf[40];
|
---|
134 |
|
---|
135 | //reset card
|
---|
136 | call (cnxt_smc_reset_card (specdev()->handle, ATR_TIMEOUT, NULL, NULL));
|
---|
137 | cs_sleepms(50);
|
---|
138 | call (cnxt_smc_get_atr (specdev()->handle, buf, &n));
|
---|
139 |
|
---|
140 | return 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 | int32_t Cool_Close (void)
|
---|
144 | {
|
---|
145 | call(cnxt_smc_close (specdev()->handle));
|
---|
146 | NULLFREE(cur_client()->reader->spec_dev);
|
---|
147 | call(cnxt_kal_terminate()); //should call this only once in a thread
|
---|
148 | cnxt_drv_term();
|
---|
149 | return OK;
|
---|
150 | }
|
---|
151 |
|
---|
152 | #endif
|
---|