1 /* OpenPCD specific RC632 transport layer
3 * (C) 2006 by Harald Welte <laforge@gnumonks.org>
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.
10 * - put hdl from static variable into asic transport or reader handle
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
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.
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
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>
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
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;
57 #ifndef LIBRFID_FIRMWARE
60 #include "libusb_dyn.h"
65 static struct usb_device *dev;
66 static struct usb_dev_handle *hdl;
68 static int openpcd_send_command(u_int8_t cmd, u_int8_t reg, u_int8_t val,
69 u_int16_t len, const unsigned char *data)
77 snd_hdr->flags = OPENPCD_FLAG_RESPOND;
79 memcpy(snd_hdr->data, data, len);
81 cur = sizeof(*snd_hdr) + len;
83 return usb_bulk_write(hdl, OPENPCD_OUT_EP, (char *)snd_hdr, cur, 0);
86 static int openpcd_recv_reply(void)
90 ret = usb_bulk_read(hdl, OPENPCD_IN_EP, rcv_buf, sizeof(rcv_buf), 1000);
95 static int openpcd_xcv(u_int8_t cmd, u_int8_t reg, u_int8_t val,
96 u_int16_t len, const unsigned char *data)
100 ret = openpcd_send_command(cmd, reg, val, len, data);
103 if (ret < sizeof(struct openpcd_hdr))
106 return openpcd_recv_reply();
114 static const struct usb_id opcd_usb_ids[] = {
115 { .vid = 0x2342, .pid = 0x0001 }, /* prototypes */
116 { .vid = 0x16c0, .pid = 0x076b }, /* first official device id */
119 static struct usb_device *find_opcd_device(void)
123 for (bus = usb_busses; bus; bus = bus->next) {
124 struct usb_device *dev;
125 for (dev = bus->devices; dev; dev = dev->next) {
127 for (i = 0; i < ARRAY_SIZE(opcd_usb_ids); i++) {
128 const struct usb_id *id = &opcd_usb_ids[i];
129 if (dev->descriptor.idVendor == id->vid &&
130 dev->descriptor.idProduct == id->pid)
138 /* RC632 access primitives for librfid inside reader firmware */
140 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
141 unsigned char reg, unsigned char value)
145 DEBUGP("reg=0x%02x, val=%02x: ", reg, value);
147 ret = openpcd_xcv(OPENPCD_CMD_WRITE_REG, reg, value, 0, NULL);
149 DEBUGPC("ERROR sending command\n");
156 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
158 unsigned char *value)
162 DEBUGP("reg=0x%02x, ", reg);
164 ret = openpcd_xcv(OPENPCD_CMD_READ_REG, reg, 0, 0, NULL);
166 DEBUGPC("ERROR sending command\n");
170 if (ret < sizeof(struct openpcd_hdr)) {
171 DEBUGPC("ERROR: short packet\n");
175 *value = rcv_hdr->val;
176 DEBUGPC("val=%02x: OK\n", *value);
181 static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
182 unsigned char num_bytes,
189 ret = openpcd_xcv(OPENPCD_CMD_READ_FIFO, 0x00, num_bytes, 0, NULL);
191 DEBUGPC("ERROR sending command\n");
194 DEBUGPC("ret = %d\n", ret);
196 memcpy(buf, rcv_hdr->data, ret - sizeof(struct openpcd_hdr));
197 DEBUGPC("len=%d val=%s: OK\n", ret - sizeof(struct openpcd_hdr),
198 rfid_hexdump(rcv_hdr->data, ret - sizeof(struct openpcd_hdr)));
203 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
205 const unsigned char *bytes,
210 DEBUGP("len=%u, data=%s\n", len, rfid_hexdump(bytes, len));
211 ret = openpcd_xcv(OPENPCD_CMD_WRITE_FIFO, 0, 0, len, bytes);
216 const struct rfid_asic_transport openpcd_rat = {
217 .name = "OpenPCD Dumb USB Protocol",
220 .reg_write = &openpcd_reg_write,
221 .reg_read = &openpcd_reg_read,
222 .fifo_write = &openpcd_fifo_write,
223 .fifo_read = &openpcd_fifo_read,
229 /* RC632 access primitives for librfid inside reader firmware */
231 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
232 unsigned char reg, unsigned char value)
234 return opcd_rc632_reg_write(rath, reg, value);
237 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
239 unsigned char *value)
241 return opcd_rc632_reg_read(rath, reg, value);
245 static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
246 unsigned char num_bytes,
249 return opcd_rc632_fifo_read(rath, num_bytes, buf);
252 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
254 const unsigned char *bytes,
257 return opcd_rc632_fifo_write(rath, len, bytes, flags);
260 const struct rfid_asic_transport openpcd_rat = {
261 .name = "OpenPCD Firmware RC632 Access",
264 .reg_write = &openpcd_reg_write,
265 .reg_read = &openpcd_reg_read,
266 .fifo_write = &openpcd_fifo_write,
267 .fifo_read = &openpcd_fifo_read,
272 #endif /* LIBRFID_FIRMWARE */
274 static int openpcd_transceive(struct rfid_reader_handle *rh,
275 enum rfid_frametype frametype,
276 const unsigned char *tx_data, unsigned int tx_len,
277 unsigned char *rx_data, unsigned int *rx_len,
278 u_int64_t timeout, unsigned int flags)
280 return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
286 static int openpcd_transceive_sf(struct rfid_reader_handle *rh,
287 unsigned char cmd, struct iso14443a_atqa *atqa)
289 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
295 openpcd_transceive_acf(struct rfid_reader_handle *rh,
296 struct iso14443a_anticol_cmd *cmd,
297 unsigned int *bit_of_col)
299 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
304 openpcd_14443a_init(struct rfid_reader_handle *rh)
306 return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
310 openpcd_14443a_set_speed(struct rfid_reader_handle *rh,
316 DEBUGP("setting rate: ");
318 case RFID_14443A_SPEED_106K:
322 case RFID_14443A_SPEED_212K:
326 case RFID_14443A_SPEED_424K:
330 case RFID_14443A_SPEED_848K:
338 return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
343 openpcd_14443b_init(struct rfid_reader_handle *rh)
345 return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
349 openpcd_15693_init(struct rfid_reader_handle *rh)
351 return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
355 openpcd_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
357 return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
361 openpcd_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
362 u_int32_t serno, u_int8_t block)
364 return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah,
368 static struct rfid_reader_handle *
369 openpcd_open(void *data)
371 struct rfid_reader_handle *rh;
372 struct rfid_asic_transport_handle *rath;
374 snd_hdr = (struct openpcd_hdr *)snd_buf;
375 rcv_hdr = (struct openpcd_hdr *)rcv_buf;
377 #ifndef LIBRFID_FIRMWARE
379 if (usb_find_busses() < 0)
381 if (usb_find_devices() < 0)
384 dev = find_opcd_device();
386 DEBUGP("No matching USB device found\n");
392 DEBUGP("Can't open USB device\n");
396 if (usb_claim_interface(hdl, 0) < 0) {
397 DEBUGP("Can't claim interface\n");
403 rh = malloc_reader_handle(sizeof(*rh));
406 memset(rh, 0, sizeof(*rh));
408 rath = malloc_rat_handle(sizeof(*rath));
411 memset(rath, 0, sizeof(*rath));
413 rath->rat = &openpcd_rat;
414 rh->reader = &rfid_reader_openpcd;
416 rh->ah = rc632_open(rath);
420 DEBUGP("returning %p\n", rh);
424 free_rat_handle(rath);
426 free_reader_handle(rh);
432 openpcd_close(struct rfid_reader_handle *rh)
434 struct rfid_asic_transport_handle *rath = rh->ah->rath;
437 free_rat_handle(rath);
438 free_reader_handle(rh);
440 #ifndef LIBRFID_FIRMWARE
445 const struct rfid_reader rfid_reader_openpcd = {
446 .name = "OpenPCD RFID Reader",
447 .id = RFID_READER_OPENPCD,
448 .open = &openpcd_open,
449 .close = &openpcd_close,
450 .transceive = &openpcd_transceive,
451 .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
452 (1 << RFID_LAYER2_ISO14443B) |
453 (1 << RFID_LAYER2_ISO15693),
454 .proto_supported = (1 << RFID_PROTOCOL_TCL) |
455 (1 << RFID_PROTOCOL_MIFARE_UL) |
456 (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
458 .init = &openpcd_14443a_init,
459 .transceive_sf = &openpcd_transceive_sf,
460 .transceive_acf = &openpcd_transceive_acf,
461 .speed = RFID_14443A_SPEED_106K | RFID_14443A_SPEED_212K |
462 RFID_14443A_SPEED_424K, //| RFID_14443A_SPEED_848K,
463 .set_speed = &openpcd_14443a_set_speed,
466 .init = &openpcd_14443b_init,
469 .init = &openpcd_15693_init,
472 .setkey = &openpcd_mifare_setkey,
473 .auth = &openpcd_mifare_auth,