partial ISO15693 support (based on patch by Bjoern Kaiser)
[librfid] / src / rfid_reader_spidev.c
1 /* Direct spi for RC632 transport layer
2  * (based on openpcd reader)
3  *
4  * (C) 2007 by Frederic RODO <f.rodo@til-technologies.fr>
5  *
6  * This reader use the Linux's spidev interface, so it need a least
7  * kernel 2.6.22
8  */
9
10 /*
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
14  *
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.
19  *
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
23  */
24
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32 #include <linux/types.h>
33 #include <linux/spi/spidev.h>
34
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>
42
43 /* FIXME */
44 #include "rc632.h"
45 static int spidev_fd;
46
47 struct spi_ioc_transfer xfer[1];
48
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];
54
55 static int spidev_read(unsigned char reg, unsigned char len,
56                        unsigned char *buf)
57 {
58         int ret;
59
60         if (!len)
61                 return -EINVAL;
62
63         snd_buf[0] = (reg<<1) | 0x80;
64         if (len > 1)
65                 memset(&snd_buf[1], reg<<1 , len-1);
66         snd_buf[len] = 0;
67
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;
72
73         ret = ioctl(spidev_fd, SPI_IOC_MESSAGE(1), xfer);
74         if (ret < 0) {
75                 DEBUGPC("ERROR sending command\n");
76                 return ret;
77         } else if (ret != (len + 1)) {
78                 DEBUGPC("ERROR sending command bad length\n");
79                 return -EINVAL;
80         }
81
82         memcpy(buf, &rcv_buf[1], len);
83
84         return len;
85 }
86
87 static int spidev_write(unsigned char reg, unsigned char len,
88                        const unsigned char *buf)
89 {
90         int ret;
91
92         if (!len)
93                 return -EINVAL;
94
95         snd_buf[0] = (reg << 1) & 0x7E;
96         memcpy(&snd_buf[1], buf, len);
97
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;
102
103         ret = ioctl(spidev_fd, SPI_IOC_MESSAGE(1), xfer);
104         if (ret < 0) {
105                 DEBUGPC("ERROR sending command\n");
106                 return ret;
107         }
108         else if (ret != len+1)
109                 return -EINVAL;
110
111         return len;
112 }
113
114 static int spidev_reg_read(struct rfid_asic_transport_handle *rath,
115                            unsigned char reg, unsigned char *value)
116 {
117         int ret;
118
119         ret = spidev_read(reg, 1, value);
120         if (ret < 0)
121                 return ret;
122         DEBUGP("%s reg = 0x%02x, val = 0x%02x\n", __FUNCTION__, reg, *value);
123
124         return 1;
125 }
126
127 static int spidev_reg_write(struct rfid_asic_transport_handle *rath,
128                             unsigned char reg, unsigned char value)
129 {
130         int ret;
131
132         ret = spidev_write(reg, 1, &value);
133         if (ret < 0)
134                 return ret;
135
136         DEBUGP("%s reg = 0x%02x, val = 0x%02x\n", __FUNCTION__, reg, value);
137
138         return 1;
139 }
140
141 static int spidev_fifo_read(struct rfid_asic_transport_handle *rath,
142                             unsigned char len, unsigned char *buf)
143 {
144         int ret;
145
146         ret = spidev_read(2, len, buf);
147         if (ret < 0)
148                 return ret;
149
150         DEBUGP("%s len=%u, val=%s\n", __FUNCTION__, len,
151                rfid_hexdump(buf, len));
152
153         return len;
154 }
155
156 static int spidev_fifo_write(struct rfid_asic_transport_handle *rath,
157                              unsigned char len, const unsigned char *buf,
158                              unsigned char flags)
159 {
160         int ret;
161
162         ret = spidev_write(2, len, buf);
163         if (ret < 0)
164                 return ret;
165
166         DEBUGP("%s len=%u, data=%s\n", __FUNCTION__, len,
167                rfid_hexdump(buf, len));
168
169         return len;
170 }
171
172 struct rfid_asic_transport spidev_spi = {
173         .name = "spidev",
174         .priv.rc632 = {
175                        .fn = {
176                               .reg_write = &spidev_reg_write,
177                               .reg_read = &spidev_reg_read,
178                               .fifo_write = &spidev_fifo_write,
179                               .fifo_read = &spidev_fifo_read,
180                               },
181                        },
182 };
183
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,
189                                unsigned int flags)
190 {
191         return rh->ah->asic->priv.rc632.fn.transceive(rh->ah, frametype,
192                                                       tx_data, tx_len, rx_data,
193                                                       rx_len, timeout, flags);
194 }
195
196 static int spidev_transceive_sf(struct rfid_reader_handle *rh,
197                                   unsigned char cmd,
198                                   struct iso14443a_atqa *atqa)
199 {
200         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_sf(rh->ah, cmd,
201                                                                    atqa);
202 }
203
204 static int
205 spidev_transceive_acf(struct rfid_reader_handle *rh,
206                         struct iso14443a_anticol_cmd *cmd,
207                         unsigned int *bit_of_col)
208 {
209         return rh->ah->asic->priv.rc632.fn.iso14443a.transceive_acf(rh->ah,
210                                                                     cmd,
211                                                                     bit_of_col);
212 }
213
214 static int spidev_14443a_init(struct rfid_reader_handle *rh)
215 {
216         int ret;
217         ret = rh->ah->asic->priv.rc632.fn.iso14443a.init(rh->ah);
218         return ret;
219 }
220
221 static int
222 spidev_14443a_set_speed(struct rfid_reader_handle *rh,
223                           unsigned int tx, unsigned int speed)
224 {
225         u_int8_t rate;
226
227         DEBUGP("setting rate: ");
228         switch (speed) {
229             case RFID_14443A_SPEED_106K:
230                     rate = 0x00;
231                     DEBUGPC("106K\n");
232                     break;
233             case RFID_14443A_SPEED_212K:
234                     rate = 0x01;
235                     DEBUGPC("212K\n");
236                     break;
237             case RFID_14443A_SPEED_424K:
238                     rate = 0x02;
239                     DEBUGPC("424K\n");
240                     break;
241             case RFID_14443A_SPEED_848K:
242                     rate = 0x03;
243                     DEBUGPC("848K\n");
244                     break;
245             default:
246                     return -EINVAL;
247                     break;
248         }
249         return rh->ah->asic->priv.rc632.fn.iso14443a.set_speed(rh->ah,
250                                                                tx, rate);
251 }
252
253 static int spidev_14443b_init(struct rfid_reader_handle *rh)
254 {
255         return rh->ah->asic->priv.rc632.fn.iso14443b.init(rh->ah);
256 }
257
258 static int spidev_15693_init(struct rfid_reader_handle *rh)
259 {
260         return rh->ah->asic->priv.rc632.fn.iso15693.init(rh->ah);
261 }
262
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],
266                                       char *bit_of_col)
267 {
268         return rh->ah->asic->priv.rc632.fn.iso15693.transceive_ac(
269                                         rh->ah, acf, uuid, bit_of_col);
270 }
271
272 static int
273 spidev_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t * key)
274 {
275         return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
276 }
277
278 static int
279 spidev_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
280                      u_int32_t serno, u_int8_t block)
281 {
282         return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah,
283                                                                cmd, serno,
284                                                                block);
285 }
286 static int
287 spidev_rf_power(struct rfid_reader_handle *rh, int on)
288 {
289         return rh->ah->asic->priv.rc632.fn.rf_power(rh->ah, on);
290 }
291
292 static struct rfid_reader_handle *spidev_open(void *data)
293 {
294         struct rfid_reader_handle *rh;
295         struct rfid_asic_transport_handle *rath;
296         __u32 tmp;
297
298         /* open spi device */
299         if (!data) {
300                 DEBUGP("No device name\n");
301                 return NULL;
302         }
303         if ((spidev_fd = open(data, O_RDWR)) < 0) {
304                 DEBUGP("Unable to open:\n");
305                 return NULL;
306         }
307
308         rh = malloc(sizeof(*rh));
309         if (!rh)
310                 goto out_close_spi;
311
312         memset(rh, 0, sizeof(*rh));
313
314         rath = malloc(sizeof(*rath));
315         if (!rath)
316                 goto out_rh;
317         memset(rath, 0, sizeof(*rath));
318
319         rath->rat = &spidev_spi;
320         rh->reader = &rfid_reader_spidev;
321
322         /* Configure spi device, MODE 0 */
323         tmp = SPI_MODE_0;
324         if (ioctl(spidev_fd, SPI_IOC_WR_MODE, &tmp) < 0)
325                 goto out_rath;
326
327         /* MSB First */
328         tmp = 0;
329         if (ioctl(spidev_fd, SPI_IOC_WR_LSB_FIRST, &tmp) < 0)
330                 goto out_rath;
331
332         /* 8 bits per word */
333         tmp = 8;
334         if (ioctl(spidev_fd, SPI_IOC_WR_BITS_PER_WORD, &tmp) < 0)
335                 goto out_rath;
336
337         /* 1 MHz */
338         tmp = 1e6;
339         if (ioctl(spidev_fd, SPI_IOC_WR_MAX_SPEED_HZ, &tmp) < 0)
340                 goto out_rath;
341
342         /* turn on rc632 */
343         rh->ah = rc632_open(rath);
344         if (!rh->ah)
345                 goto out_rath;
346
347         /* everything is ok, returning reader handler */
348         return rh;
349 out_rath:
350         free(rath);
351 out_rh:
352         free(rh);
353 out_close_spi:
354         close(spidev_fd);
355         return NULL;
356 }
357
358 static void spidev_close(struct rfid_reader_handle *rh)
359 {
360         struct rfid_asic_transport_handle *rath = rh->ah->rath;
361
362         if (rh->ah)
363                 rc632_close(rh->ah);
364
365         if (spidev_fd > 0)
366                 close(spidev_fd);
367
368         if (rath)
369                 free(rath);
370
371         if (rh)
372                 free(rh);
373 }
374
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),
388         .iso14443a = {
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,
395         },
396         .iso14443b = {
397                       .init = &spidev_14443b_init,
398         },
399         .iso15693 = {
400                      .init = &spidev_15693_init,
401                      .transceive_ac = &spidev_15693_transceive_ac,
402         },
403         .mifare_classic = {
404                 .setkey = &spidev_mifare_setkey,
405                 .auth = &spidev_mifare_auth,
406         },
407 };
408