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