aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Lapuyade <eric.lapuyade@linux.intel.com>2012-10-02 12:44:06 -0400
committerSamuel Ortiz <sameo@linux.intel.com>2012-10-26 12:26:46 -0400
commit97f18414af395c547f20300e5d4c81d7190a4155 (patch)
tree4a2eb92f03d4bf8f8ff82452b2118f91d5780eef
parent632c016ab8ba1f98262bd6242b38d73ee4ae652e (diff)
NFC: Separate pn544 hci driver in HW dependant and independant parts
The driver now has all HCI stuff isolated in one file, and all the hardware link specifics in another. Writing a pn544 driver on top of another hardware link is now just a matter of adding a new file for that new hardware specifics. Signed-off-by: Eric Lapuyade <eric.lapuyade@intel.com> Signed-off-by: Samuel Ortiz <sameo@linux.intel.com>
-rw-r--r--drivers/nfc/Makefile2
-rw-r--r--drivers/nfc/pn544/Makefile7
-rw-r--r--drivers/nfc/pn544/i2c.c500
-rw-r--r--drivers/nfc/pn544/pn544.c (renamed from drivers/nfc/pn544_hci.c)438
-rw-r--r--drivers/nfc/pn544/pn544.h32
-rw-r--r--include/net/nfc/hci.h6
6 files changed, 570 insertions, 415 deletions
diff --git a/drivers/nfc/Makefile b/drivers/nfc/Makefile
index bf05831fdf09..36c359043f54 100644
--- a/drivers/nfc/Makefile
+++ b/drivers/nfc/Makefile
@@ -2,7 +2,7 @@
2# Makefile for nfc devices 2# Makefile for nfc devices
3# 3#
4 4
5obj-$(CONFIG_PN544_HCI_NFC) += pn544_hci.o 5obj-$(CONFIG_PN544_HCI_NFC) += pn544/
6obj-$(CONFIG_NFC_PN533) += pn533.o 6obj-$(CONFIG_NFC_PN533) += pn533.o
7obj-$(CONFIG_NFC_WILINK) += nfcwilink.o 7obj-$(CONFIG_NFC_WILINK) += nfcwilink.o
8 8
diff --git a/drivers/nfc/pn544/Makefile b/drivers/nfc/pn544/Makefile
new file mode 100644
index 000000000000..725733881eb3
--- /dev/null
+++ b/drivers/nfc/pn544/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for PN544 HCI based NFC driver
3#
4
5obj-$(CONFIG_PN544_HCI_NFC) += pn544_i2c.o
6
7pn544_i2c-y := pn544.o i2c.o
diff --git a/drivers/nfc/pn544/i2c.c b/drivers/nfc/pn544/i2c.c
new file mode 100644
index 000000000000..fb430d882352
--- /dev/null
+++ b/drivers/nfc/pn544/i2c.c
@@ -0,0 +1,500 @@
1/*
2 * I2C Link Layer for PN544 HCI based Driver
3 *
4 * Copyright (C) 2012 Intel Corporation. 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, write to the
17 * Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21#include <linux/crc-ccitt.h>
22#include <linux/module.h>
23#include <linux/i2c.h>
24#include <linux/gpio.h>
25#include <linux/miscdevice.h>
26#include <linux/interrupt.h>
27#include <linux/delay.h>
28
29#include <linux/nfc/pn544.h>
30
31#include <net/nfc/hci.h>
32#include <net/nfc/llc.h>
33
34#include "pn544.h"
35
36#define PN544_I2C_FRAME_HEADROOM 1
37#define PN544_I2C_FRAME_TAILROOM 2
38
39/* framing in HCI mode */
40#define PN544_HCI_I2C_LLC_LEN 1
41#define PN544_HCI_I2C_LLC_CRC 2
42#define PN544_HCI_I2C_LLC_LEN_CRC (PN544_HCI_I2C_LLC_LEN + \
43 PN544_HCI_I2C_LLC_CRC)
44#define PN544_HCI_I2C_LLC_MIN_SIZE (1 + PN544_HCI_I2C_LLC_LEN_CRC)
45#define PN544_HCI_I2C_LLC_MAX_PAYLOAD 29
46#define PN544_HCI_I2C_LLC_MAX_SIZE (PN544_HCI_I2C_LLC_LEN_CRC + 1 + \
47 PN544_HCI_I2C_LLC_MAX_PAYLOAD)
48
49static struct i2c_device_id pn544_hci_i2c_id_table[] = {
50 {"pn544", 0},
51 {}
52};
53
54MODULE_DEVICE_TABLE(i2c, pn544_hci_i2c_id_table);
55
56#define PN544_HCI_I2C_DRIVER_NAME "pn544_hci_i2c"
57
58struct pn544_i2c_phy {
59 struct i2c_client *i2c_dev;
60 struct nfc_hci_dev *hdev;
61
62 unsigned int gpio_en;
63 unsigned int gpio_irq;
64 unsigned int gpio_fw;
65 unsigned int en_polarity;
66
67 int powered;
68
69 int hard_fault; /*
70 * < 0 if hardware error occured (e.g. i2c err)
71 * and prevents normal operation.
72 */
73};
74
75#define I2C_DUMP_SKB(info, skb) \
76do { \
77 pr_debug("%s:\n", info); \
78 print_hex_dump(KERN_DEBUG, "i2c: ", DUMP_PREFIX_OFFSET, \
79 16, 1, (skb)->data, (skb)->len, 0); \
80} while (0)
81
82static void pn544_hci_i2c_platform_init(struct pn544_i2c_phy *phy)
83{
84 int polarity, retry, ret;
85 char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
86 int count = sizeof(rset_cmd);
87
88 pr_info(DRIVER_DESC ": %s\n", __func__);
89 dev_info(&phy->i2c_dev->dev, "Detecting nfc_en polarity\n");
90
91 /* Disable fw download */
92 gpio_set_value(phy->gpio_fw, 0);
93
94 for (polarity = 0; polarity < 2; polarity++) {
95 phy->en_polarity = polarity;
96 retry = 3;
97 while (retry--) {
98 /* power off */
99 gpio_set_value(phy->gpio_en, !phy->en_polarity);
100 usleep_range(10000, 15000);
101
102 /* power on */
103 gpio_set_value(phy->gpio_en, phy->en_polarity);
104 usleep_range(10000, 15000);
105
106 /* send reset */
107 dev_dbg(&phy->i2c_dev->dev, "Sending reset cmd\n");
108 ret = i2c_master_send(phy->i2c_dev, rset_cmd, count);
109 if (ret == count) {
110 dev_info(&phy->i2c_dev->dev,
111 "nfc_en polarity : active %s\n",
112 (polarity == 0 ? "low" : "high"));
113 goto out;
114 }
115 }
116 }
117
118 dev_err(&phy->i2c_dev->dev,
119 "Could not detect nfc_en polarity, fallback to active high\n");
120
121out:
122 gpio_set_value(phy->gpio_en, !phy->en_polarity);
123}
124
125static int pn544_hci_i2c_enable(void *phy_id)
126{
127 struct pn544_i2c_phy *phy = phy_id;
128
129 pr_info(DRIVER_DESC ": %s\n", __func__);
130
131 gpio_set_value(phy->gpio_fw, 0);
132 gpio_set_value(phy->gpio_en, phy->en_polarity);
133 usleep_range(10000, 15000);
134
135 phy->powered = 1;
136
137 return 0;
138}
139
140static void pn544_hci_i2c_disable(void *phy_id)
141{
142 struct pn544_i2c_phy *phy = phy_id;
143
144 pr_info(DRIVER_DESC ": %s\n", __func__);
145
146 gpio_set_value(phy->gpio_fw, 0);
147 gpio_set_value(phy->gpio_en, !phy->en_polarity);
148 usleep_range(10000, 15000);
149
150 gpio_set_value(phy->gpio_en, phy->en_polarity);
151 usleep_range(10000, 15000);
152
153 gpio_set_value(phy->gpio_en, !phy->en_polarity);
154 usleep_range(10000, 15000);
155
156 phy->powered = 0;
157}
158
159static void pn544_hci_i2c_add_len_crc(struct sk_buff *skb)
160{
161 u16 crc;
162 int len;
163
164 len = skb->len + 2;
165 *skb_push(skb, 1) = len;
166
167 crc = crc_ccitt(0xffff, skb->data, skb->len);
168 crc = ~crc;
169 *skb_put(skb, 1) = crc & 0xff;
170 *skb_put(skb, 1) = crc >> 8;
171}
172
173static void pn544_hci_i2c_remove_len_crc(struct sk_buff *skb)
174{
175 skb_pull(skb, PN544_I2C_FRAME_HEADROOM);
176 skb_trim(skb, PN544_I2C_FRAME_TAILROOM);
177}
178
179/*
180 * Writing a frame must not return the number of written bytes.
181 * It must return either zero for success, or <0 for error.
182 * In addition, it must not alter the skb
183 */
184static int pn544_hci_i2c_write(void *phy_id, struct sk_buff *skb)
185{
186 int r;
187 struct pn544_i2c_phy *phy = phy_id;
188 struct i2c_client *client = phy->i2c_dev;
189
190 if (phy->hard_fault != 0)
191 return phy->hard_fault;
192
193 usleep_range(3000, 6000);
194
195 pn544_hci_i2c_add_len_crc(skb);
196
197 I2C_DUMP_SKB("i2c frame written", skb);
198
199 r = i2c_master_send(client, skb->data, skb->len);
200
201 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
202 usleep_range(6000, 10000);
203 r = i2c_master_send(client, skb->data, skb->len);
204 }
205
206 if (r >= 0) {
207 if (r != skb->len)
208 r = -EREMOTEIO;
209 else
210 r = 0;
211 }
212
213 pn544_hci_i2c_remove_len_crc(skb);
214
215 return r;
216}
217
218static int check_crc(u8 *buf, int buflen)
219{
220 int len;
221 u16 crc;
222
223 len = buf[0] + 1;
224 crc = crc_ccitt(0xffff, buf, len - 2);
225 crc = ~crc;
226
227 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
228 pr_err(PN544_HCI_I2C_DRIVER_NAME
229 ": CRC error 0x%x != 0x%x 0x%x\n",
230 crc, buf[len - 1], buf[len - 2]);
231
232 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
233 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
234 16, 2, buf, buflen, false);
235 return -EPERM;
236 }
237 return 0;
238}
239
240/*
241 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
242 * that i2c bus will be flushed and that next read will start on a new frame.
243 * returned skb contains only LLC header and payload.
244 * returns:
245 * -EREMOTEIO : i2c read error (fatal)
246 * -EBADMSG : frame was incorrect and discarded
247 * -ENOMEM : cannot allocate skb, frame dropped
248 */
249static int pn544_hci_i2c_read(struct pn544_i2c_phy *phy, struct sk_buff **skb)
250{
251 int r;
252 u8 len;
253 u8 tmp[PN544_HCI_I2C_LLC_MAX_SIZE - 1];
254 struct i2c_client *client = phy->i2c_dev;
255
256 r = i2c_master_recv(client, &len, 1);
257 if (r != 1) {
258 dev_err(&client->dev, "cannot read len byte\n");
259 return -EREMOTEIO;
260 }
261
262 if ((len < (PN544_HCI_I2C_LLC_MIN_SIZE - 1)) ||
263 (len > (PN544_HCI_I2C_LLC_MAX_SIZE - 1))) {
264 dev_err(&client->dev, "invalid len byte\n");
265 r = -EBADMSG;
266 goto flush;
267 }
268
269 *skb = alloc_skb(1 + len, GFP_KERNEL);
270 if (*skb == NULL) {
271 r = -ENOMEM;
272 goto flush;
273 }
274
275 *skb_put(*skb, 1) = len;
276
277 r = i2c_master_recv(client, skb_put(*skb, len), len);
278 if (r != len) {
279 kfree_skb(*skb);
280 return -EREMOTEIO;
281 }
282
283 I2C_DUMP_SKB("i2c frame read", *skb);
284
285 r = check_crc((*skb)->data, (*skb)->len);
286 if (r != 0) {
287 kfree_skb(*skb);
288 r = -EBADMSG;
289 goto flush;
290 }
291
292 skb_pull(*skb, 1);
293 skb_trim(*skb, (*skb)->len - 2);
294
295 usleep_range(3000, 6000);
296
297 return 0;
298
299flush:
300 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
301 r = -EREMOTEIO;
302
303 usleep_range(3000, 6000);
304
305 return r;
306}
307
308/*
309 * Reads an shdlc frame from the chip. This is not as straightforward as it
310 * seems. There are cases where we could loose the frame start synchronization.
311 * The frame format is len-data-crc, and corruption can occur anywhere while
312 * transiting on i2c bus, such that we could read an invalid len.
313 * In order to recover synchronization with the next frame, we must be sure
314 * to read the real amount of data without using the len byte. We do this by
315 * assuming the following:
316 * - the chip will always present only one single complete frame on the bus
317 * before triggering the interrupt
318 * - the chip will not present a new frame until we have completely read
319 * the previous one (or until we have handled the interrupt).
320 * The tricky case is when we read a corrupted len that is less than the real
321 * len. We must detect this here in order to determine that we need to flush
322 * the bus. This is the reason why we check the crc here.
323 */
324static irqreturn_t pn544_hci_i2c_irq_thread_fn(int irq, void *phy_id)
325{
326 struct pn544_i2c_phy *phy = phy_id;
327 struct i2c_client *client;
328 struct sk_buff *skb = NULL;
329 int r;
330
331 if (!phy || irq != phy->i2c_dev->irq) {
332 WARN_ON_ONCE(1);
333 return IRQ_NONE;
334 }
335
336 client = phy->i2c_dev;
337 dev_dbg(&client->dev, "IRQ\n");
338
339 if (phy->hard_fault != 0)
340 return IRQ_HANDLED;
341
342 r = pn544_hci_i2c_read(phy, &skb);
343 if (r == -EREMOTEIO) {
344 phy->hard_fault = r;
345
346 nfc_hci_recv_frame(phy->hdev, NULL);
347
348 return IRQ_HANDLED;
349 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
350 return IRQ_HANDLED;
351 }
352
353 nfc_hci_recv_frame(phy->hdev, skb);
354
355 return IRQ_HANDLED;
356}
357
358static struct nfc_phy_ops i2c_phy_ops = {
359 .write = pn544_hci_i2c_write,
360 .enable = pn544_hci_i2c_enable,
361 .disable = pn544_hci_i2c_disable,
362};
363
364static int __devinit pn544_hci_i2c_probe(struct i2c_client *client,
365 const struct i2c_device_id *id)
366{
367 struct pn544_i2c_phy *phy;
368 struct pn544_nfc_platform_data *pdata;
369 int r = 0;
370
371 dev_dbg(&client->dev, "%s\n", __func__);
372 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
373
374 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
375 dev_err(&client->dev, "Need I2C_FUNC_I2C\n");
376 return -ENODEV;
377 }
378
379 phy = kzalloc(sizeof(struct pn544_i2c_phy), GFP_KERNEL);
380 if (!phy) {
381 dev_err(&client->dev,
382 "Cannot allocate memory for pn544 i2c phy.\n");
383 r = -ENOMEM;
384 goto err_phy_alloc;
385 }
386
387 phy->i2c_dev = client;
388 i2c_set_clientdata(client, phy);
389
390 pdata = client->dev.platform_data;
391 if (pdata == NULL) {
392 dev_err(&client->dev, "No platform data\n");
393 r = -EINVAL;
394 goto err_pdata;
395 }
396
397 if (pdata->request_resources == NULL) {
398 dev_err(&client->dev, "request_resources() missing\n");
399 r = -EINVAL;
400 goto err_pdata;
401 }
402
403 r = pdata->request_resources(client);
404 if (r) {
405 dev_err(&client->dev, "Cannot get platform resources\n");
406 goto err_pdata;
407 }
408
409 phy->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
410 phy->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
411 phy->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
412
413 pn544_hci_i2c_platform_init(phy);
414
415 r = request_threaded_irq(client->irq, NULL, pn544_hci_i2c_irq_thread_fn,
416 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
417 PN544_HCI_I2C_DRIVER_NAME, phy);
418 if (r < 0) {
419 dev_err(&client->dev, "Unable to register IRQ handler\n");
420 goto err_rti;
421 }
422
423 r = pn544_hci_probe(phy, &i2c_phy_ops, LLC_SHDLC_NAME,
424 PN544_I2C_FRAME_HEADROOM, PN544_I2C_FRAME_TAILROOM,
425 PN544_HCI_I2C_LLC_MAX_PAYLOAD, &phy->hdev);
426 if (r < 0)
427 goto err_hci;
428
429 return 0;
430
431err_hci:
432 free_irq(client->irq, phy);
433
434err_rti:
435 if (pdata->free_resources != NULL)
436 pdata->free_resources();
437
438err_pdata:
439 kfree(phy);
440
441err_phy_alloc:
442 return r;
443}
444
445static __devexit int pn544_hci_i2c_remove(struct i2c_client *client)
446{
447 struct pn544_i2c_phy *phy = i2c_get_clientdata(client);
448 struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
449
450 dev_dbg(&client->dev, "%s\n", __func__);
451
452 pn544_hci_remove(phy->hdev);
453
454 if (phy->powered)
455 pn544_hci_i2c_disable(phy);
456
457 free_irq(client->irq, phy);
458 if (pdata->free_resources)
459 pdata->free_resources();
460
461 kfree(phy);
462
463 return 0;
464}
465
466static struct i2c_driver pn544_hci_i2c_driver = {
467 .driver = {
468 .name = PN544_HCI_I2C_DRIVER_NAME,
469 },
470 .probe = pn544_hci_i2c_probe,
471 .id_table = pn544_hci_i2c_id_table,
472 .remove = __devexit_p(pn544_hci_i2c_remove),
473};
474
475static int __init pn544_hci_i2c_init(void)
476{
477 int r;
478
479 pr_debug(DRIVER_DESC ": %s\n", __func__);
480
481 r = i2c_add_driver(&pn544_hci_i2c_driver);
482 if (r) {
483 pr_err(PN544_HCI_I2C_DRIVER_NAME
484 ": driver registration failed\n");
485 return r;
486 }
487
488 return 0;
489}
490
491static void __exit pn544_hci_i2c_exit(void)
492{
493 i2c_del_driver(&pn544_hci_i2c_driver);
494}
495
496module_init(pn544_hci_i2c_init);
497module_exit(pn544_hci_i2c_exit);
498
499MODULE_LICENSE("GPL");
500MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/nfc/pn544_hci.c b/drivers/nfc/pn544/pn544.c
index 70858b5f81e4..cf344450815c 100644
--- a/drivers/nfc/pn544_hci.c
+++ b/drivers/nfc/pn544/pn544.c
@@ -18,47 +18,21 @@
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */ 19 */
20 20
21#include <linux/crc-ccitt.h>
22#include <linux/module.h>
23#include <linux/delay.h> 21#include <linux/delay.h>
24#include <linux/slab.h> 22#include <linux/slab.h>
25#include <linux/miscdevice.h>
26#include <linux/interrupt.h>
27#include <linux/gpio.h>
28#include <linux/i2c.h>
29 23
30#include <linux/nfc.h> 24#include <linux/nfc.h>
31#include <net/nfc/hci.h> 25#include <net/nfc/hci.h>
32#include <net/nfc/llc.h> 26#include <net/nfc/llc.h>
33 27
34#include <linux/nfc/pn544.h> 28#include "pn544.h"
35
36#define DRIVER_DESC "HCI NFC driver for PN544"
37
38#define PN544_HCI_DRIVER_NAME "pn544_hci"
39 29
40/* Timing restrictions (ms) */ 30/* Timing restrictions (ms) */
41#define PN544_HCI_RESETVEN_TIME 30 31#define PN544_HCI_RESETVEN_TIME 30
42 32
43static struct i2c_device_id pn544_hci_id_table[] = {
44 {"pn544", 0},
45 {}
46};
47
48MODULE_DEVICE_TABLE(i2c, pn544_hci_id_table);
49
50#define HCI_MODE 0 33#define HCI_MODE 0
51#define FW_MODE 1 34#define FW_MODE 1
52 35
53/* framing in HCI mode */
54#define PN544_HCI_LLC_LEN 1
55#define PN544_HCI_LLC_CRC 2
56#define PN544_HCI_LLC_LEN_CRC (PN544_HCI_LLC_LEN + PN544_HCI_LLC_CRC)
57#define PN544_HCI_LLC_MIN_SIZE (1 + PN544_HCI_LLC_LEN_CRC)
58#define PN544_HCI_LLC_MAX_PAYLOAD 29
59#define PN544_HCI_LLC_MAX_SIZE (PN544_HCI_LLC_LEN_CRC + 1 + \
60 PN544_HCI_LLC_MAX_PAYLOAD)
61
62enum pn544_state { 36enum pn544_state {
63 PN544_ST_COLD, 37 PN544_ST_COLD,
64 PN544_ST_FW_READY, 38 PN544_ST_FW_READY,
@@ -141,259 +115,22 @@ static struct nfc_hci_gate pn544_gates[] = {
141 115
142/* Largest headroom needed for outgoing custom commands */ 116/* Largest headroom needed for outgoing custom commands */
143#define PN544_CMDS_HEADROOM 2 117#define PN544_CMDS_HEADROOM 2
144#define PN544_FRAME_HEADROOM 1
145#define PN544_FRAME_TAILROOM 2
146 118
147struct pn544_hci_info { 119struct pn544_hci_info {
148 struct i2c_client *i2c_dev; 120 struct nfc_phy_ops *phy_ops;
121 void *phy_id;
122
149 struct nfc_hci_dev *hdev; 123 struct nfc_hci_dev *hdev;
150 124
151 enum pn544_state state; 125 enum pn544_state state;
152 126
153 struct mutex info_lock; 127 struct mutex info_lock;
154 128
155 unsigned int gpio_en;
156 unsigned int gpio_irq;
157 unsigned int gpio_fw;
158 unsigned int en_polarity;
159
160 int hard_fault; /*
161 * < 0 if hardware error occured (e.g. i2c err)
162 * and prevents normal operation.
163 */
164 int async_cb_type; 129 int async_cb_type;
165 data_exchange_cb_t async_cb; 130 data_exchange_cb_t async_cb;
166 void *async_cb_context; 131 void *async_cb_context;
167}; 132};
168 133
169static void pn544_hci_platform_init(struct pn544_hci_info *info)
170{
171 int polarity, retry, ret;
172 char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
173 int count = sizeof(rset_cmd);
174
175 pr_info(DRIVER_DESC ": %s\n", __func__);
176 dev_info(&info->i2c_dev->dev, "Detecting nfc_en polarity\n");
177
178 /* Disable fw download */
179 gpio_set_value(info->gpio_fw, 0);
180
181 for (polarity = 0; polarity < 2; polarity++) {
182 info->en_polarity = polarity;
183 retry = 3;
184 while (retry--) {
185 /* power off */
186 gpio_set_value(info->gpio_en, !info->en_polarity);
187 usleep_range(10000, 15000);
188
189 /* power on */
190 gpio_set_value(info->gpio_en, info->en_polarity);
191 usleep_range(10000, 15000);
192
193 /* send reset */
194 dev_dbg(&info->i2c_dev->dev, "Sending reset cmd\n");
195 ret = i2c_master_send(info->i2c_dev, rset_cmd, count);
196 if (ret == count) {
197 dev_info(&info->i2c_dev->dev,
198 "nfc_en polarity : active %s\n",
199 (polarity == 0 ? "low" : "high"));
200 goto out;
201 }
202 }
203 }
204
205 dev_err(&info->i2c_dev->dev,
206 "Could not detect nfc_en polarity, fallback to active high\n");
207
208out:
209 gpio_set_value(info->gpio_en, !info->en_polarity);
210}
211
212static int pn544_hci_enable(struct pn544_hci_info *info, int mode)
213{
214 pr_info(DRIVER_DESC ": %s\n", __func__);
215
216 gpio_set_value(info->gpio_fw, 0);
217 gpio_set_value(info->gpio_en, info->en_polarity);
218 usleep_range(10000, 15000);
219
220 return 0;
221}
222
223static void pn544_hci_disable(struct pn544_hci_info *info)
224{
225 pr_info(DRIVER_DESC ": %s\n", __func__);
226
227 gpio_set_value(info->gpio_fw, 0);
228 gpio_set_value(info->gpio_en, !info->en_polarity);
229 usleep_range(10000, 15000);
230
231 gpio_set_value(info->gpio_en, info->en_polarity);
232 usleep_range(10000, 15000);
233
234 gpio_set_value(info->gpio_en, !info->en_polarity);
235 usleep_range(10000, 15000);
236}
237
238static int pn544_hci_i2c_write(struct i2c_client *client, u8 *buf, int len)
239{
240 int r;
241
242 usleep_range(3000, 6000);
243
244 r = i2c_master_send(client, buf, len);
245
246 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
247 usleep_range(6000, 10000);
248 r = i2c_master_send(client, buf, len);
249 }
250
251 if (r >= 0) {
252 if (r != len)
253 return -EREMOTEIO;
254 else
255 return 0;
256 }
257
258 return r;
259}
260
261static int check_crc(u8 *buf, int buflen)
262{
263 int len;
264 u16 crc;
265
266 len = buf[0] + 1;
267 crc = crc_ccitt(0xffff, buf, len - 2);
268 crc = ~crc;
269
270 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
271 pr_err(PN544_HCI_DRIVER_NAME ": CRC error 0x%x != 0x%x 0x%x\n",
272 crc, buf[len - 1], buf[len - 2]);
273
274 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
275 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
276 16, 2, buf, buflen, false);
277 return -EPERM;
278 }
279 return 0;
280}
281
282/*
283 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
284 * that i2c bus will be flushed and that next read will start on a new frame.
285 * returned skb contains only LLC header and payload.
286 * returns:
287 * -EREMOTEIO : i2c read error (fatal)
288 * -EBADMSG : frame was incorrect and discarded
289 * -ENOMEM : cannot allocate skb, frame dropped
290 */
291static int pn544_hci_i2c_read(struct i2c_client *client, struct sk_buff **skb)
292{
293 int r;
294 u8 len;
295 u8 tmp[PN544_HCI_LLC_MAX_SIZE - 1];
296
297 r = i2c_master_recv(client, &len, 1);
298 if (r != 1) {
299 dev_err(&client->dev, "cannot read len byte\n");
300 return -EREMOTEIO;
301 }
302
303 if ((len < (PN544_HCI_LLC_MIN_SIZE - 1)) ||
304 (len > (PN544_HCI_LLC_MAX_SIZE - 1))) {
305 dev_err(&client->dev, "invalid len byte\n");
306 r = -EBADMSG;
307 goto flush;
308 }
309
310 *skb = alloc_skb(1 + len, GFP_KERNEL);
311 if (*skb == NULL) {
312 r = -ENOMEM;
313 goto flush;
314 }
315
316 *skb_put(*skb, 1) = len;
317
318 r = i2c_master_recv(client, skb_put(*skb, len), len);
319 if (r != len) {
320 kfree_skb(*skb);
321 return -EREMOTEIO;
322 }
323
324 r = check_crc((*skb)->data, (*skb)->len);
325 if (r != 0) {
326 kfree_skb(*skb);
327 r = -EBADMSG;
328 goto flush;
329 }
330
331 skb_pull(*skb, 1);
332 skb_trim(*skb, (*skb)->len - 2);
333
334 usleep_range(3000, 6000);
335
336 return 0;
337
338flush:
339 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
340 r = -EREMOTEIO;
341
342 usleep_range(3000, 6000);
343
344 return r;
345}
346
347/*
348 * Reads an shdlc frame from the chip. This is not as straightforward as it
349 * seems. There are cases where we could loose the frame start synchronization.
350 * The frame format is len-data-crc, and corruption can occur anywhere while
351 * transiting on i2c bus, such that we could read an invalid len.
352 * In order to recover synchronization with the next frame, we must be sure
353 * to read the real amount of data without using the len byte. We do this by
354 * assuming the following:
355 * - the chip will always present only one single complete frame on the bus
356 * before triggering the interrupt
357 * - the chip will not present a new frame until we have completely read
358 * the previous one (or until we have handled the interrupt).
359 * The tricky case is when we read a corrupted len that is less than the real
360 * len. We must detect this here in order to determine that we need to flush
361 * the bus. This is the reason why we check the crc here.
362 */
363static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
364{
365 struct pn544_hci_info *info = dev_id;
366 struct i2c_client *client;
367 struct sk_buff *skb = NULL;
368 int r;
369
370 if (!info || irq != info->i2c_dev->irq) {
371 WARN_ON_ONCE(1);
372 return IRQ_NONE;
373 }
374
375 client = info->i2c_dev;
376 dev_dbg(&client->dev, "IRQ\n");
377
378 if (info->hard_fault != 0)
379 return IRQ_HANDLED;
380
381 r = pn544_hci_i2c_read(client, &skb);
382 if (r == -EREMOTEIO) {
383 info->hard_fault = r;
384
385 nfc_hci_recv_frame(info->hdev, NULL);
386
387 return IRQ_HANDLED;
388 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
389 return IRQ_HANDLED;
390 }
391
392 nfc_hci_recv_frame(info->hdev, skb);
393
394 return IRQ_HANDLED;
395}
396
397static int pn544_hci_open(struct nfc_hci_dev *hdev) 134static int pn544_hci_open(struct nfc_hci_dev *hdev)
398{ 135{
399 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev); 136 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
@@ -406,7 +143,7 @@ static int pn544_hci_open(struct nfc_hci_dev *hdev)
406 goto out; 143 goto out;
407 } 144 }
408 145
409 r = pn544_hci_enable(info, HCI_MODE); 146 r = info->phy_ops->enable(info->phy_id);
410 147
411 if (r == 0) 148 if (r == 0)
412 info->state = PN544_ST_READY; 149 info->state = PN544_ST_READY;
@@ -425,7 +162,7 @@ static void pn544_hci_close(struct nfc_hci_dev *hdev)
425 if (info->state == PN544_ST_COLD) 162 if (info->state == PN544_ST_COLD)
426 goto out; 163 goto out;
427 164
428 pn544_hci_disable(info); 165 info->phy_ops->disable(info->phy_id);
429 166
430 info->state = PN544_ST_COLD; 167 info->state = PN544_ST_COLD;
431 168
@@ -600,40 +337,11 @@ static int pn544_hci_ready(struct nfc_hci_dev *hdev)
600 return 0; 337 return 0;
601} 338}
602 339
603static void pn544_hci_add_len_crc(struct sk_buff *skb)
604{
605 u16 crc;
606 int len;
607
608 len = skb->len + 2;
609 *skb_push(skb, 1) = len;
610
611 crc = crc_ccitt(0xffff, skb->data, skb->len);
612 crc = ~crc;
613 *skb_put(skb, 1) = crc & 0xff;
614 *skb_put(skb, 1) = crc >> 8;
615}
616
617static void pn544_hci_remove_len_crc(struct sk_buff *skb)
618{
619 skb_pull(skb, PN544_FRAME_HEADROOM);
620 skb_trim(skb, PN544_FRAME_TAILROOM);
621}
622
623static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb) 340static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
624{ 341{
625 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev); 342 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
626 struct i2c_client *client = info->i2c_dev;
627 int r;
628
629 if (info->hard_fault != 0)
630 return info->hard_fault;
631
632 pn544_hci_add_len_crc(skb);
633 r = pn544_hci_i2c_write(client, skb->data, skb->len);
634 pn544_hci_remove_len_crc(skb);
635 343
636 return r; 344 return info->phy_ops->write(info->phy_id, skb);
637} 345}
638 346
639static int pn544_hci_start_poll(struct nfc_hci_dev *hdev, 347static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
@@ -1076,68 +784,26 @@ static struct nfc_hci_ops pn544_hci_ops = {
1076 .event_received = pn544_hci_event_received, 784 .event_received = pn544_hci_event_received,
1077}; 785};
1078 786
1079static int __devinit pn544_hci_probe(struct i2c_client *client, 787int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
1080 const struct i2c_device_id *id) 788 int phy_headroom, int phy_tailroom, int phy_payload,
789 struct nfc_hci_dev **hdev)
1081{ 790{
1082 struct pn544_hci_info *info; 791 struct pn544_hci_info *info;
1083 struct pn544_nfc_platform_data *pdata;
1084 int r = 0;
1085 u32 protocols; 792 u32 protocols;
1086 struct nfc_hci_init_data init_data; 793 struct nfc_hci_init_data init_data;
1087 794 int r;
1088 dev_dbg(&client->dev, "%s\n", __func__);
1089 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
1090
1091 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1092 dev_err(&client->dev, "Need I2C_FUNC_I2C\n");
1093 return -ENODEV;
1094 }
1095 795
1096 info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL); 796 info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
1097 if (!info) { 797 if (!info) {
1098 dev_err(&client->dev, 798 pr_err("Cannot allocate memory for pn544_hci_info.\n");
1099 "Cannot allocate memory for pn544_hci_info.\n");
1100 r = -ENOMEM; 799 r = -ENOMEM;
1101 goto err_info_alloc; 800 goto err_info_alloc;
1102 } 801 }
1103 802
1104 info->i2c_dev = client; 803 info->phy_ops = phy_ops;
804 info->phy_id = phy_id;
1105 info->state = PN544_ST_COLD; 805 info->state = PN544_ST_COLD;
1106 mutex_init(&info->info_lock); 806 mutex_init(&info->info_lock);
1107 i2c_set_clientdata(client, info);
1108
1109 pdata = client->dev.platform_data;
1110 if (pdata == NULL) {
1111 dev_err(&client->dev, "No platform data\n");
1112 r = -EINVAL;
1113 goto err_pdata;
1114 }
1115
1116 if (pdata->request_resources == NULL) {
1117 dev_err(&client->dev, "request_resources() missing\n");
1118 r = -EINVAL;
1119 goto err_pdata;
1120 }
1121
1122 r = pdata->request_resources(client);
1123 if (r) {
1124 dev_err(&client->dev, "Cannot get platform resources\n");
1125 goto err_pdata;
1126 }
1127
1128 info->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
1129 info->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
1130 info->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
1131
1132 pn544_hci_platform_init(info);
1133
1134 r = request_threaded_irq(client->irq, NULL, pn544_hci_irq_thread_fn,
1135 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1136 PN544_HCI_DRIVER_NAME, info);
1137 if (r < 0) {
1138 dev_err(&client->dev, "Unable to register IRQ handler\n");
1139 goto err_rti;
1140 }
1141 807
1142 init_data.gate_count = ARRAY_SIZE(pn544_gates); 808 init_data.gate_count = ARRAY_SIZE(pn544_gates);
1143 809
@@ -1157,13 +823,11 @@ static int __devinit pn544_hci_probe(struct i2c_client *client,
1157 NFC_PROTO_NFC_DEP_MASK; 823 NFC_PROTO_NFC_DEP_MASK;
1158 824
1159 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data, 825 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data,
1160 protocols, LLC_SHDLC_NAME, 826 protocols, llc_name,
1161 PN544_FRAME_HEADROOM + 827 phy_headroom + PN544_CMDS_HEADROOM,
1162 PN544_CMDS_HEADROOM, 828 phy_tailroom, phy_payload);
1163 PN544_FRAME_TAILROOM,
1164 PN544_HCI_LLC_MAX_PAYLOAD);
1165 if (!info->hdev) { 829 if (!info->hdev) {
1166 dev_err(&client->dev, "Cannot allocate nfc hdev.\n"); 830 pr_err("Cannot allocate nfc hdev.\n");
1167 r = -ENOMEM; 831 r = -ENOMEM;
1168 goto err_alloc_hdev; 832 goto err_alloc_hdev;
1169 } 833 }
@@ -1174,79 +838,25 @@ static int __devinit pn544_hci_probe(struct i2c_client *client,
1174 if (r) 838 if (r)
1175 goto err_regdev; 839 goto err_regdev;
1176 840
841 *hdev = info->hdev;
842
1177 return 0; 843 return 0;
1178 844
1179err_regdev: 845err_regdev:
1180 nfc_hci_free_device(info->hdev); 846 nfc_hci_free_device(info->hdev);
1181 847
1182err_alloc_hdev: 848err_alloc_hdev:
1183 free_irq(client->irq, info);
1184
1185err_rti:
1186 if (pdata->free_resources != NULL)
1187 pdata->free_resources();
1188
1189err_pdata:
1190 kfree(info); 849 kfree(info);
1191 850
1192err_info_alloc: 851err_info_alloc:
1193 return r; 852 return r;
1194} 853}
1195 854
1196static __devexit int pn544_hci_remove(struct i2c_client *client) 855void pn544_hci_remove(struct nfc_hci_dev *hdev)
1197{ 856{
1198 struct pn544_hci_info *info = i2c_get_clientdata(client); 857 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
1199 struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
1200
1201 dev_dbg(&client->dev, "%s\n", __func__);
1202
1203 nfc_hci_free_device(info->hdev);
1204
1205 if (info->state != PN544_ST_COLD) {
1206 if (pdata->disable)
1207 pdata->disable();
1208 }
1209
1210 free_irq(client->irq, info);
1211 if (pdata->free_resources)
1212 pdata->free_resources();
1213 858
859 nfc_hci_unregister_device(hdev);
860 nfc_hci_free_device(hdev);
1214 kfree(info); 861 kfree(info);
1215
1216 return 0;
1217}
1218
1219static struct i2c_driver pn544_hci_driver = {
1220 .driver = {
1221 .name = PN544_HCI_DRIVER_NAME,
1222 },
1223 .probe = pn544_hci_probe,
1224 .id_table = pn544_hci_id_table,
1225 .remove = __devexit_p(pn544_hci_remove),
1226};
1227
1228static int __init pn544_hci_init(void)
1229{
1230 int r;
1231
1232 pr_debug(DRIVER_DESC ": %s\n", __func__);
1233
1234 r = i2c_add_driver(&pn544_hci_driver);
1235 if (r) {
1236 pr_err(PN544_HCI_DRIVER_NAME ": driver registration failed\n");
1237 return r;
1238 }
1239
1240 return 0;
1241} 862}
1242
1243static void __exit pn544_hci_exit(void)
1244{
1245 i2c_del_driver(&pn544_hci_driver);
1246}
1247
1248module_init(pn544_hci_init);
1249module_exit(pn544_hci_exit);
1250
1251MODULE_LICENSE("GPL");
1252MODULE_DESCRIPTION(DRIVER_DESC);
diff --git a/drivers/nfc/pn544/pn544.h b/drivers/nfc/pn544/pn544.h
new file mode 100644
index 000000000000..f47c6454914b
--- /dev/null
+++ b/drivers/nfc/pn544/pn544.h
@@ -0,0 +1,32 @@
1/*
2 * Copyright (C) 2011 - 2012 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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, write to the
16 * Free Software Foundation, Inc.,
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20#ifndef __LOCAL_PN544_H_
21#define __LOCAL_PN544_H_
22
23#include <net/nfc/hci.h>
24
25#define DRIVER_DESC "HCI NFC driver for PN544"
26
27int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
28 int phy_headroom, int phy_tailroom, int phy_payload,
29 struct nfc_hci_dev **hdev);
30void pn544_hci_remove(struct nfc_hci_dev *hdev);
31
32#endif /* __LOCAL_PN544_H_ */
diff --git a/include/net/nfc/hci.h b/include/net/nfc/hci.h
index bc87b8f2d692..639f50af42df 100644
--- a/include/net/nfc/hci.h
+++ b/include/net/nfc/hci.h
@@ -24,6 +24,12 @@
24 24
25#include <net/nfc/nfc.h> 25#include <net/nfc/nfc.h>
26 26
27struct nfc_phy_ops {
28 int (*write)(void *dev_id, struct sk_buff *skb);
29 int (*enable)(void *dev_id);
30 void (*disable)(void *dev_id);
31};
32
27struct nfc_hci_dev; 33struct nfc_hci_dev;
28 34
29struct nfc_hci_ops { 35struct nfc_hci_ops {