2017-10-27 05:21:35 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2017 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <spi.h>
|
|
|
|
#include <syscall_handler.h>
|
|
|
|
|
2018-05-15 04:27:06 +08:00
|
|
|
/* This assumes that bufs and buf_copy are copies from the values passed
|
|
|
|
* as syscall arguments.
|
|
|
|
*/
|
|
|
|
static void copy_and_check(struct spi_buf_set *bufs,
|
|
|
|
struct spi_buf *buf_copy,
|
|
|
|
int writable, void *ssf)
|
2017-10-27 05:21:35 +08:00
|
|
|
{
|
2018-05-15 04:27:06 +08:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (bufs->count == 0) {
|
|
|
|
bufs->buffers = NULL;
|
2017-10-27 05:21:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate the array of struct spi_buf instances */
|
2018-05-15 04:27:06 +08:00
|
|
|
_SYSCALL_MEMORY_ARRAY_READ(bufs->buffers,
|
|
|
|
bufs->count,
|
|
|
|
sizeof(struct spi_buf));
|
2017-10-27 05:21:35 +08:00
|
|
|
|
2018-05-15 04:27:06 +08:00
|
|
|
/* Not worried abuot overflow here: _SYSCALL_MEMORY_ARRAY_READ()
|
|
|
|
* takes care of it.
|
|
|
|
*/
|
|
|
|
bufs->buffers = memcpy(buf_copy,
|
|
|
|
bufs->buffers,
|
|
|
|
bufs->count * sizeof(struct spi_buf));
|
|
|
|
|
|
|
|
for (i = 0; i < bufs->count; i++) {
|
2017-10-27 05:21:35 +08:00
|
|
|
/* Now for each array element, validate the memory buffers
|
|
|
|
* that they point to
|
|
|
|
*/
|
2018-05-15 04:27:06 +08:00
|
|
|
struct spi_buf *buf = &bufs->buffers[i];
|
|
|
|
|
|
|
|
_SYSCALL_MEMORY(buf->buf, buf->len, writable);
|
2017-10-27 05:21:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 04:27:06 +08:00
|
|
|
/* This function is only here so tx_buf_copy and rx_buf_copy can be allocated
|
|
|
|
* using VLA. It assumes that both tx_bufs and rx_bufs will receive a copy of
|
|
|
|
* the values passed to the syscall as arguments. It also assumes that the
|
|
|
|
* count member has been verified and is a value that won't lead to stack
|
|
|
|
* overflow.
|
|
|
|
*/
|
|
|
|
static u32_t copy_bufs_and_transceive(struct device *dev,
|
|
|
|
const struct spi_config *config,
|
|
|
|
struct spi_buf_set *tx_bufs,
|
|
|
|
struct spi_buf_set *rx_bufs,
|
|
|
|
void *ssf)
|
|
|
|
{
|
|
|
|
struct spi_buf tx_buf_copy[tx_bufs->count ? tx_bufs->count : 1];
|
|
|
|
struct spi_buf rx_buf_copy[rx_bufs->count ? rx_bufs->count : 1];
|
|
|
|
|
|
|
|
copy_and_check(tx_bufs, tx_buf_copy, 0, ssf);
|
|
|
|
copy_and_check(rx_bufs, rx_buf_copy, 1, ssf);
|
|
|
|
|
|
|
|
return _impl_spi_transceive((struct device *)dev, config,
|
|
|
|
tx_bufs, rx_bufs);
|
|
|
|
}
|
|
|
|
|
2018-01-30 20:49:01 +08:00
|
|
|
_SYSCALL_HANDLER(spi_transceive, dev, config_p, tx_bufs, rx_bufs)
|
2017-10-27 05:21:35 +08:00
|
|
|
{
|
2018-01-29 20:37:25 +08:00
|
|
|
const struct spi_config *config = (const struct spi_config *)config_p;
|
2018-05-15 04:27:06 +08:00
|
|
|
struct spi_buf_set tx_bufs_copy;
|
|
|
|
struct spi_buf_set rx_bufs_copy;
|
|
|
|
struct spi_config config_copy;
|
2017-10-27 05:21:35 +08:00
|
|
|
|
|
|
|
_SYSCALL_MEMORY_READ(config, sizeof(*config));
|
2018-04-05 04:50:32 +08:00
|
|
|
_SYSCALL_DRIVER_SPI(dev, transceive);
|
2017-10-27 05:21:35 +08:00
|
|
|
|
2018-01-30 20:49:01 +08:00
|
|
|
if (tx_bufs) {
|
|
|
|
const struct spi_buf_set *tx =
|
|
|
|
(const struct spi_buf_set *)tx_bufs;
|
|
|
|
|
|
|
|
_SYSCALL_MEMORY_READ(tx_bufs, sizeof(struct spi_buf_set));
|
2018-05-15 04:27:06 +08:00
|
|
|
memcpy(&tx_bufs_copy, tx, sizeof(tx_bufs_copy));
|
|
|
|
_SYSCALL_VERIFY(tx_bufs_copy.count < 32);
|
|
|
|
} else {
|
|
|
|
memset(&tx_bufs_copy, 0, sizeof(tx_bufs_copy));
|
2018-01-30 20:49:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rx_bufs) {
|
|
|
|
const struct spi_buf_set *rx =
|
|
|
|
(const struct spi_buf_set *)rx_bufs;
|
|
|
|
|
|
|
|
_SYSCALL_MEMORY_READ(rx_bufs, sizeof(struct spi_buf_set));
|
2018-05-15 04:27:06 +08:00
|
|
|
memcpy(&rx_bufs_copy, rx, sizeof(rx_bufs_copy));
|
|
|
|
_SYSCALL_VERIFY(rx_bufs_copy.count < 32);
|
|
|
|
} else {
|
|
|
|
memset(&rx_bufs_copy, 0, sizeof(rx_bufs_copy));
|
2018-01-30 20:49:01 +08:00
|
|
|
}
|
2017-10-27 05:21:35 +08:00
|
|
|
|
2018-05-15 04:27:06 +08:00
|
|
|
memcpy(&config_copy, config, sizeof(*config));
|
|
|
|
if (config_copy.cs) {
|
|
|
|
const struct spi_cs_control *cs = config_copy.cs;
|
2017-10-27 05:21:35 +08:00
|
|
|
|
|
|
|
_SYSCALL_MEMORY_READ(cs, sizeof(*cs));
|
|
|
|
if (cs->gpio_dev) {
|
|
|
|
_SYSCALL_OBJ(cs->gpio_dev, K_OBJ_DRIVER_GPIO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 04:27:06 +08:00
|
|
|
/* ssf is implicit system call stack frame parameter, used by
|
|
|
|
* _SYSCALL_* APIs when something goes wrong.
|
|
|
|
*/
|
|
|
|
return copy_bufs_and_transceive((struct device *)dev,
|
|
|
|
&config_copy,
|
|
|
|
&tx_bufs_copy,
|
|
|
|
&rx_bufs_copy,
|
|
|
|
ssf);
|
2017-10-27 05:21:35 +08:00
|
|
|
}
|
|
|
|
|
2018-01-30 20:49:01 +08:00
|
|
|
_SYSCALL_HANDLER(spi_release, dev, config_p)
|
2017-10-27 05:21:35 +08:00
|
|
|
{
|
2018-01-29 20:37:25 +08:00
|
|
|
const struct spi_config *config = (const struct spi_config *)config_p;
|
2017-10-27 05:21:35 +08:00
|
|
|
|
|
|
|
_SYSCALL_MEMORY_READ(config, sizeof(*config));
|
2018-04-05 04:50:32 +08:00
|
|
|
_SYSCALL_DRIVER_SPI(dev, release);
|
2018-01-30 20:49:01 +08:00
|
|
|
return _impl_spi_release((struct device *)dev, config);
|
2017-10-27 05:21:35 +08:00
|
|
|
}
|