Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+29
View File
@@ -0,0 +1,29 @@
# SPDX-License-Identifier: GPL-2.0-only
config NFC_NCI
depends on NFC
tristate "NCI protocol support"
default n
help
NCI (NFC Controller Interface) is a communication protocol between
an NFC Controller (NFCC) and a Device Host (DH).
Say Y here to compile NCI support into the kernel or say M to
compile it as module (nci).
config NFC_NCI_SPI
depends on NFC_NCI && SPI
select CRC_CCITT
tristate "NCI over SPI protocol support"
default n
help
NCI (NFC Controller Interface) is a communication protocol between
an NFC Controller (NFCC) and a Device Host (DH).
Say yes if you use an NCI driver that requires SPI link layer.
config NFC_NCI_UART
depends on NFC_NCI && TTY
tristate "NCI over UART protocol support"
default n
help
Say yes if you use an NCI driver that requires UART link layer.
+14
View File
@@ -0,0 +1,14 @@
# SPDX-License-Identifier: GPL-2.0
#
# Makefile for the Linux NFC NCI layer.
#
obj-$(CONFIG_NFC_NCI) += nci.o
nci-objs := core.o data.o lib.o ntf.o rsp.o hci.o
nci_spi-y += spi.o
obj-$(CONFIG_NFC_NCI_SPI) += nci_spi.o
nci_uart-y += uart.o
obj-$(CONFIG_NFC_NCI_UART) += nci_uart.o
File diff suppressed because it is too large Load Diff
+305
View File
@@ -0,0 +1,305 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* The NFC Controller Interface is the communication protocol between an
* NFC Controller (NFCC) and a Device Host (DH).
*
* Copyright (C) 2011 Texas Instruments, Inc.
* Copyright (C) 2014 Marvell International Ltd.
*
* Written by Ilan Elias <ilane@ti.com>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/bitops.h>
#include <linux/skbuff.h>
#include "../nfc.h"
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
#include <linux/nfc.h>
/* Complete data exchange transaction and forward skb to nfc core */
void nci_data_exchange_complete(struct nci_dev *ndev, struct sk_buff *skb,
__u8 conn_id, int err)
{
const struct nci_conn_info *conn_info;
data_exchange_cb_t cb;
void *cb_context;
conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
if (!conn_info) {
kfree_skb(skb);
clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
return;
}
cb = conn_info->data_exchange_cb;
cb_context = conn_info->data_exchange_cb_context;
pr_debug("len %d, err %d\n", skb ? skb->len : 0, err);
/* data exchange is complete, stop the data timer */
timer_delete_sync(&ndev->data_timer);
clear_bit(NCI_DATA_EXCHANGE_TO, &ndev->flags);
/* Mark the exchange as done before calling the callback.
* The callback (e.g. rawsock_data_exchange_complete) may
* want to immediately queue another data exchange.
*/
clear_bit(NCI_DATA_EXCHANGE, &ndev->flags);
if (cb) {
/* forward skb to nfc core */
cb(cb_context, skb, err);
} else if (skb) {
pr_err("no rx callback, dropping rx data...\n");
/* no waiting callback, free skb */
kfree_skb(skb);
}
}
/* ----------------- NCI TX Data ----------------- */
static inline void nci_push_data_hdr(struct nci_dev *ndev,
__u8 conn_id,
struct sk_buff *skb,
__u8 pbf)
{
struct nci_data_hdr *hdr;
int plen = skb->len;
hdr = skb_push(skb, NCI_DATA_HDR_SIZE);
hdr->conn_id = conn_id;
hdr->rfu = 0;
hdr->plen = plen;
nci_mt_set((__u8 *)hdr, NCI_MT_DATA_PKT);
nci_pbf_set((__u8 *)hdr, pbf);
}
int nci_conn_max_data_pkt_payload_size(struct nci_dev *ndev, __u8 conn_id)
{
const struct nci_conn_info *conn_info;
conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
if (!conn_info)
return -EPROTO;
return conn_info->max_pkt_payload_len;
}
EXPORT_SYMBOL(nci_conn_max_data_pkt_payload_size);
static int nci_queue_tx_data_frags(struct nci_dev *ndev,
__u8 conn_id,
struct sk_buff *skb) {
const struct nci_conn_info *conn_info;
int total_len = skb->len;
const unsigned char *data = skb->data;
unsigned long flags;
struct sk_buff_head frags_q;
struct sk_buff *skb_frag;
int frag_len;
int rc = 0;
pr_debug("conn_id 0x%x, total_len %d\n", conn_id, total_len);
conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
if (!conn_info) {
rc = -EPROTO;
goto exit;
}
__skb_queue_head_init(&frags_q);
while (total_len) {
frag_len =
min_t(int, total_len, conn_info->max_pkt_payload_len);
skb_frag = nci_skb_alloc(ndev,
(NCI_DATA_HDR_SIZE + frag_len),
GFP_ATOMIC);
if (skb_frag == NULL) {
rc = -ENOMEM;
goto free_exit;
}
skb_reserve(skb_frag, NCI_DATA_HDR_SIZE);
/* first, copy the data */
skb_put_data(skb_frag, data, frag_len);
/* second, set the header */
nci_push_data_hdr(ndev, conn_id, skb_frag,
((total_len == frag_len) ?
(NCI_PBF_LAST) : (NCI_PBF_CONT)));
__skb_queue_tail(&frags_q, skb_frag);
data += frag_len;
total_len -= frag_len;
pr_debug("frag_len %d, remaining total_len %d\n",
frag_len, total_len);
}
/* queue all fragments atomically */
spin_lock_irqsave(&ndev->tx_q.lock, flags);
while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
__skb_queue_tail(&ndev->tx_q, skb_frag);
spin_unlock_irqrestore(&ndev->tx_q.lock, flags);
/* free the original skb */
kfree_skb(skb);
goto exit;
free_exit:
while ((skb_frag = __skb_dequeue(&frags_q)) != NULL)
kfree_skb(skb_frag);
exit:
return rc;
}
/* Send NCI data */
int nci_send_data(struct nci_dev *ndev, __u8 conn_id, struct sk_buff *skb)
{
const struct nci_conn_info *conn_info;
int rc = 0;
pr_debug("conn_id 0x%x, plen %d\n", conn_id, skb->len);
conn_info = nci_get_conn_info_by_conn_id(ndev, conn_id);
if (!conn_info) {
rc = -EPROTO;
goto free_exit;
}
/* check if the packet need to be fragmented */
if (skb->len <= conn_info->max_pkt_payload_len) {
/* no need to fragment packet */
nci_push_data_hdr(ndev, conn_id, skb, NCI_PBF_LAST);
skb_queue_tail(&ndev->tx_q, skb);
} else {
/* fragment packet and queue the fragments */
rc = nci_queue_tx_data_frags(ndev, conn_id, skb);
if (rc) {
pr_err("failed to fragment tx data packet\n");
goto free_exit;
}
}
ndev->cur_conn_id = conn_id;
queue_work(ndev->tx_wq, &ndev->tx_work);
goto exit;
free_exit:
kfree_skb(skb);
exit:
return rc;
}
EXPORT_SYMBOL(nci_send_data);
/* ----------------- NCI RX Data ----------------- */
static void nci_add_rx_data_frag(struct nci_dev *ndev,
struct sk_buff *skb,
__u8 pbf, __u8 conn_id, __u8 status)
{
int reassembly_len;
int err = 0;
if (status) {
err = status;
goto exit;
}
if (ndev->rx_data_reassembly) {
reassembly_len = ndev->rx_data_reassembly->len;
/* first, make enough room for the already accumulated data */
if (skb_cow_head(skb, reassembly_len)) {
pr_err("error adding room for accumulated rx data\n");
kfree_skb(skb);
skb = NULL;
kfree_skb(ndev->rx_data_reassembly);
ndev->rx_data_reassembly = NULL;
err = -ENOMEM;
goto exit;
}
/* second, combine the two fragments */
memcpy(skb_push(skb, reassembly_len),
ndev->rx_data_reassembly->data,
reassembly_len);
/* third, free old reassembly */
kfree_skb(ndev->rx_data_reassembly);
ndev->rx_data_reassembly = NULL;
}
if (pbf == NCI_PBF_CONT) {
/* need to wait for next fragment, store skb and exit */
ndev->rx_data_reassembly = skb;
return;
}
exit:
if (ndev->nfc_dev->rf_mode == NFC_RF_TARGET) {
/* Data received in Target mode, forward to nfc core */
err = nfc_tm_data_received(ndev->nfc_dev, skb);
if (err)
pr_err("unable to handle received data\n");
} else {
nci_data_exchange_complete(ndev, skb, conn_id, err);
}
}
/* Rx Data packet */
void nci_rx_data_packet(struct nci_dev *ndev, struct sk_buff *skb)
{
__u8 pbf = nci_pbf(skb->data);
__u8 status = 0;
__u8 conn_id = nci_conn_id(skb->data);
const struct nci_conn_info *conn_info;
pr_debug("len %d\n", skb->len);
pr_debug("NCI RX: MT=data, PBF=%d, conn_id=%d, plen=%d\n",
nci_pbf(skb->data),
nci_conn_id(skb->data),
nci_plen(skb->data));
conn_info = nci_get_conn_info_by_conn_id(ndev, nci_conn_id(skb->data));
if (!conn_info) {
kfree_skb(skb);
return;
}
/* strip the nci data header */
skb_pull(skb, NCI_DATA_HDR_SIZE);
if (ndev->target_active_prot == NFC_PROTO_MIFARE ||
ndev->target_active_prot == NFC_PROTO_JEWEL ||
ndev->target_active_prot == NFC_PROTO_FELICA ||
ndev->target_active_prot == NFC_PROTO_ISO15693) {
/* frame I/F => remove the status byte */
pr_debug("frame I/F => remove the status byte\n");
status = skb->data[skb->len - 1];
skb_trim(skb, (skb->len - 1));
}
nci_add_rx_data_frag(ndev, skb, pbf, conn_id, nci_to_errno(status));
}
+805
View File
@@ -0,0 +1,805 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* The NFC Controller Interface is the communication protocol between an
* NFC Controller (NFCC) and a Device Host (DH).
* This is the HCI over NCI implementation, as specified in the 10.2
* section of the NCI 1.1 specification.
*
* Copyright (C) 2014 STMicroelectronics SAS. All rights reserved.
*/
#include <linux/skbuff.h>
#include "../nfc.h"
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
#include <linux/nfc.h>
#include <linux/kcov.h>
struct nci_data {
u8 conn_id;
u8 pipe;
u8 cmd;
const u8 *data;
u32 data_len;
} __packed;
struct nci_hci_create_pipe_params {
u8 src_gate;
u8 dest_host;
u8 dest_gate;
} __packed;
struct nci_hci_create_pipe_resp {
u8 src_host;
u8 src_gate;
u8 dest_host;
u8 dest_gate;
u8 pipe;
} __packed;
struct nci_hci_delete_pipe_noti {
u8 pipe;
} __packed;
struct nci_hci_all_pipe_cleared_noti {
u8 host;
} __packed;
struct nci_hcp_message {
u8 header; /* type -cmd,evt,rsp- + instruction */
u8 data[];
} __packed;
struct nci_hcp_packet {
u8 header; /* cbit+pipe */
struct nci_hcp_message message;
} __packed;
#define NCI_HCI_ANY_SET_PARAMETER 0x01
#define NCI_HCI_ANY_GET_PARAMETER 0x02
#define NCI_HCI_ANY_CLOSE_PIPE 0x04
#define NCI_HCI_ADM_CLEAR_ALL_PIPE 0x14
#define NCI_HFP_NO_CHAINING 0x80
#define NCI_NFCEE_ID_HCI 0x80
#define NCI_EVT_HOT_PLUG 0x03
#define NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY 0x01
#define NCI_HCI_ADM_CREATE_PIPE 0x10
#define NCI_HCI_ADM_DELETE_PIPE 0x11
/* HCP headers */
#define NCI_HCI_HCP_PACKET_HEADER_LEN 1
#define NCI_HCI_HCP_MESSAGE_HEADER_LEN 1
#define NCI_HCI_HCP_HEADER_LEN 2
/* HCP types */
#define NCI_HCI_HCP_COMMAND 0x00
#define NCI_HCI_HCP_EVENT 0x01
#define NCI_HCI_HCP_RESPONSE 0x02
#define NCI_HCI_ADM_NOTIFY_PIPE_CREATED 0x12
#define NCI_HCI_ADM_NOTIFY_PIPE_DELETED 0x13
#define NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED 0x15
#define NCI_HCI_FRAGMENT 0x7f
#define NCI_HCP_HEADER(type, instr) ((((type) & 0x03) << 6) |\
((instr) & 0x3f))
#define NCI_HCP_MSG_GET_TYPE(header) ((header & 0xc0) >> 6)
#define NCI_HCP_MSG_GET_CMD(header) (header & 0x3f)
#define NCI_HCP_MSG_GET_PIPE(header) (header & 0x7f)
static int nci_hci_result_to_errno(u8 result)
{
switch (result) {
case NCI_HCI_ANY_OK:
return 0;
case NCI_HCI_ANY_E_REG_PAR_UNKNOWN:
return -EOPNOTSUPP;
case NCI_HCI_ANY_E_TIMEOUT:
return -ETIME;
default:
return -1;
}
}
/* HCI core */
static void nci_hci_reset_pipes(struct nci_hci_dev *hdev)
{
int i;
for (i = 0; i < NCI_HCI_MAX_PIPES; i++) {
hdev->pipes[i].gate = NCI_HCI_INVALID_GATE;
hdev->pipes[i].host = NCI_HCI_INVALID_HOST;
}
memset(hdev->gate2pipe, NCI_HCI_INVALID_PIPE, sizeof(hdev->gate2pipe));
}
static void nci_hci_reset_pipes_per_host(struct nci_dev *ndev, u8 host)
{
int i;
for (i = 0; i < NCI_HCI_MAX_PIPES; i++) {
if (ndev->hci_dev->pipes[i].host == host) {
ndev->hci_dev->pipes[i].gate = NCI_HCI_INVALID_GATE;
ndev->hci_dev->pipes[i].host = NCI_HCI_INVALID_HOST;
}
}
}
/* Fragment HCI data over NCI packet.
* NFC Forum NCI 10.2.2 Data Exchange:
* The payload of the Data Packets sent on the Logical Connection SHALL be
* valid HCP packets, as defined within [ETSI_102622]. Each Data Packet SHALL
* contain a single HCP packet. NCI Segmentation and Reassembly SHALL NOT be
* applied to Data Messages in either direction. The HCI fragmentation mechanism
* is used if required.
*/
static int nci_hci_send_data(struct nci_dev *ndev, u8 pipe,
const u8 data_type, const u8 *data,
size_t data_len)
{
const struct nci_conn_info *conn_info;
struct sk_buff *skb;
int len, i, r;
u8 cb = pipe;
conn_info = ndev->hci_dev->conn_info;
if (!conn_info)
return -EPROTO;
i = 0;
skb = nci_skb_alloc(ndev, conn_info->max_pkt_payload_len +
NCI_DATA_HDR_SIZE, GFP_ATOMIC);
if (!skb)
return -ENOMEM;
skb_reserve(skb, NCI_DATA_HDR_SIZE + 2);
*(u8 *)skb_push(skb, 1) = data_type;
do {
/* If last packet add NCI_HFP_NO_CHAINING */
if (i + conn_info->max_pkt_payload_len -
(skb->len + 1) >= data_len) {
cb |= NCI_HFP_NO_CHAINING;
len = data_len - i;
} else {
len = conn_info->max_pkt_payload_len - skb->len - 1;
}
*(u8 *)skb_push(skb, 1) = cb;
if (len > 0)
skb_put_data(skb, data + i, len);
r = nci_send_data(ndev, conn_info->conn_id, skb);
if (r < 0)
return r;
i += len;
if (i < data_len) {
skb = nci_skb_alloc(ndev,
conn_info->max_pkt_payload_len +
NCI_DATA_HDR_SIZE, GFP_ATOMIC);
if (!skb)
return -ENOMEM;
skb_reserve(skb, NCI_DATA_HDR_SIZE + 1);
}
} while (i < data_len);
return i;
}
static void nci_hci_send_data_req(struct nci_dev *ndev, const void *opt)
{
const struct nci_data *data = opt;
nci_hci_send_data(ndev, data->pipe, data->cmd,
data->data, data->data_len);
}
int nci_hci_send_event(struct nci_dev *ndev, u8 gate, u8 event,
const u8 *param, size_t param_len)
{
u8 pipe = ndev->hci_dev->gate2pipe[gate];
if (pipe == NCI_HCI_INVALID_PIPE)
return -EADDRNOTAVAIL;
return nci_hci_send_data(ndev, pipe,
NCI_HCP_HEADER(NCI_HCI_HCP_EVENT, event),
param, param_len);
}
EXPORT_SYMBOL(nci_hci_send_event);
int nci_hci_send_cmd(struct nci_dev *ndev, u8 gate, u8 cmd,
const u8 *param, size_t param_len,
struct sk_buff **skb)
{
const struct nci_hcp_message *message;
const struct nci_conn_info *conn_info;
struct nci_data data;
int r;
u8 pipe = ndev->hci_dev->gate2pipe[gate];
if (pipe == NCI_HCI_INVALID_PIPE)
return -EADDRNOTAVAIL;
conn_info = ndev->hci_dev->conn_info;
if (!conn_info)
return -EPROTO;
data.conn_id = conn_info->conn_id;
data.pipe = pipe;
data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND, cmd);
data.data = param;
data.data_len = param_len;
r = nci_request(ndev, nci_hci_send_data_req, &data,
msecs_to_jiffies(NCI_DATA_TIMEOUT));
if (r == NCI_STATUS_OK) {
message = (struct nci_hcp_message *)conn_info->rx_skb->data;
r = nci_hci_result_to_errno(
NCI_HCP_MSG_GET_CMD(message->header));
skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
if (!r && skb)
*skb = conn_info->rx_skb;
}
return r;
}
EXPORT_SYMBOL(nci_hci_send_cmd);
int nci_hci_clear_all_pipes(struct nci_dev *ndev)
{
int r;
r = nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
NCI_HCI_ADM_CLEAR_ALL_PIPE, NULL, 0, NULL);
if (r < 0)
return r;
nci_hci_reset_pipes(ndev->hci_dev);
return r;
}
EXPORT_SYMBOL(nci_hci_clear_all_pipes);
static void nci_hci_event_received(struct nci_dev *ndev, u8 pipe,
u8 event, struct sk_buff *skb)
{
if (ndev->ops->hci_event_received)
ndev->ops->hci_event_received(ndev, pipe, event, skb);
}
static void nci_hci_cmd_received(struct nci_dev *ndev, u8 pipe,
u8 cmd, struct sk_buff *skb)
{
u8 gate = ndev->hci_dev->pipes[pipe].gate;
u8 status = NCI_HCI_ANY_OK | ~NCI_HCI_FRAGMENT;
u8 dest_gate, new_pipe;
struct nci_hci_create_pipe_resp *create_info;
struct nci_hci_delete_pipe_noti *delete_info;
struct nci_hci_all_pipe_cleared_noti *cleared_info;
pr_debug("from gate %x pipe %x cmd %x\n", gate, pipe, cmd);
switch (cmd) {
case NCI_HCI_ADM_NOTIFY_PIPE_CREATED:
if (skb->len != 5) {
status = NCI_HCI_ANY_E_NOK;
goto exit;
}
create_info = (struct nci_hci_create_pipe_resp *)skb->data;
dest_gate = create_info->dest_gate;
new_pipe = create_info->pipe;
if (new_pipe >= NCI_HCI_MAX_PIPES) {
status = NCI_HCI_ANY_E_NOK;
goto exit;
}
/* Save the new created pipe and bind with local gate,
* the description for skb->data[3] is destination gate id
* but since we received this cmd from host controller, we
* are the destination and it is our local gate
*/
ndev->hci_dev->gate2pipe[dest_gate] = new_pipe;
ndev->hci_dev->pipes[new_pipe].gate = dest_gate;
ndev->hci_dev->pipes[new_pipe].host =
create_info->src_host;
break;
case NCI_HCI_ANY_OPEN_PIPE:
/* If the pipe is not created report an error */
if (gate == NCI_HCI_INVALID_GATE) {
status = NCI_HCI_ANY_E_NOK;
goto exit;
}
break;
case NCI_HCI_ADM_NOTIFY_PIPE_DELETED:
if (skb->len != 1) {
status = NCI_HCI_ANY_E_NOK;
goto exit;
}
delete_info = (struct nci_hci_delete_pipe_noti *)skb->data;
if (delete_info->pipe >= NCI_HCI_MAX_PIPES) {
status = NCI_HCI_ANY_E_NOK;
goto exit;
}
ndev->hci_dev->pipes[delete_info->pipe].gate =
NCI_HCI_INVALID_GATE;
ndev->hci_dev->pipes[delete_info->pipe].host =
NCI_HCI_INVALID_HOST;
break;
case NCI_HCI_ADM_NOTIFY_ALL_PIPE_CLEARED:
if (skb->len != 1) {
status = NCI_HCI_ANY_E_NOK;
goto exit;
}
cleared_info =
(struct nci_hci_all_pipe_cleared_noti *)skb->data;
nci_hci_reset_pipes_per_host(ndev, cleared_info->host);
break;
default:
pr_debug("Discarded unknown cmd %x to gate %x\n", cmd, gate);
break;
}
if (ndev->ops->hci_cmd_received)
ndev->ops->hci_cmd_received(ndev, pipe, cmd, skb);
exit:
nci_hci_send_data(ndev, pipe, status, NULL, 0);
kfree_skb(skb);
}
static void nci_hci_resp_received(struct nci_dev *ndev, u8 pipe,
struct sk_buff *skb)
{
struct nci_conn_info *conn_info;
conn_info = ndev->hci_dev->conn_info;
if (!conn_info)
goto exit;
conn_info->rx_skb = skb;
exit:
nci_req_complete(ndev, NCI_STATUS_OK);
}
/* Receive hcp message for pipe, with type and cmd.
* skb contains optional message data only.
*/
static void nci_hci_hcp_message_rx(struct nci_dev *ndev, u8 pipe,
u8 type, u8 instruction, struct sk_buff *skb)
{
switch (type) {
case NCI_HCI_HCP_RESPONSE:
nci_hci_resp_received(ndev, pipe, skb);
break;
case NCI_HCI_HCP_COMMAND:
nci_hci_cmd_received(ndev, pipe, instruction, skb);
break;
case NCI_HCI_HCP_EVENT:
nci_hci_event_received(ndev, pipe, instruction, skb);
break;
default:
pr_err("UNKNOWN MSG Type %d, instruction=%d\n",
type, instruction);
kfree_skb(skb);
break;
}
nci_req_complete(ndev, NCI_STATUS_OK);
}
static void nci_hci_msg_rx_work(struct work_struct *work)
{
struct nci_hci_dev *hdev =
container_of(work, struct nci_hci_dev, msg_rx_work);
struct sk_buff *skb;
const struct nci_hcp_message *message;
u8 pipe, type, instruction;
for (; (skb = skb_dequeue(&hdev->msg_rx_queue)); kcov_remote_stop()) {
kcov_remote_start_common(skb_get_kcov_handle(skb));
pipe = NCI_HCP_MSG_GET_PIPE(skb->data[0]);
skb_pull(skb, NCI_HCI_HCP_PACKET_HEADER_LEN);
message = (struct nci_hcp_message *)skb->data;
type = NCI_HCP_MSG_GET_TYPE(message->header);
instruction = NCI_HCP_MSG_GET_CMD(message->header);
skb_pull(skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
nci_hci_hcp_message_rx(hdev->ndev, pipe,
type, instruction, skb);
}
}
void nci_hci_data_received_cb(void *context,
struct sk_buff *skb, int err)
{
struct nci_dev *ndev = (struct nci_dev *)context;
struct nci_hcp_packet *packet;
u8 pipe, type;
struct sk_buff *hcp_skb;
struct sk_buff *frag_skb;
int msg_len;
if (err) {
nci_req_complete(ndev, err);
return;
}
if (!pskb_may_pull(skb, NCI_HCI_HCP_PACKET_HEADER_LEN)) {
kfree_skb(skb);
return;
}
packet = (struct nci_hcp_packet *)skb->data;
if ((packet->header & ~NCI_HCI_FRAGMENT) == 0) {
skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
return;
}
/* it's the last fragment. Does it need re-aggregation? */
if (skb_queue_len(&ndev->hci_dev->rx_hcp_frags)) {
pipe = NCI_HCP_MSG_GET_PIPE(packet->header);
skb_queue_tail(&ndev->hci_dev->rx_hcp_frags, skb);
msg_len = 0;
skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
msg_len += (frag_skb->len -
NCI_HCI_HCP_PACKET_HEADER_LEN);
}
hcp_skb = nfc_alloc_recv_skb(NCI_HCI_HCP_PACKET_HEADER_LEN +
msg_len, GFP_KERNEL);
if (!hcp_skb) {
nci_req_complete(ndev, -ENOMEM);
return;
}
skb_put_u8(hcp_skb, pipe);
skb_queue_walk(&ndev->hci_dev->rx_hcp_frags, frag_skb) {
msg_len = frag_skb->len - NCI_HCI_HCP_PACKET_HEADER_LEN;
skb_put_data(hcp_skb,
frag_skb->data + NCI_HCI_HCP_PACKET_HEADER_LEN,
msg_len);
}
skb_queue_purge(&ndev->hci_dev->rx_hcp_frags);
} else {
packet->header &= NCI_HCI_FRAGMENT;
hcp_skb = skb;
}
/* if this is a response, dispatch immediately to
* unblock waiting cmd context. Otherwise, enqueue to dispatch
* in separate context where handler can also execute command.
*/
if (!pskb_may_pull(hcp_skb, NCI_HCI_HCP_HEADER_LEN)) {
kfree_skb(hcp_skb);
return;
}
packet = (struct nci_hcp_packet *)hcp_skb->data;
type = NCI_HCP_MSG_GET_TYPE(packet->message.header);
if (type == NCI_HCI_HCP_RESPONSE) {
pipe = NCI_HCP_MSG_GET_PIPE(packet->header);
skb_pull(hcp_skb, NCI_HCI_HCP_PACKET_HEADER_LEN);
nci_hci_hcp_message_rx(ndev, pipe, type,
NCI_STATUS_OK, hcp_skb);
} else {
skb_queue_tail(&ndev->hci_dev->msg_rx_queue, hcp_skb);
schedule_work(&ndev->hci_dev->msg_rx_work);
}
}
int nci_hci_open_pipe(struct nci_dev *ndev, u8 pipe)
{
struct nci_data data;
const struct nci_conn_info *conn_info;
conn_info = ndev->hci_dev->conn_info;
if (!conn_info)
return -EPROTO;
data.conn_id = conn_info->conn_id;
data.pipe = pipe;
data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
NCI_HCI_ANY_OPEN_PIPE);
data.data = NULL;
data.data_len = 0;
return nci_request(ndev, nci_hci_send_data_req, &data,
msecs_to_jiffies(NCI_DATA_TIMEOUT));
}
EXPORT_SYMBOL(nci_hci_open_pipe);
static u8 nci_hci_create_pipe(struct nci_dev *ndev, u8 dest_host,
u8 dest_gate, int *result)
{
u8 pipe;
struct sk_buff *skb;
struct nci_hci_create_pipe_params params;
const struct nci_hci_create_pipe_resp *resp;
pr_debug("gate=%d\n", dest_gate);
params.src_gate = NCI_HCI_ADMIN_GATE;
params.dest_host = dest_host;
params.dest_gate = dest_gate;
*result = nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
NCI_HCI_ADM_CREATE_PIPE,
(u8 *)&params, sizeof(params), &skb);
if (*result < 0)
return NCI_HCI_INVALID_PIPE;
resp = (struct nci_hci_create_pipe_resp *)skb->data;
pipe = resp->pipe;
kfree_skb(skb);
pr_debug("pipe created=%d\n", pipe);
if (pipe >= NCI_HCI_MAX_PIPES)
pipe = NCI_HCI_INVALID_PIPE;
return pipe;
}
static int nci_hci_delete_pipe(struct nci_dev *ndev, u8 pipe)
{
return nci_hci_send_cmd(ndev, NCI_HCI_ADMIN_GATE,
NCI_HCI_ADM_DELETE_PIPE, &pipe, 1, NULL);
}
int nci_hci_set_param(struct nci_dev *ndev, u8 gate, u8 idx,
const u8 *param, size_t param_len)
{
const struct nci_hcp_message *message;
const struct nci_conn_info *conn_info;
struct nci_data data;
int r;
u8 *tmp;
u8 pipe = ndev->hci_dev->gate2pipe[gate];
pr_debug("idx=%d to gate %d\n", idx, gate);
if (pipe == NCI_HCI_INVALID_PIPE)
return -EADDRNOTAVAIL;
conn_info = ndev->hci_dev->conn_info;
if (!conn_info)
return -EPROTO;
tmp = kmalloc(1 + param_len, GFP_KERNEL);
if (!tmp)
return -ENOMEM;
*tmp = idx;
memcpy(tmp + 1, param, param_len);
data.conn_id = conn_info->conn_id;
data.pipe = pipe;
data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
NCI_HCI_ANY_SET_PARAMETER);
data.data = tmp;
data.data_len = param_len + 1;
r = nci_request(ndev, nci_hci_send_data_req, &data,
msecs_to_jiffies(NCI_DATA_TIMEOUT));
if (r == NCI_STATUS_OK) {
message = (struct nci_hcp_message *)conn_info->rx_skb->data;
r = nci_hci_result_to_errno(
NCI_HCP_MSG_GET_CMD(message->header));
skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
}
kfree(tmp);
return r;
}
EXPORT_SYMBOL(nci_hci_set_param);
int nci_hci_get_param(struct nci_dev *ndev, u8 gate, u8 idx,
struct sk_buff **skb)
{
const struct nci_hcp_message *message;
const struct nci_conn_info *conn_info;
struct nci_data data;
int r;
u8 pipe = ndev->hci_dev->gate2pipe[gate];
pr_debug("idx=%d to gate %d\n", idx, gate);
if (pipe == NCI_HCI_INVALID_PIPE)
return -EADDRNOTAVAIL;
conn_info = ndev->hci_dev->conn_info;
if (!conn_info)
return -EPROTO;
data.conn_id = conn_info->conn_id;
data.pipe = pipe;
data.cmd = NCI_HCP_HEADER(NCI_HCI_HCP_COMMAND,
NCI_HCI_ANY_GET_PARAMETER);
data.data = &idx;
data.data_len = 1;
r = nci_request(ndev, nci_hci_send_data_req, &data,
msecs_to_jiffies(NCI_DATA_TIMEOUT));
if (r == NCI_STATUS_OK) {
message = (struct nci_hcp_message *)conn_info->rx_skb->data;
r = nci_hci_result_to_errno(
NCI_HCP_MSG_GET_CMD(message->header));
skb_pull(conn_info->rx_skb, NCI_HCI_HCP_MESSAGE_HEADER_LEN);
if (!r && skb)
*skb = conn_info->rx_skb;
}
return r;
}
EXPORT_SYMBOL(nci_hci_get_param);
int nci_hci_connect_gate(struct nci_dev *ndev,
u8 dest_host, u8 dest_gate, u8 pipe)
{
bool pipe_created = false;
int r;
if (pipe == NCI_HCI_DO_NOT_OPEN_PIPE)
return 0;
if (ndev->hci_dev->gate2pipe[dest_gate] != NCI_HCI_INVALID_PIPE)
return -EADDRINUSE;
if (pipe != NCI_HCI_INVALID_PIPE)
goto open_pipe;
switch (dest_gate) {
case NCI_HCI_LINK_MGMT_GATE:
pipe = NCI_HCI_LINK_MGMT_PIPE;
break;
case NCI_HCI_ADMIN_GATE:
pipe = NCI_HCI_ADMIN_PIPE;
break;
default:
pipe = nci_hci_create_pipe(ndev, dest_host, dest_gate, &r);
if (pipe == NCI_HCI_INVALID_PIPE)
return r;
pipe_created = true;
break;
}
open_pipe:
r = nci_hci_open_pipe(ndev, pipe);
if (r < 0) {
if (pipe_created) {
if (nci_hci_delete_pipe(ndev, pipe) < 0) {
/* TODO: Cannot clean by deleting pipe...
* -> inconsistent state
*/
}
}
return r;
}
ndev->hci_dev->pipes[pipe].gate = dest_gate;
ndev->hci_dev->pipes[pipe].host = dest_host;
ndev->hci_dev->gate2pipe[dest_gate] = pipe;
return 0;
}
EXPORT_SYMBOL(nci_hci_connect_gate);
static int nci_hci_dev_connect_gates(struct nci_dev *ndev,
u8 gate_count,
const struct nci_hci_gate *gates)
{
int r;
while (gate_count--) {
r = nci_hci_connect_gate(ndev, gates->dest_host,
gates->gate, gates->pipe);
if (r < 0)
return r;
gates++;
}
return 0;
}
int nci_hci_dev_session_init(struct nci_dev *ndev)
{
struct nci_conn_info *conn_info;
struct sk_buff *skb;
int r;
ndev->hci_dev->count_pipes = 0;
ndev->hci_dev->expected_pipes = 0;
conn_info = ndev->hci_dev->conn_info;
if (!conn_info)
return -EPROTO;
conn_info->data_exchange_cb = nci_hci_data_received_cb;
conn_info->data_exchange_cb_context = ndev;
nci_hci_reset_pipes(ndev->hci_dev);
if (ndev->hci_dev->init_data.gates[0].gate != NCI_HCI_ADMIN_GATE)
return -EPROTO;
r = nci_hci_connect_gate(ndev,
ndev->hci_dev->init_data.gates[0].dest_host,
ndev->hci_dev->init_data.gates[0].gate,
ndev->hci_dev->init_data.gates[0].pipe);
if (r < 0)
return r;
r = nci_hci_get_param(ndev, NCI_HCI_ADMIN_GATE,
NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY, &skb);
if (r < 0)
return r;
if (skb->len &&
skb->len == strlen(ndev->hci_dev->init_data.session_id) &&
!memcmp(ndev->hci_dev->init_data.session_id, skb->data, skb->len) &&
ndev->ops->hci_load_session) {
/* Restore gate<->pipe table from some proprietary location. */
r = ndev->ops->hci_load_session(ndev);
} else {
r = nci_hci_clear_all_pipes(ndev);
if (r < 0)
goto exit;
r = nci_hci_dev_connect_gates(ndev,
ndev->hci_dev->init_data.gate_count,
ndev->hci_dev->init_data.gates);
if (r < 0)
goto exit;
r = nci_hci_set_param(ndev, NCI_HCI_ADMIN_GATE,
NCI_HCI_ADMIN_PARAM_SESSION_IDENTITY,
ndev->hci_dev->init_data.session_id,
strlen(ndev->hci_dev->init_data.session_id));
}
exit:
kfree_skb(skb);
return r;
}
EXPORT_SYMBOL(nci_hci_dev_session_init);
struct nci_hci_dev *nci_hci_allocate(struct nci_dev *ndev)
{
struct nci_hci_dev *hdev;
hdev = kzalloc_obj(*hdev);
if (!hdev)
return NULL;
skb_queue_head_init(&hdev->rx_hcp_frags);
INIT_WORK(&hdev->msg_rx_work, nci_hci_msg_rx_work);
skb_queue_head_init(&hdev->msg_rx_queue);
hdev->ndev = ndev;
return hdev;
}
void nci_hci_deallocate(struct nci_dev *ndev)
{
kfree(ndev->hci_dev);
}
+73
View File
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* The NFC Controller Interface is the communication protocol between an
* NFC Controller (NFCC) and a Device Host (DH).
*
* Copyright (C) 2011 Texas Instruments, Inc.
*
* Written by Ilan Elias <ilane@ti.com>
*
* Acknowledgements:
* This file is based on lib.c, which was written
* by Maxim Krasnyansky.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/errno.h>
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
/* NCI status codes to Unix errno mapping */
int nci_to_errno(__u8 code)
{
switch (code) {
case NCI_STATUS_OK:
return 0;
case NCI_STATUS_REJECTED:
return -EBUSY;
case NCI_STATUS_RF_FRAME_CORRUPTED:
return -EBADMSG;
case NCI_STATUS_NOT_INITIALIZED:
return -EHOSTDOWN;
case NCI_STATUS_SYNTAX_ERROR:
case NCI_STATUS_SEMANTIC_ERROR:
case NCI_STATUS_INVALID_PARAM:
case NCI_STATUS_RF_PROTOCOL_ERROR:
case NCI_STATUS_NFCEE_PROTOCOL_ERROR:
return -EPROTO;
case NCI_STATUS_UNKNOWN_GID:
case NCI_STATUS_UNKNOWN_OID:
return -EBADRQC;
case NCI_STATUS_MESSAGE_SIZE_EXCEEDED:
return -EMSGSIZE;
case NCI_STATUS_DISCOVERY_ALREADY_STARTED:
return -EALREADY;
case NCI_STATUS_DISCOVERY_TARGET_ACTIVATION_FAILED:
case NCI_STATUS_NFCEE_INTERFACE_ACTIVATION_FAILED:
return -ECONNREFUSED;
case NCI_STATUS_RF_TRANSMISSION_ERROR:
case NCI_STATUS_NFCEE_TRANSMISSION_ERROR:
return -ECOMM;
case NCI_STATUS_RF_TIMEOUT_ERROR:
case NCI_STATUS_NFCEE_TIMEOUT_ERROR:
return -ETIMEDOUT;
case NCI_STATUS_FAILED:
default:
return -ENOSYS;
}
}
EXPORT_SYMBOL(nci_to_errno);
File diff suppressed because it is too large Load Diff
+429
View File
@@ -0,0 +1,429 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* The NFC Controller Interface is the communication protocol between an
* NFC Controller (NFCC) and a Device Host (DH).
*
* Copyright (C) 2011 Texas Instruments, Inc.
*
* Written by Ilan Elias <ilane@ti.com>
*
* Acknowledgements:
* This file is based on hci_event.c, which was written
* by Maxim Krasnyansky.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": %s: " fmt, __func__
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/bitops.h>
#include <linux/skbuff.h>
#include "../nfc.h"
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
/* Handle NCI Response packets */
static void nci_core_reset_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
const struct nci_core_reset_rsp *rsp = (void *)skb->data;
pr_debug("status 0x%x\n", rsp->status);
/* Handle NCI 1.x ver */
if (skb->len != 1) {
if (rsp->status == NCI_STATUS_OK) {
ndev->nci_ver = rsp->nci_ver;
pr_debug("nci_ver 0x%x, config_status 0x%x\n",
rsp->nci_ver, rsp->config_status);
}
nci_req_complete(ndev, rsp->status);
}
}
static u8 nci_core_init_rsp_packet_v1(struct nci_dev *ndev,
const struct sk_buff *skb)
{
const struct nci_core_init_rsp_1 *rsp_1 = (void *)skb->data;
const struct nci_core_init_rsp_2 *rsp_2;
pr_debug("status 0x%x\n", rsp_1->status);
if (rsp_1->status != NCI_STATUS_OK)
return rsp_1->status;
ndev->nfcc_features = __le32_to_cpu(rsp_1->nfcc_features);
ndev->num_supported_rf_interfaces = rsp_1->num_supported_rf_interfaces;
ndev->num_supported_rf_interfaces =
min((int)ndev->num_supported_rf_interfaces,
NCI_MAX_SUPPORTED_RF_INTERFACES);
memcpy(ndev->supported_rf_interfaces,
rsp_1->supported_rf_interfaces,
ndev->num_supported_rf_interfaces);
rsp_2 = (void *) (skb->data + 6 + rsp_1->num_supported_rf_interfaces);
ndev->max_logical_connections = rsp_2->max_logical_connections;
ndev->max_routing_table_size =
__le16_to_cpu(rsp_2->max_routing_table_size);
ndev->max_ctrl_pkt_payload_len =
rsp_2->max_ctrl_pkt_payload_len;
ndev->max_size_for_large_params =
__le16_to_cpu(rsp_2->max_size_for_large_params);
ndev->manufact_id =
rsp_2->manufact_id;
ndev->manufact_specific_info =
__le32_to_cpu(rsp_2->manufact_specific_info);
return NCI_STATUS_OK;
}
static u8 nci_core_init_rsp_packet_v2(struct nci_dev *ndev,
const struct sk_buff *skb)
{
const struct nci_core_init_rsp_nci_ver2 *rsp = (void *)skb->data;
const u8 *supported_rf_interface = rsp->supported_rf_interfaces;
u8 rf_interface_idx = 0;
u8 rf_extension_cnt = 0;
pr_debug("status %x\n", rsp->status);
if (rsp->status != NCI_STATUS_OK)
return rsp->status;
ndev->nfcc_features = __le32_to_cpu(rsp->nfcc_features);
ndev->num_supported_rf_interfaces = rsp->num_supported_rf_interfaces;
ndev->num_supported_rf_interfaces =
min((int)ndev->num_supported_rf_interfaces,
NCI_MAX_SUPPORTED_RF_INTERFACES);
while (rf_interface_idx < ndev->num_supported_rf_interfaces) {
ndev->supported_rf_interfaces[rf_interface_idx++] = *supported_rf_interface++;
/* skip rf extension parameters */
rf_extension_cnt = *supported_rf_interface++;
supported_rf_interface += rf_extension_cnt;
}
ndev->max_logical_connections = rsp->max_logical_connections;
ndev->max_routing_table_size =
__le16_to_cpu(rsp->max_routing_table_size);
ndev->max_ctrl_pkt_payload_len =
rsp->max_ctrl_pkt_payload_len;
ndev->max_size_for_large_params = NCI_MAX_LARGE_PARAMS_NCI_v2;
return NCI_STATUS_OK;
}
static void nci_core_init_rsp_packet(struct nci_dev *ndev, const struct sk_buff *skb)
{
u8 status = 0;
if (!(ndev->nci_ver & NCI_VER_2_MASK))
status = nci_core_init_rsp_packet_v1(ndev, skb);
else
status = nci_core_init_rsp_packet_v2(ndev, skb);
if (status != NCI_STATUS_OK)
goto exit;
pr_debug("nfcc_features 0x%x\n",
ndev->nfcc_features);
pr_debug("num_supported_rf_interfaces %d\n",
ndev->num_supported_rf_interfaces);
pr_debug("supported_rf_interfaces[0] 0x%x\n",
ndev->supported_rf_interfaces[0]);
pr_debug("supported_rf_interfaces[1] 0x%x\n",
ndev->supported_rf_interfaces[1]);
pr_debug("supported_rf_interfaces[2] 0x%x\n",
ndev->supported_rf_interfaces[2]);
pr_debug("supported_rf_interfaces[3] 0x%x\n",
ndev->supported_rf_interfaces[3]);
pr_debug("max_logical_connections %d\n",
ndev->max_logical_connections);
pr_debug("max_routing_table_size %d\n",
ndev->max_routing_table_size);
pr_debug("max_ctrl_pkt_payload_len %d\n",
ndev->max_ctrl_pkt_payload_len);
pr_debug("max_size_for_large_params %d\n",
ndev->max_size_for_large_params);
pr_debug("manufact_id 0x%x\n",
ndev->manufact_id);
pr_debug("manufact_specific_info 0x%x\n",
ndev->manufact_specific_info);
exit:
nci_req_complete(ndev, status);
}
static void nci_core_set_config_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
const struct nci_core_set_config_rsp *rsp = (void *)skb->data;
pr_debug("status 0x%x\n", rsp->status);
nci_req_complete(ndev, rsp->status);
}
static void nci_rf_disc_map_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
__u8 status = skb->data[0];
pr_debug("status 0x%x\n", status);
nci_req_complete(ndev, status);
}
static void nci_rf_disc_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
struct nci_conn_info *conn_info;
__u8 status = skb->data[0];
pr_debug("status 0x%x\n", status);
if (status == NCI_STATUS_OK) {
atomic_set(&ndev->state, NCI_DISCOVERY);
conn_info = ndev->rf_conn_info;
if (!conn_info) {
conn_info = devm_kzalloc(&ndev->nfc_dev->dev,
sizeof(struct nci_conn_info),
GFP_KERNEL);
if (!conn_info) {
status = NCI_STATUS_REJECTED;
goto exit;
}
conn_info->conn_id = NCI_STATIC_RF_CONN_ID;
INIT_LIST_HEAD(&conn_info->list);
list_add(&conn_info->list, &ndev->conn_info_list);
ndev->rf_conn_info = conn_info;
}
}
exit:
nci_req_complete(ndev, status);
}
static void nci_rf_disc_select_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
__u8 status = skb->data[0];
pr_debug("status 0x%x\n", status);
/* Complete the request on intf_activated_ntf or generic_error_ntf */
if (status != NCI_STATUS_OK)
nci_req_complete(ndev, status);
}
static void nci_rf_deactivate_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
__u8 status = skb->data[0];
pr_debug("status 0x%x\n", status);
/* If target was active, complete the request only in deactivate_ntf */
if ((status != NCI_STATUS_OK) ||
(atomic_read(&ndev->state) != NCI_POLL_ACTIVE)) {
nci_clear_target_list(ndev);
atomic_set(&ndev->state, NCI_IDLE);
nci_req_complete(ndev, status);
}
}
static void nci_nfcee_discover_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
const struct nci_nfcee_discover_rsp *discover_rsp;
if (skb->len != 2) {
nci_req_complete(ndev, NCI_STATUS_NFCEE_PROTOCOL_ERROR);
return;
}
discover_rsp = (struct nci_nfcee_discover_rsp *)skb->data;
if (discover_rsp->status != NCI_STATUS_OK ||
discover_rsp->num_nfcee == 0)
nci_req_complete(ndev, discover_rsp->status);
}
static void nci_nfcee_mode_set_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
__u8 status = skb->data[0];
pr_debug("status 0x%x\n", status);
nci_req_complete(ndev, status);
}
static void nci_core_conn_create_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
__u8 status = skb->data[0];
struct nci_conn_info *conn_info = NULL;
const struct nci_core_conn_create_rsp *rsp;
pr_debug("status 0x%x\n", status);
if (status == NCI_STATUS_OK) {
rsp = (struct nci_core_conn_create_rsp *)skb->data;
conn_info = devm_kzalloc(&ndev->nfc_dev->dev,
sizeof(*conn_info), GFP_KERNEL);
if (!conn_info) {
status = NCI_STATUS_REJECTED;
goto exit;
}
conn_info->dest_params = devm_kzalloc(&ndev->nfc_dev->dev,
sizeof(struct dest_spec_params),
GFP_KERNEL);
if (!conn_info->dest_params) {
status = NCI_STATUS_REJECTED;
goto free_conn_info;
}
conn_info->dest_type = ndev->cur_dest_type;
conn_info->dest_params->id = ndev->cur_params.id;
conn_info->dest_params->protocol = ndev->cur_params.protocol;
conn_info->conn_id = rsp->conn_id;
/* Note: data_exchange_cb and data_exchange_cb_context need to
* be specify out of nci_core_conn_create_rsp_packet
*/
INIT_LIST_HEAD(&conn_info->list);
list_add(&conn_info->list, &ndev->conn_info_list);
if (ndev->cur_params.id == ndev->hci_dev->nfcee_id)
ndev->hci_dev->conn_info = conn_info;
conn_info->conn_id = rsp->conn_id;
conn_info->max_pkt_payload_len = rsp->max_ctrl_pkt_payload_len;
atomic_set(&conn_info->credits_cnt, rsp->credits_cnt);
}
free_conn_info:
if (status == NCI_STATUS_REJECTED)
devm_kfree(&ndev->nfc_dev->dev, conn_info);
exit:
nci_req_complete(ndev, status);
}
static void nci_core_conn_close_rsp_packet(struct nci_dev *ndev,
const struct sk_buff *skb)
{
struct nci_conn_info *conn_info;
__u8 status = skb->data[0];
pr_debug("status 0x%x\n", status);
if (status == NCI_STATUS_OK) {
conn_info = nci_get_conn_info_by_conn_id(ndev,
ndev->cur_conn_id);
if (conn_info) {
list_del(&conn_info->list);
if (conn_info == ndev->rf_conn_info)
ndev->rf_conn_info = NULL;
devm_kfree(&ndev->nfc_dev->dev, conn_info->dest_params);
devm_kfree(&ndev->nfc_dev->dev, conn_info);
}
}
nci_req_complete(ndev, status);
}
void nci_rsp_packet(struct nci_dev *ndev, struct sk_buff *skb)
{
__u16 rsp_opcode = nci_opcode(skb->data);
/* we got a rsp, stop the cmd timer */
timer_delete(&ndev->cmd_timer);
pr_debug("NCI RX: MT=rsp, PBF=%d, GID=0x%x, OID=0x%x, plen=%d\n",
nci_pbf(skb->data),
nci_opcode_gid(rsp_opcode),
nci_opcode_oid(rsp_opcode),
nci_plen(skb->data));
/* strip the nci control header */
skb_pull(skb, NCI_CTRL_HDR_SIZE);
if (nci_opcode_gid(rsp_opcode) == NCI_GID_PROPRIETARY) {
if (nci_prop_rsp_packet(ndev, rsp_opcode, skb) == -ENOTSUPP) {
pr_err("unsupported rsp opcode 0x%x\n",
rsp_opcode);
}
goto end;
}
switch (rsp_opcode) {
case NCI_OP_CORE_RESET_RSP:
nci_core_reset_rsp_packet(ndev, skb);
break;
case NCI_OP_CORE_INIT_RSP:
nci_core_init_rsp_packet(ndev, skb);
break;
case NCI_OP_CORE_SET_CONFIG_RSP:
nci_core_set_config_rsp_packet(ndev, skb);
break;
case NCI_OP_CORE_CONN_CREATE_RSP:
nci_core_conn_create_rsp_packet(ndev, skb);
break;
case NCI_OP_CORE_CONN_CLOSE_RSP:
nci_core_conn_close_rsp_packet(ndev, skb);
break;
case NCI_OP_RF_DISCOVER_MAP_RSP:
nci_rf_disc_map_rsp_packet(ndev, skb);
break;
case NCI_OP_RF_DISCOVER_RSP:
nci_rf_disc_rsp_packet(ndev, skb);
break;
case NCI_OP_RF_DISCOVER_SELECT_RSP:
nci_rf_disc_select_rsp_packet(ndev, skb);
break;
case NCI_OP_RF_DEACTIVATE_RSP:
nci_rf_deactivate_rsp_packet(ndev, skb);
break;
case NCI_OP_NFCEE_DISCOVER_RSP:
nci_nfcee_discover_rsp_packet(ndev, skb);
break;
case NCI_OP_NFCEE_MODE_SET_RSP:
nci_nfcee_mode_set_rsp_packet(ndev, skb);
break;
default:
pr_err("unknown rsp opcode 0x%x\n", rsp_opcode);
break;
}
nci_core_rsp_packet(ndev, rsp_opcode, skb);
end:
kfree_skb(skb);
/* trigger the next cmd */
atomic_set(&ndev->cmd_cnt, 1);
if (!skb_queue_empty(&ndev->cmd_q))
queue_work(ndev->cmd_wq, &ndev->cmd_work);
}
+323
View File
@@ -0,0 +1,323 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2013 Intel Corporation. All rights reserved.
*/
#define pr_fmt(fmt) "nci_spi: %s: " fmt, __func__
#include <linux/module.h>
#include <linux/export.h>
#include <linux/spi/spi.h>
#include <linux/crc-ccitt.h>
#include <net/nfc/nci_core.h>
#define NCI_SPI_ACK_SHIFT 6
#define NCI_SPI_MSB_PAYLOAD_MASK 0x3F
#define NCI_SPI_SEND_TIMEOUT (NCI_CMD_TIMEOUT > NCI_DATA_TIMEOUT ? \
NCI_CMD_TIMEOUT : NCI_DATA_TIMEOUT)
#define NCI_SPI_DIRECT_WRITE 0x01
#define NCI_SPI_DIRECT_READ 0x02
#define ACKNOWLEDGE_NONE 0
#define ACKNOWLEDGE_ACK 1
#define ACKNOWLEDGE_NACK 2
#define CRC_INIT 0xFFFF
static int __nci_spi_send(struct nci_spi *nspi, const struct sk_buff *skb,
int cs_change)
{
struct spi_message m;
struct spi_transfer t;
memset(&t, 0, sizeof(struct spi_transfer));
/* a NULL skb means we just want the SPI chip select line to raise */
if (skb) {
t.tx_buf = skb->data;
t.len = skb->len;
} else {
/* still set tx_buf non NULL to make the driver happy */
t.tx_buf = &t;
t.len = 0;
}
t.cs_change = cs_change;
t.delay.value = nspi->xfer_udelay;
t.delay.unit = SPI_DELAY_UNIT_USECS;
t.speed_hz = nspi->xfer_speed_hz;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return spi_sync(nspi->spi, &m);
}
int nci_spi_send(struct nci_spi *nspi,
struct completion *write_handshake_completion,
struct sk_buff *skb)
{
unsigned int payload_len = skb->len;
unsigned char *hdr;
int ret;
long completion_rc;
/* add the NCI SPI header to the start of the buffer */
hdr = skb_push(skb, NCI_SPI_HDR_LEN);
hdr[0] = NCI_SPI_DIRECT_WRITE;
hdr[1] = nspi->acknowledge_mode;
hdr[2] = payload_len >> 8;
hdr[3] = payload_len & 0xFF;
if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
u16 crc;
crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
skb_put_u8(skb, crc >> 8);
skb_put_u8(skb, crc & 0xFF);
}
if (write_handshake_completion) {
/* Trick SPI driver to raise chip select */
ret = __nci_spi_send(nspi, NULL, 1);
if (ret)
goto done;
/* wait for NFC chip hardware handshake to complete */
if (wait_for_completion_timeout(write_handshake_completion,
msecs_to_jiffies(1000)) == 0) {
ret = -ETIME;
goto done;
}
}
ret = __nci_spi_send(nspi, skb, 0);
if (ret != 0 || nspi->acknowledge_mode == NCI_SPI_CRC_DISABLED)
goto done;
reinit_completion(&nspi->req_completion);
completion_rc = wait_for_completion_interruptible_timeout(
&nspi->req_completion,
NCI_SPI_SEND_TIMEOUT);
if (completion_rc <= 0 || nspi->req_result == ACKNOWLEDGE_NACK)
ret = -EIO;
done:
kfree_skb(skb);
return ret;
}
EXPORT_SYMBOL_GPL(nci_spi_send);
/* ---- Interface to NCI SPI drivers ---- */
/**
* nci_spi_allocate_spi - allocate a new nci spi
*
* @spi: SPI device
* @acknowledge_mode: Acknowledge mode used by the NFC device
* @delay: delay between transactions in us
* @ndev: nci dev to send incoming nci frames to
*/
struct nci_spi *nci_spi_allocate_spi(struct spi_device *spi,
u8 acknowledge_mode, unsigned int delay,
struct nci_dev *ndev)
{
struct nci_spi *nspi;
nspi = devm_kzalloc(&spi->dev, sizeof(struct nci_spi), GFP_KERNEL);
if (!nspi)
return NULL;
nspi->acknowledge_mode = acknowledge_mode;
nspi->xfer_udelay = delay;
/* Use controller max SPI speed by default */
nspi->xfer_speed_hz = 0;
nspi->spi = spi;
nspi->ndev = ndev;
init_completion(&nspi->req_completion);
return nspi;
}
EXPORT_SYMBOL_GPL(nci_spi_allocate_spi);
static int send_acknowledge(struct nci_spi *nspi, u8 acknowledge)
{
struct sk_buff *skb;
unsigned char *hdr;
u16 crc;
int ret;
skb = nci_skb_alloc(nspi->ndev, 0, GFP_KERNEL);
if (!skb)
return -ENOMEM;
/* add the NCI SPI header to the start of the buffer */
hdr = skb_push(skb, NCI_SPI_HDR_LEN);
hdr[0] = NCI_SPI_DIRECT_WRITE;
hdr[1] = NCI_SPI_CRC_ENABLED;
hdr[2] = acknowledge << NCI_SPI_ACK_SHIFT;
hdr[3] = 0;
crc = crc_ccitt(CRC_INIT, skb->data, skb->len);
skb_put_u8(skb, crc >> 8);
skb_put_u8(skb, crc & 0xFF);
ret = __nci_spi_send(nspi, skb, 0);
kfree_skb(skb);
return ret;
}
static struct sk_buff *__nci_spi_read(struct nci_spi *nspi)
{
struct sk_buff *skb;
struct spi_message m;
unsigned char req[2], resp_hdr[2];
struct spi_transfer tx, rx;
unsigned short rx_len = 0;
int ret;
spi_message_init(&m);
memset(&tx, 0, sizeof(struct spi_transfer));
req[0] = NCI_SPI_DIRECT_READ;
req[1] = nspi->acknowledge_mode;
tx.tx_buf = req;
tx.len = 2;
tx.cs_change = 0;
tx.speed_hz = nspi->xfer_speed_hz;
spi_message_add_tail(&tx, &m);
memset(&rx, 0, sizeof(struct spi_transfer));
rx.rx_buf = resp_hdr;
rx.len = 2;
rx.cs_change = 1;
rx.speed_hz = nspi->xfer_speed_hz;
spi_message_add_tail(&rx, &m);
ret = spi_sync(nspi->spi, &m);
if (ret)
return NULL;
if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
rx_len = ((resp_hdr[0] & NCI_SPI_MSB_PAYLOAD_MASK) << 8) +
resp_hdr[1] + NCI_SPI_CRC_LEN;
else
rx_len = (resp_hdr[0] << 8) | resp_hdr[1];
skb = nci_skb_alloc(nspi->ndev, rx_len, GFP_KERNEL);
if (!skb)
return NULL;
spi_message_init(&m);
memset(&rx, 0, sizeof(struct spi_transfer));
rx.rx_buf = skb_put(skb, rx_len);
rx.len = rx_len;
rx.cs_change = 0;
rx.delay.value = nspi->xfer_udelay;
rx.delay.unit = SPI_DELAY_UNIT_USECS;
rx.speed_hz = nspi->xfer_speed_hz;
spi_message_add_tail(&rx, &m);
ret = spi_sync(nspi->spi, &m);
if (ret)
goto receive_error;
if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
*(u8 *)skb_push(skb, 1) = resp_hdr[1];
*(u8 *)skb_push(skb, 1) = resp_hdr[0];
}
return skb;
receive_error:
kfree_skb(skb);
return NULL;
}
static int nci_spi_check_crc(struct sk_buff *skb)
{
u16 crc_data = (skb->data[skb->len - 2] << 8) |
skb->data[skb->len - 1];
int ret;
ret = (crc_ccitt(CRC_INIT, skb->data, skb->len - NCI_SPI_CRC_LEN)
== crc_data);
skb_trim(skb, skb->len - NCI_SPI_CRC_LEN);
return ret;
}
static u8 nci_spi_get_ack(struct sk_buff *skb)
{
u8 ret;
ret = skb->data[0] >> NCI_SPI_ACK_SHIFT;
/* Remove NFCC part of the header: ACK, NACK and MSB payload len */
skb_pull(skb, 2);
return ret;
}
/**
* nci_spi_read - read frame from NCI SPI drivers
*
* @nspi: The nci spi
* Context: can sleep
*
* This call may only be used from a context that may sleep. The sleep
* is non-interruptible, and has no timeout.
*
* It returns an allocated skb containing the frame on success, or NULL.
*/
struct sk_buff *nci_spi_read(struct nci_spi *nspi)
{
struct sk_buff *skb;
/* Retrieve frame from SPI */
skb = __nci_spi_read(nspi);
if (!skb)
goto done;
if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED) {
if (!nci_spi_check_crc(skb)) {
send_acknowledge(nspi, ACKNOWLEDGE_NACK);
goto done;
}
/* In case of acknowledged mode: if ACK or NACK received,
* unblock completion of latest frame sent.
*/
nspi->req_result = nci_spi_get_ack(skb);
if (nspi->req_result)
complete(&nspi->req_completion);
}
/* If there is no payload (ACK/NACK only frame),
* free the socket buffer
*/
if (!skb->len) {
kfree_skb(skb);
skb = NULL;
goto done;
}
if (nspi->acknowledge_mode == NCI_SPI_CRC_ENABLED)
send_acknowledge(nspi, ACKNOWLEDGE_ACK);
done:
return skb;
}
EXPORT_SYMBOL_GPL(nci_spi_read);
MODULE_DESCRIPTION("NFC Controller Interface (NCI) SPI link layer");
MODULE_LICENSE("GPL");
+454
View File
@@ -0,0 +1,454 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2015, Marvell International Ltd.
*
* Inspired (hugely) by HCI LDISC implementation in Bluetooth.
*
* Copyright (C) 2000-2001 Qualcomm Incorporated
* Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
* Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/poll.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/signal.h>
#include <linux/ioctl.h>
#include <linux/skbuff.h>
#include <net/nfc/nci.h>
#include <net/nfc/nci_core.h>
/* TX states */
#define NCI_UART_SENDING 1
#define NCI_UART_TX_WAKEUP 2
static struct nci_uart *nci_uart_drivers[NCI_UART_DRIVER_MAX];
static inline struct sk_buff *nci_uart_dequeue(struct nci_uart *nu)
{
struct sk_buff *skb = nu->tx_skb;
if (!skb)
skb = skb_dequeue(&nu->tx_q);
else
nu->tx_skb = NULL;
return skb;
}
static inline int nci_uart_queue_empty(struct nci_uart *nu)
{
if (nu->tx_skb)
return 0;
return skb_queue_empty(&nu->tx_q);
}
static int nci_uart_tx_wakeup(struct nci_uart *nu)
{
if (test_and_set_bit(NCI_UART_SENDING, &nu->tx_state)) {
set_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
return 0;
}
schedule_work(&nu->write_work);
return 0;
}
static void nci_uart_write_work(struct work_struct *work)
{
struct nci_uart *nu = container_of(work, struct nci_uart, write_work);
struct tty_struct *tty = nu->tty;
struct sk_buff *skb;
restart:
clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
if (nu->ops.tx_start)
nu->ops.tx_start(nu);
while ((skb = nci_uart_dequeue(nu))) {
int len;
set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
len = tty->ops->write(tty, skb->data, skb->len);
skb_pull(skb, len);
if (skb->len) {
nu->tx_skb = skb;
break;
}
kfree_skb(skb);
}
if (test_bit(NCI_UART_TX_WAKEUP, &nu->tx_state))
goto restart;
if (nu->ops.tx_done && nci_uart_queue_empty(nu))
nu->ops.tx_done(nu);
clear_bit(NCI_UART_SENDING, &nu->tx_state);
}
static int nci_uart_set_driver(struct tty_struct *tty, unsigned int driver)
{
struct nci_uart *nu = NULL;
int ret;
if (driver >= NCI_UART_DRIVER_MAX)
return -EINVAL;
if (!nci_uart_drivers[driver])
return -ENOENT;
nu = kzalloc_obj(*nu);
if (!nu)
return -ENOMEM;
memcpy(nu, nci_uart_drivers[driver], sizeof(struct nci_uart));
nu->tty = tty;
skb_queue_head_init(&nu->tx_q);
INIT_WORK(&nu->write_work, nci_uart_write_work);
spin_lock_init(&nu->rx_lock);
ret = nu->ops.open(nu);
if (ret) {
kfree(nu);
return ret;
} else if (!try_module_get(nu->owner)) {
nu->ops.close(nu);
kfree(nu);
return -ENOENT;
}
tty->disc_data = nu;
return 0;
}
/* ------ LDISC part ------ */
/* nci_uart_tty_open
*
* Called when line discipline changed to NCI_UART.
*
* Arguments:
* tty pointer to tty info structure
* Return Value:
* 0 if success, otherwise error code
*/
static int nci_uart_tty_open(struct tty_struct *tty)
{
/* Error if the tty has no write op instead of leaving an exploitable
* hole
*/
if (!tty->ops->write)
return -EOPNOTSUPP;
tty->disc_data = NULL;
tty->receive_room = 65536;
/* Flush any pending characters in the driver */
tty_driver_flush_buffer(tty);
return 0;
}
/* nci_uart_tty_close()
*
* Called when the line discipline is changed to something
* else, the tty is closed, or the tty detects a hangup.
*/
static void nci_uart_tty_close(struct tty_struct *tty)
{
struct nci_uart *nu = tty->disc_data;
/* Detach from the tty */
tty->disc_data = NULL;
if (!nu)
return;
kfree_skb(nu->tx_skb);
kfree_skb(nu->rx_skb);
skb_queue_purge(&nu->tx_q);
nu->ops.close(nu);
nu->tty = NULL;
module_put(nu->owner);
cancel_work_sync(&nu->write_work);
kfree(nu);
}
/* nci_uart_tty_wakeup()
*
* Callback for transmit wakeup. Called when low level
* device driver can accept more send data.
*
* Arguments: tty pointer to associated tty instance data
* Return Value: None
*/
static void nci_uart_tty_wakeup(struct tty_struct *tty)
{
struct nci_uart *nu = tty->disc_data;
if (!nu)
return;
clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
if (tty != nu->tty)
return;
nci_uart_tx_wakeup(nu);
}
/* -- Default recv_buf handler --
*
* This handler supposes that NCI frames are sent over UART link without any
* framing. It reads NCI header, retrieve the packet size and once all packet
* bytes are received it passes it to nci_uart driver for processing.
*/
static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
int count)
{
int chunk_len;
if (!nu->ndev) {
nfc_err(nu->tty->dev,
"receive data from tty but no NCI dev is attached yet, drop buffer\n");
return 0;
}
/* Decode all incoming data in packets
* and enqueue then for processing.
*/
while (count > 0) {
/* If this is the first data of a packet, allocate a buffer */
if (!nu->rx_skb) {
nu->rx_packet_len = -1;
nu->rx_skb = nci_skb_alloc(nu->ndev,
NCI_MAX_PACKET_SIZE,
GFP_ATOMIC);
if (!nu->rx_skb)
return -ENOMEM;
}
/* Eat byte after byte till full packet header is received */
if (nu->rx_skb->len < NCI_CTRL_HDR_SIZE) {
skb_put_u8(nu->rx_skb, *data++);
--count;
continue;
}
/* Header was received but packet len was not read */
if (nu->rx_packet_len < 0)
nu->rx_packet_len = NCI_CTRL_HDR_SIZE +
nci_plen(nu->rx_skb->data);
/* Compute how many bytes are missing and how many bytes can
* be consumed.
*/
chunk_len = nu->rx_packet_len - nu->rx_skb->len;
if (count < chunk_len)
chunk_len = count;
skb_put_data(nu->rx_skb, data, chunk_len);
data += chunk_len;
count -= chunk_len;
/* Check if packet is fully received */
if (nu->rx_packet_len == nu->rx_skb->len) {
/* Pass RX packet to driver */
if (nu->ops.recv(nu, nu->rx_skb) != 0)
nfc_err(nu->tty->dev, "corrupted RX packet\n");
/* Next packet will be a new one */
nu->rx_skb = NULL;
}
}
return 0;
}
/* nci_uart_tty_receive()
*
* Called by tty low level driver when receive data is
* available.
*
* Arguments: tty pointer to tty instance data
* data pointer to received data
* flags pointer to flags for data
* count count of received data in bytes
*
* Return Value: None
*/
static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
const u8 *flags, size_t count)
{
struct nci_uart *nu = tty->disc_data;
if (!nu || tty != nu->tty)
return;
spin_lock(&nu->rx_lock);
nci_uart_default_recv_buf(nu, data, count);
spin_unlock(&nu->rx_lock);
tty_unthrottle(tty);
}
/* nci_uart_tty_ioctl()
*
* Process IOCTL system call for the tty device.
*
* Arguments:
*
* tty pointer to tty instance data
* cmd IOCTL command code
* arg argument for IOCTL call (cmd dependent)
*
* Return Value: Command dependent
*/
static int nci_uart_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
unsigned long arg)
{
struct nci_uart *nu = tty->disc_data;
int err = 0;
switch (cmd) {
case NCIUARTSETDRIVER:
if (!nu)
return nci_uart_set_driver(tty, (unsigned int)arg);
else
return -EBUSY;
break;
default:
err = n_tty_ioctl_helper(tty, cmd, arg);
break;
}
return err;
}
/* We don't provide read/write/poll interface for user space. */
static ssize_t nci_uart_tty_read(struct tty_struct *tty, struct file *file,
u8 *buf, size_t nr, void **cookie,
unsigned long offset)
{
return 0;
}
static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file,
const u8 *data, size_t count)
{
return 0;
}
static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
{
/* Queue TX packet */
skb_queue_tail(&nu->tx_q, skb);
/* Try to start TX (if possible) */
nci_uart_tx_wakeup(nu);
return 0;
}
int nci_uart_register(struct nci_uart *nu)
{
if (!nu || !nu->ops.open ||
!nu->ops.recv || !nu->ops.close)
return -EINVAL;
/* Set the send callback */
nu->ops.send = nci_uart_send;
/* Add this driver in the driver list */
if (nci_uart_drivers[nu->driver]) {
pr_err("driver %d is already registered\n", nu->driver);
return -EBUSY;
}
nci_uart_drivers[nu->driver] = nu;
pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
return 0;
}
EXPORT_SYMBOL_GPL(nci_uart_register);
void nci_uart_unregister(struct nci_uart *nu)
{
pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
nu->driver);
/* Remove this driver from the driver list */
nci_uart_drivers[nu->driver] = NULL;
}
EXPORT_SYMBOL_GPL(nci_uart_unregister);
void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
{
struct ktermios new_termios;
if (!nu->tty)
return;
down_read(&nu->tty->termios_rwsem);
new_termios = nu->tty->termios;
up_read(&nu->tty->termios_rwsem);
tty_termios_encode_baud_rate(&new_termios, baudrate, baudrate);
if (flow_ctrl)
new_termios.c_cflag |= CRTSCTS;
else
new_termios.c_cflag &= ~CRTSCTS;
tty_set_termios(nu->tty, &new_termios);
}
EXPORT_SYMBOL_GPL(nci_uart_set_config);
static const struct tty_ldisc_ops nci_uart_ldisc = {
.owner = THIS_MODULE,
.num = N_NCI,
.name = "n_nci",
.open = nci_uart_tty_open,
.close = nci_uart_tty_close,
.read = nci_uart_tty_read,
.write = nci_uart_tty_write,
.receive_buf = nci_uart_tty_receive,
.write_wakeup = nci_uart_tty_wakeup,
.ioctl = nci_uart_tty_ioctl,
.compat_ioctl = nci_uart_tty_ioctl,
};
static int __init nci_uart_init(void)
{
return tty_register_ldisc(&nci_uart_ldisc);
}
static void __exit nci_uart_exit(void)
{
tty_unregister_ldisc(&nci_uart_ldisc);
}
module_init(nci_uart_init);
module_exit(nci_uart_exit);
MODULE_AUTHOR("Marvell International Ltd.");
MODULE_DESCRIPTION("NFC NCI UART driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS_LDISC(N_NCI);