example dump from debian7 inside vmware
[librfid] / openct-cm5121-librfid.patch
1 diff -Nru openct-0.6.7/src/ifd/Makefile.am openct-0.6.7-rfid/src/ifd/Makefile.am
2 --- openct-0.6.7/src/ifd/Makefile.am    2006-04-21 08:37:58.000000000 +0200
3 +++ openct-0.6.7-rfid/src/ifd/Makefile.am       2006-05-27 19:32:21.000000000 +0200
4 @@ -20,7 +20,7 @@
5         ifd-wbeiuu.c \
6         \
7         proto-gbp.c proto-sync.c proto-t0.c proto-t1.c \
8 -       proto-trans.c \
9 +       proto-trans.c proto-escape.c \
10         \
11         sys-sunray.c sys-solaris.c sys-bsd.c sys-linux.c sys-null.c sys-osx.c \
12         \
13 diff -Nru openct-0.6.7/src/ifd/ifd-ccid.c openct-0.6.7-rfid/src/ifd/ifd-ccid.c
14 --- openct-0.6.7/src/ifd/ifd-ccid.c     2006-04-24 21:52:03.000000000 +0200
15 +++ openct-0.6.7-rfid/src/ifd/ifd-ccid.c        2006-05-27 19:46:29.000000000 +0200
16 @@ -5,6 +5,9 @@
17   *
18   * 2005-04-20: Harald Welte <laforge@gnumonks.org>
19   *     Add support for PCMCIA based CCID Device (CardMan 4040)
20 + *
21 + * 2005-05-22: Harald Welte <laforge@gnumonks.org>
22 + *     Add suport for OmniKey Cardman 5121 RFID extensions
23   */
24  
25  #include "internal.h"
26 @@ -122,6 +125,7 @@
27  
28  #define SUPPORT_T0     0x1
29  #define SUPPORT_T1     0x2
30 +#define SUPPORT_ESCAPE 0x80
31  
32  #define SUPPORT_50V    1
33  #define SUPPORT_33V    2
34 @@ -740,6 +744,12 @@
35                 st->reader_type = TYPE_TPDU;
36         }
37  
38 +       if (de.idVendor == 0x076b && de.idProduct == 0x5121) {
39 +               /* special handling of RFID part of OmniKey 5121 */
40 +               reader->nslots++;       /* one virtual slot for RFID escape */
41 +               st->proto_support |= SUPPORT_ESCAPE;
42 +       }
43 +
44         return 0;
45  }
46  
47 @@ -816,6 +826,13 @@
48                 int any = 0;
49                 int i, j, bits, stat;
50  
51 +               if (st->proto_support & SUPPORT_ESCAPE
52 +                   && slot == reader->nslots-1) {
53 +                       ifd_debug(1, "virtual escape slot, setting card present\n");
54 +                       *status = IFD_CARD_PRESENT;
55 +                       return 0;
56 +               }
57 +
58                 i = 1 + (slot / 4);
59                 j = 2 * (slot % 4);
60                 stat = 0;
61 @@ -880,6 +897,8 @@
62         return 0;
63  }
64  
65 +static int ccid_set_protocol(ifd_reader_t *reader, int s, int proto);
66 +
67  /*
68   * Reset
69   */
70 @@ -898,6 +917,13 @@
71         if (!(status & IFD_CARD_PRESENT))
72                 return IFD_ERROR_NO_CARD;
73  
74 +       if (st->proto_support & SUPPORT_ESCAPE
75 +           && slot == reader->nslots-1) {
76 +               ifd_debug(1, "slot: %d, setting atr to 0xff", slot);
77 +               *((char *)atr) = 0xff;
78 +               ccid_set_protocol(reader, slot, IFD_PROTOCOL_ESCAPE);
79 +               return 1;
80 +       }
81         memset(ctlbuf, 0, 3);
82  
83         n = -1;
84 @@ -940,6 +966,17 @@
85         ifd_atr_info_t atr_info;
86         int r;
87  
88 +       slot = &reader->slot[s];
89 +
90 +       /* If we support RFID escaping, we only allow ESCAPE protocol
91 +        * at the last (== virtual) slot */
92 +       if ((st->proto_support & SUPPORT_ESCAPE)
93 +           && (proto != IFD_PROTOCOL_ESCAPE)
94 +           && (s == reader->nslots-1)) {
95 +               ct_error("reader doesn't support this protocol at this slot\n");
96 +               return IFD_ERROR_NOT_SUPPORTED;
97 +       }
98 +
99         switch (proto) {
100         case IFD_PROTOCOL_T0:
101                 if (!(st->proto_support & SUPPORT_T0)) {
102 @@ -953,13 +990,36 @@
103                         return IFD_ERROR_NOT_SUPPORTED;
104                 }
105                 break;
106 +       case IFD_PROTOCOL_ESCAPE:
107 +               /* virtual "escape" fallthrough protocol for stacking RFID
108 +                * protocol stack on top of openct */
109 +               if (!(st->proto_support & SUPPORT_ESCAPE)) {
110 +                       ct_error("reader does not support this protocol");
111 +                       return IFD_ERROR_NOT_SUPPORTED;
112 +               }
113 +               if (s != reader->nslots-1) {
114 +                       ct_error("reader doesn't support this protocol at this slot");
115 +                       return IFD_ERROR_NOT_SUPPORTED;
116 +               }
117 +               p = ifd_protocol_new(IFD_PROTOCOL_ESCAPE,reader, slot->dad);
118 +               if (!p) {
119 +                       ct_error("%s: internal error", reader->name);
120 +                       return -1;
121 +               }
122 +               if (slot->proto) {
123 +                       ifd_protocol_free(slot->proto);
124 +                       slot->proto = NULL;
125 +               }
126 +               slot->proto = p;
127 +               st->icc_proto[s] = proto;
128 +               ifd_debug(1, "set protocol to ESCAPE\n");
129 +               return 0;
130 +               break;
131         default:
132                 ct_error("protocol unknown");
133                 return IFD_ERROR_NOT_SUPPORTED;
134         }
135  
136 -       slot = &reader->slot[s];
137 -
138         if (st->reader_type == TYPE_APDU) {
139                 p = ifd_protocol_new(IFD_PROTOCOL_TRANSPARENT,
140                                      reader, slot->dad);
141 @@ -1103,6 +1163,27 @@
142         return 0;
143  }
144  
145 +static int ccid_escape(ifd_reader_t *reader, int slot, void *sbuf,
146 +                      size_t slen, void *rbuf, size_t rlen)
147 +{
148 +     unsigned char sendbuf[CCID_MAX_MSG_LEN];
149 +     unsigned char recvbuf[CCID_MAX_MSG_LEN];
150 +     int r;
151 +
152 +     ifd_debug(1, "slot: %d, slen %d, rlen %d", slot, slen, rlen);
153 +
154 +     r = ccid_prepare_cmd(reader, sendbuf, sizeof(sendbuf), slot,
155 +                         CCID_CMD_ESCAPE, NULL, sbuf, slen);
156 +     if (r < 0)
157 +         return r;
158 +
159 +     r = ccid_command(reader, &sendbuf[0], r, recvbuf, sizeof(recvbuf));
160 +     if (r < 0)
161 +         return r;
162 +
163 +     return ccid_extract_data(&recvbuf, r, rbuf, rlen);
164 +}
165 +
166  static int
167  ccid_transparent(ifd_reader_t * reader, int slot,
168                  const void *sbuf, size_t slen, void *rbuf, size_t rlen)
169 @@ -1177,6 +1258,7 @@
170         ccid_driver.transparent = ccid_transparent;
171         ccid_driver.send = ccid_send;
172         ccid_driver.recv = ccid_recv;
173 +       ccid_driver.escape = ccid_escape;
174  
175         ifd_driver_register("ccid", &ccid_driver);
176  }
177 diff -Nru openct-0.6.7/src/ifd/init.c openct-0.6.7-rfid/src/ifd/init.c
178 --- openct-0.6.7/src/ifd/init.c 2006-04-21 08:37:58.000000000 +0200
179 +++ openct-0.6.7-rfid/src/ifd/init.c    2006-05-27 19:32:21.000000000 +0200
180 @@ -54,6 +54,7 @@
181         ifd_protocol_register(&ifd_protocol_2wire);
182         ifd_protocol_register(&ifd_protocol_3wire);
183         ifd_protocol_register(&ifd_protocol_eurochip);
184 +       ifd_protocol_register(&ifd_protocol_esc);
185  
186         if (ifd_conf_get_integer("debug", &ival) >= 0 && ival > ct_config.debug)
187                 ct_config.debug = ival;
188 diff -Nru openct-0.6.7/src/ifd/internal.h openct-0.6.7-rfid/src/ifd/internal.h
189 --- openct-0.6.7/src/ifd/internal.h     2006-04-21 08:37:58.000000000 +0200
190 +++ openct-0.6.7-rfid/src/ifd/internal.h        2006-05-27 19:32:21.000000000 +0200
191 @@ -118,6 +118,7 @@
192  extern struct ifd_protocol_ops ifd_protocol_2wire;
193  extern struct ifd_protocol_ops ifd_protocol_3wire;
194  extern struct ifd_protocol_ops ifd_protocol_eurochip;
195 +extern struct ifd_protocol_ops ifd_protocol_esc;
196  
197  extern void ifd_acr30u_register(void);
198  extern void ifd_cardman_register(void);
199 diff -Nru openct-0.6.7/src/ifd/proto-escape.c openct-0.6.7-rfid/src/ifd/proto-escape.c
200 --- openct-0.6.7/src/ifd/proto-escape.c 1970-01-01 01:00:00.000000000 +0100
201 +++ openct-0.6.7-rfid/src/ifd/proto-escape.c    2006-05-27 19:32:21.000000000 +0200
202 @@ -0,0 +1,77 @@
203 +/*
204 + * Escape protocol - simply pass everything to the reader driver's escape()
205 + *
206 + * This is required for exporting access to vendor-specific CCID extensions,
207 + * such as the Omnikey CardMan 5121 RFID support.
208 + *
209 + * The higher-level applications select a virtual slot (the last available slot
210 + * number).  This virtual slot will automatically get the IFD_PROTOCOL_ESCAPE 
211 + * assgigned to it and can then be used to transceive() data to/from the CCID.
212 + *
213 + * It's a bit ugly, but I was unable to come up with something cleaner.
214 + *
215 + * Copyright (C) 2005, Harald Welte <laforge@gnumonks.org>
216 + */
217 +
218 +#include "internal.h"
219 +#include <unistd.h>
220 +#include <stdlib.h>
221 +#include <string.h>
222 +
223 +static int
224 +escape_init(ifd_protocol_t *prot)
225 +{
226 +       ifd_reader_t    *reader = prot->reader;
227 +       const ifd_driver_t *drv;
228 +
229 +       if (!reader || !(drv = reader->driver)
230 +        || !drv->ops || !drv->ops->escape)
231 +               return -1;
232 +       return 0;
233 +}
234 +
235 +static void
236 +escape_release(ifd_protocol_t *prot)
237 +{
238 +       /* NOP */
239 +}
240 +
241 +static int
242 +escape_set_param(ifd_protocol_t *prot, int type, long value)
243 +{
244 +       ct_error("set_pameter not supported");
245 +       return -1;
246 +}
247 +
248 +static int
249 +escape_get_param(ifd_protocol_t *prot, int type, long *result)
250 +{
251 +       ct_error("get_pameter not supported");
252 +       return -1;
253 +}
254 +
255 +static int
256 +escape_transceive(ifd_protocol_t *prot, int dad,
257 +               const void *sbuf, size_t slen,
258 +               void *rbuf, size_t rlen)
259 +{
260 +       ifd_reader_t    *reader = prot->reader;
261 +       const ifd_driver_t *drv = reader->driver;
262 +
263 +       return drv->ops->escape(reader, dad, sbuf, slen, rbuf, rlen);
264 +}
265 +
266 +struct ifd_protocol_ops        ifd_protocol_esc = {
267 +       IFD_PROTOCOL_ESCAPE,            /* id */
268 +       "escape",                       /* name */
269 +       sizeof(ifd_protocol_t),         /* size */
270 +       escape_init,                    /* init */
271 +       escape_release,                 /* release */
272 +       escape_set_param,               /* set_param */
273 +       escape_get_param,               /* get_param */
274 +       NULL,                           /* resynchronize */
275 +       escape_transceive,              /* transceive */
276 +       NULL,                           /* sync_read */
277 +       NULL,                           /* sync_write */
278 +};
279 +
280 diff -Nru openct-0.6.7/src/include/openct/driver.h openct-0.6.7-rfid/src/include/openct/driver.h
281 --- openct-0.6.7/src/include/openct/driver.h    2006-04-21 08:37:58.000000000 +0200
282 +++ openct-0.6.7-rfid/src/include/openct/driver.h       2006-05-27 19:32:21.000000000 +0200
283 @@ -318,6 +318,11 @@
284         int             (*sync_write)(ifd_reader_t *reader, int slot, int proto,
285                                 unsigned short addr,
286                                 const unsigned char *sbuf, size_t slen);
287 +
288 +       /*Support for transparent access to "escape" */
289 +       int             (*escape)(ifd_reader_t *reader, int slot,
290 +                               void *sbuf, size_t slen,
291 +                               void *rbuf, size_t rlen);
292  };
293  
294  extern void            ifd_driver_register(const char *,
295 diff -Nru openct-0.6.7/src/include/openct/ifd.h openct-0.6.7-rfid/src/include/openct/ifd.h
296 --- openct-0.6.7/src/include/openct/ifd.h       2006-04-21 08:37:58.000000000 +0200
297 +++ openct-0.6.7-rfid/src/include/openct/ifd.h  2006-05-27 19:42:54.000000000 +0200
298 @@ -29,6 +29,8 @@
299         IFD_PROTOCOL_TLP,               /* older Gemplus protocol */
300         IFD_PROTOCOL_GBP,               /* Gemplus block protocol */
301         IFD_PROTOCOL_EUROCHIP,          /* Eurochip Countercard */
302 +       IFD_PROTOCOL_TCL,               /* ISO 14443-4 T=CL */
303 +       IFD_PROTOCOL_ESCAPE,            /* Virtual 'escape' protocol */
304         IFD_PROTOCOL_TRANSPARENT = 128
305  };
306