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