- use C99 compiler flags
[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 #include <usb.h>
36
37 #include <librfid/rfid.h>
38 #include <librfid/rfid_reader.h>
39 #include <librfid/rfid_asic.h>
40 #include <librfid/rfid_asic_rc632.h>
41 #include <librfid/rfid_reader_openpcd.h>
42 #include <librfid/rfid_layer2.h>
43 #include <librfid/rfid_protocol.h>
44
45 /* FIXME */
46 #include "rc632.h"
47
48
49 #define SENDBUF_LEN     (256+4+10) /* 256bytes max FSD/FSC, plus 4 bytes header,
50                                     plus 10 bytes reserve */
51 #define RECVBUF_LEN     SENDBUF_LEN
52
53 static char snd_buf[SENDBUF_LEN];
54 static char rcv_buf[RECVBUF_LEN];
55 static struct openpcd_hdr *snd_hdr;
56 static struct openpcd_hdr *rcv_hdr;
57
58
59 static struct usb_device *dev;
60 static struct usb_dev_handle *hdl;
61
62 static int openpcd_send_command(u_int8_t cmd, u_int8_t reg, u_int8_t val,
63                                 u_int16_t len, const unsigned char *data)
64 {
65         int ret;
66         u_int16_t cur;
67
68         snd_hdr->cmd = cmd;
69         snd_hdr->reg = reg;
70         snd_hdr->val = val;
71         snd_hdr->flags = OPENPCD_FLAG_RESPOND;
72         if (data && len)
73                 memcpy(snd_hdr->data, data, len);
74
75         cur = sizeof(*snd_hdr) + len;
76
77         return usb_bulk_write(hdl, OPENPCD_OUT_EP, (char *)snd_hdr, cur, 0);
78 }
79
80 static int openpcd_recv_reply(void)
81 {
82         int ret;
83
84         ret = usb_bulk_read(hdl, OPENPCD_IN_EP, rcv_buf, sizeof(rcv_buf), 1000);
85
86         return ret;
87 }
88
89 static int openpcd_xcv(u_int8_t cmd, u_int8_t reg, u_int8_t val,
90                         u_int16_t len, const unsigned char *data)
91 {
92         int ret;
93         
94         ret = openpcd_send_command(cmd, reg, val, len, data);
95         if (ret < 0)
96                 return ret;
97         if (ret < sizeof(sizeof(struct openpcd_hdr)))
98                 return -EINVAL;
99
100         return openpcd_recv_reply();
101 }
102
103 struct usb_id {
104         u_int16_t vid;
105         u_int16_t pid;
106 };
107
108 static const struct usb_id opcd_usb_ids[] = {
109         { .vid = 0x2342, .pid = 0x0001 },       /* prototypes */
110         { .vid = 0x16c0, .pid = 0x076b },       /* first official device id */
111 };
112
113 static struct usb_device *find_opcd_device(void)
114 {
115         struct usb_bus *bus;
116
117         for (bus = usb_busses; bus; bus = bus->next) {
118                 struct usb_device *dev;
119                 for (dev = bus->devices; dev; dev = dev->next) {
120                         int i;
121                         printf("usb: %4x:%4x\n", dev->descriptor.idVendor,
122                                 dev->descriptor.idProduct);
123                         for (i = 0; i < ARRAY_SIZE(opcd_usb_ids); i++) {
124                                 const struct usb_id *id = &opcd_usb_ids[i];
125                                 printf("%x:%x\n", id->vid, id->pid);
126                                 if (dev->descriptor.idVendor == id->vid &&
127                                     dev->descriptor.idProduct == id->pid)
128                                         return dev;
129                         }
130                 }
131         }
132         return NULL;
133 }
134
135 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
136                              unsigned char reg, unsigned char value)
137 {
138         int ret;
139
140         DEBUGP("reg=0x%02x, val=%02x: ", reg, value);
141
142         ret = openpcd_xcv(OPENPCD_CMD_WRITE_REG, reg, value, 0, NULL);
143         if (ret < 0)
144                 DEBUGPC("ERROR sending command\n");
145         else
146                 DEBUGPC("OK\n");
147
148         return ret;
149 }
150
151 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
152                             unsigned char reg,
153                             unsigned char *value)
154 {
155         int ret;        
156
157         DEBUGP("reg=0x%02x, ", reg);
158
159         ret = openpcd_xcv(OPENPCD_CMD_READ_REG, reg, 0, 0, NULL);
160         if (ret < 0) {
161                 DEBUGPC("ERROR sending command\n");
162                 return ret;
163         }
164
165         if (ret < sizeof(struct openpcd_hdr)) {
166                 DEBUGPC("ERROR: short packet\n");
167                 return ret;
168         }
169
170         *value = rcv_hdr->val;
171         DEBUGPC("val=%02x: OK\n", *value);
172
173         return ret;
174 }
175
176 static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
177                              unsigned char num_bytes,
178                              unsigned char *buf)
179 {
180         int ret;
181
182         DEBUGP(" ");
183
184         ret = openpcd_xcv(OPENPCD_CMD_READ_FIFO, 0x00, num_bytes, 0, NULL);
185         if (ret < 0) {
186                 DEBUGPC("ERROR sending command\n");
187                 return ret;
188         }
189         DEBUGPC("ret = %d\n", ret);
190
191         memcpy(buf, rcv_hdr->data, ret - sizeof(struct openpcd_hdr));
192         DEBUGPC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
193                 rfid_hexdump(rcv_hdr->data, ret - sizeof(struct openpcd_hdr)));
194
195         return ret;
196 }
197
198 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
199                              unsigned char len,
200                              const unsigned char *bytes,
201                              unsigned char flags)
202 {
203         int ret;
204
205         DEBUGP("len=%u, data=%s\n", len, rfid_hexdump(bytes, len));
206         ret = openpcd_xcv(OPENPCD_CMD_WRITE_FIFO, 0, 0, len, bytes);
207
208         return ret;
209 }
210
211 static int openpcd_transceive(struct rfid_reader_handle *rh,
212                              enum rfid_frametype frametype,
213                              const unsigned char *tx_data, unsigned int tx_len,
214                              unsigned char *rx_data, unsigned int *rx_len,
215                              u_int64_t timeout, unsigned int flags)
216 {
217         return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
218                                                       tx_data, tx_len, 
219                                                       rx_data, rx_len,
220                                                       timeout, flags);
221 }
222
223 static int openpcd_transceive_sf(struct rfid_reader_handle *rh,
224                                unsigned char cmd, struct iso14443a_atqa *atqa)
225 {
226         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
227                                                                    cmd,
228                                                                    atqa);
229 }
230
231 static int
232 openpcd_transceive_acf(struct rfid_reader_handle *rh,
233                       struct iso14443a_anticol_cmd *cmd,
234                       unsigned int *bit_of_col)
235 {
236         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
237                                                          cmd, bit_of_col);
238 }
239
240 static int
241 openpcd_14443a_init(struct rfid_reader_handle *rh)
242 {
243         return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
244 }
245
246 static int
247 openpcd_14443a_set_speed(struct rfid_reader_handle *rh, 
248                         unsigned int tx,
249                         unsigned int speed)
250 {
251         u_int8_t rate;
252         
253         DEBUGP("setting rate: ");
254         switch (speed) {
255         case RFID_14443A_SPEED_106K:
256                 rate = 0x00;
257                 DEBUGPC("106K\n");
258                 break;
259         case RFID_14443A_SPEED_212K:
260                 rate = 0x01;
261                 DEBUGPC("212K\n");
262                 break;
263         case RFID_14443A_SPEED_424K:
264                 rate = 0x02;
265                 DEBUGPC("424K\n");
266                 break;
267         case RFID_14443A_SPEED_848K:
268                 rate = 0x03;
269                 DEBUGPC("848K\n");
270                 break;
271         default:
272                 return -EINVAL;
273                 break;
274         }
275         return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
276                                                                 tx, rate);
277 }
278
279 static int
280 openpcd_14443b_init(struct rfid_reader_handle *rh)
281 {
282         return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
283 }
284
285 static int
286 openpcd_15693_init(struct rfid_reader_handle *rh)
287 {
288         return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
289 }
290
291 static int
292 openpcd_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
293 {
294         return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
295 }
296
297 static int
298 openpcd_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
299                    u_int32_t serno, u_int8_t block)
300 {
301         return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah, 
302                                                         cmd, serno, block);
303 }
304
305 struct rfid_asic_transport openpcd_ccid = {
306         .name = "OpenPCD Dumb USB Protocol",
307         .priv.rc632 = {
308                 .fn = {
309                         .reg_write      = &openpcd_reg_write,
310                         .reg_read       = &openpcd_reg_read,
311                         .fifo_write     = &openpcd_fifo_write,
312                         .fifo_read      = &openpcd_fifo_read,
313                 },
314         },
315 };
316
317 static struct rfid_reader_handle *
318 openpcd_open(void *data)
319 {
320         struct rfid_reader_handle *rh;
321         struct rfid_asic_transport_handle *rath;
322
323         snd_hdr = (struct openpcd_hdr *)snd_buf;
324         rcv_hdr = (struct openpcd_hdr *)rcv_buf;
325
326         usb_init();
327         if (usb_find_busses() < 0)
328                 return NULL;
329         if (usb_find_devices() < 0) 
330                 return NULL;
331         
332         dev = find_opcd_device();
333         if (!dev) {
334                 DEBUGP("No matching USB device found\n");
335                 return NULL;
336         }
337
338         hdl = usb_open(dev);
339         if (!hdl) {
340                 DEBUGP("Can't open USB device\n");
341                 return NULL;
342         }
343
344         if (usb_claim_interface(hdl, 0) < 0) {
345                 DEBUGP("Can't claim interface\n");
346                 usb_close(hdl);
347                 return NULL;
348         }
349
350         rh = malloc(sizeof(*rh));
351         if (!rh)
352                 return NULL;
353         memset(rh, 0, sizeof(*rh));
354
355         rath = malloc(sizeof(*rath));
356         if (!rath)
357                 goto out_rh;
358         memset(rath, 0, sizeof(*rath));
359
360         rath->rat = &openpcd_ccid;
361         rh->reader = &rfid_reader_openpcd;
362
363         rh->ah = rc632_open(rath);
364         if (!rh->ah) 
365                 goto out_rath;
366
367         DEBUGP("returning %p\n", rh);
368         return rh;
369
370 out_rath:
371         free(rath);
372 out_rh:
373         free(rh);
374
375         return NULL;
376 }
377
378 static void
379 openpcd_close(struct rfid_reader_handle *rh)
380 {
381         struct rfid_asic_transport_handle *rath = rh->ah->rath;
382
383         rc632_close(rh->ah);
384         free(rath);
385         free(rh);
386
387         usb_close(hdl);
388 }
389
390 struct rfid_reader rfid_reader_openpcd = {
391         .name   = "OpenPCD RFID Reader",
392         .id = RFID_READER_OPENPCD,
393         .open = &openpcd_open,
394         .close = &openpcd_close,
395         .transceive = &openpcd_transceive,
396         .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
397                         (1 << RFID_LAYER2_ISO14443B) |
398                         (1 << RFID_LAYER2_ISO15693),
399         .proto_supported = (1 << RFID_PROTOCOL_TCL) |
400                         (1 << RFID_PROTOCOL_MIFARE_UL) |
401                         (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
402         .iso14443a = {
403                 .init = &openpcd_14443a_init,
404                 .transceive_sf = &openpcd_transceive_sf,
405                 .transceive_acf = &openpcd_transceive_acf,
406                 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
407                          RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
408                 .set_speed = &openpcd_14443a_set_speed,
409         },
410         .iso14443b = {
411                 .init = &openpcd_14443b_init,
412         },
413         .mifare_classic = {
414                 .setkey = &openpcd_mifare_setkey,
415                 .auth = &openpcd_mifare_auth,
416         },
417 };