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.
9 * - put hdl from static variable into asic transport or reader handle
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
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.
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
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>
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
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;
56 static struct usb_device *dev;
57 static struct usb_dev_handle *hdl;
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)
68 snd_hdr->flags = OPENPCD_FLAG_RESPOND;
70 memcpy(snd_hdr->data, data, len);
72 cur = sizeof(*snd_hdr) + len;
74 return usb_bulk_write(hdl, OPENPCD_OUT_EP, snd_hdr, cur, 0);
77 static int openpcd_recv_reply(void)
81 ret = usb_bulk_read(hdl, OPENPCD_IN_EP, rcv_buf, sizeof(rcv_buf), 1000);
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)
91 ret = openpcd_send_command(cmd, reg, val, len, data);
94 if (ret < sizeof(sizeof(struct openpcd_hdr)))
97 return openpcd_recv_reply();
100 static struct usb_device *find_opcd_device(void)
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)
119 static int openpcd_reg_write(struct rfid_asic_transport_handle *rath,
120 unsigned char reg, unsigned char value)
124 DEBUGP("reg=0x%02x, val=%02x: ", reg, value);
126 ret = openpcd_xcv(OPENPCD_CMD_WRITE_REG, reg, value, 0, NULL);
128 DEBUGPC("ERROR sending command\n");
135 static int openpcd_reg_read(struct rfid_asic_transport_handle *rath,
137 unsigned char *value)
141 DEBUGP("reg=0x%02x, ", reg);
143 ret = openpcd_xcv(OPENPCD_CMD_READ_REG, reg, 0, 0, NULL);
145 DEBUGPC("ERROR sending command\n");
149 if (ret < sizeof(struct openpcd_hdr)) {
150 DEBUGPC("ERROR: short packet\n");
154 *value = rcv_hdr->val;
155 DEBUGPC("val=%02x: OK\n", *value);
160 static int openpcd_fifo_read(struct rfid_asic_transport_handle *rath,
161 unsigned char num_bytes,
168 ret = openpcd_xcv(OPENPCD_CMD_READ_FIFO, 0x00, num_bytes, 0, NULL);
170 DEBUGPC("ERROR sending command\n");
173 DEBUGPC("ret = %d\n", ret);
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)));
182 static int openpcd_fifo_write(struct rfid_asic_transport_handle *rath,
184 const unsigned char *bytes,
189 DEBUGP("len=%u, data=%s\n", len, rfid_hexdump(bytes, len));
190 ret = openpcd_xcv(OPENPCD_CMD_WRITE_FIFO, 0, 0, len, bytes);
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)
201 return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
202 tx_data, tx_len, rx_data,
203 rx_len, timeout, flags);
206 static int openpcd_transceive_sf(struct rfid_reader_handle *rh,
207 unsigned char cmd, struct iso14443a_atqa *atqa)
209 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah,
215 openpcd_transceive_acf(struct rfid_reader_handle *rh,
216 struct iso14443a_anticol_cmd *cmd,
217 unsigned int *bit_of_col)
219 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
224 openpcd_14443a_init(struct rfid_reader_handle *rh)
226 return rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
230 openpcd_14443a_set_speed(struct rfid_reader_handle *rh,
236 DEBUGP("setting rate: ");
238 case RFID_14443A_SPEED_106K:
242 case RFID_14443A_SPEED_212K:
246 case RFID_14443A_SPEED_424K:
250 case RFID_14443A_SPEED_848K:
258 return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
263 openpcd_14443b_init(struct rfid_reader_handle *rh)
265 return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
269 openpcd_15693_init(struct rfid_reader_handle *rh)
271 return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
275 openpcd_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t *key)
277 return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
281 openpcd_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
282 u_int32_t serno, u_int8_t block)
284 return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah,
288 struct rfid_asic_transport openpcd_ccid = {
289 .name = "OpenPCD Dumb USB Protocol",
292 .reg_write = &openpcd_reg_write,
293 .reg_read = &openpcd_reg_read,
294 .fifo_write = &openpcd_fifo_write,
295 .fifo_read = &openpcd_fifo_read,
300 static struct rfid_reader_handle *
301 openpcd_open(void *data)
303 struct rfid_reader_handle *rh;
304 struct rfid_asic_transport_handle *rath;
306 snd_hdr = (struct openpcd_hdr *)snd_buf;
307 rcv_hdr = (struct openpcd_hdr *)rcv_buf;
310 if (!usb_find_busses())
312 if (!usb_find_devices())
315 dev = find_opcd_device();
317 DEBUGP("No matching USB device found\n");
323 DEBUGP("Can't open USB device\n");
327 if (usb_claim_interface(hdl, 0) < 0) {
328 DEBUGP("Can't claim interface\n");
333 rh = malloc(sizeof(*rh));
336 memset(rh, 0, sizeof(*rh));
338 rath = malloc(sizeof(*rath));
341 memset(rath, 0, sizeof(*rath));
343 rath->rat = &openpcd_ccid;
344 rh->reader = &rfid_reader_openpcd;
346 rh->ah = rc632_open(rath);
350 DEBUGP("returning %p\n", rh);
362 openpcd_close(struct rfid_reader_handle *rh)
364 struct rfid_asic_transport_handle *rath = rh->ah->rath;
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,
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,
388 .init = &openpcd_14443b_init,
391 .setkey = &openpcd_mifare_setkey,
392 .auth = &openpcd_mifare_auth,