blob: 8b21a8efd7516bca575a91e3da75965e3d35148f [file] [log] [blame]
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001/*
2 * HCI based Driver for NXP PN544 NFC Chip
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/delay.h>
24#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
30#include <linux/nfc.h>
31#include <net/nfc/hci.h>
Eric Lapuyade412fda52012-09-18 19:45:48 +020032#include <net/nfc/llc.h>
Eric Lapuyadebbed0de2012-05-07 12:31:29 +020033
34#include <linux/nfc/pn544.h>
35
36#define DRIVER_DESC "HCI NFC driver for PN544"
37
38#define PN544_HCI_DRIVER_NAME "pn544_hci"
39
40/* Timing restrictions (ms) */
41#define PN544_HCI_RESETVEN_TIME 30
42
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
51#define FW_MODE 1
52
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 {
63 PN544_ST_COLD,
64 PN544_ST_FW_READY,
65 PN544_ST_READY,
66};
67
68#define FULL_VERSION_LEN 11
69
70/* Proprietary commands */
71#define PN544_WRITE 0x3f
72
73/* Proprietary gates, events, commands and registers */
74
75/* NFC_HCI_RF_READER_A_GATE additional registers and commands */
76#define PN544_RF_READER_A_AUTO_ACTIVATION 0x10
77#define PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION 0x12
78#define PN544_MIFARE_CMD 0x21
79
80/* Commands that apply to all RF readers */
81#define PN544_RF_READER_CMD_PRESENCE_CHECK 0x30
82#define PN544_RF_READER_CMD_ACTIVATE_NEXT 0x32
83
84/* NFC_HCI_ID_MGMT_GATE additional registers */
85#define PN544_ID_MGMT_FULL_VERSION_SW 0x10
86
87#define PN544_RF_READER_ISO15693_GATE 0x12
88
89#define PN544_RF_READER_F_GATE 0x14
90#define PN544_FELICA_ID 0x04
91#define PN544_FELICA_RAW 0x20
92
93#define PN544_RF_READER_JEWEL_GATE 0x15
94#define PN544_JEWEL_RAW_CMD 0x23
95
96#define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30
97#define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31
98
99#define PN544_SYS_MGMT_GATE 0x90
100#define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02
101
102#define PN544_POLLING_LOOP_MGMT_GATE 0x94
Arron Wang7e2afc92012-09-27 17:32:54 +0800103#define PN544_DEP_MODE 0x01
104#define PN544_DEP_ATR_REQ 0x02
105#define PN544_DEP_ATR_RES 0x03
106#define PN544_DEP_MERGE 0x0D
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200107#define PN544_PL_RDPHASES 0x06
108#define PN544_PL_EMULATION 0x07
109#define PN544_PL_NFCT_DEACTIVATED 0x09
110
111#define PN544_SWP_MGMT_GATE 0xA0
112
113#define PN544_NFC_WI_MGMT_GATE 0xA1
114
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200115static struct nfc_hci_gate pn544_gates[] = {
116 {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE},
117 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
118 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
119 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE},
120 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
121 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
122 {PN544_SYS_MGMT_GATE, NFC_HCI_INVALID_PIPE},
123 {PN544_SWP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
124 {PN544_POLLING_LOOP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
125 {PN544_NFC_WI_MGMT_GATE, NFC_HCI_INVALID_PIPE},
126 {PN544_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
127 {PN544_RF_READER_JEWEL_GATE, NFC_HCI_INVALID_PIPE},
128 {PN544_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
129 {PN544_RF_READER_NFCIP1_INITIATOR_GATE, NFC_HCI_INVALID_PIPE},
130 {PN544_RF_READER_NFCIP1_TARGET_GATE, NFC_HCI_INVALID_PIPE}
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200131};
132
133/* Largest headroom needed for outgoing custom commands */
134#define PN544_CMDS_HEADROOM 2
Waldemar Rymarkiewiczade67202012-09-07 11:08:29 +0200135#define PN544_FRAME_HEADROOM 1
136#define PN544_FRAME_TAILROOM 2
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200137
138struct pn544_hci_info {
139 struct i2c_client *i2c_dev;
Eric Lapuyade412fda52012-09-18 19:45:48 +0200140 struct nfc_hci_dev *hdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200141
142 enum pn544_state state;
143
144 struct mutex info_lock;
145
146 unsigned int gpio_en;
147 unsigned int gpio_irq;
148 unsigned int gpio_fw;
149 unsigned int en_polarity;
150
151 int hard_fault; /*
152 * < 0 if hardware error occured (e.g. i2c err)
153 * and prevents normal operation.
154 */
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200155 int async_cb_type;
156 data_exchange_cb_t async_cb;
157 void *async_cb_context;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200158};
159
160static void pn544_hci_platform_init(struct pn544_hci_info *info)
161{
162 int polarity, retry, ret;
163 char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
164 int count = sizeof(rset_cmd);
165
166 pr_info(DRIVER_DESC ": %s\n", __func__);
167 dev_info(&info->i2c_dev->dev, "Detecting nfc_en polarity\n");
168
169 /* Disable fw download */
170 gpio_set_value(info->gpio_fw, 0);
171
172 for (polarity = 0; polarity < 2; polarity++) {
173 info->en_polarity = polarity;
174 retry = 3;
175 while (retry--) {
176 /* power off */
177 gpio_set_value(info->gpio_en, !info->en_polarity);
178 usleep_range(10000, 15000);
179
180 /* power on */
181 gpio_set_value(info->gpio_en, info->en_polarity);
182 usleep_range(10000, 15000);
183
184 /* send reset */
185 dev_dbg(&info->i2c_dev->dev, "Sending reset cmd\n");
186 ret = i2c_master_send(info->i2c_dev, rset_cmd, count);
187 if (ret == count) {
188 dev_info(&info->i2c_dev->dev,
189 "nfc_en polarity : active %s\n",
190 (polarity == 0 ? "low" : "high"));
191 goto out;
192 }
193 }
194 }
195
196 dev_err(&info->i2c_dev->dev,
197 "Could not detect nfc_en polarity, fallback to active high\n");
198
199out:
200 gpio_set_value(info->gpio_en, !info->en_polarity);
201}
202
203static int pn544_hci_enable(struct pn544_hci_info *info, int mode)
204{
205 pr_info(DRIVER_DESC ": %s\n", __func__);
206
207 gpio_set_value(info->gpio_fw, 0);
208 gpio_set_value(info->gpio_en, info->en_polarity);
209 usleep_range(10000, 15000);
210
211 return 0;
212}
213
214static void pn544_hci_disable(struct pn544_hci_info *info)
215{
216 pr_info(DRIVER_DESC ": %s\n", __func__);
217
218 gpio_set_value(info->gpio_fw, 0);
219 gpio_set_value(info->gpio_en, !info->en_polarity);
220 usleep_range(10000, 15000);
221
222 gpio_set_value(info->gpio_en, info->en_polarity);
223 usleep_range(10000, 15000);
224
225 gpio_set_value(info->gpio_en, !info->en_polarity);
226 usleep_range(10000, 15000);
227}
228
229static int pn544_hci_i2c_write(struct i2c_client *client, u8 *buf, int len)
230{
231 int r;
232
233 usleep_range(3000, 6000);
234
235 r = i2c_master_send(client, buf, len);
236
237 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
238 usleep_range(6000, 10000);
239 r = i2c_master_send(client, buf, len);
240 }
241
Waldemar Rymarkiewicz96e32402012-09-20 08:59:10 +0200242 if (r >= 0) {
243 if (r != len)
244 return -EREMOTEIO;
245 else
246 return 0;
247 }
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200248
249 return r;
250}
251
252static int check_crc(u8 *buf, int buflen)
253{
Dan Carpenter885ba1d2012-05-18 10:36:47 +0300254 int len;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200255 u16 crc;
256
257 len = buf[0] + 1;
258 crc = crc_ccitt(0xffff, buf, len - 2);
259 crc = ~crc;
260
261 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
262 pr_err(PN544_HCI_DRIVER_NAME ": CRC error 0x%x != 0x%x 0x%x\n",
263 crc, buf[len - 1], buf[len - 2]);
264
265 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
266 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
267 16, 2, buf, buflen, false);
268 return -EPERM;
269 }
270 return 0;
271}
272
273/*
274 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
275 * that i2c bus will be flushed and that next read will start on a new frame.
276 * returned skb contains only LLC header and payload.
277 * returns:
278 * -EREMOTEIO : i2c read error (fatal)
279 * -EBADMSG : frame was incorrect and discarded
280 * -ENOMEM : cannot allocate skb, frame dropped
281 */
282static int pn544_hci_i2c_read(struct i2c_client *client, struct sk_buff **skb)
283{
284 int r;
285 u8 len;
286 u8 tmp[PN544_HCI_LLC_MAX_SIZE - 1];
287
288 r = i2c_master_recv(client, &len, 1);
289 if (r != 1) {
290 dev_err(&client->dev, "cannot read len byte\n");
291 return -EREMOTEIO;
292 }
293
294 if ((len < (PN544_HCI_LLC_MIN_SIZE - 1)) ||
295 (len > (PN544_HCI_LLC_MAX_SIZE - 1))) {
296 dev_err(&client->dev, "invalid len byte\n");
297 r = -EBADMSG;
298 goto flush;
299 }
300
301 *skb = alloc_skb(1 + len, GFP_KERNEL);
302 if (*skb == NULL) {
303 r = -ENOMEM;
304 goto flush;
305 }
306
307 *skb_put(*skb, 1) = len;
308
309 r = i2c_master_recv(client, skb_put(*skb, len), len);
310 if (r != len) {
311 kfree_skb(*skb);
312 return -EREMOTEIO;
313 }
314
315 r = check_crc((*skb)->data, (*skb)->len);
316 if (r != 0) {
317 kfree_skb(*skb);
318 r = -EBADMSG;
319 goto flush;
320 }
321
322 skb_pull(*skb, 1);
323 skb_trim(*skb, (*skb)->len - 2);
324
325 usleep_range(3000, 6000);
326
327 return 0;
328
329flush:
330 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
331 r = -EREMOTEIO;
332
333 usleep_range(3000, 6000);
334
335 return r;
336}
337
338/*
339 * Reads an shdlc frame from the chip. This is not as straightforward as it
340 * seems. There are cases where we could loose the frame start synchronization.
341 * The frame format is len-data-crc, and corruption can occur anywhere while
342 * transiting on i2c bus, such that we could read an invalid len.
343 * In order to recover synchronization with the next frame, we must be sure
344 * to read the real amount of data without using the len byte. We do this by
345 * assuming the following:
346 * - the chip will always present only one single complete frame on the bus
347 * before triggering the interrupt
348 * - the chip will not present a new frame until we have completely read
349 * the previous one (or until we have handled the interrupt).
350 * The tricky case is when we read a corrupted len that is less than the real
351 * len. We must detect this here in order to determine that we need to flush
352 * the bus. This is the reason why we check the crc here.
353 */
354static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
355{
356 struct pn544_hci_info *info = dev_id;
Wei Yongjunc51ad342012-09-08 21:19:44 +0800357 struct i2c_client *client;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200358 struct sk_buff *skb = NULL;
359 int r;
360
Wei Yongjunc51ad342012-09-08 21:19:44 +0800361 if (!info || irq != info->i2c_dev->irq) {
362 WARN_ON_ONCE(1);
363 return IRQ_NONE;
364 }
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200365
Wei Yongjunc51ad342012-09-08 21:19:44 +0800366 client = info->i2c_dev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200367 dev_dbg(&client->dev, "IRQ\n");
368
369 if (info->hard_fault != 0)
370 return IRQ_HANDLED;
371
372 r = pn544_hci_i2c_read(client, &skb);
373 if (r == -EREMOTEIO) {
374 info->hard_fault = r;
375
Eric Lapuyade412fda52012-09-18 19:45:48 +0200376 nfc_hci_recv_frame(info->hdev, NULL);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200377
378 return IRQ_HANDLED;
379 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
380 return IRQ_HANDLED;
381 }
382
Eric Lapuyade412fda52012-09-18 19:45:48 +0200383 nfc_hci_recv_frame(info->hdev, skb);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200384
385 return IRQ_HANDLED;
386}
387
Eric Lapuyade412fda52012-09-18 19:45:48 +0200388static int pn544_hci_open(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200389{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200390 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200391 int r = 0;
392
393 mutex_lock(&info->info_lock);
394
395 if (info->state != PN544_ST_COLD) {
396 r = -EBUSY;
397 goto out;
398 }
399
400 r = pn544_hci_enable(info, HCI_MODE);
401
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200402 if (r == 0)
403 info->state = PN544_ST_READY;
404
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200405out:
406 mutex_unlock(&info->info_lock);
407 return r;
408}
409
Eric Lapuyade412fda52012-09-18 19:45:48 +0200410static void pn544_hci_close(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200411{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200412 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200413
414 mutex_lock(&info->info_lock);
415
416 if (info->state == PN544_ST_COLD)
417 goto out;
418
419 pn544_hci_disable(info);
420
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200421 info->state = PN544_ST_COLD;
422
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200423out:
424 mutex_unlock(&info->info_lock);
425}
426
Eric Lapuyade412fda52012-09-18 19:45:48 +0200427static int pn544_hci_ready(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200428{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200429 struct sk_buff *skb;
430 static struct hw_config {
431 u8 adr[2];
432 u8 value;
433 } hw_config[] = {
434 {{0x9f, 0x9a}, 0x00},
435
436 {{0x98, 0x10}, 0xbc},
437
438 {{0x9e, 0x71}, 0x00},
439
440 {{0x98, 0x09}, 0x00},
441
442 {{0x9e, 0xb4}, 0x00},
443
444 {{0x9e, 0xd9}, 0xff},
445 {{0x9e, 0xda}, 0xff},
446 {{0x9e, 0xdb}, 0x23},
447 {{0x9e, 0xdc}, 0x21},
448 {{0x9e, 0xdd}, 0x22},
449 {{0x9e, 0xde}, 0x24},
450
451 {{0x9c, 0x01}, 0x08},
452
453 {{0x9e, 0xaa}, 0x01},
454
455 {{0x9b, 0xd1}, 0x0d},
456 {{0x9b, 0xd2}, 0x24},
457 {{0x9b, 0xd3}, 0x0a},
458 {{0x9b, 0xd4}, 0x22},
459 {{0x9b, 0xd5}, 0x08},
460 {{0x9b, 0xd6}, 0x1e},
461 {{0x9b, 0xdd}, 0x1c},
462
463 {{0x9b, 0x84}, 0x13},
464 {{0x99, 0x81}, 0x7f},
465 {{0x99, 0x31}, 0x70},
466
467 {{0x98, 0x00}, 0x3f},
468
469 {{0x9f, 0x09}, 0x00},
470
471 {{0x9f, 0x0a}, 0x05},
472
473 {{0x9e, 0xd1}, 0xa1},
474 {{0x99, 0x23}, 0x00},
475
476 {{0x9e, 0x74}, 0x80},
477
478 {{0x9f, 0x28}, 0x10},
479
480 {{0x9f, 0x35}, 0x14},
481
482 {{0x9f, 0x36}, 0x60},
483
484 {{0x9c, 0x31}, 0x00},
485
486 {{0x9c, 0x32}, 0xc8},
487
488 {{0x9c, 0x19}, 0x40},
489
490 {{0x9c, 0x1a}, 0x40},
491
492 {{0x9c, 0x0c}, 0x00},
493
494 {{0x9c, 0x0d}, 0x00},
495
496 {{0x9c, 0x12}, 0x00},
497
498 {{0x9c, 0x13}, 0x00},
499
500 {{0x98, 0xa2}, 0x0e},
501
502 {{0x98, 0x93}, 0x40},
503
504 {{0x98, 0x7d}, 0x02},
505 {{0x98, 0x7e}, 0x00},
506 {{0x9f, 0xc8}, 0x01},
507 };
508 struct hw_config *p = hw_config;
509 int count = ARRAY_SIZE(hw_config);
510 struct sk_buff *res_skb;
511 u8 param[4];
512 int r;
513
514 param[0] = 0;
515 while (count--) {
516 param[1] = p->adr[0];
517 param[2] = p->adr[1];
518 param[3] = p->value;
519
520 r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_WRITE,
521 param, 4, &res_skb);
522 if (r < 0)
523 return r;
524
525 if (res_skb->len != 1) {
526 kfree_skb(res_skb);
527 return -EPROTO;
528 }
529
530 if (res_skb->data[0] != p->value) {
531 kfree_skb(res_skb);
532 return -EIO;
533 }
534
535 kfree_skb(res_skb);
536
537 p++;
538 }
539
540 param[0] = NFC_HCI_UICC_HOST_ID;
541 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
542 NFC_HCI_ADMIN_WHITELIST, param, 1);
543 if (r < 0)
544 return r;
545
546 param[0] = 0x3d;
547 r = nfc_hci_set_param(hdev, PN544_SYS_MGMT_GATE,
548 PN544_SYS_MGMT_INFO_NOTIFICATION, param, 1);
549 if (r < 0)
550 return r;
551
552 param[0] = 0x0;
553 r = nfc_hci_set_param(hdev, NFC_HCI_RF_READER_A_GATE,
554 PN544_RF_READER_A_AUTO_ACTIVATION, param, 1);
555 if (r < 0)
556 return r;
557
558 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
559 NFC_HCI_EVT_END_OPERATION, NULL, 0);
560 if (r < 0)
561 return r;
562
563 param[0] = 0x1;
564 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
565 PN544_PL_NFCT_DEACTIVATED, param, 1);
566 if (r < 0)
567 return r;
568
569 param[0] = 0x0;
570 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
571 PN544_PL_RDPHASES, param, 1);
572 if (r < 0)
573 return r;
574
575 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
576 PN544_ID_MGMT_FULL_VERSION_SW, &skb);
577 if (r < 0)
578 return r;
579
580 if (skb->len != FULL_VERSION_LEN) {
581 kfree_skb(skb);
582 return -EINVAL;
583 }
584
585 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
586 DUMP_PREFIX_NONE, 16, 1,
587 skb->data, FULL_VERSION_LEN, false);
588
589 kfree_skb(skb);
590
591 return 0;
592}
593
Waldemar Rymarkiewiczade67202012-09-07 11:08:29 +0200594static void pn544_hci_add_len_crc(struct sk_buff *skb)
595{
596 u16 crc;
597 int len;
598
599 len = skb->len + 2;
600 *skb_push(skb, 1) = len;
601
602 crc = crc_ccitt(0xffff, skb->data, skb->len);
603 crc = ~crc;
604 *skb_put(skb, 1) = crc & 0xff;
605 *skb_put(skb, 1) = crc >> 8;
606}
607
608static void pn544_hci_remove_len_crc(struct sk_buff *skb)
609{
610 skb_pull(skb, PN544_FRAME_HEADROOM);
611 skb_trim(skb, PN544_FRAME_TAILROOM);
612}
613
Eric Lapuyade412fda52012-09-18 19:45:48 +0200614static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200615{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200616 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200617 struct i2c_client *client = info->i2c_dev;
Waldemar Rymarkiewiczade67202012-09-07 11:08:29 +0200618 int r;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200619
620 if (info->hard_fault != 0)
621 return info->hard_fault;
622
Waldemar Rymarkiewiczade67202012-09-07 11:08:29 +0200623 pn544_hci_add_len_crc(skb);
624 r = pn544_hci_i2c_write(client, skb->data, skb->len);
625 pn544_hci_remove_len_crc(skb);
626
627 return r;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200628}
629
Eric Lapuyade412fda52012-09-18 19:45:48 +0200630static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200631 u32 im_protocols, u32 tm_protocols)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200632{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200633 u8 phases = 0;
634 int r;
635 u8 duration[2];
636 u8 activated;
Arron Wang7e2afc92012-09-27 17:32:54 +0800637 u8 i_mode = 0x3f; /* Enable all supported modes */
638 u8 t_mode = 0x0f;
639 u8 t_merge = 0x01; /* Enable merge by default */
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200640
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200641 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
642 __func__, im_protocols, tm_protocols);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200643
644 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
645 NFC_HCI_EVT_END_OPERATION, NULL, 0);
646 if (r < 0)
647 return r;
648
649 duration[0] = 0x18;
650 duration[1] = 0x6a;
651 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
652 PN544_PL_EMULATION, duration, 2);
653 if (r < 0)
654 return r;
655
656 activated = 0;
657 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
658 PN544_PL_NFCT_DEACTIVATED, &activated, 1);
659 if (r < 0)
660 return r;
661
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200662 if (im_protocols & (NFC_PROTO_ISO14443_MASK | NFC_PROTO_MIFARE_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200663 NFC_PROTO_JEWEL_MASK))
664 phases |= 1; /* Type A */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200665 if (im_protocols & NFC_PROTO_FELICA_MASK) {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200666 phases |= (1 << 2); /* Type F 212 */
667 phases |= (1 << 3); /* Type F 424 */
668 }
669
670 phases |= (1 << 5); /* NFC active */
671
672 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
673 PN544_PL_RDPHASES, &phases, 1);
674 if (r < 0)
675 return r;
676
Arron Wang7e2afc92012-09-27 17:32:54 +0800677 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
678 hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
679 &hdev->gb_len);
680 pr_debug("generate local bytes %p", hdev->gb);
681 if (hdev->gb == NULL || hdev->gb_len == 0) {
682 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
683 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
684 }
685 }
686
687 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
688 r = nfc_hci_send_event(hdev,
689 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
690 NFC_HCI_EVT_END_OPERATION, NULL, 0);
691 if (r < 0)
692 return r;
693
694 r = nfc_hci_set_param(hdev,
695 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
696 PN544_DEP_MODE, &i_mode, 1);
697 if (r < 0)
698 return r;
699
700 r = nfc_hci_set_param(hdev,
701 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
702 PN544_DEP_ATR_REQ, hdev->gb, hdev->gb_len);
703 if (r < 0)
704 return r;
705
706 r = nfc_hci_send_event(hdev,
707 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
708 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
709 if (r < 0)
710 nfc_hci_send_event(hdev,
711 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
712 NFC_HCI_EVT_END_OPERATION, NULL, 0);
713 }
714
715 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
716 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
717 PN544_DEP_MODE, &t_mode, 1);
718 if (r < 0)
719 return r;
720
721 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
722 PN544_DEP_ATR_RES, hdev->gb, hdev->gb_len);
723 if (r < 0)
724 return r;
725
726 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
727 PN544_DEP_MERGE, &t_merge, 1);
728 if (r < 0)
729 return r;
730 }
731
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200732 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
733 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
734 if (r < 0)
735 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
736 NFC_HCI_EVT_END_OPERATION, NULL, 0);
737
738 return r;
739}
740
Eric Lapuyade412fda52012-09-18 19:45:48 +0200741static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200742 struct nfc_target *target)
743{
744 switch (gate) {
745 case PN544_RF_READER_F_GATE:
746 target->supported_protocols = NFC_PROTO_FELICA_MASK;
747 break;
748 case PN544_RF_READER_JEWEL_GATE:
749 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
750 target->sens_res = 0x0c00;
751 break;
752 default:
753 return -EPROTO;
754 }
755
756 return 0;
757}
758
Eric Lapuyade412fda52012-09-18 19:45:48 +0200759static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200760 u8 gate,
761 struct nfc_target *target)
762{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200763 struct sk_buff *uid_skb;
764 int r = 0;
765
766 if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
767 if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
768 target->nfcid1_len != 10)
769 return -EPROTO;
770
771 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
772 PN544_RF_READER_CMD_ACTIVATE_NEXT,
773 target->nfcid1, target->nfcid1_len, NULL);
774 } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
775 r = nfc_hci_get_param(hdev, PN544_RF_READER_F_GATE,
776 PN544_FELICA_ID, &uid_skb);
777 if (r < 0)
778 return r;
779
780 if (uid_skb->len != 8) {
781 kfree_skb(uid_skb);
782 return -EPROTO;
783 }
784
785 r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
786 PN544_RF_READER_CMD_ACTIVATE_NEXT,
787 uid_skb->data, uid_skb->len, NULL);
788 kfree_skb(uid_skb);
789 } else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) {
790 /*
791 * TODO: maybe other ISO 14443 require some kind of continue
792 * activation, but for now we've seen only this one below.
793 */
794 if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */
795 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
796 PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION,
797 NULL, 0, NULL);
798 }
799
800 return r;
801}
802
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200803#define PN544_CB_TYPE_READER_F 1
804
805static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
806 int err)
807{
808 struct pn544_hci_info *info = context;
809
810 switch (info->async_cb_type) {
811 case PN544_CB_TYPE_READER_F:
812 if (err == 0)
813 skb_pull(skb, 1);
814 info->async_cb(info->async_cb_context, skb, err);
815 break;
816 default:
817 if (err == 0)
818 kfree_skb(skb);
819 break;
820 }
821}
822
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200823#define MIFARE_CMD_AUTH_KEY_A 0x60
824#define MIFARE_CMD_AUTH_KEY_B 0x61
825#define MIFARE_CMD_HEADER 2
826#define MIFARE_UID_LEN 4
827#define MIFARE_KEY_LEN 6
828#define MIFARE_CMD_LEN 12
829/*
830 * Returns:
831 * <= 0: driver handled the data exchange
832 * 1: driver doesn't especially handle, please do standard processing
833 */
Eric Lapuyade412fda52012-09-18 19:45:48 +0200834static int pn544_hci_data_exchange(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200835 struct nfc_target *target,
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200836 struct sk_buff *skb, data_exchange_cb_t cb,
837 void *cb_context)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200838{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200839 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200840
841 pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
842 target->hci_reader_gate);
843
844 switch (target->hci_reader_gate) {
845 case NFC_HCI_RF_READER_A_GATE:
846 if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
847 /*
848 * It seems that pn544 is inverting key and UID for
849 * MIFARE authentication commands.
850 */
851 if (skb->len == MIFARE_CMD_LEN &&
852 (skb->data[0] == MIFARE_CMD_AUTH_KEY_A ||
853 skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) {
854 u8 uid[MIFARE_UID_LEN];
855 u8 *data = skb->data + MIFARE_CMD_HEADER;
856
857 memcpy(uid, data + MIFARE_KEY_LEN,
858 MIFARE_UID_LEN);
859 memmove(data + MIFARE_UID_LEN, data,
860 MIFARE_KEY_LEN);
861 memcpy(data, uid, MIFARE_UID_LEN);
862 }
863
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200864 return nfc_hci_send_cmd_async(hdev,
865 target->hci_reader_gate,
866 PN544_MIFARE_CMD,
867 skb->data, skb->len,
868 cb, cb_context);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200869 } else
870 return 1;
871 case PN544_RF_READER_F_GATE:
872 *skb_push(skb, 1) = 0;
873 *skb_push(skb, 1) = 0;
874
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200875 info->async_cb_type = PN544_CB_TYPE_READER_F;
876 info->async_cb = cb;
877 info->async_cb_context = cb_context;
878
879 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
880 PN544_FELICA_RAW, skb->data,
881 skb->len,
882 pn544_hci_data_exchange_cb, info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200883 case PN544_RF_READER_JEWEL_GATE:
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200884 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
885 PN544_JEWEL_RAW_CMD, skb->data,
886 skb->len, cb, cb_context);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200887 default:
888 return 1;
889 }
890}
891
Eric Lapuyade412fda52012-09-18 19:45:48 +0200892static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200893 struct nfc_target *target)
894{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200895 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
896 PN544_RF_READER_CMD_PRESENCE_CHECK,
897 NULL, 0, NULL);
898}
899
Eric Lapuyade412fda52012-09-18 19:45:48 +0200900static struct nfc_hci_ops pn544_hci_ops = {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200901 .open = pn544_hci_open,
902 .close = pn544_hci_close,
903 .hci_ready = pn544_hci_ready,
904 .xmit = pn544_hci_xmit,
905 .start_poll = pn544_hci_start_poll,
906 .target_from_gate = pn544_hci_target_from_gate,
907 .complete_target_discovered = pn544_hci_complete_target_discovered,
908 .data_exchange = pn544_hci_data_exchange,
909 .check_presence = pn544_hci_check_presence,
910};
911
912static int __devinit pn544_hci_probe(struct i2c_client *client,
913 const struct i2c_device_id *id)
914{
915 struct pn544_hci_info *info;
916 struct pn544_nfc_platform_data *pdata;
917 int r = 0;
918 u32 protocols;
919 struct nfc_hci_init_data init_data;
920
921 dev_dbg(&client->dev, "%s\n", __func__);
922 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
923
924 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
925 dev_err(&client->dev, "Need I2C_FUNC_I2C\n");
926 return -ENODEV;
927 }
928
929 info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
930 if (!info) {
931 dev_err(&client->dev,
932 "Cannot allocate memory for pn544_hci_info.\n");
933 r = -ENOMEM;
934 goto err_info_alloc;
935 }
936
937 info->i2c_dev = client;
938 info->state = PN544_ST_COLD;
939 mutex_init(&info->info_lock);
940 i2c_set_clientdata(client, info);
941
942 pdata = client->dev.platform_data;
943 if (pdata == NULL) {
944 dev_err(&client->dev, "No platform data\n");
945 r = -EINVAL;
946 goto err_pdata;
947 }
948
949 if (pdata->request_resources == NULL) {
950 dev_err(&client->dev, "request_resources() missing\n");
951 r = -EINVAL;
952 goto err_pdata;
953 }
954
955 r = pdata->request_resources(client);
956 if (r) {
957 dev_err(&client->dev, "Cannot get platform resources\n");
958 goto err_pdata;
959 }
960
961 info->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
962 info->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
963 info->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
964
965 pn544_hci_platform_init(info);
966
967 r = request_threaded_irq(client->irq, NULL, pn544_hci_irq_thread_fn,
Samuel Ortizf2ce3982012-08-25 00:40:16 +0200968 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
969 PN544_HCI_DRIVER_NAME, info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200970 if (r < 0) {
971 dev_err(&client->dev, "Unable to register IRQ handler\n");
972 goto err_rti;
973 }
974
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200975 init_data.gate_count = ARRAY_SIZE(pn544_gates);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200976
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200977 memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates));
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200978
979 /*
980 * TODO: Session id must include the driver name + some bus addr
981 * persistent info to discriminate 2 identical chips
982 */
983 strcpy(init_data.session_id, "ID544HCI");
984
985 protocols = NFC_PROTO_JEWEL_MASK |
986 NFC_PROTO_MIFARE_MASK |
987 NFC_PROTO_FELICA_MASK |
988 NFC_PROTO_ISO14443_MASK |
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200989 NFC_PROTO_ISO14443_B_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200990 NFC_PROTO_NFC_DEP_MASK;
991
Eric Lapuyade412fda52012-09-18 19:45:48 +0200992 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data,
993 protocols, LLC_SHDLC_NAME,
994 PN544_FRAME_HEADROOM +
995 PN544_CMDS_HEADROOM,
996 PN544_FRAME_TAILROOM,
997 PN544_HCI_LLC_MAX_PAYLOAD);
998 if (!info->hdev) {
999 dev_err(&client->dev, "Cannot allocate nfc hdev.\n");
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001000 r = -ENOMEM;
Eric Lapuyade412fda52012-09-18 19:45:48 +02001001 goto err_alloc_hdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001002 }
1003
Eric Lapuyade412fda52012-09-18 19:45:48 +02001004 nfc_hci_set_clientdata(info->hdev, info);
1005
1006 r = nfc_hci_register_device(info->hdev);
1007 if (r)
1008 goto err_regdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001009
1010 return 0;
1011
Eric Lapuyade412fda52012-09-18 19:45:48 +02001012err_regdev:
1013 nfc_hci_free_device(info->hdev);
1014
1015err_alloc_hdev:
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001016 free_irq(client->irq, info);
1017
1018err_rti:
1019 if (pdata->free_resources != NULL)
1020 pdata->free_resources();
1021
1022err_pdata:
1023 kfree(info);
1024
1025err_info_alloc:
1026 return r;
1027}
1028
1029static __devexit int pn544_hci_remove(struct i2c_client *client)
1030{
1031 struct pn544_hci_info *info = i2c_get_clientdata(client);
1032 struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
1033
1034 dev_dbg(&client->dev, "%s\n", __func__);
1035
Eric Lapuyade412fda52012-09-18 19:45:48 +02001036 nfc_hci_free_device(info->hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001037
1038 if (info->state != PN544_ST_COLD) {
1039 if (pdata->disable)
1040 pdata->disable();
1041 }
1042
1043 free_irq(client->irq, info);
1044 if (pdata->free_resources)
1045 pdata->free_resources();
1046
1047 kfree(info);
1048
1049 return 0;
1050}
1051
1052static struct i2c_driver pn544_hci_driver = {
1053 .driver = {
1054 .name = PN544_HCI_DRIVER_NAME,
1055 },
1056 .probe = pn544_hci_probe,
1057 .id_table = pn544_hci_id_table,
1058 .remove = __devexit_p(pn544_hci_remove),
1059};
1060
1061static int __init pn544_hci_init(void)
1062{
1063 int r;
1064
1065 pr_debug(DRIVER_DESC ": %s\n", __func__);
1066
1067 r = i2c_add_driver(&pn544_hci_driver);
1068 if (r) {
1069 pr_err(PN544_HCI_DRIVER_NAME ": driver registration failed\n");
1070 return r;
1071 }
1072
1073 return 0;
1074}
1075
1076static void __exit pn544_hci_exit(void)
1077{
1078 i2c_del_driver(&pn544_hci_driver);
1079}
1080
1081module_init(pn544_hci_init);
1082module_exit(pn544_hci_exit);
1083
1084MODULE_LICENSE("GPL");
1085MODULE_DESCRIPTION(DRIVER_DESC);