use DEBUGP, not printf (Bjoern Riemer)
[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 #include "rfid_reader_rc632_common.h"
42
43 /* FIXME */
44 #include "rc632.h"
45
46 #define SENDBUF_LEN     (256+4+10) /* 256bytes max FSD/FSC, plus 4 bytes header,
47                                     plus 10 bytes reserve */
48 #define RECVBUF_LEN     SENDBUF_LEN
49
50 static char snd_buf[SENDBUF_LEN];
51 static char rcv_buf[RECVBUF_LEN];
52 static struct openpcd_hdr *snd_hdr;
53 static struct openpcd_hdr *rcv_hdr;
54
55 #ifndef LIBRFID_FIRMWARE
56
57 #ifdef  __MINGW32__
58 #include "libusb_dyn.h"
59 #else /*__MINGW32__*/
60 #include <usb.h>
61 #endif/*__MINGW32__*/
62
63 #ifdef DEBUG_REGISTER
64 #define DEBUGRC DEBUGPC
65 #define DEBUGR DEBUGP
66 #else
67 #define DEBUGRC(x, args ...)    do {} while(0)
68 #define DEBUGR(x, args ...)     do {} while(0)
69 #endif
70
71 static struct usb_device *dev;
72 static struct usb_dev_handle *hdl;
73
74 static int openpcd_send_command(u_int8_t cmd, u_int8_t reg, u_int8_t val,
75                                 u_int16_t len, const unsigned char *data)
76 {
77         int ret;
78         u_int16_t cur;
79
80         snd_hdr->cmd = cmd;
81         snd_hdr->reg = reg;
82         snd_hdr->val = val;
83         snd_hdr->flags = OPENPCD_FLAG_RESPOND;
84         if (data && len)
85                 memcpy(snd_hdr->data, data, len);
86
87         cur = sizeof(*snd_hdr) + len;
88
89         return usb_bulk_write(hdl, OPENPCD_OUT_EP, (char *)snd_hdr, cur, 1000);
90 }
91
92 static int openpcd_recv_reply(void)
93 {
94         int ret;
95
96         ret = usb_bulk_read(hdl, OPENPCD_IN_EP, rcv_buf, sizeof(rcv_buf), 1000);
97
98         return ret;
99 }
100
101 static int openpcd_xcv(u_int8_t cmd, u_int8_t reg, u_int8_t val,
102                         u_int16_t len, const unsigned char *data)
103 {
104         int ret;
105         
106         ret = openpcd_send_command(cmd, reg, val, len, data);
107         if (ret < 0)
108                 return ret;
109         if (ret < sizeof(struct openpcd_hdr))
110                 return -EINVAL;
111
112         return openpcd_recv_reply();
113 }
114
115 struct usb_id {
116         u_int16_t vid;
117         u_int16_t pid;
118 };
119
120 static const struct usb_id opcd_usb_ids[] = {
121         { .vid = 0x2342, .pid = 0x0001 },       /* prototypes */
122         { .vid = 0x16c0, .pid = 0x076b },       /* first official device id */
123 };
124
125 static struct usb_device *find_opcd_device(void)
126 {
127         struct usb_bus *bus;
128
129         for (bus = usb_get_busses(); bus; bus = bus->next) {
130                 struct usb_device *dev;
131                 for (dev = bus->devices; dev; dev = dev->next) {
132                         int i;
133                         for (i = 0; i < ARRAY_SIZE(opcd_usb_ids); i++) {
134                                 const struct usb_id *id = &opcd_usb_ids[i];
135                                 if (dev->descriptor.idVendor == id->vid &&
136                                     dev->descriptor.idProduct == id->pid)
137                                         return dev;
138                         }
139                 }
140         }
141         return NULL;
142 }
143
144 /* RC632 access primitives for librfid inside reader firmware */
145
146 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
147                              unsigned char reg, unsigned char value)
148 {
149         int ret;
150
151         DEBUGR("reg=0x%02x, val=%02x: ", reg, value);
152
153         ret = openpcd_xcv(OPENPCD_CMD_WRITE_REG, reg, value, 0, NULL);
154         if (ret < 0)
155                 DEBUGRC("ERROR sending command\n");
156         else
157                 DEBUGRC("OK\n");
158
159         return ret;
160 }
161
162 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
163                             unsigned char reg,
164                             unsigned char *value)
165 {
166         int ret;        
167
168         DEBUGR("reg=0x%02x, ", reg);
169
170         ret = openpcd_xcv(OPENPCD_CMD_READ_REG, reg, 0, 0, NULL);
171         if (ret < 0) {
172                 DEBUGRC("ERROR sending command\n");
173                 return ret;
174         }
175
176         if (ret < sizeof(struct openpcd_hdr)) {
177                 DEBUGRC("ERROR: short packet\n");
178                 return ret;
179         }
180
181         *value = rcv_hdr->val;
182         DEBUGRC("val=%02x: OK\n", *value);
183
184         return ret;
185 }
186
187 static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
188                              unsigned char num_bytes,
189                              unsigned char *buf)
190 {
191         int ret;
192
193         DEBUGR(" ");
194
195         ret = openpcd_xcv(OPENPCD_CMD_READ_FIFO, 0x00, num_bytes, 0, NULL);
196         if (ret < 0) {
197                 DEBUGRC("ERROR sending command\n");
198                 return ret;
199         }
200         DEBUGRC("ret = %d\n", ret);
201
202         memcpy(buf, rcv_hdr->data, ret - sizeof(struct openpcd_hdr));
203         DEBUGRC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
204                 rfid_hexdump(rcv_hdr->data, ret - sizeof(struct openpcd_hdr)));
205
206         return ret;
207 }
208
209 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
210                              unsigned char len,
211                              const unsigned char *bytes,
212                              unsigned char flags)
213 {
214         int ret;
215
216         DEBUGR("len=%u, data=%s\n", len, rfid_hexdump(bytes, len));
217         ret = openpcd_xcv(OPENPCD_CMD_WRITE_FIFO, 0, 0, len, bytes);
218
219         return ret;
220 }
221
222 const struct rfid_asic_transport openpcd_rat = {
223         .name = "OpenPCD Dumb USB Protocol",
224         .priv.rc632 = {
225                 .fn = {
226                         .reg_write      = &openpcd_reg_write,
227                         .reg_read       = &openpcd_reg_read,
228                         .fifo_write     = &openpcd_fifo_write,
229                         .fifo_read      = &openpcd_fifo_read,
230                 },
231         },
232 };
233
234 static int openpcd_get_api_version(struct rfid_reader_handle *rh, u_int8_t *version)
235 {
236         int ret;
237         
238         // preset version result to zero
239         rcv_hdr->val=0;
240     
241         ret = openpcd_xcv(OPENPCD_CMD_GET_API_VERSION, 0, 0, 0, NULL);
242         if (ret < 0) {
243                 DEBUGPC("ERROR sending command [%i]\n", ret);
244                 return ret;
245         }
246
247         if (ret < sizeof(struct openpcd_hdr)) {
248                 DEBUGPC("ERROR: short packet [%i]\n", ret);
249                 return -EINVAL;
250         }
251
252         *version = rcv_hdr->val;
253         
254         return ret;
255 }
256
257 static int openpcd_get_environment(struct rfid_reader_handle *rh,
258                                    unsigned char num_bytes,
259                                    unsigned char *buf)
260 {
261         int ret;
262
263         DEBUGP(" ");
264
265         ret = openpcd_xcv(OPENPCD_CMD_GET_ENVIRONMENT, 0x00, num_bytes, 0, NULL);
266         if (ret < 0) {
267                 DEBUGPC("ERROR sending command [%i]\n",ret);
268                 return ret;
269         }
270         DEBUGPC("ret = %d\n", ret);
271
272         memcpy(buf, rcv_hdr->data, ret - sizeof(struct openpcd_hdr));
273         DEBUGPC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
274                 rfid_hexdump(rcv_hdr->data, ret - sizeof(struct openpcd_hdr)));
275
276         return ret;
277 }
278
279 static int openpcd_set_environment(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_getopt(struct rfid_reader_handle *rh, int optname,
356                           void *optval, unsigned int *optlen)
357 {
358         int rc;
359         u_int8_t *val_u8 = (u_int8_t *) optval;
360
361         switch (optname) {
362 #ifndef LIBRFID_FIRMWARE
363         case RFID_OPT_RDR_FW_VERSION:
364                 return openpcd_get_api_version(rh, val_u8);
365 #endif
366         default:
367                 return _rdr_rc632_getopt(rh, optname, optval, optlen);
368         }
369
370         return 0;
371 }
372
373
374 static struct rfid_reader_handle *
375 openpcd_open(void *data)
376 {
377         struct rfid_reader_handle *rh;
378         struct rfid_asic_transport_handle *rath;
379
380         snd_hdr = (struct openpcd_hdr *)snd_buf;
381         rcv_hdr = (struct openpcd_hdr *)rcv_buf;
382
383 #ifndef LIBRFID_FIRMWARE
384         usb_init();
385         if (usb_find_busses() < 0)
386                 return NULL;
387         if (usb_find_devices() < 0) 
388                 return NULL;
389         
390         dev = find_opcd_device();
391         if (!dev) {
392                 DEBUGP("No matching USB device found\n");
393                 return NULL;
394         }
395
396         hdl = usb_open(dev);
397         if (!hdl) {
398                 DEBUGP("Can't open USB device\n");
399                 return NULL;
400         }
401
402         if(usb_set_configuration(hdl, 1 ) < 0)
403         {
404             DEBUGP("setting config failed\n");
405             usb_close( hdl );
406             return NULL;
407         }
408                                                                         
409         if (usb_claim_interface(hdl, 0) < 0) {
410                 DEBUGP("Can't claim interface\n");
411                 usb_close(hdl);
412                 return NULL;
413         }
414 #endif
415
416         rh = malloc_reader_handle(sizeof(*rh));
417         if (!rh)
418                 return NULL;
419         memset(rh, 0, sizeof(*rh));
420
421         rath = malloc_rat_handle(sizeof(*rath));
422         if (!rath)
423                 goto out_rh;
424         memset(rath, 0, sizeof(*rath));
425
426         rath->rat = &openpcd_rat;
427         rh->reader = &rfid_reader_openpcd;
428
429         rh->ah = rc632_open(rath);
430         if (!rh->ah) 
431                 goto out_rath;
432
433         DEBUGP("returning %p\n", rh);
434         return rh;
435
436 out_rath:
437         free_rat_handle(rath);
438 out_rh:
439         free_reader_handle(rh);
440
441         return NULL;
442 }
443
444 static void
445 openpcd_close(struct rfid_reader_handle *rh)
446 {
447         struct rfid_asic_transport_handle *rath = rh->ah->rath;
448
449         rc632_close(rh->ah);
450         free_rat_handle(rath);
451         free_reader_handle(rh);
452
453 #ifndef LIBRFID_FIRMWARE
454         usb_close(hdl);
455 #endif
456 }
457
458 const struct rfid_reader rfid_reader_openpcd = {
459         .name   = "OpenPCD RFID Reader",
460         .id = RFID_READER_OPENPCD,
461         .open = &openpcd_open,
462         .close = &openpcd_close,
463         .getopt = &openpcd_getopt,
464 #ifndef LIBRFID_FIRMWARE
465         .reset = &openpcd_reset,
466 #endif
467         .setopt = &_rdr_rc632_setopt,
468         .init = &_rdr_rc632_l2_init,
469         .transceive = &_rdr_rc632_transceive,
470         .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
471                         (1 << RFID_LAYER2_ISO14443B) |
472                         (1 << RFID_LAYER2_ISO15693),
473         .proto_supported = (1 << RFID_PROTOCOL_TCL) |
474                         (1 << RFID_PROTOCOL_MIFARE_UL) |
475                         (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
476         .iso14443a = {
477                 .transceive_sf = &_rdr_rc632_transceive_sf,
478                 .transceive_acf = &_rdr_rc632_transceive_acf,
479                 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
480                          RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
481                 .set_speed = &_rdr_rc632_14443a_set_speed,
482         },
483         .iso15693 = {
484                 .transceive_ac = &_rdr_rc632_iso15693_transceive_ac,
485         },
486         .mifare_classic = {
487                 .setkey = &_rdr_rc632_mifare_setkey,
488                 .auth = &_rdr_rc632_mifare_auth,
489         },
490 };