Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Mar 24-27, 2025, special US time zones
Register
Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (C) 2012  Intel Corporation. All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
  4 */
  5
  6#define pr_fmt(fmt) "hci: %s: " fmt, __func__
  7
  8#include <linux/init.h>
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11
 12#include <net/nfc/hci.h>
 13
 14#include "hci.h"
 15
 16/*
 17 * Payload is the HCP message data only. Instruction will be prepended.
 18 * Guarantees that cb will be called upon completion or timeout delay
 19 * counted from the moment the cmd is sent to the transport.
 20 */
 21int nfc_hci_hcp_message_tx(struct nfc_hci_dev *hdev, u8 pipe,
 22			   u8 type, u8 instruction,
 23			   const u8 *payload, size_t payload_len,
 24			   data_exchange_cb_t cb, void *cb_context,
 25			   unsigned long completion_delay)
 26{
 27	struct nfc_dev *ndev = hdev->ndev;
 28	struct hci_msg *cmd;
 29	const u8 *ptr = payload;
 30	int hci_len, err;
 31	bool firstfrag = true;
 32
 33	cmd = kzalloc(sizeof(struct hci_msg), GFP_KERNEL);
 34	if (cmd == NULL)
 35		return -ENOMEM;
 36
 37	INIT_LIST_HEAD(&cmd->msg_l);
 38	skb_queue_head_init(&cmd->msg_frags);
 39	cmd->wait_response = (type == NFC_HCI_HCP_COMMAND) ? true : false;
 40	cmd->cb = cb;
 41	cmd->cb_context = cb_context;
 42	cmd->completion_delay = completion_delay;
 43
 44	hci_len = payload_len + 1;
 45	while (hci_len > 0) {
 46		struct sk_buff *skb;
 47		int skb_len, data_link_len;
 48		struct hcp_packet *packet;
 49
 50		if (NFC_HCI_HCP_PACKET_HEADER_LEN + hci_len <=
 51		    hdev->max_data_link_payload)
 52			data_link_len = hci_len;
 53		else
 54			data_link_len = hdev->max_data_link_payload -
 55					NFC_HCI_HCP_PACKET_HEADER_LEN;
 56
 57		skb_len = ndev->tx_headroom + NFC_HCI_HCP_PACKET_HEADER_LEN +
 58			  data_link_len + ndev->tx_tailroom;
 59		hci_len -= data_link_len;
 60
 61		skb = alloc_skb(skb_len, GFP_KERNEL);
 62		if (skb == NULL) {
 63			err = -ENOMEM;
 64			goto out_skb_err;
 65		}
 66		skb_reserve(skb, ndev->tx_headroom);
 67
 68		skb_put(skb, NFC_HCI_HCP_PACKET_HEADER_LEN + data_link_len);
 69
 70		/* Only the last fragment will have the cb bit set to 1 */
 71		packet = (struct hcp_packet *)skb->data;
 72		packet->header = pipe;
 73		if (firstfrag) {
 74			firstfrag = false;
 75			packet->message.header = HCP_HEADER(type, instruction);
 76			if (ptr) {
 77				memcpy(packet->message.data, ptr,
 78				       data_link_len - 1);
 79				ptr += data_link_len - 1;
 80			}
 81		} else {
 82			memcpy(&packet->message, ptr, data_link_len);
 83			ptr += data_link_len;
 84		}
 85
 86		/* This is the last fragment, set the cb bit */
 87		if (hci_len == 0)
 88			packet->header |= ~NFC_HCI_FRAGMENT;
 89
 90		skb_queue_tail(&cmd->msg_frags, skb);
 91	}
 92
 93	mutex_lock(&hdev->msg_tx_mutex);
 94
 95	if (hdev->shutting_down) {
 96		err = -ESHUTDOWN;
 97		mutex_unlock(&hdev->msg_tx_mutex);
 98		goto out_skb_err;
 99	}
100
101	list_add_tail(&cmd->msg_l, &hdev->msg_tx_queue);
102	mutex_unlock(&hdev->msg_tx_mutex);
103
104	schedule_work(&hdev->msg_tx_work);
105
106	return 0;
107
108out_skb_err:
109	skb_queue_purge(&cmd->msg_frags);
110	kfree(cmd);
111
112	return err;
113}
114
115/*
116 * Receive hcp message for pipe, with type and cmd.
117 * skb contains optional message data only.
118 */
119void nfc_hci_hcp_message_rx(struct nfc_hci_dev *hdev, u8 pipe, u8 type,
120			    u8 instruction, struct sk_buff *skb)
121{
122	switch (type) {
123	case NFC_HCI_HCP_RESPONSE:
124		nfc_hci_resp_received(hdev, instruction, skb);
125		break;
126	case NFC_HCI_HCP_COMMAND:
127		nfc_hci_cmd_received(hdev, pipe, instruction, skb);
128		break;
129	case NFC_HCI_HCP_EVENT:
130		nfc_hci_event_received(hdev, pipe, instruction, skb);
131		break;
132	default:
133		pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
134		       type, instruction);
135		kfree_skb(skb);
136		break;
137	}
138}
v4.6
 
  1/*
  2 * Copyright (C) 2012  Intel Corporation. All rights reserved.
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License as published by
  6 * the Free Software Foundation; either version 2 of the License, or
  7 * (at your option) any later version.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 12 * GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 16 */
 17
 18#define pr_fmt(fmt) "hci: %s: " fmt, __func__
 19
 20#include <linux/init.h>
 21#include <linux/kernel.h>
 22#include <linux/module.h>
 23
 24#include <net/nfc/hci.h>
 25
 26#include "hci.h"
 27
 28/*
 29 * Payload is the HCP message data only. Instruction will be prepended.
 30 * Guarantees that cb will be called upon completion or timeout delay
 31 * counted from the moment the cmd is sent to the transport.
 32 */
 33int nfc_hci_hcp_message_tx(struct nfc_hci_dev *hdev, u8 pipe,
 34			   u8 type, u8 instruction,
 35			   const u8 *payload, size_t payload_len,
 36			   data_exchange_cb_t cb, void *cb_context,
 37			   unsigned long completion_delay)
 38{
 39	struct nfc_dev *ndev = hdev->ndev;
 40	struct hci_msg *cmd;
 41	const u8 *ptr = payload;
 42	int hci_len, err;
 43	bool firstfrag = true;
 44
 45	cmd = kzalloc(sizeof(struct hci_msg), GFP_KERNEL);
 46	if (cmd == NULL)
 47		return -ENOMEM;
 48
 49	INIT_LIST_HEAD(&cmd->msg_l);
 50	skb_queue_head_init(&cmd->msg_frags);
 51	cmd->wait_response = (type == NFC_HCI_HCP_COMMAND) ? true : false;
 52	cmd->cb = cb;
 53	cmd->cb_context = cb_context;
 54	cmd->completion_delay = completion_delay;
 55
 56	hci_len = payload_len + 1;
 57	while (hci_len > 0) {
 58		struct sk_buff *skb;
 59		int skb_len, data_link_len;
 60		struct hcp_packet *packet;
 61
 62		if (NFC_HCI_HCP_PACKET_HEADER_LEN + hci_len <=
 63		    hdev->max_data_link_payload)
 64			data_link_len = hci_len;
 65		else
 66			data_link_len = hdev->max_data_link_payload -
 67					NFC_HCI_HCP_PACKET_HEADER_LEN;
 68
 69		skb_len = ndev->tx_headroom + NFC_HCI_HCP_PACKET_HEADER_LEN +
 70			  data_link_len + ndev->tx_tailroom;
 71		hci_len -= data_link_len;
 72
 73		skb = alloc_skb(skb_len, GFP_KERNEL);
 74		if (skb == NULL) {
 75			err = -ENOMEM;
 76			goto out_skb_err;
 77		}
 78		skb_reserve(skb, ndev->tx_headroom);
 79
 80		skb_put(skb, NFC_HCI_HCP_PACKET_HEADER_LEN + data_link_len);
 81
 82		/* Only the last fragment will have the cb bit set to 1 */
 83		packet = (struct hcp_packet *)skb->data;
 84		packet->header = pipe;
 85		if (firstfrag) {
 86			firstfrag = false;
 87			packet->message.header = HCP_HEADER(type, instruction);
 88			if (ptr) {
 89				memcpy(packet->message.data, ptr,
 90				       data_link_len - 1);
 91				ptr += data_link_len - 1;
 92			}
 93		} else {
 94			memcpy(&packet->message, ptr, data_link_len);
 95			ptr += data_link_len;
 96		}
 97
 98		/* This is the last fragment, set the cb bit */
 99		if (hci_len == 0)
100			packet->header |= ~NFC_HCI_FRAGMENT;
101
102		skb_queue_tail(&cmd->msg_frags, skb);
103	}
104
105	mutex_lock(&hdev->msg_tx_mutex);
106
107	if (hdev->shutting_down) {
108		err = -ESHUTDOWN;
109		mutex_unlock(&hdev->msg_tx_mutex);
110		goto out_skb_err;
111	}
112
113	list_add_tail(&cmd->msg_l, &hdev->msg_tx_queue);
114	mutex_unlock(&hdev->msg_tx_mutex);
115
116	schedule_work(&hdev->msg_tx_work);
117
118	return 0;
119
120out_skb_err:
121	skb_queue_purge(&cmd->msg_frags);
122	kfree(cmd);
123
124	return err;
125}
126
127/*
128 * Receive hcp message for pipe, with type and cmd.
129 * skb contains optional message data only.
130 */
131void nfc_hci_hcp_message_rx(struct nfc_hci_dev *hdev, u8 pipe, u8 type,
132			    u8 instruction, struct sk_buff *skb)
133{
134	switch (type) {
135	case NFC_HCI_HCP_RESPONSE:
136		nfc_hci_resp_received(hdev, instruction, skb);
137		break;
138	case NFC_HCI_HCP_COMMAND:
139		nfc_hci_cmd_received(hdev, pipe, instruction, skb);
140		break;
141	case NFC_HCI_HCP_EVENT:
142		nfc_hci_event_received(hdev, pipe, instruction, skb);
143		break;
144	default:
145		pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
146		       type, instruction);
147		kfree_skb(skb);
148		break;
149	}
150}