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