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