Linux 6.12-rc1
[linux-2.6-block.git] / net / nfc / nci / rsp.c
CommitLineData
caab277b 1// SPDX-License-Identifier: GPL-2.0-only
6a2968aa
IE
2/*
3 * The NFC Controller Interface is the communication protocol between an
4 * NFC Controller (NFCC) and a Device Host (DH).
5 *
6 * Copyright (C) 2011 Texas Instruments, Inc.
7 *
8 * Written by Ilan Elias <ilane@ti.com>
9 *
10 * Acknowledgements:
11 * This file is based on hci_event.c, which was written
12 * by Maxim Krasnyansky.
6a2968aa
IE
13 */
14
52858b51 15#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
ed1e0ad8 16
6a2968aa
IE
17#include <linux/types.h>
18#include <linux/interrupt.h>
19#include <linux/bitops.h>
20#include <linux/skbuff.h>
21
22#include "../nfc.h"
23#include <net/nfc/nci.h>
24#include <net/nfc/nci_core.h>
25
26/* Handle NCI Response packets */
27
ddecf555
KK
28static void nci_core_reset_rsp_packet(struct nci_dev *ndev,
29 const struct sk_buff *skb)
6a2968aa 30{
ddecf555 31 const struct nci_core_reset_rsp *rsp = (void *)skb->data;
6a2968aa 32
24bf3304 33 pr_debug("status 0x%x\n", rsp->status);
6a2968aa 34
bcd684aa
BJ
35 /* Handle NCI 1.x ver */
36 if (skb->len != 1) {
37 if (rsp->status == NCI_STATUS_OK) {
38 ndev->nci_ver = rsp->nci_ver;
39 pr_debug("nci_ver 0x%x, config_status 0x%x\n",
40 rsp->nci_ver, rsp->config_status);
41 }
42
43 nci_req_complete(ndev, rsp->status);
e8c0dacd 44 }
6a2968aa
IE
45}
46
ddecf555
KK
47static u8 nci_core_init_rsp_packet_v1(struct nci_dev *ndev,
48 const struct sk_buff *skb)
6a2968aa 49{
ddecf555
KK
50 const struct nci_core_init_rsp_1 *rsp_1 = (void *)skb->data;
51 const struct nci_core_init_rsp_2 *rsp_2;
6a2968aa 52
24bf3304 53 pr_debug("status 0x%x\n", rsp_1->status);
6a2968aa
IE
54
55 if (rsp_1->status != NCI_STATUS_OK)
bcd684aa 56 return rsp_1->status;
6a2968aa
IE
57
58 ndev->nfcc_features = __le32_to_cpu(rsp_1->nfcc_features);
59 ndev->num_supported_rf_interfaces = rsp_1->num_supported_rf_interfaces;
60
bcd684aa
BJ
61 ndev->num_supported_rf_interfaces =
62 min((int)ndev->num_supported_rf_interfaces,
63 NCI_MAX_SUPPORTED_RF_INTERFACES);
6a2968aa
IE
64
65 memcpy(ndev->supported_rf_interfaces,
eb9bc6e9
SO
66 rsp_1->supported_rf_interfaces,
67 ndev->num_supported_rf_interfaces);
6a2968aa 68
e8c0dacd 69 rsp_2 = (void *) (skb->data + 6 + rsp_1->num_supported_rf_interfaces);
6a2968aa 70
eb9bc6e9 71 ndev->max_logical_connections = rsp_2->max_logical_connections;
6a2968aa
IE
72 ndev->max_routing_table_size =
73 __le16_to_cpu(rsp_2->max_routing_table_size);
e8c0dacd
IE
74 ndev->max_ctrl_pkt_payload_len =
75 rsp_2->max_ctrl_pkt_payload_len;
76 ndev->max_size_for_large_params =
77 __le16_to_cpu(rsp_2->max_size_for_large_params);
e8c0dacd
IE
78 ndev->manufact_id =
79 rsp_2->manufact_id;
80 ndev->manufact_specific_info =
81 __le32_to_cpu(rsp_2->manufact_specific_info);
82
bcd684aa
BJ
83 return NCI_STATUS_OK;
84}
85
ddecf555
KK
86static u8 nci_core_init_rsp_packet_v2(struct nci_dev *ndev,
87 const struct sk_buff *skb)
bcd684aa 88{
ddecf555
KK
89 const struct nci_core_init_rsp_nci_ver2 *rsp = (void *)skb->data;
90 const u8 *supported_rf_interface = rsp->supported_rf_interfaces;
bcd684aa
BJ
91 u8 rf_interface_idx = 0;
92 u8 rf_extension_cnt = 0;
93
94 pr_debug("status %x\n", rsp->status);
95
96 if (rsp->status != NCI_STATUS_OK)
97 return rsp->status;
98
99 ndev->nfcc_features = __le32_to_cpu(rsp->nfcc_features);
100 ndev->num_supported_rf_interfaces = rsp->num_supported_rf_interfaces;
101
102 ndev->num_supported_rf_interfaces =
103 min((int)ndev->num_supported_rf_interfaces,
104 NCI_MAX_SUPPORTED_RF_INTERFACES);
105
106 while (rf_interface_idx < ndev->num_supported_rf_interfaces) {
107 ndev->supported_rf_interfaces[rf_interface_idx++] = *supported_rf_interface++;
108
109 /* skip rf extension parameters */
110 rf_extension_cnt = *supported_rf_interface++;
111 supported_rf_interface += rf_extension_cnt;
112 }
113
114 ndev->max_logical_connections = rsp->max_logical_connections;
115 ndev->max_routing_table_size =
116 __le16_to_cpu(rsp->max_routing_table_size);
117 ndev->max_ctrl_pkt_payload_len =
118 rsp->max_ctrl_pkt_payload_len;
119 ndev->max_size_for_large_params = NCI_MAX_LARGE_PARAMS_NCI_v2;
120
121 return NCI_STATUS_OK;
122}
123
ddecf555 124static void nci_core_init_rsp_packet(struct nci_dev *ndev, const struct sk_buff *skb)
bcd684aa
BJ
125{
126 u8 status = 0;
127
128 if (!(ndev->nci_ver & NCI_VER_2_MASK))
129 status = nci_core_init_rsp_packet_v1(ndev, skb);
130 else
131 status = nci_core_init_rsp_packet_v2(ndev, skb);
132
133 if (status != NCI_STATUS_OK)
134 goto exit;
135
20c239c1
JP
136 pr_debug("nfcc_features 0x%x\n",
137 ndev->nfcc_features);
138 pr_debug("num_supported_rf_interfaces %d\n",
139 ndev->num_supported_rf_interfaces);
140 pr_debug("supported_rf_interfaces[0] 0x%x\n",
141 ndev->supported_rf_interfaces[0]);
142 pr_debug("supported_rf_interfaces[1] 0x%x\n",
143 ndev->supported_rf_interfaces[1]);
144 pr_debug("supported_rf_interfaces[2] 0x%x\n",
145 ndev->supported_rf_interfaces[2]);
146 pr_debug("supported_rf_interfaces[3] 0x%x\n",
147 ndev->supported_rf_interfaces[3]);
148 pr_debug("max_logical_connections %d\n",
149 ndev->max_logical_connections);
150 pr_debug("max_routing_table_size %d\n",
151 ndev->max_routing_table_size);
152 pr_debug("max_ctrl_pkt_payload_len %d\n",
153 ndev->max_ctrl_pkt_payload_len);
154 pr_debug("max_size_for_large_params %d\n",
155 ndev->max_size_for_large_params);
20c239c1
JP
156 pr_debug("manufact_id 0x%x\n",
157 ndev->manufact_id);
158 pr_debug("manufact_specific_info 0x%x\n",
159 ndev->manufact_specific_info);
e8c0dacd
IE
160
161exit:
bcd684aa 162 nci_req_complete(ndev, status);
6a2968aa
IE
163}
164
7e035230 165static void nci_core_set_config_rsp_packet(struct nci_dev *ndev,
ddecf555 166 const struct sk_buff *skb)
7e035230 167{
ddecf555 168 const struct nci_core_set_config_rsp *rsp = (void *)skb->data;
7e035230
IE
169
170 pr_debug("status 0x%x\n", rsp->status);
171
172 nci_req_complete(ndev, rsp->status);
173}
174
6a2968aa 175static void nci_rf_disc_map_rsp_packet(struct nci_dev *ndev,
ddecf555 176 const struct sk_buff *skb)
6a2968aa
IE
177{
178 __u8 status = skb->data[0];
179
24bf3304 180 pr_debug("status 0x%x\n", status);
6a2968aa
IE
181
182 nci_req_complete(ndev, status);
183}
184
ddecf555
KK
185static void nci_rf_disc_rsp_packet(struct nci_dev *ndev,
186 const struct sk_buff *skb)
6a2968aa 187{
77411df5 188 struct nci_conn_info *conn_info;
6a2968aa
IE
189 __u8 status = skb->data[0];
190
24bf3304 191 pr_debug("status 0x%x\n", status);
6a2968aa 192
4aeee687 193 if (status == NCI_STATUS_OK) {
8939e47f 194 atomic_set(&ndev->state, NCI_DISCOVERY);
6a2968aa 195
12bdf27d 196 conn_info = ndev->rf_conn_info;
4aeee687
CR
197 if (!conn_info) {
198 conn_info = devm_kzalloc(&ndev->nfc_dev->dev,
199 sizeof(struct nci_conn_info),
200 GFP_KERNEL);
201 if (!conn_info) {
202 status = NCI_STATUS_REJECTED;
203 goto exit;
204 }
205 conn_info->conn_id = NCI_STATIC_RF_CONN_ID;
206 INIT_LIST_HEAD(&conn_info->list);
207 list_add(&conn_info->list, &ndev->conn_info_list);
12bdf27d 208 ndev->rf_conn_info = conn_info;
4aeee687
CR
209 }
210 }
211
212exit:
6a2968aa
IE
213 nci_req_complete(ndev, status);
214}
215
019c4fba 216static void nci_rf_disc_select_rsp_packet(struct nci_dev *ndev,
ddecf555 217 const struct sk_buff *skb)
019c4fba
IE
218{
219 __u8 status = skb->data[0];
220
221 pr_debug("status 0x%x\n", status);
222
223 /* Complete the request on intf_activated_ntf or generic_error_ntf */
224 if (status != NCI_STATUS_OK)
225 nci_req_complete(ndev, status);
226}
227
6a2968aa 228static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev,
ddecf555 229 const struct sk_buff *skb)
6a2968aa
IE
230{
231 __u8 status = skb->data[0];
232
24bf3304 233 pr_debug("status 0x%x\n", status);
6a2968aa 234
bd7e01bc
IE
235 /* If target was active, complete the request only in deactivate_ntf */
236 if ((status != NCI_STATUS_OK) ||
eb9bc6e9 237 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
019c4fba 238 nci_clear_target_list(ndev);
8939e47f 239 atomic_set(&ndev->state, NCI_IDLE);
bd7e01bc 240 nci_req_complete(ndev, status);
8939e47f 241 }
6a2968aa
IE
242}
243
af9c8aa6 244static void nci_nfcee_discover_rsp_packet(struct nci_dev *ndev,
ddecf555 245 const struct sk_buff *skb)
af9c8aa6 246{
ddecf555 247 const struct nci_nfcee_discover_rsp *discover_rsp;
af9c8aa6
CR
248
249 if (skb->len != 2) {
250 nci_req_complete(ndev, NCI_STATUS_NFCEE_PROTOCOL_ERROR);
251 return;
252 }
253
254 discover_rsp = (struct nci_nfcee_discover_rsp *)skb->data;
255
256 if (discover_rsp->status != NCI_STATUS_OK ||
257 discover_rsp->num_nfcee == 0)
258 nci_req_complete(ndev, discover_rsp->status);
259}
260
f7f793f3 261static void nci_nfcee_mode_set_rsp_packet(struct nci_dev *ndev,
ddecf555 262 const struct sk_buff *skb)
f7f793f3
CR
263{
264 __u8 status = skb->data[0];
265
266 pr_debug("status 0x%x\n", status);
267 nci_req_complete(ndev, status);
268}
269
736bb957 270static void nci_core_conn_create_rsp_packet(struct nci_dev *ndev,
ddecf555 271 const struct sk_buff *skb)
736bb957
CR
272{
273 __u8 status = skb->data[0];
9b8d1a4c 274 struct nci_conn_info *conn_info = NULL;
ddecf555 275 const struct nci_core_conn_create_rsp *rsp;
736bb957
CR
276
277 pr_debug("status 0x%x\n", status);
278
279 if (status == NCI_STATUS_OK) {
280 rsp = (struct nci_core_conn_create_rsp *)skb->data;
736bb957 281
15d4a8da
CR
282 conn_info = devm_kzalloc(&ndev->nfc_dev->dev,
283 sizeof(*conn_info), GFP_KERNEL);
284 if (!conn_info) {
736bb957
CR
285 status = NCI_STATUS_REJECTED;
286 goto exit;
287 }
288
9b8d1a4c
CR
289 conn_info->dest_params = devm_kzalloc(&ndev->nfc_dev->dev,
290 sizeof(struct dest_spec_params),
291 GFP_KERNEL);
292 if (!conn_info->dest_params) {
293 status = NCI_STATUS_REJECTED;
294 goto free_conn_info;
295 }
296
297 conn_info->dest_type = ndev->cur_dest_type;
298 conn_info->dest_params->id = ndev->cur_params.id;
299 conn_info->dest_params->protocol = ndev->cur_params.protocol;
15d4a8da
CR
300 conn_info->conn_id = rsp->conn_id;
301
302 /* Note: data_exchange_cb and data_exchange_cb_context need to
303 * be specify out of nci_core_conn_create_rsp_packet
304 */
305
306 INIT_LIST_HEAD(&conn_info->list);
307 list_add(&conn_info->list, &ndev->conn_info_list);
308
9b8d1a4c 309 if (ndev->cur_params.id == ndev->hci_dev->nfcee_id)
15d4a8da
CR
310 ndev->hci_dev->conn_info = conn_info;
311
736bb957
CR
312 conn_info->conn_id = rsp->conn_id;
313 conn_info->max_pkt_payload_len = rsp->max_ctrl_pkt_payload_len;
3ba5c846 314 atomic_set(&conn_info->credits_cnt, rsp->credits_cnt);
736bb957
CR
315 }
316
9b8d1a4c
CR
317free_conn_info:
318 if (status == NCI_STATUS_REJECTED)
319 devm_kfree(&ndev->nfc_dev->dev, conn_info);
736bb957 320exit:
9b8d1a4c 321
736bb957
CR
322 nci_req_complete(ndev, status);
323}
324
325static void nci_core_conn_close_rsp_packet(struct nci_dev *ndev,
ddecf555 326 const struct sk_buff *skb)
736bb957 327{
15d4a8da 328 struct nci_conn_info *conn_info;
736bb957
CR
329 __u8 status = skb->data[0];
330
331 pr_debug("status 0x%x\n", status);
15d4a8da 332 if (status == NCI_STATUS_OK) {
de5ea851
CR
333 conn_info = nci_get_conn_info_by_conn_id(ndev,
334 ndev->cur_conn_id);
15d4a8da
CR
335 if (conn_info) {
336 list_del(&conn_info->list);
1b1499a8
LM
337 if (conn_info == ndev->rf_conn_info)
338 ndev->rf_conn_info = NULL;
15d4a8da
CR
339 devm_kfree(&ndev->nfc_dev->dev, conn_info);
340 }
341 }
736bb957
CR
342 nci_req_complete(ndev, status);
343}
344
6a2968aa
IE
345void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
346{
347 __u16 rsp_opcode = nci_opcode(skb->data);
348
349 /* we got a rsp, stop the cmd timer */
350 del_timer(&ndev->cmd_timer);
351
20c239c1
JP
352 pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
353 nci_pbf(skb->data),
354 nci_opcode_gid(rsp_opcode),
355 nci_opcode_oid(rsp_opcode),
356 nci_plen(skb->data));
6a2968aa
IE
357
358 /* strip the nci control header */
359 skb_pull(skb, NCI_CTRL_HDR_SIZE);
360
b6355e97
SO
361 if (nci_opcode_gid(rsp_opcode) == NCI_GID_PROPRIETARY) {
362 if (nci_prop_rsp_packet(ndev, rsp_opcode, skb) == -ENOTSUPP) {
363 pr_err("unsupported rsp opcode 0x%x\n",
364 rsp_opcode);
365 }
366
367 goto end;
368 }
369
6a2968aa
IE
370 switch (rsp_opcode) {
371 case NCI_OP_CORE_RESET_RSP:
372 nci_core_reset_rsp_packet(ndev, skb);
373 break;
374
375 case NCI_OP_CORE_INIT_RSP:
376 nci_core_init_rsp_packet(ndev, skb);
377 break;
378
7e035230
IE
379 case NCI_OP_CORE_SET_CONFIG_RSP:
380 nci_core_set_config_rsp_packet(ndev, skb);
381 break;
382
736bb957
CR
383 case NCI_OP_CORE_CONN_CREATE_RSP:
384 nci_core_conn_create_rsp_packet(ndev, skb);
385 break;
386
387 case NCI_OP_CORE_CONN_CLOSE_RSP:
388 nci_core_conn_close_rsp_packet(ndev, skb);
389 break;
390
6a2968aa
IE
391 case NCI_OP_RF_DISCOVER_MAP_RSP:
392 nci_rf_disc_map_rsp_packet(ndev, skb);
393 break;
394
395 case NCI_OP_RF_DISCOVER_RSP:
396 nci_rf_disc_rsp_packet(ndev, skb);
397 break;
398
019c4fba
IE
399 case NCI_OP_RF_DISCOVER_SELECT_RSP:
400 nci_rf_disc_select_rsp_packet(ndev, skb);
401 break;
402
6a2968aa
IE
403 case NCI_OP_RF_DEACTIVATE_RSP:
404 nci_rf_deactivate_rsp_packet(ndev, skb);
405 break;
406
af9c8aa6
CR
407 case NCI_OP_NFCEE_DISCOVER_RSP:
408 nci_nfcee_discover_rsp_packet(ndev, skb);
409 break;
410
f7f793f3
CR
411 case NCI_OP_NFCEE_MODE_SET_RSP:
412 nci_nfcee_mode_set_rsp_packet(ndev, skb);
413 break;
414
6a2968aa 415 default:
ed1e0ad8 416 pr_err("unknown rsp opcode 0x%x\n", rsp_opcode);
6a2968aa
IE
417 break;
418 }
419
0a97a3cb 420 nci_core_rsp_packet(ndev, rsp_opcode, skb);
b6355e97 421end:
6a2968aa
IE
422 kfree_skb(skb);
423
424 /* trigger the next cmd */
425 atomic_set(&ndev->cmd_cnt, 1);
426 if (!skb_queue_empty(&ndev->cmd_q))
427 queue_work(ndev->cmd_wq, &ndev->cmd_work);
428}