blob: 69213f37b7ba93416724dc9376d0e29fc285f02c [file] [log] [blame]
Christophe Ricard68957302014-03-25 06:51:47 +01001/*
2 * HCI based Driver for STMicroelectronics NFC Chip
3 *
4 * Copyright (C) 2014 STMicroelectronics SAS. 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, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/crc-ccitt.h>
20#include <linux/module.h>
21#include <linux/delay.h>
22#include <linux/slab.h>
23#include <linux/miscdevice.h>
24#include <linux/interrupt.h>
25#include <linux/gpio.h>
26#include <linux/i2c.h>
27
28#include <linux/nfc.h>
29#include <net/nfc/hci.h>
30#include <net/nfc/llc.h>
31
32#include <uapi/linux/nfc.h>
33
34#include "st21nfca.h"
35#include <linux/platform_data/st21nfca.h>
36
37#define DRIVER_DESC "HCI NFC driver for ST21NFCA"
38
39#define FULL_VERSION_LEN 3
40
41/* Proprietary gates, events, commands and registers */
42
43/* Commands that apply to all RF readers */
44#define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK 0x30
45
46#define ST21NFCA_RF_READER_ISO15693_GATE 0x12
47
48/*
49 * Reader gate for communication with contact-less cards using Type A
50 * protocol ISO14443-3 but not compliant with ISO14443-4
51 */
52#define ST21NFCA_RF_READER_14443_3_A_GATE 0x15
53#define ST21NFCA_RF_READER_14443_3_A_UID 0x02
54#define ST21NFCA_RF_READER_14443_3_A_ATQA 0x03
55#define ST21NFCA_RF_READER_14443_3_A_SAK 0x04
56
57#define ST21NFCA_DEVICE_MGNT_GATE 0x01
58#define ST21NFCA_DEVICE_MGNT_PIPE 0x02
59#define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
60
61
62static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
63
64static struct nfc_hci_gate st21nfca_gates[] = {
65 {NFC_HCI_ADMIN_GATE, NFC_HCI_INVALID_PIPE},
66 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
67 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
68 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_INVALID_PIPE},
69 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
70 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
71 {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
72 {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
73 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
74};
75/* Largest headroom needed for outgoing custom commands */
76#define ST21NFCA_CMDS_HEADROOM 7
77
78static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
79{
80 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
81 int r;
82
83 mutex_lock(&info->info_lock);
84
85 if (info->state != ST21NFCA_ST_COLD) {
86 r = -EBUSY;
87 goto out;
88 }
89
90 r = info->phy_ops->enable(info->phy_id);
91
92 if (r == 0)
93 info->state = ST21NFCA_ST_READY;
94
95out:
96 mutex_unlock(&info->info_lock);
97 return r;
98}
99
100static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
101{
102 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
103
104 mutex_lock(&info->info_lock);
105
106 if (info->state == ST21NFCA_ST_COLD)
107 goto out;
108
109 info->phy_ops->disable(info->phy_id);
110 info->state = ST21NFCA_ST_COLD;
111
112out:
113 mutex_unlock(&info->info_lock);
114}
115
116static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
117{
118 struct sk_buff *skb;
119
120 u8 param;
121 int r;
122
123 param = NFC_HCI_UICC_HOST_ID;
124 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
125 NFC_HCI_ADMIN_WHITELIST, &param, 1);
126 if (r < 0)
127 return r;
128
129 /* Set NFC_MODE in device management gate to enable */
130 r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
131 ST21NFCA_NFC_MODE, &skb);
132 if (r < 0)
133 return r;
134
135 if (skb->data[0] == 0) {
136 kfree_skb(skb);
137 param = 1;
138
139 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
140 ST21NFCA_NFC_MODE, &param, 1);
141 if (r < 0)
142 return r;
143 }
144
145 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
146 NFC_HCI_EVT_END_OPERATION, NULL, 0);
147 if (r < 0)
148 return r;
149
150 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
151 NFC_HCI_ID_MGMT_VERSION_SW, &skb);
152 if (r < 0)
153 return r;
154
155 if (skb->len != FULL_VERSION_LEN) {
156 kfree_skb(skb);
157 return -EINVAL;
158 }
159
160 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
161 DUMP_PREFIX_NONE, 16, 1,
162 skb->data, FULL_VERSION_LEN, false);
163
164 kfree_skb(skb);
165
166 return 0;
167}
168
169static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
170{
171 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
172
173 return info->phy_ops->write(info->phy_id, skb);
174}
175
176static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
177 u32 im_protocols, u32 tm_protocols)
178{
179 int r;
180
181 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
182 __func__, im_protocols, tm_protocols);
183
184 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
185 NFC_HCI_EVT_END_OPERATION, NULL, 0);
186 if (r < 0)
187 return r;
188 if (im_protocols) {
189 /*
190 * enable polling according to im_protocols & tm_protocols
191 * - CLOSE pipe according to im_protocols & tm_protocols
192 */
193 if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
194 r = nfc_hci_disconnect_gate(hdev,
195 NFC_HCI_RF_READER_B_GATE);
196 if (r < 0)
197 return r;
198 }
199
200 if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
201 r = nfc_hci_disconnect_gate(hdev,
202 NFC_HCI_RF_READER_A_GATE);
203 if (r < 0)
204 return r;
205 }
206
207 if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
208 r = nfc_hci_disconnect_gate(hdev,
209 ST21NFCA_RF_READER_F_GATE);
210 if (r < 0)
211 return r;
212 }
213
214 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
215 r = nfc_hci_disconnect_gate(hdev,
216 ST21NFCA_RF_READER_14443_3_A_GATE);
217 if (r < 0)
218 return r;
219 }
220
221 if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
222 r = nfc_hci_disconnect_gate(hdev,
223 ST21NFCA_RF_READER_ISO15693_GATE);
224 if (r < 0)
225 return r;
226 }
227
228 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
229 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
230 if (r < 0)
231 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
232 NFC_HCI_EVT_END_OPERATION, NULL, 0);
233 }
234 return r;
235}
236
237static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
238{
239 int r;
240 struct sk_buff *atqa_skb = NULL;
241
242 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
243 ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
244 if (r < 0)
245 goto exit;
246
247 if (atqa_skb->len != 2) {
248 r = -EPROTO;
249 goto exit;
250 }
251
252 *atqa = be16_to_cpu(*(u16 *) atqa_skb->data);
253
254exit:
255 kfree_skb(atqa_skb);
256 return r;
257}
258
259static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
260{
261 int r;
262 struct sk_buff *sak_skb = NULL;
263
264 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
265 ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
266 if (r < 0)
267 goto exit;
268
269 if (sak_skb->len != 1) {
270 r = -EPROTO;
271 goto exit;
272 }
273
274 *sak = sak_skb->data[0];
275
276exit:
277 kfree_skb(sak_skb);
278 return r;
279}
280
281static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
282 int *len)
283{
284 int r;
285 struct sk_buff *uid_skb = NULL;
286
287 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
288 ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
289 if (r < 0)
290 goto exit;
291
292 if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
293 r = -EPROTO;
294 goto exit;
295 }
296
297 gate = uid_skb->data;
298 *len = uid_skb->len;
299exit:
300 kfree_skb(uid_skb);
301 return r;
302}
303
304static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
305 struct nfc_target *target)
306{
307 int r, len;
308 u16 atqa;
309 u8 sak;
310 u8 uid[NFC_NFCID1_MAXSIZE];
311
312 switch (gate) {
313 case ST21NFCA_RF_READER_F_GATE:
314 target->supported_protocols = NFC_PROTO_FELICA_MASK;
315 break;
316 case ST21NFCA_RF_READER_14443_3_A_GATE:
317 /* ISO14443-3 type 1 or 2 tags */
318 r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
319 if (r < 0)
320 return r;
321 if (atqa == 0x000c) {
322 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
323 target->sens_res = 0x0c00;
324 } else {
325 r = st21nfca_get_iso14443_3_sak(hdev, &sak);
326 if (r < 0)
327 return r;
328
329 r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
330 if (r < 0)
331 return r;
332
333 target->supported_protocols =
334 nfc_hci_sak_to_protocol(sak);
335 if (target->supported_protocols == 0xffffffff)
336 return -EPROTO;
337
338 target->sens_res = atqa;
339 target->sel_res = sak;
340 memcpy(target->nfcid1, uid, len);
341 target->nfcid1_len = len;
342 }
343
344 break;
345 default:
346 return -EPROTO;
347 }
348
349 return 0;
350}
351
352/*
353 * Returns:
354 * <= 0: driver handled the data exchange
355 * 1: driver doesn't especially handle, please do standard processing
356 */
357static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
358 struct nfc_target *target,
359 struct sk_buff *skb,
360 data_exchange_cb_t cb, void *cb_context)
361{
362 pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
363 target->hci_reader_gate, skb->len);
364
365 switch (target->hci_reader_gate) {
366 case ST21NFCA_RF_READER_F_GATE:
367 *skb_push(skb, 1) = 0x1a;
368 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
369 ST21NFCA_WR_XCHG_DATA, skb->data,
370 skb->len, cb, cb_context);
371 case ST21NFCA_RF_READER_14443_3_A_GATE:
372 *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
373
374 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
375 ST21NFCA_WR_XCHG_DATA, skb->data,
376 skb->len, cb, cb_context);
377 default:
378 return 1;
379 }
380}
381
382static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
383 struct nfc_target *target)
384{
385 u8 fwi = 0x11;
386 switch (target->hci_reader_gate) {
387 case NFC_HCI_RF_READER_A_GATE:
388 case NFC_HCI_RF_READER_B_GATE:
389 /*
390 * PRESENCE_CHECK on those gates is available
391 * However, the answer to this command is taking 3 * fwi
392 * if the card is no present.
393 * Instead, we send an empty I-Frame with a very short
394 * configurable fwi ~604µs.
395 */
396 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
397 ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
398 case ST21NFCA_RF_READER_14443_3_A_GATE:
399 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
400 ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
401 NULL, 0, NULL);
402 default:
403 return -EOPNOTSUPP;
404 }
405}
406
407static struct nfc_hci_ops st21nfca_hci_ops = {
408 .open = st21nfca_hci_open,
409 .close = st21nfca_hci_close,
410 .hci_ready = st21nfca_hci_ready,
411 .xmit = st21nfca_hci_xmit,
412 .start_poll = st21nfca_hci_start_poll,
413 .target_from_gate = st21nfca_hci_target_from_gate,
414 .im_transceive = st21nfca_hci_im_transceive,
415 .check_presence = st21nfca_hci_check_presence,
416};
417
418int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
419 char *llc_name, int phy_headroom, int phy_tailroom,
420 int phy_payload, struct nfc_hci_dev **hdev)
421{
422 struct st21nfca_hci_info *info;
423 int r = 0;
424 int dev_num;
425 u32 protocols;
426 struct nfc_hci_init_data init_data;
427 unsigned long quirks = 0;
428
429 info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
430 if (!info) {
431 r = -ENOMEM;
432 goto err_alloc_hdev;
433 }
434
435 info->phy_ops = phy_ops;
436 info->phy_id = phy_id;
437 info->state = ST21NFCA_ST_COLD;
438 mutex_init(&info->info_lock);
439
440 init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
441
442 memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
443
444 /*
445 * Session id must include the driver name + i2c bus addr
446 * persistent info to discriminate 2 identical chips
447 */
448 dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
449 if (dev_num >= ST21NFCA_NUM_DEVICES)
450 goto err_alloc_hdev;
451
452 scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
453 "ST21AH", dev_num);
454
455 protocols = NFC_PROTO_JEWEL_MASK |
456 NFC_PROTO_MIFARE_MASK |
457 NFC_PROTO_FELICA_MASK |
458 NFC_PROTO_ISO14443_MASK |
459 NFC_PROTO_ISO14443_B_MASK;
460
461 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
462
463 info->hdev =
464 nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
465 protocols, llc_name,
466 phy_headroom + ST21NFCA_CMDS_HEADROOM,
467 phy_tailroom, phy_payload);
468
469 if (!info->hdev) {
470 pr_err("Cannot allocate nfc hdev.\n");
471 r = -ENOMEM;
472 goto err_alloc_hdev;
473 }
474
475 nfc_hci_set_clientdata(info->hdev, info);
476
477 r = nfc_hci_register_device(info->hdev);
478 if (r)
479 goto err_regdev;
480
481 *hdev = info->hdev;
482
483 return 0;
484
485err_regdev:
486 nfc_hci_free_device(info->hdev);
487
488err_alloc_hdev:
489 kfree(info);
490
491 return r;
492}
493EXPORT_SYMBOL(st21nfca_hci_probe);
494
495void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
496{
497 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
498
499 nfc_hci_unregister_device(hdev);
500 nfc_hci_free_device(hdev);
501 kfree(info);
502}
503EXPORT_SYMBOL(st21nfca_hci_remove);
504
505MODULE_LICENSE("GPL");
506MODULE_DESCRIPTION(DRIVER_DESC);