blob: dd8b150fbffa0203eecde797693d8b1d08b4a10d [file] [log] [blame]
Christophe Ricard68957302014-03-25 06:51:47 +01001/*
2 * HCI based Driver for STMicroelectronics NFC Chip
3 *
4 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
Christophe Ricard68957302014-03-25 06:51:47 +010019#include <linux/module.h>
Christophe Ricard68957302014-03-25 06:51:47 +010020#include <linux/nfc.h>
21#include <net/nfc/hci.h>
22#include <net/nfc/llc.h>
23
Christophe Ricard68957302014-03-25 06:51:47 +010024#include "st21nfca.h"
Christophe Ricard68957302014-03-25 06:51:47 +010025
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
Christophe Ricardefaf9562014-09-03 23:30:29 +020036#define ST21NFCA_RF_READER_ISO15693_INVENTORY 0x01
Christophe Ricard68957302014-03-25 06:51:47 +010037
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
Christophe Ricardefaf9562014-09-03 23:30:29 +020047#define ST21NFCA_RF_READER_F_DATARATE 0x01
48#define ST21NFCA_RF_READER_F_DATARATE_106 0x01
49#define ST21NFCA_RF_READER_F_DATARATE_212 0x02
50#define ST21NFCA_RF_READER_F_DATARATE_424 0x04
51#define ST21NFCA_RF_READER_F_POL_REQ 0x02
52#define ST21NFCA_RF_READER_F_POL_REQ_DEFAULT 0xffff0000
53#define ST21NFCA_RF_READER_F_NFCID2 0x03
54#define ST21NFCA_RF_READER_F_NFCID1 0x04
Christophe Ricardcebe2222014-09-03 23:30:28 +020055
Christophe Ricardefaf9562014-09-03 23:30:29 +020056#define ST21NFCA_RF_CARD_F_MODE 0x01
57#define ST21NFCA_RF_CARD_F_NFCID2_LIST 0x04
58#define ST21NFCA_RF_CARD_F_NFCID1 0x05
59#define ST21NFCA_RF_CARD_F_SENS_RES 0x06
60#define ST21NFCA_RF_CARD_F_SEL_RES 0x07
61#define ST21NFCA_RF_CARD_F_DATARATE 0x08
62#define ST21NFCA_RF_CARD_F_DATARATE_212_424 0x01
Christophe Ricardcebe2222014-09-03 23:30:28 +020063
Christophe Ricard68957302014-03-25 06:51:47 +010064#define ST21NFCA_DEVICE_MGNT_PIPE 0x02
Christophe Ricard68957302014-03-25 06:51:47 +010065
Christophe Ricardefaf9562014-09-03 23:30:29 +020066#define ST21NFCA_DM_GETINFO 0x13
67#define ST21NFCA_DM_GETINFO_PIPE_LIST 0x02
68#define ST21NFCA_DM_GETINFO_PIPE_INFO 0x01
69#define ST21NFCA_DM_PIPE_CREATED 0x02
70#define ST21NFCA_DM_PIPE_OPEN 0x04
71#define ST21NFCA_DM_RF_ACTIVE 0x80
72#define ST21NFCA_DM_DISCONNECT 0x30
Christophe Ricard58e1e0a2014-03-25 06:51:50 +010073
74#define ST21NFCA_DM_IS_PIPE_OPEN(p) \
75 ((p & 0x0f) == (ST21NFCA_DM_PIPE_CREATED | ST21NFCA_DM_PIPE_OPEN))
76
Christophe Ricardefaf9562014-09-03 23:30:29 +020077#define ST21NFCA_NFC_MODE 0x03 /* NFC_MODE parameter*/
Christophe Ricard68957302014-03-25 06:51:47 +010078
Christophe Ricard2130fb92015-01-27 01:18:19 +010079#define ST21NFCA_EVT_HOT_PLUG 0x03
80#define ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(x) (x->data[0] & 0x80)
81
82#define ST21NFCA_SE_TO_PIPES 2000
83
Christophe Ricard68957302014-03-25 06:51:47 +010084static DECLARE_BITMAP(dev_mask, ST21NFCA_NUM_DEVICES);
85
86static struct nfc_hci_gate st21nfca_gates[] = {
Christophe Ricarda779b882014-05-13 22:03:41 +020087 {NFC_HCI_ADMIN_GATE, NFC_HCI_ADMIN_PIPE},
Christophe Ricardba723192015-10-25 22:54:35 +010088 {NFC_HCI_LINK_MGMT_GATE, NFC_HCI_LINK_MGMT_PIPE},
89 {ST21NFCA_DEVICE_MGNT_GATE, ST21NFCA_DEVICE_MGNT_PIPE},
90
Christophe Ricard68957302014-03-25 06:51:47 +010091 {NFC_HCI_LOOPBACK_GATE, NFC_HCI_INVALID_PIPE},
92 {NFC_HCI_ID_MGMT_GATE, NFC_HCI_INVALID_PIPE},
Christophe Ricard68957302014-03-25 06:51:47 +010093 {NFC_HCI_RF_READER_B_GATE, NFC_HCI_INVALID_PIPE},
94 {NFC_HCI_RF_READER_A_GATE, NFC_HCI_INVALID_PIPE},
Christophe Ricard68957302014-03-25 06:51:47 +010095 {ST21NFCA_RF_READER_F_GATE, NFC_HCI_INVALID_PIPE},
96 {ST21NFCA_RF_READER_14443_3_A_GATE, NFC_HCI_INVALID_PIPE},
Christophe Ricard79747282014-05-13 22:03:42 +020097 {ST21NFCA_RF_READER_ISO15693_GATE, NFC_HCI_INVALID_PIPE},
Christophe Ricard1892bf82014-05-20 22:21:59 +020098 {ST21NFCA_RF_CARD_F_GATE, NFC_HCI_INVALID_PIPE},
Christophe Ricard2130fb92015-01-27 01:18:19 +010099
100 /* Secure element pipes are created by secure element host */
101 {ST21NFCA_CONNECTIVITY_GATE, NFC_HCI_DO_NOT_CREATE_PIPE},
102 {ST21NFCA_APDU_READER_GATE, NFC_HCI_DO_NOT_CREATE_PIPE},
Christophe Ricard68957302014-03-25 06:51:47 +0100103};
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100104
105struct st21nfca_pipe_info {
106 u8 pipe_state;
107 u8 src_host_id;
108 u8 src_gate_id;
109 u8 dst_host_id;
110 u8 dst_gate_id;
Christophe Ricard761a2c42014-04-01 00:34:04 +0200111} __packed;
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100112
Christophe Ricard68957302014-03-25 06:51:47 +0100113/* Largest headroom needed for outgoing custom commands */
114#define ST21NFCA_CMDS_HEADROOM 7
115
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100116static int st21nfca_hci_load_session(struct nfc_hci_dev *hdev)
117{
118 int i, j, r;
119 struct sk_buff *skb_pipe_list, *skb_pipe_info;
120 struct st21nfca_pipe_info *info;
121
122 u8 pipe_list[] = { ST21NFCA_DM_GETINFO_PIPE_LIST,
123 NFC_HCI_TERMINAL_HOST_ID
124 };
125 u8 pipe_info[] = { ST21NFCA_DM_GETINFO_PIPE_INFO,
126 NFC_HCI_TERMINAL_HOST_ID, 0
127 };
128
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100129 /* On ST21NFCA device pipes number are dynamics
130 * A maximum of 16 pipes can be created at the same time
131 * If pipes are already created, hci_dev_up will fail.
132 * Doing a clear all pipe is a bad idea because:
133 * - It does useless EEPROM cycling
134 * - It might cause issue for secure elements support
135 * (such as removing connectivity or APDU reader pipe)
136 * A better approach on ST21NFCA is to:
137 * - get a pipe list for each host.
138 * (eg: NFC_HCI_HOST_CONTROLLER_ID for now).
139 * (TODO Later on UICC HOST and eSE HOST)
140 * - get pipe information
141 * - match retrieved pipe list in st21nfca_gates
142 * ST21NFCA_DEVICE_MGNT_GATE is a proprietary gate
143 * with ST21NFCA_DEVICE_MGNT_PIPE.
144 * Pipe can be closed and need to be open.
145 */
146 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
Christophe Ricard2130fb92015-01-27 01:18:19 +0100147 ST21NFCA_DEVICE_MGNT_GATE,
148 ST21NFCA_DEVICE_MGNT_PIPE);
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100149 if (r < 0)
Christophe Ricard5a357002015-08-14 22:33:33 +0200150 return r;
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100151
152 /* Get pipe list */
153 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
154 ST21NFCA_DM_GETINFO, pipe_list, sizeof(pipe_list),
155 &skb_pipe_list);
156 if (r < 0)
Christophe Ricard5a357002015-08-14 22:33:33 +0200157 return r;
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100158
159 /* Complete the existing gate_pipe table */
160 for (i = 0; i < skb_pipe_list->len; i++) {
161 pipe_info[2] = skb_pipe_list->data[i];
162 r = nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
163 ST21NFCA_DM_GETINFO, pipe_info,
164 sizeof(pipe_info), &skb_pipe_info);
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100165 if (r)
166 continue;
167
168 /*
169 * Match pipe ID and gate ID
170 * Output format from ST21NFC_DM_GETINFO is:
171 * - pipe state (1byte)
172 * - source hid (1byte)
173 * - source gid (1byte)
174 * - destination hid (1byte)
175 * - destination gid (1byte)
176 */
Christophe Ricarde8efab62014-04-01 00:34:02 +0200177 info = (struct st21nfca_pipe_info *) skb_pipe_info->data;
Christophe Ricard2130fb92015-01-27 01:18:19 +0100178 if (info->dst_gate_id == ST21NFCA_APDU_READER_GATE &&
179 info->src_host_id != ST21NFCA_ESE_HOST_ID) {
180 pr_err("Unexpected apdu_reader pipe on host %x\n",
181 info->src_host_id);
Christophe Ricard5a357002015-08-14 22:33:33 +0200182 kfree_skb(skb_pipe_info);
Christophe Ricard2130fb92015-01-27 01:18:19 +0100183 continue;
184 }
185
Christophe Ricardba723192015-10-25 22:54:35 +0100186 for (j = 3; (j < ARRAY_SIZE(st21nfca_gates)) &&
Christophe Ricard2130fb92015-01-27 01:18:19 +0100187 (st21nfca_gates[j].gate != info->dst_gate_id) ; j++)
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100188 ;
189
Christophe Ricarde8efab62014-04-01 00:34:02 +0200190 if (j < ARRAY_SIZE(st21nfca_gates) &&
191 st21nfca_gates[j].gate == info->dst_gate_id &&
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100192 ST21NFCA_DM_IS_PIPE_OPEN(info->pipe_state)) {
Christophe Ricard92d108b2015-10-25 22:54:31 +0100193 hdev->init_data.gates[j].pipe = pipe_info[2];
Christophe Ricard2130fb92015-01-27 01:18:19 +0100194
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100195 hdev->gate2pipe[st21nfca_gates[j].gate] =
Christophe Ricard92d108b2015-10-25 22:54:31 +0100196 pipe_info[2];
197 hdev->pipes[pipe_info[2]].gate =
198 st21nfca_gates[j].gate;
199 hdev->pipes[pipe_info[2]].dest_host =
200 info->src_host_id;
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100201 }
Christophe Ricard5a357002015-08-14 22:33:33 +0200202 kfree_skb(skb_pipe_info);
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100203 }
Christophe Ricarda779b882014-05-13 22:03:41 +0200204
205 /*
Christophe Ricard6443ce92015-10-25 22:54:29 +0100206 * 3 gates have a well known pipe ID. Only NFC_HCI_LINK_MGMT_GATE
207 * is not yet open at this stage.
Christophe Ricarda779b882014-05-13 22:03:41 +0200208 */
Christophe Ricard6443ce92015-10-25 22:54:29 +0100209 r = nfc_hci_connect_gate(hdev, NFC_HCI_HOST_CONTROLLER_ID,
210 NFC_HCI_LINK_MGMT_GATE,
211 NFC_HCI_LINK_MGMT_PIPE);
Christophe Ricarda779b882014-05-13 22:03:41 +0200212
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100213 kfree_skb(skb_pipe_list);
214 return r;
215}
216
Christophe Ricard68957302014-03-25 06:51:47 +0100217static int st21nfca_hci_open(struct nfc_hci_dev *hdev)
218{
219 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
220 int r;
221
222 mutex_lock(&info->info_lock);
223
224 if (info->state != ST21NFCA_ST_COLD) {
225 r = -EBUSY;
226 goto out;
227 }
228
229 r = info->phy_ops->enable(info->phy_id);
230
231 if (r == 0)
232 info->state = ST21NFCA_ST_READY;
233
234out:
235 mutex_unlock(&info->info_lock);
236 return r;
237}
238
239static void st21nfca_hci_close(struct nfc_hci_dev *hdev)
240{
241 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
242
243 mutex_lock(&info->info_lock);
244
245 if (info->state == ST21NFCA_ST_COLD)
246 goto out;
247
248 info->phy_ops->disable(info->phy_id);
249 info->state = ST21NFCA_ST_COLD;
250
251out:
252 mutex_unlock(&info->info_lock);
253}
254
255static int st21nfca_hci_ready(struct nfc_hci_dev *hdev)
256{
Christophe Ricard2130fb92015-01-27 01:18:19 +0100257 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
Christophe Ricard68957302014-03-25 06:51:47 +0100258 struct sk_buff *skb;
259
260 u8 param;
Christophe Ricard2130fb92015-01-27 01:18:19 +0100261 u8 white_list[2];
262 int wl_size = 0;
Christophe Ricard68957302014-03-25 06:51:47 +0100263 int r;
264
Christophe Ricard2130fb92015-01-27 01:18:19 +0100265 if (info->se_status->is_ese_present &&
266 info->se_status->is_uicc_present) {
267 white_list[wl_size++] = NFC_HCI_UICC_HOST_ID;
268 white_list[wl_size++] = ST21NFCA_ESE_HOST_ID;
269 } else if (!info->se_status->is_ese_present &&
270 info->se_status->is_uicc_present) {
271 white_list[wl_size++] = NFC_HCI_UICC_HOST_ID;
272 } else if (info->se_status->is_ese_present &&
273 !info->se_status->is_uicc_present) {
274 white_list[wl_size++] = ST21NFCA_ESE_HOST_ID;
275 }
276
277 if (wl_size) {
278 r = nfc_hci_set_param(hdev, NFC_HCI_ADMIN_GATE,
279 NFC_HCI_ADMIN_WHITELIST,
280 (u8 *) &white_list, wl_size);
281 if (r < 0)
282 return r;
283 }
Christophe Ricard68957302014-03-25 06:51:47 +0100284
285 /* Set NFC_MODE in device management gate to enable */
286 r = nfc_hci_get_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
287 ST21NFCA_NFC_MODE, &skb);
288 if (r < 0)
289 return r;
290
Christophe Ricardc490c552015-01-25 23:33:28 +0100291 param = skb->data[0];
292 kfree_skb(skb);
293 if (param == 0) {
Christophe Ricard68957302014-03-25 06:51:47 +0100294 param = 1;
295
296 r = nfc_hci_set_param(hdev, ST21NFCA_DEVICE_MGNT_GATE,
297 ST21NFCA_NFC_MODE, &param, 1);
298 if (r < 0)
299 return r;
300 }
301
302 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
303 NFC_HCI_EVT_END_OPERATION, NULL, 0);
304 if (r < 0)
305 return r;
306
307 r = nfc_hci_get_param(hdev, NFC_HCI_ID_MGMT_GATE,
308 NFC_HCI_ID_MGMT_VERSION_SW, &skb);
309 if (r < 0)
310 return r;
311
312 if (skb->len != FULL_VERSION_LEN) {
313 kfree_skb(skb);
314 return -EINVAL;
315 }
316
317 print_hex_dump(KERN_DEBUG, "FULL VERSION SOFTWARE INFO: ",
318 DUMP_PREFIX_NONE, 16, 1,
319 skb->data, FULL_VERSION_LEN, false);
320
321 kfree_skb(skb);
322
323 return 0;
324}
325
326static int st21nfca_hci_xmit(struct nfc_hci_dev *hdev, struct sk_buff *skb)
327{
328 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
329
330 return info->phy_ops->write(info->phy_id, skb);
331}
332
333static int st21nfca_hci_start_poll(struct nfc_hci_dev *hdev,
334 u32 im_protocols, u32 tm_protocols)
335{
336 int r;
Christophe Ricard1892bf82014-05-20 22:21:59 +0200337 u32 pol_req;
338 u8 param[19];
339 struct sk_buff *datarate_skb;
Christophe Ricard68957302014-03-25 06:51:47 +0100340
341 pr_info(DRIVER_DESC ": %s protocols 0x%x 0x%x\n",
342 __func__, im_protocols, tm_protocols);
343
344 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
345 NFC_HCI_EVT_END_OPERATION, NULL, 0);
346 if (r < 0)
347 return r;
348 if (im_protocols) {
349 /*
350 * enable polling according to im_protocols & tm_protocols
351 * - CLOSE pipe according to im_protocols & tm_protocols
352 */
353 if ((NFC_HCI_RF_READER_B_GATE & im_protocols) == 0) {
354 r = nfc_hci_disconnect_gate(hdev,
355 NFC_HCI_RF_READER_B_GATE);
356 if (r < 0)
357 return r;
358 }
359
360 if ((NFC_HCI_RF_READER_A_GATE & im_protocols) == 0) {
361 r = nfc_hci_disconnect_gate(hdev,
362 NFC_HCI_RF_READER_A_GATE);
363 if (r < 0)
364 return r;
365 }
366
367 if ((ST21NFCA_RF_READER_F_GATE & im_protocols) == 0) {
368 r = nfc_hci_disconnect_gate(hdev,
369 ST21NFCA_RF_READER_F_GATE);
370 if (r < 0)
371 return r;
Christophe Ricard1892bf82014-05-20 22:21:59 +0200372 } else {
373 hdev->gb = nfc_get_local_general_bytes(hdev->ndev,
374 &hdev->gb_len);
375
376 if (hdev->gb == NULL || hdev->gb_len == 0) {
377 im_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
378 tm_protocols &= ~NFC_PROTO_NFC_DEP_MASK;
379 }
380
381 param[0] = ST21NFCA_RF_READER_F_DATARATE_106 |
382 ST21NFCA_RF_READER_F_DATARATE_212 |
383 ST21NFCA_RF_READER_F_DATARATE_424;
384 r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
385 ST21NFCA_RF_READER_F_DATARATE,
386 param, 1);
387 if (r < 0)
388 return r;
389
Christophe Ricard6ae3ed12014-07-28 18:11:30 +0200390 pol_req = be32_to_cpu((__force __be32)
391 ST21NFCA_RF_READER_F_POL_REQ_DEFAULT);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200392 r = nfc_hci_set_param(hdev, ST21NFCA_RF_READER_F_GATE,
393 ST21NFCA_RF_READER_F_POL_REQ,
394 (u8 *) &pol_req, 4);
395 if (r < 0)
396 return r;
Christophe Ricard68957302014-03-25 06:51:47 +0100397 }
398
399 if ((ST21NFCA_RF_READER_14443_3_A_GATE & im_protocols) == 0) {
400 r = nfc_hci_disconnect_gate(hdev,
401 ST21NFCA_RF_READER_14443_3_A_GATE);
402 if (r < 0)
403 return r;
404 }
405
406 if ((ST21NFCA_RF_READER_ISO15693_GATE & im_protocols) == 0) {
407 r = nfc_hci_disconnect_gate(hdev,
408 ST21NFCA_RF_READER_ISO15693_GATE);
409 if (r < 0)
410 return r;
411 }
412
413 r = nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
414 NFC_HCI_EVT_READER_REQUESTED, NULL, 0);
415 if (r < 0)
416 nfc_hci_send_event(hdev, NFC_HCI_RF_READER_A_GATE,
417 NFC_HCI_EVT_END_OPERATION, NULL, 0);
418 }
Christophe Ricard1892bf82014-05-20 22:21:59 +0200419
420 if (tm_protocols & NFC_PROTO_NFC_DEP_MASK) {
421 r = nfc_hci_get_param(hdev, ST21NFCA_RF_CARD_F_GATE,
422 ST21NFCA_RF_CARD_F_DATARATE,
423 &datarate_skb);
424 if (r < 0)
425 return r;
426
427 /* Configure the maximum supported datarate to 424Kbps */
428 if (datarate_skb->len > 0 &&
429 datarate_skb->data[0] !=
430 ST21NFCA_RF_CARD_F_DATARATE_212_424) {
431 param[0] = ST21NFCA_RF_CARD_F_DATARATE_212_424;
432 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
433 ST21NFCA_RF_CARD_F_DATARATE,
434 param, 1);
Christophe Ricardc490c552015-01-25 23:33:28 +0100435 if (r < 0) {
436 kfree_skb(datarate_skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200437 return r;
Christophe Ricardc490c552015-01-25 23:33:28 +0100438 }
Christophe Ricard1892bf82014-05-20 22:21:59 +0200439 }
Christophe Ricardc490c552015-01-25 23:33:28 +0100440 kfree_skb(datarate_skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200441
442 /*
443 * Configure sens_res
444 *
445 * NFC Forum Digital Spec Table 7:
446 * NFCID1 size: triple (10 bytes)
447 */
448 param[0] = 0x00;
449 param[1] = 0x08;
450 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
451 ST21NFCA_RF_CARD_F_SENS_RES, param, 2);
452 if (r < 0)
453 return r;
454
455 /*
456 * Configure sel_res
457 *
458 * NFC Forum Digistal Spec Table 17:
459 * b3 set to 0b (value b7-b6):
460 * - 10b: Configured for NFC-DEP Protocol
461 */
462 param[0] = 0x40;
463 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
464 ST21NFCA_RF_CARD_F_SEL_RES, param, 1);
465 if (r < 0)
466 return r;
467
468 /* Configure NFCID1 Random uid */
469 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
470 ST21NFCA_RF_CARD_F_NFCID1, NULL, 0);
471 if (r < 0)
472 return r;
473
474 /* Configure NFCID2_LIST */
475 /* System Code */
476 param[0] = 0x00;
477 param[1] = 0x00;
478 /* NFCID2 */
479 param[2] = 0x01;
480 param[3] = 0xfe;
481 param[4] = 'S';
482 param[5] = 'T';
483 param[6] = 'M';
484 param[7] = 'i';
485 param[8] = 'c';
486 param[9] = 'r';
487 /* 8 byte Pad bytes used for polling respone frame */
488
489 /*
490 * Configuration byte:
491 * - bit 0: define the default NFCID2 entry used when the
492 * system code is equal to 'FFFF'
493 * - bit 1: use a random value for lowest 6 bytes of
494 * NFCID2 value
495 * - bit 2: ignore polling request frame if request code
496 * is equal to '01'
497 * - Other bits are RFU
498 */
499 param[18] = 0x01;
500 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
501 ST21NFCA_RF_CARD_F_NFCID2_LIST, param,
502 19);
503 if (r < 0)
504 return r;
505
506 param[0] = 0x02;
507 r = nfc_hci_set_param(hdev, ST21NFCA_RF_CARD_F_GATE,
508 ST21NFCA_RF_CARD_F_MODE, param, 1);
509 }
510
Christophe Ricard68957302014-03-25 06:51:47 +0100511 return r;
512}
513
Christophe Ricardd57d74e2014-05-20 22:21:58 +0200514static void st21nfca_hci_stop_poll(struct nfc_hci_dev *hdev)
515{
516 nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
517 ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
518}
519
Christophe Ricard68957302014-03-25 06:51:47 +0100520static int st21nfca_get_iso14443_3_atqa(struct nfc_hci_dev *hdev, u16 *atqa)
521{
522 int r;
523 struct sk_buff *atqa_skb = NULL;
524
525 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
526 ST21NFCA_RF_READER_14443_3_A_ATQA, &atqa_skb);
527 if (r < 0)
528 goto exit;
529
530 if (atqa_skb->len != 2) {
531 r = -EPROTO;
532 goto exit;
533 }
534
Christophe Ricardcc3a9f72014-04-01 00:34:00 +0200535 *atqa = be16_to_cpu(*(__be16 *) atqa_skb->data);
Christophe Ricard68957302014-03-25 06:51:47 +0100536
537exit:
538 kfree_skb(atqa_skb);
539 return r;
540}
541
542static int st21nfca_get_iso14443_3_sak(struct nfc_hci_dev *hdev, u8 *sak)
543{
544 int r;
545 struct sk_buff *sak_skb = NULL;
546
547 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
548 ST21NFCA_RF_READER_14443_3_A_SAK, &sak_skb);
549 if (r < 0)
550 goto exit;
551
552 if (sak_skb->len != 1) {
553 r = -EPROTO;
554 goto exit;
555 }
556
557 *sak = sak_skb->data[0];
558
559exit:
560 kfree_skb(sak_skb);
561 return r;
562}
563
Christophe Ricarda8f686e2015-03-31 08:02:17 +0200564static int st21nfca_get_iso14443_3_uid(struct nfc_hci_dev *hdev, u8 *uid,
Christophe Ricard68957302014-03-25 06:51:47 +0100565 int *len)
566{
567 int r;
568 struct sk_buff *uid_skb = NULL;
569
570 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_14443_3_A_GATE,
571 ST21NFCA_RF_READER_14443_3_A_UID, &uid_skb);
572 if (r < 0)
573 goto exit;
574
575 if (uid_skb->len == 0 || uid_skb->len > NFC_NFCID1_MAXSIZE) {
576 r = -EPROTO;
577 goto exit;
578 }
579
Christophe Ricarda8f686e2015-03-31 08:02:17 +0200580 memcpy(uid, uid_skb->data, uid_skb->len);
Christophe Ricard68957302014-03-25 06:51:47 +0100581 *len = uid_skb->len;
582exit:
583 kfree_skb(uid_skb);
584 return r;
585}
586
Christophe Ricard79747282014-05-13 22:03:42 +0200587static int st21nfca_get_iso15693_inventory(struct nfc_hci_dev *hdev,
588 struct nfc_target *target)
589{
590 int r;
591 struct sk_buff *inventory_skb = NULL;
592
593 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_ISO15693_GATE,
594 ST21NFCA_RF_READER_ISO15693_INVENTORY,
595 &inventory_skb);
596 if (r < 0)
597 goto exit;
598
599 skb_pull(inventory_skb, 2);
600
601 if (inventory_skb->len == 0 ||
602 inventory_skb->len > NFC_ISO15693_UID_MAXSIZE) {
603 r = -EPROTO;
604 goto exit;
605 }
606
607 memcpy(target->iso15693_uid, inventory_skb->data, inventory_skb->len);
608 target->iso15693_dsfid = inventory_skb->data[1];
609 target->is_iso15693 = 1;
610exit:
611 kfree_skb(inventory_skb);
612 return r;
613}
614
Christophe Ricard1892bf82014-05-20 22:21:59 +0200615static int st21nfca_hci_dep_link_up(struct nfc_hci_dev *hdev,
616 struct nfc_target *target, u8 comm_mode,
617 u8 *gb, size_t gb_len)
618{
619 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
620
621 info->dep_info.idx = target->idx;
622 return st21nfca_im_send_atr_req(hdev, gb, gb_len);
623}
624
625static int st21nfca_hci_dep_link_down(struct nfc_hci_dev *hdev)
626{
627 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
628
629 info->state = ST21NFCA_ST_READY;
630
631 return nfc_hci_send_cmd(hdev, ST21NFCA_DEVICE_MGNT_GATE,
632 ST21NFCA_DM_DISCONNECT, NULL, 0, NULL);
633}
634
Christophe Ricard68957302014-03-25 06:51:47 +0100635static int st21nfca_hci_target_from_gate(struct nfc_hci_dev *hdev, u8 gate,
636 struct nfc_target *target)
637{
638 int r, len;
639 u16 atqa;
640 u8 sak;
641 u8 uid[NFC_NFCID1_MAXSIZE];
642
643 switch (gate) {
644 case ST21NFCA_RF_READER_F_GATE:
645 target->supported_protocols = NFC_PROTO_FELICA_MASK;
646 break;
647 case ST21NFCA_RF_READER_14443_3_A_GATE:
648 /* ISO14443-3 type 1 or 2 tags */
649 r = st21nfca_get_iso14443_3_atqa(hdev, &atqa);
650 if (r < 0)
651 return r;
652 if (atqa == 0x000c) {
653 target->supported_protocols = NFC_PROTO_JEWEL_MASK;
654 target->sens_res = 0x0c00;
655 } else {
656 r = st21nfca_get_iso14443_3_sak(hdev, &sak);
657 if (r < 0)
658 return r;
659
660 r = st21nfca_get_iso14443_3_uid(hdev, uid, &len);
661 if (r < 0)
662 return r;
663
664 target->supported_protocols =
665 nfc_hci_sak_to_protocol(sak);
666 if (target->supported_protocols == 0xffffffff)
667 return -EPROTO;
668
669 target->sens_res = atqa;
670 target->sel_res = sak;
671 memcpy(target->nfcid1, uid, len);
672 target->nfcid1_len = len;
673 }
674
675 break;
Christophe Ricard79747282014-05-13 22:03:42 +0200676 case ST21NFCA_RF_READER_ISO15693_GATE:
677 target->supported_protocols = NFC_PROTO_ISO15693_MASK;
678 r = st21nfca_get_iso15693_inventory(hdev, target);
679 if (r < 0)
680 return r;
681 break;
Christophe Ricard68957302014-03-25 06:51:47 +0100682 default:
683 return -EPROTO;
684 }
685
686 return 0;
687}
688
Christophe Ricard1892bf82014-05-20 22:21:59 +0200689static int st21nfca_hci_complete_target_discovered(struct nfc_hci_dev *hdev,
690 u8 gate,
691 struct nfc_target *target)
692{
693 int r;
Christophe Ricardc490c552015-01-25 23:33:28 +0100694 struct sk_buff *nfcid_skb = NULL;
Christophe Ricard1892bf82014-05-20 22:21:59 +0200695
696 if (gate == ST21NFCA_RF_READER_F_GATE) {
697 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
Christophe Ricardc490c552015-01-25 23:33:28 +0100698 ST21NFCA_RF_READER_F_NFCID2, &nfcid_skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200699 if (r < 0)
700 goto exit;
701
Christophe Ricardc490c552015-01-25 23:33:28 +0100702 if (nfcid_skb->len > NFC_SENSF_RES_MAXSIZE) {
Christophe Ricard1892bf82014-05-20 22:21:59 +0200703 r = -EPROTO;
704 goto exit;
705 }
706
707 /*
708 * - After the recepton of polling response for type F frame
709 * at 212 or 424 Kbit/s, NFCID2 registry parameters will be
710 * updated.
711 * - After the reception of SEL_RES with NFCIP-1 compliant bit
712 * set for type A frame NFCID1 will be updated
713 */
Christophe Ricardc490c552015-01-25 23:33:28 +0100714 if (nfcid_skb->len > 0) {
Christophe Ricard1892bf82014-05-20 22:21:59 +0200715 /* P2P in type F */
Christophe Ricardc490c552015-01-25 23:33:28 +0100716 memcpy(target->sensf_res, nfcid_skb->data,
717 nfcid_skb->len);
718 target->sensf_res_len = nfcid_skb->len;
Christophe Ricard1892bf82014-05-20 22:21:59 +0200719 /* NFC Forum Digital Protocol Table 44 */
720 if (target->sensf_res[0] == 0x01 &&
721 target->sensf_res[1] == 0xfe)
722 target->supported_protocols =
723 NFC_PROTO_NFC_DEP_MASK;
724 else
725 target->supported_protocols =
726 NFC_PROTO_FELICA_MASK;
727 } else {
Christophe Ricardc490c552015-01-25 23:33:28 +0100728 kfree_skb(nfcid_skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200729 /* P2P in type A */
730 r = nfc_hci_get_param(hdev, ST21NFCA_RF_READER_F_GATE,
731 ST21NFCA_RF_READER_F_NFCID1,
Christophe Ricardc490c552015-01-25 23:33:28 +0100732 &nfcid_skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200733 if (r < 0)
734 goto exit;
735
Christophe Ricardc490c552015-01-25 23:33:28 +0100736 if (nfcid_skb->len > NFC_NFCID1_MAXSIZE) {
Christophe Ricard1892bf82014-05-20 22:21:59 +0200737 r = -EPROTO;
738 goto exit;
739 }
Christophe Ricardc490c552015-01-25 23:33:28 +0100740 memcpy(target->sensf_res, nfcid_skb->data,
741 nfcid_skb->len);
742 target->sensf_res_len = nfcid_skb->len;
Christophe Ricard1892bf82014-05-20 22:21:59 +0200743 target->supported_protocols = NFC_PROTO_NFC_DEP_MASK;
744 }
745 target->hci_reader_gate = ST21NFCA_RF_READER_F_GATE;
746 }
747 r = 1;
748exit:
Christophe Ricardc490c552015-01-25 23:33:28 +0100749 kfree_skb(nfcid_skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200750 return r;
751}
752
Christophe Ricard79747282014-05-13 22:03:42 +0200753#define ST21NFCA_CB_TYPE_READER_ISO15693 1
754static void st21nfca_hci_data_exchange_cb(void *context, struct sk_buff *skb,
755 int err)
756{
757 struct st21nfca_hci_info *info = context;
758
759 switch (info->async_cb_type) {
760 case ST21NFCA_CB_TYPE_READER_ISO15693:
761 if (err == 0)
762 skb_trim(skb, skb->len - 1);
763 info->async_cb(info->async_cb_context, skb, err);
764 break;
765 default:
766 if (err == 0)
767 kfree_skb(skb);
768 break;
769 }
770}
771
Christophe Ricard68957302014-03-25 06:51:47 +0100772/*
773 * Returns:
774 * <= 0: driver handled the data exchange
775 * 1: driver doesn't especially handle, please do standard processing
776 */
777static int st21nfca_hci_im_transceive(struct nfc_hci_dev *hdev,
778 struct nfc_target *target,
779 struct sk_buff *skb,
780 data_exchange_cb_t cb, void *cb_context)
781{
Christophe Ricard79747282014-05-13 22:03:42 +0200782 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
783
Christophe Ricard68957302014-03-25 06:51:47 +0100784 pr_info(DRIVER_DESC ": %s for gate=%d len=%d\n", __func__,
785 target->hci_reader_gate, skb->len);
786
787 switch (target->hci_reader_gate) {
788 case ST21NFCA_RF_READER_F_GATE:
Christophe Ricard1892bf82014-05-20 22:21:59 +0200789 if (target->supported_protocols == NFC_PROTO_NFC_DEP_MASK)
790 return st21nfca_im_send_dep_req(hdev, skb);
791
Christophe Ricard68957302014-03-25 06:51:47 +0100792 *skb_push(skb, 1) = 0x1a;
793 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
794 ST21NFCA_WR_XCHG_DATA, skb->data,
795 skb->len, cb, cb_context);
796 case ST21NFCA_RF_READER_14443_3_A_GATE:
797 *skb_push(skb, 1) = 0x1a; /* CTR, see spec:10.2.2.1 */
798
799 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
800 ST21NFCA_WR_XCHG_DATA, skb->data,
801 skb->len, cb, cb_context);
Christophe Ricard79747282014-05-13 22:03:42 +0200802 case ST21NFCA_RF_READER_ISO15693_GATE:
803 info->async_cb_type = ST21NFCA_CB_TYPE_READER_ISO15693;
804 info->async_cb = cb;
805 info->async_cb_context = cb_context;
806
807 *skb_push(skb, 1) = 0x17;
808
809 return nfc_hci_send_cmd_async(hdev, target->hci_reader_gate,
810 ST21NFCA_WR_XCHG_DATA, skb->data,
811 skb->len,
812 st21nfca_hci_data_exchange_cb,
813 info);
814 break;
Christophe Ricard68957302014-03-25 06:51:47 +0100815 default:
816 return 1;
817 }
818}
819
Christophe Ricard1892bf82014-05-20 22:21:59 +0200820static int st21nfca_hci_tm_send(struct nfc_hci_dev *hdev, struct sk_buff *skb)
821{
822 return st21nfca_tm_send_dep_res(hdev, skb);
823}
824
Christophe Ricard68957302014-03-25 06:51:47 +0100825static int st21nfca_hci_check_presence(struct nfc_hci_dev *hdev,
826 struct nfc_target *target)
827{
828 u8 fwi = 0x11;
Christophe Ricard3e6df912014-07-28 18:11:31 +0200829
Christophe Ricard68957302014-03-25 06:51:47 +0100830 switch (target->hci_reader_gate) {
831 case NFC_HCI_RF_READER_A_GATE:
832 case NFC_HCI_RF_READER_B_GATE:
833 /*
834 * PRESENCE_CHECK on those gates is available
835 * However, the answer to this command is taking 3 * fwi
836 * if the card is no present.
837 * Instead, we send an empty I-Frame with a very short
838 * configurable fwi ~604µs.
839 */
840 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
841 ST21NFCA_WR_XCHG_DATA, &fwi, 1, NULL);
842 case ST21NFCA_RF_READER_14443_3_A_GATE:
843 return nfc_hci_send_cmd(hdev, target->hci_reader_gate,
844 ST21NFCA_RF_READER_CMD_PRESENCE_CHECK,
845 NULL, 0, NULL);
846 default:
847 return -EOPNOTSUPP;
848 }
849}
850
Christophe Ricard2130fb92015-01-27 01:18:19 +0100851static void st21nfca_hci_cmd_received(struct nfc_hci_dev *hdev, u8 pipe, u8 cmd,
852 struct sk_buff *skb)
853{
854 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
855 u8 gate = hdev->pipes[pipe].gate;
856
857 pr_debug("cmd: %x\n", cmd);
858
859 switch (cmd) {
860 case NFC_HCI_ANY_OPEN_PIPE:
861 if (gate != ST21NFCA_APDU_READER_GATE &&
862 hdev->pipes[pipe].dest_host != NFC_HCI_UICC_HOST_ID)
863 info->se_info.count_pipes++;
864
865 if (info->se_info.count_pipes == info->se_info.expected_pipes) {
866 del_timer_sync(&info->se_info.se_active_timer);
867 info->se_info.se_active = false;
868 info->se_info.count_pipes = 0;
869 complete(&info->se_info.req_completion);
870 }
871 break;
872 }
873}
874
875static int st21nfca_admin_event_received(struct nfc_hci_dev *hdev, u8 event,
876 struct sk_buff *skb)
877{
878 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
879
880 pr_debug("admin event: %x\n", event);
881
882 switch (event) {
883 case ST21NFCA_EVT_HOT_PLUG:
884 if (info->se_info.se_active) {
885 if (!ST21NFCA_EVT_HOT_PLUG_IS_INHIBITED(skb)) {
886 del_timer_sync(&info->se_info.se_active_timer);
887 info->se_info.se_active = false;
888 complete(&info->se_info.req_completion);
889 } else {
890 mod_timer(&info->se_info.se_active_timer,
891 jiffies +
892 msecs_to_jiffies(ST21NFCA_SE_TO_PIPES));
893 }
894 }
895 break;
Christophe Ricarda9e062d2015-10-25 22:54:47 +0100896 default:
897 nfc_err(&hdev->ndev->dev, "Unexpected event on admin gate\n");
Christophe Ricard2130fb92015-01-27 01:18:19 +0100898 }
899 kfree_skb(skb);
900 return 0;
901}
902
Christophe Ricard1892bf82014-05-20 22:21:59 +0200903/*
904 * Returns:
905 * <= 0: driver handled the event, skb consumed
906 * 1: driver does not handle the event, please do standard processing
907 */
Christophe Ricard2130fb92015-01-27 01:18:19 +0100908static int st21nfca_hci_event_received(struct nfc_hci_dev *hdev, u8 pipe,
Christophe Ricard1892bf82014-05-20 22:21:59 +0200909 u8 event, struct sk_buff *skb)
910{
Christophe Ricard2130fb92015-01-27 01:18:19 +0100911 u8 gate = hdev->pipes[pipe].gate;
912 u8 host = hdev->pipes[pipe].dest_host;
913
Christophe Ricarda4415e72014-11-13 00:30:39 +0100914 pr_debug("hci event: %d gate: %x\n", event, gate);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200915
Christophe Ricarda4415e72014-11-13 00:30:39 +0100916 switch (gate) {
Christophe Ricard2130fb92015-01-27 01:18:19 +0100917 case NFC_HCI_ADMIN_GATE:
918 return st21nfca_admin_event_received(hdev, event, skb);
Christophe Ricarda4415e72014-11-13 00:30:39 +0100919 case ST21NFCA_RF_CARD_F_GATE:
920 return st21nfca_dep_event_received(hdev, event, skb);
Christophe Ricard2130fb92015-01-27 01:18:19 +0100921 case ST21NFCA_CONNECTIVITY_GATE:
922 return st21nfca_connectivity_event_received(hdev, host,
923 event, skb);
924 case ST21NFCA_APDU_READER_GATE:
925 return st21nfca_apdu_reader_event_received(hdev, event, skb);
Christophe Ricard15d17172015-10-26 07:50:11 +0100926 case NFC_HCI_LOOPBACK_GATE:
927 return st21nfca_hci_loopback_event_received(hdev, event, skb);
Christophe Ricard1892bf82014-05-20 22:21:59 +0200928 default:
929 return 1;
930 }
Christophe Ricard1892bf82014-05-20 22:21:59 +0200931}
932
Christophe Ricard68957302014-03-25 06:51:47 +0100933static struct nfc_hci_ops st21nfca_hci_ops = {
934 .open = st21nfca_hci_open,
935 .close = st21nfca_hci_close,
Christophe Ricard58e1e0a2014-03-25 06:51:50 +0100936 .load_session = st21nfca_hci_load_session,
Christophe Ricard68957302014-03-25 06:51:47 +0100937 .hci_ready = st21nfca_hci_ready,
938 .xmit = st21nfca_hci_xmit,
939 .start_poll = st21nfca_hci_start_poll,
Christophe Ricardd57d74e2014-05-20 22:21:58 +0200940 .stop_poll = st21nfca_hci_stop_poll,
Christophe Ricard1892bf82014-05-20 22:21:59 +0200941 .dep_link_up = st21nfca_hci_dep_link_up,
942 .dep_link_down = st21nfca_hci_dep_link_down,
Christophe Ricard68957302014-03-25 06:51:47 +0100943 .target_from_gate = st21nfca_hci_target_from_gate,
Christophe Ricard1892bf82014-05-20 22:21:59 +0200944 .complete_target_discovered = st21nfca_hci_complete_target_discovered,
Christophe Ricard68957302014-03-25 06:51:47 +0100945 .im_transceive = st21nfca_hci_im_transceive,
Christophe Ricard1892bf82014-05-20 22:21:59 +0200946 .tm_send = st21nfca_hci_tm_send,
Christophe Ricard68957302014-03-25 06:51:47 +0100947 .check_presence = st21nfca_hci_check_presence,
Christophe Ricard1892bf82014-05-20 22:21:59 +0200948 .event_received = st21nfca_hci_event_received,
Christophe Ricard2130fb92015-01-27 01:18:19 +0100949 .cmd_received = st21nfca_hci_cmd_received,
950 .discover_se = st21nfca_hci_discover_se,
951 .enable_se = st21nfca_hci_enable_se,
952 .disable_se = st21nfca_hci_disable_se,
953 .se_io = st21nfca_hci_se_io,
Christophe Ricard68957302014-03-25 06:51:47 +0100954};
955
956int st21nfca_hci_probe(void *phy_id, struct nfc_phy_ops *phy_ops,
957 char *llc_name, int phy_headroom, int phy_tailroom,
Christophe Ricard2130fb92015-01-27 01:18:19 +0100958 int phy_payload, struct nfc_hci_dev **hdev,
959 struct st21nfca_se_status *se_status)
Christophe Ricard68957302014-03-25 06:51:47 +0100960{
961 struct st21nfca_hci_info *info;
962 int r = 0;
963 int dev_num;
964 u32 protocols;
965 struct nfc_hci_init_data init_data;
966 unsigned long quirks = 0;
967
968 info = kzalloc(sizeof(struct st21nfca_hci_info), GFP_KERNEL);
969 if (!info) {
970 r = -ENOMEM;
971 goto err_alloc_hdev;
972 }
973
974 info->phy_ops = phy_ops;
975 info->phy_id = phy_id;
976 info->state = ST21NFCA_ST_COLD;
977 mutex_init(&info->info_lock);
978
979 init_data.gate_count = ARRAY_SIZE(st21nfca_gates);
980
981 memcpy(init_data.gates, st21nfca_gates, sizeof(st21nfca_gates));
982
983 /*
984 * Session id must include the driver name + i2c bus addr
985 * persistent info to discriminate 2 identical chips
986 */
987 dev_num = find_first_zero_bit(dev_mask, ST21NFCA_NUM_DEVICES);
988 if (dev_num >= ST21NFCA_NUM_DEVICES)
Christophe Ricard9ec1f582014-07-28 18:11:37 +0200989 return -ENODEV;
990
991 set_bit(dev_num, dev_mask);
Christophe Ricard68957302014-03-25 06:51:47 +0100992
993 scnprintf(init_data.session_id, sizeof(init_data.session_id), "%s%2x",
994 "ST21AH", dev_num);
995
996 protocols = NFC_PROTO_JEWEL_MASK |
997 NFC_PROTO_MIFARE_MASK |
998 NFC_PROTO_FELICA_MASK |
999 NFC_PROTO_ISO14443_MASK |
Christophe Ricard79747282014-05-13 22:03:42 +02001000 NFC_PROTO_ISO14443_B_MASK |
Christophe Ricard1892bf82014-05-20 22:21:59 +02001001 NFC_PROTO_ISO15693_MASK |
1002 NFC_PROTO_NFC_DEP_MASK;
Christophe Ricard68957302014-03-25 06:51:47 +01001003
1004 set_bit(NFC_HCI_QUIRK_SHORT_CLEAR, &quirks);
1005
1006 info->hdev =
1007 nfc_hci_allocate_device(&st21nfca_hci_ops, &init_data, quirks,
1008 protocols, llc_name,
1009 phy_headroom + ST21NFCA_CMDS_HEADROOM,
1010 phy_tailroom, phy_payload);
1011
1012 if (!info->hdev) {
1013 pr_err("Cannot allocate nfc hdev.\n");
1014 r = -ENOMEM;
1015 goto err_alloc_hdev;
1016 }
1017
Christophe Ricard2130fb92015-01-27 01:18:19 +01001018 info->se_status = se_status;
1019
Christophe Ricard68957302014-03-25 06:51:47 +01001020 nfc_hci_set_clientdata(info->hdev, info);
1021
1022 r = nfc_hci_register_device(info->hdev);
1023 if (r)
1024 goto err_regdev;
1025
1026 *hdev = info->hdev;
Christophe Ricard1892bf82014-05-20 22:21:59 +02001027 st21nfca_dep_init(info->hdev);
Christophe Ricard2130fb92015-01-27 01:18:19 +01001028 st21nfca_se_init(info->hdev);
Christophe Ricard15d17172015-10-26 07:50:11 +01001029 st21nfca_vendor_cmds_init(info->hdev);
Christophe Ricard68957302014-03-25 06:51:47 +01001030
1031 return 0;
1032
1033err_regdev:
1034 nfc_hci_free_device(info->hdev);
1035
1036err_alloc_hdev:
1037 kfree(info);
1038
1039 return r;
1040}
1041EXPORT_SYMBOL(st21nfca_hci_probe);
1042
1043void st21nfca_hci_remove(struct nfc_hci_dev *hdev)
1044{
1045 struct st21nfca_hci_info *info = nfc_hci_get_clientdata(hdev);
1046
Christophe Ricard1892bf82014-05-20 22:21:59 +02001047 st21nfca_dep_deinit(hdev);
Christophe Ricard2130fb92015-01-27 01:18:19 +01001048 st21nfca_se_deinit(hdev);
Christophe Ricard68957302014-03-25 06:51:47 +01001049 nfc_hci_unregister_device(hdev);
1050 nfc_hci_free_device(hdev);
1051 kfree(info);
1052}
1053EXPORT_SYMBOL(st21nfca_hci_remove);
1054
1055MODULE_LICENSE("GPL");
1056MODULE_DESCRIPTION(DRIVER_DESC);