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