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