Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * The NFC Controller Interface is the communication protocol between an |
| 3 | * NFC Controller (NFCC) and a Device Host (DH). |
| 4 | * This is the HCI over NCI implementation, as specified in the 10.2 |
| 5 | * section of the NCI 1.1 specification. |
| 6 | * |
| 7 | * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 |
| 11 | * as published by the Free Software Foundation |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <linux/skbuff.h> |
| 24 | |
| 25 | #include "../nfc.h" |
| 26 | #include <net/nfc/nci.h> |
| 27 | #include <net/nfc/nci_core.h> |
| 28 | #include <linux/nfc.h> |
| 29 | |
| 30 | struct nci_data { |
| 31 | u8 conn_id; |
| 32 | u8 pipe; |
| 33 | u8 cmd; |
| 34 | const u8 *data; |
| 35 | u32 data_len; |
| 36 | } __packed; |
| 37 | |
| 38 | struct nci_hci_create_pipe_params { |
| 39 | u8 src_gate; |
| 40 | u8 dest_host; |
| 41 | u8 dest_gate; |
| 42 | } __packed; |
| 43 | |
| 44 | struct nci_hci_create_pipe_resp { |
| 45 | u8 src_host; |
| 46 | u8 src_gate; |
| 47 | u8 dest_host; |
| 48 | u8 dest_gate; |
| 49 | u8 pipe; |
| 50 | } __packed; |
| 51 | |
| 52 | struct nci_hci_delete_pipe_noti { |
| 53 | u8 pipe; |
| 54 | } __packed; |
| 55 | |
| 56 | struct nci_hci_all_pipe_cleared_noti { |
| 57 | u8 host; |
| 58 | } __packed; |
| 59 | |
| 60 | struct nci_hcp_message { |
| 61 | u8 header; /* type -cmd,evt,rsp- + instruction */ |
| 62 | u8 data[]; |
| 63 | } __packed; |
| 64 | |
| 65 | struct nci_hcp_packet { |
| 66 | u8 header; /* cbit+pipe */ |
| 67 | struct nci_hcp_message message; |
| 68 | } __packed; |
| 69 | |
| 70 | #define NCI_HCI_ANY_SET_PARAMETER 0x01 |
| 71 | #define NCI_HCI_ANY_GET_PARAMETER 0x02 |
| 72 | #define NCI_HCI_ANY_CLOSE_PIPE 0x04 |
Christophe Ricard | fa6fbad | 2015-10-25 22:54:23 +0100 | [diff] [blame] | 73 | #define NCI_HCI_ADM_CLEAR_ALL_PIPE 0x14 |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 74 | |
| 75 | #define NCI_HFP_NO_CHAINING 0x80 |
| 76 | |
| 77 | #define NCI_NFCEE_ID_HCI 0x80 |
| 78 | |
| 79 | #define NCI_EVT_HOT_PLUG 0x03 |
| 80 | |
| 81 | #define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY 0x01 |
| 82 | |
| 83 | /* HCP headers */ |
| 84 | #define NCI_HCI_HCP_PACKET_HEADER_LEN 1 |
| 85 | #define NCI_HCI_HCP_MESSAGE_HEADER_LEN 1 |
| 86 | #define NCI_HCI_HCP_HEADER_LEN 2 |
| 87 | |
| 88 | /* HCP types */ |
| 89 | #define NCI_HCI_HCP_COMMAND 0x00 |
| 90 | #define NCI_HCI_HCP_EVENT 0x01 |
| 91 | #define NCI_HCI_HCP_RESPONSE 0x02 |
| 92 | |
| 93 | #define NCI_HCI_ADM_NOTIFY_PIPE_CREATED 0x12 |
| 94 | #define NCI_HCI_ADM_NOTIFY_PIPE_DELETED 0x13 |
| 95 | #define NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED 0x15 |
| 96 | |
| 97 | #define NCI_HCI_FRAGMENT 0x7f |
| 98 | #define NCI_HCP_HEADER(type, instr) ((((type) & 0x03) << 6) |\ |
| 99 | ((instr) & 0x3f)) |
| 100 | |
| 101 | #define NCI_HCP_MSG_GET_TYPE(header) ((header & 0xc0) >> 6) |
| 102 | #define NCI_HCP_MSG_GET_CMD(header) (header & 0x3f) |
| 103 | #define NCI_HCP_MSG_GET_PIPE(header) (header & 0x7f) |
| 104 | |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 105 | static int nci_hci_result_to_errno(u8 result) |
| 106 | { |
| 107 | switch (result) { |
| 108 | case NCI_HCI_ANY_OK: |
| 109 | return 0; |
| 110 | case NCI_HCI_ANY_E_REG_PAR_UNKNOWN: |
| 111 | return -EOPNOTSUPP; |
| 112 | case NCI_HCI_ANY_E_TIMEOUT: |
| 113 | return -ETIME; |
| 114 | default: |
| 115 | return -1; |
| 116 | } |
| 117 | } |
| 118 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 119 | /* HCI core */ |
| 120 | static void nci_hci_reset_pipes(struct nci_hci_dev *hdev) |
| 121 | { |
| 122 | int i; |
| 123 | |
| 124 | for (i = 0; i < NCI_HCI_MAX_PIPES; i++) { |
| 125 | hdev->pipes[i].gate = NCI_HCI_INVALID_GATE; |
| 126 | hdev->pipes[i].host = NCI_HCI_INVALID_HOST; |
| 127 | } |
| 128 | memset(hdev->gate2pipe, NCI_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe)); |
| 129 | } |
| 130 | |
| 131 | static void nci_hci_reset_pipes_per_host(struct nci_dev *ndev, u8 host) |
| 132 | { |
| 133 | int i; |
| 134 | |
| 135 | for (i = 0; i < NCI_HCI_MAX_PIPES; i++) { |
| 136 | if (ndev->hci_dev->pipes[i].host == host) { |
| 137 | ndev->hci_dev->pipes[i].gate = NCI_HCI_INVALID_GATE; |
| 138 | ndev->hci_dev->pipes[i].host = NCI_HCI_INVALID_HOST; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* Fragment HCI data over NCI packet. |
| 144 | * NFC Forum NCI 10.2.2 Data Exchange: |
| 145 | * The payload of the Data Packets sent on the Logical Connection SHALL be |
| 146 | * valid HCP packets, as defined within [ETSI_102622]. Each Data Packet SHALL |
| 147 | * contain a single HCP packet. NCI Segmentation and Reassembly SHALL NOT be |
| 148 | * applied to Data Messages in either direction. The HCI fragmentation mechanism |
| 149 | * is used if required. |
| 150 | */ |
| 151 | static int nci_hci_send_data(struct nci_dev *ndev, u8 pipe, |
| 152 | const u8 data_type, const u8 *data, |
| 153 | size_t data_len) |
| 154 | { |
| 155 | struct nci_conn_info *conn_info; |
| 156 | struct sk_buff *skb; |
| 157 | int len, i, r; |
| 158 | u8 cb = pipe; |
| 159 | |
| 160 | conn_info = ndev->hci_dev->conn_info; |
| 161 | if (!conn_info) |
| 162 | return -EPROTO; |
| 163 | |
Christophe Ricard | 500c4ef | 2015-10-25 22:54:20 +0100 | [diff] [blame] | 164 | i = 0; |
| 165 | skb = nci_skb_alloc(ndev, conn_info->max_pkt_payload_len + |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 166 | NCI_DATA_HDR_SIZE, GFP_KERNEL); |
| 167 | if (!skb) |
| 168 | return -ENOMEM; |
| 169 | |
Christophe Ricard | 500c4ef | 2015-10-25 22:54:20 +0100 | [diff] [blame] | 170 | skb_reserve(skb, NCI_DATA_HDR_SIZE + 2); |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 171 | *skb_push(skb, 1) = data_type; |
| 172 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 173 | do { |
Christophe Ricard | 500c4ef | 2015-10-25 22:54:20 +0100 | [diff] [blame] | 174 | len = conn_info->max_pkt_payload_len; |
| 175 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 176 | /* If last packet add NCI_HFP_NO_CHAINING */ |
| 177 | if (i + conn_info->max_pkt_payload_len - |
| 178 | (skb->len + 1) >= data_len) { |
| 179 | cb |= NCI_HFP_NO_CHAINING; |
| 180 | len = data_len - i; |
| 181 | } else { |
| 182 | len = conn_info->max_pkt_payload_len - skb->len - 1; |
| 183 | } |
| 184 | |
| 185 | *skb_push(skb, 1) = cb; |
| 186 | |
| 187 | if (len > 0) |
| 188 | memcpy(skb_put(skb, len), data + i, len); |
| 189 | |
| 190 | r = nci_send_data(ndev, conn_info->conn_id, skb); |
| 191 | if (r < 0) |
| 192 | return r; |
| 193 | |
| 194 | i += len; |
Christophe Ricard | 500c4ef | 2015-10-25 22:54:20 +0100 | [diff] [blame] | 195 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 196 | if (i < data_len) { |
Christophe Ricard | 500c4ef | 2015-10-25 22:54:20 +0100 | [diff] [blame] | 197 | skb = nci_skb_alloc(ndev, |
| 198 | conn_info->max_pkt_payload_len + |
| 199 | NCI_DATA_HDR_SIZE, GFP_KERNEL); |
| 200 | if (!skb) |
| 201 | return -ENOMEM; |
| 202 | |
| 203 | skb_reserve(skb, NCI_DATA_HDR_SIZE + 1); |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 204 | } |
| 205 | } while (i < data_len); |
| 206 | |
| 207 | return i; |
| 208 | } |
| 209 | |
| 210 | static void nci_hci_send_data_req(struct nci_dev *ndev, unsigned long opt) |
| 211 | { |
| 212 | struct nci_data *data = (struct nci_data *)opt; |
| 213 | |
| 214 | nci_hci_send_data(ndev, data->pipe, data->cmd, |
| 215 | data->data, data->data_len); |
| 216 | } |
| 217 | |
| 218 | int nci_hci_send_event(struct nci_dev *ndev, u8 gate, u8 event, |
| 219 | const u8 *param, size_t param_len) |
| 220 | { |
| 221 | u8 pipe = ndev->hci_dev->gate2pipe[gate]; |
| 222 | |
| 223 | if (pipe == NCI_HCI_INVALID_PIPE) |
| 224 | return -EADDRNOTAVAIL; |
| 225 | |
| 226 | return nci_hci_send_data(ndev, pipe, |
| 227 | NCI_HCP_HEADER(NCI_HCI_HCP_EVENT, event), |
| 228 | param, param_len); |
| 229 | } |
| 230 | EXPORT_SYMBOL(nci_hci_send_event); |
| 231 | |
| 232 | int nci_hci_send_cmd(struct nci_dev *ndev, u8 gate, u8 cmd, |
| 233 | const u8 *param, size_t param_len, |
| 234 | struct sk_buff **skb) |
| 235 | { |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 236 | struct nci_hcp_message *message; |
| 237 | struct nci_conn_info *conn_info; |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 238 | struct nci_data data; |
| 239 | int r; |
| 240 | u8 pipe = ndev->hci_dev->gate2pipe[gate]; |
| 241 | |
| 242 | if (pipe == NCI_HCI_INVALID_PIPE) |
| 243 | return -EADDRNOTAVAIL; |
| 244 | |
| 245 | conn_info = ndev->hci_dev->conn_info; |
| 246 | if (!conn_info) |
| 247 | return -EPROTO; |
| 248 | |
| 249 | data.conn_id = conn_info->conn_id; |
| 250 | data.pipe = pipe; |
| 251 | data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND, cmd); |
| 252 | data.data = param; |
| 253 | data.data_len = param_len; |
| 254 | |
| 255 | r = nci_request(ndev, nci_hci_send_data_req, (unsigned long)&data, |
| 256 | msecs_to_jiffies(NCI_DATA_TIMEOUT)); |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 257 | if (r == NCI_STATUS_OK) { |
| 258 | message = (struct nci_hcp_message *)conn_info->rx_skb->data; |
| 259 | r = nci_hci_result_to_errno( |
| 260 | NCI_HCP_MSG_GET_CMD(message->header)); |
| 261 | skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN); |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 262 | |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 263 | if (!r && skb) |
| 264 | *skb = conn_info->rx_skb; |
| 265 | } |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 266 | |
| 267 | return r; |
| 268 | } |
| 269 | EXPORT_SYMBOL(nci_hci_send_cmd); |
| 270 | |
Christophe Ricard | fa6fbad | 2015-10-25 22:54:23 +0100 | [diff] [blame] | 271 | int nci_hci_clear_all_pipes(struct nci_dev *ndev) |
| 272 | { |
| 273 | int r; |
| 274 | |
| 275 | r = nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE, |
| 276 | NCI_HCI_ADM_CLEAR_ALL_PIPE, NULL, 0, NULL); |
| 277 | if (r < 0) |
| 278 | return r; |
| 279 | |
| 280 | nci_hci_reset_pipes(ndev->hci_dev); |
| 281 | return r; |
| 282 | } |
| 283 | EXPORT_SYMBOL(nci_hci_clear_all_pipes); |
| 284 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 285 | static void nci_hci_event_received(struct nci_dev *ndev, u8 pipe, |
| 286 | u8 event, struct sk_buff *skb) |
| 287 | { |
| 288 | if (ndev->ops->hci_event_received) |
| 289 | ndev->ops->hci_event_received(ndev, pipe, event, skb); |
| 290 | } |
| 291 | |
| 292 | static void nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe, |
| 293 | u8 cmd, struct sk_buff *skb) |
| 294 | { |
| 295 | u8 gate = ndev->hci_dev->pipes[pipe].gate; |
| 296 | u8 status = NCI_HCI_ANY_OK | ~NCI_HCI_FRAGMENT; |
| 297 | u8 dest_gate, new_pipe; |
| 298 | struct nci_hci_create_pipe_resp *create_info; |
| 299 | struct nci_hci_delete_pipe_noti *delete_info; |
| 300 | struct nci_hci_all_pipe_cleared_noti *cleared_info; |
| 301 | |
| 302 | pr_debug("from gate %x pipe %x cmd %x\n", gate, pipe, cmd); |
| 303 | |
| 304 | switch (cmd) { |
| 305 | case NCI_HCI_ADM_NOTIFY_PIPE_CREATED: |
| 306 | if (skb->len != 5) { |
| 307 | status = NCI_HCI_ANY_E_NOK; |
| 308 | goto exit; |
| 309 | } |
| 310 | create_info = (struct nci_hci_create_pipe_resp *)skb->data; |
| 311 | dest_gate = create_info->dest_gate; |
| 312 | new_pipe = create_info->pipe; |
| 313 | |
| 314 | /* Save the new created pipe and bind with local gate, |
| 315 | * the description for skb->data[3] is destination gate id |
| 316 | * but since we received this cmd from host controller, we |
| 317 | * are the destination and it is our local gate |
| 318 | */ |
| 319 | ndev->hci_dev->gate2pipe[dest_gate] = new_pipe; |
| 320 | ndev->hci_dev->pipes[new_pipe].gate = dest_gate; |
| 321 | ndev->hci_dev->pipes[new_pipe].host = |
| 322 | create_info->src_host; |
| 323 | break; |
| 324 | case NCI_HCI_ANY_OPEN_PIPE: |
| 325 | /* If the pipe is not created report an error */ |
| 326 | if (gate == NCI_HCI_INVALID_GATE) { |
| 327 | status = NCI_HCI_ANY_E_NOK; |
| 328 | goto exit; |
| 329 | } |
| 330 | break; |
| 331 | case NCI_HCI_ADM_NOTIFY_PIPE_DELETED: |
| 332 | if (skb->len != 1) { |
| 333 | status = NCI_HCI_ANY_E_NOK; |
| 334 | goto exit; |
| 335 | } |
| 336 | delete_info = (struct nci_hci_delete_pipe_noti *)skb->data; |
| 337 | |
| 338 | ndev->hci_dev->pipes[delete_info->pipe].gate = |
| 339 | NCI_HCI_INVALID_GATE; |
| 340 | ndev->hci_dev->pipes[delete_info->pipe].host = |
| 341 | NCI_HCI_INVALID_HOST; |
| 342 | break; |
| 343 | case NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED: |
| 344 | if (skb->len != 1) { |
| 345 | status = NCI_HCI_ANY_E_NOK; |
| 346 | goto exit; |
| 347 | } |
| 348 | |
| 349 | cleared_info = |
| 350 | (struct nci_hci_all_pipe_cleared_noti *)skb->data; |
| 351 | nci_hci_reset_pipes_per_host(ndev, cleared_info->host); |
| 352 | break; |
| 353 | default: |
| 354 | pr_debug("Discarded unknown cmd %x to gate %x\n", cmd, gate); |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | if (ndev->ops->hci_cmd_received) |
| 359 | ndev->ops->hci_cmd_received(ndev, pipe, cmd, skb); |
| 360 | |
| 361 | exit: |
| 362 | nci_hci_send_data(ndev, pipe, status, NULL, 0); |
| 363 | |
| 364 | kfree_skb(skb); |
| 365 | } |
| 366 | |
| 367 | static void nci_hci_resp_received(struct nci_dev *ndev, u8 pipe, |
| 368 | u8 result, struct sk_buff *skb) |
| 369 | { |
| 370 | struct nci_conn_info *conn_info; |
| 371 | u8 status = result; |
| 372 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 373 | conn_info = ndev->hci_dev->conn_info; |
| 374 | if (!conn_info) { |
| 375 | status = NCI_STATUS_REJECTED; |
| 376 | goto exit; |
| 377 | } |
| 378 | |
| 379 | conn_info->rx_skb = skb; |
| 380 | |
| 381 | exit: |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 382 | nci_req_complete(ndev, NCI_STATUS_OK); |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* Receive hcp message for pipe, with type and cmd. |
| 386 | * skb contains optional message data only. |
| 387 | */ |
| 388 | static void nci_hci_hcp_message_rx(struct nci_dev *ndev, u8 pipe, |
| 389 | u8 type, u8 instruction, struct sk_buff *skb) |
| 390 | { |
| 391 | switch (type) { |
| 392 | case NCI_HCI_HCP_RESPONSE: |
| 393 | nci_hci_resp_received(ndev, pipe, instruction, skb); |
| 394 | break; |
| 395 | case NCI_HCI_HCP_COMMAND: |
| 396 | nci_hci_cmd_received(ndev, pipe, instruction, skb); |
| 397 | break; |
| 398 | case NCI_HCI_HCP_EVENT: |
| 399 | nci_hci_event_received(ndev, pipe, instruction, skb); |
| 400 | break; |
| 401 | default: |
| 402 | pr_err("UNKNOWN MSG Type %d, instruction=%d\n", |
| 403 | type, instruction); |
| 404 | kfree_skb(skb); |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | nci_req_complete(ndev, 0); |
| 409 | } |
| 410 | |
| 411 | static void nci_hci_msg_rx_work(struct work_struct *work) |
| 412 | { |
| 413 | struct nci_hci_dev *hdev = |
| 414 | container_of(work, struct nci_hci_dev, msg_rx_work); |
| 415 | struct sk_buff *skb; |
| 416 | struct nci_hcp_message *message; |
| 417 | u8 pipe, type, instruction; |
| 418 | |
| 419 | while ((skb = skb_dequeue(&hdev->msg_rx_queue)) != NULL) { |
Christophe Ricard | e65917b | 2015-10-25 22:54:22 +0100 | [diff] [blame] | 420 | pipe = NCI_HCP_MSG_GET_PIPE(skb->data[0]); |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 421 | skb_pull(skb, NCI_HCI_HCP_PACKET_HEADER_LEN); |
| 422 | message = (struct nci_hcp_message *)skb->data; |
| 423 | type = NCI_HCP_MSG_GET_TYPE(message->header); |
| 424 | instruction = NCI_HCP_MSG_GET_CMD(message->header); |
| 425 | skb_pull(skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN); |
| 426 | |
| 427 | nci_hci_hcp_message_rx(hdev->ndev, pipe, |
| 428 | type, instruction, skb); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void nci_hci_data_received_cb(void *context, |
| 433 | struct sk_buff *skb, int err) |
| 434 | { |
| 435 | struct nci_dev *ndev = (struct nci_dev *)context; |
| 436 | struct nci_hcp_packet *packet; |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 437 | u8 pipe, type; |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 438 | struct sk_buff *hcp_skb; |
| 439 | struct sk_buff *frag_skb; |
| 440 | int msg_len; |
| 441 | |
| 442 | pr_debug("\n"); |
| 443 | |
| 444 | if (err) { |
| 445 | nci_req_complete(ndev, err); |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | packet = (struct nci_hcp_packet *)skb->data; |
| 450 | if ((packet->header & ~NCI_HCI_FRAGMENT) == 0) { |
| 451 | skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | /* it's the last fragment. Does it need re-aggregation? */ |
| 456 | if (skb_queue_len(&ndev->hci_dev->rx_hcp_frags)) { |
Christophe Ricard | e65917b | 2015-10-25 22:54:22 +0100 | [diff] [blame] | 457 | pipe = NCI_HCP_MSG_GET_PIPE(packet->header); |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 458 | skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb); |
| 459 | |
| 460 | msg_len = 0; |
| 461 | skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) { |
| 462 | msg_len += (frag_skb->len - |
| 463 | NCI_HCI_HCP_PACKET_HEADER_LEN); |
| 464 | } |
| 465 | |
| 466 | hcp_skb = nfc_alloc_recv_skb(NCI_HCI_HCP_PACKET_HEADER_LEN + |
| 467 | msg_len, GFP_KERNEL); |
| 468 | if (!hcp_skb) { |
| 469 | nci_req_complete(ndev, -ENOMEM); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | *skb_put(hcp_skb, NCI_HCI_HCP_PACKET_HEADER_LEN) = pipe; |
| 474 | |
| 475 | skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) { |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 476 | msg_len = frag_skb->len - NCI_HCI_HCP_PACKET_HEADER_LEN; |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 477 | memcpy(skb_put(hcp_skb, msg_len), frag_skb->data + |
| 478 | NCI_HCI_HCP_PACKET_HEADER_LEN, msg_len); |
| 479 | } |
| 480 | |
| 481 | skb_queue_purge(&ndev->hci_dev->rx_hcp_frags); |
| 482 | } else { |
| 483 | packet->header &= NCI_HCI_FRAGMENT; |
| 484 | hcp_skb = skb; |
| 485 | } |
| 486 | |
| 487 | /* if this is a response, dispatch immediately to |
| 488 | * unblock waiting cmd context. Otherwise, enqueue to dispatch |
| 489 | * in separate context where handler can also execute command. |
| 490 | */ |
| 491 | packet = (struct nci_hcp_packet *)hcp_skb->data; |
| 492 | type = NCI_HCP_MSG_GET_TYPE(packet->message.header); |
| 493 | if (type == NCI_HCI_HCP_RESPONSE) { |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 494 | pipe = NCI_HCP_MSG_GET_PIPE(packet->header); |
| 495 | skb_pull(hcp_skb, NCI_HCI_HCP_PACKET_HEADER_LEN); |
| 496 | nci_hci_hcp_message_rx(ndev, pipe, type, |
| 497 | NCI_STATUS_OK, hcp_skb); |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 498 | } else { |
| 499 | skb_queue_tail(&ndev->hci_dev->msg_rx_queue, hcp_skb); |
| 500 | schedule_work(&ndev->hci_dev->msg_rx_work); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | int nci_hci_open_pipe(struct nci_dev *ndev, u8 pipe) |
| 505 | { |
| 506 | struct nci_data data; |
| 507 | struct nci_conn_info *conn_info; |
| 508 | |
| 509 | conn_info = ndev->hci_dev->conn_info; |
| 510 | if (!conn_info) |
| 511 | return -EPROTO; |
| 512 | |
| 513 | data.conn_id = conn_info->conn_id; |
| 514 | data.pipe = pipe; |
| 515 | data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND, |
| 516 | NCI_HCI_ANY_OPEN_PIPE); |
| 517 | data.data = NULL; |
| 518 | data.data_len = 0; |
| 519 | |
| 520 | return nci_request(ndev, nci_hci_send_data_req, |
| 521 | (unsigned long)&data, |
| 522 | msecs_to_jiffies(NCI_DATA_TIMEOUT)); |
| 523 | } |
| 524 | EXPORT_SYMBOL(nci_hci_open_pipe); |
| 525 | |
| 526 | int nci_hci_set_param(struct nci_dev *ndev, u8 gate, u8 idx, |
| 527 | const u8 *param, size_t param_len) |
| 528 | { |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 529 | struct nci_hcp_message *message; |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 530 | struct nci_conn_info *conn_info; |
| 531 | struct nci_data data; |
| 532 | int r; |
| 533 | u8 *tmp; |
| 534 | u8 pipe = ndev->hci_dev->gate2pipe[gate]; |
| 535 | |
| 536 | pr_debug("idx=%d to gate %d\n", idx, gate); |
| 537 | |
| 538 | if (pipe == NCI_HCI_INVALID_PIPE) |
| 539 | return -EADDRNOTAVAIL; |
| 540 | |
| 541 | conn_info = ndev->hci_dev->conn_info; |
| 542 | if (!conn_info) |
| 543 | return -EPROTO; |
| 544 | |
| 545 | tmp = kmalloc(1 + param_len, GFP_KERNEL); |
| 546 | if (!tmp) |
| 547 | return -ENOMEM; |
| 548 | |
| 549 | *tmp = idx; |
| 550 | memcpy(tmp + 1, param, param_len); |
| 551 | |
| 552 | data.conn_id = conn_info->conn_id; |
| 553 | data.pipe = pipe; |
| 554 | data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND, |
| 555 | NCI_HCI_ANY_SET_PARAMETER); |
| 556 | data.data = tmp; |
| 557 | data.data_len = param_len + 1; |
| 558 | |
| 559 | r = nci_request(ndev, nci_hci_send_data_req, |
| 560 | (unsigned long)&data, |
| 561 | msecs_to_jiffies(NCI_DATA_TIMEOUT)); |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 562 | if (r == NCI_STATUS_OK) { |
| 563 | message = (struct nci_hcp_message *)conn_info->rx_skb->data; |
| 564 | r = nci_hci_result_to_errno( |
| 565 | NCI_HCP_MSG_GET_CMD(message->header)); |
| 566 | skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN); |
| 567 | } |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 568 | |
| 569 | kfree(tmp); |
| 570 | return r; |
| 571 | } |
| 572 | EXPORT_SYMBOL(nci_hci_set_param); |
| 573 | |
| 574 | int nci_hci_get_param(struct nci_dev *ndev, u8 gate, u8 idx, |
| 575 | struct sk_buff **skb) |
| 576 | { |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 577 | struct nci_hcp_message *message; |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 578 | struct nci_conn_info *conn_info; |
| 579 | struct nci_data data; |
| 580 | int r; |
| 581 | u8 pipe = ndev->hci_dev->gate2pipe[gate]; |
| 582 | |
| 583 | pr_debug("idx=%d to gate %d\n", idx, gate); |
| 584 | |
| 585 | if (pipe == NCI_HCI_INVALID_PIPE) |
| 586 | return -EADDRNOTAVAIL; |
| 587 | |
| 588 | conn_info = ndev->hci_dev->conn_info; |
| 589 | if (!conn_info) |
| 590 | return -EPROTO; |
| 591 | |
| 592 | data.conn_id = conn_info->conn_id; |
| 593 | data.pipe = pipe; |
| 594 | data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND, |
| 595 | NCI_HCI_ANY_GET_PARAMETER); |
| 596 | data.data = &idx; |
| 597 | data.data_len = 1; |
| 598 | |
| 599 | r = nci_request(ndev, nci_hci_send_data_req, (unsigned long)&data, |
| 600 | msecs_to_jiffies(NCI_DATA_TIMEOUT)); |
| 601 | |
Christophe Ricard | d8cd37e | 2015-10-25 22:54:21 +0100 | [diff] [blame] | 602 | if (r == NCI_STATUS_OK) { |
| 603 | message = (struct nci_hcp_message *)conn_info->rx_skb->data; |
| 604 | r = nci_hci_result_to_errno( |
| 605 | NCI_HCP_MSG_GET_CMD(message->header)); |
| 606 | skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN); |
| 607 | |
| 608 | if (!r && skb) |
| 609 | *skb = conn_info->rx_skb; |
| 610 | } |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 611 | |
| 612 | return r; |
| 613 | } |
| 614 | EXPORT_SYMBOL(nci_hci_get_param); |
| 615 | |
| 616 | int nci_hci_connect_gate(struct nci_dev *ndev, |
| 617 | u8 dest_host, u8 dest_gate, u8 pipe) |
| 618 | { |
| 619 | int r; |
| 620 | |
| 621 | if (pipe == NCI_HCI_DO_NOT_OPEN_PIPE) |
| 622 | return 0; |
| 623 | |
| 624 | if (ndev->hci_dev->gate2pipe[dest_gate] != NCI_HCI_INVALID_PIPE) |
| 625 | return -EADDRINUSE; |
| 626 | |
| 627 | if (pipe != NCI_HCI_INVALID_PIPE) |
| 628 | goto open_pipe; |
| 629 | |
| 630 | switch (dest_gate) { |
| 631 | case NCI_HCI_LINK_MGMT_GATE: |
| 632 | pipe = NCI_HCI_LINK_MGMT_PIPE; |
| 633 | break; |
| 634 | case NCI_HCI_ADMIN_GATE: |
| 635 | pipe = NCI_HCI_ADMIN_PIPE; |
| 636 | break; |
| 637 | } |
| 638 | |
| 639 | open_pipe: |
| 640 | r = nci_hci_open_pipe(ndev, pipe); |
| 641 | if (r < 0) |
| 642 | return r; |
| 643 | |
| 644 | ndev->hci_dev->pipes[pipe].gate = dest_gate; |
| 645 | ndev->hci_dev->pipes[pipe].host = dest_host; |
| 646 | ndev->hci_dev->gate2pipe[dest_gate] = pipe; |
| 647 | |
| 648 | return 0; |
| 649 | } |
| 650 | EXPORT_SYMBOL(nci_hci_connect_gate); |
| 651 | |
| 652 | static int nci_hci_dev_connect_gates(struct nci_dev *ndev, |
| 653 | u8 gate_count, |
| 654 | struct nci_hci_gate *gates) |
| 655 | { |
| 656 | int r; |
| 657 | |
| 658 | while (gate_count--) { |
| 659 | r = nci_hci_connect_gate(ndev, gates->dest_host, |
| 660 | gates->gate, gates->pipe); |
| 661 | if (r < 0) |
| 662 | return r; |
| 663 | gates++; |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
| 669 | int nci_hci_dev_session_init(struct nci_dev *ndev) |
| 670 | { |
Christophe Ricard | 15d4a8d | 2015-02-03 19:48:07 +0100 | [diff] [blame] | 671 | struct nci_conn_info *conn_info; |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 672 | struct sk_buff *skb; |
| 673 | int r; |
| 674 | |
| 675 | ndev->hci_dev->count_pipes = 0; |
| 676 | ndev->hci_dev->expected_pipes = 0; |
| 677 | |
Christophe Ricard | 15d4a8d | 2015-02-03 19:48:07 +0100 | [diff] [blame] | 678 | conn_info = ndev->hci_dev->conn_info; |
| 679 | if (!conn_info) |
| 680 | return -EPROTO; |
| 681 | |
| 682 | conn_info->data_exchange_cb = nci_hci_data_received_cb; |
| 683 | conn_info->data_exchange_cb_context = ndev; |
| 684 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 685 | nci_hci_reset_pipes(ndev->hci_dev); |
| 686 | |
| 687 | if (ndev->hci_dev->init_data.gates[0].gate != NCI_HCI_ADMIN_GATE) |
| 688 | return -EPROTO; |
| 689 | |
| 690 | r = nci_hci_connect_gate(ndev, |
| 691 | ndev->hci_dev->init_data.gates[0].dest_host, |
| 692 | ndev->hci_dev->init_data.gates[0].gate, |
| 693 | ndev->hci_dev->init_data.gates[0].pipe); |
| 694 | if (r < 0) |
Joe Perches | 2622e2a | 2015-05-31 17:44:45 -0700 | [diff] [blame] | 695 | return r; |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 696 | |
| 697 | r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE, |
| 698 | NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY, &skb); |
| 699 | if (r < 0) |
Joe Perches | 2622e2a | 2015-05-31 17:44:45 -0700 | [diff] [blame] | 700 | return r; |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 701 | |
| 702 | if (skb->len && |
| 703 | skb->len == strlen(ndev->hci_dev->init_data.session_id) && |
Joe Perches | 2622e2a | 2015-05-31 17:44:45 -0700 | [diff] [blame] | 704 | !memcmp(ndev->hci_dev->init_data.session_id, skb->data, skb->len) && |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 705 | ndev->ops->hci_load_session) { |
| 706 | /* Restore gate<->pipe table from some proprietary location. */ |
| 707 | r = ndev->ops->hci_load_session(ndev); |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 708 | } else { |
Christophe Ricard | 8a49943 | 2015-10-25 22:54:24 +0100 | [diff] [blame^] | 709 | r = nci_hci_clear_all_pipes(ndev); |
| 710 | if (r < 0) |
| 711 | goto exit; |
| 712 | |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 713 | r = nci_hci_dev_connect_gates(ndev, |
| 714 | ndev->hci_dev->init_data.gate_count, |
| 715 | ndev->hci_dev->init_data.gates); |
| 716 | if (r < 0) |
| 717 | goto exit; |
| 718 | |
| 719 | r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE, |
| 720 | NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY, |
| 721 | ndev->hci_dev->init_data.session_id, |
| 722 | strlen(ndev->hci_dev->init_data.session_id)); |
| 723 | } |
Christophe Ricard | 11f54f2 | 2015-02-01 22:26:14 +0100 | [diff] [blame] | 724 | |
| 725 | exit: |
| 726 | kfree_skb(skb); |
| 727 | |
| 728 | return r; |
| 729 | } |
| 730 | EXPORT_SYMBOL(nci_hci_dev_session_init); |
| 731 | |
| 732 | struct nci_hci_dev *nci_hci_allocate(struct nci_dev *ndev) |
| 733 | { |
| 734 | struct nci_hci_dev *hdev; |
| 735 | |
| 736 | hdev = kzalloc(sizeof(*hdev), GFP_KERNEL); |
| 737 | if (!hdev) |
| 738 | return NULL; |
| 739 | |
| 740 | skb_queue_head_init(&hdev->rx_hcp_frags); |
| 741 | INIT_WORK(&hdev->msg_rx_work, nci_hci_msg_rx_work); |
| 742 | skb_queue_head_init(&hdev->msg_rx_queue); |
| 743 | hdev->ndev = ndev; |
| 744 | |
| 745 | return hdev; |
| 746 | } |