always compile the rfid_hexdump() function, since DEBUG_LIBRFID might be defined...
[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
264 spidev_mifare_setkey(struct rfid_reader_handle *rh, const u_int8_t * key)
265 {
266         return rh->ah->asic->priv.rc632.fn.mifare_classic.setkey(rh->ah, key);
267 }
268
269 static int
270 spidev_mifare_auth(struct rfid_reader_handle *rh, u_int8_t cmd,
271                      u_int32_t serno, u_int8_t block)
272 {
273         return rh->ah->asic->priv.rc632.fn.mifare_classic.auth(rh->ah,
274                                                                cmd, serno,
275                                                                block);
276 }
277
278 static struct rfid_reader_handle *spidev_open(void *data)
279 {
280         struct rfid_reader_handle *rh;
281         struct rfid_asic_transport_handle *rath;
282         __u32 tmp;
283
284         /* open spi device */
285         if (!data) {
286                 DEBUGP("No device name\n");
287                 return NULL;
288         }
289         if ((spidev_fd = open(data, O_RDWR)) < 0) {
290                 DEBUGP("Unable to open:\n");
291                 return NULL;
292         }
293
294         rh = malloc(sizeof(*rh));
295         if (!rh)
296                 goto out_close_spi;
297
298         memset(rh, 0, sizeof(*rh));
299
300         rath = malloc(sizeof(*rath));
301         if (!rath)
302                 goto out_rh;
303         memset(rath, 0, sizeof(*rath));
304
305         rath->rat = &spidev_spi;
306         rh->reader = &rfid_reader_spidev;
307
308         /* Configure spi device, MODE 0 */
309         tmp = SPI_MODE_0;
310         if (ioctl(spidev_fd, SPI_IOC_WR_MODE, &tmp) < 0)
311                 goto out_rath;
312
313         /* MSB First */
314         tmp = 0;
315         if (ioctl(spidev_fd, SPI_IOC_WR_LSB_FIRST, &tmp) < 0)
316                 goto out_rath;
317
318         /* 8 bits per word */
319         tmp = 8;
320         if (ioctl(spidev_fd, SPI_IOC_WR_BITS_PER_WORD, &tmp) < 0)
321                 goto out_rath;
322
323         /* 1 MHz */
324         tmp = 1e6;
325         if (ioctl(spidev_fd, SPI_IOC_WR_MAX_SPEED_HZ, &tmp) < 0)
326                 goto out_rath;
327
328         /* turn on rc632 */
329         rh->ah = rc632_open(rath);
330         if (!rh->ah)
331                 goto out_rath;
332
333         /* everything is ok, returning reader handler */
334         return rh;
335 out_rath:
336         free(rath);
337 out_rh:
338         free(rh);
339 out_close_spi:
340         close(spidev_fd);
341         return NULL;
342 }
343
344 static void spidev_close(struct rfid_reader_handle *rh)
345 {
346         struct rfid_asic_transport_handle *rath = rh->ah->rath;
347
348         if (rh->ah)
349                 rc632_close(rh->ah);
350
351         if (spidev_fd > 0)
352                 close(spidev_fd);
353
354         if (rath)
355                 free(rath);
356
357         if (rh)
358                 free(rh);
359 }
360
361 struct rfid_reader rfid_reader_spidev = {
362         .name = "spidev reader",
363         .id = RFID_READER_SPIDEV,
364         .open = &spidev_open,
365         .close = &spidev_close,
366         .transceive = &spidev_transceive,
367         .l2_supported = (1 << RFID_LAYER2_ISO14443A) |
368                         (1 << RFID_LAYER2_ISO14443B) |
369                         (1 << RFID_LAYER2_ISO15693),
370         .proto_supported = (1 << RFID_PROTOCOL_TCL) |
371                            (1 << RFID_PROTOCOL_MIFARE_UL) |
372                            (1 << RFID_PROTOCOL_MIFARE_CLASSIC),
373         .iso14443a = {
374                       .init = &spidev_14443a_init,
375                       .transceive_sf = &spidev_transceive_sf,
376                       .transceive_acf = &spidev_transceive_acf,
377                       .speed = RFID_14443A_SPEED_106K
378                       | RFID_14443A_SPEED_212K | RFID_14443A_SPEED_424K,
379                       .set_speed = &spidev_14443a_set_speed,
380         },
381         .iso14443b = {
382                       .init = &spidev_14443b_init,
383         },
384         .mifare_classic = {
385                 .setkey = &spidev_mifare_setkey,
386                 .auth = &spidev_mifare_auth,
387         },
388 };
389