^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2012 Intel Corporation. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #define pr_fmt(fmt) "hci: %s: " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <net/nfc/hci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "hci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Payload is the HCP message data only. Instruction will be prepended.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * Guarantees that cb will be called upon completion or timeout delay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * counted from the moment the cmd is sent to the transport.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) int nfc_hci_hcp_message_tx(struct nfc_hci_dev *hdev, u8 pipe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) u8 type, u8 instruction,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) const u8 *payload, size_t payload_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) data_exchange_cb_t cb, void *cb_context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned long completion_delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct nfc_dev *ndev = hdev->ndev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct hci_msg *cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) const u8 *ptr = payload;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int hci_len, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) bool firstfrag = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) cmd = kzalloc(sizeof(struct hci_msg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (cmd == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) INIT_LIST_HEAD(&cmd->msg_l);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) skb_queue_head_init(&cmd->msg_frags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) cmd->wait_response = (type == NFC_HCI_HCP_COMMAND) ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) cmd->cb = cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) cmd->cb_context = cb_context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) cmd->completion_delay = completion_delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) hci_len = payload_len + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) while (hci_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct sk_buff *skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int skb_len, data_link_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct hcp_packet *packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (NFC_HCI_HCP_PACKET_HEADER_LEN + hci_len <=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) hdev->max_data_link_payload)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) data_link_len = hci_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) data_link_len = hdev->max_data_link_payload -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) NFC_HCI_HCP_PACKET_HEADER_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) skb_len = ndev->tx_headroom + NFC_HCI_HCP_PACKET_HEADER_LEN +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) data_link_len + ndev->tx_tailroom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) hci_len -= data_link_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) skb = alloc_skb(skb_len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (skb == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) goto out_skb_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) skb_reserve(skb, ndev->tx_headroom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) skb_put(skb, NFC_HCI_HCP_PACKET_HEADER_LEN + data_link_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) /* Only the last fragment will have the cb bit set to 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) packet = (struct hcp_packet *)skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) packet->header = pipe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (firstfrag) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) firstfrag = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) packet->message.header = HCP_HEADER(type, instruction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) memcpy(packet->message.data, ptr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) data_link_len - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) ptr += data_link_len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) memcpy(&packet->message, ptr, data_link_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ptr += data_link_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) /* This is the last fragment, set the cb bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (hci_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) packet->header |= ~NFC_HCI_FRAGMENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) skb_queue_tail(&cmd->msg_frags, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) mutex_lock(&hdev->msg_tx_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (hdev->shutting_down) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) err = -ESHUTDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) mutex_unlock(&hdev->msg_tx_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) goto out_skb_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) list_add_tail(&cmd->msg_l, &hdev->msg_tx_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) mutex_unlock(&hdev->msg_tx_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) schedule_work(&hdev->msg_tx_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) out_skb_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) skb_queue_purge(&cmd->msg_frags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) kfree(cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * Receive hcp message for pipe, with type and cmd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * skb contains optional message data only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) void nfc_hci_hcp_message_rx(struct nfc_hci_dev *hdev, u8 pipe, u8 type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) u8 instruction, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) case NFC_HCI_HCP_RESPONSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) nfc_hci_resp_received(hdev, instruction, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) case NFC_HCI_HCP_COMMAND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) nfc_hci_cmd_received(hdev, pipe, instruction, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) case NFC_HCI_HCP_EVENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) nfc_hci_event_received(hdev, pipe, instruction, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) type, instruction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }