source: trunk/csctapi/ifd_phoenix.c@ 8331

Last change on this file since 8331 was 8331, checked in by theparasol, 9 years ago
  • Revert commit 7892 it breaks infinity usb readers

Tnx to ryszardzonk for research and testing

  • Property svn:eol-style set to LF
File size: 5.9 KB
Line 
1/*
2 ifd_phoenix.c
3 This module provides IFD handling functions for Smartmouse/Phoenix reader.
4*/
5
6#include "../globals.h"
7
8#ifdef CARDREADER_PHOENIX
9#include "../oscam-time.h"
10#include "icc_async.h"
11#include "ifd_db2com.h"
12#include "ifd_phoenix.h"
13#include "io_serial.h"
14
15#define OK 0
16#define ERROR 1
17
18#define GPIO_PIN (1 << (reader->detect - 4))
19
20static inline int reader_use_gpio(struct s_reader * reader) {
21 return reader->use_gpio && reader->detect > 4;
22}
23
24static void set_gpio(struct s_reader * reader, int32_t level)
25{
26 int ret = 0;
27
28 ret |= read(reader->gpio_outen, &reader->gpio, sizeof(reader->gpio));
29 reader->gpio |= GPIO_PIN;
30 ret |= write(reader->gpio_outen, &reader->gpio, sizeof(reader->gpio));
31
32 ret |= read(reader->gpio_out, &reader->gpio, sizeof(reader->gpio));
33 if (level > 0)
34 reader->gpio |= GPIO_PIN;
35 else
36 reader->gpio &= ~GPIO_PIN;
37 ret |= write(reader->gpio_out, &reader->gpio, sizeof(reader->gpio));
38
39 rdr_debug_mask(reader, D_IFD, "%s level: %d ret: %d", __func__, level, ret);
40}
41
42static void set_gpio_input(struct s_reader * reader)
43{
44 int ret = 0;
45 ret |= read(reader->gpio_outen, &reader->gpio, sizeof(reader->gpio));
46 reader->gpio &= ~GPIO_PIN;
47 ret |= write(reader->gpio_outen, &reader->gpio, sizeof(reader->gpio));
48 rdr_debug_mask(reader, D_IFD, "%s ret:%d", __func__, ret);
49}
50
51static int32_t get_gpio(struct s_reader * reader)
52{
53 int ret = 0;
54 set_gpio_input(reader);
55 ret = read(reader->gpio_in, &reader->gpio, sizeof(reader->gpio));
56 rdr_debug_mask(reader, D_IFD, "%s ok:%d ret:%d", __func__, reader->gpio & GPIO_PIN, ret);
57 if (reader->gpio & GPIO_PIN)
58 return OK;
59 else
60 return ERROR;
61}
62
63int32_t Phoenix_Init (struct s_reader * reader)
64{
65 // First set card in reset state, to not change any parameters while communication ongoing
66 IO_Serial_RTS_Set(reader);
67 if (reader->crdr.flush) IO_Serial_Flush(reader);
68
69 // define reader->gpio number used for card detect and reset. ref to globals.h
70 if (reader_use_gpio(reader))
71 {
72 reader->gpio_outen = open("/dev/gpio/outen", O_RDWR);
73 reader->gpio_out = open("/dev/gpio/out", O_RDWR);
74 reader->gpio_in = open("/dev/gpio/in", O_RDWR);
75 rdr_debug_mask(reader, D_IFD, "init gpio_outen:%d gpio_out:%d gpio_in:%d",
76 reader->gpio_outen, reader->gpio_out, reader->gpio_in);
77 set_gpio_input(reader);
78 }
79
80 rdr_debug_mask(reader, D_IFD, "Initializing reader type=%d", reader->typ);
81
82 /* Default serial port settings */
83 if (reader->atr[0] == 0) {
84 if(IO_Serial_SetParams (reader, DEFAULT_BAUDRATE, 8, PARITY_EVEN, 2, NULL, NULL)) return ERROR;
85 if (reader->crdr.flush) IO_Serial_Flush(reader);
86 }
87 return OK;
88}
89
90int32_t Phoenix_GetStatus (struct s_reader * reader, int32_t * status)
91{
92 // detect card via defined reader->gpio
93 if (reader_use_gpio(reader)) {
94 *status = !get_gpio(reader);
95 return OK;
96 } else {
97 return IO_Serial_GetStatus(reader, status);
98 }
99}
100
101int32_t Phoenix_Reset (struct s_reader * reader, ATR * atr)
102{
103 rdr_debug_mask(reader, D_IFD, "Resetting card");
104 int32_t ret;
105 int32_t i;
106 unsigned char buf[ATR_MAX_SIZE];
107 int32_t parity[3] = {PARITY_EVEN, PARITY_ODD, PARITY_NONE};
108
109 if ( ! reader->ins7e11_fast_reset ) {
110 call (IO_Serial_SetBaudrate(reader, DEFAULT_BAUDRATE));
111 }
112 else {
113 rdr_log(reader, "Doing fast reset");
114 }
115
116 for(i=0; i<3; i++) {
117 if (reader->crdr.flush) IO_Serial_Flush(reader);
118 if (reader->crdr.set_parity) IO_Serial_SetParity (reader, parity[i]);
119
120 ret = ERROR;
121
122 IO_Serial_Ioctl_Lock(reader, 1);
123 if (reader_use_gpio(reader))
124 set_gpio(reader, 0);
125 else
126 IO_Serial_RTS_Set(reader);
127
128 cs_sleepms(50);
129
130 // felix: set card reset hi (inactive)
131 if (reader_use_gpio(reader))
132 set_gpio_input(reader);
133 else
134 IO_Serial_RTS_Clr(reader);
135 cs_sleepms(50);
136 IO_Serial_Ioctl_Lock(reader, 0);
137
138 int32_t n=0;
139 while(n<ATR_MAX_SIZE && !IO_Serial_Read(reader, 0, ATR_TIMEOUT, 1, buf+n))
140 n++;
141 if(n==0)
142 continue;
143 if (ATR_InitFromArray (atr, buf, n) != ERROR)
144 ret = OK;
145 // Succesfully retrieve ATR
146 if (ret == OK)
147 break;
148 }
149
150 return ret;
151}
152
153int32_t Phoenix_Close (struct s_reader * reader)
154{
155 rdr_debug_mask(reader, D_IFD, "Closing phoenix device %s", reader->device);
156 if (reader_use_gpio(reader))
157 {
158 if (reader->gpio_outen > -1)
159 close(reader->gpio_outen);
160 if (reader->gpio_out > -1)
161 close(reader->gpio_out);
162 if (reader->gpio_in > -1)
163 close(reader->gpio_in);
164 }
165 IO_Serial_Close(reader);
166 return OK;
167}
168
169/*
170int32_t Phoenix_FastReset (struct s_reader * reader, int32_t delay)
171{
172 IO_Serial_Ioctl_Lock(reader, 1);
173 if (reader_use_gpio(reader))
174 set_gpio(reader, 0);
175 else
176 IO_Serial_RTS_Set(reader);
177
178 cs_sleepms(delay);
179
180 // set card reset hi (inactive)
181 if (reader_use_gpio(reader))
182 set_gpio_input(reader);
183 else
184 IO_Serial_RTS_Clr(reader);
185
186 IO_Serial_Ioctl_Lock(reader, 0);
187
188 cs_sleepms(50);
189
190 IO_Serial_Flush(reader);
191 return 0;
192
193}
194*/
195static int32_t mouse_init(struct s_reader *reader) {
196 if (detect_db2com_reader(reader)) {
197 cardreader_db2com(&reader->crdr);
198 return reader->crdr.reader_init(reader);
199 }
200
201 reader->handle = open (reader->device, O_RDWR | O_NOCTTY| O_NONBLOCK);
202 if (reader->handle < 0) {
203 rdr_log(reader, "ERROR: Opening device %s (errno=%d %s)",
204 reader->device, errno, strerror(errno));
205 return ERROR;
206 }
207 if (Phoenix_Init(reader)) {
208 rdr_log(reader, "ERROR: Phoenix_Init returns error");
209 Phoenix_Close (reader);
210 return ERROR;
211 }
212 return OK;
213}
214
215void cardreader_mouse(struct s_cardreader *crdr)
216{
217 crdr->desc = "mouse";
218 crdr->typ = R_MOUSE;
219 crdr->flush = 1;
220 crdr->read_written = 1;
221 crdr->need_inverse = 1;
222 crdr->reader_init = mouse_init;
223 crdr->get_status = Phoenix_GetStatus;
224 crdr->activate = Phoenix_Reset;
225 crdr->transmit = IO_Serial_Transmit;
226 crdr->receive = IO_Serial_Receive;
227 crdr->close = Phoenix_Close;
228 crdr->set_parity = IO_Serial_SetParity;
229 crdr->set_baudrate = IO_Serial_SetBaudrate;
230}
231#endif
Note: See TracBrowser for help on using the repository browser.