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