1 /* Direct spi for RC632 transport layer
2 * (based on openpcd reader)
4 * (C) 2007 by Frederic RODO <f.rodo@til-technologies.fr>
6 * This reader use the Linux's spidev interface, so it need a least
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include <sys/ioctl.h>
32 #include <linux/types.h>
33 #include <linux/spi/spidev.h>
35 #include <librfid/rfid.h>
36 #include <librfid/rfid_reader.h>
37 #include <librfid/rfid_asic.h>
38 #include <librfid/rfid_asic_rc632.h>
39 #include <librfid/rfid_reader_spidev.h>
40 #include <librfid/rfid_layer2.h>
41 #include <librfid/rfid_protocol.h>
47 struct spi_ioc_transfer xfer[1];
49 /* 256bytes max FSD/FSC, plus 1 bytes header, plus 10 bytes reserve */
50 #define SENDBUF_LEN (256+1+10)
51 #define RECVBUF_LEN SENDBUF_LEN
52 static char snd_buf[SENDBUF_LEN];
53 static char rcv_buf[RECVBUF_LEN];
55 static int spidev_read(unsigned char reg, unsigned char len,
63 snd_buf[0] = (reg<<1) | 0x80;
65 memset(&snd_buf[1], reg<<1 , len-1);
68 /* prepare spi buffer */
69 xfer[0].tx_buf = (__u64) snd_buf;
70 xfer[0].rx_buf = (__u64) rcv_buf;
71 xfer[0].len = len + 1;
73 ret = ioctl(spidev_fd, SPI_IOC_MESSAGE(1), xfer);
75 DEBUGPC("ERROR sending command\n");
77 } else if (ret != (len + 1)) {
78 DEBUGPC("ERROR sending command bad length\n");
82 memcpy(buf, &rcv_buf[1], len);
87 static int spidev_write(unsigned char reg, unsigned char len,
88 const unsigned char *buf)
95 snd_buf[0] = (reg << 1) & 0x7E;
96 memcpy(&snd_buf[1], buf, len);
98 /* prepare spi buffer */
99 xfer[0].tx_buf = (__u64) snd_buf;
100 xfer[0].rx_buf = (__u64) NULL;
101 xfer[0].len = len + 1;
103 ret = ioctl(spidev_fd, SPI_IOC_MESSAGE(1), xfer);
105 DEBUGPC("ERROR sending command\n");
108 else if (ret != len+1)
114 static int spidev_reg_read(struct rfid_asic_transport_handle *rath,
115 unsigned char reg, unsigned char *value)
119 ret = spidev_read(reg, 1, value);
122 DEBUGP("%s reg = 0x%02x, val = 0x%02x\n", __FUNCTION__, reg, *value);
127 static int spidev_reg_write(struct rfid_asic_transport_handle *rath,
128 unsigned char reg, unsigned char value)
132 ret = spidev_write(reg, 1, &value);
136 DEBUGP("%s reg = 0x%02x, val = 0x%02x\n", __FUNCTION__, reg, value);
141 static int spidev_fifo_read(struct rfid_asic_transport_handle *rath,
142 unsigned char len, unsigned char *buf)
146 ret = spidev_read(2, len, buf);
150 DEBUGP("%s len=%u, val=%s\n", __FUNCTION__, len,
151 rfid_hexdump(buf, len));
156 static int spidev_fifo_write(struct rfid_asic_transport_handle *rath,
157 unsigned char len, const unsigned char *buf,
162 ret = spidev_write(2, len, buf);
166 DEBUGP("%s len=%u, data=%s\n", __FUNCTION__, len,
167 rfid_hexdump(buf, len));
172 struct rfid_asic_transport spidev_spi = {
176 .reg_write = &spidev_reg_write,
177 .reg_read = &spidev_reg_read,
178 .fifo_write = &spidev_fifo_write,
179 .fifo_read = &spidev_fifo_read,
184 static int spidev_transceive(struct rfid_reader_handle *rh,
185 enum rfid_frametype frametype,
186 const unsigned char *tx_data,
187 unsigned int tx_len, unsigned char *rx_data,
188 unsigned int *rx_len, u_int64_t timeout,
191 return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
192 tx_data, tx_len, rx_data,
193 rx_len, timeout, flags);
196 static int spidev_transceive_sf(struct rfid_reader_handle *rh,
198 struct iso14443a_atqa *atqa)
200 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah, cmd,
205 spidev_transceive_acf(struct rfid_reader_handle *rh,
206 struct iso14443a_anticol_cmd *cmd,
207 unsigned int *bit_of_col)
209 return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
214 static int spidev_14443a_init(struct rfid_reader_handle *rh)
217 ret = rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
222 spidev_14443a_set_speed(struct rfid_reader_handle *rh,
223 unsigned int tx, unsigned int speed)
227 DEBUGP("setting rate: ");
229 case RFID_14443A_SPEED_106K:
233 case RFID_14443A_SPEED_212K:
237 case RFID_14443A_SPEED_424K:
241 case RFID_14443A_SPEED_848K:
249 return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
253 static int spidev_14443b_init(struct rfid_reader_handle *rh)
255 return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
258 static int spidev_15693_init(struct rfid_reader_handle *rh)
260 return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
263 static int spidev_15693_transceive_ac(struct rfid_reader_handle *rh,
264 struct iso15693_anticol_cmd *acf,
265 unsigned char uuid[ISO15693_UID_LEN],
268 return rh->ah->asic->priv.rc632.fn.iso15693.transceive_ac(
269 rh->ah, acf, uuid, bit_of_col);
273 spidev_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t * key)
275 return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
279 spidev_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
280 u_int32_t serno, u_int8_t block)
282 return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah,
287 spidev_rf_power(struct rfid_reader_handle *rh, int on)
289 return rh->ah->asic->priv.rc632.fn.rf_power(rh->ah, on);
292 static struct rfid_reader_handle *spidev_open(void *data)
294 struct rfid_reader_handle *rh;
295 struct rfid_asic_transport_handle *rath;
298 /* open spi device */
300 DEBUGP("No device name\n");
303 if ((spidev_fd = open(data, O_RDWR)) < 0) {
304 DEBUGP("Unable to open:\n");
308 rh = malloc(sizeof(*rh));
312 memset(rh, 0, sizeof(*rh));
314 rath = malloc(sizeof(*rath));
317 memset(rath, 0, sizeof(*rath));
319 rath->rat = &spidev_spi;
320 rh->reader = &rfid_reader_spidev;
322 /* Configure spi device, MODE 0 */
324 if (ioctl(spidev_fd, SPI_IOC_WR_MODE, &tmp) < 0)
329 if (ioctl(spidev_fd, SPI_IOC_WR_LSB_FIRST, &tmp) < 0)
332 /* 8 bits per word */
334 if (ioctl(spidev_fd, SPI_IOC_WR_BITS_PER_WORD, &tmp) < 0)
339 if (ioctl(spidev_fd, SPI_IOC_WR_MAX_SPEED_HZ, &tmp) < 0)
343 rh->ah = rc632_open(rath);
347 /* everything is ok, returning reader handler */
358 static void spidev_close(struct rfid_reader_handle *rh)
360 struct rfid_asic_transport_handle *rath = rh->ah->rath;
375 struct rfid_reader rfid_reader_spidev = {
376 .name = "spidev reader",
377 .id = RFID_READER_SPIDEV,
378 .open = &spidev_open,
379 .close = &spidev_close,
380 .rf_power = &spidev_rf_power,
381 .transceive = &spidev_transceive,
382 .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
383 (1 << RFID_LAYER2_ISO14443B) |
384 (1 << RFID_LAYER2_ISO15693),
385 .proto_supported = (1 << RFID_PROTOCOL_TCL) |
386 (1 << RFID_PROTOCOL_MIFARE_UL) |
387 (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
389 .init = &spidev_14443a_init,
390 .transceive_sf = &spidev_transceive_sf,
391 .transceive_acf = &spidev_transceive_acf,
392 .speed = RFID_14443A_SPEED_106K
393 | RFID_14443A_SPEED_212K | RFID_14443A_SPEED_424K,
394 .set_speed = &spidev_14443a_set_speed,
397 .init = &spidev_14443b_init,
400 .init = &spidev_15693_init,
401 .transceive_ac = &spidev_15693_transceive_ac,
404 .setkey = &spidev_mifare_setkey,
405 .auth = &spidev_mifare_auth,