blob: 70858b5f81e434ac0ebc0eb933b4fe2e06827d08 [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
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800115#define PN544_HCI_EVT_SND_DATA 0x01
116#define PN544_HCI_EVT_ACTIVATED 0x02
117#define PN544_HCI_EVT_DEACTIVATED 0x03
118#define PN544_HCI_EVT_RCV_DATA 0x04
119#define PN544_HCI_EVT_CONTINUE_MI 0x05
120
Arron Wangda052852012-09-27 17:32:59 +0800121#define PN544_HCI_CMD_ATTREQUEST 0x12
Arron Wang928326f2012-09-27 17:32:56 +0800122#define PN544_HCI_CMD_CONTINUE_ACTIVATION 0x13
123
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200124static struct nfc_hci_gate pn544_gates[] = {
125 {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE},
126 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
127 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
128 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE},
129 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
130 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
131 {PN544_SYS_MGMT_GATE, NFC_HCI_INVALID_PIPE},
132 {PN544_SWP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
133 {PN544_POLLING_LOOP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
134 {PN544_NFC_WI_MGMT_GATE, NFC_HCI_INVALID_PIPE},
135 {PN544_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
136 {PN544_RF_READER_JEWEL_GATE, NFC_HCI_INVALID_PIPE},
137 {PN544_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
138 {PN544_RF_READER_NFCIP1_INITIATOR_GATE, NFC_HCI_INVALID_PIPE},
139 {PN544_RF_READER_NFCIP1_TARGET_GATE, NFC_HCI_INVALID_PIPE}
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200140};
141
142/* Largest headroom needed for outgoing custom commands */
143#define PN544_CMDS_HEADROOM 2
Waldemar Rymarkiewiczade672082012-09-07 11:08:29 +0200144#define PN544_FRAME_HEADROOM 1
145#define PN544_FRAME_TAILROOM 2
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200146
147struct pn544_hci_info {
148 struct i2c_client *i2c_dev;
Eric Lapuyade412fda52012-09-18 19:45:48 +0200149 struct nfc_hci_dev *hdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200150
151 enum pn544_state state;
152
153 struct mutex info_lock;
154
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 */
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200164 int async_cb_type;
165 data_exchange_cb_t async_cb;
166 void *async_cb_context;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200167};
168
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
Waldemar Rymarkiewicz96e32402012-09-20 08:59:10 +0200251 if (r >= 0) {
252 if (r != len)
253 return -EREMOTEIO;
254 else
255 return 0;
256 }
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200257
258 return r;
259}
260
261static int check_crc(u8 *buf, int buflen)
262{
Dan Carpenter885ba1d2012-05-18 10:36:47 +0300263 int len;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200264 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;
Wei Yongjunc51ad342012-09-08 21:19:44 +0800366 struct i2c_client *client;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200367 struct sk_buff *skb = NULL;
368 int r;
369
Wei Yongjunc51ad342012-09-08 21:19:44 +0800370 if (!info || irq != info->i2c_dev->irq) {
371 WARN_ON_ONCE(1);
372 return IRQ_NONE;
373 }
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200374
Wei Yongjunc51ad342012-09-08 21:19:44 +0800375 client = info->i2c_dev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200376 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
Eric Lapuyade412fda52012-09-18 19:45:48 +0200385 nfc_hci_recv_frame(info->hdev, NULL);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200386
387 return IRQ_HANDLED;
388 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
389 return IRQ_HANDLED;
390 }
391
Eric Lapuyade412fda52012-09-18 19:45:48 +0200392 nfc_hci_recv_frame(info->hdev, skb);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200393
394 return IRQ_HANDLED;
395}
396
Eric Lapuyade412fda52012-09-18 19:45:48 +0200397static int pn544_hci_open(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200398{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200399 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200400 int r = 0;
401
402 mutex_lock(&info->info_lock);
403
404 if (info->state != PN544_ST_COLD) {
405 r = -EBUSY;
406 goto out;
407 }
408
409 r = pn544_hci_enable(info, HCI_MODE);
410
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200411 if (r == 0)
412 info->state = PN544_ST_READY;
413
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200414out:
415 mutex_unlock(&info->info_lock);
416 return r;
417}
418
Eric Lapuyade412fda52012-09-18 19:45:48 +0200419static void pn544_hci_close(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200420{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200421 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200422
423 mutex_lock(&info->info_lock);
424
425 if (info->state == PN544_ST_COLD)
426 goto out;
427
428 pn544_hci_disable(info);
429
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200430 info->state = PN544_ST_COLD;
431
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200432out:
433 mutex_unlock(&info->info_lock);
434}
435
Eric Lapuyade412fda52012-09-18 19:45:48 +0200436static int pn544_hci_ready(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200437{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200438 struct sk_buff *skb;
439 static struct hw_config {
440 u8 adr[2];
441 u8 value;
442 } hw_config[] = {
443 {{0x9f, 0x9a}, 0x00},
444
445 {{0x98, 0x10}, 0xbc},
446
447 {{0x9e, 0x71}, 0x00},
448
449 {{0x98, 0x09}, 0x00},
450
451 {{0x9e, 0xb4}, 0x00},
452
453 {{0x9e, 0xd9}, 0xff},
454 {{0x9e, 0xda}, 0xff},
455 {{0x9e, 0xdb}, 0x23},
456 {{0x9e, 0xdc}, 0x21},
457 {{0x9e, 0xdd}, 0x22},
458 {{0x9e, 0xde}, 0x24},
459
460 {{0x9c, 0x01}, 0x08},
461
462 {{0x9e, 0xaa}, 0x01},
463
464 {{0x9b, 0xd1}, 0x0d},
465 {{0x9b, 0xd2}, 0x24},
466 {{0x9b, 0xd3}, 0x0a},
467 {{0x9b, 0xd4}, 0x22},
468 {{0x9b, 0xd5}, 0x08},
469 {{0x9b, 0xd6}, 0x1e},
470 {{0x9b, 0xdd}, 0x1c},
471
472 {{0x9b, 0x84}, 0x13},
473 {{0x99, 0x81}, 0x7f},
474 {{0x99, 0x31}, 0x70},
475
476 {{0x98, 0x00}, 0x3f},
477
478 {{0x9f, 0x09}, 0x00},
479
480 {{0x9f, 0x0a}, 0x05},
481
482 {{0x9e, 0xd1}, 0xa1},
483 {{0x99, 0x23}, 0x00},
484
485 {{0x9e, 0x74}, 0x80},
486
487 {{0x9f, 0x28}, 0x10},
488
489 {{0x9f, 0x35}, 0x14},
490
491 {{0x9f, 0x36}, 0x60},
492
493 {{0x9c, 0x31}, 0x00},
494
495 {{0x9c, 0x32}, 0xc8},
496
497 {{0x9c, 0x19}, 0x40},
498
499 {{0x9c, 0x1a}, 0x40},
500
501 {{0x9c, 0x0c}, 0x00},
502
503 {{0x9c, 0x0d}, 0x00},
504
505 {{0x9c, 0x12}, 0x00},
506
507 {{0x9c, 0x13}, 0x00},
508
509 {{0x98, 0xa2}, 0x0e},
510
511 {{0x98, 0x93}, 0x40},
512
513 {{0x98, 0x7d}, 0x02},
514 {{0x98, 0x7e}, 0x00},
515 {{0x9f, 0xc8}, 0x01},
516 };
517 struct hw_config *p = hw_config;
518 int count = ARRAY_SIZE(hw_config);
519 struct sk_buff *res_skb;
520 u8 param[4];
521 int r;
522
523 param[0] = 0;
524 while (count--) {
525 param[1] = p->adr[0];
526 param[2] = p->adr[1];
527 param[3] = p->value;
528
529 r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_WRITE,
530 param, 4, &res_skb);
531 if (r < 0)
532 return r;
533
534 if (res_skb->len != 1) {
535 kfree_skb(res_skb);
536 return -EPROTO;
537 }
538
539 if (res_skb->data[0] != p->value) {
540 kfree_skb(res_skb);
541 return -EIO;
542 }
543
544 kfree_skb(res_skb);
545
546 p++;
547 }
548
549 param[0] = NFC_HCI_UICC_HOST_ID;
550 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
551 NFC_HCI_ADMIN_WHITELIST, param, 1);
552 if (r < 0)
553 return r;
554
555 param[0] = 0x3d;
556 r = nfc_hci_set_param(hdev, PN544_SYS_MGMT_GATE,
557 PN544_SYS_MGMT_INFO_NOTIFICATION, param, 1);
558 if (r < 0)
559 return r;
560
561 param[0] = 0x0;
562 r = nfc_hci_set_param(hdev, NFC_HCI_RF_READER_A_GATE,
563 PN544_RF_READER_A_AUTO_ACTIVATION, param, 1);
564 if (r < 0)
565 return r;
566
567 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
568 NFC_HCI_EVT_END_OPERATION, NULL, 0);
569 if (r < 0)
570 return r;
571
572 param[0] = 0x1;
573 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
574 PN544_PL_NFCT_DEACTIVATED, param, 1);
575 if (r < 0)
576 return r;
577
578 param[0] = 0x0;
579 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
580 PN544_PL_RDPHASES, param, 1);
581 if (r < 0)
582 return r;
583
584 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
585 PN544_ID_MGMT_FULL_VERSION_SW, &skb);
586 if (r < 0)
587 return r;
588
589 if (skb->len != FULL_VERSION_LEN) {
590 kfree_skb(skb);
591 return -EINVAL;
592 }
593
594 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
595 DUMP_PREFIX_NONE, 16, 1,
596 skb->data, FULL_VERSION_LEN, false);
597
598 kfree_skb(skb);
599
600 return 0;
601}
602
Waldemar Rymarkiewiczade672082012-09-07 11:08:29 +0200603static 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
Eric Lapuyade412fda52012-09-18 19:45:48 +0200623static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200624{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200625 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200626 struct i2c_client *client = info->i2c_dev;
Waldemar Rymarkiewiczade672082012-09-07 11:08:29 +0200627 int r;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200628
629 if (info->hard_fault != 0)
630 return info->hard_fault;
631
Waldemar Rymarkiewiczade672082012-09-07 11:08:29 +0200632 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
636 return r;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200637}
638
Eric Lapuyade412fda52012-09-18 19:45:48 +0200639static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200640 u32 im_protocols, u32 tm_protocols)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200641{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200642 u8 phases = 0;
643 int r;
644 u8 duration[2];
645 u8 activated;
Arron Wang7e2afc92012-09-27 17:32:54 +0800646 u8 i_mode = 0x3f; /* Enable all supported modes */
647 u8 t_mode = 0x0f;
648 u8 t_merge = 0x01; /* Enable merge by default */
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200649
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200650 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
651 __func__, im_protocols, tm_protocols);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200652
653 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
654 NFC_HCI_EVT_END_OPERATION, NULL, 0);
655 if (r < 0)
656 return r;
657
658 duration[0] = 0x18;
659 duration[1] = 0x6a;
660 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
661 PN544_PL_EMULATION, duration, 2);
662 if (r < 0)
663 return r;
664
665 activated = 0;
666 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
667 PN544_PL_NFCT_DEACTIVATED, &activated, 1);
668 if (r < 0)
669 return r;
670
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200671 if (im_protocols & (NFC_PROTO_ISO14443_MASK | NFC_PROTO_MIFARE_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200672 NFC_PROTO_JEWEL_MASK))
673 phases |= 1; /* Type A */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200674 if (im_protocols & NFC_PROTO_FELICA_MASK) {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200675 phases |= (1 << 2); /* Type F 212 */
676 phases |= (1 << 3); /* Type F 424 */
677 }
678
679 phases |= (1 << 5); /* NFC active */
680
681 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
682 PN544_PL_RDPHASES, &phases, 1);
683 if (r < 0)
684 return r;
685
Arron Wang7e2afc92012-09-27 17:32:54 +0800686 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
687 hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
688 &hdev->gb_len);
689 pr_debug("generate local bytes %p", hdev->gb);
690 if (hdev->gb == NULL || hdev->gb_len == 0) {
691 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
692 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
693 }
694 }
695
696 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
697 r = nfc_hci_send_event(hdev,
698 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
699 NFC_HCI_EVT_END_OPERATION, NULL, 0);
700 if (r < 0)
701 return r;
702
703 r = nfc_hci_set_param(hdev,
704 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
705 PN544_DEP_MODE, &i_mode, 1);
706 if (r < 0)
707 return r;
708
709 r = nfc_hci_set_param(hdev,
710 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
711 PN544_DEP_ATR_REQ, hdev->gb, hdev->gb_len);
712 if (r < 0)
713 return r;
714
715 r = nfc_hci_send_event(hdev,
716 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
717 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
718 if (r < 0)
719 nfc_hci_send_event(hdev,
720 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
721 NFC_HCI_EVT_END_OPERATION, NULL, 0);
722 }
723
724 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
725 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
726 PN544_DEP_MODE, &t_mode, 1);
727 if (r < 0)
728 return r;
729
730 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
731 PN544_DEP_ATR_RES, hdev->gb, hdev->gb_len);
732 if (r < 0)
733 return r;
734
735 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
736 PN544_DEP_MERGE, &t_merge, 1);
737 if (r < 0)
738 return r;
739 }
740
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200741 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
742 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
743 if (r < 0)
744 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
745 NFC_HCI_EVT_END_OPERATION, NULL, 0);
746
747 return r;
748}
749
Arron Wangc40d1742012-09-27 17:32:57 +0800750static int pn544_hci_dep_link_up(struct nfc_hci_dev *hdev,
751 struct nfc_target *target, u8 comm_mode,
752 u8 *gb, size_t gb_len)
753{
754 struct sk_buff *rgb_skb = NULL;
755 int r;
756
757 r = nfc_hci_get_param(hdev, target->hci_reader_gate,
758 PN544_DEP_ATR_RES, &rgb_skb);
759 if (r < 0)
760 return r;
761
762 if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) {
763 r = -EPROTO;
764 goto exit;
765 }
766 print_hex_dump(KERN_DEBUG, "remote gb: ", DUMP_PREFIX_OFFSET,
767 16, 1, rgb_skb->data, rgb_skb->len, true);
768
769 r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data,
770 rgb_skb->len);
771
772 if (r == 0)
773 r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode,
774 NFC_RF_INITIATOR);
775exit:
776 kfree_skb(rgb_skb);
777 return r;
778}
779
780static int pn544_hci_dep_link_down(struct nfc_hci_dev *hdev)
781{
782
783 return nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_INITIATOR_GATE,
784 NFC_HCI_EVT_END_OPERATION, NULL, 0);
785}
786
Eric Lapuyade412fda52012-09-18 19:45:48 +0200787static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200788 struct nfc_target *target)
789{
790 switch (gate) {
791 case PN544_RF_READER_F_GATE:
792 target->supported_protocols = NFC_PROTO_FELICA_MASK;
793 break;
794 case PN544_RF_READER_JEWEL_GATE:
795 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
796 target->sens_res = 0x0c00;
797 break;
Arron Wang928326f2012-09-27 17:32:56 +0800798 case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
799 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
800 break;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200801 default:
802 return -EPROTO;
803 }
804
805 return 0;
806}
807
Eric Lapuyade412fda52012-09-18 19:45:48 +0200808static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200809 u8 gate,
810 struct nfc_target *target)
811{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200812 struct sk_buff *uid_skb;
813 int r = 0;
814
Arron Wang928326f2012-09-27 17:32:56 +0800815 if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE)
816 return r;
817
818 if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
819 r = nfc_hci_send_cmd(hdev,
820 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
821 PN544_HCI_CMD_CONTINUE_ACTIVATION, NULL, 0, NULL);
822 if (r < 0)
823 return r;
824
825 target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE;
826 } else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200827 if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
828 target->nfcid1_len != 10)
829 return -EPROTO;
830
831 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
832 PN544_RF_READER_CMD_ACTIVATE_NEXT,
833 target->nfcid1, target->nfcid1_len, NULL);
834 } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
835 r = nfc_hci_get_param(hdev, PN544_RF_READER_F_GATE,
836 PN544_FELICA_ID, &uid_skb);
837 if (r < 0)
838 return r;
839
840 if (uid_skb->len != 8) {
841 kfree_skb(uid_skb);
842 return -EPROTO;
843 }
844
845 r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
846 PN544_RF_READER_CMD_ACTIVATE_NEXT,
847 uid_skb->data, uid_skb->len, NULL);
848 kfree_skb(uid_skb);
Arron Wang928326f2012-09-27 17:32:56 +0800849
850 r = nfc_hci_send_cmd(hdev,
851 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
852 PN544_HCI_CMD_CONTINUE_ACTIVATION,
853 NULL, 0, NULL);
854 if (r < 0)
855 return r;
856
857 target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE;
858 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200859 } else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) {
860 /*
861 * TODO: maybe other ISO 14443 require some kind of continue
862 * activation, but for now we've seen only this one below.
863 */
864 if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */
865 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
866 PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION,
867 NULL, 0, NULL);
868 }
869
870 return r;
871}
872
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200873#define PN544_CB_TYPE_READER_F 1
874
875static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
876 int err)
877{
878 struct pn544_hci_info *info = context;
879
880 switch (info->async_cb_type) {
881 case PN544_CB_TYPE_READER_F:
882 if (err == 0)
883 skb_pull(skb, 1);
884 info->async_cb(info->async_cb_context, skb, err);
885 break;
886 default:
887 if (err == 0)
888 kfree_skb(skb);
889 break;
890 }
891}
892
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200893#define MIFARE_CMD_AUTH_KEY_A 0x60
894#define MIFARE_CMD_AUTH_KEY_B 0x61
895#define MIFARE_CMD_HEADER 2
896#define MIFARE_UID_LEN 4
897#define MIFARE_KEY_LEN 6
898#define MIFARE_CMD_LEN 12
899/*
900 * Returns:
901 * <= 0: driver handled the data exchange
902 * 1: driver doesn't especially handle, please do standard processing
903 */
Arron Wange8107622012-09-27 17:32:58 +0800904static int pn544_hci_im_transceive(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200905 struct nfc_target *target,
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200906 struct sk_buff *skb, data_exchange_cb_t cb,
907 void *cb_context)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200908{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200909 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200910
911 pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
912 target->hci_reader_gate);
913
914 switch (target->hci_reader_gate) {
915 case NFC_HCI_RF_READER_A_GATE:
916 if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
917 /*
918 * It seems that pn544 is inverting key and UID for
919 * MIFARE authentication commands.
920 */
921 if (skb->len == MIFARE_CMD_LEN &&
922 (skb->data[0] == MIFARE_CMD_AUTH_KEY_A ||
923 skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) {
924 u8 uid[MIFARE_UID_LEN];
925 u8 *data = skb->data + MIFARE_CMD_HEADER;
926
927 memcpy(uid, data + MIFARE_KEY_LEN,
928 MIFARE_UID_LEN);
929 memmove(data + MIFARE_UID_LEN, data,
930 MIFARE_KEY_LEN);
931 memcpy(data, uid, MIFARE_UID_LEN);
932 }
933
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200934 return nfc_hci_send_cmd_async(hdev,
935 target->hci_reader_gate,
936 PN544_MIFARE_CMD,
937 skb->data, skb->len,
938 cb, cb_context);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200939 } else
940 return 1;
941 case PN544_RF_READER_F_GATE:
942 *skb_push(skb, 1) = 0;
943 *skb_push(skb, 1) = 0;
944
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200945 info->async_cb_type = PN544_CB_TYPE_READER_F;
946 info->async_cb = cb;
947 info->async_cb_context = cb_context;
948
949 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
950 PN544_FELICA_RAW, skb->data,
951 skb->len,
952 pn544_hci_data_exchange_cb, info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200953 case PN544_RF_READER_JEWEL_GATE:
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200954 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
955 PN544_JEWEL_RAW_CMD, skb->data,
956 skb->len, cb, cb_context);
Arron Wange8107622012-09-27 17:32:58 +0800957 case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
958 *skb_push(skb, 1) = 0;
959
960 return nfc_hci_send_event(hdev, target->hci_reader_gate,
961 PN544_HCI_EVT_SND_DATA, skb->data,
962 skb->len);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200963 default:
964 return 1;
965 }
966}
967
Arron Wange8107622012-09-27 17:32:58 +0800968static int pn544_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
969{
970 /* Set default false for multiple information chaining */
971 *skb_push(skb, 1) = 0;
972
973 return nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
974 PN544_HCI_EVT_SND_DATA, skb->data, skb->len);
975}
976
Eric Lapuyade412fda52012-09-18 19:45:48 +0200977static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200978 struct nfc_target *target)
979{
Arron Wangda052852012-09-27 17:32:59 +0800980 pr_debug("supported protocol %d", target->supported_protocols);
981 if (target->supported_protocols & (NFC_PROTO_ISO14443_MASK |
982 NFC_PROTO_ISO14443_B_MASK)) {
983 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
984 PN544_RF_READER_CMD_PRESENCE_CHECK,
985 NULL, 0, NULL);
986 } else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
987 if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
988 target->nfcid1_len != 10)
Eric Lapuyade632c0162012-10-02 17:27:36 +0200989 return -EOPNOTSUPP;
Arron Wangda052852012-09-27 17:32:59 +0800990
991 return nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
992 PN544_RF_READER_CMD_ACTIVATE_NEXT,
993 target->nfcid1, target->nfcid1_len, NULL);
994 } else if (target->supported_protocols & NFC_PROTO_JEWEL_MASK) {
995 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
996 PN544_JEWEL_RAW_CMD, NULL, 0, NULL);
997 } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
998 return nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
999 PN544_FELICA_RAW, NULL, 0, NULL);
1000 } else if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
1001 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
1002 PN544_HCI_CMD_ATTREQUEST,
1003 NULL, 0, NULL);
1004 }
1005
1006 return 0;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001007}
1008
Arron Wangf7a5f6c2012-09-27 17:32:55 +08001009void pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, u8 event,
1010 struct sk_buff *skb)
1011{
1012 struct sk_buff *rgb_skb = NULL;
1013 int r = 0;
1014
1015 pr_debug("hci event %d", event);
1016 switch (event) {
1017 case PN544_HCI_EVT_ACTIVATED:
1018 if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE)
1019 nfc_hci_target_discovered(hdev, gate);
1020 else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) {
1021 r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ,
1022 &rgb_skb);
1023
1024 if (r < 0)
1025 goto exit;
1026
1027 nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
1028 NFC_COMM_PASSIVE, rgb_skb->data,
1029 rgb_skb->len);
1030
1031 kfree_skb(rgb_skb);
1032 }
1033
1034 break;
1035 case PN544_HCI_EVT_DEACTIVATED:
1036 nfc_hci_send_event(hdev, gate,
1037 NFC_HCI_EVT_END_OPERATION, NULL, 0);
1038 break;
Arron Wange8107622012-09-27 17:32:58 +08001039 case PN544_HCI_EVT_RCV_DATA:
1040 if (skb->len < 2) {
1041 r = -EPROTO;
1042 goto exit;
1043 }
1044
1045 if (skb->data[0] != 0) {
1046 pr_debug("data0 %d", skb->data[0]);
1047 r = -EPROTO;
1048 goto exit;
1049 }
1050
1051 skb_pull(skb, 2);
1052 nfc_tm_data_received(hdev->ndev, skb);
1053
1054 return;
Arron Wangf7a5f6c2012-09-27 17:32:55 +08001055 default:
1056 break;
1057 }
1058
1059exit:
1060 kfree_skb(skb);
1061}
1062
Eric Lapuyade412fda52012-09-18 19:45:48 +02001063static struct nfc_hci_ops pn544_hci_ops = {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001064 .open = pn544_hci_open,
1065 .close = pn544_hci_close,
1066 .hci_ready = pn544_hci_ready,
1067 .xmit = pn544_hci_xmit,
1068 .start_poll = pn544_hci_start_poll,
Arron Wangc40d1742012-09-27 17:32:57 +08001069 .dep_link_up = pn544_hci_dep_link_up,
1070 .dep_link_down = pn544_hci_dep_link_down,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001071 .target_from_gate = pn544_hci_target_from_gate,
1072 .complete_target_discovered = pn544_hci_complete_target_discovered,
Arron Wange8107622012-09-27 17:32:58 +08001073 .im_transceive = pn544_hci_im_transceive,
1074 .tm_send = pn544_hci_tm_send,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001075 .check_presence = pn544_hci_check_presence,
Arron Wangf7a5f6c2012-09-27 17:32:55 +08001076 .event_received = pn544_hci_event_received,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001077};
1078
1079static int __devinit pn544_hci_probe(struct i2c_client *client,
1080 const struct i2c_device_id *id)
1081{
1082 struct pn544_hci_info *info;
1083 struct pn544_nfc_platform_data *pdata;
1084 int r = 0;
1085 u32 protocols;
1086 struct nfc_hci_init_data init_data;
1087
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
1096 info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
1097 if (!info) {
1098 dev_err(&client->dev,
1099 "Cannot allocate memory for pn544_hci_info.\n");
1100 r = -ENOMEM;
1101 goto err_info_alloc;
1102 }
1103
1104 info->i2c_dev = client;
1105 info->state = PN544_ST_COLD;
1106 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,
Samuel Ortizf2ce3982012-08-25 00:40:16 +02001135 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1136 PN544_HCI_DRIVER_NAME, info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001137 if (r < 0) {
1138 dev_err(&client->dev, "Unable to register IRQ handler\n");
1139 goto err_rti;
1140 }
1141
Eric Lapuyadea10d5952012-06-05 14:42:11 +02001142 init_data.gate_count = ARRAY_SIZE(pn544_gates);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001143
Eric Lapuyadea10d5952012-06-05 14:42:11 +02001144 memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates));
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001145
1146 /*
1147 * TODO: Session id must include the driver name + some bus addr
1148 * persistent info to discriminate 2 identical chips
1149 */
1150 strcpy(init_data.session_id, "ID544HCI");
1151
1152 protocols = NFC_PROTO_JEWEL_MASK |
1153 NFC_PROTO_MIFARE_MASK |
1154 NFC_PROTO_FELICA_MASK |
1155 NFC_PROTO_ISO14443_MASK |
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001156 NFC_PROTO_ISO14443_B_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001157 NFC_PROTO_NFC_DEP_MASK;
1158
Eric Lapuyade412fda52012-09-18 19:45:48 +02001159 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data,
1160 protocols, LLC_SHDLC_NAME,
1161 PN544_FRAME_HEADROOM +
1162 PN544_CMDS_HEADROOM,
1163 PN544_FRAME_TAILROOM,
1164 PN544_HCI_LLC_MAX_PAYLOAD);
1165 if (!info->hdev) {
1166 dev_err(&client->dev, "Cannot allocate nfc hdev.\n");
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001167 r = -ENOMEM;
Eric Lapuyade412fda52012-09-18 19:45:48 +02001168 goto err_alloc_hdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001169 }
1170
Eric Lapuyade412fda52012-09-18 19:45:48 +02001171 nfc_hci_set_clientdata(info->hdev, info);
1172
1173 r = nfc_hci_register_device(info->hdev);
1174 if (r)
1175 goto err_regdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001176
1177 return 0;
1178
Eric Lapuyade412fda52012-09-18 19:45:48 +02001179err_regdev:
1180 nfc_hci_free_device(info->hdev);
1181
1182err_alloc_hdev:
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001183 free_irq(client->irq, info);
1184
1185err_rti:
1186 if (pdata->free_resources != NULL)
1187 pdata->free_resources();
1188
1189err_pdata:
1190 kfree(info);
1191
1192err_info_alloc:
1193 return r;
1194}
1195
1196static __devexit int pn544_hci_remove(struct i2c_client *client)
1197{
1198 struct pn544_hci_info *info = i2c_get_clientdata(client);
1199 struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
1200
1201 dev_dbg(&client->dev, "%s\n", __func__);
1202
Eric Lapuyade412fda52012-09-18 19:45:48 +02001203 nfc_hci_free_device(info->hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001204
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
1214 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}
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);