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);
264 spidev_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t * key)
266 return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
270 spidev_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
271 u_int32_t serno, u_int8_t block)
273 return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah,
278 spidev_rf_power(struct rfid_reader_handle *rh, int on)
280 return rh->ah->asic->priv.rc632.fn.rf_power(rh->ah, on);
283 static struct rfid_reader_handle *spidev_open(void *data)
285 struct rfid_reader_handle *rh;
286 struct rfid_asic_transport_handle *rath;
289 /* open spi device */
291 DEBUGP("No device name\n");
294 if ((spidev_fd = open(data, O_RDWR)) < 0) {
295 DEBUGP("Unable to open:\n");
299 rh = malloc(sizeof(*rh));
303 memset(rh, 0, sizeof(*rh));
305 rath = malloc(sizeof(*rath));
308 memset(rath, 0, sizeof(*rath));
310 rath->rat = &spidev_spi;
311 rh->reader = &rfid_reader_spidev;
313 /* Configure spi device, MODE 0 */
315 if (ioctl(spidev_fd, SPI_IOC_WR_MODE, &tmp) < 0)
320 if (ioctl(spidev_fd, SPI_IOC_WR_LSB_FIRST, &tmp) < 0)
323 /* 8 bits per word */
325 if (ioctl(spidev_fd, SPI_IOC_WR_BITS_PER_WORD, &tmp) < 0)
330 if (ioctl(spidev_fd, SPI_IOC_WR_MAX_SPEED_HZ, &tmp) < 0)
334 rh->ah = rc632_open(rath);
338 /* everything is ok, returning reader handler */
349 static void spidev_close(struct rfid_reader_handle *rh)
351 struct rfid_asic_transport_handle *rath = rh->ah->rath;
366 struct rfid_reader rfid_reader_spidev = {
367 .name = "spidev reader",
368 .id = RFID_READER_SPIDEV,
369 .open = &spidev_open,
370 .close = &spidev_close,
371 .rf_power = &spidev_rf_power,
372 .transceive = &spidev_transceive,
373 .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
374 (1 << RFID_LAYER2_ISO14443B) |
375 (1 << RFID_LAYER2_ISO15693),
376 .proto_supported = (1 << RFID_PROTOCOL_TCL) |
377 (1 << RFID_PROTOCOL_MIFARE_UL) |
378 (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
380 .init = &spidev_14443a_init,
381 .transceive_sf = &spidev_transceive_sf,
382 .transceive_acf = &spidev_transceive_acf,
383 .speed = RFID_14443A_SPEED_106K
384 | RFID_14443A_SPEED_212K | RFID_14443A_SPEED_424K,
385 .set_speed = &spidev_14443a_set_speed,
388 .init = &spidev_14443b_init,
391 .setkey = &spidev_mifare_setkey,
392 .auth = &spidev_mifare_auth,