update to new (four-byte, explicit response_request flag) revision of openpcd usb...
[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.
7  *
8  * TODO:
9  * - put hdl from static variable into asic transport or reader handle 
10  */
11
12 /*
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License version 2 
15  *  as published by the Free Software Foundation
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 //#define DEBUG
28
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #include <usb.h>
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
42 /* FIXME */
43 #include "rc632.h"
44
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
56 static struct usb_device *dev;
57 static struct usb_dev_handle *hdl;
58
59 static int openpcd_send_command(u_int8_t cmd, u_int8_t reg, u_int8_t val,
60                                 u_int16_t len, const unsigned char *data)
61 {
62         int ret;
63         u_int16_t cur;
64
65         snd_hdr->cmd = cmd;
66         snd_hdr->reg = reg;
67         snd_hdr->val = val;
68         snd_hdr->flags = OPENPCD_FLAG_RESPOND;
69         if (data && len)
70                 memcpy(snd_hdr->data, data, len);
71
72         cur = sizeof(*snd_hdr) + len;
73
74         return usb_bulk_write(hdl, OPENPCD_OUT_EP, snd_hdr, cur, 0);
75 }
76
77 static int openpcd_recv_reply(void)
78 {
79         int ret;
80
81         ret = usb_bulk_read(hdl, OPENPCD_IN_EP, rcv_buf, sizeof(rcv_buf), 1000);
82
83         return ret;
84 }
85
86 static int openpcd_xcv(u_int8_t cmd, u_int8_t reg, u_int8_t val,
87                         u_int16_t len, const unsigned char *data)
88 {
89         int ret;
90         
91         ret = openpcd_send_command(cmd, reg, val, len, data);
92         if (ret < 0)
93                 return ret;
94         if (ret < sizeof(sizeof(struct openpcd_hdr)))
95                 return -EINVAL;
96
97         return openpcd_recv_reply();
98 }
99         
100 static struct usb_device *find_opcd_device(void)
101 {
102         struct usb_bus *bus;
103
104         for (bus = usb_busses; bus; bus = bus->next) {
105                 struct usb_device *dev;
106                 for (dev = bus->devices; dev; dev = dev->next) {
107                         if (dev->descriptor.idVendor == OPENPCD_VENDOR_ID
108                             && dev->descriptor.idProduct == OPENPCD_PRODUCT_ID
109                             && dev->descriptor.iManufacturer == 0
110                             && dev->descriptor.iProduct == 0
111                             && dev->descriptor.bNumConfigurations == 1
112                             && dev->config->iConfiguration == 0)
113                                 return dev;
114                 }
115         }
116         return NULL;
117 }
118
119 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
120                              unsigned char reg, unsigned char value)
121 {
122         int ret;
123
124         DEBUGP("reg=0x%02x, val=%02x: ", reg, value);
125
126         ret = openpcd_xcv(OPENPCD_CMD_WRITE_REG, reg, value, 0, NULL);
127         if (ret < 0)
128                 DEBUGPC("ERROR sending command\n");
129         else
130                 DEBUGPC("OK\n");
131
132         return ret;
133 }
134
135 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
136                             unsigned char reg,
137                             unsigned char *value)
138 {
139         int ret;        
140
141         DEBUGP("reg=0x%02x, ", reg);
142
143         ret = openpcd_xcv(OPENPCD_CMD_READ_REG, reg, 0, 0, NULL);
144         if (ret < 0) {
145                 DEBUGPC("ERROR sending command\n");
146                 return ret;
147         }
148
149         if (ret < sizeof(struct openpcd_hdr)) {
150                 DEBUGPC("ERROR: short packet\n");
151                 return ret;
152         }
153
154         *value = rcv_hdr->val;
155         DEBUGPC("val=%02x: OK\n", *value);
156
157         return ret;
158 }
159
160 static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
161                              unsigned char num_bytes,
162                              unsigned char *buf)
163 {
164         int ret;
165
166         DEBUGP(" ");
167
168         ret = openpcd_xcv(OPENPCD_CMD_READ_FIFO, 0x00, num_bytes, 0, NULL);
169         if (ret < 0) {
170                 DEBUGPC("ERROR sending command\n");
171                 return ret;
172         }
173         DEBUGPC("ret = %d\n", ret);
174
175         memcpy(buf, rcv_hdr->data, ret - sizeof(struct openpcd_hdr));
176         DEBUGPC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
177                 rfid_hexdump(rcv_hdr->data, ret - sizeof(struct openpcd_hdr)));
178
179         return ret;
180 }
181
182 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
183                              unsigned char len,
184                              const unsigned char *bytes,
185                              unsigned char flags)
186 {
187         int ret;
188
189         DEBUGP("len=%u, data=%s\n", len, rfid_hexdump(bytes, len));
190         ret = openpcd_xcv(OPENPCD_CMD_WRITE_FIFO, 0, 0, len, bytes);
191
192         return ret;
193 }
194
195 static int openpcd_transceive(struct rfid_reader_handle *rh,
196                              enum rfid_frametype frametype,
197                              const unsigned char *tx_data, unsigned int tx_len,
198                              unsigned char *rx_data, unsigned int *rx_len,
199                              u_int64_t timeout, unsigned int flags)
200 {
201         return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
202                                                 tx_data, tx_len, rx_data,
203                                                 rx_len, timeout, flags);
204 }
205
206 static int openpcd_transceive_sf(struct rfid_reader_handle *rh,
207                                unsigned char cmd, struct iso14443a_atqa *atqa)
208 {
209         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
210                                                                    cmd,
211                                                                    atqa);
212 }
213
214 static int
215 openpcd_transceive_acf(struct rfid_reader_handle *rh,
216                       struct iso14443a_anticol_cmd *cmd,
217                       unsigned int *bit_of_col)
218 {
219         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
220                                                          cmd, bit_of_col);
221 }
222
223 static int
224 openpcd_14443a_init(struct rfid_reader_handle *rh)
225 {
226         return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
227 }
228
229 static int
230 openpcd_14443a_set_speed(struct rfid_reader_handle *rh, 
231                         unsigned int tx,
232                         unsigned int speed)
233 {
234         u_int8_t rate;
235         
236         DEBUGP("setting rate: ");
237         switch (speed) {
238         case RFID_14443A_SPEED_106K:
239                 rate = 0x00;
240                 DEBUGPC("106K\n");
241                 break;
242         case RFID_14443A_SPEED_212K:
243                 rate = 0x01;
244                 DEBUGPC("212K\n");
245                 break;
246         case RFID_14443A_SPEED_424K:
247                 rate = 0x02;
248                 DEBUGPC("424K\n");
249                 break;
250         case RFID_14443A_SPEED_848K:
251                 rate = 0x03;
252                 DEBUGPC("848K\n");
253                 break;
254         default:
255                 return -EINVAL;
256                 break;
257         }
258         return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
259                                                                 tx, rate);
260 }
261
262 static int
263 openpcd_14443b_init(struct rfid_reader_handle *rh)
264 {
265         return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
266 }
267
268 static int
269 openpcd_15693_init(struct rfid_reader_handle *rh)
270 {
271         return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
272 }
273
274 static int
275 openpcd_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
276 {
277         return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
278 }
279
280 static int
281 openpcd_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
282                    u_int32_t serno, u_int8_t block)
283 {
284         return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah, 
285                                                         cmd, serno, block);
286 }
287
288 struct rfid_asic_transport openpcd_ccid = {
289         .name = "OpenPCD Dumb USB Protocol",
290         .priv.rc632 = {
291                 .fn = {
292                         .reg_write      = &openpcd_reg_write,
293                         .reg_read       = &openpcd_reg_read,
294                         .fifo_write     = &openpcd_fifo_write,
295                         .fifo_read      = &openpcd_fifo_read,
296                 },
297         },
298 };
299
300 static struct rfid_reader_handle *
301 openpcd_open(void *data)
302 {
303         struct rfid_reader_handle *rh;
304         struct rfid_asic_transport_handle *rath;
305
306         snd_hdr = (struct openpcd_hdr *)snd_buf;
307         rcv_hdr = (struct openpcd_hdr *)rcv_buf;
308
309         usb_init();
310         if (!usb_find_busses())
311                 return NULL;
312         if (!usb_find_devices())
313                 return NULL;
314         
315         dev = find_opcd_device();
316         if (!dev) {
317                 DEBUGP("No matching USB device found\n");
318                 return NULL;
319         }
320
321         hdl = usb_open(dev);
322         if (!hdl) {
323                 DEBUGP("Can't open USB device\n");
324                 return NULL;
325         }
326
327         if (usb_claim_interface(hdl, 0) < 0) {
328                 DEBUGP("Can't claim interface\n");
329                 usb_close(hdl);
330                 return NULL;
331         }
332
333         rh = malloc(sizeof(*rh));
334         if (!rh)
335                 return NULL;
336         memset(rh, 0, sizeof(*rh));
337
338         rath = malloc(sizeof(*rath));
339         if (!rath)
340                 goto out_rh;
341         memset(rath, 0, sizeof(*rath));
342
343         rath->rat = &openpcd_ccid;
344         rh->reader = &rfid_reader_openpcd;
345
346         rh->ah = rc632_open(rath);
347         if (!rh->ah) 
348                 goto out_rath;
349
350         DEBUGP("returning %p\n", rh);
351         return rh;
352
353 out_rath:
354         free(rath);
355 out_rh:
356         free(rh);
357
358         return NULL;
359 }
360
361 static void
362 openpcd_close(struct rfid_reader_handle *rh)
363 {
364         struct rfid_asic_transport_handle *rath = rh->ah->rath;
365
366         rc632_close(rh->ah);
367         free(rath);
368         free(rh);
369
370         usb_close(hdl);
371 }
372
373 struct rfid_reader rfid_reader_openpcd = {
374         .name   = "OpenPCD RFID Reader",
375         .id = RFID_READER_OPENPCD,
376         .open = &openpcd_open,
377         .close = &openpcd_close,
378         .transceive = &openpcd_transceive,
379         .iso14443a = {
380                 .init = &openpcd_14443a_init,
381                 .transceive_sf = &openpcd_transceive_sf,
382                 .transceive_acf = &openpcd_transceive_acf,
383                 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
384                          RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
385                 .set_speed = &openpcd_14443a_set_speed,
386         },
387         .iso14443b = {
388                 .init = &openpcd_14443b_init,
389         },
390         .mifare_classic = {
391                 .setkey = &openpcd_mifare_setkey,
392                 .auth = &openpcd_mifare_auth,
393         },
394 };