1 | /*
|
---|
2 | ifd_smartreader.c
|
---|
3 | This module provides IFD handling functions for for Argolis smartreader+.
|
---|
4 | */
|
---|
5 |
|
---|
6 | #include "../globals.h"
|
---|
7 |
|
---|
8 | #ifdef CARDREADER_SMART
|
---|
9 | #include <memory.h>
|
---|
10 | #if defined(__FreeBSD__)
|
---|
11 | #include <libusb.h>
|
---|
12 | #else
|
---|
13 | #include <libusb-1.0/libusb.h>
|
---|
14 | #endif
|
---|
15 | #include "../oscam-lock.h"
|
---|
16 | #include "../oscam-string.h"
|
---|
17 | #include "../oscam-time.h"
|
---|
18 | #include "atr.h"
|
---|
19 | #include "ifd_smartreader_types.h"
|
---|
20 |
|
---|
21 | #if defined(__CYGWIN__)
|
---|
22 | #undef OK
|
---|
23 | #undef ERROR
|
---|
24 | #undef LOBYTE
|
---|
25 | #undef HIBYTE
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | #define OK 0
|
---|
29 | #define ERROR 1
|
---|
30 | #define LOBYTE(w) ((unsigned char)((w) & 0xff))
|
---|
31 | #define HIBYTE(w) ((unsigned char)((w) >> 8))
|
---|
32 |
|
---|
33 | //The number of concurrent bulk reads to queue onto the smartreader
|
---|
34 | #define NUM_TXFERS 2
|
---|
35 |
|
---|
36 | static CS_MUTEX_LOCK sr_lock;
|
---|
37 |
|
---|
38 | struct s_sr_config {
|
---|
39 | int32_t F;
|
---|
40 | float D;
|
---|
41 | int32_t fs;
|
---|
42 | int32_t N;
|
---|
43 | int32_t T;
|
---|
44 | int32_t inv;
|
---|
45 | int32_t parity;
|
---|
46 | int32_t irdeto;
|
---|
47 | int32_t running;
|
---|
48 | libusb_device *usb_dev;
|
---|
49 | libusb_device_handle *usb_dev_handle;
|
---|
50 | enum smartreader_chip_type type;
|
---|
51 | uint8_t in_ep; // 0x01
|
---|
52 | uint8_t out_ep; // 0x82
|
---|
53 | int32_t index;
|
---|
54 | /** usb read timeout */
|
---|
55 | int32_t usb_read_timeout;
|
---|
56 | /** usb write timeout */
|
---|
57 | int32_t usb_write_timeout;
|
---|
58 | uint32_t writebuffer_chunksize;
|
---|
59 | unsigned char bitbang_enabled;
|
---|
60 | int32_t baudrate;
|
---|
61 | int32_t interface; // 0 or 1
|
---|
62 | /** maximum packet size. Needed for filtering modem status bytes every n packets. */
|
---|
63 | uint32_t max_packet_size;
|
---|
64 | unsigned char g_read_buffer[4096];
|
---|
65 | uint32_t g_read_buffer_size;
|
---|
66 | pthread_mutex_t g_read_mutex;
|
---|
67 | pthread_cond_t g_read_cond;
|
---|
68 | pthread_mutex_t g_usb_mutex;
|
---|
69 | pthread_cond_t g_usb_cond;
|
---|
70 | int32_t poll;
|
---|
71 | pthread_t rt;
|
---|
72 | unsigned char modem_status;
|
---|
73 | };
|
---|
74 |
|
---|
75 | static int32_t init_count = 0;
|
---|
76 |
|
---|
77 | static int32_t smart_read(struct s_reader *reader, unsigned char* buff, uint32_t size, int32_t timeout_sec)
|
---|
78 | {
|
---|
79 | int32_t ret = 0;
|
---|
80 | uint32_t total_read = 0;
|
---|
81 | struct timeval start, now, dif;
|
---|
82 | struct timespec timeout;
|
---|
83 |
|
---|
84 | memset(&dif, 0, sizeof(struct timeval));
|
---|
85 |
|
---|
86 | gettimeofday(&start, NULL);
|
---|
87 | timeout.tv_sec = start.tv_sec + timeout_sec;
|
---|
88 | timeout.tv_nsec = start.tv_usec * 1000;
|
---|
89 |
|
---|
90 | while(total_read < size && dif.tv_sec < timeout_sec) {
|
---|
91 | pthread_mutex_lock(&reader->sr_config->g_read_mutex);
|
---|
92 |
|
---|
93 | while (reader->sr_config->g_read_buffer_size == 0 && dif.tv_sec < timeout_sec)
|
---|
94 | {
|
---|
95 | pthread_cond_timedwait(&reader->sr_config->g_read_cond, &reader->sr_config->g_read_mutex, &timeout);
|
---|
96 | gettimeofday(&now,NULL);
|
---|
97 | timersub(&now, &start, &dif);
|
---|
98 | }
|
---|
99 |
|
---|
100 | ret = (reader->sr_config->g_read_buffer_size > size-total_read ? size - total_read : reader->sr_config->g_read_buffer_size);
|
---|
101 | memcpy(buff + total_read, reader->sr_config->g_read_buffer,ret);
|
---|
102 | reader->sr_config->g_read_buffer_size -= ret;
|
---|
103 |
|
---|
104 | if(reader->sr_config->g_read_buffer_size > 0)
|
---|
105 | memmove(reader->sr_config->g_read_buffer, reader->sr_config->g_read_buffer + ret, reader->sr_config->g_read_buffer_size);
|
---|
106 |
|
---|
107 | total_read += ret;
|
---|
108 | pthread_mutex_unlock(&reader->sr_config->g_read_mutex);
|
---|
109 |
|
---|
110 | gettimeofday(&now, NULL);
|
---|
111 | timersub(&now, &start, &dif);
|
---|
112 | }
|
---|
113 |
|
---|
114 | rdr_ddump_mask(reader, D_DEVICE, buff, total_read, "SR: Receive:");
|
---|
115 | return total_read;
|
---|
116 | }
|
---|
117 |
|
---|
118 | static int32_t smart_write(struct s_reader *reader, unsigned char* buff, uint32_t size)
|
---|
119 | {
|
---|
120 | int32_t write_size;
|
---|
121 | uint32_t offset = 0;
|
---|
122 | int32_t total_written = 0;
|
---|
123 | int32_t written;
|
---|
124 |
|
---|
125 | if(size<reader->sr_config->writebuffer_chunksize)
|
---|
126 | write_size=size;
|
---|
127 | else
|
---|
128 | write_size = reader->sr_config->writebuffer_chunksize;
|
---|
129 |
|
---|
130 | while (offset < size)
|
---|
131 | {
|
---|
132 | if (offset+write_size > size)
|
---|
133 | write_size = size-offset;
|
---|
134 |
|
---|
135 | int32_t ret = libusb_bulk_transfer(reader->sr_config->usb_dev_handle,
|
---|
136 | reader->sr_config->in_ep,
|
---|
137 | buff+offset,
|
---|
138 | write_size,
|
---|
139 | &written,
|
---|
140 | reader->sr_config->usb_write_timeout);
|
---|
141 | if (ret < 0) {
|
---|
142 | rdr_log(reader, "usb bulk write failed : ret = %d",ret);
|
---|
143 | return(ret);
|
---|
144 | }
|
---|
145 | rdr_ddump_mask(reader, D_DEVICE, buff+offset, written, "SR: Transmit:");
|
---|
146 | total_written += written;
|
---|
147 | offset += write_size;
|
---|
148 | }
|
---|
149 |
|
---|
150 | return total_written;
|
---|
151 | }
|
---|
152 |
|
---|
153 | static bool smartreader_check_endpoint(libusb_device *usb_dev,uint8_t out_endpoint,uint8_t in_endpoint)
|
---|
154 | {
|
---|
155 | struct libusb_device_descriptor usbdesc;
|
---|
156 | struct libusb_config_descriptor *configDesc;
|
---|
157 | int32_t ret;
|
---|
158 | int32_t j,k,l;
|
---|
159 | uint8_t tmpEndpointAddress;
|
---|
160 | int32_t nb_endpoint_ok;
|
---|
161 |
|
---|
162 |
|
---|
163 | nb_endpoint_ok=0;
|
---|
164 | ret = libusb_get_device_descriptor(usb_dev, &usbdesc);
|
---|
165 | if (ret < 0) {
|
---|
166 | cs_log("Smartreader : couldn't read device descriptor, assuming this is not a smartreader");
|
---|
167 | return 0;
|
---|
168 | }
|
---|
169 | if (usbdesc.bNumConfigurations) {
|
---|
170 | ret=libusb_get_active_config_descriptor(usb_dev,&configDesc);
|
---|
171 | if(ret) {
|
---|
172 | cs_log("Smartreader : couldn't read config descriptor , assuming this is not a smartreader");
|
---|
173 | return 0;
|
---|
174 | }
|
---|
175 |
|
---|
176 | for(j=0; j<configDesc->bNumInterfaces; j++)
|
---|
177 | for(k=0; k<configDesc->interface[j].num_altsetting; k++)
|
---|
178 | for(l=0; l<configDesc->interface[j].altsetting[k].bNumEndpoints; l++) {
|
---|
179 | tmpEndpointAddress=configDesc->interface[j].altsetting[k].endpoint[l].bEndpointAddress;
|
---|
180 | if((tmpEndpointAddress == in_endpoint) || (tmpEndpointAddress == out_endpoint))
|
---|
181 | nb_endpoint_ok++;
|
---|
182 | }
|
---|
183 | }
|
---|
184 | if(nb_endpoint_ok!=2)
|
---|
185 | return 0;
|
---|
186 | return 1;
|
---|
187 | }
|
---|
188 |
|
---|
189 | static struct libusb_device* find_smartreader(const char *busname,const char *dev_name, uint8_t out_endpoint, uint8_t in_endpoint)
|
---|
190 | {
|
---|
191 | int32_t dev_found = 0;
|
---|
192 | libusb_device *dev;
|
---|
193 | libusb_device_handle *usb_dev_handle;
|
---|
194 | libusb_device **devs;
|
---|
195 | ssize_t cnt;
|
---|
196 | int32_t i = 0;
|
---|
197 | int32_t ret;
|
---|
198 | struct libusb_device_descriptor usbdesc;
|
---|
199 |
|
---|
200 | cnt = libusb_get_device_list(NULL, &devs);
|
---|
201 | if (cnt < 0)
|
---|
202 | return NULL;
|
---|
203 |
|
---|
204 | while ((dev = devs[i++]) != NULL) {
|
---|
205 | dev_found=0;
|
---|
206 | ret = libusb_get_device_descriptor(dev, &usbdesc);
|
---|
207 | if (ret < 0) {
|
---|
208 | cs_log("failed to get device descriptor for device %s on bus %s", dev_name, busname);
|
---|
209 | return NULL;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (usbdesc.idVendor==0x0403 && (usbdesc.idProduct==0x6001 || usbdesc.idProduct==0x6011)) {
|
---|
213 | ret=libusb_open(dev,&usb_dev_handle);
|
---|
214 | if (ret) {
|
---|
215 | cs_log ("coulnd't open device %03d:%03d", libusb_get_bus_number(dev), libusb_get_device_address(dev));
|
---|
216 | switch(ret) {
|
---|
217 | case LIBUSB_ERROR_NO_MEM:
|
---|
218 | cs_log("libusb_open error LIBUSB_ERROR_NO_MEM : memory allocation failure");
|
---|
219 | break;
|
---|
220 | case LIBUSB_ERROR_ACCESS:
|
---|
221 | cs_log("libusb_open error LIBUSB_ERROR_ACCESS : the user has insufficient permissions");
|
---|
222 | break;
|
---|
223 | case LIBUSB_ERROR_NO_DEVICE:
|
---|
224 | cs_log("libusb_open error LIBUSB_ERROR_NO_DEVICE : the device has been disconnected");
|
---|
225 | break;
|
---|
226 | default:
|
---|
227 | cs_log("libusb_open unknown error : %d", ret);
|
---|
228 | break;
|
---|
229 | }
|
---|
230 | continue;
|
---|
231 | }
|
---|
232 |
|
---|
233 | // If the device is specified as "Serial:number", check iSerial
|
---|
234 | if(!strcmp(busname,"Serial")) {
|
---|
235 | char iserialbuffer[128];
|
---|
236 | if(libusb_get_string_descriptor_ascii(usb_dev_handle,usbdesc.iSerialNumber,(unsigned char *)iserialbuffer,sizeof(iserialbuffer))>0) {
|
---|
237 | if(!strcmp(trim(iserialbuffer),dev_name)) {
|
---|
238 | cs_log("Found reader with serial %s at %03d:%03d",dev_name,libusb_get_bus_number(dev),libusb_get_device_address(dev));
|
---|
239 | if(smartreader_check_endpoint(dev,out_endpoint,in_endpoint))
|
---|
240 | dev_found=1;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|
244 | else if(libusb_get_bus_number(dev)==atoi(busname) && libusb_get_device_address(dev)==atoi(dev_name)) {
|
---|
245 | cs_debug_mask(D_DEVICE, "SR: Checking FTDI device: %03d on bus %03d",libusb_get_device_address(dev),libusb_get_bus_number(dev));
|
---|
246 | // check for smargo endpoints.
|
---|
247 | if(smartreader_check_endpoint(dev,out_endpoint,in_endpoint))
|
---|
248 | dev_found=1;
|
---|
249 | }
|
---|
250 | libusb_close(usb_dev_handle);
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (dev_found)
|
---|
254 | break;
|
---|
255 | }
|
---|
256 |
|
---|
257 | if(!dev_found) {
|
---|
258 | cs_log("Smartreader device %s:%s not found",busname,dev_name);
|
---|
259 | return NULL;
|
---|
260 | }
|
---|
261 | else
|
---|
262 | cs_log("Found smartreader device %s:%s",busname,dev_name);
|
---|
263 |
|
---|
264 | return dev;
|
---|
265 | }
|
---|
266 |
|
---|
267 | void smartreader_init(struct s_reader *reader,uint8_t out_endpoint,uint8_t in_endpoint)
|
---|
268 | {
|
---|
269 | reader->sr_config->usb_dev = NULL;
|
---|
270 | reader->sr_config->usb_dev_handle=NULL;
|
---|
271 | reader->sr_config->usb_read_timeout = 10000;
|
---|
272 | reader->sr_config->usb_write_timeout = 10000;
|
---|
273 |
|
---|
274 | reader->sr_config->type = TYPE_BM; /* chip type */
|
---|
275 | reader->sr_config->baudrate = -1;
|
---|
276 | reader->sr_config->bitbang_enabled = 0; /* 0: normal mode 1: any of the bitbang modes enabled */
|
---|
277 |
|
---|
278 | reader->sr_config->writebuffer_chunksize = 64;
|
---|
279 | reader->sr_config->max_packet_size = 0;
|
---|
280 |
|
---|
281 | reader->sr_config->interface = INTERFACE_ANY;
|
---|
282 | reader->sr_config->index = INTERFACE_A;
|
---|
283 | reader->sr_config->in_ep = in_endpoint;
|
---|
284 | reader->sr_config->out_ep = out_endpoint;
|
---|
285 | }
|
---|
286 |
|
---|
287 |
|
---|
288 | static uint32_t smartreader_determine_max_packet_size(struct s_reader *reader)
|
---|
289 | {
|
---|
290 | uint32_t packet_size;
|
---|
291 | struct libusb_device_descriptor usbdesc;
|
---|
292 | struct libusb_config_descriptor *configDesc;
|
---|
293 | struct libusb_interface interface;
|
---|
294 | struct libusb_interface_descriptor intDesc;
|
---|
295 |
|
---|
296 | int32_t ret;
|
---|
297 | // Determine maximum packet size. Init with default value.
|
---|
298 | // New hi-speed devices from FTDI use a packet size of 512 bytes
|
---|
299 | // but could be connected to a normal speed USB hub -> 64 bytes packet size.
|
---|
300 | if (reader->sr_config->type == TYPE_2232H || reader->sr_config->type == TYPE_4232H)
|
---|
301 | packet_size = 512;
|
---|
302 | else
|
---|
303 | packet_size = 64;
|
---|
304 |
|
---|
305 | ret = libusb_get_device_descriptor(reader->sr_config->usb_dev, &usbdesc);
|
---|
306 | if (ret < 0) {
|
---|
307 | rdr_log(reader, "Smartreader : couldn't read device descriptor , using default packet size");
|
---|
308 | return packet_size;
|
---|
309 | }
|
---|
310 | if (usbdesc.bNumConfigurations)
|
---|
311 | {
|
---|
312 | ret=libusb_get_active_config_descriptor(reader->sr_config->usb_dev,&configDesc);
|
---|
313 | if(ret) {
|
---|
314 | rdr_log(reader, "Smartreader : couldn't read config descriptor , using default packet size");
|
---|
315 | return packet_size;
|
---|
316 | }
|
---|
317 |
|
---|
318 | if (reader->sr_config->interface < configDesc->bNumInterfaces)
|
---|
319 | {
|
---|
320 | interface=configDesc->interface[reader->sr_config->interface];
|
---|
321 | if (interface.num_altsetting > 0)
|
---|
322 | {
|
---|
323 | intDesc = interface.altsetting[0];
|
---|
324 | if (intDesc.bNumEndpoints > 0)
|
---|
325 | {
|
---|
326 | packet_size = intDesc.endpoint[0].wMaxPacketSize;
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | return packet_size;
|
---|
333 | }
|
---|
334 |
|
---|
335 |
|
---|
336 | static int32_t smartreader_usb_close_internal (struct s_reader *reader)
|
---|
337 | {
|
---|
338 | int32_t ret = 0;
|
---|
339 |
|
---|
340 | if (reader->sr_config->usb_dev_handle)
|
---|
341 | {
|
---|
342 | libusb_close (reader->sr_config->usb_dev_handle);
|
---|
343 | reader->sr_config->usb_dev_handle=NULL;
|
---|
344 | }
|
---|
345 |
|
---|
346 | return ret;
|
---|
347 | }
|
---|
348 |
|
---|
349 |
|
---|
350 | static int32_t smartreader_usb_reset(struct s_reader *reader)
|
---|
351 | {
|
---|
352 | if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
|
---|
353 | FTDI_DEVICE_OUT_REQTYPE,
|
---|
354 | SIO_RESET_REQUEST,
|
---|
355 | SIO_RESET_SIO,
|
---|
356 | reader->sr_config->index,
|
---|
357 | NULL,
|
---|
358 | 0,
|
---|
359 | reader->sr_config->usb_write_timeout) != 0) {
|
---|
360 | rdr_log(reader, "Smartreader reset failed");
|
---|
361 | return (-1);
|
---|
362 | }
|
---|
363 |
|
---|
364 |
|
---|
365 | return 0;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | static int32_t smartreader_usb_purge_rx_buffer(struct s_reader *reader)
|
---|
370 | {
|
---|
371 | if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
|
---|
372 | FTDI_DEVICE_OUT_REQTYPE,
|
---|
373 | SIO_RESET_REQUEST,
|
---|
374 | SIO_RESET_PURGE_RX,
|
---|
375 | reader->sr_config->index,
|
---|
376 | NULL,
|
---|
377 | 0,
|
---|
378 | reader->sr_config->usb_write_timeout) != 0) {
|
---|
379 | rdr_log(reader, "FTDI purge of RX buffer failed");
|
---|
380 | return (-1);
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | return 0;
|
---|
385 | }
|
---|
386 |
|
---|
387 | static int32_t smartreader_usb_purge_tx_buffer(struct s_reader *reader)
|
---|
388 | {
|
---|
389 | if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
|
---|
390 | FTDI_DEVICE_OUT_REQTYPE,
|
---|
391 | SIO_RESET_REQUEST,
|
---|
392 | SIO_RESET_PURGE_TX,
|
---|
393 | reader->sr_config->index,
|
---|
394 | NULL,
|
---|
395 | 0,
|
---|
396 | reader->sr_config->usb_write_timeout) != 0) {
|
---|
397 | rdr_log(reader, "FTDI purge of TX buffer failed");
|
---|
398 | return (-1);
|
---|
399 | }
|
---|
400 |
|
---|
401 | return 0;
|
---|
402 | }
|
---|
403 |
|
---|
404 | static int32_t smartreader_usb_purge_buffers(struct s_reader *reader)
|
---|
405 | {
|
---|
406 | int32_t result;
|
---|
407 |
|
---|
408 | result = smartreader_usb_purge_rx_buffer(reader);
|
---|
409 | if (result < 0)
|
---|
410 | return -1;
|
---|
411 |
|
---|
412 | result = smartreader_usb_purge_tx_buffer(reader);
|
---|
413 | if (result < 0)
|
---|
414 | return -2;
|
---|
415 |
|
---|
416 | return 0;
|
---|
417 | }
|
---|
418 |
|
---|
419 | static int32_t smartreader_convert_baudrate(int32_t baudrate, struct s_reader *reader, uint16_t *value, uint16_t *idx)
|
---|
420 | {
|
---|
421 | static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
|
---|
422 | static const char am_adjust_dn[8] = {0, 0, 0, 1, 0, 1, 2, 3};
|
---|
423 | static const char frac_code[8] = {0, 3, 2, 4, 1, 5, 6, 7};
|
---|
424 | int32_t divisor, best_divisor, best_baud, best_baud_diff;
|
---|
425 | uint32_t encoded_divisor;
|
---|
426 | int32_t i;
|
---|
427 |
|
---|
428 | if (baudrate <= 0)
|
---|
429 | {
|
---|
430 | // Return error
|
---|
431 | return -1;
|
---|
432 | }
|
---|
433 |
|
---|
434 | divisor = 24000000 / baudrate;
|
---|
435 |
|
---|
436 | if (reader->sr_config->type == TYPE_AM)
|
---|
437 | {
|
---|
438 | // Round down to supported fraction (AM only)
|
---|
439 | divisor -= am_adjust_dn[divisor & 7];
|
---|
440 | }
|
---|
441 |
|
---|
442 | // Try this divisor and the one above it (because division rounds down)
|
---|
443 | best_divisor = 0;
|
---|
444 | best_baud = 0;
|
---|
445 | best_baud_diff = 0;
|
---|
446 | for (i = 0; i < 2; i++)
|
---|
447 | {
|
---|
448 | int32_t try_divisor = divisor + i;
|
---|
449 | int32_t baud_estimate;
|
---|
450 | int32_t baud_diff;
|
---|
451 |
|
---|
452 | // Round up to supported divisor value
|
---|
453 | if (try_divisor <= 8)
|
---|
454 | {
|
---|
455 | // Round up to minimum supported divisor
|
---|
456 | try_divisor = 8;
|
---|
457 | }
|
---|
458 | else if (reader->sr_config->type != TYPE_AM && try_divisor < 12)
|
---|
459 | {
|
---|
460 | // BM doesn't support divisors 9 through 11 inclusive
|
---|
461 | try_divisor = 12;
|
---|
462 | }
|
---|
463 | else if (divisor < 16)
|
---|
464 | {
|
---|
465 | // AM doesn't support divisors 9 through 15 inclusive
|
---|
466 | try_divisor = 16;
|
---|
467 | }
|
---|
468 | else
|
---|
469 | {
|
---|
470 | if (reader->sr_config->type == TYPE_AM)
|
---|
471 | {
|
---|
472 | // Round up to supported fraction (AM only)
|
---|
473 | try_divisor += am_adjust_up[try_divisor & 7];
|
---|
474 | if (try_divisor > 0x1FFF8)
|
---|
475 | {
|
---|
476 | // Round down to maximum supported divisor value (for AM)
|
---|
477 | try_divisor = 0x1FFF8;
|
---|
478 | }
|
---|
479 | }
|
---|
480 | else
|
---|
481 | {
|
---|
482 | if (try_divisor > 0x1FFFF)
|
---|
483 | {
|
---|
484 | // Round down to maximum supported divisor value (for BM)
|
---|
485 | try_divisor = 0x1FFFF;
|
---|
486 | }
|
---|
487 | }
|
---|
488 | }
|
---|
489 | // Get estimated baud rate (to nearest integer)
|
---|
490 | baud_estimate = (24000000 + (try_divisor / 2)) / try_divisor;
|
---|
491 | // Get absolute difference from requested baud rate
|
---|
492 | if (baud_estimate < baudrate)
|
---|
493 | {
|
---|
494 | baud_diff = baudrate - baud_estimate;
|
---|
495 | }
|
---|
496 | else
|
---|
497 | {
|
---|
498 | baud_diff = baud_estimate - baudrate;
|
---|
499 | }
|
---|
500 | if (i == 0 || baud_diff < best_baud_diff)
|
---|
501 | {
|
---|
502 | // Closest to requested baud rate so far
|
---|
503 | best_divisor = try_divisor;
|
---|
504 | best_baud = baud_estimate;
|
---|
505 | best_baud_diff = baud_diff;
|
---|
506 | if (baud_diff == 0)
|
---|
507 | {
|
---|
508 | // Spot on! No point trying
|
---|
509 | break;
|
---|
510 | }
|
---|
511 | }
|
---|
512 | }
|
---|
513 | // Encode the best divisor value
|
---|
514 | encoded_divisor = (best_divisor >> 3) | (frac_code[best_divisor & 7] << 14);
|
---|
515 | // Deal with special cases for encoded value
|
---|
516 | if (encoded_divisor == 1)
|
---|
517 | {
|
---|
518 | encoded_divisor = 0; // 3000000 baud
|
---|
519 | }
|
---|
520 | else if (encoded_divisor == 0x4001)
|
---|
521 | {
|
---|
522 | encoded_divisor = 1; // 2000000 baud (BM only)
|
---|
523 | }
|
---|
524 | // Split into "value" and "index" values
|
---|
525 | *value = (uint16_t )(encoded_divisor & 0xFFFF);
|
---|
526 | if (reader->sr_config->type == TYPE_2232C || reader->sr_config->type == TYPE_2232H || reader->sr_config->type == TYPE_4232H)
|
---|
527 | {
|
---|
528 | *idx = (uint16_t )(encoded_divisor >> 8);
|
---|
529 | *idx &= 0xFF00;
|
---|
530 | *idx |= reader->sr_config->index;
|
---|
531 | }
|
---|
532 | else
|
---|
533 | *idx = (uint16_t )(encoded_divisor >> 16);
|
---|
534 |
|
---|
535 | // Return the nearest baud rate
|
---|
536 | return best_baud;
|
---|
537 | }
|
---|
538 |
|
---|
539 | static int32_t smartreader_set_baudrate(struct s_reader *reader, int32_t baudrate)
|
---|
540 | {
|
---|
541 | uint16_t value, idx;
|
---|
542 | int32_t actual_baudrate;
|
---|
543 |
|
---|
544 | if (reader->sr_config->bitbang_enabled)
|
---|
545 | {
|
---|
546 | baudrate = baudrate*4;
|
---|
547 | }
|
---|
548 |
|
---|
549 | actual_baudrate = smartreader_convert_baudrate(baudrate, reader, &value, &idx);
|
---|
550 | if (actual_baudrate <= 0) {
|
---|
551 | rdr_log(reader, "Silly baudrate <= 0.");
|
---|
552 | return (-1);
|
---|
553 | }
|
---|
554 |
|
---|
555 | // Check within tolerance (about 5%)
|
---|
556 | if ((actual_baudrate * 2 < baudrate /* Catch overflows */ )
|
---|
557 | || ((actual_baudrate < baudrate)
|
---|
558 | ? (actual_baudrate * 21 < baudrate * 20)
|
---|
559 | : (baudrate * 21 < actual_baudrate * 20))) {
|
---|
560 | rdr_log(reader, "Unsupported baudrate. Note: bitbang baudrates are automatically multiplied by 4");
|
---|
561 | return (-1);
|
---|
562 | }
|
---|
563 |
|
---|
564 | if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
|
---|
565 | FTDI_DEVICE_OUT_REQTYPE,
|
---|
566 | SIO_SET_BAUDRATE_REQUEST,
|
---|
567 | value,
|
---|
568 | idx,
|
---|
569 | NULL,
|
---|
570 | 0,
|
---|
571 | reader->sr_config->usb_write_timeout) != 0) {
|
---|
572 | rdr_log(reader, "Setting new baudrate failed");
|
---|
573 | return (-2);
|
---|
574 | }
|
---|
575 |
|
---|
576 | reader->sr_config->baudrate = baudrate;
|
---|
577 | return 0;
|
---|
578 | }
|
---|
579 |
|
---|
580 | static int32_t smartreader_setdtr_rts(struct s_reader *reader, int32_t dtr, int32_t rts)
|
---|
581 | {
|
---|
582 | uint16_t usb_val;
|
---|
583 |
|
---|
584 | if (dtr)
|
---|
585 | usb_val = SIO_SET_DTR_HIGH;
|
---|
586 | else
|
---|
587 | usb_val = SIO_SET_DTR_LOW;
|
---|
588 |
|
---|
589 | if (rts)
|
---|
590 | usb_val |= SIO_SET_RTS_HIGH;
|
---|
591 | else
|
---|
592 | usb_val |= SIO_SET_RTS_LOW;
|
---|
593 |
|
---|
594 | if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
|
---|
595 | FTDI_DEVICE_OUT_REQTYPE,
|
---|
596 | SIO_SET_MODEM_CTRL_REQUEST,
|
---|
597 | usb_val,
|
---|
598 | reader->sr_config->index,
|
---|
599 | NULL,
|
---|
600 | 0,
|
---|
601 | reader->sr_config->usb_write_timeout) != 0) {
|
---|
602 | rdr_log(reader, "set of rts/dtr failed");
|
---|
603 | return (-1);
|
---|
604 | }
|
---|
605 |
|
---|
606 | return 0;
|
---|
607 | }
|
---|
608 |
|
---|
609 | static int32_t smartreader_setflowctrl(struct s_reader *reader, int32_t flowctrl)
|
---|
610 | {
|
---|
611 | if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
|
---|
612 | FTDI_DEVICE_OUT_REQTYPE,
|
---|
613 | SIO_SET_FLOW_CTRL_REQUEST,
|
---|
614 | 0,
|
---|
615 | (flowctrl | reader->sr_config->index),
|
---|
616 | NULL,
|
---|
617 | 0,
|
---|
618 | reader->sr_config->usb_write_timeout) != 0) {
|
---|
619 | rdr_log(reader, "set flow control failed");
|
---|
620 | return (-1);
|
---|
621 | }
|
---|
622 |
|
---|
623 | return 0;
|
---|
624 | }
|
---|
625 |
|
---|
626 | static int32_t smartreader_set_line_property2(struct s_reader *reader, enum smartreader_bits_type bits,
|
---|
627 | enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity,
|
---|
628 | enum smartreader_break_type break_type)
|
---|
629 | {
|
---|
630 | uint16_t value = bits;
|
---|
631 |
|
---|
632 | switch (parity)
|
---|
633 | {
|
---|
634 | case NONE:
|
---|
635 | value |= (0x00 << 8);
|
---|
636 | break;
|
---|
637 | case ODD:
|
---|
638 | value |= (0x01 << 8);
|
---|
639 | break;
|
---|
640 | case EVEN:
|
---|
641 | value |= (0x02 << 8);
|
---|
642 | break;
|
---|
643 | case MARK:
|
---|
644 | value |= (0x03 << 8);
|
---|
645 | break;
|
---|
646 | case SPACE:
|
---|
647 | value |= (0x04 << 8);
|
---|
648 | break;
|
---|
649 | }
|
---|
650 |
|
---|
651 | switch (sbit)
|
---|
652 | {
|
---|
653 | case STOP_BIT_1:
|
---|
654 | value |= (0x00 << 11);
|
---|
655 | break;
|
---|
656 | case STOP_BIT_15:
|
---|
657 | value |= (0x01 << 11);
|
---|
658 | break;
|
---|
659 | case STOP_BIT_2:
|
---|
660 | value |= (0x02 << 11);
|
---|
661 | break;
|
---|
662 | }
|
---|
663 |
|
---|
664 | switch (break_type)
|
---|
665 | {
|
---|
666 | case BREAK_OFF:
|
---|
667 | value |= (0x00 << 14);
|
---|
668 | break;
|
---|
669 | case BREAK_ON:
|
---|
670 | value |= (0x01 << 14);
|
---|
671 | break;
|
---|
672 | }
|
---|
673 |
|
---|
674 | if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
|
---|
675 | FTDI_DEVICE_OUT_REQTYPE,
|
---|
676 | SIO_SET_DATA_REQUEST,
|
---|
677 | value,
|
---|
678 | reader->sr_config->index,
|
---|
679 | NULL,
|
---|
680 | 0,
|
---|
681 | reader->sr_config->usb_write_timeout) != 0) {
|
---|
682 | rdr_log(reader, "Setting new line property failed");
|
---|
683 | return (-1);
|
---|
684 | }
|
---|
685 |
|
---|
686 | return 0;
|
---|
687 | }
|
---|
688 |
|
---|
689 |
|
---|
690 | static int32_t smartreader_set_line_property(struct s_reader *reader, enum smartreader_bits_type bits,
|
---|
691 | enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity)
|
---|
692 | {
|
---|
693 | return smartreader_set_line_property2(reader, bits, sbit, parity, BREAK_OFF);
|
---|
694 | }
|
---|
695 |
|
---|
696 |
|
---|
697 |
|
---|
698 | static void smart_flush(struct s_reader *reader)
|
---|
699 | {
|
---|
700 | smartreader_usb_purge_buffers(reader);
|
---|
701 |
|
---|
702 | pthread_mutex_lock(&reader->sr_config->g_read_mutex);
|
---|
703 | reader->sr_config->g_read_buffer_size = 0;
|
---|
704 | pthread_mutex_unlock(&reader->sr_config->g_read_mutex);
|
---|
705 | }
|
---|
706 |
|
---|
707 | static int32_t smartreader_set_latency_timer(struct s_reader *reader, uint16_t latency)
|
---|
708 | {
|
---|
709 | uint16_t usb_val;
|
---|
710 |
|
---|
711 | if (latency < 1) {
|
---|
712 | rdr_log(reader, "latency out of range. Only valid for 1-255");
|
---|
713 | return (-1);
|
---|
714 | }
|
---|
715 |
|
---|
716 | usb_val = latency;
|
---|
717 | if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
|
---|
718 | FTDI_DEVICE_OUT_REQTYPE,
|
---|
719 | SIO_SET_LATENCY_TIMER_REQUEST,
|
---|
720 | usb_val,
|
---|
721 | reader->sr_config->index,
|
---|
722 | NULL,
|
---|
723 | 0,
|
---|
724 | reader->sr_config->usb_write_timeout) != 0) {
|
---|
725 | rdr_log(reader, "unable to set latency timer");
|
---|
726 | return (-2);
|
---|
727 | }
|
---|
728 |
|
---|
729 | return 0;
|
---|
730 | }
|
---|
731 |
|
---|
732 | #if defined(__CYGWIN__)
|
---|
733 | static WINAPI read_callback(struct libusb_transfer *transfer){
|
---|
734 | #else
|
---|
735 | static void read_callback(struct libusb_transfer *transfer){
|
---|
736 | #endif
|
---|
737 | struct s_reader *reader = (struct s_reader*)transfer->user_data;
|
---|
738 | int32_t copy_size;
|
---|
739 | int32_t ret;
|
---|
740 |
|
---|
741 | if(transfer->status == LIBUSB_TRANSFER_COMPLETED) {
|
---|
742 | if(transfer->actual_length > 2) { //FTDI always sends modem status bytes as first 2 chars with the 232BM
|
---|
743 | pthread_mutex_lock(&reader->sr_config->g_read_mutex);
|
---|
744 |
|
---|
745 | if(reader->sr_config->g_read_buffer_size == sizeof(reader->sr_config->g_read_buffer)) {
|
---|
746 | rdr_log(reader, "SR: buffer full");
|
---|
747 | //if out read buffer is full then delay
|
---|
748 | //slightly and go around again
|
---|
749 | ret = libusb_submit_transfer(transfer);
|
---|
750 | if(ret!=0)
|
---|
751 | rdr_log(reader, "SR: submit async transfer failed with error %d",ret);
|
---|
752 | pthread_cond_signal(&reader->sr_config->g_read_cond);
|
---|
753 | pthread_mutex_unlock(&reader->sr_config->g_read_mutex);
|
---|
754 | return;
|
---|
755 | }
|
---|
756 | reader->sr_config->modem_status = transfer->buffer[0];
|
---|
757 |
|
---|
758 | copy_size = sizeof(reader->sr_config->g_read_buffer) - reader->sr_config->g_read_buffer_size > (uint32_t )transfer->actual_length-2 ? (uint32_t )transfer->actual_length-2: sizeof(reader->sr_config->g_read_buffer) - reader->sr_config->g_read_buffer_size;
|
---|
759 | memcpy(reader->sr_config->g_read_buffer+reader->sr_config->g_read_buffer_size,transfer->buffer+2,copy_size);
|
---|
760 | reader->sr_config->g_read_buffer_size += copy_size;
|
---|
761 |
|
---|
762 | pthread_cond_signal(&reader->sr_config->g_read_cond);
|
---|
763 | pthread_mutex_unlock(&reader->sr_config->g_read_mutex);
|
---|
764 | }
|
---|
765 | else {
|
---|
766 | if(transfer->actual_length==2) {
|
---|
767 | pthread_mutex_lock(&reader->sr_config->g_read_mutex);
|
---|
768 | reader->sr_config->modem_status=transfer->buffer[0];
|
---|
769 | pthread_mutex_unlock(&reader->sr_config->g_read_mutex);
|
---|
770 | }
|
---|
771 | }
|
---|
772 |
|
---|
773 | ret = libusb_submit_transfer(transfer);
|
---|
774 |
|
---|
775 | if(ret!=0)
|
---|
776 | rdr_log(reader, "SR: submit async transfer failed with error %d",ret);
|
---|
777 |
|
---|
778 | }
|
---|
779 | else
|
---|
780 | rdr_log(reader, "SR: USB bulk read failed with error %d",transfer->status);
|
---|
781 | }
|
---|
782 |
|
---|
783 | static int32_t smartreader_usb_open_dev(struct s_reader *reader)
|
---|
784 | {
|
---|
785 | int32_t detach_errno = 0;
|
---|
786 | struct libusb_device_descriptor usbdesc;
|
---|
787 | int32_t ret;
|
---|
788 |
|
---|
789 | #ifdef __WIN32__
|
---|
790 | int32_t config;
|
---|
791 | int32_t config_val = 1;
|
---|
792 | #endif
|
---|
793 |
|
---|
794 | ret=libusb_open(reader->sr_config->usb_dev,&reader->sr_config->usb_dev_handle);
|
---|
795 | if (ret) {
|
---|
796 | rdr_log(reader, "coulnd't open SmartReader device %03d:%03d", libusb_get_bus_number(reader->sr_config->usb_dev), libusb_get_device_address(reader->sr_config->usb_dev));
|
---|
797 | switch(ret) {
|
---|
798 | case LIBUSB_ERROR_NO_MEM:
|
---|
799 | rdr_log(reader, "libusb_open error LIBUSB_ERROR_NO_MEM : memory allocation failure");
|
---|
800 | break;
|
---|
801 | case LIBUSB_ERROR_ACCESS:
|
---|
802 | rdr_log(reader, "libusb_open error LIBUSB_ERROR_ACCESS : the user has insufficient permissions");
|
---|
803 | break;
|
---|
804 | case LIBUSB_ERROR_NO_DEVICE:
|
---|
805 | rdr_log(reader, "libusb_open error LIBUSB_ERROR_NO_DEVICE : the device has been disconnected");
|
---|
806 | break;
|
---|
807 | default:
|
---|
808 | rdr_log(reader, "libusb_open unknown error : %d", ret);
|
---|
809 | break;
|
---|
810 | }
|
---|
811 | return (-4);
|
---|
812 | }
|
---|
813 |
|
---|
814 | #if defined(__linux__)
|
---|
815 | // Try to detach ftdi_sio kernel module.
|
---|
816 | // Returns ENODATA if driver is not loaded.
|
---|
817 | //
|
---|
818 | // The return code is kept in a separate variable and only parsed
|
---|
819 | // if usb_set_configuration() or usb_claim_interface() fails as the
|
---|
820 | // detach operation might be denied and everything still works fine.
|
---|
821 | // Likely scenario is a static smartreader_sio kernel module.
|
---|
822 | if (libusb_detach_kernel_driver(reader->sr_config->usb_dev_handle, reader->sr_config->interface) != 0 && errno != ENODATA) {
|
---|
823 | detach_errno = errno;
|
---|
824 | rdr_log(reader, "Couldn't detach interface from kernel. Please unload the FTDI drivers");
|
---|
825 | return(LIBUSB_ERROR_NOT_SUPPORTED);
|
---|
826 | }
|
---|
827 | #endif
|
---|
828 | ret = libusb_get_device_descriptor(reader->sr_config->usb_dev, &usbdesc);
|
---|
829 |
|
---|
830 | #ifdef __WIN32__
|
---|
831 | // set configuration (needed especially for windows)
|
---|
832 | // tolerate EBUSY: one device with one configuration, but two interfaces
|
---|
833 | // and libftdi sessions to both interfaces (e.g. FT2232)
|
---|
834 |
|
---|
835 | if (usbdesc.bNumConfigurations > 0)
|
---|
836 | {
|
---|
837 | ret=libusb_get_configuration(reader->sr_config->usb_dev_handle,&config);
|
---|
838 |
|
---|
839 | // libusb-win32 on Windows 64 can return a null pointer for a valid device
|
---|
840 | if (libusb_set_configuration(reader->sr_config->usb_dev_handle, config) &&
|
---|
841 | errno != EBUSY)
|
---|
842 | {
|
---|
843 | #if defined(__linux__)
|
---|
844 | if(detach_errno == 0) libusb_attach_kernel_driver(reader->sr_config->usb_dev_handle, reader->sr_config->interface);
|
---|
845 | #endif
|
---|
846 | smartreader_usb_close_internal (reader);
|
---|
847 | if (detach_errno == EPERM) {
|
---|
848 | rdr_log(reader, "inappropriate permissions on device!");
|
---|
849 | return(-8);
|
---|
850 | }
|
---|
851 | else {
|
---|
852 | rdr_log(reader, "unable to set usb configuration. Make sure smartreader_sio is unloaded!");
|
---|
853 | return (-3);
|
---|
854 | }
|
---|
855 | }
|
---|
856 | }
|
---|
857 | #endif
|
---|
858 |
|
---|
859 | ret=libusb_claim_interface(reader->sr_config->usb_dev_handle, reader->sr_config->interface) ;
|
---|
860 | if (ret!= 0)
|
---|
861 | {
|
---|
862 | #if defined(__linux__)
|
---|
863 | if(detach_errno == 0) libusb_attach_kernel_driver(reader->sr_config->usb_dev_handle, reader->sr_config->interface);
|
---|
864 | #endif
|
---|
865 | smartreader_usb_close_internal (reader);
|
---|
866 | if (detach_errno == EPERM) {
|
---|
867 | rdr_log(reader, "inappropriate permissions on device!");
|
---|
868 | return (-8);
|
---|
869 | }
|
---|
870 | else {
|
---|
871 | rdr_log(reader, "unable to claim usb device. Make sure smartreader_sio is unloaded!");
|
---|
872 | return (-5);
|
---|
873 | }
|
---|
874 | }
|
---|
875 |
|
---|
876 | if (smartreader_usb_reset (reader) != 0) {
|
---|
877 | libusb_release_interface(reader->sr_config->usb_dev_handle, reader->sr_config->interface);
|
---|
878 | #if defined(__linux__)
|
---|
879 | if(detach_errno == 0) libusb_attach_kernel_driver(reader->sr_config->usb_dev_handle, reader->sr_config->interface);
|
---|
880 | #endif
|
---|
881 | smartreader_usb_close_internal (reader);
|
---|
882 | rdr_log(reader, "smartreader_usb_reset failed");
|
---|
883 | return (-6);
|
---|
884 | }
|
---|
885 |
|
---|
886 | // Try to guess chip type
|
---|
887 | // Bug in the BM type chips: bcdDevice is 0x200 for serial == 0
|
---|
888 | if (usbdesc.bcdDevice == 0x400 || (usbdesc.bcdDevice == 0x200
|
---|
889 | && usbdesc.iSerialNumber == 0))
|
---|
890 | reader->sr_config->type = TYPE_BM;
|
---|
891 | else if (usbdesc.bcdDevice == 0x200)
|
---|
892 | reader->sr_config->type = TYPE_AM;
|
---|
893 | else if (usbdesc.bcdDevice == 0x500)
|
---|
894 | reader->sr_config->type = TYPE_2232C;
|
---|
895 | else if (usbdesc.bcdDevice == 0x600)
|
---|
896 | reader->sr_config->type = TYPE_R;
|
---|
897 | else if (usbdesc.bcdDevice == 0x700)
|
---|
898 | reader->sr_config->type = TYPE_2232H;
|
---|
899 | else if (usbdesc.bcdDevice == 0x800)
|
---|
900 | reader->sr_config->type = TYPE_4232H;
|
---|
901 |
|
---|
902 | // Set default interface on dual/quad type chips
|
---|
903 | switch(reader->sr_config->type) {
|
---|
904 | case TYPE_2232C:
|
---|
905 | case TYPE_2232H:
|
---|
906 | case TYPE_4232H:
|
---|
907 | if (!reader->sr_config->index)
|
---|
908 | reader->sr_config->index = INTERFACE_A;
|
---|
909 | break;
|
---|
910 | default:
|
---|
911 | break;
|
---|
912 | }
|
---|
913 |
|
---|
914 | // Determine maximum packet size
|
---|
915 | reader->sr_config->max_packet_size = smartreader_determine_max_packet_size(reader);
|
---|
916 |
|
---|
917 | if (smartreader_set_baudrate (reader, 9600) != 0) {
|
---|
918 | libusb_release_interface(reader->sr_config->usb_dev_handle, reader->sr_config->interface);
|
---|
919 | #if defined(__linux__)
|
---|
920 | if(detach_errno == 0) libusb_attach_kernel_driver(reader->sr_config->usb_dev_handle, reader->sr_config->interface);
|
---|
921 | #endif
|
---|
922 | smartreader_usb_close_internal (reader);
|
---|
923 | rdr_log(reader, "set baudrate failed");
|
---|
924 | return (-7);
|
---|
925 | }
|
---|
926 |
|
---|
927 | return (0);
|
---|
928 | }
|
---|
929 |
|
---|
930 | static void EnableSmartReader(struct s_reader *reader, int32_t clock_val, uint16_t Fi, unsigned char Di, unsigned char Ni, unsigned char T, unsigned char inv,int32_t parity) {
|
---|
931 |
|
---|
932 | unsigned char FiDi[4];
|
---|
933 | uint16_t freqk;
|
---|
934 | unsigned char Freq[3];
|
---|
935 | unsigned char N[2];
|
---|
936 | unsigned char Prot[2];
|
---|
937 | unsigned char Invert[2];
|
---|
938 | unsigned char temp_T;
|
---|
939 |
|
---|
940 | smartreader_set_baudrate(reader, 9600);
|
---|
941 | smartreader_setflowctrl(reader, 0);
|
---|
942 | smartreader_set_line_property(reader, (enum smartreader_bits_type) 5, STOP_BIT_2, NONE);
|
---|
943 |
|
---|
944 | // command 1, set F and D parameter
|
---|
945 | if(!reader->sr_config->irdeto) {
|
---|
946 | rdr_debug_mask(reader, D_DEVICE, "SR: sending F=%04X (%d) to smartreader",Fi,Fi);
|
---|
947 | rdr_debug_mask(reader, D_DEVICE, "SR: sending D=%02X (%d) to smartreader",Di,Di);
|
---|
948 | FiDi[0]=0x01;
|
---|
949 | FiDi[1]=HIBYTE(Fi);
|
---|
950 | FiDi[2]=LOBYTE(Fi);
|
---|
951 | FiDi[3]=Di;
|
---|
952 | smart_write(reader,FiDi, sizeof (FiDi));
|
---|
953 | }
|
---|
954 | else {
|
---|
955 | rdr_debug_mask(reader, D_ATR, "Not setting F and D as we're in Irdeto mode");
|
---|
956 | }
|
---|
957 |
|
---|
958 | // command 2, set the frequency in KHz
|
---|
959 | // direct from the source .. 4MHz is the best init frequency for T=0 card, but looks like it's causing issue with some nagra card, reveting to 3.69MHz
|
---|
960 | freqk = clock_val * 10; //clock with type int32_t couldnt hold freq in Hz on all platforms, so I reverted to 10khz units (like mhz) - dingo
|
---|
961 | rdr_debug_mask(reader, D_DEVICE, "SR: sending Freq=%04X (%d) to smartreader",freqk,freqk);
|
---|
962 | Freq[0]=0x02;
|
---|
963 | Freq[1]=HIBYTE(freqk);
|
---|
964 | Freq[2]=LOBYTE(freqk);
|
---|
965 | smart_write(reader, Freq, sizeof (Freq));
|
---|
966 |
|
---|
967 | // command 3, set paramter N
|
---|
968 | rdr_debug_mask(reader, D_DEVICE, "SR: sending N=%02X (%d) to smartreader",Ni,Ni);
|
---|
969 | N[0]=0x03;
|
---|
970 | N[1]=Ni;
|
---|
971 | smart_write(reader, N, sizeof (N));
|
---|
972 |
|
---|
973 | // command 4 , set parameter T
|
---|
974 | temp_T=T;
|
---|
975 | if(T==2) // special trick to get ATR for Irdeto card, we need T=1 at reset, after that oscam takes care of T1 protocol, so we need T=0
|
---|
976 | //if(reader->sr_config->irdeto) // special trick to get ATR for Irdeto card, we need T=1 at reset, after that oscam takes care of T1 protocol, so we need T=0
|
---|
977 | {
|
---|
978 | T=1;
|
---|
979 | reader->sr_config->T=1;
|
---|
980 | temp_T=1;
|
---|
981 | }
|
---|
982 | else if (T==1)
|
---|
983 | T=0; // T=1 protocol is handled by oscam
|
---|
984 |
|
---|
985 | rdr_debug_mask(reader, D_DEVICE, "SR: sending T=%02X (%d) to smartreader",T,T);
|
---|
986 | Prot[0]=0x04;
|
---|
987 | Prot[1]=T;
|
---|
988 | smart_write(reader, Prot, sizeof (Prot));
|
---|
989 |
|
---|
990 | // command 5, set invert y/n
|
---|
991 | rdr_debug_mask(reader, D_DEVICE, "SR: sending inv=%02X to smartreader",inv);
|
---|
992 | Invert[0]=0x05;
|
---|
993 | Invert[1]=inv;
|
---|
994 | smart_write(reader, Invert, sizeof (Invert));
|
---|
995 |
|
---|
996 | smartreader_set_line_property2(reader, BITS_8, STOP_BIT_2, parity, BREAK_ON);
|
---|
997 | // send break for 350ms, also comes from JoePub debugging.
|
---|
998 | cs_sleepms(350);
|
---|
999 | if(temp_T==1)
|
---|
1000 | smartreader_set_line_property2(reader, BITS_8, STOP_BIT_1, parity, BREAK_OFF);
|
---|
1001 | else
|
---|
1002 | smartreader_set_line_property2(reader, BITS_8, STOP_BIT_2, parity, BREAK_OFF);
|
---|
1003 |
|
---|
1004 | smart_flush(reader);
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 |
|
---|
1008 | static void* ReaderThread(void *p)
|
---|
1009 | {
|
---|
1010 | struct libusb_transfer* usbt[NUM_TXFERS];
|
---|
1011 | unsigned char usb_buffers[NUM_TXFERS][64];
|
---|
1012 | struct s_reader *reader;
|
---|
1013 | int32_t ret,idx;
|
---|
1014 |
|
---|
1015 | reader = (struct s_reader *)p;
|
---|
1016 | reader->sr_config->running=1;
|
---|
1017 |
|
---|
1018 | for(idx=0; idx<NUM_TXFERS; idx++) {
|
---|
1019 | usbt[idx] = libusb_alloc_transfer(0);
|
---|
1020 | libusb_fill_bulk_transfer( usbt[idx],
|
---|
1021 | reader->sr_config->usb_dev_handle,
|
---|
1022 | reader->sr_config->out_ep,
|
---|
1023 | usb_buffers[idx],
|
---|
1024 | 64,
|
---|
1025 | (void *)(&read_callback),
|
---|
1026 | p,
|
---|
1027 | 0 );
|
---|
1028 |
|
---|
1029 | ret = libusb_submit_transfer(usbt[idx]);
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | while(reader->sr_config->running) {
|
---|
1033 | ret = libusb_handle_events(NULL);
|
---|
1034 | if(ret!=0)
|
---|
1035 | rdr_log(reader, "libusb_handle_events returned with %d",ret);
|
---|
1036 |
|
---|
1037 | pthread_mutex_lock(&reader->sr_config->g_usb_mutex);
|
---|
1038 |
|
---|
1039 | if(!reader->sr_config->poll) {
|
---|
1040 | struct timeval start;
|
---|
1041 | struct timespec timeout;
|
---|
1042 |
|
---|
1043 | gettimeofday(&start, NULL);
|
---|
1044 | timeout.tv_sec = start.tv_sec + 1;
|
---|
1045 | timeout.tv_nsec = start.tv_usec * 1000;
|
---|
1046 |
|
---|
1047 | pthread_cond_timedwait(&reader->sr_config->g_usb_cond, &reader->sr_config->g_usb_mutex, &timeout);
|
---|
1048 | }
|
---|
1049 | pthread_mutex_unlock(&reader->sr_config->g_usb_mutex);
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | pthread_exit(NULL);
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | static void smart_fastpoll(struct s_reader *reader, int32_t on)
|
---|
1056 | {
|
---|
1057 | pthread_mutex_lock(&reader->sr_config->g_usb_mutex);
|
---|
1058 | //printf("poll stat: %d\n", on);
|
---|
1059 | reader->sr_config->poll = on;
|
---|
1060 | pthread_cond_signal(&reader->sr_config->g_usb_cond);
|
---|
1061 | pthread_mutex_unlock(&reader->sr_config->g_usb_mutex);
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | static int32_t SR_Init (struct s_reader *reader)
|
---|
1065 | {
|
---|
1066 | uint8_t out_endpoint;
|
---|
1067 | uint8_t in_endpoint;
|
---|
1068 | int32_t ret;
|
---|
1069 | char device[128];
|
---|
1070 | char *busname, *dev, *search = ":", *saveptr1 = NULL;
|
---|
1071 | // split the device name from the reader conf into devname and busname
|
---|
1072 | memcpy(device,reader->device,128);
|
---|
1073 | busname=strtok_r(device,search, &saveptr1);
|
---|
1074 | dev=strtok_r(NULL,search, &saveptr1);
|
---|
1075 | if(!busname || !dev) {
|
---|
1076 | rdr_log(reader, "Wrong device format (%s), it should be Device=bus:dev",reader->device);
|
---|
1077 | return ERROR;
|
---|
1078 | }
|
---|
1079 | if (!reader->sr_config && !cs_malloc(&reader->sr_config, sizeof(struct s_sr_config)))
|
---|
1080 | return ERROR;
|
---|
1081 | cs_writelock(&sr_lock);
|
---|
1082 | rdr_debug_mask(reader, D_DEVICE, "SR: Looking for device %s on bus %s",dev,busname);
|
---|
1083 |
|
---|
1084 | if(!init_count) {
|
---|
1085 | ret = libusb_init(NULL);
|
---|
1086 | if (ret < 0) {
|
---|
1087 | cs_writeunlock(&sr_lock);
|
---|
1088 | rdr_log(reader, "Libusb init error : %d",ret);
|
---|
1089 | return ret;
|
---|
1090 | }
|
---|
1091 | }
|
---|
1092 | init_count++;
|
---|
1093 |
|
---|
1094 | //Overwrite default endpoint if config has a value for it
|
---|
1095 | if(reader->device_endpoint != 0)
|
---|
1096 | out_endpoint = reader->device_endpoint;
|
---|
1097 | else
|
---|
1098 | out_endpoint = 0x82;
|
---|
1099 |
|
---|
1100 | // ToDo in endpoint hard coded, should be configurable
|
---|
1101 | switch (out_endpoint) {
|
---|
1102 | case 0x82: // Single smartreader
|
---|
1103 | in_endpoint = 0x1;
|
---|
1104 | break;
|
---|
1105 | case 0x81: // Triple reader 1st
|
---|
1106 | in_endpoint = 0x2;
|
---|
1107 | break;
|
---|
1108 | case 0x83: // Triple reader 2nd
|
---|
1109 | in_endpoint = 0x4;
|
---|
1110 | break;
|
---|
1111 | case 0x85: // Triple reader 3rd
|
---|
1112 | in_endpoint = 0x6;
|
---|
1113 | break;
|
---|
1114 | default: // previous default set in smartreader_init()
|
---|
1115 | in_endpoint = 0x2;
|
---|
1116 | break;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 | rdr_log(reader, "Using 0x%02X/0x%02X as endpoint for smartreader hardware detection", out_endpoint, in_endpoint);
|
---|
1120 |
|
---|
1121 | smartreader_init(reader,out_endpoint,in_endpoint);
|
---|
1122 |
|
---|
1123 | reader->sr_config->usb_dev=find_smartreader(busname,dev,out_endpoint,in_endpoint);
|
---|
1124 | if (!reader->sr_config->usb_dev) {
|
---|
1125 | --init_count;
|
---|
1126 | if (!init_count)
|
---|
1127 | libusb_exit(NULL);
|
---|
1128 | cs_writeunlock(&sr_lock);
|
---|
1129 | return ERROR;
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | //The smartreader has different endpoint addresses
|
---|
1133 | //compared to a real FT232 device, so change them here,
|
---|
1134 | //also a good way to compare a real FT232 with a smartreader
|
---|
1135 | //if you enumarate usb devices
|
---|
1136 | reader->sr_config->in_ep = in_endpoint;
|
---|
1137 | reader->sr_config->out_ep = out_endpoint;
|
---|
1138 |
|
---|
1139 | rdr_debug_mask(reader, D_DEVICE, "SR: Opening smartreader device %s on bus %s endpoint in 0x%02X out 0x%02X",dev,busname,in_endpoint,out_endpoint);
|
---|
1140 |
|
---|
1141 | if ((ret=smartreader_usb_open_dev(reader))) {
|
---|
1142 | --init_count;
|
---|
1143 | if (!init_count)
|
---|
1144 | libusb_exit(NULL);
|
---|
1145 | cs_writeunlock(&sr_lock);
|
---|
1146 | rdr_log(reader, "unable to open smartreader device %s in bus %s endpoint in 0x%02X out 0x%02X (ret=%d)\n", dev,busname,in_endpoint,out_endpoint,ret);
|
---|
1147 | return ERROR;
|
---|
1148 | }
|
---|
1149 |
|
---|
1150 | rdr_debug_mask(reader, D_DEVICE, "SR: Setting smartreader latency timer to 1ms");
|
---|
1151 |
|
---|
1152 | //Set the FTDI latency timer to 1ms
|
---|
1153 | ret = smartreader_set_latency_timer(reader, 1);
|
---|
1154 |
|
---|
1155 | //Set databits to 8o2
|
---|
1156 | ret = smartreader_set_line_property(reader, BITS_8, STOP_BIT_2, ODD);
|
---|
1157 |
|
---|
1158 | //Set the DTR HIGH and RTS LOW
|
---|
1159 | ret = smartreader_setdtr_rts(reader, 0, 0);
|
---|
1160 |
|
---|
1161 | //Disable flow control
|
---|
1162 | ret = smartreader_setflowctrl(reader, 0);
|
---|
1163 |
|
---|
1164 | cs_writeunlock(&sr_lock);
|
---|
1165 |
|
---|
1166 | // start the reading thread
|
---|
1167 | reader->sr_config->g_read_buffer_size = 0;
|
---|
1168 | reader->sr_config->modem_status = 0 ;
|
---|
1169 | pthread_mutex_init(&reader->sr_config->g_read_mutex,NULL);
|
---|
1170 | pthread_cond_init(&reader->sr_config->g_read_cond,NULL);
|
---|
1171 | pthread_mutex_init(&reader->sr_config->g_usb_mutex,NULL);
|
---|
1172 | pthread_cond_init(&reader->sr_config->g_usb_cond,NULL);
|
---|
1173 | ret = pthread_create(&reader->sr_config->rt, NULL, ReaderThread, (void *)(reader));
|
---|
1174 | if (ret) {
|
---|
1175 | rdr_log(reader, "ERROR: Can't create smartreader thread (errno=%d %s)", ret, strerror(ret));
|
---|
1176 | return ERROR;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | return OK;
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | static int32_t SR_Reset (struct s_reader *reader, ATR *atr)
|
---|
1183 | {
|
---|
1184 | unsigned char data[ATR_MAX_SIZE];
|
---|
1185 | int32_t ret;
|
---|
1186 | int32_t atr_ok;
|
---|
1187 | uint32_t i;
|
---|
1188 | int32_t parity[4] = {EVEN, ODD, NONE, EVEN}; // the last EVEN is to try with different F, D values for irdeto card.
|
---|
1189 | static const char * const parity_str[5] = {"NONE", "ODD", "EVEN", "MARK", "SPACE"};
|
---|
1190 |
|
---|
1191 | if(reader->mhz == reader->cardmhz && reader->cardmhz * 10000 > 3690000)
|
---|
1192 | reader->sr_config->fs = reader->cardmhz * 10000;
|
---|
1193 | else
|
---|
1194 | reader->sr_config->fs = 3690000;
|
---|
1195 |
|
---|
1196 | smart_fastpoll(reader, 1);
|
---|
1197 | smart_flush(reader);
|
---|
1198 | // set smartreader+ default values
|
---|
1199 | reader->sr_config->F = 372;
|
---|
1200 | reader->sr_config->D = 1.0;
|
---|
1201 | reader->sr_config->N = 0;
|
---|
1202 | reader->sr_config->T = 1;
|
---|
1203 | reader->sr_config->inv = 0;
|
---|
1204 |
|
---|
1205 | for(i=0 ; i < 4 ;i++) {
|
---|
1206 | reader->sr_config->irdeto=0;
|
---|
1207 | atr_ok=ERROR;
|
---|
1208 | memset(data,0,sizeof(data));
|
---|
1209 | rdr_debug_mask(reader, D_DEVICE, "SR: Trying with parity %s",parity_str[parity[i]]);
|
---|
1210 |
|
---|
1211 |
|
---|
1212 | // special irdeto case
|
---|
1213 | if(i==3) {
|
---|
1214 | rdr_debug_mask(reader, D_DEVICE, "SR: Trying irdeto");
|
---|
1215 | reader->sr_config->F=618; /// magic smartreader value
|
---|
1216 | reader->sr_config->D=1;
|
---|
1217 | reader->sr_config->T=2; // will be set to T=1 in EnableSmartReader
|
---|
1218 | reader->sr_config->fs=6000000;
|
---|
1219 | }
|
---|
1220 |
|
---|
1221 | smart_flush(reader);
|
---|
1222 | EnableSmartReader(reader, reader->sr_config->fs/10000, reader->sr_config->F, (unsigned char)reader->sr_config->D, reader->sr_config->N, reader->sr_config->T, reader->sr_config->inv,parity[i]);
|
---|
1223 |
|
---|
1224 | //Reset smartcard
|
---|
1225 |
|
---|
1226 | //Set the DTR HIGH and RTS HIGH
|
---|
1227 | smartreader_setdtr_rts(reader, 1, 1);
|
---|
1228 | // A card with an active low reset is reset by maintaining RST in state L for at least 40 000 clock cycles
|
---|
1229 | // so if we have a base freq of 3.5712MHz : 40000/3690000 = .0112007168458781 seconds, aka 11ms
|
---|
1230 | // so if we have a base freq of 6.00MHz : 40000/6000000 = .0066666666666666 seconds, aka 6ms
|
---|
1231 | cs_sleepms(20);
|
---|
1232 |
|
---|
1233 | //Set the DTR HIGH and RTS LOW
|
---|
1234 | smartreader_setdtr_rts(reader, 1, 0);
|
---|
1235 |
|
---|
1236 | //Read the ATR
|
---|
1237 | ret = smart_read(reader,data,ATR_MAX_SIZE,1);
|
---|
1238 | rdr_debug_mask(reader, D_DEVICE, "SR: get ATR ret = %d" , ret);
|
---|
1239 | if(ret)
|
---|
1240 | rdr_ddump_mask(reader, D_DEVICE, data, ATR_MAX_SIZE * 2, "SR:");
|
---|
1241 |
|
---|
1242 | // this is to make sure we don't think this 03 FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 is a valid ATR.
|
---|
1243 | if((data[0]!=0x3B && data[0]!=0x03 && data[0]!=0x3F) || (data[1]==0xFF && data[2]==0x00)) {
|
---|
1244 | reader->sr_config->irdeto=0;
|
---|
1245 | continue; // this is not a valid ATR.
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | if(data[0]==0x03) {
|
---|
1249 | rdr_debug_mask(reader, D_DEVICE, "SR: Inverse convention detected, setting smartreader inv to 1");
|
---|
1250 |
|
---|
1251 | reader->sr_config->inv=1;
|
---|
1252 | EnableSmartReader(reader, reader->sr_config->fs/10000, reader->sr_config->F, (unsigned char)reader->sr_config->D, reader->sr_config->N, reader->sr_config->T, reader->sr_config->inv,parity[i]);
|
---|
1253 | }
|
---|
1254 | // parse atr
|
---|
1255 | if(ATR_InitFromArray (atr, data, ret) != ERROR) {
|
---|
1256 | rdr_debug_mask(reader, D_DEVICE, "SR: ATR parsing OK");
|
---|
1257 | atr_ok=OK;
|
---|
1258 | if(i==3) {
|
---|
1259 | rdr_debug_mask(reader, D_DEVICE, "SR: Locking F and D for Irdeto mode");
|
---|
1260 | reader->sr_config->irdeto=1;
|
---|
1261 | }
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | if(atr_ok == OK)
|
---|
1265 | break;
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | smart_fastpoll(reader, 0);
|
---|
1269 |
|
---|
1270 | return atr_ok;
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | static int32_t SR_GetStatus (struct s_reader *reader, int32_t * in)
|
---|
1274 | {
|
---|
1275 | int32_t state;
|
---|
1276 |
|
---|
1277 | smart_fastpoll(reader, 1);
|
---|
1278 | pthread_mutex_lock(&reader->sr_config->g_read_mutex);
|
---|
1279 | state =(reader->sr_config->modem_status & 0x80) == 0x80 ? 0 : 2;
|
---|
1280 | pthread_mutex_unlock(&reader->sr_config->g_read_mutex);
|
---|
1281 | smart_fastpoll(reader, 0);
|
---|
1282 |
|
---|
1283 | //state = 0 no card, 1 = not ready, 2 = ready
|
---|
1284 | if (state)
|
---|
1285 | *in = 1; //CARD, even if not ready report card is in, or it will never get activated
|
---|
1286 | else
|
---|
1287 | *in = 0; //NOCARD
|
---|
1288 |
|
---|
1289 | return OK;
|
---|
1290 | }
|
---|
1291 |
|
---|
1292 | static int32_t SR_Transmit (struct s_reader *reader, unsigned char * buffer, uint32_t size, uint32_t delay, uint32_t timeout) // delay and timeout not used (yet)!
|
---|
1293 | {
|
---|
1294 | (void) delay; // delay not used (yet)!
|
---|
1295 | (void) timeout; // timeout not used (yet)!
|
---|
1296 | uint32_t ret;
|
---|
1297 |
|
---|
1298 | smart_fastpoll(reader, 1);
|
---|
1299 | ret = smart_write(reader, buffer, size);
|
---|
1300 | smart_fastpoll(reader, 0);
|
---|
1301 | if (ret!=size)
|
---|
1302 | return ERROR;
|
---|
1303 |
|
---|
1304 | return OK;
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | static int32_t SR_Receive (struct s_reader *reader, unsigned char * buffer, uint32_t size, uint32_t delay, uint32_t timeout) // delay and timeout not used (yet)!
|
---|
1308 | {
|
---|
1309 | (void) delay; // delay not used (yet)!
|
---|
1310 | (void) timeout; // timeout not used (yet)!
|
---|
1311 | uint32_t ret;
|
---|
1312 |
|
---|
1313 | smart_fastpoll(reader, 1);
|
---|
1314 | ret = smart_read(reader, buffer, size, 1);
|
---|
1315 | smart_fastpoll(reader, 0);
|
---|
1316 | if (ret!=size)
|
---|
1317 | return ERROR;
|
---|
1318 |
|
---|
1319 | return OK;
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | int32_t SR_WriteSettings (struct s_reader *reader, uint16_t F, unsigned char D, unsigned char N, unsigned char T, uint16_t convention)
|
---|
1323 | {
|
---|
1324 | // smartreader supports 3.20, 3.43, 3.69, 4.00, 4.36, 4.80, 5.34, 6.00, 6.86, 8.00, 9.61, 12.0, 16.0 Mhz
|
---|
1325 | reader->sr_config->inv = convention;//FIXME this one is set by icc_async and local smartreader reset routine
|
---|
1326 |
|
---|
1327 | if (reader->mhz >=1600) reader->mhz = 1600; else
|
---|
1328 | if (reader->mhz >=1200) reader->mhz = 1200; else
|
---|
1329 | if (reader->mhz >=961) reader->mhz = 961; else
|
---|
1330 | if (reader->mhz >=800) reader->mhz = 800; else
|
---|
1331 | if (reader->mhz >=686) reader->mhz = 686; else
|
---|
1332 | if (reader->mhz >=600) reader->mhz = 600; else
|
---|
1333 | if (reader->mhz >=534) reader->mhz = 534; else
|
---|
1334 | if (reader->mhz >=480) reader->mhz = 480; else
|
---|
1335 | if (reader->mhz >=436) reader->mhz = 436; else
|
---|
1336 | if (reader->mhz >=400) reader->mhz = 400; else
|
---|
1337 | if (reader->mhz >=369) reader->mhz = 369; else
|
---|
1338 | if (reader->mhz ==368) reader->mhz = 369; else
|
---|
1339 | if (reader->mhz >=343) reader->mhz = 343; else
|
---|
1340 | reader->mhz = 320;
|
---|
1341 |
|
---|
1342 | smart_fastpoll(reader, 1);
|
---|
1343 | EnableSmartReader(reader, reader->mhz, F, D, N, T, reader->sr_config->inv,reader->sr_config->parity);
|
---|
1344 |
|
---|
1345 | //baud rate not really used in native mode since
|
---|
1346 | //it's handled by the card, so just set to maximum 3Mb/s
|
---|
1347 | smartreader_set_baudrate(reader, 3000000);
|
---|
1348 | smart_fastpoll(reader, 0);
|
---|
1349 |
|
---|
1350 | return OK;
|
---|
1351 | }
|
---|
1352 |
|
---|
1353 | static int32_t SR_SetParity (struct s_reader *reader, uchar parity)
|
---|
1354 | {
|
---|
1355 | int32_t ret;
|
---|
1356 |
|
---|
1357 | static const char * const parity_str[5]={"NONE", "ODD", "EVEN", "MARK", "SPACE"};
|
---|
1358 | rdr_debug_mask(reader, D_DEVICE, "SR: Setting parity to %s",parity_str[parity]);
|
---|
1359 |
|
---|
1360 | reader->sr_config->parity = parity;
|
---|
1361 | smart_fastpoll(reader, 1);
|
---|
1362 | ret = smartreader_set_line_property(reader, (enum smartreader_bits_type) 8, STOP_BIT_2, parity);
|
---|
1363 | smart_fastpoll(reader, 0);
|
---|
1364 | if(ret)
|
---|
1365 | return ERROR;
|
---|
1366 |
|
---|
1367 | return OK;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | static int32_t SR_Close (struct s_reader *reader)
|
---|
1371 | {
|
---|
1372 | if (!reader->sr_config) return OK;
|
---|
1373 | rdr_debug_mask(reader, D_DEVICE, "SR: Closing smartreader");
|
---|
1374 |
|
---|
1375 | reader->sr_config->running=0;
|
---|
1376 | if (reader->sr_config->usb_dev_handle) {
|
---|
1377 | cs_writelock(&sr_lock);
|
---|
1378 | smart_fastpoll(reader, 1);
|
---|
1379 | pthread_join(reader->sr_config->rt,NULL);
|
---|
1380 | smart_fastpoll(reader, 0);
|
---|
1381 | libusb_release_interface(reader->sr_config->usb_dev_handle, reader->sr_config->interface);
|
---|
1382 | #if defined(__linux__)
|
---|
1383 | libusb_attach_kernel_driver(reader->sr_config->usb_dev_handle, reader->sr_config->interface);
|
---|
1384 | #endif
|
---|
1385 | libusb_close(reader->sr_config->usb_dev_handle);
|
---|
1386 | init_count--;
|
---|
1387 | if (!init_count)
|
---|
1388 | libusb_exit(NULL);
|
---|
1389 | cs_writeunlock(&sr_lock);
|
---|
1390 | }
|
---|
1391 | free(reader->sr_config);
|
---|
1392 | reader->sr_config = NULL;
|
---|
1393 | return OK;
|
---|
1394 | }
|
---|
1395 |
|
---|
1396 | /*static int32_t SR_FastReset(struct s_reader *reader, int32_t delay)
|
---|
1397 | {
|
---|
1398 | unsigned char data[ATR_MAX_SIZE];
|
---|
1399 |
|
---|
1400 | smart_fastpoll(reader, 1);
|
---|
1401 | //Set the DTR HIGH and RTS HIGH
|
---|
1402 | smartreader_setdtr_rts(reader, 1, 1);
|
---|
1403 | // A card with an active low reset is reset by maintaining RST in state L for at least 40 000 clock cycles
|
---|
1404 | // so if we have a base freq of 3.5712MHz : 40000/3690000 = .0112007168458781 seconds, aka 11ms
|
---|
1405 | // so if we have a base freq of 6.00MHz : 40000/6000000 = .0066666666666666 seconds, aka 6ms
|
---|
1406 | cs_sleepms(delay);
|
---|
1407 |
|
---|
1408 | //Set the DTR HIGH and RTS LOW
|
---|
1409 | smartreader_setdtr_rts(reader, 1, 0);
|
---|
1410 |
|
---|
1411 | //Read the ATR
|
---|
1412 | smart_read(reader,data, ATR_MAX_SIZE,1);
|
---|
1413 | smart_fastpoll(reader, 0);
|
---|
1414 | return 0;
|
---|
1415 | } */
|
---|
1416 |
|
---|
1417 | static int32_t SR_FastReset_With_ATR(struct s_reader *reader, ATR *atr)
|
---|
1418 | {
|
---|
1419 | unsigned char data[ATR_MAX_SIZE];
|
---|
1420 | int32_t ret;
|
---|
1421 | int32_t atr_ok=ERROR;
|
---|
1422 |
|
---|
1423 | smart_fastpoll(reader, 1);
|
---|
1424 | //Set the DTR HIGH and RTS HIGH
|
---|
1425 | smartreader_setdtr_rts(reader, 1, 1);
|
---|
1426 | // A card with an active low reset is reset by maintaining RST in state L for at least 40 000 clock cycles
|
---|
1427 | // so if we have a base freq of 3.5712MHz : 40000/3690000 = .0112007168458781 seconds, aka 11ms
|
---|
1428 | // so if we have a base freq of 6.00MHz : 40000/6000000 = .0066666666666666 seconds, aka 6ms
|
---|
1429 | cs_sleepms(20);
|
---|
1430 |
|
---|
1431 | //Set the DTR HIGH and RTS LOW
|
---|
1432 | smartreader_setdtr_rts(reader, 1, 0);
|
---|
1433 |
|
---|
1434 | //Read the ATR
|
---|
1435 | ret = smart_read(reader,data, ATR_MAX_SIZE,1);
|
---|
1436 |
|
---|
1437 | // parse atr
|
---|
1438 | if (ATR_InitFromArray (atr, data, ret) != ERROR) {
|
---|
1439 | rdr_debug_mask(reader, D_DEVICE, "SR: ATR parsing OK");
|
---|
1440 | atr_ok = OK;
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | smart_fastpoll(reader, 0);
|
---|
1444 | return atr_ok;
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | int32_t SR_Activate(struct s_reader *reader, struct s_ATR *atr)
|
---|
1448 | {
|
---|
1449 | if (!reader->ins7e11_fast_reset) {
|
---|
1450 | call(SR_Reset(reader, atr));
|
---|
1451 | } else {
|
---|
1452 | rdr_log(reader, "Doing fast reset");
|
---|
1453 | call(SR_FastReset_With_ATR(reader, atr));
|
---|
1454 | }
|
---|
1455 | return OK;
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | int32_t sr_write_settings(struct s_reader *reader,
|
---|
1459 | uint32_t UNUSED(ETU),
|
---|
1460 | uint32_t EGT,
|
---|
1461 | unsigned char UNUSED(P),
|
---|
1462 | unsigned char UNUSED(I),
|
---|
1463 | uint16_t Fi,
|
---|
1464 | unsigned char Di,
|
---|
1465 | unsigned char UNUSED(Ni))
|
---|
1466 | {
|
---|
1467 | SR_WriteSettings(reader, Fi, Di, (unsigned char)EGT, (unsigned char)reader->protocol_type, reader->convention);
|
---|
1468 | return OK;
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | static pthread_mutex_t init_lock_mutex = PTHREAD_MUTEX_INITIALIZER;
|
---|
1472 |
|
---|
1473 | static int32_t sr_init_locks(struct s_reader *UNUSED(reader))
|
---|
1474 | {
|
---|
1475 | // Prevent double initalization of sr_lock
|
---|
1476 | if (pthread_mutex_trylock(&init_lock_mutex)) {
|
---|
1477 | cs_lock_create(&sr_lock, 10, "sr_lock");
|
---|
1478 | }
|
---|
1479 | return 0;
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 | void cardreader_smartreader(struct s_cardreader *crdr)
|
---|
1483 | {
|
---|
1484 | crdr->desc = "smartreader";
|
---|
1485 | crdr->typ = R_SMART;
|
---|
1486 | crdr->reader_init = SR_Init;
|
---|
1487 | crdr->get_status = SR_GetStatus;
|
---|
1488 | crdr->set_parity = SR_SetParity;
|
---|
1489 | crdr->activate = SR_Activate;
|
---|
1490 | crdr->transmit = SR_Transmit;
|
---|
1491 | crdr->receive = SR_Receive;
|
---|
1492 | crdr->close = SR_Close;
|
---|
1493 | crdr->write_settings = sr_write_settings;
|
---|
1494 | crdr->lock_init = sr_init_locks;
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | #endif
|
---|