updated win32 support for MINGW32-compiler/cross-compiler
[librfid] / src / rfid_reader_openpcd.c
1 /* OpenPCD specific RC632 transport layer 
2  *
3  * (C) 2006 by Harald Welte <laforge@gnumonks.org>
4  *
5  * The OpenPCD is an Atmel AT91SAM7Sxx based USB RFID reader.
6  * It's CL RC632 is connected via SPI.  OpenPCD has multiple firmware
7  * images.  This driver is for the "main_dumbreader" firmware.
8  *
9  * TODO:
10  * - put hdl from static variable into asic transport or reader handle 
11  */
12
13 /*
14  *  This program is free software; you can redistribute it and/or modify
15  *  it under the terms of the GNU General Public License version 2 
16  *  as published by the Free Software Foundation
17  *
18  *  This program is distributed in the hope that it will be useful,
19  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  *  GNU General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License
24  *  along with this program; if not, write to the Free Software
25  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26  */
27
28 //#define DEBUG
29
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34
35
36 #include <librfid/rfid.h>
37 #include <librfid/rfid_reader.h>
38 #include <librfid/rfid_asic.h>
39 #include <librfid/rfid_asic_rc632.h>
40 #include <librfid/rfid_reader_openpcd.h>
41 #include <librfid/rfid_layer2.h>
42 #include <librfid/rfid_protocol.h>
43
44 /* FIXME */
45 #include "rc632.h"
46
47 #define SENDBUF_LEN     (256+4+10) /* 256bytes max FSD/FSC, plus 4 bytes header,
48                                     plus 10 bytes reserve */
49 #define RECVBUF_LEN     SENDBUF_LEN
50
51 static char snd_buf[SENDBUF_LEN];
52 static char rcv_buf[RECVBUF_LEN];
53 static struct openpcd_hdr *snd_hdr;
54 static struct openpcd_hdr *rcv_hdr;
55
56
57 #ifndef LIBRFID_FIRMWARE
58
59 #ifdef  __MINGW32__
60 #include "libusb_dyn.h"
61 #else /*__MINGW32__*/
62 #include <usb.h>
63 #endif/*__MINGW32__*/
64
65 static struct usb_device *dev;
66 static struct usb_dev_handle *hdl;
67
68 static int openpcd_send_command(u_int8_t cmd, u_int8_t reg, u_int8_t val,
69                                 u_int16_t len, const unsigned char *data)
70 {
71         int ret;
72         u_int16_t cur;
73
74         snd_hdr->cmd = cmd;
75         snd_hdr->reg = reg;
76         snd_hdr->val = val;
77         snd_hdr->flags = OPENPCD_FLAG_RESPOND;
78         if (data && len)
79                 memcpy(snd_hdr->data, data, len);
80
81         cur = sizeof(*snd_hdr) + len;
82
83         return usb_bulk_write(hdl, OPENPCD_OUT_EP, (char *)snd_hdr, cur, 1000);
84 }
85
86 static int openpcd_recv_reply(void)
87 {
88         int ret;
89
90         ret = usb_bulk_read(hdl, OPENPCD_IN_EP, rcv_buf, sizeof(rcv_buf), 1000);
91
92         return ret;
93 }
94
95 static int openpcd_xcv(u_int8_t cmd, u_int8_t reg, u_int8_t val,
96                         u_int16_t len, const unsigned char *data)
97 {
98         int ret;
99         
100         ret = openpcd_send_command(cmd, reg, val, len, data);
101         if (ret < 0)
102                 return ret;
103         if (ret < sizeof(struct openpcd_hdr))
104                 return -EINVAL;
105
106         return openpcd_recv_reply();
107 }
108
109 struct usb_id {
110         u_int16_t vid;
111         u_int16_t pid;
112 };
113
114 static const struct usb_id opcd_usb_ids[] = {
115         { .vid = 0x2342, .pid = 0x0001 },       /* prototypes */
116         { .vid = 0x16c0, .pid = 0x076b },       /* first official device id */
117 };
118
119 static struct usb_device *find_opcd_device(void)
120 {
121         struct usb_bus *bus;
122
123         for (bus = usb_get_busses(); bus; bus = bus->next) {
124                 struct usb_device *dev;
125                 for (dev = bus->devices; dev; dev = dev->next) {
126                         int i;
127                         for (i = 0; i < ARRAY_SIZE(opcd_usb_ids); i++) {
128                                 const struct usb_id *id = &opcd_usb_ids[i];
129                                 if (dev->descriptor.idVendor == id->vid &&
130                                     dev->descriptor.idProduct == id->pid)
131                                         return dev;
132                         }
133                 }
134         }
135         return NULL;
136 }
137
138 /* RC632 access primitives for librfid inside reader firmware */
139
140 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
141                              unsigned char reg, unsigned char value)
142 {
143         int ret;
144
145         DEBUGP("reg=0x%02x, val=%02x: ", reg, value);
146
147         ret = openpcd_xcv(OPENPCD_CMD_WRITE_REG, reg, value, 0, NULL);
148         if (ret < 0)
149                 DEBUGPC("ERROR sending command\n");
150         else
151                 DEBUGPC("OK\n");
152
153         return ret;
154 }
155
156 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
157                             unsigned char reg,
158                             unsigned char *value)
159 {
160         int ret;        
161
162         DEBUGP("reg=0x%02x, ", reg);
163
164         ret = openpcd_xcv(OPENPCD_CMD_READ_REG, reg, 0, 0, NULL);
165         if (ret < 0) {
166                 DEBUGPC("ERROR sending command\n");
167                 return ret;
168         }
169
170         if (ret < sizeof(struct openpcd_hdr)) {
171                 DEBUGPC("ERROR: short packet\n");
172                 return ret;
173         }
174
175         *value = rcv_hdr->val;
176         DEBUGPC("val=%02x: OK\n", *value);
177
178         return ret;
179 }
180
181 static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
182                              unsigned char num_bytes,
183                              unsigned char *buf)
184 {
185         int ret;
186
187         DEBUGP(" ");
188
189         ret = openpcd_xcv(OPENPCD_CMD_READ_FIFO, 0x00, num_bytes, 0, NULL);
190         if (ret < 0) {
191                 DEBUGPC("ERROR sending command\n");
192                 return ret;
193         }
194         DEBUGPC("ret = %d\n", ret);
195
196         memcpy(buf, rcv_hdr->data, ret - sizeof(struct openpcd_hdr));
197         DEBUGPC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
198                 rfid_hexdump(rcv_hdr->data, ret - sizeof(struct openpcd_hdr)));
199
200         return ret;
201 }
202
203 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
204                              unsigned char len,
205                              const unsigned char *bytes,
206                              unsigned char flags)
207 {
208         int ret;
209
210         DEBUGP("len=%u, data=%s\n", len, rfid_hexdump(bytes, len));
211         ret = openpcd_xcv(OPENPCD_CMD_WRITE_FIFO, 0, 0, len, bytes);
212
213         return ret;
214 }
215
216 const struct rfid_asic_transport openpcd_rat = {
217         .name = "OpenPCD Dumb USB Protocol",
218         .priv.rc632 = {
219                 .fn = {
220                         .reg_write      = &openpcd_reg_write,
221                         .reg_read       = &openpcd_reg_read,
222                         .fifo_write     = &openpcd_fifo_write,
223                         .fifo_read      = &openpcd_fifo_read,
224                 },
225         },
226 };
227
228 static int openpcd_get_api_version(struct rfid_reader_handle *rh, u_int8_t *version)
229 {
230         int ret;
231         
232         // preset version result to zero
233         rcv_hdr->val=0;
234     
235         ret = openpcd_xcv(OPENPCD_CMD_GET_API_VERSION, 0, 0, 0, NULL);
236         if (ret < 0) {
237                 DEBUGPC("ERROR sending command [%i]\n", ret);
238                 return ret;
239         }
240
241         if (ret < sizeof(struct openpcd_hdr)) {
242                 DEBUGPC("ERROR: short packet [%i]\n", ret);
243                 return -EINVAL;
244         }
245
246         *version = rcv_hdr->val;
247         
248         return ret;
249 }
250
251 static int openpcd_get_environment(
252     struct rfid_reader_handle *rh,
253     unsigned char num_bytes,
254     unsigned char *buf)
255 {
256         int ret;
257
258         DEBUGP(" ");
259
260         ret = openpcd_xcv(OPENPCD_CMD_GET_ENVIRONMENT, 0x00, num_bytes, 0, NULL);
261         if (ret < 0) {
262                 DEBUGPC("ERROR sending command [%i]\n",ret);
263                 return ret;
264         }
265         DEBUGPC("ret = %d\n", ret);
266
267         memcpy(buf, rcv_hdr->data, ret - sizeof(struct openpcd_hdr));
268         DEBUGPC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
269                 rfid_hexdump(rcv_hdr->data, ret - sizeof(struct openpcd_hdr)));
270
271         return ret;
272 }
273
274 static int openpcd_set_environment(
275     struct rfid_reader_handle *rh, 
276     const unsigned char num_bytes,
277     unsigned char *buf)
278 {
279         int ret;
280         
281         ret = openpcd_xcv(OPENPCD_CMD_SET_ENVIRONMENT, 0, 0, num_bytes, buf);
282         if (ret < 0) {
283                 DEBUGPC("ERROR sending command [%i]\n",ret);
284                 return ret;
285         }
286
287         if (ret < sizeof(struct openpcd_hdr)) {
288                 DEBUGPC("ERROR: short packet [%i]\n", ret);
289                 return -EINVAL;
290         }
291
292         return rcv_hdr->val;
293 }
294
295 static int openpcd_reset(struct rfid_reader_handle *rh)
296 {
297         int ret;
298
299         DEBUGP("reset ");
300         ret = openpcd_xcv(OPENPCD_CMD_RESET, 0, 0, 0, 0);
301
302         return ret;
303 }
304
305 #else
306 /* RC632 access primitives for librfid inside reader firmware */
307
308 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
309                              unsigned char reg, unsigned char value)
310 {
311         return opcd_rc632_reg_write(rath, reg, value);
312 }
313
314 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
315                             unsigned char reg,
316                             unsigned char *value)
317 {
318         return opcd_rc632_reg_read(rath, reg, value);
319 }
320
321
322 static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
323                              unsigned char num_bytes,
324                              unsigned char *buf)
325 {
326         return opcd_rc632_fifo_read(rath, num_bytes, buf);
327 }
328
329 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
330                              unsigned char len,
331                              const unsigned char *bytes,
332                              unsigned char flags)
333 {
334         return opcd_rc632_fifo_write(rath, len, bytes, flags);
335 }
336
337 const struct rfid_asic_transport openpcd_rat = {
338         .name = "OpenPCD Firmware RC632 Access",
339         .priv.rc632 = {
340                 .fn = {
341                         .reg_write      = &openpcd_reg_write,
342                         .reg_read       = &openpcd_reg_read,
343                         .fifo_write     = &openpcd_fifo_write,
344                         .fifo_read      = &openpcd_fifo_read,
345                 },
346         },
347 };
348
349 #endif /* LIBRFID_FIRMWARE */
350
351 static int openpcd_transceive(struct rfid_reader_handle *rh,
352                              enum rfid_frametype frametype,
353                              const unsigned char *tx_data, unsigned int tx_len,
354                              unsigned char *rx_data, unsigned int *rx_len,
355                              u_int64_t timeout, unsigned int flags)
356 {
357         return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
358                                                       tx_data, tx_len, 
359                                                       rx_data, rx_len,
360                                                       timeout, flags);
361 }
362
363 static int openpcd_transceive_sf(struct rfid_reader_handle *rh,
364                                unsigned char cmd, struct iso14443a_atqa *atqa)
365 {
366         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
367                                                                    cmd,
368                                                                    atqa);
369 }
370
371 static int
372 openpcd_transceive_acf(struct rfid_reader_handle *rh,
373                       struct iso14443a_anticol_cmd *cmd,
374                       unsigned int *bit_of_col)
375 {
376         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
377                                                          cmd, bit_of_col);
378 }
379
380 static int
381 openpcd_14443a_init(struct rfid_reader_handle *rh)
382 {
383         return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
384 }
385
386 static int
387 openpcd_14443a_set_speed(struct rfid_reader_handle *rh, 
388                         unsigned int tx,
389                         unsigned int speed)
390 {
391         u_int8_t rate;
392         
393         DEBUGP("setting rate: ");
394         switch (speed) {
395         case RFID_14443A_SPEED_106K:
396                 rate = 0x00;
397                 DEBUGPC("106K\n");
398                 break;
399         case RFID_14443A_SPEED_212K:
400                 rate = 0x01;
401                 DEBUGPC("212K\n");
402                 break;
403         case RFID_14443A_SPEED_424K:
404                 rate = 0x02;
405                 DEBUGPC("424K\n");
406                 break;
407         case RFID_14443A_SPEED_848K:
408                 rate = 0x03;
409                 DEBUGPC("848K\n");
410                 break;
411         default:
412                 return -EINVAL;
413                 break;
414         }
415         return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
416                                                                 tx, rate);
417 }
418
419 static int
420 openpcd_14443b_init(struct rfid_reader_handle *rh)
421 {
422         return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
423 }
424
425 static int
426 openpcd_15693_init(struct rfid_reader_handle *rh)
427 {
428         return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
429 }
430
431 static int
432 openpcd_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
433 {
434         return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
435 }
436
437 static int
438 openpcd_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
439                    u_int32_t serno, u_int8_t block)
440 {
441         return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah, 
442                                                         cmd, serno, block);
443 }
444
445 static struct rfid_reader_handle *
446 openpcd_open(void *data)
447 {
448         struct rfid_reader_handle *rh;
449         struct rfid_asic_transport_handle *rath;
450
451         snd_hdr = (struct openpcd_hdr *)snd_buf;
452         rcv_hdr = (struct openpcd_hdr *)rcv_buf;
453
454 #ifndef LIBRFID_FIRMWARE
455         usb_init();
456         if (usb_find_busses() < 0)
457                 return NULL;
458         if (usb_find_devices() < 0) 
459                 return NULL;
460         
461         dev = find_opcd_device();
462         if (!dev) {
463                 DEBUGP("No matching USB device found\n");
464                 return NULL;
465         }
466
467         hdl = usb_open(dev);
468         if (!hdl) {
469                 DEBUGP("Can't open USB device\n");
470                 return NULL;
471         }
472
473         if(usb_set_configuration(hdl, 1 ) < 0)
474         {
475             DEBUGP("setting config failed\n");
476             usb_close( hdl );
477             return NULL;
478         }
479                                                                         
480         if (usb_claim_interface(hdl, 0) < 0) {
481                 DEBUGP("Can't claim interface\n");
482                 usb_close(hdl);
483                 return NULL;
484         }
485 #endif
486
487         rh = malloc_reader_handle(sizeof(*rh));
488         if (!rh)
489                 return NULL;
490         memset(rh, 0, sizeof(*rh));
491
492         rath = malloc_rat_handle(sizeof(*rath));
493         if (!rath)
494                 goto out_rh;
495         memset(rath, 0, sizeof(*rath));
496
497         rath->rat = &openpcd_rat;
498         rh->reader = &rfid_reader_openpcd;
499
500         rh->ah = rc632_open(rath);
501         if (!rh->ah) 
502                 goto out_rath;
503
504         DEBUGP("returning %p\n", rh);
505         return rh;
506
507 out_rath:
508         free_rat_handle(rath);
509 out_rh:
510         free_reader_handle(rh);
511
512         return NULL;
513 }
514
515 static void
516 openpcd_close(struct rfid_reader_handle *rh)
517 {
518         struct rfid_asic_transport_handle *rath = rh->ah->rath;
519
520         rc632_close(rh->ah);
521         free_rat_handle(rath);
522         free_reader_handle(rh);
523
524 #ifndef LIBRFID_FIRMWARE
525         usb_close(hdl);
526 #endif
527 }
528
529 const struct rfid_reader rfid_reader_openpcd = {
530         .name   = "OpenPCD RFID Reader",
531         .id = RFID_READER_OPENPCD,
532         .open = &openpcd_open,
533         .close = &openpcd_close,
534         
535         .get_api_version = &openpcd_get_api_version,
536         .get_environment = &openpcd_get_environment,
537         .set_environment = &openpcd_set_environment,
538         .reset = &openpcd_reset,
539                                         
540         .transceive = &openpcd_transceive,
541         .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
542                         (1 << RFID_LAYER2_ISO14443B) |
543                         (1 << RFID_LAYER2_ISO15693),
544         .proto_supported = (1 << RFID_PROTOCOL_TCL) |
545                         (1 << RFID_PROTOCOL_MIFARE_UL) |
546                         (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
547         .iso14443a = {
548                 .init = &openpcd_14443a_init,
549                 .transceive_sf = &openpcd_transceive_sf,
550                 .transceive_acf = &openpcd_transceive_acf,
551                 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
552                          RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
553                 .set_speed = &openpcd_14443a_set_speed,
554         },
555         .iso14443b = {
556                 .init = &openpcd_14443b_init,
557         },
558         .iso15693 = {
559                 .init = &openpcd_15693_init,
560         },
561         .mifare_classic = {
562                 .setkey = &openpcd_mifare_setkey,
563                 .auth = &openpcd_mifare_auth,
564         },
565 };