blob: 0963de2f6ab0ca940f975561e143f063dc42527f [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
Eric Lapuyadebbed0de2012-05-07 12:31:29 +020021#include <linux/delay.h>
22#include <linux/slab.h>
Samuel Ortizaa741032012-12-18 16:26:23 +010023#include <linux/module.h>
Eric Lapuyadebbed0de2012-05-07 12:31:29 +020024
25#include <linux/nfc.h>
26#include <net/nfc/hci.h>
Eric Lapuyade412fda52012-09-18 19:45:48 +020027#include <net/nfc/llc.h>
Eric Lapuyadebbed0de2012-05-07 12:31:29 +020028
Eric Lapuyade97f18412012-10-02 18:44:06 +020029#include "pn544.h"
Eric Lapuyadebbed0de2012-05-07 12:31:29 +020030
31/* Timing restrictions (ms) */
32#define PN544_HCI_RESETVEN_TIME 30
33
Eric Lapuyadebbed0de2012-05-07 12:31:29 +020034#define HCI_MODE 0
35#define FW_MODE 1
36
Eric Lapuyadebbed0de2012-05-07 12:31:29 +020037enum pn544_state {
38 PN544_ST_COLD,
39 PN544_ST_FW_READY,
40 PN544_ST_READY,
41};
42
43#define FULL_VERSION_LEN 11
44
45/* Proprietary commands */
46#define PN544_WRITE 0x3f
47
48/* Proprietary gates, events, commands and registers */
49
50/* NFC_HCI_RF_READER_A_GATE additional registers and commands */
51#define PN544_RF_READER_A_AUTO_ACTIVATION 0x10
52#define PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION 0x12
53#define PN544_MIFARE_CMD 0x21
54
55/* Commands that apply to all RF readers */
56#define PN544_RF_READER_CMD_PRESENCE_CHECK 0x30
57#define PN544_RF_READER_CMD_ACTIVATE_NEXT 0x32
58
59/* NFC_HCI_ID_MGMT_GATE additional registers */
60#define PN544_ID_MGMT_FULL_VERSION_SW 0x10
61
62#define PN544_RF_READER_ISO15693_GATE 0x12
63
64#define PN544_RF_READER_F_GATE 0x14
65#define PN544_FELICA_ID 0x04
66#define PN544_FELICA_RAW 0x20
67
68#define PN544_RF_READER_JEWEL_GATE 0x15
69#define PN544_JEWEL_RAW_CMD 0x23
70
71#define PN544_RF_READER_NFCIP1_INITIATOR_GATE 0x30
72#define PN544_RF_READER_NFCIP1_TARGET_GATE 0x31
73
74#define PN544_SYS_MGMT_GATE 0x90
75#define PN544_SYS_MGMT_INFO_NOTIFICATION 0x02
76
77#define PN544_POLLING_LOOP_MGMT_GATE 0x94
Arron Wang7e2afc92012-09-27 17:32:54 +080078#define PN544_DEP_MODE 0x01
79#define PN544_DEP_ATR_REQ 0x02
80#define PN544_DEP_ATR_RES 0x03
81#define PN544_DEP_MERGE 0x0D
Eric Lapuyadebbed0de2012-05-07 12:31:29 +020082#define PN544_PL_RDPHASES 0x06
83#define PN544_PL_EMULATION 0x07
84#define PN544_PL_NFCT_DEACTIVATED 0x09
85
86#define PN544_SWP_MGMT_GATE 0xA0
87
88#define PN544_NFC_WI_MGMT_GATE 0xA1
89
Arron Wangf7a5f6c2012-09-27 17:32:55 +080090#define PN544_HCI_EVT_SND_DATA 0x01
91#define PN544_HCI_EVT_ACTIVATED 0x02
92#define PN544_HCI_EVT_DEACTIVATED 0x03
93#define PN544_HCI_EVT_RCV_DATA 0x04
94#define PN544_HCI_EVT_CONTINUE_MI 0x05
95
Arron Wangda052852012-09-27 17:32:59 +080096#define PN544_HCI_CMD_ATTREQUEST 0x12
Arron Wang928326f2012-09-27 17:32:56 +080097#define PN544_HCI_CMD_CONTINUE_ACTIVATION 0x13
98
Eric Lapuyadea10d5952012-06-05 14:42:11 +020099static struct nfc_hci_gate pn544_gates[] = {
100 {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE},
101 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
102 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
103 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE},
104 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
105 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
106 {PN544_SYS_MGMT_GATE, NFC_HCI_INVALID_PIPE},
107 {PN544_SWP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
108 {PN544_POLLING_LOOP_MGMT_GATE, NFC_HCI_INVALID_PIPE},
109 {PN544_NFC_WI_MGMT_GATE, NFC_HCI_INVALID_PIPE},
110 {PN544_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
111 {PN544_RF_READER_JEWEL_GATE, NFC_HCI_INVALID_PIPE},
112 {PN544_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
113 {PN544_RF_READER_NFCIP1_INITIATOR_GATE, NFC_HCI_INVALID_PIPE},
114 {PN544_RF_READER_NFCIP1_TARGET_GATE, NFC_HCI_INVALID_PIPE}
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200115};
116
117/* Largest headroom needed for outgoing custom commands */
118#define PN544_CMDS_HEADROOM 2
119
120struct pn544_hci_info {
Eric Lapuyade97f18412012-10-02 18:44:06 +0200121 struct nfc_phy_ops *phy_ops;
122 void *phy_id;
123
Eric Lapuyade412fda52012-09-18 19:45:48 +0200124 struct nfc_hci_dev *hdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200125
126 enum pn544_state state;
127
128 struct mutex info_lock;
129
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200130 int async_cb_type;
131 data_exchange_cb_t async_cb;
132 void *async_cb_context;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200133};
134
Eric Lapuyade412fda52012-09-18 19:45:48 +0200135static int pn544_hci_open(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200136{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200137 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200138 int r = 0;
139
140 mutex_lock(&info->info_lock);
141
142 if (info->state != PN544_ST_COLD) {
143 r = -EBUSY;
144 goto out;
145 }
146
Eric Lapuyade97f18412012-10-02 18:44:06 +0200147 r = info->phy_ops->enable(info->phy_id);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200148
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200149 if (r == 0)
150 info->state = PN544_ST_READY;
151
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200152out:
153 mutex_unlock(&info->info_lock);
154 return r;
155}
156
Eric Lapuyade412fda52012-09-18 19:45:48 +0200157static void pn544_hci_close(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200158{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200159 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200160
161 mutex_lock(&info->info_lock);
162
163 if (info->state == PN544_ST_COLD)
164 goto out;
165
Eric Lapuyade97f18412012-10-02 18:44:06 +0200166 info->phy_ops->disable(info->phy_id);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200167
Eric Lapuyadeeae202a2012-05-30 18:13:06 +0200168 info->state = PN544_ST_COLD;
169
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200170out:
171 mutex_unlock(&info->info_lock);
172}
173
Eric Lapuyade412fda52012-09-18 19:45:48 +0200174static int pn544_hci_ready(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200175{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200176 struct sk_buff *skb;
177 static struct hw_config {
178 u8 adr[2];
179 u8 value;
180 } hw_config[] = {
181 {{0x9f, 0x9a}, 0x00},
182
183 {{0x98, 0x10}, 0xbc},
184
185 {{0x9e, 0x71}, 0x00},
186
187 {{0x98, 0x09}, 0x00},
188
189 {{0x9e, 0xb4}, 0x00},
190
191 {{0x9e, 0xd9}, 0xff},
192 {{0x9e, 0xda}, 0xff},
193 {{0x9e, 0xdb}, 0x23},
194 {{0x9e, 0xdc}, 0x21},
195 {{0x9e, 0xdd}, 0x22},
196 {{0x9e, 0xde}, 0x24},
197
198 {{0x9c, 0x01}, 0x08},
199
200 {{0x9e, 0xaa}, 0x01},
201
202 {{0x9b, 0xd1}, 0x0d},
203 {{0x9b, 0xd2}, 0x24},
204 {{0x9b, 0xd3}, 0x0a},
205 {{0x9b, 0xd4}, 0x22},
206 {{0x9b, 0xd5}, 0x08},
207 {{0x9b, 0xd6}, 0x1e},
208 {{0x9b, 0xdd}, 0x1c},
209
210 {{0x9b, 0x84}, 0x13},
211 {{0x99, 0x81}, 0x7f},
212 {{0x99, 0x31}, 0x70},
213
214 {{0x98, 0x00}, 0x3f},
215
216 {{0x9f, 0x09}, 0x00},
217
218 {{0x9f, 0x0a}, 0x05},
219
220 {{0x9e, 0xd1}, 0xa1},
221 {{0x99, 0x23}, 0x00},
222
223 {{0x9e, 0x74}, 0x80},
224
225 {{0x9f, 0x28}, 0x10},
226
227 {{0x9f, 0x35}, 0x14},
228
229 {{0x9f, 0x36}, 0x60},
230
231 {{0x9c, 0x31}, 0x00},
232
233 {{0x9c, 0x32}, 0xc8},
234
235 {{0x9c, 0x19}, 0x40},
236
237 {{0x9c, 0x1a}, 0x40},
238
239 {{0x9c, 0x0c}, 0x00},
240
241 {{0x9c, 0x0d}, 0x00},
242
243 {{0x9c, 0x12}, 0x00},
244
245 {{0x9c, 0x13}, 0x00},
246
247 {{0x98, 0xa2}, 0x0e},
248
249 {{0x98, 0x93}, 0x40},
250
251 {{0x98, 0x7d}, 0x02},
252 {{0x98, 0x7e}, 0x00},
253 {{0x9f, 0xc8}, 0x01},
254 };
255 struct hw_config *p = hw_config;
256 int count = ARRAY_SIZE(hw_config);
257 struct sk_buff *res_skb;
258 u8 param[4];
259 int r;
260
261 param[0] = 0;
262 while (count--) {
263 param[1] = p->adr[0];
264 param[2] = p->adr[1];
265 param[3] = p->value;
266
267 r = nfc_hci_send_cmd(hdev, PN544_SYS_MGMT_GATE, PN544_WRITE,
268 param, 4, &res_skb);
269 if (r < 0)
270 return r;
271
272 if (res_skb->len != 1) {
273 kfree_skb(res_skb);
274 return -EPROTO;
275 }
276
277 if (res_skb->data[0] != p->value) {
278 kfree_skb(res_skb);
279 return -EIO;
280 }
281
282 kfree_skb(res_skb);
283
284 p++;
285 }
286
287 param[0] = NFC_HCI_UICC_HOST_ID;
288 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
289 NFC_HCI_ADMIN_WHITELIST, param, 1);
290 if (r < 0)
291 return r;
292
293 param[0] = 0x3d;
294 r = nfc_hci_set_param(hdev, PN544_SYS_MGMT_GATE,
295 PN544_SYS_MGMT_INFO_NOTIFICATION, param, 1);
296 if (r < 0)
297 return r;
298
299 param[0] = 0x0;
300 r = nfc_hci_set_param(hdev, NFC_HCI_RF_READER_A_GATE,
301 PN544_RF_READER_A_AUTO_ACTIVATION, param, 1);
302 if (r < 0)
303 return r;
304
305 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
306 NFC_HCI_EVT_END_OPERATION, NULL, 0);
307 if (r < 0)
308 return r;
309
310 param[0] = 0x1;
311 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
312 PN544_PL_NFCT_DEACTIVATED, param, 1);
313 if (r < 0)
314 return r;
315
316 param[0] = 0x0;
317 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
318 PN544_PL_RDPHASES, param, 1);
319 if (r < 0)
320 return r;
321
322 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
323 PN544_ID_MGMT_FULL_VERSION_SW, &skb);
324 if (r < 0)
325 return r;
326
327 if (skb->len != FULL_VERSION_LEN) {
328 kfree_skb(skb);
329 return -EINVAL;
330 }
331
332 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
333 DUMP_PREFIX_NONE, 16, 1,
334 skb->data, FULL_VERSION_LEN, false);
335
336 kfree_skb(skb);
337
338 return 0;
339}
340
Eric Lapuyade412fda52012-09-18 19:45:48 +0200341static int pn544_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200342{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200343 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200344
Eric Lapuyade97f18412012-10-02 18:44:06 +0200345 return info->phy_ops->write(info->phy_id, skb);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200346}
347
Eric Lapuyade412fda52012-09-18 19:45:48 +0200348static int pn544_hci_start_poll(struct nfc_hci_dev *hdev,
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200349 u32 im_protocols, u32 tm_protocols)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200350{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200351 u8 phases = 0;
352 int r;
353 u8 duration[2];
354 u8 activated;
Arron Wang7e2afc92012-09-27 17:32:54 +0800355 u8 i_mode = 0x3f; /* Enable all supported modes */
356 u8 t_mode = 0x0f;
357 u8 t_merge = 0x01; /* Enable merge by default */
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200358
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200359 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
360 __func__, im_protocols, tm_protocols);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200361
362 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
363 NFC_HCI_EVT_END_OPERATION, NULL, 0);
364 if (r < 0)
365 return r;
366
367 duration[0] = 0x18;
368 duration[1] = 0x6a;
369 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
370 PN544_PL_EMULATION, duration, 2);
371 if (r < 0)
372 return r;
373
374 activated = 0;
375 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
376 PN544_PL_NFCT_DEACTIVATED, &activated, 1);
377 if (r < 0)
378 return r;
379
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200380 if (im_protocols & (NFC_PROTO_ISO14443_MASK | NFC_PROTO_MIFARE_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200381 NFC_PROTO_JEWEL_MASK))
382 phases |= 1; /* Type A */
Samuel Ortizfe7c5802012-05-15 15:57:06 +0200383 if (im_protocols & NFC_PROTO_FELICA_MASK) {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200384 phases |= (1 << 2); /* Type F 212 */
385 phases |= (1 << 3); /* Type F 424 */
386 }
387
388 phases |= (1 << 5); /* NFC active */
389
390 r = nfc_hci_set_param(hdev, PN544_POLLING_LOOP_MGMT_GATE,
391 PN544_PL_RDPHASES, &phases, 1);
392 if (r < 0)
393 return r;
394
Arron Wang7e2afc92012-09-27 17:32:54 +0800395 if ((im_protocols | tm_protocols) & NFC_PROTO_NFC_DEP_MASK) {
396 hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
397 &hdev->gb_len);
398 pr_debug("generate local bytes %p", hdev->gb);
399 if (hdev->gb == NULL || hdev->gb_len == 0) {
400 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
401 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
402 }
403 }
404
405 if (im_protocols & NFC_PROTO_NFC_DEP_MASK) {
406 r = nfc_hci_send_event(hdev,
407 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
408 NFC_HCI_EVT_END_OPERATION, NULL, 0);
409 if (r < 0)
410 return r;
411
412 r = nfc_hci_set_param(hdev,
413 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
414 PN544_DEP_MODE, &i_mode, 1);
415 if (r < 0)
416 return r;
417
418 r = nfc_hci_set_param(hdev,
419 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
420 PN544_DEP_ATR_REQ, hdev->gb, hdev->gb_len);
421 if (r < 0)
422 return r;
423
424 r = nfc_hci_send_event(hdev,
425 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
426 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
427 if (r < 0)
428 nfc_hci_send_event(hdev,
429 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
430 NFC_HCI_EVT_END_OPERATION, NULL, 0);
431 }
432
433 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
434 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
435 PN544_DEP_MODE, &t_mode, 1);
436 if (r < 0)
437 return r;
438
439 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
440 PN544_DEP_ATR_RES, hdev->gb, hdev->gb_len);
441 if (r < 0)
442 return r;
443
444 r = nfc_hci_set_param(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
445 PN544_DEP_MERGE, &t_merge, 1);
446 if (r < 0)
447 return r;
448 }
449
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200450 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
451 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
452 if (r < 0)
453 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
454 NFC_HCI_EVT_END_OPERATION, NULL, 0);
455
456 return r;
457}
458
Arron Wangc40d1742012-09-27 17:32:57 +0800459static int pn544_hci_dep_link_up(struct nfc_hci_dev *hdev,
460 struct nfc_target *target, u8 comm_mode,
461 u8 *gb, size_t gb_len)
462{
463 struct sk_buff *rgb_skb = NULL;
464 int r;
465
466 r = nfc_hci_get_param(hdev, target->hci_reader_gate,
467 PN544_DEP_ATR_RES, &rgb_skb);
468 if (r < 0)
469 return r;
470
471 if (rgb_skb->len == 0 || rgb_skb->len > NFC_GB_MAXSIZE) {
472 r = -EPROTO;
473 goto exit;
474 }
475 print_hex_dump(KERN_DEBUG, "remote gb: ", DUMP_PREFIX_OFFSET,
476 16, 1, rgb_skb->data, rgb_skb->len, true);
477
478 r = nfc_set_remote_general_bytes(hdev->ndev, rgb_skb->data,
479 rgb_skb->len);
480
481 if (r == 0)
482 r = nfc_dep_link_is_up(hdev->ndev, target->idx, comm_mode,
483 NFC_RF_INITIATOR);
484exit:
485 kfree_skb(rgb_skb);
486 return r;
487}
488
489static int pn544_hci_dep_link_down(struct nfc_hci_dev *hdev)
490{
491
492 return nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_INITIATOR_GATE,
493 NFC_HCI_EVT_END_OPERATION, NULL, 0);
494}
495
Eric Lapuyade412fda52012-09-18 19:45:48 +0200496static int pn544_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200497 struct nfc_target *target)
498{
499 switch (gate) {
500 case PN544_RF_READER_F_GATE:
501 target->supported_protocols = NFC_PROTO_FELICA_MASK;
502 break;
503 case PN544_RF_READER_JEWEL_GATE:
504 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
505 target->sens_res = 0x0c00;
506 break;
Arron Wang928326f2012-09-27 17:32:56 +0800507 case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
508 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
509 break;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200510 default:
511 return -EPROTO;
512 }
513
514 return 0;
515}
516
Eric Lapuyade412fda52012-09-18 19:45:48 +0200517static int pn544_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200518 u8 gate,
519 struct nfc_target *target)
520{
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200521 struct sk_buff *uid_skb;
522 int r = 0;
523
Arron Wang928326f2012-09-27 17:32:56 +0800524 if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE)
525 return r;
526
527 if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
528 r = nfc_hci_send_cmd(hdev,
529 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
530 PN544_HCI_CMD_CONTINUE_ACTIVATION, NULL, 0, NULL);
531 if (r < 0)
532 return r;
533
534 target->hci_reader_gate = PN544_RF_READER_NFCIP1_INITIATOR_GATE;
535 } else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200536 if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
537 target->nfcid1_len != 10)
538 return -EPROTO;
539
540 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
541 PN544_RF_READER_CMD_ACTIVATE_NEXT,
542 target->nfcid1, target->nfcid1_len, NULL);
543 } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
544 r = nfc_hci_get_param(hdev, PN544_RF_READER_F_GATE,
545 PN544_FELICA_ID, &uid_skb);
546 if (r < 0)
547 return r;
548
549 if (uid_skb->len != 8) {
550 kfree_skb(uid_skb);
551 return -EPROTO;
552 }
553
Arron Wang9c598442013-04-22 17:21:04 +0800554 /* Type F NFC-DEP IDm has prefix 0x01FE */
555 if ((uid_skb->data[0] == 0x01) && (uid_skb->data[1] == 0xfe)) {
556 kfree_skb(uid_skb);
557 r = nfc_hci_send_cmd(hdev,
Arron Wang928326f2012-09-27 17:32:56 +0800558 PN544_RF_READER_NFCIP1_INITIATOR_GATE,
559 PN544_HCI_CMD_CONTINUE_ACTIVATION,
560 NULL, 0, NULL);
Arron Wang9c598442013-04-22 17:21:04 +0800561 if (r < 0)
562 return r;
Arron Wang928326f2012-09-27 17:32:56 +0800563
Arron Wang9c598442013-04-22 17:21:04 +0800564 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
565 target->hci_reader_gate =
566 PN544_RF_READER_NFCIP1_INITIATOR_GATE;
567 } else {
568 r = nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
569 PN544_RF_READER_CMD_ACTIVATE_NEXT,
570 uid_skb->data, uid_skb->len, NULL);
571 kfree_skb(uid_skb);
572 }
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200573 } else if (target->supported_protocols & NFC_PROTO_ISO14443_MASK) {
574 /*
575 * TODO: maybe other ISO 14443 require some kind of continue
576 * activation, but for now we've seen only this one below.
577 */
578 if (target->sens_res == 0x4403) /* Type 4 Mifare DESFire */
579 r = nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
580 PN544_RF_READER_A_CMD_CONTINUE_ACTIVATION,
581 NULL, 0, NULL);
582 }
583
584 return r;
585}
586
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200587#define PN544_CB_TYPE_READER_F 1
588
589static void pn544_hci_data_exchange_cb(void *context, struct sk_buff *skb,
590 int err)
591{
592 struct pn544_hci_info *info = context;
593
594 switch (info->async_cb_type) {
595 case PN544_CB_TYPE_READER_F:
596 if (err == 0)
597 skb_pull(skb, 1);
598 info->async_cb(info->async_cb_context, skb, err);
599 break;
600 default:
601 if (err == 0)
602 kfree_skb(skb);
603 break;
604 }
605}
606
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200607#define MIFARE_CMD_AUTH_KEY_A 0x60
608#define MIFARE_CMD_AUTH_KEY_B 0x61
609#define MIFARE_CMD_HEADER 2
610#define MIFARE_UID_LEN 4
611#define MIFARE_KEY_LEN 6
612#define MIFARE_CMD_LEN 12
613/*
614 * Returns:
615 * <= 0: driver handled the data exchange
616 * 1: driver doesn't especially handle, please do standard processing
617 */
Arron Wange8107622012-09-27 17:32:58 +0800618static int pn544_hci_im_transceive(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200619 struct nfc_target *target,
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200620 struct sk_buff *skb, data_exchange_cb_t cb,
621 void *cb_context)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200622{
Eric Lapuyade412fda52012-09-18 19:45:48 +0200623 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200624
625 pr_info(DRIVER_DESC ": %s for gate=%d\n", __func__,
626 target->hci_reader_gate);
627
628 switch (target->hci_reader_gate) {
629 case NFC_HCI_RF_READER_A_GATE:
630 if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
631 /*
632 * It seems that pn544 is inverting key and UID for
633 * MIFARE authentication commands.
634 */
635 if (skb->len == MIFARE_CMD_LEN &&
636 (skb->data[0] == MIFARE_CMD_AUTH_KEY_A ||
637 skb->data[0] == MIFARE_CMD_AUTH_KEY_B)) {
638 u8 uid[MIFARE_UID_LEN];
639 u8 *data = skb->data + MIFARE_CMD_HEADER;
640
641 memcpy(uid, data + MIFARE_KEY_LEN,
642 MIFARE_UID_LEN);
643 memmove(data + MIFARE_UID_LEN, data,
644 MIFARE_KEY_LEN);
645 memcpy(data, uid, MIFARE_UID_LEN);
646 }
647
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200648 return nfc_hci_send_cmd_async(hdev,
649 target->hci_reader_gate,
650 PN544_MIFARE_CMD,
651 skb->data, skb->len,
652 cb, cb_context);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200653 } else
654 return 1;
655 case PN544_RF_READER_F_GATE:
656 *skb_push(skb, 1) = 0;
657 *skb_push(skb, 1) = 0;
658
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200659 info->async_cb_type = PN544_CB_TYPE_READER_F;
660 info->async_cb = cb;
661 info->async_cb_context = cb_context;
662
663 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
664 PN544_FELICA_RAW, skb->data,
665 skb->len,
666 pn544_hci_data_exchange_cb, info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200667 case PN544_RF_READER_JEWEL_GATE:
Eric Lapuyadef3e8fb52012-09-11 10:43:50 +0200668 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
669 PN544_JEWEL_RAW_CMD, skb->data,
670 skb->len, cb, cb_context);
Arron Wange8107622012-09-27 17:32:58 +0800671 case PN544_RF_READER_NFCIP1_INITIATOR_GATE:
672 *skb_push(skb, 1) = 0;
673
674 return nfc_hci_send_event(hdev, target->hci_reader_gate,
675 PN544_HCI_EVT_SND_DATA, skb->data,
676 skb->len);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200677 default:
678 return 1;
679 }
680}
681
Arron Wange8107622012-09-27 17:32:58 +0800682static int pn544_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
683{
Eric Lapuyade924d4a02012-12-04 16:44:25 +0100684 int r;
685
Arron Wange8107622012-09-27 17:32:58 +0800686 /* Set default false for multiple information chaining */
687 *skb_push(skb, 1) = 0;
688
Eric Lapuyade924d4a02012-12-04 16:44:25 +0100689 r = nfc_hci_send_event(hdev, PN544_RF_READER_NFCIP1_TARGET_GATE,
690 PN544_HCI_EVT_SND_DATA, skb->data, skb->len);
691
692 kfree_skb(skb);
693
694 return r;
Arron Wange8107622012-09-27 17:32:58 +0800695}
696
Eric Lapuyade412fda52012-09-18 19:45:48 +0200697static int pn544_hci_check_presence(struct nfc_hci_dev *hdev,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200698 struct nfc_target *target)
699{
Arron Wangda052852012-09-27 17:32:59 +0800700 pr_debug("supported protocol %d", target->supported_protocols);
701 if (target->supported_protocols & (NFC_PROTO_ISO14443_MASK |
702 NFC_PROTO_ISO14443_B_MASK)) {
703 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
704 PN544_RF_READER_CMD_PRESENCE_CHECK,
705 NULL, 0, NULL);
706 } else if (target->supported_protocols & NFC_PROTO_MIFARE_MASK) {
707 if (target->nfcid1_len != 4 && target->nfcid1_len != 7 &&
708 target->nfcid1_len != 10)
Eric Lapuyade632c0162012-10-02 17:27:36 +0200709 return -EOPNOTSUPP;
Arron Wangda052852012-09-27 17:32:59 +0800710
711 return nfc_hci_send_cmd(hdev, NFC_HCI_RF_READER_A_GATE,
712 PN544_RF_READER_CMD_ACTIVATE_NEXT,
713 target->nfcid1, target->nfcid1_len, NULL);
714 } else if (target->supported_protocols & NFC_PROTO_JEWEL_MASK) {
715 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
716 PN544_JEWEL_RAW_CMD, NULL, 0, NULL);
717 } else if (target->supported_protocols & NFC_PROTO_FELICA_MASK) {
718 return nfc_hci_send_cmd(hdev, PN544_RF_READER_F_GATE,
719 PN544_FELICA_RAW, NULL, 0, NULL);
720 } else if (target->supported_protocols & NFC_PROTO_NFC_DEP_MASK) {
721 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
722 PN544_HCI_CMD_ATTREQUEST,
723 NULL, 0, NULL);
724 }
725
726 return 0;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200727}
728
Eric Lapuyade40d06d32012-12-04 16:43:24 +0100729/*
730 * Returns:
731 * <= 0: driver handled the event, skb consumed
732 * 1: driver does not handle the event, please do standard processing
733 */
Eric Lapuyade27c31192012-11-28 15:48:44 +0100734static int pn544_hci_event_received(struct nfc_hci_dev *hdev, u8 gate, u8 event,
735 struct sk_buff *skb)
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800736{
737 struct sk_buff *rgb_skb = NULL;
Eric Lapuyade40d06d32012-12-04 16:43:24 +0100738 int r;
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800739
740 pr_debug("hci event %d", event);
741 switch (event) {
742 case PN544_HCI_EVT_ACTIVATED:
Eric Lapuyade40d06d32012-12-04 16:43:24 +0100743 if (gate == PN544_RF_READER_NFCIP1_INITIATOR_GATE) {
Eric Lapuyade27c31192012-11-28 15:48:44 +0100744 r = nfc_hci_target_discovered(hdev, gate);
Eric Lapuyade40d06d32012-12-04 16:43:24 +0100745 } else if (gate == PN544_RF_READER_NFCIP1_TARGET_GATE) {
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800746 r = nfc_hci_get_param(hdev, gate, PN544_DEP_ATR_REQ,
Eric Lapuyade27c31192012-11-28 15:48:44 +0100747 &rgb_skb);
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800748 if (r < 0)
749 goto exit;
750
Eric Lapuyade27c31192012-11-28 15:48:44 +0100751 r = nfc_tm_activated(hdev->ndev, NFC_PROTO_NFC_DEP_MASK,
752 NFC_COMM_PASSIVE, rgb_skb->data,
753 rgb_skb->len);
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800754
755 kfree_skb(rgb_skb);
Eric Lapuyade40d06d32012-12-04 16:43:24 +0100756 } else {
757 r = -EINVAL;
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800758 }
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800759 break;
760 case PN544_HCI_EVT_DEACTIVATED:
Eric Lapuyade27c31192012-11-28 15:48:44 +0100761 r = nfc_hci_send_event(hdev, gate, NFC_HCI_EVT_END_OPERATION,
762 NULL, 0);
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800763 break;
Arron Wange8107622012-09-27 17:32:58 +0800764 case PN544_HCI_EVT_RCV_DATA:
765 if (skb->len < 2) {
766 r = -EPROTO;
767 goto exit;
768 }
769
770 if (skb->data[0] != 0) {
771 pr_debug("data0 %d", skb->data[0]);
772 r = -EPROTO;
773 goto exit;
774 }
775
776 skb_pull(skb, 2);
Eric Lapuyade27c31192012-11-28 15:48:44 +0100777 return nfc_tm_data_received(hdev->ndev, skb);
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800778 default:
Eric Lapuyade40d06d32012-12-04 16:43:24 +0100779 return 1;
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800780 }
781
782exit:
783 kfree_skb(skb);
Eric Lapuyade27c31192012-11-28 15:48:44 +0100784
785 return r;
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800786}
787
Eric Lapuyade412fda52012-09-18 19:45:48 +0200788static struct nfc_hci_ops pn544_hci_ops = {
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200789 .open = pn544_hci_open,
790 .close = pn544_hci_close,
791 .hci_ready = pn544_hci_ready,
792 .xmit = pn544_hci_xmit,
793 .start_poll = pn544_hci_start_poll,
Arron Wangc40d1742012-09-27 17:32:57 +0800794 .dep_link_up = pn544_hci_dep_link_up,
795 .dep_link_down = pn544_hci_dep_link_down,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200796 .target_from_gate = pn544_hci_target_from_gate,
797 .complete_target_discovered = pn544_hci_complete_target_discovered,
Arron Wange8107622012-09-27 17:32:58 +0800798 .im_transceive = pn544_hci_im_transceive,
799 .tm_send = pn544_hci_tm_send,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200800 .check_presence = pn544_hci_check_presence,
Arron Wangf7a5f6c2012-09-27 17:32:55 +0800801 .event_received = pn544_hci_event_received,
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200802};
803
Eric Lapuyade97f18412012-10-02 18:44:06 +0200804int pn544_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops, char *llc_name,
805 int phy_headroom, int phy_tailroom, int phy_payload,
806 struct nfc_hci_dev **hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200807{
808 struct pn544_hci_info *info;
Samuel Ortiz390a1bd2012-12-19 19:11:32 +0100809 u32 protocols, se;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200810 struct nfc_hci_init_data init_data;
Eric Lapuyade97f18412012-10-02 18:44:06 +0200811 int r;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200812
813 info = kzalloc(sizeof(struct pn544_hci_info), GFP_KERNEL);
814 if (!info) {
Eric Lapuyade97f18412012-10-02 18:44:06 +0200815 pr_err("Cannot allocate memory for pn544_hci_info.\n");
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200816 r = -ENOMEM;
817 goto err_info_alloc;
818 }
819
Eric Lapuyade97f18412012-10-02 18:44:06 +0200820 info->phy_ops = phy_ops;
821 info->phy_id = phy_id;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200822 info->state = PN544_ST_COLD;
823 mutex_init(&info->info_lock);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200824
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200825 init_data.gate_count = ARRAY_SIZE(pn544_gates);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200826
Eric Lapuyadea10d5952012-06-05 14:42:11 +0200827 memcpy(init_data.gates, pn544_gates, sizeof(pn544_gates));
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200828
829 /*
830 * TODO: Session id must include the driver name + some bus addr
831 * persistent info to discriminate 2 identical chips
832 */
833 strcpy(init_data.session_id, "ID544HCI");
834
835 protocols = NFC_PROTO_JEWEL_MASK |
836 NFC_PROTO_MIFARE_MASK |
837 NFC_PROTO_FELICA_MASK |
838 NFC_PROTO_ISO14443_MASK |
Samuel Ortiz01d719a2012-07-04 00:14:04 +0200839 NFC_PROTO_ISO14443_B_MASK |
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200840 NFC_PROTO_NFC_DEP_MASK;
841
Samuel Ortiz390a1bd2012-12-19 19:11:32 +0100842 se = NFC_SE_UICC | NFC_SE_EMBEDDED;
843
Eric Lapuyadebf71ab82012-12-18 14:15:49 +0100844 info->hdev = nfc_hci_allocate_device(&pn544_hci_ops, &init_data, 0,
Samuel Ortiz390a1bd2012-12-19 19:11:32 +0100845 protocols, se, llc_name,
Eric Lapuyade97f18412012-10-02 18:44:06 +0200846 phy_headroom + PN544_CMDS_HEADROOM,
847 phy_tailroom, phy_payload);
Eric Lapuyade412fda52012-09-18 19:45:48 +0200848 if (!info->hdev) {
Eric Lapuyade97f18412012-10-02 18:44:06 +0200849 pr_err("Cannot allocate nfc hdev.\n");
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200850 r = -ENOMEM;
Eric Lapuyade412fda52012-09-18 19:45:48 +0200851 goto err_alloc_hdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200852 }
853
Eric Lapuyade412fda52012-09-18 19:45:48 +0200854 nfc_hci_set_clientdata(info->hdev, info);
855
856 r = nfc_hci_register_device(info->hdev);
857 if (r)
858 goto err_regdev;
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200859
Eric Lapuyade97f18412012-10-02 18:44:06 +0200860 *hdev = info->hdev;
861
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200862 return 0;
863
Eric Lapuyade412fda52012-09-18 19:45:48 +0200864err_regdev:
865 nfc_hci_free_device(info->hdev);
866
867err_alloc_hdev:
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200868 kfree(info);
869
870err_info_alloc:
871 return r;
872}
Samuel Ortizaa741032012-12-18 16:26:23 +0100873EXPORT_SYMBOL(pn544_hci_probe);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200874
Eric Lapuyade97f18412012-10-02 18:44:06 +0200875void pn544_hci_remove(struct nfc_hci_dev *hdev)
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200876{
Eric Lapuyade97f18412012-10-02 18:44:06 +0200877 struct pn544_hci_info *info = nfc_hci_get_clientdata(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200878
Eric Lapuyade97f18412012-10-02 18:44:06 +0200879 nfc_hci_unregister_device(hdev);
880 nfc_hci_free_device(hdev);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200881 kfree(info);
Eric Lapuyadebbed0de2012-05-07 12:31:29 +0200882}
Samuel Ortizaa741032012-12-18 16:26:23 +0100883EXPORT_SYMBOL(pn544_hci_remove);
884
885MODULE_LICENSE("GPL");
886MODULE_DESCRIPTION(DRIVER_DESC);