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, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/types.h>
31 #include <linux/interrupt.h>
32 #include <linux/bitops.h>
33 #include <linux/skbuff.h>
36 #include <net/nfc/nci.h>
37 #include <net/nfc/nci_core.h>
39 /* Handle NCI Response packets */
41 static void nci_core_reset_rsp_packet(struct nci_dev
*ndev
, struct sk_buff
*skb
)
43 struct nci_core_reset_rsp
*rsp
= (void *) skb
->data
;
45 nfc_dbg("entry, status 0x%x", rsp
->status
);
47 if (rsp
->status
== NCI_STATUS_OK
) {
48 ndev
->nci_ver
= rsp
->nci_ver
;
49 nfc_dbg("nci_ver 0x%x, config_status 0x%x",
50 rsp
->nci_ver
, rsp
->config_status
);
53 nci_req_complete(ndev
, rsp
->status
);
56 static void nci_core_init_rsp_packet(struct nci_dev
*ndev
, struct sk_buff
*skb
)
58 struct nci_core_init_rsp_1
*rsp_1
= (void *) skb
->data
;
59 struct nci_core_init_rsp_2
*rsp_2
;
61 nfc_dbg("entry, status 0x%x", rsp_1
->status
);
63 if (rsp_1
->status
!= NCI_STATUS_OK
)
66 ndev
->nfcc_features
= __le32_to_cpu(rsp_1
->nfcc_features
);
67 ndev
->num_supported_rf_interfaces
= rsp_1
->num_supported_rf_interfaces
;
69 if (ndev
->num_supported_rf_interfaces
>
70 NCI_MAX_SUPPORTED_RF_INTERFACES
) {
71 ndev
->num_supported_rf_interfaces
=
72 NCI_MAX_SUPPORTED_RF_INTERFACES
;
75 memcpy(ndev
->supported_rf_interfaces
,
76 rsp_1
->supported_rf_interfaces
,
77 ndev
->num_supported_rf_interfaces
);
79 rsp_2
= (void *) (skb
->data
+ 6 + rsp_1
->num_supported_rf_interfaces
);
81 ndev
->max_logical_connections
=
82 rsp_2
->max_logical_connections
;
83 ndev
->max_routing_table_size
=
84 __le16_to_cpu(rsp_2
->max_routing_table_size
);
85 ndev
->max_ctrl_pkt_payload_len
=
86 rsp_2
->max_ctrl_pkt_payload_len
;
87 ndev
->max_size_for_large_params
=
88 __le16_to_cpu(rsp_2
->max_size_for_large_params
);
89 ndev
->max_data_pkt_payload_size
=
90 rsp_2
->max_data_pkt_payload_size
;
91 ndev
->initial_num_credits
=
92 rsp_2
->initial_num_credits
;
95 ndev
->manufact_specific_info
=
96 __le32_to_cpu(rsp_2
->manufact_specific_info
);
98 atomic_set(&ndev
->credits_cnt
, ndev
->initial_num_credits
);
100 nfc_dbg("nfcc_features 0x%x",
101 ndev
->nfcc_features
);
102 nfc_dbg("num_supported_rf_interfaces %d",
103 ndev
->num_supported_rf_interfaces
);
104 nfc_dbg("supported_rf_interfaces[0] 0x%x",
105 ndev
->supported_rf_interfaces
[0]);
106 nfc_dbg("supported_rf_interfaces[1] 0x%x",
107 ndev
->supported_rf_interfaces
[1]);
108 nfc_dbg("supported_rf_interfaces[2] 0x%x",
109 ndev
->supported_rf_interfaces
[2]);
110 nfc_dbg("supported_rf_interfaces[3] 0x%x",
111 ndev
->supported_rf_interfaces
[3]);
112 nfc_dbg("max_logical_connections %d",
113 ndev
->max_logical_connections
);
114 nfc_dbg("max_routing_table_size %d",
115 ndev
->max_routing_table_size
);
116 nfc_dbg("max_ctrl_pkt_payload_len %d",
117 ndev
->max_ctrl_pkt_payload_len
);
118 nfc_dbg("max_size_for_large_params %d",
119 ndev
->max_size_for_large_params
);
120 nfc_dbg("max_data_pkt_payload_size %d",
121 ndev
->max_data_pkt_payload_size
);
122 nfc_dbg("initial_num_credits %d",
123 ndev
->initial_num_credits
);
124 nfc_dbg("manufact_id 0x%x",
126 nfc_dbg("manufact_specific_info 0x%x",
127 ndev
->manufact_specific_info
);
130 nci_req_complete(ndev
, rsp_1
->status
);
133 static void nci_rf_disc_map_rsp_packet(struct nci_dev
*ndev
,
136 __u8 status
= skb
->data
[0];
138 nfc_dbg("entry, status 0x%x", status
);
140 nci_req_complete(ndev
, status
);
143 static void nci_rf_disc_rsp_packet(struct nci_dev
*ndev
, struct sk_buff
*skb
)
145 __u8 status
= skb
->data
[0];
147 nfc_dbg("entry, status 0x%x", status
);
149 if (status
== NCI_STATUS_OK
)
150 set_bit(NCI_DISCOVERY
, &ndev
->flags
);
152 nci_req_complete(ndev
, status
);
155 static void nci_rf_deactivate_rsp_packet(struct nci_dev
*ndev
,
158 __u8 status
= skb
->data
[0];
160 nfc_dbg("entry, status 0x%x", status
);
162 clear_bit(NCI_DISCOVERY
, &ndev
->flags
);
164 nci_req_complete(ndev
, status
);
167 void nci_rsp_packet(struct nci_dev
*ndev
, struct sk_buff
*skb
)
169 __u16 rsp_opcode
= nci_opcode(skb
->data
);
171 /* we got a rsp, stop the cmd timer */
172 del_timer(&ndev
->cmd_timer
);
174 nfc_dbg("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d",
176 nci_opcode_gid(rsp_opcode
),
177 nci_opcode_oid(rsp_opcode
),
178 nci_plen(skb
->data
));
180 /* strip the nci control header */
181 skb_pull(skb
, NCI_CTRL_HDR_SIZE
);
183 switch (rsp_opcode
) {
184 case NCI_OP_CORE_RESET_RSP
:
185 nci_core_reset_rsp_packet(ndev
, skb
);
188 case NCI_OP_CORE_INIT_RSP
:
189 nci_core_init_rsp_packet(ndev
, skb
);
192 case NCI_OP_RF_DISCOVER_MAP_RSP
:
193 nci_rf_disc_map_rsp_packet(ndev
, skb
);
196 case NCI_OP_RF_DISCOVER_RSP
:
197 nci_rf_disc_rsp_packet(ndev
, skb
);
200 case NCI_OP_RF_DEACTIVATE_RSP
:
201 nci_rf_deactivate_rsp_packet(ndev
, skb
);
205 pr_err("unknown rsp opcode 0x%x\n", rsp_opcode
);
211 /* trigger the next cmd */
212 atomic_set(&ndev
->cmd_cnt
, 1);
213 if (!skb_queue_empty(&ndev
->cmd_q
))
214 queue_work(ndev
->cmd_wq
, &ndev
->cmd_work
);