1 /* $Id: ifdhandler.c,v 1.7 2001/08/12 12:03:01 heko Exp $ */
2
3 /*
4 copyright 2000
5 the regents of the university of michigan
6 all rights reserved
7
8 permission is granted to use, copy, create derivative works
9 and redistribute this software and such derivative works
10 for any purpose, so long as the name of the university of
11 michigan is not used in any advertising or publicity
12 pertaining to the use or distribution of this software
13 without specific, written prior authorization. if the
14 above copyright notice or any other identification of the
15 university of michigan is included in any copy of any
16 portion of this software, then the disclaimer below must
17 also be included.
18
19 this software is provided as is, without representation
20 from the university of michigan as to its fitness for any
21 purpose, and without warranty by the university of
22 michigan of any kind, either express or implied, including
23 without limitation the implied warranties of
24 merchantability and fitness for a particular purpose. the
25 regents of the university of michigan shall not be liable
26 for any damages, including special, indirect, incidental, or
27 consequential damages, with respect to any claim arising
28 out of or in connection with the use of the software, even
29 if it has been or is hereafter advised of the possibility of
30 such damages.
31 */
32
33 /*
34 * IFD handler for CITI sc7816 dumb reader driver
35 *
36 * Jim Rees, University of Michigan CITI, August 2000
37 */
38
39 #include <sys/types.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <string.h>
43
44 #include "sectok.h"
45 #include "todos_scrw.h"
46 #include "ifdhandler.h"
47
48 /* capability table */
49 struct cap_entry {
50 u_long tag;
51 int flags;
52 } cap_table[] = {
53 {SCTAG_OPEN_FLAGS, 0}, /* open flags */
54 {SCTAG_RESET_FLAGS, 0}, /* reset flags */
55 {0, 0} /* terminator */
56 };
57
58 static int CT_ttyn;
59 static int CT_atrlen;
60 static unsigned char CT_atr[MAX_ATR_SIZE];
61
62 static short silly_channels[] = {
63 0x3F8, 0x2F8, 0x3E8, 0x2E8, -1
64 };
65
66 u_long
IO_Create_Channel(u_long ChannelId)67 IO_Create_Channel(u_long ChannelId)
68 {
69 int i, ttyn;
70
71 #ifdef DEBUG
72 fprintf (stderr, "IO_Create_Channel: ChannelId == %06x\n", (int) ChannelId);
73 #endif /* DEBUG */
74
75 if ((ChannelId & 0xffff0000) != 0x10000)
76 return IFD_NOT_SUPPORTED;
77
78 ttyn = ChannelId & 0xffff;
79 for (i = 0; silly_channels[i] > 0; i++) {
80 if (silly_channels[i] == ttyn) {
81 ttyn = i;
82 break;
83 }
84 }
85
86 #ifdef DEBUG
87 fprintf (stderr, "IO_Create_Channel: ttyn == %d, flags == %d\n",
88 ttyn, cap_table[0].flags);
89 #endif /* DEBUG */
90
91 CT_ttyn = todos_scopen(ttyn, cap_table[0].flags, NULL);
92
93 #ifdef DEBUG
94 fprintf (stderr, "IO_Create_Channel: todos_scopen() returns %d\n", CT_ttyn);
95 #endif /* DEBUG */
96
97 return (CT_ttyn < 0) ? IFD_COMMUNICATION_ERROR : 0;
98 }
99
100 u_long
IO_Close_Channel()101 IO_Close_Channel()
102 {
103 todos_scclose(CT_ttyn);
104 CT_ttyn = -1;
105 CT_atrlen = 0;
106 return 0;
107 }
108
109 u_long
IFD_Get_Capabilities(u_long Tag,u_char Value[])110 IFD_Get_Capabilities(u_long Tag, u_char Value[])
111 {
112 u_long r = IFD_ERROR_NOT_SUPPORTED;
113
114 switch (Tag) {
115 case 0x201:
116 r = 0;
117 break;
118 case SCTAG_IFD_CARDPRESENT:
119 *(u_long *) Value = todos_sccardpresent(CT_ttyn) ? IFD_ICC_PRESENT : IFD_ICC_NOT_PRESENT;
120 r = 0;
121 break;
122 case TAG_IFD_ATR:
123 memcpy(Value, CT_atr, CT_atrlen);
124 r = 0;
125 break;
126 case SCTAG_IFD_ATRLEN:
127 *(int *) Value = CT_atrlen;
128 r = 0;
129 break;
130 case SCTAG_OPEN_FLAGS:
131 *(int *) Value = cap_table[0].flags;
132 r = 0;
133 break;
134 case SCTAG_RESET_FLAGS:
135 *(int *) Value = cap_table[1].flags;
136 r = 0;
137 break;
138 }
139
140 return r;
141 }
142
143 /* NI:
144 set capabilities.
145 I take only one of two tags, that is, 0x800 (open flags) and
146 0x801 (reset flags).
147
148 input: tag and the value.
149 output: if the tag is one of the two, set it, and return 0.
150 otherwise return NOT_SUPPORTED.
151 */
152
153 u_long
IFD_Set_Capabilities(u_long Tag,u_char Value[])154 IFD_Set_Capabilities(u_long Tag, u_char Value[])
155 {
156 int i;
157
158 for (i = 0 ; cap_table[i].tag != 0 ; i++ ) {
159 if (Tag == cap_table[i].tag) {
160 /* found the tag. set it. */
161 cap_table[i].flags = (int)*((int *)Value);
162 #ifdef DEBUG
163 fprintf (stderr, "cap_table[%x].flags = %d\n",
164 (int) Tag, cap_table[i].flags);
165 #endif /* DEBUG */
166
167 return 0;
168 }
169 }
170
171 return IFD_ERROR_NOT_SUPPORTED;
172 }
173
174 u_long
IFD_Set_Protocol_Parameters(u_long ProtocolType,u_char SelectionFlags,u_char PTS1,u_char PTS2,u_char PTS3)175 IFD_Set_Protocol_Parameters(u_long ProtocolType, u_char SelectionFlags, u_char PTS1, u_char PTS2, u_char PTS3)
176 {
177 return IFD_ERROR_NOT_SUPPORTED;
178 }
179
180 u_long
IFD_Power_ICC(u_long a)181 IFD_Power_ICC(u_long a)
182 {
183 u_long r;
184
185 if (a == IFD_POWER_UP)
186 r = (todos_scdtr(CT_ttyn, 1) < 0) ? IFD_ERROR_POWER_ACTION : 0;
187 else if (a == IFD_POWER_DOWN)
188 r = (todos_scdtr(CT_ttyn, 0) < 0) ? IFD_ERROR_POWER_ACTION : 0;
189 else if (a == IFD_RESET) {
190 CT_atrlen = todos_scxreset(CT_ttyn, cap_table[1].flags, CT_atr, NULL);
191 r = (CT_atrlen <= 0) ? IFD_ERROR_POWER_ACTION : 0;
192 } else
193 r = IFD_NOT_SUPPORTED;
194
195 return r;
196 }
197
198 u_long
IFD_Swallow_ICC()199 IFD_Swallow_ICC()
200 {
201 return IFD_ERROR_NOT_SUPPORTED;
202 }
203
204 u_long
IFD_Eject_ICC()205 IFD_Eject_ICC()
206 {
207 return (todos_scdtr(CT_ttyn, 0) < 0) ? IFD_COMMUNICATION_ERROR : 0;
208 }
209
210 u_long
IFD_Confiscate_ICC()211 IFD_Confiscate_ICC()
212 {
213 return IFD_ERROR_NOT_SUPPORTED;
214 }
215
216 u_long
IFD_Transmit_to_ICC(struct SCARD_IO_HEADER SendPci,u_char ibuf[],u_long ilen,u_char obuf[],u_long * olen,struct SCARD_IO_HEADER * RecvPci)217 IFD_Transmit_to_ICC(struct SCARD_IO_HEADER SendPci,
218 u_char ibuf[], u_long ilen, u_char obuf[], u_long *olen,
219 struct SCARD_IO_HEADER *RecvPci)
220 {
221 unsigned char buf[255+2];
222 int n, sw1, sw2;
223
224 #ifdef DEBUG
225 printf("p3 %x ilen %x *olen %x\n", ibuf[4], (int) ilen, (int) *olen);
226 #endif
227 ilen -= 5;
228
229 if (ilen > 0) {
230 /* "in" data; stupid ifd interface tacks le on the end */
231 if (ilen == ibuf[4] + 1) {
232 n = ibuf[5 + --ilen];
233 #ifdef DEBUG
234 printf("found trailing le %d\n", n);
235 #endif
236 } else if (*olen > 2)
237 n = sizeof buf;
238 else
239 n = 0;
240 } else
241 n = ibuf[4];
242
243 n = todos_scrw(CT_ttyn, ibuf[0], ibuf[1], ibuf[2], ibuf[3], ilen, &ibuf[5], n, buf, &sw1, &sw2);
244 if (n < 0)
245 return IFD_COMMUNICATION_ERROR;
246
247 if (n)
248 memcpy(obuf, buf, n);
249
250 obuf[n+0] = sw1;
251 obuf[n+1] = sw2;
252 *olen = n + 2;
253
254 return 0;
255 }
256
257 u_long
IFD_Is_ICC_Present()258 IFD_Is_ICC_Present()
259 {
260 return (todos_sccardpresent(CT_ttyn) ? IFD_ICC_PRESENT : IFD_ICC_NOT_PRESENT);
261 }
262