]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/nfc/st21nfca/st21nfca.c
ENGR00294114 PXP: correct the PS U/V buffer settings when format is YVU420P
[karo-tx-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
26 #define DRIVER_DESC "HCI NFC driver for ST21NFCA"
27
28 #define FULL_VERSION_LEN 3
29
30 /* Proprietary gates, events, commands and registers */
31
32 /* Commands that apply to all RF readers */
33 #define ST21NFCA_RF_READER_CMD_PRESENCE_CHECK   0x30
34
35 #define ST21NFCA_RF_READER_ISO15693_GATE        0x12
36 #define ST21NFCA_RF_READER_ISO15693_INVENTORY 0x01
37
38 /*
39  * Reader gate for communication with contact-less cards using Type A
40  * protocol ISO14443-3 but not compliant with ISO14443-4
41  */
42 #define ST21NFCA_RF_READER_14443_3_A_GATE       0x15
43 #define ST21NFCA_RF_READER_14443_3_A_UID        0x02
44 #define ST21NFCA_RF_READER_14443_3_A_ATQA       0x03
45 #define ST21NFCA_RF_READER_14443_3_A_SAK        0x04
46
47 #define ST21NFCA_DEVICE_MGNT_GATE               0x01
48 #define ST21NFCA_DEVICE_MGNT_PIPE               0x02
49
50 #define ST21NFCA_DM_GETINFO         0x13
51 #define ST21NFCA_DM_GETINFO_PIPE_LIST       0x02
52 #define ST21NFCA_DM_GETINFO_PIPE_INFO       0x01
53 #define ST21NFCA_DM_PIPE_CREATED        0x02
54 #define ST21NFCA_DM_PIPE_OPEN           0x04
55 #define ST21NFCA_DM_RF_ACTIVE           0x80
56
57 #define ST21NFCA_DM_IS_PIPE_OPEN(p) \
58         ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
59
60 #define ST21NFCA_NFC_MODE       0x03    /* NFC_MODE parameter*/
61
62 static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
63
64 static struct nfc_hci_gate st21nfca_gates[] = {
65         {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
66         {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
67         {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
68         {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
69         {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
70         {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
71         {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
72         {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
73         {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
74         {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
75 };
76
77 struct st21nfca_pipe_info {
78         u8 pipe_state;
79         u8 src_host_id;
80         u8 src_gate_id;
81         u8 dst_host_id;
82         u8 dst_gate_id;
83 } __packed;
84
85 /* Largest headroom needed for outgoing custom commands */
86 #define ST21NFCA_CMDS_HEADROOM  7
87
88 static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
89 {
90         int i, j, r;
91         struct sk_buff *skb_pipe_list, *skb_pipe_info;
92         struct st21nfca_pipe_info *info;
93
94         u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
95                 NFC_HCI_TERMINAL_HOST_ID
96         };
97         u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
98                 NFC_HCI_TERMINAL_HOST_ID, 0
99         };
100
101         skb_pipe_list = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
102         if (!skb_pipe_list) {
103                 r = -ENOMEM;
104                 goto free_list;
105         }
106
107         skb_pipe_info = alloc_skb(ST21NFCA_HCI_LLC_MAX_SIZE, GFP_KERNEL);
108         if (!skb_pipe_info) {
109                 r = -ENOMEM;
110                 goto free_info;
111         }
112
113         /* On ST21NFCA device pipes number are dynamics
114          * A maximum of 16 pipes can be created at the same time
115          * If pipes are already created, hci_dev_up will fail.
116          * Doing a clear all pipe is a bad idea because:
117          * - It does useless EEPROM cycling
118          * - It might cause issue for secure elements support
119          * (such as removing connectivity or APDU reader pipe)
120          * A better approach on ST21NFCA is to:
121          * - get a pipe list for each host.
122          * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
123          * (TODO Later on UICC HOST and eSE HOST)
124          * - get pipe information
125          * - match retrieved pipe list in st21nfca_gates
126          * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
127          * with ST21NFCA_DEVICE_MGNT_PIPE.
128          * Pipe can be closed and need to be open.
129          */
130         r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
131                 ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE);
132         if (r < 0)
133                 goto free_info;
134
135         /* Get pipe list */
136         r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
137                         ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
138                         &skb_pipe_list);
139         if (r < 0)
140                 goto free_info;
141
142         /* Complete the existing gate_pipe table */
143         for (i = 0; i < skb_pipe_list->len; i++) {
144                 pipe_info[2] = skb_pipe_list->data[i];
145                 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
146                                         ST21NFCA_DM_GETINFO, pipe_info,
147                                         sizeof(pipe_info), &skb_pipe_info);
148
149                 if (r)
150                         continue;
151
152                 /*
153                  * Match pipe ID and gate ID
154                  * Output format from ST21NFC_DM_GETINFO is:
155                  * - pipe state (1byte)
156                  * - source hid (1byte)
157                  * - source gid (1byte)
158                  * - destination hid (1byte)
159                  * - destination gid (1byte)
160                  */
161                 info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
162                 for (j = 0; (j < ARRAY_SIZE(st21nfca_gates)) &&
163                         (st21nfca_gates[j].gate != info->dst_gate_id);
164                         j++)
165                         ;
166
167                 if (j < ARRAY_SIZE(st21nfca_gates) &&
168                         st21nfca_gates[j].gate == info->dst_gate_id &&
169                         ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
170                         st21nfca_gates[j].pipe = pipe_info[2];
171                         hdev->gate2pipe[st21nfca_gates[j].gate] =
172                                 st21nfca_gates[j].pipe;
173                 }
174         }
175
176         /*
177          * 3 gates have a well known pipe ID.
178          * They will never appear in the pipe list
179          */
180         if (skb_pipe_list->len + 3 < ARRAY_SIZE(st21nfca_gates)) {
181                 for (i = skb_pipe_list->len + 3;
182                                 i < ARRAY_SIZE(st21nfca_gates); i++) {
183                         r = nfc_hci_connect_gate(hdev,
184                                         NFC_HCI_HOST_CONTROLLER_ID,
185                                         st21nfca_gates[i].gate,
186                                         st21nfca_gates[i].pipe);
187                         if (r < 0)
188                                 goto free_info;
189                 }
190         }
191
192         memcpy(hdev->init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
193 free_info:
194         kfree_skb(skb_pipe_info);
195 free_list:
196         kfree_skb(skb_pipe_list);
197         return r;
198 }
199
200 static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
201 {
202         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
203         int r;
204
205         mutex_lock(&info->info_lock);
206
207         if (info->state != ST21NFCA_ST_COLD) {
208                 r = -EBUSY;
209                 goto out;
210         }
211
212         r = info->phy_ops->enable(info->phy_id);
213
214         if (r == 0)
215                 info->state = ST21NFCA_ST_READY;
216
217 out:
218         mutex_unlock(&info->info_lock);
219         return r;
220 }
221
222 static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
223 {
224         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
225
226         mutex_lock(&info->info_lock);
227
228         if (info->state == ST21NFCA_ST_COLD)
229                 goto out;
230
231         info->phy_ops->disable(info->phy_id);
232         info->state = ST21NFCA_ST_COLD;
233
234 out:
235         mutex_unlock(&info->info_lock);
236 }
237
238 static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
239 {
240         struct sk_buff *skb;
241
242         u8 param;
243         int r;
244
245         param = NFC_HCI_UICC_HOST_ID;
246         r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
247                               NFC_HCI_ADMIN_WHITELIST, &param, 1);
248         if (r < 0)
249                 return r;
250
251         /* Set NFC_MODE in device management gate to enable */
252         r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
253                               ST21NFCA_NFC_MODE, &skb);
254         if (r < 0)
255                 return r;
256
257         if (skb->data[0] == 0) {
258                 kfree_skb(skb);
259                 param = 1;
260
261                 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
262                                         ST21NFCA_NFC_MODE, &param, 1);
263                 if (r < 0)
264                         return r;
265         }
266
267         r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
268                                NFC_HCI_EVT_END_OPERATION, NULL, 0);
269         if (r < 0)
270                 return r;
271
272         r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
273                               NFC_HCI_ID_MGMT_VERSION_SW, &skb);
274         if (r < 0)
275                 return r;
276
277         if (skb->len != FULL_VERSION_LEN) {
278                 kfree_skb(skb);
279                 return -EINVAL;
280         }
281
282         print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
283                        DUMP_PREFIX_NONE, 16, 1,
284                        skb->data, FULL_VERSION_LEN, false);
285
286         kfree_skb(skb);
287
288         return 0;
289 }
290
291 static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
292 {
293         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
294
295         return info->phy_ops->write(info->phy_id, skb);
296 }
297
298 static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
299                                    u32 im_protocols, u32 tm_protocols)
300 {
301         int r;
302
303         pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
304                 __func__, im_protocols, tm_protocols);
305
306         r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
307                                NFC_HCI_EVT_END_OPERATION, NULL, 0);
308         if (r < 0)
309                 return r;
310         if (im_protocols) {
311                 /*
312                  * enable polling according to im_protocols & tm_protocols
313                  * - CLOSE pipe according to im_protocols & tm_protocols
314                  */
315                 if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
316                         r = nfc_hci_disconnect_gate(hdev,
317                                         NFC_HCI_RF_READER_B_GATE);
318                         if (r < 0)
319                                 return r;
320                 }
321
322                 if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
323                         r = nfc_hci_disconnect_gate(hdev,
324                                         NFC_HCI_RF_READER_A_GATE);
325                         if (r < 0)
326                                 return r;
327                 }
328
329                 if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
330                         r = nfc_hci_disconnect_gate(hdev,
331                                         ST21NFCA_RF_READER_F_GATE);
332                         if (r < 0)
333                                 return r;
334                 }
335
336                 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
337                         r = nfc_hci_disconnect_gate(hdev,
338                                         ST21NFCA_RF_READER_14443_3_A_GATE);
339                         if (r < 0)
340                                 return r;
341                 }
342
343                 if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
344                         r = nfc_hci_disconnect_gate(hdev,
345                                         ST21NFCA_RF_READER_ISO15693_GATE);
346                         if (r < 0)
347                                 return r;
348                 }
349
350                 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
351                                        NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
352                 if (r < 0)
353                         nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
354                                            NFC_HCI_EVT_END_OPERATION, NULL, 0);
355         }
356         return r;
357 }
358
359 static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
360 {
361         int r;
362         struct sk_buff *atqa_skb = NULL;
363
364         r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
365                               ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
366         if (r < 0)
367                 goto exit;
368
369         if (atqa_skb->len != 2) {
370                 r = -EPROTO;
371                 goto exit;
372         }
373
374         *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
375
376 exit:
377         kfree_skb(atqa_skb);
378         return r;
379 }
380
381 static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
382 {
383         int r;
384         struct sk_buff *sak_skb = NULL;
385
386         r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
387                               ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
388         if (r < 0)
389                 goto exit;
390
391         if (sak_skb->len != 1) {
392                 r = -EPROTO;
393                 goto exit;
394         }
395
396         *sak = sak_skb->data[0];
397
398 exit:
399         kfree_skb(sak_skb);
400         return r;
401 }
402
403 static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *gate,
404                                        int *len)
405 {
406         int r;
407         struct sk_buff *uid_skb = NULL;
408
409         r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
410                               ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
411         if (r < 0)
412                 goto exit;
413
414         if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
415                 r = -EPROTO;
416                 goto exit;
417         }
418
419         gate = uid_skb->data;
420         *len = uid_skb->len;
421 exit:
422         kfree_skb(uid_skb);
423         return r;
424 }
425
426 static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
427                                            struct nfc_target *target)
428 {
429         int r;
430         struct sk_buff *inventory_skb = NULL;
431
432         r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
433                               ST21NFCA_RF_READER_ISO15693_INVENTORY,
434                               &inventory_skb);
435         if (r < 0)
436                 goto exit;
437
438         skb_pull(inventory_skb, 2);
439
440         if (inventory_skb->len == 0 ||
441             inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
442                 r = -EPROTO;
443                 goto exit;
444         }
445
446         memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
447         target->iso15693_dsfid  = inventory_skb->data[1];
448         target->is_iso15693 = 1;
449 exit:
450         kfree_skb(inventory_skb);
451         return r;
452 }
453
454 static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
455                                          struct nfc_target *target)
456 {
457         int r, len;
458         u16 atqa;
459         u8 sak;
460         u8 uid[NFC_NFCID1_MAXSIZE];
461
462         switch (gate) {
463         case ST21NFCA_RF_READER_F_GATE:
464                 target->supported_protocols = NFC_PROTO_FELICA_MASK;
465                 break;
466         case ST21NFCA_RF_READER_14443_3_A_GATE:
467                 /* ISO14443-3 type 1 or 2 tags */
468                 r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
469                 if (r < 0)
470                         return r;
471                 if (atqa == 0x000c) {
472                         target->supported_protocols = NFC_PROTO_JEWEL_MASK;
473                         target->sens_res = 0x0c00;
474                 } else {
475                         r = st21nfca_get_iso14443_3_sak(hdev, &sak);
476                         if (r < 0)
477                                 return r;
478
479                         r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
480                         if (r < 0)
481                                 return r;
482
483                         target->supported_protocols =
484                             nfc_hci_sak_to_protocol(sak);
485                         if (target->supported_protocols == 0xffffffff)
486                                 return -EPROTO;
487
488                         target->sens_res = atqa;
489                         target->sel_res = sak;
490                         memcpy(target->nfcid1, uid, len);
491                         target->nfcid1_len = len;
492                 }
493
494                 break;
495         case ST21NFCA_RF_READER_ISO15693_GATE:
496                 target->supported_protocols = NFC_PROTO_ISO15693_MASK;
497                 r = st21nfca_get_iso15693_inventory(hdev, target);
498                 if (r < 0)
499                         return r;
500                 break;
501         default:
502                 return -EPROTO;
503         }
504
505         return 0;
506 }
507
508 #define ST21NFCA_CB_TYPE_READER_ISO15693 1
509 static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
510                                           int err)
511 {
512         struct st21nfca_hci_info *info = context;
513
514         switch (info->async_cb_type) {
515         case ST21NFCA_CB_TYPE_READER_ISO15693:
516                 if (err == 0)
517                         skb_trim(skb, skb->len - 1);
518                 info->async_cb(info->async_cb_context, skb, err);
519                 break;
520         default:
521                 if (err == 0)
522                         kfree_skb(skb);
523                 break;
524         }
525 }
526
527 /*
528  * Returns:
529  * <= 0: driver handled the data exchange
530  *    1: driver doesn't especially handle, please do standard processing
531  */
532 static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
533                                       struct nfc_target *target,
534                                       struct sk_buff *skb,
535                                       data_exchange_cb_t cb, void *cb_context)
536 {
537         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
538
539         pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
540                 target->hci_reader_gate, skb->len);
541
542         switch (target->hci_reader_gate) {
543         case ST21NFCA_RF_READER_F_GATE:
544                 *skb_push(skb, 1) = 0x1a;
545                 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
546                                               ST21NFCA_WR_XCHG_DATA, skb->data,
547                                               skb->len, cb, cb_context);
548         case ST21NFCA_RF_READER_14443_3_A_GATE:
549                 *skb_push(skb, 1) = 0x1a;       /* CTR, see spec:10.2.2.1 */
550
551                 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
552                                               ST21NFCA_WR_XCHG_DATA, skb->data,
553                                               skb->len, cb, cb_context);
554         case ST21NFCA_RF_READER_ISO15693_GATE:
555                 info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
556                 info->async_cb = cb;
557                 info->async_cb_context = cb_context;
558
559                 *skb_push(skb, 1) = 0x17;
560
561                 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
562                                               ST21NFCA_WR_XCHG_DATA, skb->data,
563                                               skb->len,
564                                               st21nfca_hci_data_exchange_cb,
565                                               info);
566                 break;
567         default:
568                 return 1;
569         }
570 }
571
572 static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
573                                        struct nfc_target *target)
574 {
575         u8 fwi = 0x11;
576         switch (target->hci_reader_gate) {
577         case NFC_HCI_RF_READER_A_GATE:
578         case NFC_HCI_RF_READER_B_GATE:
579                 /*
580                  * PRESENCE_CHECK on those gates is available
581                  * However, the answer to this command is taking 3 * fwi
582                  * if the card is no present.
583                  * Instead, we send an empty I-Frame with a very short
584                  * configurable fwi ~604µs.
585                  */
586                 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
587                                         ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
588         case ST21NFCA_RF_READER_14443_3_A_GATE:
589                 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
590                                         ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
591                                         NULL, 0, NULL);
592         default:
593                 return -EOPNOTSUPP;
594         }
595 }
596
597 static struct nfc_hci_ops st21nfca_hci_ops = {
598         .open = st21nfca_hci_open,
599         .close = st21nfca_hci_close,
600         .load_session = st21nfca_hci_load_session,
601         .hci_ready = st21nfca_hci_ready,
602         .xmit = st21nfca_hci_xmit,
603         .start_poll = st21nfca_hci_start_poll,
604         .target_from_gate = st21nfca_hci_target_from_gate,
605         .im_transceive = st21nfca_hci_im_transceive,
606         .check_presence = st21nfca_hci_check_presence,
607 };
608
609 int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
610                        char *llc_name, int phy_headroom, int phy_tailroom,
611                        int phy_payload, struct nfc_hci_dev **hdev)
612 {
613         struct st21nfca_hci_info *info;
614         int r = 0;
615         int dev_num;
616         u32 protocols;
617         struct nfc_hci_init_data init_data;
618         unsigned long quirks = 0;
619
620         info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
621         if (!info) {
622                 r = -ENOMEM;
623                 goto err_alloc_hdev;
624         }
625
626         info->phy_ops = phy_ops;
627         info->phy_id = phy_id;
628         info->state = ST21NFCA_ST_COLD;
629         mutex_init(&info->info_lock);
630
631         init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
632
633         memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
634
635         /*
636          * Session id must include the driver name + i2c bus addr
637          * persistent info to discriminate 2 identical chips
638          */
639         dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
640         if (dev_num >= ST21NFCA_NUM_DEVICES)
641                 goto err_alloc_hdev;
642
643         scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
644                   "ST21AH", dev_num);
645
646         protocols = NFC_PROTO_JEWEL_MASK |
647             NFC_PROTO_MIFARE_MASK |
648             NFC_PROTO_FELICA_MASK |
649             NFC_PROTO_ISO14443_MASK |
650             NFC_PROTO_ISO14443_B_MASK |
651             NFC_PROTO_ISO15693_MASK;
652
653         set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
654
655         info->hdev =
656             nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
657                                     protocols, llc_name,
658                                     phy_headroom + ST21NFCA_CMDS_HEADROOM,
659                                     phy_tailroom, phy_payload);
660
661         if (!info->hdev) {
662                 pr_err("Cannot allocate nfc hdev.\n");
663                 r = -ENOMEM;
664                 goto err_alloc_hdev;
665         }
666
667         nfc_hci_set_clientdata(info->hdev, info);
668
669         r = nfc_hci_register_device(info->hdev);
670         if (r)
671                 goto err_regdev;
672
673         *hdev = info->hdev;
674
675         return 0;
676
677 err_regdev:
678         nfc_hci_free_device(info->hdev);
679
680 err_alloc_hdev:
681         kfree(info);
682
683         return r;
684 }
685 EXPORT_SYMBOL(st21nfca_hci_probe);
686
687 void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
688 {
689         struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
690
691         nfc_hci_unregister_device(hdev);
692         nfc_hci_free_device(hdev);
693         kfree(info);
694 }
695 EXPORT_SYMBOL(st21nfca_hci_remove);
696
697 MODULE_LICENSE("GPL");
698 MODULE_DESCRIPTION(DRIVER_DESC);