aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/nfc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/nfc')
-rw-r--r--drivers/nfc/Kconfig1
-rw-r--r--drivers/nfc/Makefile1
-rw-r--r--drivers/nfc/st21nfca/Kconfig23
-rw-r--r--drivers/nfc/st21nfca/Makefile8
-rw-r--r--drivers/nfc/st21nfca/i2c.c595
-rw-r--r--drivers/nfc/st21nfca/st21nfca.c506
-rw-r--r--drivers/nfc/st21nfca/st21nfca.h87
7 files changed, 1221 insertions, 0 deletions
diff --git a/drivers/nfc/Kconfig b/drivers/nfc/Kconfig
index 65d4ca19d132..26c66a126551 100644
--- a/drivers/nfc/Kconfig
+++ b/drivers/nfc/Kconfig
@@ -71,5 +71,6 @@ config NFC_PORT100
71source "drivers/nfc/pn544/Kconfig" 71source "drivers/nfc/pn544/Kconfig"
72source "drivers/nfc/microread/Kconfig" 72source "drivers/nfc/microread/Kconfig"
73source "drivers/nfc/nfcmrvl/Kconfig" 73source "drivers/nfc/nfcmrvl/Kconfig"
74source "drivers/nfc/st21nfca/Kconfig"
74 75
75endmenu 76endmenu
diff --git a/drivers/nfc/Makefile b/drivers/nfc/Makefile
index ae42a3fa60c9..23225b0287fd 100644
--- a/drivers/nfc/Makefile
+++ b/drivers/nfc/Makefile
@@ -11,5 +11,6 @@ obj-$(CONFIG_NFC_SIM) += nfcsim.o
11obj-$(CONFIG_NFC_PORT100) += port100.o 11obj-$(CONFIG_NFC_PORT100) += port100.o
12obj-$(CONFIG_NFC_MRVL) += nfcmrvl/ 12obj-$(CONFIG_NFC_MRVL) += nfcmrvl/
13obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o 13obj-$(CONFIG_NFC_TRF7970A) += trf7970a.o
14obj-$(CONFIG_NFC_ST21NFCA) += st21nfca/
14 15
15ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG 16ccflags-$(CONFIG_NFC_DEBUG) := -DDEBUG
diff --git a/drivers/nfc/st21nfca/Kconfig b/drivers/nfc/st21nfca/Kconfig
new file mode 100644
index 000000000000..ee459f066ade
--- /dev/null
+++ b/drivers/nfc/st21nfca/Kconfig
@@ -0,0 +1,23 @@
1config NFC_ST21NFCA
2 tristate "STMicroelectronics ST21NFCA NFC driver"
3 depends on NFC_HCI
4 select CRC_CCITT
5 default n
6 ---help---
7 STMicroelectronics ST21NFCA core driver. It implements the chipset
8 HCI logic and hooks into the NFC kernel APIs. Physical layers will
9 register against it.
10
11 To compile this driver as a module, choose m here. The module will
12 be called st21nfca.
13 Say N if unsure.
14
15config NFC_ST21NFCA_I2C
16 tristate "NFC ST21NFCA i2c support"
17 depends on NFC_ST21NFCA && I2C && NFC_SHDLC
18 ---help---
19 This module adds support for the STMicroelectronics st21nfca i2c interface.
20 Select this if your platform is using the i2c bus.
21
22 If you choose to build a module, it'll be called st21nfca_i2c.
23 Say N if unsure.
diff --git a/drivers/nfc/st21nfca/Makefile b/drivers/nfc/st21nfca/Makefile
new file mode 100644
index 000000000000..038ed093a119
--- /dev/null
+++ b/drivers/nfc/st21nfca/Makefile
@@ -0,0 +1,8 @@
1#
2# Makefile for ST21NFCA HCI based NFC driver
3#
4
5st21nfca_i2c-objs = i2c.o
6
7obj-$(CONFIG_NFC_ST21NFCA) += st21nfca.o
8obj-$(CONFIG_NFC_ST21NFCA_I2C) += st21nfca_i2c.o
diff --git a/drivers/nfc/st21nfca/i2c.c b/drivers/nfc/st21nfca/i2c.c
new file mode 100644
index 000000000000..3b0fd0f76d1c
--- /dev/null
+++ b/drivers/nfc/st21nfca/i2c.c
@@ -0,0 +1,595 @@
1/*
2 * I2C Link Layer for ST21NFCA HCI based Driver
3 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
20#include <linux/crc-ccitt.h>
21#include <linux/module.h>
22#include <linux/i2c.h>
23#include <linux/gpio.h>
24#include <linux/miscdevice.h>
25#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/nfc.h>
28#include <linux/firmware.h>
29#include <linux/unaligned/access_ok.h>
30#include <linux/platform_data/st21nfca.h>
31
32#include <net/nfc/hci.h>
33#include <net/nfc/llc.h>
34#include <net/nfc/nfc.h>
35
36#include "st21nfca.h"
37
38/*
39 * Every frame starts with ST21NFCA_SOF_EOF and ends with ST21NFCA_SOF_EOF.
40 * Because ST21NFCA_SOF_EOF is a possible data value, there is a mecanism
41 * called byte stuffing has been introduced.
42 *
43 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
44 * - insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
45 * - xor byte with ST21NFCA_BYTE_STUFFING_MASK
46 */
47#define ST21NFCA_SOF_EOF 0x7e
48#define ST21NFCA_BYTE_STUFFING_MASK 0x20
49#define ST21NFCA_ESCAPE_BYTE_STUFFING 0x7d
50
51/* SOF + 00 fill size */
52#define ST21NFCA_FRAME_HEADROOM 2
53
54/* 4 bytes crc (worst case byte stuffing) + EOF */
55#define ST21NFCA_FRAME_TAILROOM 5
56
57#define ST21NFCA_HCI_I2C_DRIVER_NAME "st21nfca_hci_i2c"
58
59static struct i2c_device_id st21nfca_hci_i2c_id_table[] = {
60 {ST21NFCA_HCI_DRIVER_NAME, 0},
61 {}
62};
63
64MODULE_DEVICE_TABLE(i2c, st21nfca_hci_i2c_id_table);
65
66struct st21nfca_i2c_phy {
67 struct i2c_client *i2c_dev;
68 struct nfc_hci_dev *hdev;
69
70 unsigned int gpio_ena;
71 unsigned int gpio_irq;
72 unsigned int irq_polarity;
73
74 struct sk_buff *pending_skb;
75 int current_read_len;
76 /*
77 * crc might have fail because i2c macro
78 * is disable due to other interface activity
79 */
80 int crc_trials;
81
82 int powered;
83 int run_mode;
84
85 /*
86 * < 0 if hardware error occured (e.g. i2c err)
87 * and prevents normal operation.
88 */
89 int hard_fault;
90};
91static u8 len_seq[] = { 13, 24, 15, 29 };
92static u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
93
94#define I2C_DUMP_SKB(info, skb) \
95do { \
96 pr_debug("%s:\n", info); \
97 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
98 16, 1, (skb)->data, (skb)->len, 0); \
99} while (0)
100
101static void st21nfca_hci_platform_init(struct st21nfca_i2c_phy *phy)
102{
103 u16 wait_tab[] = { 50, 300, 1000 };
104 char reboot_cmd[] = { 0x7E, 0x66, 0x48, 0xF6, 0x7E };
105 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE];
106 int i, r = -1;
107
108 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++)
109 r = i2c_master_recv(phy->i2c_dev, tmp,
110 ST21NFCA_HCI_LLC_MAX_SIZE);
111
112 r = -1;
113 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++)
114 r = i2c_master_send(phy->i2c_dev, reboot_cmd,
115 sizeof(reboot_cmd));
116 usleep_range(1000, 1500);
117
118}
119
120static int st21nfca_hci_i2c_enable(void *phy_id)
121{
122 struct st21nfca_i2c_phy *phy = phy_id;
123
124 gpio_set_value(phy->gpio_ena, 1);
125 phy->powered = 1;
126 phy->run_mode = ST21NFCA_HCI_MODE;
127
128 usleep_range(10000, 15000);
129
130 return 0;
131}
132
133static void st21nfca_hci_i2c_disable(void *phy_id)
134{
135 struct st21nfca_i2c_phy *phy = phy_id;
136
137 pr_info("\n");
138 gpio_set_value(phy->gpio_ena, 0);
139
140 phy->powered = 0;
141}
142
143static int st21nfca_hci_add_len_crc(struct sk_buff *skb)
144{
145 int ret = 2;
146 u16 crc;
147 u8 tmp;
148
149 *skb_push(skb, 1) = 0;
150
151 crc = crc_ccitt(0xffff, skb->data, skb->len);
152 crc = ~crc;
153
154 tmp = crc & 0x00ff;
155 *skb_put(skb, 1) = tmp;
156
157 tmp = (crc >> 8) & 0x00ff;
158 *skb_put(skb, 1) = tmp;
159
160 return ret;
161}
162
163static void st21nfca_hci_remove_len_crc(struct sk_buff *skb, int crc_len)
164{
165 skb_pull(skb, ST21NFCA_FRAME_HEADROOM);
166 skb_trim(skb, crc_len);
167}
168
169/*
170 * Writing a frame must not return the number of written bytes.
171 * It must return either zero for success, or <0 for error.
172 * In addition, it must not alter the skb
173 */
174static int st21nfca_hci_i2c_write(void *phy_id, struct sk_buff *skb)
175{
176 int r = -1, i, j, len;
177 struct st21nfca_i2c_phy *phy = phy_id;
178 struct i2c_client *client = phy->i2c_dev;
179 u16 wait_tab[] = { 2, 3, 5, 15, 20, 40};
180 u8 tmp[ST21NFCA_HCI_LLC_MAX_SIZE * 2];
181
182 I2C_DUMP_SKB("st21nfca_hci_i2c_write", skb);
183
184
185 if (phy->hard_fault != 0)
186 return phy->hard_fault;
187
188 /*
189 * Compute CRC before byte stuffing computation on frame
190 * Note st21nfca_hci_add_len_crc is doing a byte stuffing
191 * on its own value
192 */
193 len = st21nfca_hci_add_len_crc(skb);
194
195 /* add ST21NFCA_SOF_EOF on tail */
196 *skb_put(skb, 1) = ST21NFCA_SOF_EOF;
197 /* add ST21NFCA_SOF_EOF on head */
198 *skb_push(skb, 1) = ST21NFCA_SOF_EOF;
199
200 /*
201 * Compute byte stuffing
202 * if byte == ST21NFCA_SOF_EOF or ST21NFCA_ESCAPE_BYTE_STUFFING
203 * insert ST21NFCA_ESCAPE_BYTE_STUFFING (escape byte)
204 * xor byte with ST21NFCA_BYTE_STUFFING_MASK
205 */
206 tmp[0] = skb->data[0];
207 for (i = 1, j = 1; i < skb->len - 1; i++, j++) {
208 if (skb->data[i] == ST21NFCA_SOF_EOF
209 || skb->data[i] == ST21NFCA_ESCAPE_BYTE_STUFFING) {
210 tmp[j] = ST21NFCA_ESCAPE_BYTE_STUFFING;
211 j++;
212 tmp[j] = skb->data[i] ^ ST21NFCA_BYTE_STUFFING_MASK;
213 } else {
214 tmp[j] = skb->data[i];
215 }
216 }
217 tmp[j] = skb->data[i];
218 j++;
219
220 /*
221 * Manage sleep mode
222 * Try 3 times to send data with delay between each
223 */
224 for (i = 0; i < ARRAY_SIZE(wait_tab) && r < 0; i++) {
225 r = i2c_master_send(client, tmp, j);
226 if (r < 0)
227 msleep(wait_tab[i]);
228 }
229
230 if (r >= 0) {
231 if (r != j)
232 r = -EREMOTEIO;
233 else
234 r = 0;
235 }
236
237 st21nfca_hci_remove_len_crc(skb, len);
238
239 return r;
240}
241
242static int get_frame_size(u8 *buf, int buflen)
243{
244 int len = 0;
245 if (buf[len + 1] == ST21NFCA_SOF_EOF)
246 return 0;
247
248 for (len = 1; len < buflen && buf[len] != ST21NFCA_SOF_EOF; len++)
249 ;
250
251 return len;
252}
253
254static int check_crc(u8 *buf, int buflen)
255{
256 u16 crc;
257
258 crc = crc_ccitt(0xffff, buf, buflen - 2);
259 crc = ~crc;
260
261 if (buf[buflen - 2] != (crc & 0xff) || buf[buflen - 1] != (crc >> 8)) {
262 pr_err(ST21NFCA_HCI_DRIVER_NAME
263 ": CRC error 0x%x != 0x%x 0x%x\n", crc, buf[buflen - 1],
264 buf[buflen - 2]);
265
266 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
267 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
268 16, 2, buf, buflen, false);
269 return -EPERM;
270 }
271 return 0;
272}
273
274/*
275 * Prepare received data for upper layer.
276 * Received data include byte stuffing, crc and sof/eof
277 * which is not usable by hci part.
278 * returns:
279 * frame size without sof/eof, header and byte stuffing
280 * -EBADMSG : frame was incorrect and discarded
281 */
282static int st21nfca_hci_i2c_repack(struct sk_buff *skb)
283{
284 int i, j, r, size;
285 if (skb->len < 1 || (skb->len > 1 && skb->data[1] != 0))
286 return -EBADMSG;
287
288 size = get_frame_size(skb->data, skb->len);
289 if (size > 0) {
290 skb_trim(skb, size);
291 /* remove ST21NFCA byte stuffing for upper layer */
292 for (i = 1, j = 0; i < skb->len; i++) {
293 if (skb->data[i] ==
294 (u8) ST21NFCA_ESCAPE_BYTE_STUFFING) {
295 skb->data[i] =
296 skb->data[i +
297 1] | ST21NFCA_BYTE_STUFFING_MASK;
298 i++;
299 j++;
300 }
301 skb->data[i] = skb->data[i + j];
302 }
303 /* remove byte stuffing useless byte */
304 skb_trim(skb, i - j);
305 /* remove ST21NFCA_SOF_EOF from head */
306 skb_pull(skb, 1);
307
308 r = check_crc(skb->data, skb->len);
309 if (r != 0) {
310 i = 0;
311 return -EBADMSG;
312 }
313
314 /* remove headbyte */
315 skb_pull(skb, 1);
316 /* remove crc. Byte Stuffing is already removed here */
317 skb_trim(skb, skb->len - 2);
318 return skb->len;
319 }
320 return 0;
321}
322
323/*
324 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
325 * that i2c bus will be flushed and that next read will start on a new frame.
326 * returned skb contains only LLC header and payload.
327 * returns:
328 * frame size : if received frame is complete (find ST21NFCA_SOF_EOF at
329 * end of read)
330 * -EAGAIN : if received frame is incomplete (not find ST21NFCA_SOF_EOF
331 * at end of read)
332 * -EREMOTEIO : i2c read error (fatal)
333 * -EBADMSG : frame was incorrect and discarded
334 * (value returned from st21nfca_hci_i2c_repack)
335 * -EIO : if no ST21NFCA_SOF_EOF is found after reaching
336 * the read length end sequence
337 */
338static int st21nfca_hci_i2c_read(struct st21nfca_i2c_phy *phy,
339 struct sk_buff *skb)
340{
341 int r, i;
342 u8 len;
343 struct i2c_client *client = phy->i2c_dev;
344
345 if (phy->current_read_len < ARRAY_SIZE(len_seq)) {
346 len = len_seq[phy->current_read_len];
347
348 /*
349 * Add retry mecanism
350 * Operation on I2C interface may fail in case of operation on
351 * RF or SWP interface
352 */
353 r = 0;
354 for (i = 0; i < ARRAY_SIZE(wait_tab) && r <= 0; i++) {
355 r = i2c_master_recv(client, skb_put(skb, len), len);
356 if (r < 0)
357 msleep(wait_tab[i]);
358 }
359
360 if (r != len) {
361 phy->current_read_len = 0;
362 return -EREMOTEIO;
363 }
364
365 if (memchr(skb->data + 2, ST21NFCA_SOF_EOF,
366 skb->len - 2) != NULL) {
367 phy->current_read_len = 0;
368 return st21nfca_hci_i2c_repack(skb);
369 }
370 phy->current_read_len++;
371 return -EAGAIN;
372 }
373 return -EIO;
374}
375
376/*
377 * Reads an shdlc frame from the chip. This is not as straightforward as it
378 * seems. The frame format is data-crc, and corruption can occur anywhere
379 * while transiting on i2c bus, such that we could read an invalid data.
380 * The tricky case is when we read a corrupted data or crc. We must detect
381 * this here in order to determine that data can be transmitted to the hci
382 * core. This is the reason why we check the crc here.
383 * The CLF will repeat a frame until we send a RR on that frame.
384 *
385 * On ST21NFCA, IRQ goes in idle when read starts. As no size information are
386 * available in the incoming data, other IRQ might come. Every IRQ will trigger
387 * a read sequence with different length and will fill the current frame.
388 * The reception is complete once we reach a ST21NFCA_SOF_EOF.
389 */
390static irqreturn_t st21nfca_hci_irq_thread_fn(int irq, void *phy_id)
391{
392 struct st21nfca_i2c_phy *phy = phy_id;
393 struct i2c_client *client;
394
395 int r;
396
397 if (!phy || irq != phy->i2c_dev->irq) {
398 WARN_ON_ONCE(1);
399 return IRQ_NONE;
400 }
401
402 client = phy->i2c_dev;
403 dev_dbg(&client->dev, "IRQ\n");
404
405 if (phy->hard_fault != 0)
406 return IRQ_HANDLED;
407
408 r = st21nfca_hci_i2c_read(phy, phy->pending_skb);
409 if (r == -EREMOTEIO) {
410 phy->hard_fault = r;
411
412 nfc_hci_recv_frame(phy->hdev, NULL);
413
414 return IRQ_HANDLED;
415 } else if (r == -EAGAIN || r == -EIO) {
416 return IRQ_HANDLED;
417 } else if (r == -EBADMSG && phy->crc_trials < ARRAY_SIZE(wait_tab)) {
418 /*
419 * With ST21NFCA, only one interface (I2C, RF or SWP)
420 * may be active at a time.
421 * Having incorrect crc is usually due to i2c macrocell
422 * deactivation in the middle of a transmission.
423 * It may generate corrupted data on i2c.
424 * We give sometime to get i2c back.
425 * The complete frame will be repeated.
426 */
427 msleep(wait_tab[phy->crc_trials]);
428 phy->crc_trials++;
429 phy->current_read_len = 0;
430 } else if (r > 0) {
431 /*
432 * We succeeded to read data from the CLF and
433 * data is valid.
434 * Reset counter.
435 */
436 nfc_hci_recv_frame(phy->hdev, phy->pending_skb);
437 phy->crc_trials = 0;
438 }
439
440 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
441 if (phy->pending_skb == NULL) {
442 phy->hard_fault = -ENOMEM;
443 nfc_hci_recv_frame(phy->hdev, NULL);
444 }
445
446 return IRQ_HANDLED;
447}
448
449static struct nfc_phy_ops i2c_phy_ops = {
450 .write = st21nfca_hci_i2c_write,
451 .enable = st21nfca_hci_i2c_enable,
452 .disable = st21nfca_hci_i2c_disable,
453};
454
455static int st21nfca_request_resources(struct st21nfca_i2c_phy *phy,
456 struct i2c_client *client)
457{
458 struct st21nfca_nfc_platform_data *pdata;
459 int r;
460
461 pdata = client->dev.platform_data;
462 if (pdata == NULL) {
463 nfc_err(&client->dev, "No platform data\n");
464 return -EINVAL;
465 }
466
467 /* store for later use */
468 phy->gpio_irq = pdata->gpio_irq;
469 phy->gpio_ena = pdata->gpio_ena;
470 phy->irq_polarity = pdata->irq_polarity;
471 phy->i2c_dev = client;
472
473 r = devm_gpio_request(&client->dev, phy->gpio_irq, "wake_up");
474 if (r) {
475 pr_err("%s : gpio_request failed\n", __FILE__);
476 return -ENODEV;
477 }
478
479 r = gpio_direction_input(phy->gpio_irq);
480 if (r) {
481 pr_err("%s : gpio_direction_input failed\n", __FILE__);
482 return -ENODEV;
483 }
484
485 if (phy->gpio_ena != 0) {
486 r = devm_gpio_request(&client->dev,
487 phy->gpio_ena, "clf_enable");
488 if (r) {
489 pr_err("%s : ena gpio_request failed\n", __FILE__);
490 return -ENODEV;
491 }
492 r = gpio_direction_output(phy->gpio_ena, 1);
493
494 if (r) {
495 pr_err("%s : ena gpio_direction_output failed\n",
496 __FILE__);
497 return -ENODEV;
498 }
499 }
500
501 phy->pending_skb = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE * 2, GFP_KERNEL);
502 if (phy->pending_skb == NULL)
503 return -ENOMEM;
504
505 phy->current_read_len = 0;
506 phy->crc_trials = 0;
507 return r;
508}
509
510static int st21nfca_hci_i2c_probe(struct i2c_client *client,
511 const struct i2c_device_id *id)
512{
513 struct st21nfca_i2c_phy *phy;
514 struct st21nfca_nfc_platform_data *pdata;
515 int r = 0;
516
517 dev_dbg(&client->dev, "%s\n", __func__);
518 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
519
520 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
521 nfc_err(&client->dev, "Need I2C_FUNC_I2C\n");
522 return -ENODEV;
523 }
524
525 phy = devm_kzalloc(&client->dev, sizeof(struct st21nfca_i2c_phy),
526 GFP_KERNEL);
527 if (!phy) {
528 nfc_err(&client->dev,
529 "Cannot allocate memory for st21nfca i2c phy.\n");
530 return -ENOMEM;
531 }
532
533 phy->i2c_dev = client;
534
535 i2c_set_clientdata(client, phy);
536
537 pdata = client->dev.platform_data;
538 if (pdata == NULL) {
539 nfc_err(&client->dev, "No platform data\n");
540 return -EINVAL;
541 }
542
543 r = st21nfca_request_resources(phy, client);
544 if (r) {
545 nfc_err(&client->dev, "Cannot get platform resources\n");
546 return r;
547 }
548
549 st21nfca_hci_platform_init(phy);
550 r = devm_request_threaded_irq(&client->dev, client->irq, NULL,
551 st21nfca_hci_irq_thread_fn,
552 phy->irq_polarity | IRQF_ONESHOT,
553 ST21NFCA_HCI_DRIVER_NAME, phy);
554 if (r < 0) {
555 nfc_err(&client->dev, "Unable to register IRQ handler\n");
556 return r;
557 }
558
559 r = st21nfca_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
560 ST21NFCA_FRAME_HEADROOM, ST21NFCA_FRAME_TAILROOM,
561 ST21NFCA_HCI_LLC_MAX_PAYLOAD, &phy->hdev);
562
563 if (r < 0)
564 return r;
565
566 return 0;
567}
568
569static int st21nfca_hci_i2c_remove(struct i2c_client *client)
570{
571 struct st21nfca_i2c_phy *phy = i2c_get_clientdata(client);
572
573 dev_dbg(&client->dev, "%s\n", __func__);
574
575 st21nfca_hci_remove(phy->hdev);
576
577 if (phy->powered)
578 st21nfca_hci_i2c_disable(phy);
579
580 return 0;
581}
582
583static struct i2c_driver st21nfca_hci_i2c_driver = {
584 .driver = {
585 .name = ST21NFCA_HCI_I2C_DRIVER_NAME,
586 },
587 .probe = st21nfca_hci_i2c_probe,
588 .id_table = st21nfca_hci_i2c_id_table,
589 .remove = st21nfca_hci_i2c_remove,
590};
591
592module_i2c_driver(st21nfca_hci_i2c_driver);
593
594MODULE_LICENSE("GPL");
595MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/nfc/st21nfca/st21nfca.c b/drivers/nfc/st21nfca/st21nfca.c
new file mode 100644
index 000000000000..69213f37b7ba
--- /dev/null
+++ b/drivers/nfc/st21nfca/st21nfca.c
@@ -0,0 +1,506 @@
1/*
2 * HCI based Driver for STMicroelectronics NFC Chip
3 *
4 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/crc-ccitt.h>
20#include <linux/module.h>
21#include <linux/delay.h>
22#include <linux/slab.h>
23#include <linux/miscdevice.h>
24#include <linux/interrupt.h>
25#include <linux/gpio.h>
26#include <linux/i2c.h>
27
28#include <linux/nfc.h>
29#include <net/nfc/hci.h>
30#include <net/nfc/llc.h>
31
32#include <uapi/linux/nfc.h>
33
34#include "st21nfca.h"
35#include <linux/platform_data/st21nfca.h>
36
37#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
38
39#define FULL_VERSION_LEN 3
40
41/* Proprietary gates, events, commands and registers */
42
43/* Commands that apply to all RF readers */
44#define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK 0x30
45
46#define ST21NFCA_RF_READER_ISO15693_GATE 0x12
47
48/*
49 * Reader gate for communication with contact-less cards using Type A
50 * protocol ISO14443-3 but not compliant with ISO14443-4
51 */
52#define ST21NFCA_RF_READER_14443_3_A_GATE 0x15
53#define ST21NFCA_RF_READER_14443_3_A_UID 0x02
54#define ST21NFCA_RF_READER_14443_3_A_ATQA 0x03
55#define ST21NFCA_RF_READER_14443_3_A_SAK 0x04
56
57#define ST21NFCA_DEVICE_MGNT_GATE 0x01
58#define ST21NFCA_DEVICE_MGNT_PIPE 0x02
59#define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
60
61
62static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
63
64static struct nfc_hci_gate st21nfca_gates[] = {
65 {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE},
66 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
67 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
68 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE},
69 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
70 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
71 {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
72 {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
73 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
74};
75/* Largest headroom needed for outgoing custom commands */
76#define ST21NFCA_CMDS_HEADROOM 7
77
78static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
79{
80 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
81 int r;
82
83 mutex_lock(&info->info_lock);
84
85 if (info->state != ST21NFCA_ST_COLD) {
86 r = -EBUSY;
87 goto out;
88 }
89
90 r = info->phy_ops->enable(info->phy_id);
91
92 if (r == 0)
93 info->state = ST21NFCA_ST_READY;
94
95out:
96 mutex_unlock(&info->info_lock);
97 return r;
98}
99
100static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
101{
102 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
103
104 mutex_lock(&info->info_lock);
105
106 if (info->state == ST21NFCA_ST_COLD)
107 goto out;
108
109 info->phy_ops->disable(info->phy_id);
110 info->state = ST21NFCA_ST_COLD;
111
112out:
113 mutex_unlock(&info->info_lock);
114}
115
116static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
117{
118 struct sk_buff *skb;
119
120 u8 param;
121 int r;
122
123 param = NFC_HCI_UICC_HOST_ID;
124 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
125 NFC_HCI_ADMIN_WHITELIST, &param, 1);
126 if (r < 0)
127 return r;
128
129 /* Set NFC_MODE in device management gate to enable */
130 r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
131 ST21NFCA_NFC_MODE, &skb);
132 if (r < 0)
133 return r;
134
135 if (skb->data[0] == 0) {
136 kfree_skb(skb);
137 param = 1;
138
139 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
140 ST21NFCA_NFC_MODE, &param, 1);
141 if (r < 0)
142 return r;
143 }
144
145 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
146 NFC_HCI_EVT_END_OPERATION, NULL, 0);
147 if (r < 0)
148 return r;
149
150 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
151 NFC_HCI_ID_MGMT_VERSION_SW, &skb);
152 if (r < 0)
153 return r;
154
155 if (skb->len != FULL_VERSION_LEN) {
156 kfree_skb(skb);
157 return -EINVAL;
158 }
159
160 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
161 DUMP_PREFIX_NONE, 16, 1,
162 skb->data, FULL_VERSION_LEN, false);
163
164 kfree_skb(skb);
165
166 return 0;
167}
168
169static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
170{
171 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
172
173 return info->phy_ops->write(info->phy_id, skb);
174}
175
176static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
177 u32 im_protocols, u32 tm_protocols)
178{
179 int r;
180
181 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
182 __func__, im_protocols, tm_protocols);
183
184 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
185 NFC_HCI_EVT_END_OPERATION, NULL, 0);
186 if (r < 0)
187 return r;
188 if (im_protocols) {
189 /*
190 * enable polling according to im_protocols & tm_protocols
191 * - CLOSE pipe according to im_protocols & tm_protocols
192 */
193 if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
194 r = nfc_hci_disconnect_gate(hdev,
195 NFC_HCI_RF_READER_B_GATE);
196 if (r < 0)
197 return r;
198 }
199
200 if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
201 r = nfc_hci_disconnect_gate(hdev,
202 NFC_HCI_RF_READER_A_GATE);
203 if (r < 0)
204 return r;
205 }
206
207 if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
208 r = nfc_hci_disconnect_gate(hdev,
209 ST21NFCA_RF_READER_F_GATE);
210 if (r < 0)
211 return r;
212 }
213
214 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
215 r = nfc_hci_disconnect_gate(hdev,
216 ST21NFCA_RF_READER_14443_3_A_GATE);
217 if (r < 0)
218 return r;
219 }
220
221 if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
222 r = nfc_hci_disconnect_gate(hdev,
223 ST21NFCA_RF_READER_ISO15693_GATE);
224 if (r < 0)
225 return r;
226 }
227
228 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
229 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
230 if (r < 0)
231 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
232 NFC_HCI_EVT_END_OPERATION, NULL, 0);
233 }
234 return r;
235}
236
237static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
238{
239 int r;
240 struct sk_buff *atqa_skb = NULL;
241
242 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
243 ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
244 if (r < 0)
245 goto exit;
246
247 if (atqa_skb->len != 2) {
248 r = -EPROTO;
249 goto exit;
250 }
251
252 *atqa = be16_to_cpu(*(u16 *) atqa_skb->data);
253
254exit:
255 kfree_skb(atqa_skb);
256 return r;
257}
258
259static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
260{
261 int r;
262 struct sk_buff *sak_skb = NULL;
263
264 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
265 ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
266 if (r < 0)
267 goto exit;
268
269 if (sak_skb->len != 1) {
270 r = -EPROTO;
271 goto exit;
272 }
273
274 *sak = sak_skb->data[0];
275
276exit:
277 kfree_skb(sak_skb);
278 return r;
279}
280
281static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
282 int *len)
283{
284 int r;
285 struct sk_buff *uid_skb = NULL;
286
287 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
288 ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
289 if (r < 0)
290 goto exit;
291
292 if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
293 r = -EPROTO;
294 goto exit;
295 }
296
297 gate = uid_skb->data;
298 *len = uid_skb->len;
299exit:
300 kfree_skb(uid_skb);
301 return r;
302}
303
304static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
305 struct nfc_target *target)
306{
307 int r, len;
308 u16 atqa;
309 u8 sak;
310 u8 uid[NFC_NFCID1_MAXSIZE];
311
312 switch (gate) {
313 case ST21NFCA_RF_READER_F_GATE:
314 target->supported_protocols = NFC_PROTO_FELICA_MASK;
315 break;
316 case ST21NFCA_RF_READER_14443_3_A_GATE:
317 /* ISO14443-3 type 1 or 2 tags */
318 r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
319 if (r < 0)
320 return r;
321 if (atqa == 0x000c) {
322 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
323 target->sens_res = 0x0c00;
324 } else {
325 r = st21nfca_get_iso14443_3_sak(hdev, &sak);
326 if (r < 0)
327 return r;
328
329 r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
330 if (r < 0)
331 return r;
332
333 target->supported_protocols =
334 nfc_hci_sak_to_protocol(sak);
335 if (target->supported_protocols == 0xffffffff)
336 return -EPROTO;
337
338 target->sens_res = atqa;
339 target->sel_res = sak;
340 memcpy(target->nfcid1, uid, len);
341 target->nfcid1_len = len;
342 }
343
344 break;
345 default:
346 return -EPROTO;
347 }
348
349 return 0;
350}
351
352/*
353 * Returns:
354 * <= 0: driver handled the data exchange
355 * 1: driver doesn't especially handle, please do standard processing
356 */
357static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
358 struct nfc_target *target,
359 struct sk_buff *skb,
360 data_exchange_cb_t cb, void *cb_context)
361{
362 pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
363 target->hci_reader_gate, skb->len);
364
365 switch (target->hci_reader_gate) {
366 case ST21NFCA_RF_READER_F_GATE:
367 *skb_push(skb, 1) = 0x1a;
368 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
369 ST21NFCA_WR_XCHG_DATA, skb->data,
370 skb->len, cb, cb_context);
371 case ST21NFCA_RF_READER_14443_3_A_GATE:
372 *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
373
374 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
375 ST21NFCA_WR_XCHG_DATA, skb->data,
376 skb->len, cb, cb_context);
377 default:
378 return 1;
379 }
380}
381
382static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
383 struct nfc_target *target)
384{
385 u8 fwi = 0x11;
386 switch (target->hci_reader_gate) {
387 case NFC_HCI_RF_READER_A_GATE:
388 case NFC_HCI_RF_READER_B_GATE:
389 /*
390 * PRESENCE_CHECK on those gates is available
391 * However, the answer to this command is taking 3 * fwi
392 * if the card is no present.
393 * Instead, we send an empty I-Frame with a very short
394 * configurable fwi ~604µs.
395 */
396 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
397 ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
398 case ST21NFCA_RF_READER_14443_3_A_GATE:
399 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
400 ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
401 NULL, 0, NULL);
402 default:
403 return -EOPNOTSUPP;
404 }
405}
406
407static struct nfc_hci_ops st21nfca_hci_ops = {
408 .open = st21nfca_hci_open,
409 .close = st21nfca_hci_close,
410 .hci_ready = st21nfca_hci_ready,
411 .xmit = st21nfca_hci_xmit,
412 .start_poll = st21nfca_hci_start_poll,
413 .target_from_gate = st21nfca_hci_target_from_gate,
414 .im_transceive = st21nfca_hci_im_transceive,
415 .check_presence = st21nfca_hci_check_presence,
416};
417
418int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
419 char *llc_name, int phy_headroom, int phy_tailroom,
420 int phy_payload, struct nfc_hci_dev **hdev)
421{
422 struct st21nfca_hci_info *info;
423 int r = 0;
424 int dev_num;
425 u32 protocols;
426 struct nfc_hci_init_data init_data;
427 unsigned long quirks = 0;
428
429 info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
430 if (!info) {
431 r = -ENOMEM;
432 goto err_alloc_hdev;
433 }
434
435 info->phy_ops = phy_ops;
436 info->phy_id = phy_id;
437 info->state = ST21NFCA_ST_COLD;
438 mutex_init(&info->info_lock);
439
440 init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
441
442 memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
443
444 /*
445 * Session id must include the driver name + i2c bus addr
446 * persistent info to discriminate 2 identical chips
447 */
448 dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
449 if (dev_num >= ST21NFCA_NUM_DEVICES)
450 goto err_alloc_hdev;
451
452 scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
453 "ST21AH", dev_num);
454
455 protocols = NFC_PROTO_JEWEL_MASK |
456 NFC_PROTO_MIFARE_MASK |
457 NFC_PROTO_FELICA_MASK |
458 NFC_PROTO_ISO14443_MASK |
459 NFC_PROTO_ISO14443_B_MASK;
460
461 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
462
463 info->hdev =
464 nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
465 protocols, llc_name,
466 phy_headroom + ST21NFCA_CMDS_HEADROOM,
467 phy_tailroom, phy_payload);
468
469 if (!info->hdev) {
470 pr_err("Cannot allocate nfc hdev.\n");
471 r = -ENOMEM;
472 goto err_alloc_hdev;
473 }
474
475 nfc_hci_set_clientdata(info->hdev, info);
476
477 r = nfc_hci_register_device(info->hdev);
478 if (r)
479 goto err_regdev;
480
481 *hdev = info->hdev;
482
483 return 0;
484
485err_regdev:
486 nfc_hci_free_device(info->hdev);
487
488err_alloc_hdev:
489 kfree(info);
490
491 return r;
492}
493EXPORT_SYMBOL(st21nfca_hci_probe);
494
495void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
496{
497 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
498
499 nfc_hci_unregister_device(hdev);
500 nfc_hci_free_device(hdev);
501 kfree(info);
502}
503EXPORT_SYMBOL(st21nfca_hci_remove);
504
505MODULE_LICENSE("GPL");
506MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/nfc/st21nfca/st21nfca.h b/drivers/nfc/st21nfca/st21nfca.h
new file mode 100644
index 000000000000..334cd90bcc8c
--- /dev/null
+++ b/drivers/nfc/st21nfca/st21nfca.h
@@ -0,0 +1,87 @@
1/*
2 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, see <http://www.gnu.org/licenses/>.
15 */
16
17#ifndef __LOCAL_ST21NFCA_H_
18#define __LOCAL_ST21NFCA_H_
19
20#include <net/nfc/hci.h>
21
22#define HCI_MODE 0
23
24/* framing in HCI mode */
25#define ST21NFCA_SOF_EOF_LEN 2
26
27/* Almost every time value is 0 */
28#define ST21NFCA_HCI_LLC_LEN 1
29
30/* Size in worst case :
31 * In normal case CRC len = 2 but byte stuffing
32 * may appear in case one CRC byte = ST21NFCA_SOF_EOF
33 */
34#define ST21NFCA_HCI_LLC_CRC 4
35
36#define ST21NFCA_HCI_LLC_LEN_CRC (ST21NFCA_SOF_EOF_LEN + \
37 ST21NFCA_HCI_LLC_LEN + \
38 ST21NFCA_HCI_LLC_CRC)
39#define ST21NFCA_HCI_LLC_MIN_SIZE (1 + ST21NFCA_HCI_LLC_LEN_CRC)
40
41/* Worst case when adding byte stuffing between each byte */
42#define ST21NFCA_HCI_LLC_MAX_PAYLOAD 29
43#define ST21NFCA_HCI_LLC_MAX_SIZE (ST21NFCA_HCI_LLC_LEN_CRC + 1 + \
44 ST21NFCA_HCI_LLC_MAX_PAYLOAD)
45
46#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
47
48#define ST21NFCA_HCI_MODE 0
49
50#define ST21NFCA_NUM_DEVICES 256
51
52int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
53 char *llc_name, int phy_headroom, int phy_tailroom,
54 int phy_payload, struct nfc_hci_dev **hdev);
55void st21nfca_hci_remove(struct nfc_hci_dev *hdev);
56
57enum st21nfca_state {
58 ST21NFCA_ST_COLD,
59 ST21NFCA_ST_READY,
60};
61
62struct st21nfca_hci_info {
63 struct nfc_phy_ops *phy_ops;
64 void *phy_id;
65
66 struct nfc_hci_dev *hdev;
67
68 enum st21nfca_state state;
69
70 struct mutex info_lock;
71
72 int async_cb_type;
73 data_exchange_cb_t async_cb;
74 void *async_cb_context;
75
76} __packed;
77
78/* Reader RF commands */
79#define ST21NFCA_WR_XCHG_DATA 0x10
80
81#define ST21NFCA_RF_READER_F_GATE 0x14
82#define ST21NFCA_RF_READER_F_DATARATE 0x01
83#define ST21NFCA_RF_READER_F_DATARATE_106 0x01
84#define ST21NFCA_RF_READER_F_DATARATE_212 0x02
85#define ST21NFCA_RF_READER_F_DATARATE_424 0x04
86
87#endif /* __LOCAL_ST21NFCA_H_ */