2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
5 * Copyright (C) 2011 Texas Instruments, Inc.
7 * Written by Ilan Elias <ilane@ti.com>
10 * This file is based on hci_event.c, which was written
11 * by Maxim Krasnyansky.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2
15 * as published by the Free Software Foundation
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
29 #include <linux/types.h>
30 #include <linux/interrupt.h>
31 #include <linux/bitops.h>
32 #include <linux/skbuff.h>
35 #include <net/nfc/nci.h>
36 #include <net/nfc/nci_core.h>
38 /* Handle NCI Response packets */
40 static void nci_core_reset_rsp_packet(struct nci_dev
*ndev
, struct sk_buff
*skb
)
42 struct nci_core_reset_rsp
*rsp
= (void *) skb
->data
;
44 pr_debug("status 0x%x\n", rsp
->status
);
46 if (rsp
->status
== NCI_STATUS_OK
) {
47 ndev
->nci_ver
= rsp
->nci_ver
;
48 pr_debug("nci_ver 0x%x, config_status 0x%x\n",
49 rsp
->nci_ver
, rsp
->config_status
);
52 nci_req_complete(ndev
, rsp
->status
);
55 static void nci_core_init_rsp_packet(struct nci_dev
*ndev
, struct sk_buff
*skb
)
57 struct nci_core_init_rsp_1
*rsp_1
= (void *) skb
->data
;
58 struct nci_core_init_rsp_2
*rsp_2
;
60 pr_debug("status 0x%x\n", rsp_1
->status
);
62 if (rsp_1
->status
!= NCI_STATUS_OK
)
65 ndev
->nfcc_features
= __le32_to_cpu(rsp_1
->nfcc_features
);
66 ndev
->num_supported_rf_interfaces
= rsp_1
->num_supported_rf_interfaces
;
68 if (ndev
->num_supported_rf_interfaces
>
69 NCI_MAX_SUPPORTED_RF_INTERFACES
) {
70 ndev
->num_supported_rf_interfaces
=
71 NCI_MAX_SUPPORTED_RF_INTERFACES
;
74 memcpy(ndev
->supported_rf_interfaces
,
75 rsp_1
->supported_rf_interfaces
,
76 ndev
->num_supported_rf_interfaces
);
78 rsp_2
= (void *) (skb
->data
+ 6 + rsp_1
->num_supported_rf_interfaces
);
80 ndev
->max_logical_connections
= rsp_2
->max_logical_connections
;
81 ndev
->max_routing_table_size
=
82 __le16_to_cpu(rsp_2
->max_routing_table_size
);
83 ndev
->max_ctrl_pkt_payload_len
=
84 rsp_2
->max_ctrl_pkt_payload_len
;
85 ndev
->max_size_for_large_params
=
86 __le16_to_cpu(rsp_2
->max_size_for_large_params
);
89 ndev
->manufact_specific_info
=
90 __le32_to_cpu(rsp_2
->manufact_specific_info
);
92 pr_debug("nfcc_features 0x%x\n",
94 pr_debug("num_supported_rf_interfaces %d\n",
95 ndev
->num_supported_rf_interfaces
);
96 pr_debug("supported_rf_interfaces[0] 0x%x\n",
97 ndev
->supported_rf_interfaces
[0]);
98 pr_debug("supported_rf_interfaces[1] 0x%x\n",
99 ndev
->supported_rf_interfaces
[1]);
100 pr_debug("supported_rf_interfaces[2] 0x%x\n",
101 ndev
->supported_rf_interfaces
[2]);
102 pr_debug("supported_rf_interfaces[3] 0x%x\n",
103 ndev
->supported_rf_interfaces
[3]);
104 pr_debug("max_logical_connections %d\n",
105 ndev
->max_logical_connections
);
106 pr_debug("max_routing_table_size %d\n",
107 ndev
->max_routing_table_size
);
108 pr_debug("max_ctrl_pkt_payload_len %d\n",
109 ndev
->max_ctrl_pkt_payload_len
);
110 pr_debug("max_size_for_large_params %d\n",
111 ndev
->max_size_for_large_params
);
112 pr_debug("manufact_id 0x%x\n",
114 pr_debug("manufact_specific_info 0x%x\n",
115 ndev
->manufact_specific_info
);
118 nci_req_complete(ndev
, rsp_1
->status
);
121 static void nci_core_set_config_rsp_packet(struct nci_dev
*ndev
,
124 struct nci_core_set_config_rsp
*rsp
= (void *) skb
->data
;
126 pr_debug("status 0x%x\n", rsp
->status
);
128 nci_req_complete(ndev
, rsp
->status
);
131 static void nci_rf_disc_map_rsp_packet(struct nci_dev
*ndev
,
134 __u8 status
= skb
->data
[0];
136 pr_debug("status 0x%x\n", status
);
138 nci_req_complete(ndev
, status
);
141 static void nci_rf_disc_rsp_packet(struct nci_dev
*ndev
, struct sk_buff
*skb
)
143 struct nci_conn_info
*conn_info
;
144 __u8 status
= skb
->data
[0];
146 pr_debug("status 0x%x\n", status
);
148 if (status
== NCI_STATUS_OK
) {
149 atomic_set(&ndev
->state
, NCI_DISCOVERY
);
151 conn_info
= ndev
->rf_conn_info
;
153 conn_info
= devm_kzalloc(&ndev
->nfc_dev
->dev
,
154 sizeof(struct nci_conn_info
),
157 status
= NCI_STATUS_REJECTED
;
160 conn_info
->conn_id
= NCI_STATIC_RF_CONN_ID
;
161 INIT_LIST_HEAD(&conn_info
->list
);
162 list_add(&conn_info
->list
, &ndev
->conn_info_list
);
163 ndev
->rf_conn_info
= conn_info
;
168 nci_req_complete(ndev
, status
);
171 static void nci_rf_disc_select_rsp_packet(struct nci_dev
*ndev
,
174 __u8 status
= skb
->data
[0];
176 pr_debug("status 0x%x\n", status
);
178 /* Complete the request on intf_activated_ntf or generic_error_ntf */
179 if (status
!= NCI_STATUS_OK
)
180 nci_req_complete(ndev
, status
);
183 static void nci_rf_deactivate_rsp_packet(struct nci_dev
*ndev
,
186 __u8 status
= skb
->data
[0];
188 pr_debug("status 0x%x\n", status
);
190 /* If target was active, complete the request only in deactivate_ntf */
191 if ((status
!= NCI_STATUS_OK
) ||
192 (atomic_read(&ndev
->state
) != NCI_POLL_ACTIVE
)) {
193 nci_clear_target_list(ndev
);
194 atomic_set(&ndev
->state
, NCI_IDLE
);
195 nci_req_complete(ndev
, status
);
199 static void nci_nfcee_discover_rsp_packet(struct nci_dev
*ndev
,
202 struct nci_nfcee_discover_rsp
*discover_rsp
;
205 nci_req_complete(ndev
, NCI_STATUS_NFCEE_PROTOCOL_ERROR
);
209 discover_rsp
= (struct nci_nfcee_discover_rsp
*)skb
->data
;
211 if (discover_rsp
->status
!= NCI_STATUS_OK
||
212 discover_rsp
->num_nfcee
== 0)
213 nci_req_complete(ndev
, discover_rsp
->status
);
216 static void nci_nfcee_mode_set_rsp_packet(struct nci_dev
*ndev
,
219 __u8 status
= skb
->data
[0];
221 pr_debug("status 0x%x\n", status
);
222 nci_req_complete(ndev
, status
);
225 static void nci_core_conn_create_rsp_packet(struct nci_dev
*ndev
,
228 __u8 status
= skb
->data
[0];
229 struct nci_conn_info
*conn_info
;
230 struct nci_core_conn_create_rsp
*rsp
;
232 pr_debug("status 0x%x\n", status
);
234 if (status
== NCI_STATUS_OK
) {
235 rsp
= (struct nci_core_conn_create_rsp
*)skb
->data
;
237 conn_info
= devm_kzalloc(&ndev
->nfc_dev
->dev
,
238 sizeof(*conn_info
), GFP_KERNEL
);
240 status
= NCI_STATUS_REJECTED
;
244 conn_info
->id
= ndev
->cur_id
;
245 conn_info
->conn_id
= rsp
->conn_id
;
247 /* Note: data_exchange_cb and data_exchange_cb_context need to
248 * be specify out of nci_core_conn_create_rsp_packet
251 INIT_LIST_HEAD(&conn_info
->list
);
252 list_add(&conn_info
->list
, &ndev
->conn_info_list
);
254 if (ndev
->cur_id
== ndev
->hci_dev
->nfcee_id
)
255 ndev
->hci_dev
->conn_info
= conn_info
;
257 conn_info
->conn_id
= rsp
->conn_id
;
258 conn_info
->max_pkt_payload_len
= rsp
->max_ctrl_pkt_payload_len
;
259 atomic_set(&conn_info
->credits_cnt
, rsp
->credits_cnt
);
263 nci_req_complete(ndev
, status
);
266 static void nci_core_conn_close_rsp_packet(struct nci_dev
*ndev
,
269 struct nci_conn_info
*conn_info
;
270 __u8 status
= skb
->data
[0];
272 pr_debug("status 0x%x\n", status
);
273 if (status
== NCI_STATUS_OK
) {
274 conn_info
= nci_get_conn_info_by_conn_id(ndev
, ndev
->cur_id
);
276 list_del(&conn_info
->list
);
277 devm_kfree(&ndev
->nfc_dev
->dev
, conn_info
);
280 nci_req_complete(ndev
, status
);
283 void nci_rsp_packet(struct nci_dev
*ndev
, struct sk_buff
*skb
)
285 __u16 rsp_opcode
= nci_opcode(skb
->data
);
287 /* we got a rsp, stop the cmd timer */
288 del_timer(&ndev
->cmd_timer
);
290 pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
292 nci_opcode_gid(rsp_opcode
),
293 nci_opcode_oid(rsp_opcode
),
294 nci_plen(skb
->data
));
296 /* strip the nci control header */
297 skb_pull(skb
, NCI_CTRL_HDR_SIZE
);
299 if (nci_opcode_gid(rsp_opcode
) == NCI_GID_PROPRIETARY
) {
300 if (nci_prop_rsp_packet(ndev
, rsp_opcode
, skb
) == -ENOTSUPP
) {
301 pr_err("unsupported rsp opcode 0x%x\n",
308 switch (rsp_opcode
) {
309 case NCI_OP_CORE_RESET_RSP
:
310 nci_core_reset_rsp_packet(ndev
, skb
);
313 case NCI_OP_CORE_INIT_RSP
:
314 nci_core_init_rsp_packet(ndev
, skb
);
317 case NCI_OP_CORE_SET_CONFIG_RSP
:
318 nci_core_set_config_rsp_packet(ndev
, skb
);
321 case NCI_OP_CORE_CONN_CREATE_RSP
:
322 nci_core_conn_create_rsp_packet(ndev
, skb
);
325 case NCI_OP_CORE_CONN_CLOSE_RSP
:
326 nci_core_conn_close_rsp_packet(ndev
, skb
);
329 case NCI_OP_RF_DISCOVER_MAP_RSP
:
330 nci_rf_disc_map_rsp_packet(ndev
, skb
);
333 case NCI_OP_RF_DISCOVER_RSP
:
334 nci_rf_disc_rsp_packet(ndev
, skb
);
337 case NCI_OP_RF_DISCOVER_SELECT_RSP
:
338 nci_rf_disc_select_rsp_packet(ndev
, skb
);
341 case NCI_OP_RF_DEACTIVATE_RSP
:
342 nci_rf_deactivate_rsp_packet(ndev
, skb
);
345 case NCI_OP_NFCEE_DISCOVER_RSP
:
346 nci_nfcee_discover_rsp_packet(ndev
, skb
);
349 case NCI_OP_NFCEE_MODE_SET_RSP
:
350 nci_nfcee_mode_set_rsp_packet(ndev
, skb
);
354 pr_err("unknown rsp opcode 0x%x\n", rsp_opcode
);
358 nci_core_rsp_packet(ndev
, rsp_opcode
, skb
);
362 /* trigger the next cmd */
363 atomic_set(&ndev
->cmd_cnt
, 1);
364 if (!skb_queue_empty(&ndev
->cmd_q
))
365 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);