blob: 4ed2b8356017dc70c870f899742510bbd87cf252 [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 Wang928326f2012-09-27 17:32:56 +0800121#define PN544_HCI_CMD_CONTINUE_ACTIVATION 0x13
122
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200123static struct nfc_hci_gate pn544_gates[] = {
124 {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE},
125 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
126 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
127 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE},
128 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
129 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
130 {PN544_SYS_MGMT_GATE, NFC_HCI_INVALID_PIPE},
131 {PN544_SWP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
132 {PN544_POLLING_LOOP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
133 {PN544_NFC_WI_MGMT_GATE, NFC_HCI_INVALID_PIPE},
134 {PN544_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
135 {PN544_RF_READER_JEWEL_GATE, NFC_HCI_INVALID_PIPE},
136 {PN544_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
137 {PN544_RF_READER_NFCIP1_INITIATOR_GATE, NFC_HCI_INVALID_PIPE},
138 {PN544_RF_READER_NFCIP1_TARGET_GATE, NFC_HCI_INVALID_PIPE}
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200139};
140
141/* Largest headroom needed for outgoing custom commands */
142#define PN544_CMDS_HEADROOM 2
Waldemar Rymarkiewiczade67202012-09-07 11:08:29 +0200143#define PN544_FRAME_HEADROOM 1
144#define PN544_FRAME_TAILROOM 2
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200145
146struct pn544_hci_info {
147 struct i2c_client *i2c_dev;
Eric Lapuyade412fda52012-09-18 19:45:48 +0200148 struct nfc_hci_dev *hdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200149
150 enum pn544_state state;
151
152 struct mutex info_lock;
153
154 unsigned int gpio_en;
155 unsigned int gpio_irq;
156 unsigned int gpio_fw;
157 unsigned int en_polarity;
158
159 int hard_fault; /*
160 * < 0 if hardware error occured (e.g. i2c err)
161 * and prevents normal operation.
162 */
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200163 int async_cb_type;
164 data_exchange_cb_t async_cb;
165 void *async_cb_context;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200166};
167
168static void pn544_hci_platform_init(struct pn544_hci_info *info)
169{
170 int polarity, retry, ret;
171 char rset_cmd[] = { 0x05, 0xF9, 0x04, 0x00, 0xC3, 0xE5 };
172 int count = sizeof(rset_cmd);
173
174 pr_info(DRIVER_DESC ": %s\n", __func__);
175 dev_info(&info->i2c_dev->dev, "Detecting nfc_en polarity\n");
176
177 /* Disable fw download */
178 gpio_set_value(info->gpio_fw, 0);
179
180 for (polarity = 0; polarity < 2; polarity++) {
181 info->en_polarity = polarity;
182 retry = 3;
183 while (retry--) {
184 /* power off */
185 gpio_set_value(info->gpio_en, !info->en_polarity);
186 usleep_range(10000, 15000);
187
188 /* power on */
189 gpio_set_value(info->gpio_en, info->en_polarity);
190 usleep_range(10000, 15000);
191
192 /* send reset */
193 dev_dbg(&info->i2c_dev->dev, "Sending reset cmd\n");
194 ret = i2c_master_send(info->i2c_dev, rset_cmd, count);
195 if (ret == count) {
196 dev_info(&info->i2c_dev->dev,
197 "nfc_en polarity : active %s\n",
198 (polarity == 0 ? "low" : "high"));
199 goto out;
200 }
201 }
202 }
203
204 dev_err(&info->i2c_dev->dev,
205 "Could not detect nfc_en polarity, fallback to active high\n");
206
207out:
208 gpio_set_value(info->gpio_en, !info->en_polarity);
209}
210
211static int pn544_hci_enable(struct pn544_hci_info *info, int mode)
212{
213 pr_info(DRIVER_DESC ": %s\n", __func__);
214
215 gpio_set_value(info->gpio_fw, 0);
216 gpio_set_value(info->gpio_en, info->en_polarity);
217 usleep_range(10000, 15000);
218
219 return 0;
220}
221
222static void pn544_hci_disable(struct pn544_hci_info *info)
223{
224 pr_info(DRIVER_DESC ": %s\n", __func__);
225
226 gpio_set_value(info->gpio_fw, 0);
227 gpio_set_value(info->gpio_en, !info->en_polarity);
228 usleep_range(10000, 15000);
229
230 gpio_set_value(info->gpio_en, info->en_polarity);
231 usleep_range(10000, 15000);
232
233 gpio_set_value(info->gpio_en, !info->en_polarity);
234 usleep_range(10000, 15000);
235}
236
237static int pn544_hci_i2c_write(struct i2c_client *client, u8 *buf, int len)
238{
239 int r;
240
241 usleep_range(3000, 6000);
242
243 r = i2c_master_send(client, buf, len);
244
245 if (r == -EREMOTEIO) { /* Retry, chip was in standby */
246 usleep_range(6000, 10000);
247 r = i2c_master_send(client, buf, len);
248 }
249
Waldemar Rymarkiewicz96e32402012-09-20 08:59:10 +0200250 if (r >= 0) {
251 if (r != len)
252 return -EREMOTEIO;
253 else
254 return 0;
255 }
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200256
257 return r;
258}
259
260static int check_crc(u8 *buf, int buflen)
261{
Dan Carpenter885ba1d2012-05-18 10:36:47 +0300262 int len;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200263 u16 crc;
264
265 len = buf[0] + 1;
266 crc = crc_ccitt(0xffff, buf, len - 2);
267 crc = ~crc;
268
269 if (buf[len - 2] != (crc & 0xff) || buf[len - 1] != (crc >> 8)) {
270 pr_err(PN544_HCI_DRIVER_NAME ": CRC error 0x%x != 0x%x 0x%x\n",
271 crc, buf[len - 1], buf[len - 2]);
272
273 pr_info(DRIVER_DESC ": %s : BAD CRC\n", __func__);
274 print_hex_dump(KERN_DEBUG, "crc: ", DUMP_PREFIX_NONE,
275 16, 2, buf, buflen, false);
276 return -EPERM;
277 }
278 return 0;
279}
280
281/*
282 * Reads an shdlc frame and returns it in a newly allocated sk_buff. Guarantees
283 * that i2c bus will be flushed and that next read will start on a new frame.
284 * returned skb contains only LLC header and payload.
285 * returns:
286 * -EREMOTEIO : i2c read error (fatal)
287 * -EBADMSG : frame was incorrect and discarded
288 * -ENOMEM : cannot allocate skb, frame dropped
289 */
290static int pn544_hci_i2c_read(struct i2c_client *client, struct sk_buff **skb)
291{
292 int r;
293 u8 len;
294 u8 tmp[PN544_HCI_LLC_MAX_SIZE - 1];
295
296 r = i2c_master_recv(client, &len, 1);
297 if (r != 1) {
298 dev_err(&client->dev, "cannot read len byte\n");
299 return -EREMOTEIO;
300 }
301
302 if ((len < (PN544_HCI_LLC_MIN_SIZE - 1)) ||
303 (len > (PN544_HCI_LLC_MAX_SIZE - 1))) {
304 dev_err(&client->dev, "invalid len byte\n");
305 r = -EBADMSG;
306 goto flush;
307 }
308
309 *skb = alloc_skb(1 + len, GFP_KERNEL);
310 if (*skb == NULL) {
311 r = -ENOMEM;
312 goto flush;
313 }
314
315 *skb_put(*skb, 1) = len;
316
317 r = i2c_master_recv(client, skb_put(*skb, len), len);
318 if (r != len) {
319 kfree_skb(*skb);
320 return -EREMOTEIO;
321 }
322
323 r = check_crc((*skb)->data, (*skb)->len);
324 if (r != 0) {
325 kfree_skb(*skb);
326 r = -EBADMSG;
327 goto flush;
328 }
329
330 skb_pull(*skb, 1);
331 skb_trim(*skb, (*skb)->len - 2);
332
333 usleep_range(3000, 6000);
334
335 return 0;
336
337flush:
338 if (i2c_master_recv(client, tmp, sizeof(tmp)) < 0)
339 r = -EREMOTEIO;
340
341 usleep_range(3000, 6000);
342
343 return r;
344}
345
346/*
347 * Reads an shdlc frame from the chip. This is not as straightforward as it
348 * seems. There are cases where we could loose the frame start synchronization.
349 * The frame format is len-data-crc, and corruption can occur anywhere while
350 * transiting on i2c bus, such that we could read an invalid len.
351 * In order to recover synchronization with the next frame, we must be sure
352 * to read the real amount of data without using the len byte. We do this by
353 * assuming the following:
354 * - the chip will always present only one single complete frame on the bus
355 * before triggering the interrupt
356 * - the chip will not present a new frame until we have completely read
357 * the previous one (or until we have handled the interrupt).
358 * The tricky case is when we read a corrupted len that is less than the real
359 * len. We must detect this here in order to determine that we need to flush
360 * the bus. This is the reason why we check the crc here.
361 */
362static irqreturn_t pn544_hci_irq_thread_fn(int irq, void *dev_id)
363{
364 struct pn544_hci_info *info = dev_id;
Wei Yongjunc51ad342012-09-08 21:19:44 +0800365 struct i2c_client *client;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200366 struct sk_buff *skb = NULL;
367 int r;
368
Wei Yongjunc51ad342012-09-08 21:19:44 +0800369 if (!info || irq != info->i2c_dev->irq) {
370 WARN_ON_ONCE(1);
371 return IRQ_NONE;
372 }
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200373
Wei Yongjunc51ad342012-09-08 21:19:44 +0800374 client = info->i2c_dev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200375 dev_dbg(&client->dev, "IRQ\n");
376
377 if (info->hard_fault != 0)
378 return IRQ_HANDLED;
379
380 r = pn544_hci_i2c_read(client, &skb);
381 if (r == -EREMOTEIO) {
382 info->hard_fault = r;
383
Eric Lapuyade412fda52012-09-18 19:45:48 +0200384 nfc_hci_recv_frame(info->hdev, NULL);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200385
386 return IRQ_HANDLED;
387 } else if ((r == -ENOMEM) || (r == -EBADMSG)) {
388 return IRQ_HANDLED;
389 }
390
Eric Lapuyade412fda52012-09-18 19:45:48 +0200391 nfc_hci_recv_frame(info->hdev, skb);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200392
393 return IRQ_HANDLED;
394}
395
Eric Lapuyade412fda52012-09-18 19:45:48 +0200396static int pn544_hci_open(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200397{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200398 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200399 int r = 0;
400
401 mutex_lock(&info->info_lock);
402
403 if (info->state != PN544_ST_COLD) {
404 r = -EBUSY;
405 goto out;
406 }
407
408 r = pn544_hci_enable(info, HCI_MODE);
409
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200410 if (r == 0)
411 info->state = PN544_ST_READY;
412
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200413out:
414 mutex_unlock(&info->info_lock);
415 return r;
416}
417
Eric Lapuyade412fda52012-09-18 19:45:48 +0200418static void pn544_hci_close(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200419{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200420 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200421
422 mutex_lock(&info->info_lock);
423
424 if (info->state == PN544_ST_COLD)
425 goto out;
426
427 pn544_hci_disable(info);
428
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200429 info->state = PN544_ST_COLD;
430
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200431out:
432 mutex_unlock(&info->info_lock);
433}
434
Eric Lapuyade412fda52012-09-18 19:45:48 +0200435static int pn544_hci_ready(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200436{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200437 struct sk_buff *skb;
438 static struct hw_config {
439 u8 adr[2];
440 u8 value;
441 } hw_config[] = {
442 {{0x9f, 0x9a}, 0x00},
443
444 {{0x98, 0x10}, 0xbc},
445
446 {{0x9e, 0x71}, 0x00},
447
448 {{0x98, 0x09}, 0x00},
449
450 {{0x9e, 0xb4}, 0x00},
451
452 {{0x9e, 0xd9}, 0xff},
453 {{0x9e, 0xda}, 0xff},
454 {{0x9e, 0xdb}, 0x23},
455 {{0x9e, 0xdc}, 0x21},
456 {{0x9e, 0xdd}, 0x22},
457 {{0x9e, 0xde}, 0x24},
458
459 {{0x9c, 0x01}, 0x08},
460
461 {{0x9e, 0xaa}, 0x01},
462
463 {{0x9b, 0xd1}, 0x0d},
464 {{0x9b, 0xd2}, 0x24},
465 {{0x9b, 0xd3}, 0x0a},
466 {{0x9b, 0xd4}, 0x22},
467 {{0x9b, 0xd5}, 0x08},
468 {{0x9b, 0xd6}, 0x1e},
469 {{0x9b, 0xdd}, 0x1c},
470
471 {{0x9b, 0x84}, 0x13},
472 {{0x99, 0x81}, 0x7f},
473 {{0x99, 0x31}, 0x70},
474
475 {{0x98, 0x00}, 0x3f},
476
477 {{0x9f, 0x09}, 0x00},
478
479 {{0x9f, 0x0a}, 0x05},
480
481 {{0x9e, 0xd1}, 0xa1},
482 {{0x99, 0x23}, 0x00},
483
484 {{0x9e, 0x74}, 0x80},
485
486 {{0x9f, 0x28}, 0x10},
487
488 {{0x9f, 0x35}, 0x14},
489
490 {{0x9f, 0x36}, 0x60},
491
492 {{0x9c, 0x31}, 0x00},
493
494 {{0x9c, 0x32}, 0xc8},
495
496 {{0x9c, 0x19}, 0x40},
497
498 {{0x9c, 0x1a}, 0x40},
499
500 {{0x9c, 0x0c}, 0x00},
501
502 {{0x9c, 0x0d}, 0x00},
503
504 {{0x9c, 0x12}, 0x00},
505
506 {{0x9c, 0x13}, 0x00},
507
508 {{0x98, 0xa2}, 0x0e},
509
510 {{0x98, 0x93}, 0x40},
511
512 {{0x98, 0x7d}, 0x02},
513 {{0x98, 0x7e}, 0x00},
514 {{0x9f, 0xc8}, 0x01},
515 };
516 struct hw_config *p = hw_config;
517 int count = ARRAY_SIZE(hw_config);
518 struct sk_buff *res_skb;
519 u8 param[4];
520 int r;
521
522 param[0] = 0;
523 while (count--) {
524 param[1] = p->adr[0];
525 param[2] = p->adr[1];
526 param[3] = p->value;
527
528 r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_WRITE,
529 param, 4, &res_skb);
530 if (r < 0)
531 return r;
532
533 if (res_skb->len != 1) {
534 kfree_skb(res_skb);
535 return -EPROTO;
536 }
537
538 if (res_skb->data[0] != p->value) {
539 kfree_skb(res_skb);
540 return -EIO;
541 }
542
543 kfree_skb(res_skb);
544
545 p++;
546 }
547
548 param[0] = NFC_HCI_UICC_HOST_ID;
549 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
550 NFC_HCI_ADMIN_WHITELIST, param, 1);
551 if (r < 0)
552 return r;
553
554 param[0] = 0x3d;
555 r = nfc_hci_set_param(hdev, PN544_SYS_MGMT_GATE,
556 PN544_SYS_MGMT_INFO_NOTIFICATION, param, 1);
557 if (r < 0)
558 return r;
559
560 param[0] = 0x0;
561 r = nfc_hci_set_param(hdev, NFC_HCI_RF_READER_A_GATE,
562 PN544_RF_READER_A_AUTO_ACTIVATION, param, 1);
563 if (r < 0)
564 return r;
565
566 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
567 NFC_HCI_EVT_END_OPERATION, NULL, 0);
568 if (r < 0)
569 return r;
570
571 param[0] = 0x1;
572 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
573 PN544_PL_NFCT_DEACTIVATED, param, 1);
574 if (r < 0)
575 return r;
576
577 param[0] = 0x0;
578 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
579 PN544_PL_RDPHASES, param, 1);
580 if (r < 0)
581 return r;
582
583 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
584 PN544_ID_MGMT_FULL_VERSION_SW, &skb);
585 if (r < 0)
586 return r;
587
588 if (skb->len != FULL_VERSION_LEN) {
589 kfree_skb(skb);
590 return -EINVAL;
591 }
592
593 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
594 DUMP_PREFIX_NONE, 16, 1,
595 skb->data, FULL_VERSION_LEN, false);
596
597 kfree_skb(skb);
598
599 return 0;
600}
601
Waldemar Rymarkiewiczade67202012-09-07 11:08:29 +0200602static void pn544_hci_add_len_crc(struct sk_buff *skb)
603{
604 u16 crc;
605 int len;
606
607 len = skb->len + 2;
608 *skb_push(skb, 1) = len;
609
610 crc = crc_ccitt(0xffff, skb->data, skb->len);
611 crc = ~crc;
612 *skb_put(skb, 1) = crc & 0xff;
613 *skb_put(skb, 1) = crc >> 8;
614}
615
616static void pn544_hci_remove_len_crc(struct sk_buff *skb)
617{
618 skb_pull(skb, PN544_FRAME_HEADROOM);
619 skb_trim(skb, PN544_FRAME_TAILROOM);
620}
621
Eric Lapuyade412fda52012-09-18 19:45:48 +0200622static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200623{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200624 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200625 struct i2c_client *client = info->i2c_dev;
Waldemar Rymarkiewiczade67202012-09-07 11:08:29 +0200626 int r;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200627
628 if (info->hard_fault != 0)
629 return info->hard_fault;
630
Waldemar Rymarkiewiczade67202012-09-07 11:08:29 +0200631 pn544_hci_add_len_crc(skb);
632 r = pn544_hci_i2c_write(client, skb->data, skb->len);
633 pn544_hci_remove_len_crc(skb);
634
635 return r;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200636}
637
Eric Lapuyade412fda52012-09-18 19:45:48 +0200638static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200639 u32 im_protocols, u32 tm_protocols)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200640{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200641 u8 phases = 0;
642 int r;
643 u8 duration[2];
644 u8 activated;
Arron Wang7e2afc92012-09-27 17:32:54 +0800645 u8 i_mode = 0x3f; /* Enable all supported modes */
646 u8 t_mode = 0x0f;
647 u8 t_merge = 0x01; /* Enable merge by default */
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200648
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200649 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
650 __func__, im_protocols, tm_protocols);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200651
652 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
653 NFC_HCI_EVT_END_OPERATION, NULL, 0);
654 if (r < 0)
655 return r;
656
657 duration[0] = 0x18;
658 duration[1] = 0x6a;
659 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
660 PN544_PL_EMULATION, duration, 2);
661 if (r < 0)
662 return r;
663
664 activated = 0;
665 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
666 PN544_PL_NFCT_DEACTIVATED, &activated, 1);
667 if (r < 0)
668 return r;
669
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200670 if (im_protocols & (NFC_PROTO_ISO14443_MASK | NFC_PROTO_MIFARE_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200671 NFC_PROTO_JEWEL_MASK))
672 phases |= 1; /* Type A */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200673 if (im_protocols & NFC_PROTO_FELICA_MASK) {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200674 phases |= (1 << 2); /* Type F 212 */
675 phases |= (1 << 3); /* Type F 424 */
676 }
677
678 phases |= (1 << 5); /* NFC active */
679
680 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
681 PN544_PL_RDPHASES, &phases, 1);
682 if (r < 0)
683 return r;
684
Arron Wang7e2afc92012-09-27 17:32:54 +0800685 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
686 hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
687 &hdev->gb_len);
688 pr_debug("generate local bytes %p", hdev->gb);
689 if (hdev->gb == NULL || hdev->gb_len == 0) {
690 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
691 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
692 }
693 }
694
695 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
696 r = nfc_hci_send_event(hdev,
697 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
698 NFC_HCI_EVT_END_OPERATION, NULL, 0);
699 if (r < 0)
700 return r;
701
702 r = nfc_hci_set_param(hdev,
703 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
704 PN544_DEP_MODE, &i_mode, 1);
705 if (r < 0)
706 return r;
707
708 r = nfc_hci_set_param(hdev,
709 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
710 PN544_DEP_ATR_REQ, hdev->gb, hdev->gb_len);
711 if (r < 0)
712 return r;
713
714 r = nfc_hci_send_event(hdev,
715 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
716 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
717 if (r < 0)
718 nfc_hci_send_event(hdev,
719 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
720 NFC_HCI_EVT_END_OPERATION, NULL, 0);
721 }
722
723 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
724 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
725 PN544_DEP_MODE, &t_mode, 1);
726 if (r < 0)
727 return r;
728
729 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
730 PN544_DEP_ATR_RES, hdev->gb, hdev->gb_len);
731 if (r < 0)
732 return r;
733
734 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
735 PN544_DEP_MERGE, &t_merge, 1);
736 if (r < 0)
737 return r;
738 }
739
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200740 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
741 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
742 if (r < 0)
743 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
744 NFC_HCI_EVT_END_OPERATION, NULL, 0);
745
746 return r;
747}
748
Arron Wangc40d1742012-09-27 17:32:57 +0800749static int pn544_hci_dep_link_up(struct nfc_hci_dev *hdev,
750 struct nfc_target *target, u8 comm_mode,
751 u8 *gb, size_t gb_len)
752{
753 struct sk_buff *rgb_skb = NULL;
754 int r;
755
756 r = nfc_hci_get_param(hdev, target->hci_reader_gate,
757 PN544_DEP_ATR_RES, &rgb_skb);
758 if (r < 0)
759 return r;
760
761 if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) {
762 r = -EPROTO;
763 goto exit;
764 }
765 print_hex_dump(KERN_DEBUG, "remote gb: ", DUMP_PREFIX_OFFSET,
766 16, 1, rgb_skb->data, rgb_skb->len, true);
767
768 r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data,
769 rgb_skb->len);
770
771 if (r == 0)
772 r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode,
773 NFC_RF_INITIATOR);
774exit:
775 kfree_skb(rgb_skb);
776 return r;
777}
778
779static int pn544_hci_dep_link_down(struct nfc_hci_dev *hdev)
780{
781
782 return nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_INITIATOR_GATE,
783 NFC_HCI_EVT_END_OPERATION, NULL, 0);
784}
785
Eric Lapuyade412fda52012-09-18 19:45:48 +0200786static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200787 struct nfc_target *target)
788{
789 switch (gate) {
790 case PN544_RF_READER_F_GATE:
791 target->supported_protocols = NFC_PROTO_FELICA_MASK;
792 break;
793 case PN544_RF_READER_JEWEL_GATE:
794 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
795 target->sens_res = 0x0c00;
796 break;
Arron Wang928326f2012-09-27 17:32:56 +0800797 case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
798 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
799 break;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200800 default:
801 return -EPROTO;
802 }
803
804 return 0;
805}
806
Eric Lapuyade412fda52012-09-18 19:45:48 +0200807static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200808 u8 gate,
809 struct nfc_target *target)
810{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200811 struct sk_buff *uid_skb;
812 int r = 0;
813
Arron Wang928326f2012-09-27 17:32:56 +0800814 if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE)
815 return r;
816
817 if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
818 r = nfc_hci_send_cmd(hdev,
819 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
820 PN544_HCI_CMD_CONTINUE_ACTIVATION, NULL, 0, NULL);
821 if (r < 0)
822 return r;
823
824 target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE;
825 } else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200826 if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
827 target->nfcid1_len != 10)
828 return -EPROTO;
829
830 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
831 PN544_RF_READER_CMD_ACTIVATE_NEXT,
832 target->nfcid1, target->nfcid1_len, NULL);
833 } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
834 r = nfc_hci_get_param(hdev, PN544_RF_READER_F_GATE,
835 PN544_FELICA_ID, &uid_skb);
836 if (r < 0)
837 return r;
838
839 if (uid_skb->len != 8) {
840 kfree_skb(uid_skb);
841 return -EPROTO;
842 }
843
844 r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
845 PN544_RF_READER_CMD_ACTIVATE_NEXT,
846 uid_skb->data, uid_skb->len, NULL);
847 kfree_skb(uid_skb);
Arron Wang928326f2012-09-27 17:32:56 +0800848
849 r = nfc_hci_send_cmd(hdev,
850 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
851 PN544_HCI_CMD_CONTINUE_ACTIVATION,
852 NULL, 0, NULL);
853 if (r < 0)
854 return r;
855
856 target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE;
857 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200858 } else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) {
859 /*
860 * TODO: maybe other ISO 14443 require some kind of continue
861 * activation, but for now we've seen only this one below.
862 */
863 if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */
864 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
865 PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION,
866 NULL, 0, NULL);
867 }
868
869 return r;
870}
871
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200872#define PN544_CB_TYPE_READER_F 1
873
874static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
875 int err)
876{
877 struct pn544_hci_info *info = context;
878
879 switch (info->async_cb_type) {
880 case PN544_CB_TYPE_READER_F:
881 if (err == 0)
882 skb_pull(skb, 1);
883 info->async_cb(info->async_cb_context, skb, err);
884 break;
885 default:
886 if (err == 0)
887 kfree_skb(skb);
888 break;
889 }
890}
891
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200892#define MIFARE_CMD_AUTH_KEY_A 0x60
893#define MIFARE_CMD_AUTH_KEY_B 0x61
894#define MIFARE_CMD_HEADER 2
895#define MIFARE_UID_LEN 4
896#define MIFARE_KEY_LEN 6
897#define MIFARE_CMD_LEN 12
898/*
899 * Returns:
900 * <= 0: driver handled the data exchange
901 * 1: driver doesn't especially handle, please do standard processing
902 */
Eric Lapuyade412fda52012-09-18 19:45:48 +0200903static int pn544_hci_data_exchange(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200904 struct nfc_target *target,
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200905 struct sk_buff *skb, data_exchange_cb_t cb,
906 void *cb_context)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200907{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200908 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200909
910 pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
911 target->hci_reader_gate);
912
913 switch (target->hci_reader_gate) {
914 case NFC_HCI_RF_READER_A_GATE:
915 if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
916 /*
917 * It seems that pn544 is inverting key and UID for
918 * MIFARE authentication commands.
919 */
920 if (skb->len == MIFARE_CMD_LEN &&
921 (skb->data[0] == MIFARE_CMD_AUTH_KEY_A ||
922 skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) {
923 u8 uid[MIFARE_UID_LEN];
924 u8 *data = skb->data + MIFARE_CMD_HEADER;
925
926 memcpy(uid, data + MIFARE_KEY_LEN,
927 MIFARE_UID_LEN);
928 memmove(data + MIFARE_UID_LEN, data,
929 MIFARE_KEY_LEN);
930 memcpy(data, uid, MIFARE_UID_LEN);
931 }
932
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200933 return nfc_hci_send_cmd_async(hdev,
934 target->hci_reader_gate,
935 PN544_MIFARE_CMD,
936 skb->data, skb->len,
937 cb, cb_context);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200938 } else
939 return 1;
940 case PN544_RF_READER_F_GATE:
941 *skb_push(skb, 1) = 0;
942 *skb_push(skb, 1) = 0;
943
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200944 info->async_cb_type = PN544_CB_TYPE_READER_F;
945 info->async_cb = cb;
946 info->async_cb_context = cb_context;
947
948 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
949 PN544_FELICA_RAW, skb->data,
950 skb->len,
951 pn544_hci_data_exchange_cb, info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200952 case PN544_RF_READER_JEWEL_GATE:
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200953 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
954 PN544_JEWEL_RAW_CMD, skb->data,
955 skb->len, cb, cb_context);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200956 default:
957 return 1;
958 }
959}
960
Eric Lapuyade412fda52012-09-18 19:45:48 +0200961static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200962 struct nfc_target *target)
963{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200964 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
965 PN544_RF_READER_CMD_PRESENCE_CHECK,
966 NULL, 0, NULL);
967}
968
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800969void pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, u8 event,
970 struct sk_buff *skb)
971{
972 struct sk_buff *rgb_skb = NULL;
973 int r = 0;
974
975 pr_debug("hci event %d", event);
976 switch (event) {
977 case PN544_HCI_EVT_ACTIVATED:
978 if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE)
979 nfc_hci_target_discovered(hdev, gate);
980 else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) {
981 r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ,
982 &rgb_skb);
983
984 if (r < 0)
985 goto exit;
986
987 nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
988 NFC_COMM_PASSIVE, rgb_skb->data,
989 rgb_skb->len);
990
991 kfree_skb(rgb_skb);
992 }
993
994 break;
995 case PN544_HCI_EVT_DEACTIVATED:
996 nfc_hci_send_event(hdev, gate,
997 NFC_HCI_EVT_END_OPERATION, NULL, 0);
998 break;
999 default:
1000 break;
1001 }
1002
1003exit:
1004 kfree_skb(skb);
1005}
1006
Eric Lapuyade412fda52012-09-18 19:45:48 +02001007static struct nfc_hci_ops pn544_hci_ops = {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001008 .open = pn544_hci_open,
1009 .close = pn544_hci_close,
1010 .hci_ready = pn544_hci_ready,
1011 .xmit = pn544_hci_xmit,
1012 .start_poll = pn544_hci_start_poll,
Arron Wangc40d1742012-09-27 17:32:57 +08001013 .dep_link_up = pn544_hci_dep_link_up,
1014 .dep_link_down = pn544_hci_dep_link_down,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001015 .target_from_gate = pn544_hci_target_from_gate,
1016 .complete_target_discovered = pn544_hci_complete_target_discovered,
1017 .data_exchange = pn544_hci_data_exchange,
1018 .check_presence = pn544_hci_check_presence,
Arron Wangf7a5f6c2012-09-27 17:32:55 +08001019 .event_received = pn544_hci_event_received,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001020};
1021
1022static int __devinit pn544_hci_probe(struct i2c_client *client,
1023 const struct i2c_device_id *id)
1024{
1025 struct pn544_hci_info *info;
1026 struct pn544_nfc_platform_data *pdata;
1027 int r = 0;
1028 u32 protocols;
1029 struct nfc_hci_init_data init_data;
1030
1031 dev_dbg(&client->dev, "%s\n", __func__);
1032 dev_dbg(&client->dev, "IRQ: %d\n", client->irq);
1033
1034 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
1035 dev_err(&client->dev, "Need I2C_FUNC_I2C\n");
1036 return -ENODEV;
1037 }
1038
1039 info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
1040 if (!info) {
1041 dev_err(&client->dev,
1042 "Cannot allocate memory for pn544_hci_info.\n");
1043 r = -ENOMEM;
1044 goto err_info_alloc;
1045 }
1046
1047 info->i2c_dev = client;
1048 info->state = PN544_ST_COLD;
1049 mutex_init(&info->info_lock);
1050 i2c_set_clientdata(client, info);
1051
1052 pdata = client->dev.platform_data;
1053 if (pdata == NULL) {
1054 dev_err(&client->dev, "No platform data\n");
1055 r = -EINVAL;
1056 goto err_pdata;
1057 }
1058
1059 if (pdata->request_resources == NULL) {
1060 dev_err(&client->dev, "request_resources() missing\n");
1061 r = -EINVAL;
1062 goto err_pdata;
1063 }
1064
1065 r = pdata->request_resources(client);
1066 if (r) {
1067 dev_err(&client->dev, "Cannot get platform resources\n");
1068 goto err_pdata;
1069 }
1070
1071 info->gpio_en = pdata->get_gpio(NFC_GPIO_ENABLE);
1072 info->gpio_fw = pdata->get_gpio(NFC_GPIO_FW_RESET);
1073 info->gpio_irq = pdata->get_gpio(NFC_GPIO_IRQ);
1074
1075 pn544_hci_platform_init(info);
1076
1077 r = request_threaded_irq(client->irq, NULL, pn544_hci_irq_thread_fn,
Samuel Ortizf2ce3982012-08-25 00:40:16 +02001078 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1079 PN544_HCI_DRIVER_NAME, info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001080 if (r < 0) {
1081 dev_err(&client->dev, "Unable to register IRQ handler\n");
1082 goto err_rti;
1083 }
1084
Eric Lapuyadea10d5952012-06-05 14:42:11 +02001085 init_data.gate_count = ARRAY_SIZE(pn544_gates);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001086
Eric Lapuyadea10d5952012-06-05 14:42:11 +02001087 memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates));
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001088
1089 /*
1090 * TODO: Session id must include the driver name + some bus addr
1091 * persistent info to discriminate 2 identical chips
1092 */
1093 strcpy(init_data.session_id, "ID544HCI");
1094
1095 protocols = NFC_PROTO_JEWEL_MASK |
1096 NFC_PROTO_MIFARE_MASK |
1097 NFC_PROTO_FELICA_MASK |
1098 NFC_PROTO_ISO14443_MASK |
Samuel Ortiz01d719a2012-07-04 00:14:04 +02001099 NFC_PROTO_ISO14443_B_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001100 NFC_PROTO_NFC_DEP_MASK;
1101
Eric Lapuyade412fda52012-09-18 19:45:48 +02001102 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data,
1103 protocols, LLC_SHDLC_NAME,
1104 PN544_FRAME_HEADROOM +
1105 PN544_CMDS_HEADROOM,
1106 PN544_FRAME_TAILROOM,
1107 PN544_HCI_LLC_MAX_PAYLOAD);
1108 if (!info->hdev) {
1109 dev_err(&client->dev, "Cannot allocate nfc hdev.\n");
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001110 r = -ENOMEM;
Eric Lapuyade412fda52012-09-18 19:45:48 +02001111 goto err_alloc_hdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001112 }
1113
Eric Lapuyade412fda52012-09-18 19:45:48 +02001114 nfc_hci_set_clientdata(info->hdev, info);
1115
1116 r = nfc_hci_register_device(info->hdev);
1117 if (r)
1118 goto err_regdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001119
1120 return 0;
1121
Eric Lapuyade412fda52012-09-18 19:45:48 +02001122err_regdev:
1123 nfc_hci_free_device(info->hdev);
1124
1125err_alloc_hdev:
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001126 free_irq(client->irq, info);
1127
1128err_rti:
1129 if (pdata->free_resources != NULL)
1130 pdata->free_resources();
1131
1132err_pdata:
1133 kfree(info);
1134
1135err_info_alloc:
1136 return r;
1137}
1138
1139static __devexit int pn544_hci_remove(struct i2c_client *client)
1140{
1141 struct pn544_hci_info *info = i2c_get_clientdata(client);
1142 struct pn544_nfc_platform_data *pdata = client->dev.platform_data;
1143
1144 dev_dbg(&client->dev, "%s\n", __func__);
1145
Eric Lapuyade412fda52012-09-18 19:45:48 +02001146 nfc_hci_free_device(info->hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +02001147
1148 if (info->state != PN544_ST_COLD) {
1149 if (pdata->disable)
1150 pdata->disable();
1151 }
1152
1153 free_irq(client->irq, info);
1154 if (pdata->free_resources)
1155 pdata->free_resources();
1156
1157 kfree(info);
1158
1159 return 0;
1160}
1161
1162static struct i2c_driver pn544_hci_driver = {
1163 .driver = {
1164 .name = PN544_HCI_DRIVER_NAME,
1165 },
1166 .probe = pn544_hci_probe,
1167 .id_table = pn544_hci_id_table,
1168 .remove = __devexit_p(pn544_hci_remove),
1169};
1170
1171static int __init pn544_hci_init(void)
1172{
1173 int r;
1174
1175 pr_debug(DRIVER_DESC ": %s\n", __func__);
1176
1177 r = i2c_add_driver(&pn544_hci_driver);
1178 if (r) {
1179 pr_err(PN544_HCI_DRIVER_NAME ": driver registration failed\n");
1180 return r;
1181 }
1182
1183 return 0;
1184}
1185
1186static void __exit pn544_hci_exit(void)
1187{
1188 i2c_del_driver(&pn544_hci_driver);
1189}
1190
1191module_init(pn544_hci_init);
1192module_exit(pn544_hci_exit);
1193
1194MODULE_LICENSE("GPL");
1195MODULE_DESCRIPTION(DRIVER_DESC);