Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 1 | /* |
| 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 Ortiz | 52858b5 | 2011-12-14 16:43:05 +0100 | [diff] [blame] | 28 | #define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__ |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 29 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 30 | #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 | |
| 41 | static 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 Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 45 | pr_debug("status 0x%x\n", rsp->status); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 46 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 47 | if (rsp->status == NCI_STATUS_OK) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 48 | ndev->nci_ver = rsp->nci_ver; |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 49 | pr_debug("nci_ver 0x%x, config_status 0x%x\n", |
| 50 | rsp->nci_ver, rsp->config_status); |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 51 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 52 | |
| 53 | nci_req_complete(ndev, rsp->status); |
| 54 | } |
| 55 | |
| 56 | static 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 Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 61 | pr_debug("status 0x%x\n", rsp_1->status); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 62 | |
| 63 | if (rsp_1->status != NCI_STATUS_OK) |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 64 | goto exit; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 65 | |
| 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 > |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame^] | 70 | NCI_MAX_SUPPORTED_RF_INTERFACES) { |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 71 | ndev->num_supported_rf_interfaces = |
| 72 | NCI_MAX_SUPPORTED_RF_INTERFACES; |
| 73 | } |
| 74 | |
| 75 | memcpy(ndev->supported_rf_interfaces, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame^] | 76 | rsp_1->supported_rf_interfaces, |
| 77 | ndev->num_supported_rf_interfaces); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 78 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 79 | rsp_2 = (void *) (skb->data + 6 + rsp_1->num_supported_rf_interfaces); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 80 | |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame^] | 81 | ndev->max_logical_connections = rsp_2->max_logical_connections; |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 82 | ndev->max_routing_table_size = |
| 83 | __le16_to_cpu(rsp_2->max_routing_table_size); |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 84 | ndev->max_ctrl_pkt_payload_len = |
| 85 | rsp_2->max_ctrl_pkt_payload_len; |
| 86 | ndev->max_size_for_large_params = |
| 87 | __le16_to_cpu(rsp_2->max_size_for_large_params); |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 88 | ndev->manufact_id = |
| 89 | rsp_2->manufact_id; |
| 90 | ndev->manufact_specific_info = |
| 91 | __le32_to_cpu(rsp_2->manufact_specific_info); |
| 92 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 93 | pr_debug("nfcc_features 0x%x\n", |
| 94 | ndev->nfcc_features); |
| 95 | pr_debug("num_supported_rf_interfaces %d\n", |
| 96 | ndev->num_supported_rf_interfaces); |
| 97 | pr_debug("supported_rf_interfaces[0] 0x%x\n", |
| 98 | ndev->supported_rf_interfaces[0]); |
| 99 | pr_debug("supported_rf_interfaces[1] 0x%x\n", |
| 100 | ndev->supported_rf_interfaces[1]); |
| 101 | pr_debug("supported_rf_interfaces[2] 0x%x\n", |
| 102 | ndev->supported_rf_interfaces[2]); |
| 103 | pr_debug("supported_rf_interfaces[3] 0x%x\n", |
| 104 | ndev->supported_rf_interfaces[3]); |
| 105 | pr_debug("max_logical_connections %d\n", |
| 106 | ndev->max_logical_connections); |
| 107 | pr_debug("max_routing_table_size %d\n", |
| 108 | ndev->max_routing_table_size); |
| 109 | pr_debug("max_ctrl_pkt_payload_len %d\n", |
| 110 | ndev->max_ctrl_pkt_payload_len); |
| 111 | pr_debug("max_size_for_large_params %d\n", |
| 112 | ndev->max_size_for_large_params); |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 113 | pr_debug("manufact_id 0x%x\n", |
| 114 | ndev->manufact_id); |
| 115 | pr_debug("manufact_specific_info 0x%x\n", |
| 116 | ndev->manufact_specific_info); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 117 | |
Ilan Elias | e8c0dac | 2011-11-09 12:09:14 +0200 | [diff] [blame] | 118 | exit: |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 119 | nci_req_complete(ndev, rsp_1->status); |
| 120 | } |
| 121 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 122 | static void nci_rf_disc_map_rsp_packet(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame^] | 123 | struct sk_buff *skb) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 124 | { |
| 125 | __u8 status = skb->data[0]; |
| 126 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 127 | pr_debug("status 0x%x\n", status); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 128 | |
| 129 | nci_req_complete(ndev, status); |
| 130 | } |
| 131 | |
| 132 | static void nci_rf_disc_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb) |
| 133 | { |
| 134 | __u8 status = skb->data[0]; |
| 135 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 136 | pr_debug("status 0x%x\n", status); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 137 | |
| 138 | if (status == NCI_STATUS_OK) |
Ilan Elias | 8939e47 | 2012-01-18 13:16:12 +0200 | [diff] [blame] | 139 | atomic_set(&ndev->state, NCI_DISCOVERY); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 140 | |
| 141 | nci_req_complete(ndev, status); |
| 142 | } |
| 143 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 144 | static void nci_rf_disc_select_rsp_packet(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame^] | 145 | struct sk_buff *skb) |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 146 | { |
| 147 | __u8 status = skb->data[0]; |
| 148 | |
| 149 | pr_debug("status 0x%x\n", status); |
| 150 | |
| 151 | /* Complete the request on intf_activated_ntf or generic_error_ntf */ |
| 152 | if (status != NCI_STATUS_OK) |
| 153 | nci_req_complete(ndev, status); |
| 154 | } |
| 155 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 156 | static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev, |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame^] | 157 | struct sk_buff *skb) |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 158 | { |
| 159 | __u8 status = skb->data[0]; |
| 160 | |
Joe Perches | 24bf330 | 2011-11-29 11:37:35 -0800 | [diff] [blame] | 161 | pr_debug("status 0x%x\n", status); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 162 | |
Ilan Elias | bd7e01b | 2012-01-08 11:21:53 +0200 | [diff] [blame] | 163 | /* If target was active, complete the request only in deactivate_ntf */ |
| 164 | if ((status != NCI_STATUS_OK) || |
Samuel Ortiz | eb9bc6e | 2012-03-05 01:03:54 +0100 | [diff] [blame^] | 165 | (atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) { |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 166 | nci_clear_target_list(ndev); |
Ilan Elias | 8939e47 | 2012-01-18 13:16:12 +0200 | [diff] [blame] | 167 | atomic_set(&ndev->state, NCI_IDLE); |
Ilan Elias | bd7e01b | 2012-01-08 11:21:53 +0200 | [diff] [blame] | 168 | nci_req_complete(ndev, status); |
Ilan Elias | 8939e47 | 2012-01-18 13:16:12 +0200 | [diff] [blame] | 169 | } |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb) |
| 173 | { |
| 174 | __u16 rsp_opcode = nci_opcode(skb->data); |
| 175 | |
| 176 | /* we got a rsp, stop the cmd timer */ |
| 177 | del_timer(&ndev->cmd_timer); |
| 178 | |
Joe Perches | 20c239c | 2011-11-29 11:37:33 -0800 | [diff] [blame] | 179 | pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n", |
| 180 | nci_pbf(skb->data), |
| 181 | nci_opcode_gid(rsp_opcode), |
| 182 | nci_opcode_oid(rsp_opcode), |
| 183 | nci_plen(skb->data)); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 184 | |
| 185 | /* strip the nci control header */ |
| 186 | skb_pull(skb, NCI_CTRL_HDR_SIZE); |
| 187 | |
| 188 | switch (rsp_opcode) { |
| 189 | case NCI_OP_CORE_RESET_RSP: |
| 190 | nci_core_reset_rsp_packet(ndev, skb); |
| 191 | break; |
| 192 | |
| 193 | case NCI_OP_CORE_INIT_RSP: |
| 194 | nci_core_init_rsp_packet(ndev, skb); |
| 195 | break; |
| 196 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 197 | case NCI_OP_RF_DISCOVER_MAP_RSP: |
| 198 | nci_rf_disc_map_rsp_packet(ndev, skb); |
| 199 | break; |
| 200 | |
| 201 | case NCI_OP_RF_DISCOVER_RSP: |
| 202 | nci_rf_disc_rsp_packet(ndev, skb); |
| 203 | break; |
| 204 | |
Ilan Elias | 019c4fb | 2012-01-18 13:16:14 +0200 | [diff] [blame] | 205 | case NCI_OP_RF_DISCOVER_SELECT_RSP: |
| 206 | nci_rf_disc_select_rsp_packet(ndev, skb); |
| 207 | break; |
| 208 | |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 209 | case NCI_OP_RF_DEACTIVATE_RSP: |
| 210 | nci_rf_deactivate_rsp_packet(ndev, skb); |
| 211 | break; |
| 212 | |
| 213 | default: |
Joe Perches | ed1e0ad | 2011-11-29 11:37:32 -0800 | [diff] [blame] | 214 | pr_err("unknown rsp opcode 0x%x\n", rsp_opcode); |
Ilan Elias | 6a2968a | 2011-09-18 11:19:35 +0300 | [diff] [blame] | 215 | break; |
| 216 | } |
| 217 | |
| 218 | kfree_skb(skb); |
| 219 | |
| 220 | /* trigger the next cmd */ |
| 221 | atomic_set(&ndev->cmd_cnt, 1); |
| 222 | if (!skb_queue_empty(&ndev->cmd_q)) |
| 223 | queue_work(ndev->cmd_wq, &ndev->cmd_work); |
| 224 | } |