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.
7 * Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
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
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.
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/>.
23 #include <linux/skbuff.h>
26 #include <net/nfc/nci.h>
27 #include <net/nfc/nci_core.h>
28 #include <linux/nfc.h>
38 struct nci_hci_create_pipe_params
{
44 struct nci_hci_create_pipe_resp
{
52 struct nci_hci_delete_pipe_noti
{
56 struct nci_hci_all_pipe_cleared_noti
{
60 struct nci_hcp_message
{
61 u8 header
; /* type -cmd,evt,rsp- + instruction */
65 struct nci_hcp_packet
{
66 u8 header
; /* cbit+pipe */
67 struct nci_hcp_message message
;
70 #define NCI_HCI_ANY_SET_PARAMETER 0x01
71 #define NCI_HCI_ANY_GET_PARAMETER 0x02
72 #define NCI_HCI_ANY_CLOSE_PIPE 0x04
74 #define NCI_HFP_NO_CHAINING 0x80
76 #define NCI_NFCEE_ID_HCI 0x80
78 #define NCI_EVT_HOT_PLUG 0x03
80 #define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY 0x01
83 #define NCI_HCI_HCP_PACKET_HEADER_LEN 1
84 #define NCI_HCI_HCP_MESSAGE_HEADER_LEN 1
85 #define NCI_HCI_HCP_HEADER_LEN 2
88 #define NCI_HCI_HCP_COMMAND 0x00
89 #define NCI_HCI_HCP_EVENT 0x01
90 #define NCI_HCI_HCP_RESPONSE 0x02
92 #define NCI_HCI_ADM_NOTIFY_PIPE_CREATED 0x12
93 #define NCI_HCI_ADM_NOTIFY_PIPE_DELETED 0x13
94 #define NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED 0x15
96 #define NCI_HCI_FRAGMENT 0x7f
97 #define NCI_HCP_HEADER(type, instr) ((((type) & 0x03) << 6) |\
100 #define NCI_HCP_MSG_GET_TYPE(header) ((header & 0xc0) >> 6)
101 #define NCI_HCP_MSG_GET_CMD(header) (header & 0x3f)
102 #define NCI_HCP_MSG_GET_PIPE(header) (header & 0x7f)
104 static int nci_hci_result_to_errno(u8 result
)
109 case NCI_HCI_ANY_E_REG_PAR_UNKNOWN
:
111 case NCI_HCI_ANY_E_TIMEOUT
:
119 static void nci_hci_reset_pipes(struct nci_hci_dev
*hdev
)
123 for (i
= 0; i
< NCI_HCI_MAX_PIPES
; i
++) {
124 hdev
->pipes
[i
].gate
= NCI_HCI_INVALID_GATE
;
125 hdev
->pipes
[i
].host
= NCI_HCI_INVALID_HOST
;
127 memset(hdev
->gate2pipe
, NCI_HCI_INVALID_PIPE
, sizeof(hdev
->gate2pipe
));
130 static void nci_hci_reset_pipes_per_host(struct nci_dev
*ndev
, u8 host
)
134 for (i
= 0; i
< NCI_HCI_MAX_PIPES
; i
++) {
135 if (ndev
->hci_dev
->pipes
[i
].host
== host
) {
136 ndev
->hci_dev
->pipes
[i
].gate
= NCI_HCI_INVALID_GATE
;
137 ndev
->hci_dev
->pipes
[i
].host
= NCI_HCI_INVALID_HOST
;
142 /* Fragment HCI data over NCI packet.
143 * NFC Forum NCI 10.2.2 Data Exchange:
144 * The payload of the Data Packets sent on the Logical Connection SHALL be
145 * valid HCP packets, as defined within [ETSI_102622]. Each Data Packet SHALL
146 * contain a single HCP packet. NCI Segmentation and Reassembly SHALL NOT be
147 * applied to Data Messages in either direction. The HCI fragmentation mechanism
148 * is used if required.
150 static int nci_hci_send_data(struct nci_dev
*ndev
, u8 pipe
,
151 const u8 data_type
, const u8
*data
,
154 struct nci_conn_info
*conn_info
;
159 conn_info
= ndev
->hci_dev
->conn_info
;
164 skb
= nci_skb_alloc(ndev
, conn_info
->max_pkt_payload_len
+
165 NCI_DATA_HDR_SIZE
, GFP_KERNEL
);
169 skb_reserve(skb
, NCI_DATA_HDR_SIZE
+ 2);
170 *skb_push(skb
, 1) = data_type
;
173 len
= conn_info
->max_pkt_payload_len
;
175 /* If last packet add NCI_HFP_NO_CHAINING */
176 if (i
+ conn_info
->max_pkt_payload_len
-
177 (skb
->len
+ 1) >= data_len
) {
178 cb
|= NCI_HFP_NO_CHAINING
;
181 len
= conn_info
->max_pkt_payload_len
- skb
->len
- 1;
184 *skb_push(skb
, 1) = cb
;
187 memcpy(skb_put(skb
, len
), data
+ i
, len
);
189 r
= nci_send_data(ndev
, conn_info
->conn_id
, skb
);
196 skb
= nci_skb_alloc(ndev
,
197 conn_info
->max_pkt_payload_len
+
198 NCI_DATA_HDR_SIZE
, GFP_KERNEL
);
202 skb_reserve(skb
, NCI_DATA_HDR_SIZE
+ 1);
204 } while (i
< data_len
);
209 static void nci_hci_send_data_req(struct nci_dev
*ndev
, unsigned long opt
)
211 struct nci_data
*data
= (struct nci_data
*)opt
;
213 nci_hci_send_data(ndev
, data
->pipe
, data
->cmd
,
214 data
->data
, data
->data_len
);
217 int nci_hci_send_event(struct nci_dev
*ndev
, u8 gate
, u8 event
,
218 const u8
*param
, size_t param_len
)
220 u8 pipe
= ndev
->hci_dev
->gate2pipe
[gate
];
222 if (pipe
== NCI_HCI_INVALID_PIPE
)
223 return -EADDRNOTAVAIL
;
225 return nci_hci_send_data(ndev
, pipe
,
226 NCI_HCP_HEADER(NCI_HCI_HCP_EVENT
, event
),
229 EXPORT_SYMBOL(nci_hci_send_event
);
231 int nci_hci_send_cmd(struct nci_dev
*ndev
, u8 gate
, u8 cmd
,
232 const u8
*param
, size_t param_len
,
233 struct sk_buff
**skb
)
235 struct nci_hcp_message
*message
;
236 struct nci_conn_info
*conn_info
;
237 struct nci_data data
;
239 u8 pipe
= ndev
->hci_dev
->gate2pipe
[gate
];
241 if (pipe
== NCI_HCI_INVALID_PIPE
)
242 return -EADDRNOTAVAIL
;
244 conn_info
= ndev
->hci_dev
->conn_info
;
248 data
.conn_id
= conn_info
->conn_id
;
250 data
.cmd
= NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND
, cmd
);
252 data
.data_len
= param_len
;
254 r
= nci_request(ndev
, nci_hci_send_data_req
, (unsigned long)&data
,
255 msecs_to_jiffies(NCI_DATA_TIMEOUT
));
256 if (r
== NCI_STATUS_OK
) {
257 message
= (struct nci_hcp_message
*)conn_info
->rx_skb
->data
;
258 r
= nci_hci_result_to_errno(
259 NCI_HCP_MSG_GET_CMD(message
->header
));
260 skb_pull(conn_info
->rx_skb
, NCI_HCI_HCP_MESSAGE_HEADER_LEN
);
263 *skb
= conn_info
->rx_skb
;
268 EXPORT_SYMBOL(nci_hci_send_cmd
);
270 static void nci_hci_event_received(struct nci_dev
*ndev
, u8 pipe
,
271 u8 event
, struct sk_buff
*skb
)
273 if (ndev
->ops
->hci_event_received
)
274 ndev
->ops
->hci_event_received(ndev
, pipe
, event
, skb
);
277 static void nci_hci_cmd_received(struct nci_dev
*ndev
, u8 pipe
,
278 u8 cmd
, struct sk_buff
*skb
)
280 u8 gate
= ndev
->hci_dev
->pipes
[pipe
].gate
;
281 u8 status
= NCI_HCI_ANY_OK
| ~NCI_HCI_FRAGMENT
;
282 u8 dest_gate
, new_pipe
;
283 struct nci_hci_create_pipe_resp
*create_info
;
284 struct nci_hci_delete_pipe_noti
*delete_info
;
285 struct nci_hci_all_pipe_cleared_noti
*cleared_info
;
287 pr_debug("from gate %x pipe %x cmd %x\n", gate
, pipe
, cmd
);
290 case NCI_HCI_ADM_NOTIFY_PIPE_CREATED
:
292 status
= NCI_HCI_ANY_E_NOK
;
295 create_info
= (struct nci_hci_create_pipe_resp
*)skb
->data
;
296 dest_gate
= create_info
->dest_gate
;
297 new_pipe
= create_info
->pipe
;
299 /* Save the new created pipe and bind with local gate,
300 * the description for skb->data[3] is destination gate id
301 * but since we received this cmd from host controller, we
302 * are the destination and it is our local gate
304 ndev
->hci_dev
->gate2pipe
[dest_gate
] = new_pipe
;
305 ndev
->hci_dev
->pipes
[new_pipe
].gate
= dest_gate
;
306 ndev
->hci_dev
->pipes
[new_pipe
].host
=
307 create_info
->src_host
;
309 case NCI_HCI_ANY_OPEN_PIPE
:
310 /* If the pipe is not created report an error */
311 if (gate
== NCI_HCI_INVALID_GATE
) {
312 status
= NCI_HCI_ANY_E_NOK
;
316 case NCI_HCI_ADM_NOTIFY_PIPE_DELETED
:
318 status
= NCI_HCI_ANY_E_NOK
;
321 delete_info
= (struct nci_hci_delete_pipe_noti
*)skb
->data
;
323 ndev
->hci_dev
->pipes
[delete_info
->pipe
].gate
=
324 NCI_HCI_INVALID_GATE
;
325 ndev
->hci_dev
->pipes
[delete_info
->pipe
].host
=
326 NCI_HCI_INVALID_HOST
;
328 case NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED
:
330 status
= NCI_HCI_ANY_E_NOK
;
335 (struct nci_hci_all_pipe_cleared_noti
*)skb
->data
;
336 nci_hci_reset_pipes_per_host(ndev
, cleared_info
->host
);
339 pr_debug("Discarded unknown cmd %x to gate %x\n", cmd
, gate
);
343 if (ndev
->ops
->hci_cmd_received
)
344 ndev
->ops
->hci_cmd_received(ndev
, pipe
, cmd
, skb
);
347 nci_hci_send_data(ndev
, pipe
, status
, NULL
, 0);
352 static void nci_hci_resp_received(struct nci_dev
*ndev
, u8 pipe
,
353 u8 result
, struct sk_buff
*skb
)
355 struct nci_conn_info
*conn_info
;
358 conn_info
= ndev
->hci_dev
->conn_info
;
360 status
= NCI_STATUS_REJECTED
;
364 conn_info
->rx_skb
= skb
;
367 nci_req_complete(ndev
, NCI_STATUS_OK
);
370 /* Receive hcp message for pipe, with type and cmd.
371 * skb contains optional message data only.
373 static void nci_hci_hcp_message_rx(struct nci_dev
*ndev
, u8 pipe
,
374 u8 type
, u8 instruction
, struct sk_buff
*skb
)
377 case NCI_HCI_HCP_RESPONSE
:
378 nci_hci_resp_received(ndev
, pipe
, instruction
, skb
);
380 case NCI_HCI_HCP_COMMAND
:
381 nci_hci_cmd_received(ndev
, pipe
, instruction
, skb
);
383 case NCI_HCI_HCP_EVENT
:
384 nci_hci_event_received(ndev
, pipe
, instruction
, skb
);
387 pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
393 nci_req_complete(ndev
, 0);
396 static void nci_hci_msg_rx_work(struct work_struct
*work
)
398 struct nci_hci_dev
*hdev
=
399 container_of(work
, struct nci_hci_dev
, msg_rx_work
);
401 struct nci_hcp_message
*message
;
402 u8 pipe
, type
, instruction
;
404 while ((skb
= skb_dequeue(&hdev
->msg_rx_queue
)) != NULL
) {
405 pipe
= NCI_HCP_MSG_GET_PIPE(skb
->data
[0]);
406 skb_pull(skb
, NCI_HCI_HCP_PACKET_HEADER_LEN
);
407 message
= (struct nci_hcp_message
*)skb
->data
;
408 type
= NCI_HCP_MSG_GET_TYPE(message
->header
);
409 instruction
= NCI_HCP_MSG_GET_CMD(message
->header
);
410 skb_pull(skb
, NCI_HCI_HCP_MESSAGE_HEADER_LEN
);
412 nci_hci_hcp_message_rx(hdev
->ndev
, pipe
,
413 type
, instruction
, skb
);
417 void nci_hci_data_received_cb(void *context
,
418 struct sk_buff
*skb
, int err
)
420 struct nci_dev
*ndev
= (struct nci_dev
*)context
;
421 struct nci_hcp_packet
*packet
;
423 struct sk_buff
*hcp_skb
;
424 struct sk_buff
*frag_skb
;
430 nci_req_complete(ndev
, err
);
434 packet
= (struct nci_hcp_packet
*)skb
->data
;
435 if ((packet
->header
& ~NCI_HCI_FRAGMENT
) == 0) {
436 skb_queue_tail(&ndev
->hci_dev
->rx_hcp_frags
, skb
);
440 /* it's the last fragment. Does it need re-aggregation? */
441 if (skb_queue_len(&ndev
->hci_dev
->rx_hcp_frags
)) {
442 pipe
= NCI_HCP_MSG_GET_PIPE(packet
->header
);
443 skb_queue_tail(&ndev
->hci_dev
->rx_hcp_frags
, skb
);
446 skb_queue_walk(&ndev
->hci_dev
->rx_hcp_frags
, frag_skb
) {
447 msg_len
+= (frag_skb
->len
-
448 NCI_HCI_HCP_PACKET_HEADER_LEN
);
451 hcp_skb
= nfc_alloc_recv_skb(NCI_HCI_HCP_PACKET_HEADER_LEN
+
452 msg_len
, GFP_KERNEL
);
454 nci_req_complete(ndev
, -ENOMEM
);
458 *skb_put(hcp_skb
, NCI_HCI_HCP_PACKET_HEADER_LEN
) = pipe
;
460 skb_queue_walk(&ndev
->hci_dev
->rx_hcp_frags
, frag_skb
) {
461 msg_len
= frag_skb
->len
- NCI_HCI_HCP_PACKET_HEADER_LEN
;
462 memcpy(skb_put(hcp_skb
, msg_len
), frag_skb
->data
+
463 NCI_HCI_HCP_PACKET_HEADER_LEN
, msg_len
);
466 skb_queue_purge(&ndev
->hci_dev
->rx_hcp_frags
);
468 packet
->header
&= NCI_HCI_FRAGMENT
;
472 /* if this is a response, dispatch immediately to
473 * unblock waiting cmd context. Otherwise, enqueue to dispatch
474 * in separate context where handler can also execute command.
476 packet
= (struct nci_hcp_packet
*)hcp_skb
->data
;
477 type
= NCI_HCP_MSG_GET_TYPE(packet
->message
.header
);
478 if (type
== NCI_HCI_HCP_RESPONSE
) {
479 pipe
= NCI_HCP_MSG_GET_PIPE(packet
->header
);
480 skb_pull(hcp_skb
, NCI_HCI_HCP_PACKET_HEADER_LEN
);
481 nci_hci_hcp_message_rx(ndev
, pipe
, type
,
482 NCI_STATUS_OK
, hcp_skb
);
484 skb_queue_tail(&ndev
->hci_dev
->msg_rx_queue
, hcp_skb
);
485 schedule_work(&ndev
->hci_dev
->msg_rx_work
);
489 int nci_hci_open_pipe(struct nci_dev
*ndev
, u8 pipe
)
491 struct nci_data data
;
492 struct nci_conn_info
*conn_info
;
494 conn_info
= ndev
->hci_dev
->conn_info
;
498 data
.conn_id
= conn_info
->conn_id
;
500 data
.cmd
= NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND
,
501 NCI_HCI_ANY_OPEN_PIPE
);
505 return nci_request(ndev
, nci_hci_send_data_req
,
506 (unsigned long)&data
,
507 msecs_to_jiffies(NCI_DATA_TIMEOUT
));
509 EXPORT_SYMBOL(nci_hci_open_pipe
);
511 int nci_hci_set_param(struct nci_dev
*ndev
, u8 gate
, u8 idx
,
512 const u8
*param
, size_t param_len
)
514 struct nci_hcp_message
*message
;
515 struct nci_conn_info
*conn_info
;
516 struct nci_data data
;
519 u8 pipe
= ndev
->hci_dev
->gate2pipe
[gate
];
521 pr_debug("idx=%d to gate %d\n", idx
, gate
);
523 if (pipe
== NCI_HCI_INVALID_PIPE
)
524 return -EADDRNOTAVAIL
;
526 conn_info
= ndev
->hci_dev
->conn_info
;
530 tmp
= kmalloc(1 + param_len
, GFP_KERNEL
);
535 memcpy(tmp
+ 1, param
, param_len
);
537 data
.conn_id
= conn_info
->conn_id
;
539 data
.cmd
= NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND
,
540 NCI_HCI_ANY_SET_PARAMETER
);
542 data
.data_len
= param_len
+ 1;
544 r
= nci_request(ndev
, nci_hci_send_data_req
,
545 (unsigned long)&data
,
546 msecs_to_jiffies(NCI_DATA_TIMEOUT
));
547 if (r
== NCI_STATUS_OK
) {
548 message
= (struct nci_hcp_message
*)conn_info
->rx_skb
->data
;
549 r
= nci_hci_result_to_errno(
550 NCI_HCP_MSG_GET_CMD(message
->header
));
551 skb_pull(conn_info
->rx_skb
, NCI_HCI_HCP_MESSAGE_HEADER_LEN
);
557 EXPORT_SYMBOL(nci_hci_set_param
);
559 int nci_hci_get_param(struct nci_dev
*ndev
, u8 gate
, u8 idx
,
560 struct sk_buff
**skb
)
562 struct nci_hcp_message
*message
;
563 struct nci_conn_info
*conn_info
;
564 struct nci_data data
;
566 u8 pipe
= ndev
->hci_dev
->gate2pipe
[gate
];
568 pr_debug("idx=%d to gate %d\n", idx
, gate
);
570 if (pipe
== NCI_HCI_INVALID_PIPE
)
571 return -EADDRNOTAVAIL
;
573 conn_info
= ndev
->hci_dev
->conn_info
;
577 data
.conn_id
= conn_info
->conn_id
;
579 data
.cmd
= NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND
,
580 NCI_HCI_ANY_GET_PARAMETER
);
584 r
= nci_request(ndev
, nci_hci_send_data_req
, (unsigned long)&data
,
585 msecs_to_jiffies(NCI_DATA_TIMEOUT
));
587 if (r
== NCI_STATUS_OK
) {
588 message
= (struct nci_hcp_message
*)conn_info
->rx_skb
->data
;
589 r
= nci_hci_result_to_errno(
590 NCI_HCP_MSG_GET_CMD(message
->header
));
591 skb_pull(conn_info
->rx_skb
, NCI_HCI_HCP_MESSAGE_HEADER_LEN
);
594 *skb
= conn_info
->rx_skb
;
599 EXPORT_SYMBOL(nci_hci_get_param
);
601 int nci_hci_connect_gate(struct nci_dev
*ndev
,
602 u8 dest_host
, u8 dest_gate
, u8 pipe
)
606 if (pipe
== NCI_HCI_DO_NOT_OPEN_PIPE
)
609 if (ndev
->hci_dev
->gate2pipe
[dest_gate
] != NCI_HCI_INVALID_PIPE
)
612 if (pipe
!= NCI_HCI_INVALID_PIPE
)
616 case NCI_HCI_LINK_MGMT_GATE
:
617 pipe
= NCI_HCI_LINK_MGMT_PIPE
;
619 case NCI_HCI_ADMIN_GATE
:
620 pipe
= NCI_HCI_ADMIN_PIPE
;
625 r
= nci_hci_open_pipe(ndev
, pipe
);
629 ndev
->hci_dev
->pipes
[pipe
].gate
= dest_gate
;
630 ndev
->hci_dev
->pipes
[pipe
].host
= dest_host
;
631 ndev
->hci_dev
->gate2pipe
[dest_gate
] = pipe
;
635 EXPORT_SYMBOL(nci_hci_connect_gate
);
637 static int nci_hci_dev_connect_gates(struct nci_dev
*ndev
,
639 struct nci_hci_gate
*gates
)
643 while (gate_count
--) {
644 r
= nci_hci_connect_gate(ndev
, gates
->dest_host
,
645 gates
->gate
, gates
->pipe
);
654 int nci_hci_dev_session_init(struct nci_dev
*ndev
)
656 struct nci_conn_info
*conn_info
;
660 ndev
->hci_dev
->count_pipes
= 0;
661 ndev
->hci_dev
->expected_pipes
= 0;
663 conn_info
= ndev
->hci_dev
->conn_info
;
667 conn_info
->data_exchange_cb
= nci_hci_data_received_cb
;
668 conn_info
->data_exchange_cb_context
= ndev
;
670 nci_hci_reset_pipes(ndev
->hci_dev
);
672 if (ndev
->hci_dev
->init_data
.gates
[0].gate
!= NCI_HCI_ADMIN_GATE
)
675 r
= nci_hci_connect_gate(ndev
,
676 ndev
->hci_dev
->init_data
.gates
[0].dest_host
,
677 ndev
->hci_dev
->init_data
.gates
[0].gate
,
678 ndev
->hci_dev
->init_data
.gates
[0].pipe
);
682 r
= nci_hci_get_param(ndev
, NCI_HCI_ADMIN_GATE
,
683 NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY
, &skb
);
688 skb
->len
== strlen(ndev
->hci_dev
->init_data
.session_id
) &&
689 memcmp(ndev
->hci_dev
->init_data
.session_id
,
690 skb
->data
, skb
->len
) == 0 &&
691 ndev
->ops
->hci_load_session
) {
692 /* Restore gate<->pipe table from some proprietary location. */
693 r
= ndev
->ops
->hci_load_session(ndev
);
697 r
= nci_hci_dev_connect_gates(ndev
,
698 ndev
->hci_dev
->init_data
.gate_count
,
699 ndev
->hci_dev
->init_data
.gates
);
703 r
= nci_hci_set_param(ndev
, NCI_HCI_ADMIN_GATE
,
704 NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY
,
705 ndev
->hci_dev
->init_data
.session_id
,
706 strlen(ndev
->hci_dev
->init_data
.session_id
));
716 EXPORT_SYMBOL(nci_hci_dev_session_init
);
718 struct nci_hci_dev
*nci_hci_allocate(struct nci_dev
*ndev
)
720 struct nci_hci_dev
*hdev
;
722 hdev
= kzalloc(sizeof(*hdev
), GFP_KERNEL
);
726 skb_queue_head_init(&hdev
->rx_hcp_frags
);
727 INIT_WORK(&hdev
->msg_rx_work
, nci_hci_msg_rx_work
);
728 skb_queue_head_init(&hdev
->msg_rx_queue
);