Fix typo. we don't wand sizeof(sizeof(somethin)) (Rainer Keller <mail@rainerkeller...
[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
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 #include <librfid/rfid_layer2.h>
42 #include <librfid/rfid_protocol.h>
43
44 /* FIXME */
45 #include "rc632.h"
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 #ifndef LIBRFID_FIRMWARE
58
59 #include <usb.h>
60 static struct usb_device *dev;
61 static struct usb_dev_handle *hdl;
62
63 static int openpcd_send_command(u_int8_t cmd, u_int8_t reg, u_int8_t val,
64                                 u_int16_t len, const unsigned char *data)
65 {
66         int ret;
67         u_int16_t cur;
68
69         snd_hdr->cmd = cmd;
70         snd_hdr->reg = reg;
71         snd_hdr->val = val;
72         snd_hdr->flags = OPENPCD_FLAG_RESPOND;
73         if (data && len)
74                 memcpy(snd_hdr->data, data, len);
75
76         cur = sizeof(*snd_hdr) + len;
77
78         return usb_bulk_write(hdl, OPENPCD_OUT_EP, (char *)snd_hdr, cur, 0);
79 }
80
81 static int openpcd_recv_reply(void)
82 {
83         int ret;
84
85         ret = usb_bulk_read(hdl, OPENPCD_IN_EP, rcv_buf, sizeof(rcv_buf), 1000);
86
87         return ret;
88 }
89
90 static int openpcd_xcv(u_int8_t cmd, u_int8_t reg, u_int8_t val,
91                         u_int16_t len, const unsigned char *data)
92 {
93         int ret;
94         
95         ret = openpcd_send_command(cmd, reg, val, len, data);
96         if (ret < 0)
97                 return ret;
98         if (ret < sizeof(struct openpcd_hdr))
99                 return -EINVAL;
100
101         return openpcd_recv_reply();
102 }
103
104 struct usb_id {
105         u_int16_t vid;
106         u_int16_t pid;
107 };
108
109 static const struct usb_id opcd_usb_ids[] = {
110         { .vid = 0x2342, .pid = 0x0001 },       /* prototypes */
111         { .vid = 0x16c0, .pid = 0x076b },       /* first official device id */
112 };
113
114 static struct usb_device *find_opcd_device(void)
115 {
116         struct usb_bus *bus;
117
118         for (bus = usb_busses; bus; bus = bus->next) {
119                 struct usb_device *dev;
120                 for (dev = bus->devices; dev; dev = dev->next) {
121                         int i;
122                         for (i = 0; i < ARRAY_SIZE(opcd_usb_ids); i++) {
123                                 const struct usb_id *id = &opcd_usb_ids[i];
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 /* RC632 access primitives for librfid inside reader firmware */
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 const struct rfid_asic_transport openpcd_rat = {
212         .name = "OpenPCD Dumb USB Protocol",
213         .priv.rc632 = {
214                 .fn = {
215                         .reg_write      = &openpcd_reg_write,
216                         .reg_read       = &openpcd_reg_read,
217                         .fifo_write     = &openpcd_fifo_write,
218                         .fifo_read      = &openpcd_fifo_read,
219                 },
220         },
221 };
222
223 #else
224 /* RC632 access primitives for librfid inside reader firmware */
225
226 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
227                              unsigned char reg, unsigned char value)
228 {
229         return opcd_rc632_reg_write(rath, reg, value);
230 }
231
232 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
233                             unsigned char reg,
234                             unsigned char *value)
235 {
236         return opcd_rc632_reg_read(rath, reg, value);
237 }
238
239
240 static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
241                              unsigned char num_bytes,
242                              unsigned char *buf)
243 {
244         return opcd_rc632_fifo_read(rath, num_bytes, buf);
245 }
246
247 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
248                              unsigned char len,
249                              const unsigned char *bytes,
250                              unsigned char flags)
251 {
252         return opcd_rc632_fifo_write(rath, len, bytes, flags);
253 }
254
255 const struct rfid_asic_transport openpcd_rat = {
256         .name = "OpenPCD Firmware RC632 Access",
257         .priv.rc632 = {
258                 .fn = {
259                         .reg_write      = &openpcd_reg_write,
260                         .reg_read       = &openpcd_reg_read,
261                         .fifo_write     = &openpcd_fifo_write,
262                         .fifo_read      = &openpcd_fifo_read,
263                 },
264         },
265 };
266
267 #endif /* LIBRFID_FIRMWARE */
268
269 static int openpcd_transceive(struct rfid_reader_handle *rh,
270                              enum rfid_frametype frametype,
271                              const unsigned char *tx_data, unsigned int tx_len,
272                              unsigned char *rx_data, unsigned int *rx_len,
273                              u_int64_t timeout, unsigned int flags)
274 {
275         return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
276                                                       tx_data, tx_len, 
277                                                       rx_data, rx_len,
278                                                       timeout, flags);
279 }
280
281 static int openpcd_transceive_sf(struct rfid_reader_handle *rh,
282                                unsigned char cmd, struct iso14443a_atqa *atqa)
283 {
284         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
285                                                                    cmd,
286                                                                    atqa);
287 }
288
289 static int
290 openpcd_transceive_acf(struct rfid_reader_handle *rh,
291                       struct iso14443a_anticol_cmd *cmd,
292                       unsigned int *bit_of_col)
293 {
294         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
295                                                          cmd, bit_of_col);
296 }
297
298 static int
299 openpcd_14443a_init(struct rfid_reader_handle *rh)
300 {
301         return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
302 }
303
304 static int
305 openpcd_14443a_set_speed(struct rfid_reader_handle *rh, 
306                         unsigned int tx,
307                         unsigned int speed)
308 {
309         u_int8_t rate;
310         
311         DEBUGP("setting rate: ");
312         switch (speed) {
313         case RFID_14443A_SPEED_106K:
314                 rate = 0x00;
315                 DEBUGPC("106K\n");
316                 break;
317         case RFID_14443A_SPEED_212K:
318                 rate = 0x01;
319                 DEBUGPC("212K\n");
320                 break;
321         case RFID_14443A_SPEED_424K:
322                 rate = 0x02;
323                 DEBUGPC("424K\n");
324                 break;
325         case RFID_14443A_SPEED_848K:
326                 rate = 0x03;
327                 DEBUGPC("848K\n");
328                 break;
329         default:
330                 return -EINVAL;
331                 break;
332         }
333         return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
334                                                                 tx, rate);
335 }
336
337 static int
338 openpcd_14443b_init(struct rfid_reader_handle *rh)
339 {
340         return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
341 }
342
343 static int
344 openpcd_15693_init(struct rfid_reader_handle *rh)
345 {
346         return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
347 }
348
349 static int
350 openpcd_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
351 {
352         return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
353 }
354
355 static int
356 openpcd_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd, 
357                    u_int32_t serno, u_int8_t block)
358 {
359         return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah, 
360                                                         cmd, serno, block);
361 }
362
363 static struct rfid_reader_handle *
364 openpcd_open(void *data)
365 {
366         struct rfid_reader_handle *rh;
367         struct rfid_asic_transport_handle *rath;
368
369         snd_hdr = (struct openpcd_hdr *)snd_buf;
370         rcv_hdr = (struct openpcd_hdr *)rcv_buf;
371
372 #ifndef LIBRFID_FIRMWARE
373         usb_init();
374         if (usb_find_busses() < 0)
375                 return NULL;
376         if (usb_find_devices() < 0) 
377                 return NULL;
378         
379         dev = find_opcd_device();
380         if (!dev) {
381                 DEBUGP("No matching USB device found\n");
382                 return NULL;
383         }
384
385         hdl = usb_open(dev);
386         if (!hdl) {
387                 DEBUGP("Can't open USB device\n");
388                 return NULL;
389         }
390
391         if (usb_claim_interface(hdl, 0) < 0) {
392                 DEBUGP("Can't claim interface\n");
393                 usb_close(hdl);
394                 return NULL;
395         }
396 #endif
397
398         rh = malloc_reader_handle(sizeof(*rh));
399         if (!rh)
400                 return NULL;
401         memset(rh, 0, sizeof(*rh));
402
403         rath = malloc_rat_handle(sizeof(*rath));
404         if (!rath)
405                 goto out_rh;
406         memset(rath, 0, sizeof(*rath));
407
408         rath->rat = &openpcd_rat;
409         rh->reader = &rfid_reader_openpcd;
410
411         rh->ah = rc632_open(rath);
412         if (!rh->ah) 
413                 goto out_rath;
414
415         DEBUGP("returning %p\n", rh);
416         return rh;
417
418 out_rath:
419         free_rat_handle(rath);
420 out_rh:
421         free_reader_handle(rh);
422
423         return NULL;
424 }
425
426 static void
427 openpcd_close(struct rfid_reader_handle *rh)
428 {
429         struct rfid_asic_transport_handle *rath = rh->ah->rath;
430
431         rc632_close(rh->ah);
432         free_rat_handle(rath);
433         free_reader_handle(rh);
434
435 #ifndef LIBRFID_FIRMWARE
436         usb_close(hdl);
437 #endif
438 }
439
440 const struct rfid_reader rfid_reader_openpcd = {
441         .name   = "OpenPCD RFID Reader",
442         .id = RFID_READER_OPENPCD,
443         .open = &openpcd_open,
444         .close = &openpcd_close,
445         .transceive = &openpcd_transceive,
446         .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
447                         (1 << RFID_LAYER2_ISO14443B) |
448                         (1 << RFID_LAYER2_ISO15693),
449         .proto_supported = (1 << RFID_PROTOCOL_TCL) |
450                         (1 << RFID_PROTOCOL_MIFARE_UL) |
451                         (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
452         .iso14443a = {
453                 .init = &openpcd_14443a_init,
454                 .transceive_sf = &openpcd_transceive_sf,
455                 .transceive_acf = &openpcd_transceive_acf,
456                 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
457                          RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
458                 .set_speed = &openpcd_14443a_set_speed,
459         },
460         .iso14443b = {
461                 .init = &openpcd_14443b_init,
462         },
463         .iso15693 = {
464                 .init = &openpcd_15693_init,
465         },
466         .mifare_classic = {
467                 .setkey = &openpcd_mifare_setkey,
468                 .auth = &openpcd_mifare_auth,
469         },
470 };