2021-12-08 05:08:25 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* drivers/ioexpander/ioe_rpmsg.c
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2022-01-06 14:56:22 +01:00
|
|
|
#include <nuttx/ioexpander/ioe_rpmsg.h>
|
2021-12-08 05:08:25 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/nuttx.h>
|
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
#include <nuttx/rptun/openamp.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef ARRAY_SIZE
|
|
|
|
# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define IOE_RPMSG_EPT_FORMAT "rpmsg-ioe-%s"
|
|
|
|
|
|
|
|
#define IOE_RPMSG_DIRECTION 0
|
|
|
|
#define IOE_RPMSG_OPTION 1
|
|
|
|
#define IOE_RPMSG_WRITEPIN 2
|
|
|
|
#define IOE_RPMSG_READPIN 3
|
|
|
|
#define IOE_RPMSG_ATTACH 4
|
|
|
|
#define IOE_RPMSG_DETACH 5
|
|
|
|
#define IOE_RPMSG_IRQ 6
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct ioe_rpmsg_cookie_s
|
|
|
|
{
|
|
|
|
int32_t result;
|
|
|
|
sem_t sem;
|
|
|
|
};
|
|
|
|
|
|
|
|
begin_packed_struct struct ioe_rpmsg_header_s
|
|
|
|
{
|
|
|
|
uint32_t command : 31;
|
|
|
|
uint32_t response : 1;
|
|
|
|
int32_t result;
|
|
|
|
uint64_t cookie;
|
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
begin_packed_struct struct ioe_rpmsg_direction_s
|
|
|
|
{
|
|
|
|
struct ioe_rpmsg_header_s header;
|
|
|
|
uint8_t pin;
|
|
|
|
uint8_t val;
|
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
#define ioe_rpmsg_writepin_s ioe_rpmsg_direction_s
|
|
|
|
|
|
|
|
begin_packed_struct struct ioe_rpmsg_option_s
|
|
|
|
{
|
|
|
|
struct ioe_rpmsg_header_s header;
|
2021-12-14 07:28:03 +01:00
|
|
|
uint64_t val;
|
2021-12-08 05:08:25 +01:00
|
|
|
uint32_t opt;
|
|
|
|
uint8_t pin;
|
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
begin_packed_struct struct ioe_rpmsg_readpin_s
|
|
|
|
{
|
|
|
|
struct ioe_rpmsg_header_s header;
|
|
|
|
uint8_t pin;
|
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
|
|
|
|
|
|
|
begin_packed_struct struct ioe_rpmsg_attach_s
|
|
|
|
{
|
|
|
|
struct ioe_rpmsg_header_s header;
|
|
|
|
uint64_t pinset;
|
|
|
|
uint64_t cbfunc;
|
|
|
|
uint64_t cbarg;
|
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
#define ioe_rpmsg_irq_s ioe_rpmsg_attach_s
|
|
|
|
|
|
|
|
begin_packed_struct struct ioe_rpmsg_detach_s
|
|
|
|
{
|
|
|
|
struct ioe_rpmsg_header_s header;
|
|
|
|
uint32_t cbidx;
|
|
|
|
} end_packed_struct;
|
|
|
|
|
|
|
|
struct ioe_rpmsg_cb_s
|
|
|
|
{
|
|
|
|
uint64_t pendset;
|
|
|
|
uint64_t cbfunc;
|
|
|
|
uint64_t cbarg;
|
2021-12-15 05:11:39 +01:00
|
|
|
FAR void *handler;
|
|
|
|
FAR struct rpmsg_endpoint *ept;
|
2021-12-08 05:08:25 +01:00
|
|
|
struct work_s work;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct ioe_rpmsg_server_s
|
|
|
|
{
|
|
|
|
FAR struct ioexpander_dev_s *ioe;
|
|
|
|
FAR const char *name;
|
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
|
|
|
struct ioe_rpmsg_cb_s cb[CONFIG_IOEXPANDER_RPMSG_INT_NCALLBACKS];
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ioe_rpmsg_client_s
|
|
|
|
{
|
|
|
|
struct ioexpander_dev_s ioe;
|
|
|
|
FAR const char *cpuname;
|
|
|
|
FAR const char *name;
|
|
|
|
sem_t sem;
|
|
|
|
struct rpmsg_endpoint ept;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ioe_rpmsg_ept_cb(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_);
|
|
|
|
|
|
|
|
static int ioe_rpmsg_direction_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_);
|
|
|
|
static int ioe_rpmsg_option_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_);
|
|
|
|
static int ioe_rpmsg_writepin_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_);
|
|
|
|
static int ioe_rpmsg_readpin_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_);
|
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
|
|
|
static int ioe_rpmsg_attach_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_);
|
|
|
|
static int ioe_rpmsg_detach_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_);
|
|
|
|
static int ioe_rpmsg_irq_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int ioe_rpmsg_direction(FAR struct ioexpander_dev_s *dev,
|
|
|
|
uint8_t pin, int dir);
|
|
|
|
static int ioe_rpmsg_option(FAR struct ioexpander_dev_s *dev,
|
|
|
|
uint8_t pin, int opt,
|
|
|
|
void *regval);
|
|
|
|
static int ioe_rpmsg_writepin(FAR struct ioexpander_dev_s *dev,
|
|
|
|
uint8_t pin, bool value);
|
|
|
|
static int ioe_rpmsg_readpin(FAR struct ioexpander_dev_s *dev,
|
|
|
|
uint8_t pin, FAR bool *value);
|
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
|
|
|
static void *ioe_rpmsg_attach(FAR struct ioexpander_dev_s *dev,
|
|
|
|
ioe_pinset_t pinset,
|
|
|
|
ioe_callback_t callback,
|
|
|
|
FAR void *arg);
|
|
|
|
static int ioe_rpmsg_detach(FAR struct ioexpander_dev_s *dev,
|
|
|
|
FAR void *handle);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const rpmsg_ept_cb g_ioe_rpmsg_handler[] =
|
|
|
|
{
|
|
|
|
[IOE_RPMSG_DIRECTION] = ioe_rpmsg_direction_handler,
|
|
|
|
[IOE_RPMSG_OPTION] = ioe_rpmsg_option_handler,
|
|
|
|
[IOE_RPMSG_WRITEPIN] = ioe_rpmsg_writepin_handler,
|
|
|
|
[IOE_RPMSG_READPIN] = ioe_rpmsg_readpin_handler,
|
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
|
|
|
[IOE_RPMSG_ATTACH] = ioe_rpmsg_attach_handler,
|
|
|
|
[IOE_RPMSG_DETACH] = ioe_rpmsg_detach_handler,
|
|
|
|
[IOE_RPMSG_IRQ] = ioe_rpmsg_irq_handler,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct ioexpander_ops_s g_ioe_rpmsg_ops =
|
|
|
|
{
|
|
|
|
ioe_rpmsg_direction,
|
|
|
|
ioe_rpmsg_option,
|
|
|
|
ioe_rpmsg_writepin,
|
|
|
|
ioe_rpmsg_readpin,
|
2022-02-17 22:14:52 +01:00
|
|
|
ioe_rpmsg_readpin
|
|
|
|
#ifdef CONFIG_IOEXPANDER_MULTIPIN
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
, NULL
|
|
|
|
#endif
|
2021-12-08 05:08:25 +01:00
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
2022-02-17 22:14:52 +01:00
|
|
|
, ioe_rpmsg_attach
|
|
|
|
, ioe_rpmsg_detach
|
2021-12-08 05:08:25 +01:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int ioe_rpmsg_wait_ready(FAR struct rpmsg_endpoint *ept)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (!is_rpmsg_ept_ready(ept))
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_client_s *priv =
|
|
|
|
container_of(ept, struct ioe_rpmsg_client_s, ept);
|
|
|
|
|
2022-01-18 08:52:32 +01:00
|
|
|
ret = rpmsg_wait(ept, &priv->sem);
|
2021-12-08 05:08:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-18 08:52:32 +01:00
|
|
|
rpmsg_post(ept, &priv->sem);
|
2021-12-08 05:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_sendrecv(FAR struct rpmsg_endpoint *ept,
|
|
|
|
uint32_t command,
|
|
|
|
FAR struct ioe_rpmsg_header_s *msg,
|
|
|
|
int len)
|
|
|
|
{
|
|
|
|
struct ioe_rpmsg_cookie_s cookie =
|
|
|
|
{
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = ioe_rpmsg_wait_ready(ept);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
nxsem_init(&cookie.sem, 0, 0);
|
|
|
|
nxsem_set_protocol(&cookie.sem, SEM_PRIO_NONE);
|
|
|
|
|
|
|
|
msg->command = command;
|
|
|
|
msg->response = 0;
|
|
|
|
msg->result = -ENXIO;
|
|
|
|
msg->cookie = (uintptr_t)&cookie;
|
|
|
|
|
|
|
|
ret = rpmsg_send(ept, msg, len);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-18 08:52:32 +01:00
|
|
|
ret = rpmsg_wait(ept, &cookie.sem);
|
2021-12-08 05:08:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cookie.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_direction(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
int dir)
|
|
|
|
{
|
2022-02-17 22:14:52 +01:00
|
|
|
FAR struct ioe_rpmsg_client_s *priv = (struct ioe_rpmsg_client_s *)dev;
|
2021-12-08 05:08:25 +01:00
|
|
|
struct ioe_rpmsg_direction_s msg;
|
|
|
|
|
|
|
|
msg.pin = pin;
|
|
|
|
msg.val = dir;
|
|
|
|
|
|
|
|
return ioe_rpmsg_sendrecv(&priv->ept, IOE_RPMSG_DIRECTION,
|
2022-02-17 22:14:52 +01:00
|
|
|
(struct ioe_rpmsg_header_s *)&msg, sizeof(msg));
|
2021-12-08 05:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_readpin(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
FAR bool *value)
|
|
|
|
{
|
2022-02-17 22:14:52 +01:00
|
|
|
FAR struct ioe_rpmsg_client_s *priv = (struct ioe_rpmsg_client_s *)dev;
|
2021-12-08 05:08:25 +01:00
|
|
|
struct ioe_rpmsg_readpin_s msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
msg.pin = pin;
|
|
|
|
|
|
|
|
ret = ioe_rpmsg_sendrecv(&priv->ept, IOE_RPMSG_READPIN,
|
2022-02-17 22:14:52 +01:00
|
|
|
(struct ioe_rpmsg_header_s *)&msg, sizeof(msg));
|
2021-12-08 05:08:25 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
*value = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_writepin(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
bool value)
|
|
|
|
{
|
2022-02-17 22:14:52 +01:00
|
|
|
FAR struct ioe_rpmsg_client_s *priv = (struct ioe_rpmsg_client_s *)dev;
|
2021-12-08 05:08:25 +01:00
|
|
|
struct ioe_rpmsg_writepin_s msg;
|
|
|
|
|
|
|
|
msg.pin = pin;
|
|
|
|
msg.val = value;
|
|
|
|
|
|
|
|
return ioe_rpmsg_sendrecv(&priv->ept, IOE_RPMSG_WRITEPIN,
|
2022-02-17 22:14:52 +01:00
|
|
|
(struct ioe_rpmsg_header_s *)&msg, sizeof(msg));
|
2021-12-08 05:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_option(FAR struct ioexpander_dev_s *dev, uint8_t pin,
|
|
|
|
int opt, void *regval)
|
|
|
|
{
|
2022-02-17 22:14:52 +01:00
|
|
|
FAR struct ioe_rpmsg_client_s *priv = (struct ioe_rpmsg_client_s *)dev;
|
2021-12-08 05:08:25 +01:00
|
|
|
struct ioe_rpmsg_option_s msg;
|
|
|
|
|
|
|
|
msg.pin = pin;
|
|
|
|
msg.opt = opt;
|
2021-12-14 07:28:03 +01:00
|
|
|
msg.val = (uintptr_t)regval;
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
return ioe_rpmsg_sendrecv(&priv->ept, IOE_RPMSG_OPTION,
|
2022-02-17 22:14:52 +01:00
|
|
|
(struct ioe_rpmsg_header_s *)&msg, sizeof(msg));
|
2021-12-08 05:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
|
|
|
static void *ioe_rpmsg_attach(FAR struct ioexpander_dev_s *dev,
|
|
|
|
ioe_pinset_t pinset,
|
|
|
|
ioe_callback_t callback,
|
|
|
|
FAR void *arg)
|
|
|
|
{
|
2022-02-17 22:14:52 +01:00
|
|
|
FAR struct ioe_rpmsg_client_s *priv = (struct ioe_rpmsg_client_s *)dev;
|
2021-12-08 05:08:25 +01:00
|
|
|
struct ioe_rpmsg_attach_s msg;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
msg.pinset = pinset;
|
|
|
|
msg.cbfunc = (uintptr_t)callback;
|
|
|
|
msg.cbarg = (uintptr_t)arg;
|
|
|
|
|
|
|
|
ret = ioe_rpmsg_sendrecv(&priv->ept, IOE_RPMSG_ATTACH,
|
2022-02-17 22:14:52 +01:00
|
|
|
(struct ioe_rpmsg_header_s *)&msg, sizeof(msg));
|
2021-12-08 05:08:25 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
return (FAR void *)(ret + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_detach(FAR struct ioexpander_dev_s *dev,
|
|
|
|
FAR void *handle)
|
|
|
|
{
|
2022-02-17 22:14:52 +01:00
|
|
|
FAR struct ioe_rpmsg_client_s *priv = (struct ioe_rpmsg_client_s *)dev;
|
2021-12-08 05:08:25 +01:00
|
|
|
struct ioe_rpmsg_detach_s msg;
|
|
|
|
|
|
|
|
msg.cbidx = (uintptr_t)handle - 1;
|
|
|
|
|
|
|
|
return ioe_rpmsg_sendrecv(&priv->ept, IOE_RPMSG_DETACH,
|
2022-02-17 22:14:52 +01:00
|
|
|
(struct ioe_rpmsg_header_s *)&msg, sizeof(msg));
|
2021-12-08 05:08:25 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int ioe_rpmsg_direction_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_direction_s *msg = data;
|
|
|
|
FAR struct ioe_rpmsg_server_s *priv = priv_;
|
|
|
|
|
2022-02-17 22:14:52 +01:00
|
|
|
msg->header.result = IOEXP_SETDIRECTION(priv->ioe, msg->pin, msg->val);
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
return rpmsg_send(ept, msg, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_option_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_option_s *msg = data;
|
|
|
|
FAR struct ioe_rpmsg_server_s *priv = priv_;
|
|
|
|
|
2022-02-17 22:14:52 +01:00
|
|
|
msg->header.result = IOEXP_SETOPTION(priv->ioe, msg->pin, msg->opt,
|
2021-12-14 07:28:03 +01:00
|
|
|
(void *)(uintptr_t)msg->val);
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
return rpmsg_send(ept, msg, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_writepin_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_writepin_s *msg = data;
|
|
|
|
FAR struct ioe_rpmsg_server_s *priv = priv_;
|
|
|
|
|
2022-02-17 22:14:52 +01:00
|
|
|
msg->header.result = IOEXP_WRITEPIN(priv->ioe, msg->pin, msg->val);
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
return rpmsg_send(ept, msg, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_readpin_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_readpin_s *msg = data;
|
|
|
|
FAR struct ioe_rpmsg_server_s *priv = priv_;
|
|
|
|
bool val;
|
|
|
|
|
|
|
|
msg->header.result = IOEXP_READPIN(priv->ioe, msg->pin, &val);
|
|
|
|
|
|
|
|
if (msg->header.result >= 0)
|
|
|
|
{
|
|
|
|
msg->header.result = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpmsg_send(ept, msg, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_IOEXPANDER_INT_ENABLE
|
|
|
|
|
|
|
|
static void ioe_rpmsg_irqworker(FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_cb_s *cb = priv_;
|
|
|
|
struct ioe_rpmsg_irq_s msg;
|
|
|
|
|
|
|
|
msg.pinset = cb->pendset;
|
|
|
|
msg.cbfunc = cb->cbfunc;
|
|
|
|
msg.cbarg = cb->cbarg;
|
|
|
|
|
2021-12-15 05:11:39 +01:00
|
|
|
ioe_rpmsg_sendrecv(cb->ept, IOE_RPMSG_IRQ,
|
2022-02-17 22:14:52 +01:00
|
|
|
(struct ioe_rpmsg_header_s *)&msg, sizeof(msg));
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
cb->pendset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_irq_cb(FAR struct ioexpander_dev_s *dev,
|
2022-02-17 22:14:52 +01:00
|
|
|
ioe_pinset_t pinset, FAR void *priv_)
|
2021-12-08 05:08:25 +01:00
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_cb_s *cb = priv_;
|
|
|
|
|
|
|
|
cb->pendset |= pinset;
|
|
|
|
|
2022-02-17 22:14:52 +01:00
|
|
|
work_queue(HPWORK, &cb->work, ioe_rpmsg_irqworker, cb, 0);
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_attach_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_attach_s *msg = data;
|
|
|
|
FAR struct ioe_rpmsg_server_s *priv = priv_;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < CONFIG_IOEXPANDER_RPMSG_INT_NCALLBACKS; i++)
|
|
|
|
{
|
|
|
|
if (!priv->cb[i].handler)
|
|
|
|
{
|
2022-02-17 22:14:52 +01:00
|
|
|
priv->cb[i].handler = IOEP_ATTACH(priv->ioe, msg->pinset,
|
|
|
|
ioe_rpmsg_irq_cb, &priv->cb[i]);
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
if (priv->cb[i].handler)
|
|
|
|
{
|
|
|
|
priv->cb[i].cbfunc = msg->cbfunc;
|
|
|
|
priv->cb[i].cbarg = msg->cbarg;
|
2021-12-15 05:11:39 +01:00
|
|
|
priv->cb[i].ept = ept;
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
msg->header.result = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpmsg_send(ept, msg, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_detach_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_detach_s *msg = data;
|
|
|
|
FAR struct ioe_rpmsg_server_s *priv = priv_;
|
|
|
|
|
2022-02-17 22:14:52 +01:00
|
|
|
msg->header.result = IOEP_DETACH(priv->ioe, priv->cb[msg->cbidx].handler);
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
if (msg->header.result >= 0)
|
|
|
|
{
|
|
|
|
priv->cb[msg->cbidx].pendset = 0;
|
2021-12-15 05:11:39 +01:00
|
|
|
priv->cb[msg->cbidx].cbfunc = 0;
|
|
|
|
priv->cb[msg->cbidx].cbarg = 0;
|
|
|
|
priv->cb[msg->cbidx].ept = NULL;
|
2021-12-08 05:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return rpmsg_send(ept, msg, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int ioe_rpmsg_irq_handler(FAR struct rpmsg_endpoint *ept,
|
|
|
|
FAR void *data, size_t len,
|
|
|
|
uint32_t src, FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_irq_s *msg = data;
|
|
|
|
FAR struct ioe_rpmsg_client_s *priv = priv_;
|
|
|
|
|
|
|
|
ioe_callback_t cb = (ioe_callback_t)(uintptr_t)msg->cbfunc;
|
|
|
|
|
|
|
|
cb(&priv->ioe, msg->pinset, (FAR void *)(uintptr_t)msg->cbarg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int ioe_rpmsg_ept_cb(FAR struct rpmsg_endpoint *ept, FAR void *data,
|
|
|
|
size_t len, uint32_t src, FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_header_s *msg = data;
|
|
|
|
uint32_t cmd = msg->command;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
struct ioe_rpmsg_cookie_s *cookie =
|
|
|
|
(struct ioe_rpmsg_cookie_s *)(uintptr_t)msg->cookie;
|
|
|
|
|
|
|
|
if (msg->response && cookie)
|
|
|
|
{
|
|
|
|
cookie->result = msg->result;
|
2022-01-18 08:52:32 +01:00
|
|
|
rpmsg_post(ept, &cookie->sem);
|
2021-12-08 05:08:25 +01:00
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
else if (cmd < ARRAY_SIZE(g_ioe_rpmsg_handler)
|
|
|
|
&& g_ioe_rpmsg_handler[cmd])
|
|
|
|
{
|
|
|
|
msg->response = 1;
|
|
|
|
ret = g_ioe_rpmsg_handler[cmd](ept, data, len, src, priv_);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ioe_rpmsg_client_created(FAR struct rpmsg_device *rdev,
|
|
|
|
FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_client_s *priv = priv_;
|
|
|
|
|
|
|
|
if (!strcmp(priv->cpuname, rpmsg_get_cpuname(rdev)))
|
|
|
|
{
|
|
|
|
char eptname[RPMSG_NAME_SIZE];
|
2022-02-17 22:14:52 +01:00
|
|
|
snprintf(eptname, RPMSG_NAME_SIZE, IOE_RPMSG_EPT_FORMAT, priv->name);
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
priv->ept.priv = priv;
|
2022-02-17 22:14:52 +01:00
|
|
|
rpmsg_create_ept(&priv->ept, rdev, eptname, RPMSG_ADDR_ANY,
|
|
|
|
RPMSG_ADDR_ANY, ioe_rpmsg_ept_cb, NULL);
|
2021-12-08 05:08:25 +01:00
|
|
|
|
2022-01-18 08:52:32 +01:00
|
|
|
rpmsg_post(&priv->ept, &priv->sem);
|
2021-12-08 05:08:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ioe_rpmsg_client_destroy(FAR struct rpmsg_device *rdev,
|
|
|
|
FAR void *priv_)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_client_s *priv = priv_;
|
|
|
|
|
|
|
|
if (!strcmp(priv->cpuname, rpmsg_get_cpuname(rdev)))
|
|
|
|
{
|
2022-01-18 08:52:32 +01:00
|
|
|
rpmsg_wait(&priv->ept, &priv->sem);
|
2021-12-08 05:08:25 +01:00
|
|
|
rpmsg_destroy_ept(&priv->ept);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ioe_rpmsg_server_unbind(FAR struct rpmsg_endpoint *ept)
|
|
|
|
{
|
|
|
|
rpmsg_destroy_ept(ept);
|
2021-12-15 05:11:39 +01:00
|
|
|
|
|
|
|
kmm_free(ept);
|
2021-12-08 05:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ioe_rpmsg_server_bind(FAR struct rpmsg_device *rdev,
|
|
|
|
FAR void *priv_,
|
|
|
|
FAR const char *name,
|
|
|
|
uint32_t dest)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_server_s *priv = priv_;
|
|
|
|
char eptname[RPMSG_NAME_SIZE];
|
|
|
|
|
2022-02-17 22:14:52 +01:00
|
|
|
snprintf(eptname, RPMSG_NAME_SIZE, IOE_RPMSG_EPT_FORMAT, priv->name);
|
2021-12-08 05:08:25 +01:00
|
|
|
|
2021-12-15 05:11:39 +01:00
|
|
|
if (!strcmp(name, eptname))
|
2021-12-08 05:08:25 +01:00
|
|
|
{
|
2021-12-15 05:11:39 +01:00
|
|
|
FAR struct rpmsg_endpoint *ept;
|
|
|
|
|
|
|
|
ept = kmm_zalloc(sizeof(struct rpmsg_endpoint));
|
|
|
|
if (!ept)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ept->priv = priv;
|
2021-12-08 05:08:25 +01:00
|
|
|
|
2022-02-17 22:14:52 +01:00
|
|
|
rpmsg_create_ept(ept, rdev, name, RPMSG_ADDR_ANY, RPMSG_ADDR_ANY,
|
|
|
|
ioe_rpmsg_ept_cb, ioe_rpmsg_server_unbind);
|
2021-12-08 05:08:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ioe_rpmsg_server_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize IO expander rpmsg server
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ioe_rpmsg_server_initialize(FAR const char *name,
|
|
|
|
FAR struct ioexpander_dev_s *ioe)
|
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_server_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!name || !ioe)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv = kmm_zalloc(sizeof(struct ioe_rpmsg_server_s));
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->name = name;
|
|
|
|
priv->ioe = ioe;
|
|
|
|
|
2022-02-17 22:14:52 +01:00
|
|
|
ret = rpmsg_register_callback(priv, NULL, NULL, ioe_rpmsg_server_bind);
|
2021-12-08 05:08:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ioe_rpmsg_client_initialize
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Initialize IO expander rpmsg client
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct ioexpander_dev_s *
|
2022-02-17 22:14:52 +01:00
|
|
|
ioe_rpmsg_client_initialize(FAR const char *cpuname, FAR const char *name)
|
2021-12-08 05:08:25 +01:00
|
|
|
{
|
|
|
|
FAR struct ioe_rpmsg_client_s *priv;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!cpuname || !name)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv = kmm_zalloc(sizeof(struct ioe_rpmsg_client_s));
|
|
|
|
if (!priv)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->ioe.ops = &g_ioe_rpmsg_ops;
|
2022-02-17 22:14:52 +01:00
|
|
|
priv->cpuname = cpuname;
|
|
|
|
priv->name = name;
|
2021-12-08 05:08:25 +01:00
|
|
|
|
|
|
|
nxsem_init(&priv->sem, 0, 0);
|
2022-02-17 22:14:52 +01:00
|
|
|
ret = rpmsg_register_callback(priv, ioe_rpmsg_client_created,
|
|
|
|
ioe_rpmsg_client_destroy, NULL);
|
2021-12-08 05:08:25 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
kmm_free(priv);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return &priv->ioe;
|
|
|
|
}
|
|
|
|
}
|