[1226] | 1 | /*
|
---|
| 2 | ifd_phoenix.c
|
---|
| 3 | This module provides IFD handling functions for Smartmouse/Phoenix reader.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | #include <stdio.h>
|
---|
| 7 | //#include <time.h>
|
---|
| 8 | //#include <string.h>
|
---|
| 9 | //#include "ioctls.h"
|
---|
| 10 | #include "../globals.h"
|
---|
| 11 | #include "atr.h"
|
---|
| 12 | #include "ifd_towitoko.h" //FIXME
|
---|
[1254] | 13 | #include <termios.h>
|
---|
[1226] | 14 |
|
---|
[1255] | 15 | #define OK 1
|
---|
[1226] | 16 | #define ERROR 0
|
---|
| 17 |
|
---|
| 18 | #define IFD_TOWITOKO_MAX_TRANSMIT 255
|
---|
| 19 | #define IFD_TOWITOKO_ATR_TIMEOUT 800
|
---|
| 20 |
|
---|
| 21 | int Phoenix_Init ()
|
---|
| 22 | {
|
---|
| 23 | return OK;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | int Phoenix_GetStatus (int * status)
|
---|
| 27 | {
|
---|
| 28 | int in;
|
---|
| 29 | unsigned int modembits=0;
|
---|
| 30 | extern int oscam_card_detect; //FIXME kill global variable
|
---|
| 31 | if (ioctl(reader[ridx].handle, TIOCMGET,&modembits)<0)
|
---|
| 32 | return ERROR;
|
---|
| 33 | switch(oscam_card_detect&0x7f)
|
---|
| 34 | {
|
---|
| 35 | case 0: in=(modembits & TIOCM_CAR); break;
|
---|
| 36 | case 1: in=(modembits & TIOCM_DSR); break;
|
---|
| 37 | case 2: in=(modembits & TIOCM_CTS); break;
|
---|
| 38 | case 3: in=(modembits & TIOCM_RNG); break;
|
---|
| 39 | default: in=0; // dummy
|
---|
| 40 | }
|
---|
| 41 | if (!(oscam_card_detect&0x80))
|
---|
| 42 | in=!in;
|
---|
| 43 | *status = in;
|
---|
| 44 | return OK;
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | int Phoenix_Reset (ATR ** atr)
|
---|
[1255] | 48 | {
|
---|
| 49 |
|
---|
| 50 | #ifdef DEBUG_IFD
|
---|
| 51 | printf ("IFD: Resetting card:\n");
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
| 54 | int ret;
|
---|
| 55 | int i;
|
---|
| 56 | int parity[3] = {PARITY_EVEN, PARITY_ODD, PARITY_NONE};
|
---|
| 57 | #ifdef HAVE_NANOSLEEP
|
---|
| 58 | struct timespec req_ts;
|
---|
| 59 | req_ts.tv_sec = 0;
|
---|
| 60 | req_ts.tv_nsec = 50000000;
|
---|
| 61 | #endif
|
---|
| 62 |
|
---|
| 63 | (*atr) = NULL;
|
---|
| 64 | for(i=0; i<3; i++) {
|
---|
| 65 | IO_Serial_Flush();
|
---|
| 66 | if (!IO_Serial_SetParity (parity[i]))
|
---|
| 67 | return ERROR;
|
---|
| 68 |
|
---|
| 69 | ret = ERROR;
|
---|
| 70 | IO_Serial_Ioctl_Lock(1);
|
---|
| 71 | #ifdef USE_GPIO
|
---|
| 72 | if (gpio_detect){
|
---|
| 73 | set_gpio(0);
|
---|
| 74 | set_gpio1(0);
|
---|
| 75 | }
|
---|
| 76 | else
|
---|
| 77 | #endif
|
---|
| 78 | IO_Serial_RTS_Set();
|
---|
| 79 | #ifdef HAVE_NANOSLEEP
|
---|
| 80 | nanosleep (&req_ts, NULL);
|
---|
| 81 | #else
|
---|
| 82 | usleep (50000L);
|
---|
| 83 | #endif
|
---|
| 84 | #ifdef USE_GPIO
|
---|
| 85 | if (gpio_detect) {
|
---|
| 86 | set_gpio_input();
|
---|
| 87 | set_gpio1(1);
|
---|
| 88 | }
|
---|
| 89 | else
|
---|
| 90 | #endif
|
---|
| 91 | IO_Serial_RTS_Clr();
|
---|
| 92 | IO_Serial_Ioctl_Lock(0);
|
---|
| 93 | (*atr) = ATR_New ();
|
---|
| 94 | if(ATR_InitFromStream ((*atr), IFD_TOWITOKO_ATR_TIMEOUT) == ATR_OK)
|
---|
| 95 | ret = OK;
|
---|
| 96 | // Succesfully retrieve ATR
|
---|
| 97 | if (ret == OK)
|
---|
| 98 | break;
|
---|
| 99 | else
|
---|
| 100 | {
|
---|
| 101 | ATR_Delete (*atr);
|
---|
| 102 | (*atr) = NULL;
|
---|
| 103 | #ifdef USE_GPIO
|
---|
| 104 | if (gpio_detect) set_gpio1(0);
|
---|
| 105 | #endif
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | IO_Serial_Flush();
|
---|
| 109 |
|
---|
| 110 | /*
|
---|
| 111 | //PLAYGROUND faking ATR for test purposes only
|
---|
| 112 | //
|
---|
| 113 | // sky 919 unsigned char atr_test[] = { 0x3F, 0xFF, 0x13, 0x25, 0x03, 0x10, 0x80, 0x33, 0xB0, 0x0E, 0x69, 0xFF, 0x4A, 0x50, 0x70, 0x00, 0x00, 0x49, 0x54, 0x02, 0x00, 0x00 };
|
---|
| 114 | // HD+ unsigned char atr_test[] = { 0x3F, 0xFF, 0x95, 0x00, 0xFF, 0x91, 0x81, 0x71, 0xFE, 0x47, 0x00, 0x44, 0x4E, 0x41, 0x53, 0x50, 0x31, 0x34, 0x32, 0x20, 0x52, 0x65, 0x76, 0x47, 0x43, 0x34, 0x63 };
|
---|
| 115 | // S02 = irdeto unsigned char atr_test[] = { 0x3B, 0x9F, 0x21, 0x0E, 0x49, 0x52, 0x44, 0x45, 0x54, 0x4F, 0x20, 0x41, 0x43, 0x53, 0x03};
|
---|
| 116 | //cryptoworks unsigned char atr_test[] = { 0x3B, 0x78, 0x12, 0x00, 0x00, 0x65, 0xC4, 0x05, 0xFF, 0x8F, 0xF1, 0x90, 0x00 };
|
---|
| 117 | ATR_Delete(*atr); //throw away actual ATR
|
---|
| 118 | (*atr) = ATR_New ();
|
---|
| 119 | ATR_InitFromArray ((*atr), atr_test, sizeof(atr_test));
|
---|
| 120 | //END OF PLAYGROUND
|
---|
| 121 | */
|
---|
| 122 |
|
---|
| 123 | return ret;
|
---|
[1226] | 124 | }
|
---|
| 125 |
|
---|
| 126 | int Phoenix_Transmit (BYTE * buffer, unsigned size, IFD_Timings * timings)
|
---|
| 127 | {
|
---|
| 128 | unsigned block_delay, char_delay, sent=0, to_send = 0;
|
---|
| 129 |
|
---|
| 130 | #ifdef DEBUG_IFD
|
---|
| 131 | printf ("IFD: Transmit: ");
|
---|
| 132 | for (sent = 0; sent < size; sent++)
|
---|
| 133 | printf ("%X ", buffer[sent]);
|
---|
| 134 | printf ("\n");
|
---|
| 135 | #endif
|
---|
| 136 |
|
---|
| 137 | #define IFD_TOWITOKO_DELAY 0
|
---|
| 138 |
|
---|
| 139 | /* Calculate delays */
|
---|
| 140 | char_delay = IFD_TOWITOKO_DELAY + timings->char_delay;
|
---|
| 141 | block_delay = IFD_TOWITOKO_DELAY + timings->block_delay;
|
---|
| 142 |
|
---|
| 143 | #ifdef USE_GPIO
|
---|
| 144 | if (gpio_detect) set_gpio1(0);
|
---|
| 145 | #endif
|
---|
[1255] | 146 | for (sent = 0; sent < size; sent = sent + to_send)
|
---|
[1226] | 147 | {
|
---|
| 148 | /* Calculate number of bytes to send */
|
---|
| 149 | to_send = MIN(size, IFD_TOWITOKO_MAX_TRANSMIT);
|
---|
| 150 |
|
---|
| 151 | /* Send data */
|
---|
| 152 | if ((sent == 0) && (block_delay != char_delay))
|
---|
| 153 | {
|
---|
| 154 | if (!IO_Serial_Write (block_delay, 1, buffer))
|
---|
| 155 | return ERROR;
|
---|
| 156 |
|
---|
| 157 | if (!IO_Serial_Write (char_delay, to_send-1, buffer+1))
|
---|
| 158 | return ERROR;
|
---|
| 159 | }
|
---|
| 160 | else
|
---|
| 161 | {
|
---|
| 162 | if (!IO_Serial_Write (char_delay, to_send, buffer+sent))
|
---|
| 163 | return ERROR;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | #ifdef USE_GPIO
|
---|
| 167 | if (gpio_detect) set_gpio1(1);
|
---|
| 168 | #endif
|
---|
| 169 | return OK;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | int Phoenix_Receive (BYTE * buffer, unsigned size, IFD_Timings * timings)
|
---|
| 173 | {
|
---|
| 174 | unsigned char_timeout, block_timeout;
|
---|
| 175 | #ifdef DEBUG_IFD
|
---|
| 176 | int i;
|
---|
| 177 | #endif
|
---|
| 178 |
|
---|
| 179 | #define IFD_TOWITOKO_TIMEOUT 1000
|
---|
| 180 |
|
---|
| 181 | /* Calculate timeouts */
|
---|
| 182 | char_timeout = IFD_TOWITOKO_TIMEOUT + timings->char_timeout;
|
---|
| 183 | block_timeout = IFD_TOWITOKO_TIMEOUT + timings->block_timeout;
|
---|
| 184 | #ifdef USE_GPIO
|
---|
| 185 | if (gpio_detect) set_gpio1(0);
|
---|
| 186 | #endif
|
---|
| 187 | if (block_timeout != char_timeout)
|
---|
| 188 | {
|
---|
| 189 | /* Read first byte using block timeout */
|
---|
| 190 | if (!IO_Serial_Read (block_timeout, 1, buffer))
|
---|
| 191 | return ERROR;
|
---|
| 192 |
|
---|
| 193 | if (size > 1)
|
---|
| 194 | {
|
---|
| 195 | /* Read remaining data bytes using char timeout */
|
---|
| 196 | if (!IO_Serial_Read (char_timeout, size - 1, buffer + 1))
|
---|
| 197 | return ERROR;
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
| 200 | else
|
---|
| 201 | {
|
---|
| 202 | /* Read all data bytes with the same timeout */
|
---|
| 203 | if (!IO_Serial_Read (char_timeout, size, buffer))
|
---|
| 204 | return ERROR;
|
---|
| 205 | }
|
---|
| 206 | #ifdef USE_GPIO
|
---|
| 207 | if (gpio_detect) set_gpio1(1);
|
---|
| 208 | #endif
|
---|
| 209 |
|
---|
| 210 | #ifdef DEBUG_IFD
|
---|
| 211 | printf ("IFD: Receive: ");
|
---|
| 212 | for (i = 0; i < size; i++)
|
---|
| 213 | printf ("%X ", buffer[i]);
|
---|
| 214 | printf ("\n");
|
---|
| 215 | #endif
|
---|
| 216 |
|
---|
| 217 | return OK;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[1254] | 220 | int Phoenix_SetBaudrate (unsigned long baudrate)
|
---|
[1226] | 221 | {
|
---|
[1254] | 222 | if(reader[ridx].typ == R_INTERNAL)
|
---|
| 223 | return OK;
|
---|
| 224 |
|
---|
| 225 | #ifdef DEBUG_IFD
|
---|
| 226 | printf ("IFD: Setting baudrate to %lu\n", baudrate);
|
---|
| 227 | #endif
|
---|
| 228 | if (reader[ridx].baudrate == baudrate)
|
---|
| 229 | return OK;
|
---|
| 230 |
|
---|
| 231 | /* Get current settings */
|
---|
| 232 | struct termios tio;
|
---|
| 233 | if (tcgetattr (reader[ridx].handle, &tio) != 0)
|
---|
| 234 | return ERROR;
|
---|
| 235 |
|
---|
| 236 | //write baudrate here!
|
---|
| 237 | if (!IO_Serial_SetBitrate (baudrate, &tio))
|
---|
| 238 | return ERROR;
|
---|
| 239 |
|
---|
| 240 | if (!IO_Serial_SetProperties(tio))
|
---|
| 241 | return ERROR;
|
---|
| 242 |
|
---|
| 243 | reader[ridx].baudrate = baudrate;
|
---|
| 244 |
|
---|
[1226] | 245 | return OK;
|
---|
| 246 | }
|
---|