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