blob: aa63b1e99188672e799c428c814ac26a289118ad [file] [log] [blame]
Ilan Elias6a2968a2011-09-18 11:19:35 +03001/*
2 * The NFC Controller Interface is the communication protocol between an
3 * NFC Controller (NFCC) and a Device Host (DH).
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 *
7 * Written by Ilan Elias <ilane@ti.com>
8 *
9 * Acknowledgements:
10 * This file is based on hci_event.c, which was written
11 * by Maxim Krasnyansky.
12 *
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
16 *
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.
21 *
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
25 *
26 */
27
Samuel Ortiz52858b52011-12-14 16:43:05 +010028#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
Joe Perchesed1e0ad2011-11-29 11:37:32 -080029
Ilan Elias6a2968a2011-09-18 11:19:35 +030030#include <linux/types.h>
31#include <linux/interrupt.h>
32#include <linux/bitops.h>
33#include <linux/skbuff.h>
34
35#include "../nfc.h"
36#include <net/nfc/nci.h>
37#include <net/nfc/nci_core.h>
38
39/* Handle NCI Response packets */
40
41static void nci_core_reset_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
42{
43 struct nci_core_reset_rsp *rsp = (void *) skb->data;
44
Joe Perches24bf3302011-11-29 11:37:35 -080045 pr_debug("status 0x%x\n", rsp->status);
Ilan Elias6a2968a2011-09-18 11:19:35 +030046
Ilan Eliase8c0dac2011-11-09 12:09:14 +020047 if (rsp->status == NCI_STATUS_OK) {
Ilan Elias6a2968a2011-09-18 11:19:35 +030048 ndev->nci_ver = rsp->nci_ver;
Joe Perches20c239c2011-11-29 11:37:33 -080049 pr_debug("nci_ver 0x%x, config_status 0x%x\n",
50 rsp->nci_ver, rsp->config_status);
Ilan Eliase8c0dac2011-11-09 12:09:14 +020051 }
Ilan Elias6a2968a2011-09-18 11:19:35 +030052
53 nci_req_complete(ndev, rsp->status);
54}
55
56static void nci_core_init_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
57{
58 struct nci_core_init_rsp_1 *rsp_1 = (void *) skb->data;
59 struct nci_core_init_rsp_2 *rsp_2;
60
Joe Perches24bf3302011-11-29 11:37:35 -080061 pr_debug("status 0x%x\n", rsp_1->status);
Ilan Elias6a2968a2011-09-18 11:19:35 +030062
63 if (rsp_1->status != NCI_STATUS_OK)
Ilan Eliase8c0dac2011-11-09 12:09:14 +020064 goto exit;
Ilan Elias6a2968a2011-09-18 11:19:35 +030065
66 ndev->nfcc_features = __le32_to_cpu(rsp_1->nfcc_features);
67 ndev->num_supported_rf_interfaces = rsp_1->num_supported_rf_interfaces;
68
69 if (ndev->num_supported_rf_interfaces >
Ilan Eliase8c0dac2011-11-09 12:09:14 +020070 NCI_MAX_SUPPORTED_RF_INTERFACES) {
Ilan Elias6a2968a2011-09-18 11:19:35 +030071 ndev->num_supported_rf_interfaces =
72 NCI_MAX_SUPPORTED_RF_INTERFACES;
73 }
74
75 memcpy(ndev->supported_rf_interfaces,
76 rsp_1->supported_rf_interfaces,
77 ndev->num_supported_rf_interfaces);
78
Ilan Eliase8c0dac2011-11-09 12:09:14 +020079 rsp_2 = (void *) (skb->data + 6 + rsp_1->num_supported_rf_interfaces);
Ilan Elias6a2968a2011-09-18 11:19:35 +030080
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);
Ilan Eliase8c0dac2011-11-09 12:09:14 +020085 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);
Ilan Eliase8c0dac2011-11-09 12:09:14 +020089 ndev->manufact_id =
90 rsp_2->manufact_id;
91 ndev->manufact_specific_info =
92 __le32_to_cpu(rsp_2->manufact_specific_info);
93
Joe Perches20c239c2011-11-29 11:37:33 -080094 pr_debug("nfcc_features 0x%x\n",
95 ndev->nfcc_features);
96 pr_debug("num_supported_rf_interfaces %d\n",
97 ndev->num_supported_rf_interfaces);
98 pr_debug("supported_rf_interfaces[0] 0x%x\n",
99 ndev->supported_rf_interfaces[0]);
100 pr_debug("supported_rf_interfaces[1] 0x%x\n",
101 ndev->supported_rf_interfaces[1]);
102 pr_debug("supported_rf_interfaces[2] 0x%x\n",
103 ndev->supported_rf_interfaces[2]);
104 pr_debug("supported_rf_interfaces[3] 0x%x\n",
105 ndev->supported_rf_interfaces[3]);
106 pr_debug("max_logical_connections %d\n",
107 ndev->max_logical_connections);
108 pr_debug("max_routing_table_size %d\n",
109 ndev->max_routing_table_size);
110 pr_debug("max_ctrl_pkt_payload_len %d\n",
111 ndev->max_ctrl_pkt_payload_len);
112 pr_debug("max_size_for_large_params %d\n",
113 ndev->max_size_for_large_params);
Joe Perches20c239c2011-11-29 11:37:33 -0800114 pr_debug("manufact_id 0x%x\n",
115 ndev->manufact_id);
116 pr_debug("manufact_specific_info 0x%x\n",
117 ndev->manufact_specific_info);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300118
Ilan Eliase8c0dac2011-11-09 12:09:14 +0200119exit:
Ilan Elias6a2968a2011-09-18 11:19:35 +0300120 nci_req_complete(ndev, rsp_1->status);
121}
122
Ilan Elias6a2968a2011-09-18 11:19:35 +0300123static void nci_rf_disc_map_rsp_packet(struct nci_dev *ndev,
124 struct sk_buff *skb)
125{
126 __u8 status = skb->data[0];
127
Joe Perches24bf3302011-11-29 11:37:35 -0800128 pr_debug("status 0x%x\n", status);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300129
130 nci_req_complete(ndev, status);
131}
132
133static void nci_rf_disc_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
134{
135 __u8 status = skb->data[0];
136
Joe Perches24bf3302011-11-29 11:37:35 -0800137 pr_debug("status 0x%x\n", status);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300138
139 if (status == NCI_STATUS_OK)
Ilan Elias8939e472012-01-18 13:16:12 +0200140 atomic_set(&ndev->state, NCI_DISCOVERY);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300141
142 nci_req_complete(ndev, status);
143}
144
Ilan Elias019c4fb2012-01-18 13:16:14 +0200145static void nci_rf_disc_select_rsp_packet(struct nci_dev *ndev,
146 struct sk_buff *skb)
147{
148 __u8 status = skb->data[0];
149
150 pr_debug("status 0x%x\n", status);
151
152 /* Complete the request on intf_activated_ntf or generic_error_ntf */
153 if (status != NCI_STATUS_OK)
154 nci_req_complete(ndev, status);
155}
156
Ilan Elias6a2968a2011-09-18 11:19:35 +0300157static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev,
158 struct sk_buff *skb)
159{
160 __u8 status = skb->data[0];
161
Joe Perches24bf3302011-11-29 11:37:35 -0800162 pr_debug("status 0x%x\n", status);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300163
Ilan Eliasbd7e01b2012-01-08 11:21:53 +0200164 /* If target was active, complete the request only in deactivate_ntf */
165 if ((status != NCI_STATUS_OK) ||
Ilan Elias8939e472012-01-18 13:16:12 +0200166 (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
Ilan Elias019c4fb2012-01-18 13:16:14 +0200167 nci_clear_target_list(ndev);
Ilan Elias8939e472012-01-18 13:16:12 +0200168 atomic_set(&ndev->state, NCI_IDLE);
Ilan Eliasbd7e01b2012-01-08 11:21:53 +0200169 nci_req_complete(ndev, status);
Ilan Elias8939e472012-01-18 13:16:12 +0200170 }
Ilan Elias6a2968a2011-09-18 11:19:35 +0300171}
172
173void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
174{
175 __u16 rsp_opcode = nci_opcode(skb->data);
176
177 /* we got a rsp, stop the cmd timer */
178 del_timer(&ndev->cmd_timer);
179
Joe Perches20c239c2011-11-29 11:37:33 -0800180 pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
181 nci_pbf(skb->data),
182 nci_opcode_gid(rsp_opcode),
183 nci_opcode_oid(rsp_opcode),
184 nci_plen(skb->data));
Ilan Elias6a2968a2011-09-18 11:19:35 +0300185
186 /* strip the nci control header */
187 skb_pull(skb, NCI_CTRL_HDR_SIZE);
188
189 switch (rsp_opcode) {
190 case NCI_OP_CORE_RESET_RSP:
191 nci_core_reset_rsp_packet(ndev, skb);
192 break;
193
194 case NCI_OP_CORE_INIT_RSP:
195 nci_core_init_rsp_packet(ndev, skb);
196 break;
197
Ilan Elias6a2968a2011-09-18 11:19:35 +0300198 case NCI_OP_RF_DISCOVER_MAP_RSP:
199 nci_rf_disc_map_rsp_packet(ndev, skb);
200 break;
201
202 case NCI_OP_RF_DISCOVER_RSP:
203 nci_rf_disc_rsp_packet(ndev, skb);
204 break;
205
Ilan Elias019c4fb2012-01-18 13:16:14 +0200206 case NCI_OP_RF_DISCOVER_SELECT_RSP:
207 nci_rf_disc_select_rsp_packet(ndev, skb);
208 break;
209
Ilan Elias6a2968a2011-09-18 11:19:35 +0300210 case NCI_OP_RF_DEACTIVATE_RSP:
211 nci_rf_deactivate_rsp_packet(ndev, skb);
212 break;
213
214 default:
Joe Perchesed1e0ad2011-11-29 11:37:32 -0800215 pr_err("unknown rsp opcode 0x%x\n", rsp_opcode);
Ilan Elias6a2968a2011-09-18 11:19:35 +0300216 break;
217 }
218
219 kfree_skb(skb);
220
221 /* trigger the next cmd */
222 atomic_set(&ndev->cmd_cnt, 1);
223 if (!skb_queue_empty(&ndev->cmd_q))
224 queue_work(ndev->cmd_wq, &ndev->cmd_work);
225}