Merge branch 'irq-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / drivers / nfc / st21nfca / st21nfca.c
1 /*
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/module.h>
20 #include <linux/nfc.h>
21 #include <net/nfc/hci.h>
22 #include <net/nfc/llc.h>
23
24 #include "st21nfca.h"
25 #include "st21nfca_dep.h"
26
27 #define DRIVER_DESC "HCI NFC driver for ST21NFCA"
28
29 #define FULL_VERSION_LEN 3
30
31 /* Proprietary gates, events, commands and registers */
32
33 /* Commands that apply to all RF readers */
34 #define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK   0x30
35
36 #define ST21NFCA_RF_READER_ISO15693_GATE        0x12
37 #define ST21NFCA_RF_READER_ISO15693_INVENTORY   0x01
38
39 /*
40  * Reader gate for communication with contact-less cards using Type A
41  * protocol ISO14443-3 but not compliant with ISO14443-4
42  */
43 #define ST21NFCA_RF_READER_14443_3_A_GATE       0x15
44 #define ST21NFCA_RF_READER_14443_3_A_UID        0x02
45 #define ST21NFCA_RF_READER_14443_3_A_ATQA       0x03
46 #define ST21NFCA_RF_READER_14443_3_A_SAK        0x04
47
48 #define ST21NFCA_RF_READER_F_DATARATE           0x01
49 #define ST21NFCA_RF_READER_F_DATARATE_106       0x01
50 #define ST21NFCA_RF_READER_F_DATARATE_212       0x02
51 #define ST21NFCA_RF_READER_F_DATARATE_424       0x04
52 #define ST21NFCA_RF_READER_F_POL_REQ            0x02
53 #define ST21NFCA_RF_READER_F_POL_REQ_DEFAULT    0xffff0000
54 #define ST21NFCA_RF_READER_F_NFCID2             0x03
55 #define ST21NFCA_RF_READER_F_NFCID1             0x04
56
57 #define ST21NFCA_RF_CARD_F_MODE                 0x01
58 #define ST21NFCA_RF_CARD_F_NFCID2_LIST          0x04
59 #define ST21NFCA_RF_CARD_F_NFCID1               0x05
60 #define ST21NFCA_RF_CARD_F_SENS_RES             0x06
61 #define ST21NFCA_RF_CARD_F_SEL_RES              0x07
62 #define ST21NFCA_RF_CARD_F_DATARATE             0x08
63 #define ST21NFCA_RF_CARD_F_DATARATE_212_424     0x01
64
65 #define ST21NFCA_DEVICE_MGNT_GATE               0x01
66 #define ST21NFCA_DEVICE_MGNT_PIPE               0x02
67
68 #define ST21NFCA_DM_GETINFO                     0x13
69 #define ST21NFCA_DM_GETINFO_PIPE_LIST           0x02
70 #define ST21NFCA_DM_GETINFO_PIPE_INFO           0x01
71 #define ST21NFCA_DM_PIPE_CREATED                0x02
72 #define ST21NFCA_DM_PIPE_OPEN                   0x04
73 #define ST21NFCA_DM_RF_ACTIVE                   0x80
74 #define ST21NFCA_DM_DISCONNECT                  0x30
75
76 #define ST21NFCA_DM_IS_PIPE_OPEN(p) \
77         ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
78
79 #define ST21NFCA_NFC_MODE                       0x03    /* NFC_MODE parameter*/
80
81 static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
82
83 static struct nfc_hci_gate st21nfca_gates[] = {
84         {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
85         {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
86         {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
87         {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
88         {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
89         {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
90         {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
91         {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
92         {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
93         {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
94         {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE},
95 };
96
97 struct st21nfca_pipe_info {
98         u8 pipe_state;
99         u8 src_host_id;
100         u8 src_gate_id;
101         u8 dst_host_id;
102         u8 dst_gate_id;
103 } __packed;
104
105 /* Largest headroom needed for outgoing custom commands */
106 #define ST21NFCA_CMDS_HEADROOM  7
107
108 static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
109 {
110         int i, j, r;
111         struct sk_buff *skb_pipe_list, *skb_pipe_info;
112         struct st21nfca_pipe_info *info;
113
114         u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
115                 NFC_HCI_TERMINAL_HOST_ID
116         };
117         u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
118                 NFC_HCI_TERMINAL_HOST_ID, 0
119         };
120
121         skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
122         if (!skb_pipe_list) {
123                 r = -ENOMEM;
124                 goto free_list;
125         }
126
127         skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
128         if (!skb_pipe_info) {
129                 r = -ENOMEM;
130                 goto free_info;
131         }
132
133         /* On ST21NFCA device pipes number are dynamics
134          * A maximum of 16 pipes can be created at the same time
135          * If pipes are already created, hci_dev_up will fail.
136          * Doing a clear all pipe is a bad idea because:
137          * - It does useless EEPROM cycling
138          * - It might cause issue for secure elements support
139          * (such as removing connectivity or APDU reader pipe)
140          * A better approach on ST21NFCA is to:
141          * - get a pipe list for each host.
142          * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
143          * (TODO Later on UICC HOST and eSE HOST)
144          * - get pipe information
145          * - match retrieved pipe list in st21nfca_gates
146          * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
147          * with ST21NFCA_DEVICE_MGNT_PIPE.
148          * Pipe can be closed and need to be open.
149          */
150         r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
151                 ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
152         if (r < 0)
153                 goto free_info;
154
155         /* Get pipe list */
156         r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
157                         ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
158                         &skb_pipe_list);
159         if (r < 0)
160                 goto free_info;
161
162         /* Complete the existing gate_pipe table */
163         for (i = 0; i < skb_pipe_list->len; i++) {
164                 pipe_info[2] = skb_pipe_list->data[i];
165                 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
166                                         ST21NFCA_DM_GETINFO, pipe_info,
167                                         sizeof(pipe_info), &skb_pipe_info);
168
169                 if (r)
170                         continue;
171
172                 /*
173                  * Match pipe ID and gate ID
174                  * Output format from ST21NFC_DM_GETINFO is:
175                  * - pipe state (1byte)
176                  * - source hid (1byte)
177                  * - source gid (1byte)
178                  * - destination hid (1byte)
179                  * - destination gid (1byte)
180                  */
181                 info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
182                 for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
183                         (st21nfca_gates[j].gate != info->dst_gate_id);
184                         j++)
185                         ;
186
187                 if (j < ARRAY_SIZE(st21nfca_gates) &&
188                         st21nfca_gates[j].gate == info->dst_gate_id &&
189                         ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
190                         st21nfca_gates[j].pipe = pipe_info[2];
191                         hdev->gate2pipe[st21nfca_gates[j].gate] =
192                                 st21nfca_gates[j].pipe;
193                 }
194         }
195
196         /*
197          * 3 gates have a well known pipe ID.
198          * They will never appear in the pipe list
199          */
200         if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
201                 for (i = skb_pipe_list->len + 3;
202                                 i < ARRAY_SIZE(st21nfca_gates); i++) {
203                         r = nfc_hci_connect_gate(hdev,
204                                         NFC_HCI_HOST_CONTROLLER_ID,
205                                         st21nfca_gates[i].gate,
206                                         st21nfca_gates[i].pipe);
207                         if (r < 0)
208                                 goto free_info;
209                 }
210         }
211
212         memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
213 free_info:
214         kfree_skb(skb_pipe_info);
215 free_list:
216         kfree_skb(skb_pipe_list);
217         return r;
218 }
219
220 static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
221 {
222         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
223         int r;
224
225         mutex_lock(&info->info_lock);
226
227         if (info->state != ST21NFCA_ST_COLD) {
228                 r = -EBUSY;
229                 goto out;
230         }
231
232         r = info->phy_ops->enable(info->phy_id);
233
234         if (r == 0)
235                 info->state = ST21NFCA_ST_READY;
236
237 out:
238         mutex_unlock(&info->info_lock);
239         return r;
240 }
241
242 static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
243 {
244         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
245
246         mutex_lock(&info->info_lock);
247
248         if (info->state == ST21NFCA_ST_COLD)
249                 goto out;
250
251         info->phy_ops->disable(info->phy_id);
252         info->state = ST21NFCA_ST_COLD;
253
254 out:
255         mutex_unlock(&info->info_lock);
256 }
257
258 static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
259 {
260         struct sk_buff *skb;
261
262         u8 param;
263         int r;
264
265         param = NFC_HCI_UICC_HOST_ID;
266         r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
267                               NFC_HCI_ADMIN_WHITELIST, &param, 1);
268         if (r < 0)
269                 return r;
270
271         /* Set NFC_MODE in device management gate to enable */
272         r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
273                               ST21NFCA_NFC_MODE, &skb);
274         if (r < 0)
275                 return r;
276
277         if (skb->data[0] == 0) {
278                 kfree_skb(skb);
279                 param = 1;
280
281                 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
282                                         ST21NFCA_NFC_MODE, &param, 1);
283                 if (r < 0)
284                         return r;
285         }
286
287         r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
288                                NFC_HCI_EVT_END_OPERATION, NULL, 0);
289         if (r < 0)
290                 return r;
291
292         r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
293                               NFC_HCI_ID_MGMT_VERSION_SW, &skb);
294         if (r < 0)
295                 return r;
296
297         if (skb->len != FULL_VERSION_LEN) {
298                 kfree_skb(skb);
299                 return -EINVAL;
300         }
301
302         print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
303                        DUMP_PREFIX_NONE, 16, 1,
304                        skb->data, FULL_VERSION_LEN, false);
305
306         kfree_skb(skb);
307
308         return 0;
309 }
310
311 static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
312 {
313         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
314
315         return info->phy_ops->write(info->phy_id, skb);
316 }
317
318 static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
319                                    u32 im_protocols, u32 tm_protocols)
320 {
321         int r;
322         u32 pol_req;
323         u8 param[19];
324         struct sk_buff *datarate_skb;
325
326         pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
327                 __func__, im_protocols, tm_protocols);
328
329         r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
330                                NFC_HCI_EVT_END_OPERATION, NULL, 0);
331         if (r < 0)
332                 return r;
333         if (im_protocols) {
334                 /*
335                  * enable polling according to im_protocols & tm_protocols
336                  * - CLOSE pipe according to im_protocols & tm_protocols
337                  */
338                 if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
339                         r = nfc_hci_disconnect_gate(hdev,
340                                         NFC_HCI_RF_READER_B_GATE);
341                         if (r < 0)
342                                 return r;
343                 }
344
345                 if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
346                         r = nfc_hci_disconnect_gate(hdev,
347                                         NFC_HCI_RF_READER_A_GATE);
348                         if (r < 0)
349                                 return r;
350                 }
351
352                 if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
353                         r = nfc_hci_disconnect_gate(hdev,
354                                         ST21NFCA_RF_READER_F_GATE);
355                         if (r < 0)
356                                 return r;
357                 } else {
358                         hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
359                                                                &hdev->gb_len);
360
361                         if (hdev->gb == NULL || hdev->gb_len == 0) {
362                                 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
363                                 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
364                         }
365
366                         param[0] = ST21NFCA_RF_READER_F_DATARATE_106 |
367                             ST21NFCA_RF_READER_F_DATARATE_212 |
368                             ST21NFCA_RF_READER_F_DATARATE_424;
369                         r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
370                                               ST21NFCA_RF_READER_F_DATARATE,
371                                               param, 1);
372                         if (r < 0)
373                                 return r;
374
375                         pol_req = be32_to_cpu((__force __be32)
376                                         ST21NFCA_RF_READER_F_POL_REQ_DEFAULT);
377                         r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
378                                               ST21NFCA_RF_READER_F_POL_REQ,
379                                               (u8 *) &pol_req, 4);
380                         if (r < 0)
381                                 return r;
382                 }
383
384                 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
385                         r = nfc_hci_disconnect_gate(hdev,
386                                         ST21NFCA_RF_READER_14443_3_A_GATE);
387                         if (r < 0)
388                                 return r;
389                 }
390
391                 if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
392                         r = nfc_hci_disconnect_gate(hdev,
393                                         ST21NFCA_RF_READER_ISO15693_GATE);
394                         if (r < 0)
395                                 return r;
396                 }
397
398                 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
399                                        NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
400                 if (r < 0)
401                         nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
402                                            NFC_HCI_EVT_END_OPERATION, NULL, 0);
403         }
404
405         if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
406                 r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE,
407                                       ST21NFCA_RF_CARD_F_DATARATE,
408                                       &datarate_skb);
409                 if (r < 0)
410                         return r;
411
412                 /* Configure the maximum supported datarate to 424Kbps */
413                 if (datarate_skb->len > 0 &&
414                     datarate_skb->data[0] !=
415                     ST21NFCA_RF_CARD_F_DATARATE_212_424) {
416                         param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424;
417                         r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
418                                               ST21NFCA_RF_CARD_F_DATARATE,
419                                               param, 1);
420                         if (r < 0)
421                                 return r;
422                 }
423
424                 /*
425                  * Configure sens_res
426                  *
427                  * NFC Forum Digital Spec Table 7:
428                  * NFCID1 size: triple (10 bytes)
429                  */
430                 param[0] = 0x00;
431                 param[1] = 0x08;
432                 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
433                                       ST21NFCA_RF_CARD_F_SENS_RES, param, 2);
434                 if (r < 0)
435                         return r;
436
437                 /*
438                  * Configure sel_res
439                  *
440                  * NFC Forum Digistal Spec Table 17:
441                  * b3 set to 0b (value b7-b6):
442                  * - 10b: Configured for NFC-DEP Protocol
443                  */
444                 param[0] = 0x40;
445                 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
446                                       ST21NFCA_RF_CARD_F_SEL_RES, param, 1);
447                 if (r < 0)
448                         return r;
449
450                 /* Configure NFCID1 Random uid */
451                 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
452                                       ST21NFCA_RF_CARD_F_NFCID1, NULL, 0);
453                 if (r < 0)
454                         return r;
455
456                 /* Configure NFCID2_LIST */
457                 /* System Code */
458                 param[0] = 0x00;
459                 param[1] = 0x00;
460                 /* NFCID2 */
461                 param[2] = 0x01;
462                 param[3] = 0xfe;
463                 param[4] = 'S';
464                 param[5] = 'T';
465                 param[6] = 'M';
466                 param[7] = 'i';
467                 param[8] = 'c';
468                 param[9] = 'r';
469                 /* 8 byte Pad bytes used for polling respone frame */
470
471                 /*
472                  * Configuration byte:
473                  * - bit 0: define the default NFCID2 entry used when the
474                  * system code is equal to 'FFFF'
475                  * - bit 1: use a random value for lowest 6 bytes of
476                  * NFCID2 value
477                  * - bit 2: ignore polling request frame if request code
478                  * is equal to '01'
479                  * - Other bits are RFU
480                  */
481                 param[18] = 0x01;
482                 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
483                                       ST21NFCA_RF_CARD_F_NFCID2_LIST, param,
484                                       19);
485                 if (r < 0)
486                         return r;
487
488                 param[0] = 0x02;
489                 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
490                                       ST21NFCA_RF_CARD_F_MODE, param, 1);
491         }
492
493         return r;
494 }
495
496 static void st21nfca_hci_stop_poll(struct nfc_hci_dev *hdev)
497 {
498         nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
499                         ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
500 }
501
502 static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
503 {
504         int r;
505         struct sk_buff *atqa_skb = NULL;
506
507         r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
508                               ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
509         if (r < 0)
510                 goto exit;
511
512         if (atqa_skb->len != 2) {
513                 r = -EPROTO;
514                 goto exit;
515         }
516
517         *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
518
519 exit:
520         kfree_skb(atqa_skb);
521         return r;
522 }
523
524 static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
525 {
526         int r;
527         struct sk_buff *sak_skb = NULL;
528
529         r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
530                               ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
531         if (r < 0)
532                 goto exit;
533
534         if (sak_skb->len != 1) {
535                 r = -EPROTO;
536                 goto exit;
537         }
538
539         *sak = sak_skb->data[0];
540
541 exit:
542         kfree_skb(sak_skb);
543         return r;
544 }
545
546 static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
547                                        int *len)
548 {
549         int r;
550         struct sk_buff *uid_skb = NULL;
551
552         r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
553                               ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
554         if (r < 0)
555                 goto exit;
556
557         if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
558                 r = -EPROTO;
559                 goto exit;
560         }
561
562         gate = uid_skb->data;
563         *len = uid_skb->len;
564 exit:
565         kfree_skb(uid_skb);
566         return r;
567 }
568
569 static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
570                                            struct nfc_target *target)
571 {
572         int r;
573         struct sk_buff *inventory_skb = NULL;
574
575         r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
576                               ST21NFCA_RF_READER_ISO15693_INVENTORY,
577                               &inventory_skb);
578         if (r < 0)
579                 goto exit;
580
581         skb_pull(inventory_skb, 2);
582
583         if (inventory_skb->len == 0 ||
584             inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
585                 r = -EPROTO;
586                 goto exit;
587         }
588
589         memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
590         target->iso15693_dsfid  = inventory_skb->data[1];
591         target->is_iso15693 = 1;
592 exit:
593         kfree_skb(inventory_skb);
594         return r;
595 }
596
597 static int st21nfca_hci_dep_link_up(struct nfc_hci_dev *hdev,
598                                     struct nfc_target *target, u8 comm_mode,
599                                     u8 *gb, size_t gb_len)
600 {
601         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
602
603         info->dep_info.idx = target->idx;
604         return st21nfca_im_send_atr_req(hdev, gb, gb_len);
605 }
606
607 static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev)
608 {
609         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
610
611         info->state = ST21NFCA_ST_READY;
612
613         return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
614                                 ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
615 }
616
617 static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
618                                          struct nfc_target *target)
619 {
620         int r, len;
621         u16 atqa;
622         u8 sak;
623         u8 uid[NFC_NFCID1_MAXSIZE];
624
625         switch (gate) {
626         case ST21NFCA_RF_READER_F_GATE:
627                 target->supported_protocols = NFC_PROTO_FELICA_MASK;
628                 break;
629         case ST21NFCA_RF_READER_14443_3_A_GATE:
630                 /* ISO14443-3 type 1 or 2 tags */
631                 r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
632                 if (r < 0)
633                         return r;
634                 if (atqa == 0x000c) {
635                         target->supported_protocols = NFC_PROTO_JEWEL_MASK;
636                         target->sens_res = 0x0c00;
637                 } else {
638                         r = st21nfca_get_iso14443_3_sak(hdev, &sak);
639                         if (r < 0)
640                                 return r;
641
642                         r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
643                         if (r < 0)
644                                 return r;
645
646                         target->supported_protocols =
647                             nfc_hci_sak_to_protocol(sak);
648                         if (target->supported_protocols == 0xffffffff)
649                                 return -EPROTO;
650
651                         target->sens_res = atqa;
652                         target->sel_res = sak;
653                         memcpy(target->nfcid1, uid, len);
654                         target->nfcid1_len = len;
655                 }
656
657                 break;
658         case ST21NFCA_RF_READER_ISO15693_GATE:
659                 target->supported_protocols = NFC_PROTO_ISO15693_MASK;
660                 r = st21nfca_get_iso15693_inventory(hdev, target);
661                 if (r < 0)
662                         return r;
663                 break;
664         default:
665                 return -EPROTO;
666         }
667
668         return 0;
669 }
670
671 static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
672                                                 u8 gate,
673                                                 struct nfc_target *target)
674 {
675         int r;
676         struct sk_buff *nfcid2_skb = NULL, *nfcid1_skb;
677
678         if (gate == ST21NFCA_RF_READER_F_GATE) {
679                 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
680                                 ST21NFCA_RF_READER_F_NFCID2, &nfcid2_skb);
681                 if (r < 0)
682                         goto exit;
683
684                 if (nfcid2_skb->len > NFC_SENSF_RES_MAXSIZE) {
685                         r = -EPROTO;
686                         goto exit;
687                 }
688
689                 /*
690                  * - After the recepton of polling response for type F frame
691                  * at 212 or 424 Kbit/s, NFCID2 registry parameters will be
692                  * updated.
693                  * - After the reception of SEL_RES with NFCIP-1 compliant bit
694                  * set for type A frame NFCID1 will be updated
695                  */
696                 if (nfcid2_skb->len > 0) {
697                         /* P2P in type F */
698                         memcpy(target->sensf_res, nfcid2_skb->data,
699                                 nfcid2_skb->len);
700                         target->sensf_res_len = nfcid2_skb->len;
701                         /* NFC Forum Digital Protocol Table 44 */
702                         if (target->sensf_res[0] == 0x01 &&
703                             target->sensf_res[1] == 0xfe)
704                                 target->supported_protocols =
705                                                         NFC_PROTO_NFC_DEP_MASK;
706                         else
707                                 target->supported_protocols =
708                                                         NFC_PROTO_FELICA_MASK;
709                 } else {
710                         /* P2P in type A */
711                         r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
712                                         ST21NFCA_RF_READER_F_NFCID1,
713                                         &nfcid1_skb);
714                         if (r < 0)
715                                 goto exit;
716
717                         if (nfcid1_skb->len > NFC_NFCID1_MAXSIZE) {
718                                 r = -EPROTO;
719                                 goto exit;
720                         }
721                         memcpy(target->sensf_res, nfcid1_skb->data,
722                                 nfcid1_skb->len);
723                         target->sensf_res_len = nfcid1_skb->len;
724                         target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
725                 }
726                 target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
727         }
728         r = 1;
729 exit:
730         kfree_skb(nfcid2_skb);
731         return r;
732 }
733
734 #define ST21NFCA_CB_TYPE_READER_ISO15693 1
735 static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
736                                           int err)
737 {
738         struct st21nfca_hci_info *info = context;
739
740         switch (info->async_cb_type) {
741         case ST21NFCA_CB_TYPE_READER_ISO15693:
742                 if (err == 0)
743                         skb_trim(skb, skb->len - 1);
744                 info->async_cb(info->async_cb_context, skb, err);
745                 break;
746         default:
747                 if (err == 0)
748                         kfree_skb(skb);
749                 break;
750         }
751 }
752
753 /*
754  * Returns:
755  * <= 0: driver handled the data exchange
756  *    1: driver doesn't especially handle, please do standard processing
757  */
758 static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
759                                       struct nfc_target *target,
760                                       struct sk_buff *skb,
761                                       data_exchange_cb_t cb, void *cb_context)
762 {
763         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
764
765         pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
766                 target->hci_reader_gate, skb->len);
767
768         switch (target->hci_reader_gate) {
769         case ST21NFCA_RF_READER_F_GATE:
770                 if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
771                         return st21nfca_im_send_dep_req(hdev, skb);
772
773                 *skb_push(skb, 1) = 0x1a;
774                 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
775                                               ST21NFCA_WR_XCHG_DATA, skb->data,
776                                               skb->len, cb, cb_context);
777         case ST21NFCA_RF_READER_14443_3_A_GATE:
778                 *skb_push(skb, 1) = 0x1a;       /* CTR, see spec:10.2.2.1 */
779
780                 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
781                                               ST21NFCA_WR_XCHG_DATA, skb->data,
782                                               skb->len, cb, cb_context);
783         case ST21NFCA_RF_READER_ISO15693_GATE:
784                 info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
785                 info->async_cb = cb;
786                 info->async_cb_context = cb_context;
787
788                 *skb_push(skb, 1) = 0x17;
789
790                 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
791                                               ST21NFCA_WR_XCHG_DATA, skb->data,
792                                               skb->len,
793                                               st21nfca_hci_data_exchange_cb,
794                                               info);
795                 break;
796         default:
797                 return 1;
798         }
799 }
800
801 static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
802 {
803         return st21nfca_tm_send_dep_res(hdev, skb);
804 }
805
806 static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
807                                        struct nfc_target *target)
808 {
809         u8 fwi = 0x11;
810
811         switch (target->hci_reader_gate) {
812         case NFC_HCI_RF_READER_A_GATE:
813         case NFC_HCI_RF_READER_B_GATE:
814                 /*
815                  * PRESENCE_CHECK on those gates is available
816                  * However, the answer to this command is taking 3 * fwi
817                  * if the card is no present.
818                  * Instead, we send an empty I-Frame with a very short
819                  * configurable fwi ~604µs.
820                  */
821                 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
822                                         ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
823         case ST21NFCA_RF_READER_14443_3_A_GATE:
824                 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
825                                         ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
826                                         NULL, 0, NULL);
827         default:
828                 return -EOPNOTSUPP;
829         }
830 }
831
832 /*
833  * Returns:
834  * <= 0: driver handled the event, skb consumed
835  *    1: driver does not handle the event, please do standard processing
836  */
837 static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 gate,
838                                        u8 event, struct sk_buff *skb)
839 {
840         pr_debug("hci event: %d gate: %x\n", event, gate);
841
842         switch (gate) {
843         case ST21NFCA_RF_CARD_F_GATE:
844                 return st21nfca_dep_event_received(hdev, event, skb);
845         default:
846                 return 1;
847         }
848         kfree_skb(skb);
849         return 0;
850 }
851
852 static struct nfc_hci_ops st21nfca_hci_ops = {
853         .open = st21nfca_hci_open,
854         .close = st21nfca_hci_close,
855         .load_session = st21nfca_hci_load_session,
856         .hci_ready = st21nfca_hci_ready,
857         .xmit = st21nfca_hci_xmit,
858         .start_poll = st21nfca_hci_start_poll,
859         .stop_poll = st21nfca_hci_stop_poll,
860         .dep_link_up = st21nfca_hci_dep_link_up,
861         .dep_link_down = st21nfca_hci_dep_link_down,
862         .target_from_gate = st21nfca_hci_target_from_gate,
863         .complete_target_discovered = st21nfca_hci_complete_target_discovered,
864         .im_transceive = st21nfca_hci_im_transceive,
865         .tm_send = st21nfca_hci_tm_send,
866         .check_presence = st21nfca_hci_check_presence,
867         .event_received = st21nfca_hci_event_received,
868 };
869
870 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
871                        char *llc_name, int phy_headroom, int phy_tailroom,
872                        int phy_payload, struct nfc_hci_dev **hdev)
873 {
874         struct st21nfca_hci_info *info;
875         int r = 0;
876         int dev_num;
877         u32 protocols;
878         struct nfc_hci_init_data init_data;
879         unsigned long quirks = 0;
880
881         info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
882         if (!info) {
883                 r = -ENOMEM;
884                 goto err_alloc_hdev;
885         }
886
887         info->phy_ops = phy_ops;
888         info->phy_id = phy_id;
889         info->state = ST21NFCA_ST_COLD;
890         mutex_init(&info->info_lock);
891
892         init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
893
894         memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
895
896         /*
897          * Session id must include the driver name + i2c bus addr
898          * persistent info to discriminate 2 identical chips
899          */
900         dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
901
902         if (dev_num >= ST21NFCA_NUM_DEVICES)
903                 return -ENODEV;
904
905         set_bit(dev_num, dev_mask);
906
907         scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
908                   "ST21AH", dev_num);
909
910         protocols = NFC_PROTO_JEWEL_MASK |
911             NFC_PROTO_MIFARE_MASK |
912             NFC_PROTO_FELICA_MASK |
913             NFC_PROTO_ISO14443_MASK |
914             NFC_PROTO_ISO14443_B_MASK |
915             NFC_PROTO_ISO15693_MASK |
916             NFC_PROTO_NFC_DEP_MASK;
917
918         set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
919
920         info->hdev =
921             nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
922                                     protocols, llc_name,
923                                     phy_headroom + ST21NFCA_CMDS_HEADROOM,
924                                     phy_tailroom, phy_payload);
925
926         if (!info->hdev) {
927                 pr_err("Cannot allocate nfc hdev.\n");
928                 r = -ENOMEM;
929                 goto err_alloc_hdev;
930         }
931
932         nfc_hci_set_clientdata(info->hdev, info);
933
934         r = nfc_hci_register_device(info->hdev);
935         if (r)
936                 goto err_regdev;
937
938         *hdev = info->hdev;
939         st21nfca_dep_init(info->hdev);
940
941         return 0;
942
943 err_regdev:
944         nfc_hci_free_device(info->hdev);
945
946 err_alloc_hdev:
947         kfree(info);
948
949         return r;
950 }
951 EXPORT_SYMBOL(st21nfca_hci_probe);
952
953 void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
954 {
955         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
956
957         st21nfca_dep_deinit(hdev);
958         nfc_hci_unregister_device(hdev);
959         nfc_hci_free_device(hdev);
960         kfree(info);
961 }
962 EXPORT_SYMBOL(st21nfca_hci_remove);
963
964 MODULE_LICENSE("GPL");
965 MODULE_DESCRIPTION(DRIVER_DESC);