1 #include "../globals.h"
3 #ifdef CARDREADER_SMARGO
4 #include "../oscam-time.h"
8 #if defined(__CYGWIN__)
17 #define LOBYTE(w) ((unsigned char)((w) & 0xff))
18 #define HIBYTE(w) ((unsigned char)((w) >> 8))
20 #define SMARGO_DELAY 150
22 static void smargo_set_config_mode_on(struct s_reader
*reader
)
26 tcgetattr(reader
->handle
, &term
);
27 term
.c_cflag
&= ~CSIZE
;
29 tcsetattr(reader
->handle
, TCSANOW
, &term
);
31 cs_sleepms(SMARGO_DELAY
);
34 static void smargo_set_config_mode_off(struct s_reader
*reader
)
38 cs_sleepms(SMARGO_DELAY
);
40 tcgetattr(reader
->handle
, &term
);
41 term
.c_cflag
&= ~CSIZE
;
43 tcsetattr(reader
->handle
, TCSANOW
, &term
);
46 static int32_t smargo_set_settings(struct s_reader
*reader
, int32_t freq
, unsigned char T
, unsigned char inv
, uint16_t Fi
, unsigned char Di
, unsigned char Ni
)
48 uint16_t freqk
= (freq
* 10);
51 smargo_set_config_mode_on(reader
);
53 rdr_log_dbg(reader
, D_DEVICE
, "sending F=%04X (%d), D=%02X (%d), Freq=%04X (%d), N=%02X (%d), T=%02X (%d), inv=%02X (%d)",
54 Fi
, Fi
, Di
, Di
, freqk
, freqk
, Ni
, Ni
, T
, T
, inv
, inv
);
56 if(T
!= 14 || freq
== 369)
62 IO_Serial_Write(reader
, 0, 1000, 4, data
);
66 data
[1] = HIBYTE(freqk
);
67 data
[2] = LOBYTE(freqk
);
68 IO_Serial_Write(reader
, 0, 1000, 3, data
);
72 IO_Serial_Write(reader
, 0, 1000, 2, data
);
76 IO_Serial_Write(reader
, 0, 1000, 2, data
);
80 IO_Serial_Write(reader
, 0, 1000, 2, data
);
82 smargo_set_config_mode_off(reader
);
87 static int32_t smargo_write_settings(struct s_reader
*reader
, struct s_cardreader_settings
*s
)
89 return smargo_set_settings(reader
, reader
->mhz
, reader
->protocol_type
== 1 ? 0 : reader
->protocol_type
, reader
->convention
, s
->Fi
, s
->D
, s
->Ni
);
93 static int32_t smargo_init(struct s_reader
*reader
)
95 reader
->handle
= open(reader
->device
, O_RDWR
);
97 if(reader
->handle
< 0)
99 rdr_log(reader
, "ERROR: Opening device %s (errno=%d %s)", reader
->device
, errno
, strerror(errno
));
103 if(IO_Serial_SetParams(reader
, DEFAULT_BAUDRATE
, 8, PARITY_EVEN
, 2, NULL
, NULL
))
106 IO_Serial_RTS_Set(reader
);
107 IO_Serial_DTR_Set(reader
);
108 IO_Serial_Flush(reader
);
113 static int32_t smargo_Serial_Read(struct s_reader
*reader
, uint32_t timeout
, uint32_t size
, unsigned char *data
, int32_t *read_bytes
)
116 uint32_t bytes_read
= 0;
118 for(count
= 0; count
< size
; count
+= bytes_read
)
120 if(IO_Serial_WaitToRead(reader
, 0, timeout
) == OK
)
122 if((bytes_read
= read(reader
->handle
, data
+ count
, size
- count
)) < 1)
124 int saved_errno
= errno
;
125 rdr_log_dump_dbg(reader
, D_DEVICE
, data
, count
, "Receiving:");
126 rdr_log(reader
, "ERROR: %s (errno=%d %s)", __func__
, saved_errno
, strerror(saved_errno
));
132 rdr_log_dump_dbg(reader
, D_DEVICE
, data
, count
, "Receiving:");
133 rdr_log_dbg(reader
, D_DEVICE
, "Timeout in IO_Serial_Read");
138 rdr_log_dump_dbg(reader
, D_DEVICE
, data
, count
, "Receiving:");
142 static int32_t smargo_fast_reset_by_atr(struct s_reader
*reader
, ATR
*atr
)
145 unsigned char buf
[ATR_MAX_SIZE
];
149 if(reader
->seca_nagra_card
== 1)
151 atr_len
= reader
->card_atr_length
; // this is a special case the data buffer has only the atr length.
155 atr_len
= reader
->card_atr_length
+ 2; // data buffer has atr length + 2 bytes
158 IO_Serial_Read(reader
, 0, 500000, atr_len
, buf
);
160 IO_Serial_RTS_Set(reader
);
162 IO_Serial_RTS_Clr(reader
);
164 smargo_Serial_Read(reader
, ATR_TIMEOUT
, atr_len
+ 1, buf
, &n
);
166 if(ATR_InitFromArray(atr
, buf
, n
) != ERROR
)
168 rdr_log_dbg(reader
, D_DEVICE
, "SR: ATR parsing OK");
175 static int32_t smargo_reset(struct s_reader
*reader
, ATR
*atr
)
177 rdr_log_dbg(reader
, D_IFD
, "Resetting card");
180 unsigned char buf
[ATR_MAX_SIZE
];
182 int32_t parity
[4] = {PARITY_EVEN
, PARITY_ODD
, PARITY_NONE
, PARITY_EVEN
};
186 if(reader
->mhz
== reader
->cardmhz
&& reader
->cardmhz
> 369)
187 { mhz
= reader
->cardmhz
; }
189 for(i
= 0; i
< 4; i
++)
191 if(i
== 3) // hack for irdeto cards
192 { smargo_set_settings(reader
, 600, 1, 0, 618, 1, 0); }
194 { smargo_set_settings(reader
, mhz
, 0, 0, 372, 1, 0); }
196 call(IO_Serial_SetParity(reader
, parity
[i
]));
198 //IO_Serial_Flush(reader);
200 IO_Serial_Read(reader
, 0, 500000, ATR_MAX_SIZE
, buf
);
202 IO_Serial_RTS_Set(reader
);
204 IO_Serial_RTS_Clr(reader
);
208 smargo_Serial_Read(reader
, ATR_TIMEOUT
, ATR_MAX_SIZE
, buf
, &n
);
210 if(n
== 0 || buf
[0] == 0)
213 rdr_log_dump_dbg(reader
, D_IFD
, buf
, n
, "ATR: %d bytes", n
);
215 if((buf
[0] != 0x3B && buf
[0] != 0x03 && buf
[0] != 0x3F) || (buf
[1] == 0xFF && buf
[2] == 0x00))
216 { continue; } // this is not a valid ATR
218 if(ATR_InitFromArray(atr
, buf
, n
) != ERROR
)
227 ATR_GetConvention(atr
, &convention
);
228 // If inverse convention, switch here due to if not PTS will fail
229 if(convention
== ATR_CONVENTION_INVERSE
)
233 smargo_set_config_mode_on(reader
);
237 IO_Serial_Write(reader
, 0, 1000, 2, data
);
239 smargo_set_config_mode_off(reader
);
245 int32_t smargo_activate(struct s_reader
*reader
, struct s_ATR
*atr
)
247 if(!reader
->ins7e11_fast_reset
)
249 call(smargo_reset(reader
, atr
));
253 rdr_log_dbg(reader
, D_DEVICE
, "Fast card reset with atr");
254 call(smargo_fast_reset_by_atr(reader
, atr
));
259 const struct s_cardreader cardreader_smargo
=
263 .max_clock_speed
= 1,
264 .reader_init
= smargo_init
,
265 .activate
= smargo_activate
,
266 .write_settings
= smargo_write_settings
,
267 .get_status
= IO_Serial_GetStatus
,
268 .transmit
= IO_Serial_Transmit
,
269 .receive
= IO_Serial_Receive
,
270 .close
= IO_Serial_Close
,
271 .set_parity
= IO_Serial_SetParity
,