blob: 1522a210d8eb530acbd3b1b3984feb1e39dff9c6 [file] [log] [blame]
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Simple synchronous userspace interface to SPI devices
*
* Copyright (C) 2006 SWAPP
* Andrea Paterniani <a.paterniani@swapp-eng.it>
* Copyright (C) 2007 David Brownell (simplification, cleanup)
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ioctl.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/compat.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/acpi.h>
#include <linux/spi/spi.h>
#include <linux/spi/spidev.h>
#include <linux/uaccess.h>
#include <linux/of_gpio.h>
#include <linux/pinctrl/consumer.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start*/
#include <linux/wait.h>
#include <linux/suspend.h>
#define SPI_SLAVE_FOR_YK
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
/*
* This supports access to SPI devices using normal userspace I/O calls.
* Note that while traditional UNIX/POSIX I/O semantics are half duplex,
* and often mask message boundaries, full SPI support requires full duplex
* transfers. There are several kinds of internal message boundaries to
* handle chipselect management and other protocol options.
*
* SPI has a character major number assigned. We allocate minor numbers
* dynamically using a bitmask. You must use hotplug tools, such as udev
* (or mdev with busybox) to create and destroy the /dev/spidevB.C device
* nodes, since there is no fixed association of minor numbers with any
* particular SPI bus or device.
*/
#define SPIDEV_MAJOR 153 /* assigned */
#define N_SPI_MINORS 32 /* ... up to 256 */
static DECLARE_BITMAP(minors, N_SPI_MINORS);
/* Bit masks for spi_device.mode management. Note that incorrect
* settings for some settings can cause *lots* of trouble for other
* devices on a shared bus:
*
* - CS_HIGH ... this device will be active when it shouldn't be
* - 3WIRE ... when active, it won't behave as it should
* - NO_CS ... there will be no explicit message boundaries; this
* is completely incompatible with the shared bus model
* - READY ... transfers may proceed when they shouldn't.
*
* REVISIT should changing those flags be privileged?
*/
#define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
| SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
| SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
| SPI_TX_QUAD | SPI_TX_OCTAL | SPI_RX_DUAL \
| SPI_RX_QUAD | SPI_RX_OCTAL)
struct spidev_data {
dev_t devt;
spinlock_t spi_lock;
struct spi_device *spi;
struct list_head device_entry;
/* TX/RX buffers are NULL unless this device is open (users > 0) */
struct mutex buf_lock;
unsigned users;
u8 *tx_buffer;
u8 *rx_buffer;
u32 speed_hz;
u8 rd_from_rx_buffer;
//#define SPIDEV_DEBUG
#ifdef SPIDEV_DEBUG
struct pinctrl *pctrl;
struct pinctrl_state *pgpioex;
struct pinctrl_state *pint_ex;
int gpio_ex;
int gpio_int;
int irq;
int tx_flag;
int rx_cnt_in_rx_thread;
int rx_cnt_in_tx_thread;
struct semaphore wait_req;
struct semaphore rec_req;
struct semaphore rec_head_msg_req;
struct semaphore rec_data_msg_req;
spinlock_t tx_flag_lock;
int msg_id;
bool is_data_check;
int rx_data_check_ok_cnt;
int rx_data_check_err_cnt;
#endif
//#define TEST_SWAP_KERNEL_AND_USER
#ifdef TEST_SWAP_KERNEL_AND_USER
struct pinctrl *pctrl;
struct pinctrl_state *pgpioex;
struct pinctrl_state *pint_ex;
struct semaphore sig_req;
struct semaphore sem_dma_cfg_done;
int gpio_ex;
int gpio_int;
int irq;
int pid;
int dma_cfg_done;
#endif
};
static LIST_HEAD(device_list);
static DEFINE_MUTEX(device_list_lock);
static unsigned bufsiz = 4096;
module_param(bufsiz, uint, S_IRUGO);
MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
/*-------------------------------------------------------------------------*/
static ssize_t
spidev_sync(struct spidev_data *spidev, struct spi_message *message)
{
int status;
struct spi_device *spi;
spin_lock_irq(&spidev->spi_lock);
spi = spidev->spi;
spin_unlock_irq(&spidev->spi_lock);
if (spi == NULL)
status = -ESHUTDOWN;
else
status = spi_sync(spi, message);
if (status == 0)
status = message->actual_length;
return status;
}
static inline ssize_t
spidev_sync_write(struct spidev_data *spidev, size_t len)
{
struct spi_transfer t = {
.tx_buf = spidev->tx_buffer,
.len = len,
.speed_hz = spidev->speed_hz,
};
struct spi_message m;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return spidev_sync(spidev, &m);
}
static inline ssize_t
spidev_sync_read(struct spidev_data *spidev, size_t len)
{
struct spi_transfer t = {
.rx_buf = spidev->rx_buffer,
.len = len,
.speed_hz = spidev->speed_hz,
};
struct spi_message m;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return spidev_sync(spidev, &m);
}
static inline ssize_t
spidev_sync_write_and_read(struct spidev_data *spidev, size_t len)
{
struct spi_transfer t = {
.tx_buf = spidev->tx_buffer,
.rx_buf = spidev->rx_buffer,
.len = len,
.speed_hz = spidev->speed_hz,
};
struct spi_message m;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return spidev_sync(spidev, &m);
}
/*-------------------------------------------------------------------------*/
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start*/
/* Read-only message with current device setup */
static ssize_t
spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
struct spidev_data *spidev;
ssize_t status;
unsigned long missing;
/* chipselect only toggles at start or end of operation */
if (count > bufsiz)
return -EMSGSIZE;
spidev = filp->private_data;
#ifdef SPI_SLAVE_FOR_YK
size_t total = 0;
if (spidev->spi->rd_pos == spidev->spi->recv_pos) {
status = 0;
spidev->spi->is_rd_waiting = true;
if(0 != wait_event_freezable(spidev->spi->rd_wait, spidev->spi->recv_done)) {
if(spidev->spi->controller->spi_slave_rd_stop)
spidev->spi->controller->spi_slave_rd_stop(spidev->spi);
spidev->spi->is_rd_waiting = false;
return status;
}else {
spidev->spi->recv_done = false;
spidev->spi->is_rd_waiting = false;
}
}
mutex_lock(&spidev->buf_lock);
if(spidev->spi->rd_pos < spidev->spi->recv_pos) {
total = spidev->spi->recv_pos - spidev->spi->rd_pos;
status = (total > count) ? count : total;
missing = copy_to_user(buf, spidev->rx_buffer+spidev->spi->rd_pos, status);
if (missing == status) {
status = -EFAULT;
}
else {
status = status - missing;
spidev->spi->rd_pos += status;
}
}else if(spidev->spi->rd_pos > spidev->spi->recv_pos) {
total = bufsiz - (spidev->spi->rd_pos - spidev->spi->recv_pos);
status = (total > count) ? count : total;
if((spidev->spi->rd_pos + status) <= bufsiz) {
missing = copy_to_user(buf, spidev->rx_buffer+spidev->spi->rd_pos, status);
if (missing == status) {
status = -EFAULT;
}
else {
status = status - missing;
spidev->spi->rd_pos += status;
spidev->spi->rd_pos = spidev->spi->rd_pos%bufsiz;
}
}else {
unsigned long first,rest;
first = bufsiz - spidev->spi->rd_pos;
missing = copy_to_user(buf, spidev->rx_buffer+spidev->spi->rd_pos, first);
if (missing == first) {
status = -EFAULT;
} else {
status = status - missing;
}
rest = status-first;
missing = copy_to_user(buf+first, spidev->rx_buffer, rest);
if (missing == rest) {
status = -EFAULT;
} else {
status = status - missing;
}
spidev->spi->rd_pos = rest;
}
}
#else
mutex_lock(&spidev->buf_lock);
if(spidev->rd_from_rx_buffer)
status = count;
else
status = spidev_sync_read(spidev, count);
if (status > 0) {
missing = copy_to_user(buf, spidev->rx_buffer, status);
if (missing == status)
status = -EFAULT;
else
status = status - missing;
}
#endif
mutex_unlock(&spidev->buf_lock);
return status;
}
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end*/
/* Write-only message with current device setup */
static ssize_t
spidev_write(struct file *filp, const char __user *buf,
size_t count, loff_t *f_pos)
{
struct spidev_data *spidev;
ssize_t status;
unsigned long missing;
/* chipselect only toggles at start or end of operation */
if (count > bufsiz)
return -EMSGSIZE;
spidev = filp->private_data;
mutex_lock(&spidev->buf_lock);
missing = copy_from_user(spidev->tx_buffer, buf, count);
if (missing == 0) {
if(spidev->rd_from_rx_buffer)
status = spidev_sync_write_and_read(spidev, count);
else
status = spidev_sync_write(spidev, count);
}else {
status = -EFAULT;
}
mutex_unlock(&spidev->buf_lock);
return status;
}
static int spidev_message(struct spidev_data *spidev,
struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
{
struct spi_message msg;
struct spi_transfer *k_xfers;
struct spi_transfer *k_tmp;
struct spi_ioc_transfer *u_tmp;
unsigned n, total, tx_total, rx_total;
u8 *tx_buf, *rx_buf;
int status = -EFAULT;
spi_message_init(&msg);
k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
if (k_xfers == NULL)
return -ENOMEM;
/* Construct spi_message, copying any tx data to bounce buffer.
* We walk the array of user-provided transfers, using each one
* to initialize a kernel version of the same transfer.
*/
tx_buf = spidev->tx_buffer;
rx_buf = spidev->rx_buffer;
total = 0;
tx_total = 0;
rx_total = 0;
for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
n;
n--, k_tmp++, u_tmp++) {
/* Ensure that also following allocations from rx_buf/tx_buf will meet
* DMA alignment requirements.
*/
unsigned int len_aligned = ALIGN(u_tmp->len, ARCH_KMALLOC_MINALIGN);
k_tmp->len = u_tmp->len;
total += k_tmp->len;
/* Since the function returns the total length of transfers
* on success, restrict the total to positive int values to
* avoid the return value looking like an error. Also check
* each transfer length to avoid arithmetic overflow.
*/
if (total > INT_MAX || k_tmp->len > INT_MAX) {
status = -EMSGSIZE;
goto done;
}
if (u_tmp->rx_buf) {
/* this transfer needs space in RX bounce buffer */
rx_total += len_aligned;
if (rx_total > bufsiz) {
status = -EMSGSIZE;
goto done;
}
k_tmp->rx_buf = rx_buf;
rx_buf += len_aligned;
}
if (u_tmp->tx_buf) {
/* this transfer needs space in TX bounce buffer */
tx_total += len_aligned;
if (tx_total > bufsiz) {
status = -EMSGSIZE;
goto done;
}
k_tmp->tx_buf = tx_buf;
if (copy_from_user(tx_buf, (const u8 __user *)
(uintptr_t) u_tmp->tx_buf,
u_tmp->len))
goto done;
tx_buf += len_aligned;
}
k_tmp->cs_change = !!u_tmp->cs_change;
k_tmp->tx_nbits = u_tmp->tx_nbits;
k_tmp->rx_nbits = u_tmp->rx_nbits;
k_tmp->bits_per_word = u_tmp->bits_per_word;
k_tmp->delay.value = u_tmp->delay_usecs;
k_tmp->delay.unit = SPI_DELAY_UNIT_USECS;
k_tmp->speed_hz = u_tmp->speed_hz;
k_tmp->word_delay.value = u_tmp->word_delay_usecs;
k_tmp->word_delay.unit = SPI_DELAY_UNIT_USECS;
if (!k_tmp->speed_hz)
k_tmp->speed_hz = spidev->speed_hz;
#ifdef VERBOSE
dev_dbg(&spidev->spi->dev,
" xfer len %u %s%s%s%dbits %u usec %u usec %uHz\n",
k_tmp->len,
k_tmp->rx_buf ? "rx " : "",
k_tmp->tx_buf ? "tx " : "",
k_tmp->cs_change ? "cs " : "",
k_tmp->bits_per_word ? : spidev->spi->bits_per_word,
k_tmp->delay.value,
k_tmp->word_delay.value,
k_tmp->speed_hz ? : spidev->spi->max_speed_hz);
#endif
spi_message_add_tail(k_tmp, &msg);
}
status = spidev_sync(spidev, &msg);
if (status < 0)
goto done;
/* copy any rx data out of bounce buffer */
for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
n;
n--, k_tmp++, u_tmp++) {
if (u_tmp->rx_buf) {
if (copy_to_user((u8 __user *)
(uintptr_t) u_tmp->rx_buf, k_tmp->rx_buf,
u_tmp->len)) {
status = -EFAULT;
goto done;
}
}
}
status = total;
done:
kfree(k_xfers);
return status;
}
static struct spi_ioc_transfer *
spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
unsigned *n_ioc)
{
u32 tmp;
/* Check type, command number and direction */
if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC
|| _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
|| _IOC_DIR(cmd) != _IOC_WRITE)
return ERR_PTR(-ENOTTY);
tmp = _IOC_SIZE(cmd);
if ((tmp % sizeof(struct spi_ioc_transfer)) != 0)
return ERR_PTR(-EINVAL);
*n_ioc = tmp / sizeof(struct spi_ioc_transfer);
if (*n_ioc == 0)
return NULL;
/* copy into scratch area */
return memdup_user(u_ioc, tmp);
}
static long
spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int retval = 0;
struct spidev_data *spidev;
struct spi_device *spi;
u32 tmp;
unsigned n_ioc;
struct spi_ioc_transfer *ioc;
/* Check type and command number */
if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
return -ENOTTY;
/* guard against device removal before, or while,
* we issue this ioctl.
*/
spidev = filp->private_data;
spin_lock_irq(&spidev->spi_lock);
spi = spi_dev_get(spidev->spi);
spin_unlock_irq(&spidev->spi_lock);
if (spi == NULL)
return -ESHUTDOWN;
/* use the buffer lock here for triple duty:
* - prevent I/O (from us) so calling spi_setup() is safe;
* - prevent concurrent SPI_IOC_WR_* from morphing
* data fields while SPI_IOC_RD_* reads them;
* - SPI_IOC_MESSAGE needs the buffer locked "normally".
*/
mutex_lock(&spidev->buf_lock);
switch (cmd) {
/* read requests */
case SPI_IOC_RD_MODE:
retval = put_user(spi->mode & SPI_MODE_MASK,
(__u8 __user *)arg);
break;
case SPI_IOC_RD_MODE32:
retval = put_user(spi->mode & SPI_MODE_MASK,
(__u32 __user *)arg);
break;
case SPI_IOC_RD_LSB_FIRST:
retval = put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
(__u8 __user *)arg);
break;
case SPI_IOC_RD_BITS_PER_WORD:
retval = put_user(spi->bits_per_word, (__u8 __user *)arg);
break;
case SPI_IOC_RD_MAX_SPEED_HZ:
retval = put_user(spidev->speed_hz, (__u32 __user *)arg);
break;
case SPI_IOC_RD_RD_DATA_FROM:
retval = put_user(spidev->rd_from_rx_buffer, (__u32 __user *)arg);
break;
#ifdef TEST_SWAP_KERNEL_AND_USER
case SPI_IOC_RD_INT_ST:
tmp = gpio_get_value(spidev->gpio_int);
retval = put_user(tmp, (__u32 __user *)arg);
break;
#endif
/* write requests */
case SPI_IOC_WR_MODE:
case SPI_IOC_WR_MODE32:
if (cmd == SPI_IOC_WR_MODE)
retval = get_user(tmp, (u8 __user *)arg);
else
retval = get_user(tmp, (u32 __user *)arg);
if (retval == 0) {
struct spi_controller *ctlr = spi->controller;
u32 save = spi->mode;
if (tmp & ~SPI_MODE_MASK) {
retval = -EINVAL;
break;
}
if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
ctlr->cs_gpiods[spi->chip_select])
tmp |= SPI_CS_HIGH;
tmp |= spi->mode & ~SPI_MODE_MASK;
spi->mode = (u16)tmp;
retval = spi_setup(spi);
if (retval < 0)
spi->mode = save;
else
dev_dbg(&spi->dev, "spi mode %x\n", tmp);
}
break;
case SPI_IOC_WR_LSB_FIRST:
retval = get_user(tmp, (__u8 __user *)arg);
if (retval == 0) {
u32 save = spi->mode;
if (tmp)
spi->mode |= SPI_LSB_FIRST;
else
spi->mode &= ~SPI_LSB_FIRST;
retval = spi_setup(spi);
if (retval < 0)
spi->mode = save;
else
dev_dbg(&spi->dev, "%csb first\n",
tmp ? 'l' : 'm');
}
break;
case SPI_IOC_WR_BITS_PER_WORD:
retval = get_user(tmp, (__u8 __user *)arg);
if (retval == 0) {
u8 save = spi->bits_per_word;
spi->bits_per_word = tmp;
retval = spi_setup(spi);
if (retval < 0)
spi->bits_per_word = save;
else
dev_dbg(&spi->dev, "%d bits per word\n", tmp);
}
break;
case SPI_IOC_WR_MAX_SPEED_HZ:
retval = get_user(tmp, (__u32 __user *)arg);
if (retval == 0) {
u32 save = spi->max_speed_hz;
spi->max_speed_hz = tmp;
retval = spi_setup(spi);
if (retval == 0) {
spidev->speed_hz = tmp;
dev_dbg(&spi->dev, "%d Hz (max)\n",
spidev->speed_hz);
} else {
spi->max_speed_hz = save;
}
}
break;
case SPI_IOC_WR_RD_DATA_FROM:
retval = get_user(tmp, (__u8 __user *)arg);
if (retval == 0) {
spidev->rd_from_rx_buffer = tmp;
dev_dbg(&spi->dev, "RD DATA FROM %s \n",
spidev->rd_from_rx_buffer ? "RX_BUFFER":"DEVICE");
}
break;
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
#ifdef SPI_SLAVE_FOR_YK
case SPI_IOC_RD_BLOCK_RELEASE:
if(spidev->spi->is_rd_waiting == true) {
wake_up(&spidev->spi->rd_wait);
spidev->spi->recv_done = 1;
}
break;
#endif
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
#ifdef TEST_SWAP_KERNEL_AND_USER
case SPI_IOC_WR_SIG_PID:
retval = get_user(tmp, (__u32 __user *)arg);
if (retval == 0) {
spidev->pid = tmp;
dev_dbg(&spi->dev, "SET SIG PID %d \n",
spidev->pid);
}else{
printk("%s %d %d \r\n",__FUNCTION__,__LINE__,retval);
}
break;
#endif
default:
/* segmented and/or full-duplex I/O request */
/* Check message and copy into scratch area */
ioc = spidev_get_ioc_message(cmd,
(struct spi_ioc_transfer __user *)arg, &n_ioc);
if (IS_ERR(ioc)) {
retval = PTR_ERR(ioc);
break;
}
if (!ioc)
break; /* n_ioc is also 0 */
/* translate to spi_message, execute */
retval = spidev_message(spidev, ioc, n_ioc);
kfree(ioc);
break;
}
mutex_unlock(&spidev->buf_lock);
spi_dev_put(spi);
return retval;
}
#ifdef CONFIG_COMPAT
static long
spidev_compat_ioc_message(struct file *filp, unsigned int cmd,
unsigned long arg)
{
struct spi_ioc_transfer __user *u_ioc;
int retval = 0;
struct spidev_data *spidev;
struct spi_device *spi;
unsigned n_ioc, n;
struct spi_ioc_transfer *ioc;
u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg);
/* guard against device removal before, or while,
* we issue this ioctl.
*/
spidev = filp->private_data;
spin_lock_irq(&spidev->spi_lock);
spi = spi_dev_get(spidev->spi);
spin_unlock_irq(&spidev->spi_lock);
if (spi == NULL)
return -ESHUTDOWN;
/* SPI_IOC_MESSAGE needs the buffer locked "normally" */
mutex_lock(&spidev->buf_lock);
/* Check message and copy into scratch area */
ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc);
if (IS_ERR(ioc)) {
retval = PTR_ERR(ioc);
goto done;
}
if (!ioc)
goto done; /* n_ioc is also 0 */
/* Convert buffer pointers */
for (n = 0; n < n_ioc; n++) {
ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf);
ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf);
}
/* translate to spi_message, execute */
retval = spidev_message(spidev, ioc, n_ioc);
kfree(ioc);
done:
mutex_unlock(&spidev->buf_lock);
spi_dev_put(spi);
return retval;
}
static long
spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC
&& _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0))
&& _IOC_DIR(cmd) == _IOC_WRITE)
return spidev_compat_ioc_message(filp, cmd, arg);
return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
}
#else
#define spidev_compat_ioctl NULL
#endif /* CONFIG_COMPAT */
static int spidev_open(struct inode *inode, struct file *filp)
{
struct spidev_data *spidev;
int status = -ENXIO;
struct spi_device *spi;
mutex_lock(&device_list_lock);
list_for_each_entry(spidev, &device_list, device_entry) {
if (spidev->devt == inode->i_rdev) {
status = 0;
break;
}
}
if (status) {
pr_debug("spidev: nothing for minor %d\n", iminor(inode));
goto err_find_dev;
}
if (!spidev->tx_buffer) {
spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->tx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
status = -ENOMEM;
goto err_find_dev;
}
}
if (!spidev->rx_buffer) {
spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->rx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
status = -ENOMEM;
goto err_alloc_rx_buf;
}
}
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
#ifdef SPI_SLAVE_FOR_YK
if(spidev->rx_buffer) {
spidev->spi->rx_buf = spidev->rx_buffer;
if(spidev->spi->controller->spi_slave_rd_start)
spidev->spi->controller->spi_slave_rd_start(spidev->spi);
}
#endif
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
spidev->users++;
filp->private_data = spidev;
stream_open(inode, filp);
mutex_unlock(&device_list_lock);
spin_lock_irq(&spidev->spi_lock);
spi = spi_dev_get(spidev->spi);
spin_unlock_irq(&spidev->spi_lock);
if(spi && spi->master->slave)
pm_stay_awake(&spi->dev);
return 0;
err_alloc_rx_buf:
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
err_find_dev:
mutex_unlock(&device_list_lock);
return status;
}
static int spidev_release(struct inode *inode, struct file *filp)
{
struct spidev_data *spidev;
int dofree;
struct spi_device *spi;
mutex_lock(&device_list_lock);
spidev = filp->private_data;
filp->private_data = NULL;
spin_lock_irq(&spidev->spi_lock);
/* ... after we unbound from the underlying device? */
dofree = (spidev->spi == NULL);
spin_unlock_irq(&spidev->spi_lock);
/* last close? */
spidev->users--;
if (!spidev->users) {
spin_lock_irq(&spidev->spi_lock);
spi = spi_dev_get(spidev->spi);
spin_unlock_irq(&spidev->spi_lock);
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
#ifdef SPI_SLAVE_FOR_YK
if(spidev->rx_buffer) {
if(spi->controller->spi_slave_rd_stop)
spi->controller->spi_slave_rd_stop(spi);
}
#endif
/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
if(spi && spi->master->slave)
pm_relax(&spi->dev);
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
kfree(spidev->rx_buffer);
spidev->rx_buffer = NULL;
if (dofree)
kfree(spidev);
else
spidev->speed_hz = spidev->spi->max_speed_hz;
}
#ifdef CONFIG_SPI_SLAVE
if (!dofree)
spi_slave_abort(spidev->spi);
#endif
mutex_unlock(&device_list_lock);
return 0;
}
static const struct file_operations spidev_fops = {
.owner = THIS_MODULE,
/* REVISIT switch to aio primitives, so that userspace
* gets more complete API coverage. It'll simplify things
* too, except for the locking.
*/
.write = spidev_write,
.read = spidev_read,
.unlocked_ioctl = spidev_ioctl,
.compat_ioctl = spidev_compat_ioctl,
.open = spidev_open,
.release = spidev_release,
.llseek = no_llseek,
};
/*-------------------------------------------------------------------------*/
/* The main reason to have this class is to make mdev/udev create the
* /dev/spidevB.C character device nodes exposing our userspace API.
* It also simplifies memory management.
*/
static struct class *spidev_class;
#ifdef CONFIG_OF
static const struct of_device_id spidev_dt_ids[] = {
{ .compatible = "rohm,dh2228fv" },
{ .compatible = "lineartechnology,ltc2488" },
{ .compatible = "ge,achc" },
{ .compatible = "semtech,sx1301" },
{ .compatible = "lwn,bk4" },
{ .compatible = "dh,dhcom-board" },
{ .compatible = "menlo,m53cpld" },
{ .compatible = "zte,spidev" },
{},
};
MODULE_DEVICE_TABLE(of, spidev_dt_ids);
#endif
#ifdef CONFIG_ACPI
/* Dummy SPI devices not to be used in production systems */
#define SPIDEV_ACPI_DUMMY 1
static const struct acpi_device_id spidev_acpi_ids[] = {
/*
* The ACPI SPT000* devices are only meant for development and
* testing. Systems used in production should have a proper ACPI
* description of the connected peripheral and they should also use
* a proper driver instead of poking directly to the SPI bus.
*/
{ "SPT0001", SPIDEV_ACPI_DUMMY },
{ "SPT0002", SPIDEV_ACPI_DUMMY },
{ "SPT0003", SPIDEV_ACPI_DUMMY },
{},
};
MODULE_DEVICE_TABLE(acpi, spidev_acpi_ids);
static void spidev_probe_acpi(struct spi_device *spi)
{
const struct acpi_device_id *id;
if (!has_acpi_companion(&spi->dev))
return;
id = acpi_match_device(spidev_acpi_ids, &spi->dev);
if (WARN_ON(!id))
return;
if (id->driver_data == SPIDEV_ACPI_DUMMY)
dev_warn(&spi->dev, "do not use this driver in production systems!\n");
}
#else
static inline void spidev_probe_acpi(struct spi_device *spi) {}
#endif
#ifdef SPIDEV_DEBUG
#define SPIDEV_ATTR(_name) \
static struct kobj_attribute _name##_attr = { \
.attr = { \
.name = __stringify(_name), \
.mode = 0644, \
}, \
.show = _name##_show, \
.store = _name##_store, \
}
static void print_buf_data(void * buf,int count)
{
int i = 0;
if(buf) {
unsigned char *p = buf;
for(i = 0;i<= count-8;i+=8) {
printk("%02x %02x %02x %02x %02x %02x %02x %02x \r\n",p[i],p[i+1],p[i+2],p[i+3],p[i+4],p[i+5],p[i+6],p[i+7]);
}
}
}
struct spi_dev_hand_msg{
unsigned short head;
unsigned int len;
unsigned short tail;
};
#define MSG_HEAD 0xa5a5
#define MSG_TAIL 0x7e7e
extern void slave_mode_set(struct spi_device *spi,unsigned int param);
extern void set_spi_timing(struct spi_device *spi,unsigned int param);
extern int get_spi_rx_fifo(struct spi_device *spi,unsigned char *buf);
static int spidev_get_rxfifo(struct spi_device *spi,unsigned char *buf)
{
int ret = 0;
if(!spi || !buf)
return ret;
return get_spi_rx_fifo(spi,buf);
}
static int data_to_packet(void * buf,int len)
{
int i = 2,ret = -1;
unsigned char sum = 0;
unsigned char *p = (unsigned char *)buf;
if(!p || len < 4) {
printk("%s param err! \n",__FUNCTION__);
return ret;
}
for(i = 2;i<len-2;i++)
sum += p[i];
p[1] = sum;
ret = 0;
return ret;
}
static int packet_check(void *buf,int len)
{
unsigned char *p = (unsigned char *)buf;
int i = 2,ret = -1;
unsigned char sum=0;
if(!p || len < 4) {
printk("%s param err! \n",__FUNCTION__);
return ret;
}
if( (p[0] == 0xa5) && (p[len-1] == 0x7e) ) {
for(i = 2;i<len-2;i++)
sum +=p[i];
if(sum == p[1])
ret = 0;
}
return ret;
}
static int spi_dev_pin_init_test(struct spi_device *spi)
{
struct spidev_data *spidev = spi_get_drvdata(spi);
enum of_gpio_flags flags;
static int spi_dev_pin_init_flag = 0;
int status = 0;
if(spi_dev_pin_init_flag < 2){
spidev->pctrl = devm_pinctrl_get(&spi->dev);
if(!spidev->pctrl) {
dev_info(&spi->dev,"get dev pctrl failed!\n",status);
return status;
}
spidev->pint_ex = pinctrl_lookup_state(spidev->pctrl, "int_ex");
if (IS_ERR(spidev->pint_ex)) {
dev_err(&spi->dev, "TEST: missing pint_ex \n");
return status;
}
if (pinctrl_select_state(spidev->pctrl, spidev->pint_ex) < 0) {
dev_err(&spi->dev, "TEST: slect pint_ex \n");
return status;
}
spidev->pgpioex = pinctrl_lookup_state(spidev->pctrl, "ex_gpio");
if (IS_ERR(spidev->pgpioex)) {
dev_err(&spi->dev, "TEST: missing ex_gpio \n");
return status;
}
spidev->gpio_ex = of_get_gpio_flags(spi->dev.of_node, 0, &flags);
if (!gpio_is_valid(spidev->gpio_ex)) {
dev_err(&spi->dev,"gpio_ex no found,spidev->gpio_ex=%d \n",spidev->gpio_ex);
return status;
}
dev_info(&spi->dev,"gpio_ex found,spidev->gpio_ex=%d \n",spidev->gpio_ex);
status = gpio_request(spidev->gpio_ex, "gpio_ex");
if (status) {
pr_info("spidev->gpio_ex request error.\n");
}else {
gpio_direction_output(spidev->gpio_ex, 1);
dev_info(&spi->dev, "spidev->gpio_ex success \n");
}
spidev->gpio_int = of_get_gpio_flags(spi->dev.of_node, 1, &flags);
if (!gpio_is_valid(spidev->gpio_int)) {
dev_err(&spi->dev,"gpio_int no found,spidev->gpio_int=%d \n",spidev->gpio_int);
return status;
}
dev_info(&spi->dev,"gpio_int found,spidev->gpio_int=%d \n",spidev->gpio_int);
spi_dev_pin_init_flag += 1;
}
return status;
}
static irqreturn_t spidev_master_hand_shake_irq(int irqno, void *dev_id)
{
static int count;
int gpio_in_status = 0,gpio_out_status = 0;
struct spidev_data *spidev = dev_id;
gpio_out_status = gpio_get_value(spidev->gpio_ex);
gpio_in_status = gpio_get_value(spidev->gpio_int);
//pr_info("hand_shake_irq get = %d %d %d\n", ++count,gpio_out_status,gpio_in_status);
if(gpio_out_status && !gpio_in_status) {
if(spidev->tx_flag == 0) {
up(&spidev->rec_req); /*receive slave reqeuet*/
}else {
pr_info("mmm \r\n");
up(&spidev->wait_req); /*first receive master req*/
}
}else if(!gpio_out_status && !gpio_in_status) {
up(&spidev->wait_req); /*receive slave ack*/
}else {
pr_info("recive invalid request\n");
}
return IRQ_HANDLED;
}
static irqreturn_t spidev_slave_hand_shake_irq(int irqno, void *dev_id)
{
static int count;
int gpio_in_status = 0,gpio_out_status = 0;
struct spidev_data *spidev = dev_id;
gpio_out_status = gpio_get_value(spidev->gpio_ex);
gpio_in_status = gpio_get_value(spidev->gpio_int);
//pr_info("hand_shake_irq get = %d %d %d\n", ++count,gpio_out_status,gpio_in_status);
if(gpio_out_status && !gpio_in_status)
{
if(spidev->tx_flag == 0) {
up(&spidev->rec_req); /*first receive master req*/
}else {
pr_info("sss \n");
up(&spidev->wait_req);
}
/*. then set gpio_out low as ack. */
}else if(!gpio_out_status && !gpio_in_status) {
up(&spidev->wait_req); /*receive master ack*/
}else {
pr_info("recive invalid request\n");
}
return IRQ_HANDLED;
}
static int spi_dev_irq_init_test(struct spi_device *spi)
{
struct spidev_data *spidev = spi_get_drvdata(spi);
static int spi_dev_irq_init_flag = 0;
int irq = 0,ret = 0;
if(spi_dev_irq_init_flag < 2) {
if(!spi || !spidev) {
ret = -ENOENT;
return ret;
}
irq = irq_of_parse_and_map(spi->dev.of_node, 0);
if (irq <= 0) {
dev_err(&spi->dev, "ERROR: invalid interrupt number, irq = %d\n",irq);
return -EBUSY;
}
spidev->irq = irq;
dev_info(&spi->dev, "used interrupt num is %d\n", spidev->irq);
if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
ret = devm_request_irq(&spi->dev, spidev->irq, spidev_master_hand_shake_irq,
0, dev_name(&spi->dev), spidev);
}else {
ret = devm_request_irq(&spi->dev, spidev->irq, spidev_slave_hand_shake_irq,
0, dev_name(&spi->dev), spidev);
}
if (ret < 0) {
dev_err(&spi->dev, "probe - cannot get IRQ (%d)\n", ret);
return ret;
}
spi_dev_irq_init_flag += 1;
}
return ret;
}
static size_t spi_dev_send_handle_pack_test(struct spidev_data *spidev,int len,struct spi_dev_hand_msg *recv_msg)
{
struct spi_dev_hand_msg send_msg={0};
send_msg.head = MSG_HEAD;
send_msg.len = len;
send_msg.tail = MSG_TAIL;
struct spi_transfer t = {
.tx_buf = &send_msg,
.rx_buf = recv_msg,
.len = sizeof(struct spi_dev_hand_msg),
.speed_hz = spidev->speed_hz,
};
struct spi_message m;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return spidev_sync(spidev, &m);
}
static size_t spi_dev_recv_handle_pack_test(struct spidev_data *spidev,int len,struct spi_dev_hand_msg *recv_msg)
{
struct spi_dev_hand_msg send_msg={0};
struct spi_transfer t = {
.tx_buf = &send_msg,
.rx_buf = recv_msg,
.len = sizeof(struct spi_dev_hand_msg),
.speed_hz = spidev->speed_hz,
};
struct spi_message m;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return spidev_sync(spidev, &m);
}
static void wait_spi_bus_idle_status_test(struct spidev_data *spidev)
{
int count = 0;
do {
spin_lock_irq(&spidev->tx_flag_lock);
if( gpio_get_value(spidev->gpio_ex) && gpio_get_value(spidev->gpio_int))
break;
else {
spin_unlock(&spidev->tx_flag_lock);
usleep_range(50,100);
count++;
if(count%20 == 0) {
printk("bus busy %d us cnts.outst(%d),intst(%d).\n",count*50,
gpio_get_value(spidev->gpio_ex),gpio_get_value(spidev->gpio_int));
}
}
}while(1);
spidev->tx_flag = 1;
spin_unlock_irq(&spidev->tx_flag_lock);
}
static size_t spi_dev_send_one_pack_test(struct spi_device *spi,size_t len) {
struct spidev_data *spidev = spi_get_drvdata(spi);
struct spi_dev_hand_msg recv_msg={0};
size_t status;
int ret;
int rx_data_flag = 0;
if(len>4096)
printk("len(%d) err: \r\n",len);
wait_spi_bus_idle_status_test(spidev);
gpio_set_value(spidev->gpio_ex,0);
ret = down_timeout(&spidev->wait_req, msecs_to_jiffies(50)); /*first ack m= 0,s=0*/
if (ret < 0) {
printk("first ack timeout\n");
}
spi_dev_send_handle_pack_test(spidev,len,&recv_msg); /*send head msg*/
if(recv_msg.head == MSG_HEAD && recv_msg.tail == MSG_TAIL) {
len = (recv_msg.len >= len) ? recv_msg.len : len;
spidev->rx_cnt_in_tx_thread++;
rx_data_flag = 1;
if(len>4096)
printk("len(%d) err: \r\n",len);
}
ret = down_timeout(&spidev->wait_req, msecs_to_jiffies(100));
if (ret < 0) {
printk("second ack timeout\n");
}
//down(&spidev->wait_req); /*second ack m= 0,s=0*/
status = spidev_sync_write_and_read(spidev,len);
if(rx_data_flag && spidev->is_data_check) {
ret = packet_check(spidev->rx_buffer,recv_msg.len);
if(ret) {
spidev->rx_data_check_err_cnt++;
//dev_info(&spi->dev,"%s packet check err \r\n",__FUNCTION__);
}else {
spidev->rx_data_check_ok_cnt++;
//dev_info(&spi->dev,"%s packet check success \r\n",__FUNCTION__);
}
}
spidev->tx_flag = 0;
gpio_set_value(spidev->gpio_ex,1);
return status;
}
static size_t spi_dev_slave_send_one_pack_test(struct spi_device *spi,size_t len) {
struct spidev_data *spidev = spi_get_drvdata(spi);
struct spi_dev_hand_msg recv_msg={0};
size_t status;
int ret;
int rx_data_flag = 0;
if(len>4096)
printk("len(%d) err: \r\n",len);
wait_spi_bus_idle_status_test(spidev);
up(&spidev->rec_head_msg_req);/*response master tx/rx dma set */
//printk("%s %d \r\n",__FUNCTION__,__LINE__);
spi_dev_send_handle_pack_test(spidev,len,&recv_msg); /*send head msg*/
if(recv_msg.head == MSG_HEAD && recv_msg.tail == MSG_TAIL) {
if(len != recv_msg.len) {
//printk("%s len=%d rec_len=%d\n",__FUNCTION__,len,recv_msg.len);
len = (recv_msg.len >= len) ? recv_msg.len : len;
}
spidev->rx_cnt_in_tx_thread++;
rx_data_flag = 1;
if(len>4096)
printk("len(%d) err: \r\n",len);
}
//down(&spidev->wait_req);
ret = down_timeout(&spidev->wait_req, msecs_to_jiffies(100));
if (ret < 0) {
printk("wait req timeout\n");
}
up(&spidev->rec_data_msg_req);/*response master tx/rx dma set */
//printk("%s %d \r\n",__FUNCTION__,__LINE__);
status = spidev_sync_write_and_read(spidev,len);
if(rx_data_flag && spidev->is_data_check) {
ret = packet_check(spidev->rx_buffer,recv_msg.len);
if(ret) {
spidev->rx_data_check_err_cnt++;
//dev_info(&spi->dev,"%s packet check err \r\n",__FUNCTION__);
}else {
spidev->rx_data_check_ok_cnt++;
//dev_info(&spi->dev,"%s packet check success \r\n",__FUNCTION__);
}
}
spidev->tx_flag = 0;
gpio_set_value(spidev->gpio_ex,1);
return status;
}
static int spi_dev_slave_read_hand_msg_process_test(void *arg)
{
struct spi_device *spi = (struct spi_device *)arg;
struct spidev_data *spidev = spi_get_drvdata(spi);
while(1) {
down(&spidev->rec_head_msg_req);
//printk("%s %d \r\n",__FUNCTION__,__LINE__);
gpio_set_value(spidev->gpio_ex,0);
}
return 0;
}
static int spi_dev_slave_read_data_process_test(void *arg)
{
struct spi_device *spi = (struct spi_device *)arg;
struct spidev_data *spidev = spi_get_drvdata(spi);
struct spi_dev_hand_msg *recv_msg=(struct spi_dev_hand_msg *)spidev->rx_buffer;
while(1) {
down(&spidev->rec_data_msg_req);
//printk("%s %d \r\n",__FUNCTION__,__LINE__);
gpio_set_value(spidev->gpio_ex,1);
usleep_range(50,100);
gpio_set_value(spidev->gpio_ex,0);
}
return 0;
}
static int spi_dev_master_read_thread_test(void *arg)
{
struct spi_device *spi = (struct spi_device *)arg;
struct spidev_data *spidev = spi_get_drvdata(spi);
pid_t kid;
struct pid *pid;
struct task_struct * tsk;
struct spi_dev_hand_msg recv_msg;
int ret;
if(!spidev){
dev_info(&spi->dev,"spi_dev return \r\n");
return 0;
}
while(1) {
down(&spidev->rec_req); /*first receive slave req*/
spi_dev_recv_handle_pack_test(spidev, sizeof(struct spi_dev_hand_msg), &recv_msg);
gpio_set_value(spidev->gpio_ex,0);
ret = down_timeout(&spidev->wait_req, msecs_to_jiffies(100));
if (ret < 0) {
printk("%s wait req timeout\n",__FUNCTION__);
}
if(recv_msg.head == MSG_HEAD && recv_msg.tail == MSG_TAIL) {
int len = recv_msg.len;
spidev_sync_write_and_read(spidev, len); /*set dma and recv data msg*/
spidev->rx_cnt_in_rx_thread++;
if(spidev->is_data_check) {
ret = packet_check(spidev->rx_buffer,len);
if(ret) {
spidev->rx_data_check_err_cnt++;
//dev_info(&spi->dev,"%s packet check err \r\n",__FUNCTION__);
}else {
spidev->rx_data_check_ok_cnt++;
//dev_info(&spi->dev,"%s packet check success \r\n",__FUNCTION__);
}
}
gpio_set_value(spidev->gpio_ex,1);
//print_buf_data(spidev->rx_buffer, len);
}else {
printk("%s data invalid\n",__FUNCTION__);
gpio_set_value(spidev->gpio_ex,1);
}
}
return 0;
}
static int spi_dev_slave_read_thread_test(void *arg)
{
struct spi_device *spi = (struct spi_device *)arg;
struct spidev_data *spidev = spi_get_drvdata(spi);
int ret;
struct spi_dev_hand_msg recv_msg;
if(!spidev){
dev_info(&spi->dev,"spi_dev return \r\n");
return 0;
}
while(1) {
down(&spidev->rec_req); /*first receive master req*/
//printk("%s %d \r\n",__FUNCTION__,__LINE__);
up(&spidev->rec_head_msg_req);/*response master tx/rx dma set */
//printk("%s %d \r\n",__FUNCTION__,__LINE__);
//spidev_sync_write_and_read(spidev,sizeof(struct spi_dev_hand_msg)); /*set dma and recv head msg*/
spi_dev_recv_handle_pack_test(spidev, sizeof(struct spi_dev_hand_msg), &recv_msg);
//recv_msg=(struct spi_dev_hand_msg *)spidev->rx_buffer;
if(recv_msg.head == MSG_HEAD && recv_msg.tail == MSG_TAIL) {
int len = recv_msg.len;
up(&spidev->rec_data_msg_req); /*response master tx/rx dma set */
//printk("%s %d %d \r\n",__FUNCTION__,__LINE__,len);
spidev_sync_write_and_read(spidev, len); /*set dma and recv data msg*/
if(spidev->is_data_check) {
ret = packet_check(spidev->rx_buffer,len);
if(ret) {
spidev->rx_data_check_err_cnt++;
//dev_info(&spi->dev,"%s packet check err \r\n",__FUNCTION__);
}else {
spidev->rx_data_check_ok_cnt++;
//dev_info(&spi->dev,"%s packet check success \r\n",__FUNCTION__);
}
}
gpio_set_value(spidev->gpio_ex,1);
spidev->rx_cnt_in_rx_thread++;
//print_buf_data(spidev->rx_buffer, len);
}else {
up(&spidev->rec_data_msg_req);
printk("%s data invalid\n",__FUNCTION__);
gpio_set_value(spidev->gpio_ex,1);
}
}
return 0;
}
static int spidev_debug_test_init(struct spi_device *spi)
{
int ret = 0;
struct spidev_data *spidev = spi_get_drvdata(spi);
ret =spi_dev_pin_init_test(spi);
if(ret) {
dev_info(&spi->dev, "spi_dev_pin_init_test,ret=%d \n",ret);
return ret;
}
spin_lock_init(&spidev->tx_flag_lock);
sema_init(&spidev->wait_req, 0);
sema_init(&spidev->rec_req, 0);
sema_init(&spidev->rec_head_msg_req, 0);
sema_init(&spidev->rec_data_msg_req, 0);
spidev->tx_flag = 0;
spidev->rx_cnt_in_rx_thread = 0;
spidev->rx_cnt_in_tx_thread = 0;
spidev->is_data_check = false;
if (!spidev->tx_buffer) {
spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->tx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
return ret;
}
}
if (!spidev->rx_buffer) {
spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->rx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
return ret;
}
}
if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
kernel_thread(spi_dev_master_read_thread_test,spi, 0); /* fork the main thread */
}else {
kernel_thread(spi_dev_slave_read_thread_test,spi, 0); /* fork the main thread */
kernel_thread(spi_dev_slave_read_hand_msg_process_test,spi, 0);
kernel_thread(spi_dev_slave_read_data_process_test,spi, 0);
}
ret =spi_dev_irq_init_test(spi);
if(ret) {
dev_info(&spi->dev, "spi_dev_irq_init_test,ret=%d \n",ret);
return ret;
}
return ret;
}
static ssize_t spidevinfo_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf)
{
ssize_t count = 0;
struct device *dev = container_of(kobj, struct device, kobj);
//struct platform_device *pdev = container_of(dev, struct platform_device, dev);
unsigned char cmd_str[16] = {0};
u32 param1,param2,param3;
u8 rwaddr,rwsize;
int ret,i;
return count;
}
extern void get_random_bytes(void * buf, size_t len);
static ssize_t spidevinfo_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t n)
{
ssize_t ret =0;
struct device *dev = container_of(kobj, struct device, kobj);
//struct platform_device *pdev = container_of(dev, struct platform_device, dev);
struct spi_device *spi = (struct spi_device *)dev;
struct spidev_data *spidev = spi_get_drvdata(spi);
unsigned char cmd_str[0x20] = {0};
u8 bBuf[32];
u32 param1 = 0,param2 = 0,param3 = 0;
u32 rwaddr =0 ,rwsize = 0;
int i;
s8 rev = -1;
size_t count = 0;
dev_info(&spi->dev, "spidev->speed_hz:%d \n", spi->max_speed_hz);
sscanf(buf, "%31s %x %x %x", &cmd_str,&param1,&param2,&param3);
dev_info(dev, "cmd_str:%s,param1:%x,param2:%x,param3:%x\n",cmd_str,param1,param2,param3);
dev_info(&spi->dev, "mode %d, %s%s%s%s%u bits/w, %u Hz max --\n",
(int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
(spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
(spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
(spi->mode & SPI_3WIRE) ? "3wire, " : "",
(spi->mode & SPI_LOOP) ? "loopback, " : "",
spi->bits_per_word, spi->max_speed_hz);
count = param1;
ret = strcmp(cmd_str,"spi_write");
if( ret == 0) {
count = param1;
if (!spidev->tx_buffer) {
spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->tx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
return n;
}
}
dev_info(dev, "spidev->tx_buffer=0x%x\n",spidev->tx_buffer);
for(i = 0;i<count;i++) {
spidev->tx_buffer[i]=i;
}
print_buf_data(spidev->tx_buffer,count);
ret = spidev_sync_write(spidev, count);
if(ret == count) {
dev_info(dev, "send len success(len:%d) \n",ret);
}
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
dev_info(dev, "spi write end: \n");
}
ret = strcmp(cmd_str,"spi_read");
if(ret == 0) {
count = param1;
if (!spidev->rx_buffer) {
spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->rx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
return n;
}
}
memset(spidev->rx_buffer,0x0,bufsiz);
ret = spidev_sync_read(spidev, count);
if(ret == count) {
dev_info(dev, "read len success(len:%d) \n",ret);
print_buf_data(spidev->rx_buffer,count);
}
kfree(spidev->rx_buffer);
spidev->rx_buffer = NULL;
dev_info(dev, "spi read end: \n");
}
ret = strcmp(cmd_str,"write_then_read");
if(ret == 0) {
count = param1;
if (!spidev->tx_buffer) {
spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->tx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
return n;
}
}
if (!spidev->rx_buffer) {
spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->rx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
return n;
}
}
for(i = 0;i<count;i++) {
spidev->tx_buffer[i]=i;
}
//memset(spidev->rx_buffer,0x0,bufsiz);
ret = spi_write_then_read(spi, spidev->tx_buffer, count, spidev->rx_buffer, count);
if(ret == 0) {
dev_info(dev, "spi write data(%d bytes) \n",count);
print_buf_data(spidev->tx_buffer,count);
dev_info(dev, "spi read data(%d bytes) \n",count);
print_buf_data(spidev->rx_buffer,count);
}
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
kfree(spidev->rx_buffer);
spidev->rx_buffer = NULL;
dev_info(dev, "write_then_read.\n");
}
ret = strcmp(cmd_str,"write_and_read");
if(ret == 0) {
count = param1;
if (!spidev->tx_buffer) {
spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->tx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
return n;
}
}
if (!spidev->rx_buffer) {
spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->rx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
return n;
}
}
for(i = 0;i<count;i++) {
spidev->tx_buffer[i]=i;
}
memset(spidev->rx_buffer,0x0,bufsiz);
ret = spidev_sync_write_and_read(spidev, count);
if(ret == count) {
dev_info(dev, "spi write data(%d bytes) \n",ret);
print_buf_data(spidev->tx_buffer,count);
dev_info(dev, "spi read data(%d bytes) \n",ret);
print_buf_data(spidev->rx_buffer,count);
dev_info(dev, "write_and_read.\n");
}
#if 0
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
kfree(spidev->rx_buffer);
spidev->rx_buffer = NULL;
#endif
dev_info(dev, "write_and_read.\n");
}
ret = strcmp(cmd_str,"fifo_flush");
if(ret == 0) {
unsigned char buff[64] ={0};
ret = spidev_get_rxfifo(spi,buff);
dev_info(dev, "get rx_fifo_len(%d bytes) \n",ret);
print_buf_data(buff,ret);
}
ret = strcmp(cmd_str,"timing-set");
if(ret == 0) {
dev_info(dev, "timing param(%d) \n",param1);
set_spi_timing(spi,param1);
}
ret = strcmp(cmd_str,"loop-en");
if(ret == 0) {
spi->mode |= SPI_LOOP;
spi_setup(spi);
}
ret = strcmp(cmd_str,"loop-dis");
if(ret == 0) {
spi->mode &= ~SPI_LOOP;
spi_setup(spi);
}
ret = strcmp(cmd_str,"speed_set");
if(ret == 0) {
spi->max_speed_hz = param1;
spi_setup(spi);
}
ret = strcmp(cmd_str,"mode_set");
if(ret == 0) {
if(param1 != 0 && param1 != 1 && param1 != 2 && param1 != 3)
dev_info(dev, "param err(%d) \n",param1);
else
dev_info(dev, "set spi mode(%d) \n",param1);
spi->mode &= (~0x3);
spi->mode |= param1;
ret = spi_setup(spi);
dev_info(dev, "set spi mode(0x%x),ret=%d \n",spi->mode,ret);
}
ret = strcmp(cmd_str,"slave_mode_set");
if(ret == 0) {
if(param1 != 0 && param1 != 1 && param1 != 2 && param1 != 3)
dev_info(dev, "param err(%d) \n",param1);
else
dev_info(dev, "set spi mode(%d) \n",param1);
slave_mode_set(spi,param1);
}
ret = strcmp(cmd_str,"send_msg_rand_len");
if(ret == 0) {
count = 0;
int times = param1;
while(times--) {
get_random_bytes(&count,4);
count = (count%0x1000) + 1;
if (!spidev->tx_buffer) {
spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->tx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
return n;
}
}
if (!spidev->rx_buffer) {
spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->rx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
return n;
}
}
for(i = 0;i<count;i++) {
spidev->tx_buffer[i]=i;
}
//memset(spidev->rx_buffer,0x0,bufsiz);
if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
ret = spi_dev_send_one_pack_test(spi, count);
}
else {
ret = spi_dev_slave_send_one_pack_test(spi, count);
}
if(ret == count) {
#if 0
dev_info(dev, "spi write data(%d bytes) \n",ret);
print_buf_data(spidev->tx_buffer,count);
dev_info(dev, "spi read data(%d bytes) \n",ret);
print_buf_data(spidev->rx_buffer,count);
#endif
dev_info(dev, "write_and_read success. retain times:%d rx_cnt_in_tx_thread:%d spidev->rx_cnt_in_rx_thread:%d \n",
times,spidev->rx_cnt_in_tx_thread,spidev->rx_cnt_in_rx_thread);
}
msleep((count%5)+1);
//usleep_range(5+(count%10),20);
}
#if 0
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
kfree(spidev->rx_buffer);
spidev->rx_buffer = NULL;
#endif
}
ret = strcmp(cmd_str,"send_msg_fixed_len");
if(ret == 0) {
int times = param1;
int debug = param3;
count = param2;
if(count > 4096) {
printk("msg_fixed_len(%d bytes) out of range(4KB)\r\n",count);
return n;
}
while(times--) {
if (!spidev->tx_buffer) {
spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->tx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
return n;
}
}
if (!spidev->rx_buffer) {
spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->rx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
return n;
}
}
get_random_bytes(spidev->tx_buffer,count);
//memset(spidev->rx_buffer,0x0,bufsiz);
#if 0
for(i = 0;i<count;i++) {
spidev->tx_buffer[i]=i;
}
memset(spidev->rx_buffer,0x0,bufsiz);
#endif
if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
ret = spi_dev_send_one_pack_test(spi, count);
}
else {
ret = spi_dev_slave_send_one_pack_test(spi, count);
}
if(ret == count) {
if(debug) {
dev_info(dev, "spi write data(%d bytes) \n",ret);
print_buf_data(spidev->tx_buffer,count);
}
dev_info(dev, "write_and_read success. retain times:%d rx_cnt_in_tx_thread:%d spidev->rx_cnt_in_rx_thread:%d \n",
times,spidev->rx_cnt_in_tx_thread,spidev->rx_cnt_in_rx_thread);
}
msleep((count%5)+1);
}
#if 0
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
kfree(spidev->rx_buffer);
spidev->rx_buffer = NULL;
#endif
}
ret = strcmp(cmd_str,"data_check_ctrl");
if(ret == 0) {
if(param1) {
spidev->is_data_check = true;
spidev->rx_data_check_ok_cnt = 0;
spidev->rx_data_check_err_cnt = 0;
}
else {
spidev->is_data_check = false;
}
dev_info(dev, "rx_check_ok_cnt:%d rx_check_err_cnt:%d\n",spidev->rx_data_check_ok_cnt,spidev->rx_data_check_err_cnt);
}
ret = strcmp(cmd_str,"send_msg_with_check");
if(ret == 0) {
int times = param1;
int debug = param3;
count = param2;
if(count > 4096 || count < 4) {
printk("msg_fixed_len(%d bytes) out of range(4KB)\r\n",count);
return n;
}
while(times--) {
if (!spidev->tx_buffer) {
spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->tx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
return n;
}
}
if (!spidev->rx_buffer) {
spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
if (!spidev->rx_buffer) {
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
kfree(spidev->tx_buffer);
spidev->tx_buffer = NULL;
return n;
}
}
get_random_bytes(spidev->tx_buffer,count);
spidev->tx_buffer[0] = 0xa5;
spidev->tx_buffer[count-1] = 0x7e;
ret = data_to_packet(spidev->tx_buffer,count);
//memset(spidev->rx_buffer,0x0,bufsiz);
if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
ret = spi_dev_send_one_pack_test(spi, count);
}
else {
ret = spi_dev_slave_send_one_pack_test(spi, count);
}
if(ret == count) {
if(debug) {
dev_info(dev, "spi write data(%d bytes) \n",ret);
print_buf_data(spidev->tx_buffer,count);
}
dev_info(dev, "complete.retain:%d rx_cnt_in_tx_thread:%d spidev->rx_cnt_in_rx_thread:%d rx_check_ok_cnt:%d rx_check_err_cnt:%d\n",
times,spidev->rx_cnt_in_tx_thread,spidev->rx_cnt_in_rx_thread,
spidev->rx_data_check_ok_cnt,spidev->rx_data_check_err_cnt);
}
usleep_range(5+(count%10),20);
}
}
ret = strcmp(cmd_str,"gpio_out_val");
if(ret == 0) {
if(param1)
gpio_set_value(spidev->gpio_ex,1);
else
gpio_set_value(spidev->gpio_ex,0);
}
ret = strcmp(cmd_str,"test_ktime_get");
if(ret == 0) {
ktime_t k_time_start = 0;
ktime_t k_time_end = 0;
ktime_t diff = 0;
k_time_start = ktime_get();
gpio_set_value(spidev->gpio_ex,0);
do {
diff = ktime_sub(ktime_get(),k_time_start);
}while(diff <= (param1*1000));
gpio_set_value(spidev->gpio_ex,1);
printk("test ktime_get: start=%lld end=%lld diff=%lld \r\n",k_time_start,ktime_get(),diff);
}
return n;
}
SPIDEV_ATTR(spidevinfo);
static struct attribute * test_attr[] = {
&spidevinfo_attr.attr,
NULL,
};
static const struct attribute_group attr_group = {
.attrs = test_attr,
};
static const struct attribute_group *attr_groups[] = {
&attr_group,
NULL,
};
#endif
#ifdef TEST_SWAP_KERNEL_AND_USER
/* v3e
spi0(master)-------------------------------------spi1(slave)
GPIO129 <----------------------------------------INT4(GPIO51)
INT7(GPIO54) <-----------------------------------GPIO130
*/
/* v3 mdl
4#(master) --------------------------------------5#(slave)
GPIO130 <----------------------------------------INT6(GPIO53)
INT7(GPIO54) <-----------------------------------GPIO131
*/
//#define TEST_SPI_SLAVE
#ifdef TEST_SPI_SLAVE
#define GPIO_NUM_EX 131
#define GPIO_NUM_INT 53
#else
#define GPIO_NUM_EX 130
#define GPIO_NUM_INT 54
#endif
static int spi_dev_pin_init_test(struct spi_device *spi)
{
struct spidev_data *spidev = spi_get_drvdata(spi);
enum of_gpio_flags flags;
int status = 0;
spidev->pctrl = devm_pinctrl_get(&spi->dev);
if(!spidev->pctrl) {
dev_info(&spi->dev,"get dev pctrl failed!\n",status);
return status;
}
spidev->pint_ex = pinctrl_lookup_state(spidev->pctrl, "int_ex");
if (IS_ERR(spidev->pint_ex)) {
dev_err(&spi->dev, "TEST: missing pint_ex \n");
return status;
}
if (pinctrl_select_state(spidev->pctrl, spidev->pint_ex) < 0) {
dev_err(&spi->dev, "TEST: slect pint_ex \n");
return status;
}
if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
spidev->gpio_ex = GPIO_NUM_EX;
spidev->gpio_int = GPIO_NUM_INT;
} else {
spidev->gpio_ex = 130;
spidev->gpio_int = 51;
}
return status;
}
static void send_signal(int sig_no,void *dev_id)
{
int ret;
struct spidev_data *spidev = (struct spidev_data *)dev_id;
struct kernel_siginfo info;
struct task_struct * my_task = NULL;
//printk("send signal %d to pid %d \n",sig_no,spidev->pid);
memset(&info,0,sizeof(struct siginfo));
if(spidev->pid == 0) {
printk("send_signal pid is not valid \n");
return;
}
info.si_signo = sig_no;
info.si_code = gpio_get_value(spidev->gpio_int);
info.si_errno = gpio_get_value(spidev->gpio_ex);
rcu_read_lock();
my_task = pid_task(find_vpid(spidev->pid),PIDTYPE_PID);
rcu_read_unlock();
if(!my_task) {
printk("%s get pid_task failed! \n",__FUNCTION__);
return;
}
ret = send_sig_info(sig_no, &info, my_task);
if(ret < 0)
printk("send signal failed! \n");
}
static int spi_dev_sig_process_test(void *arg)
{
struct spi_device *spi = (struct spi_device *)arg;
struct spidev_data *spidev = spi_get_drvdata(spi);
while(1) {
down(&spidev->sig_req);
send_signal(SIGUSR1,spidev);
}
return 0;
}
static void send_dma_cfg_done_signal(int sig_no,void *dev_id)
{
int ret;
struct spidev_data *spidev = (struct spidev_data *)dev_id;
struct kernel_siginfo info;
struct task_struct * my_task = NULL;
int dma_cfg_done = 0;
//printk("send signal %d to pid %d \n",sig_no,spidev->pid);
memset(&info,0,sizeof(struct siginfo));
if(spidev->dma_cfg_done == 1) {
dma_cfg_done = spidev->dma_cfg_done;
spidev->dma_cfg_done= 0;
}
if(spidev->pid == 0) {
printk("%s is not valid\n",__FUNCTION__);
return;
}
info.si_signo = sig_no;
info.si_errno = dma_cfg_done;
rcu_read_lock();
my_task = pid_task(find_vpid(spidev->pid),PIDTYPE_PID);
rcu_read_unlock();
if(!my_task) {
printk("%s get pid_task failed! \n",__FUNCTION__);
return;
}
ret = send_sig_info(sig_no, &info, my_task);
if(ret < 0)
printk("send signal failed! \n");
}
static int spi_dev_dma_cfg_done_process_test(void *arg)
{
struct spi_device *spi = (struct spi_device *)arg;
struct spidev_data *spidev = spi_get_drvdata(spi);
while(1) {
down(&spidev->sem_dma_cfg_done);
send_dma_cfg_done_signal(SIGUSR2,spidev);
}
return 0;
}
static irqreturn_t spidev_hand_shake_irq(int irqno, void *dev_id)
{
struct spidev_data *spidev = (struct spidev_data *)dev_id;
int gpio_out_status = gpio_get_value(spidev->gpio_ex);
int gpio_int_status = gpio_get_value(spidev->gpio_int);
up(&spidev->sig_req);
dev_dbg(&spidev->spi->dev,"out=%d int=%d \r\n",gpio_out_status,gpio_int_status);
return IRQ_HANDLED;
}
static int spi_dev_irq_init_test(struct spi_device *spi)
{
struct spidev_data *spidev = spi_get_drvdata(spi);
int irq = 0,ret = 0;
if(!spi || !spidev) {
ret = -ENOENT;
return ret;
}
sema_init(&spidev->sig_req, 0);
sema_init(&spidev->sem_dma_cfg_done, 0);
kernel_thread(spi_dev_sig_process_test,spi, 0); /* fork the main thread */
kernel_thread(spi_dev_dma_cfg_done_process_test,spi, 0); /* fork the main thread */
irq = irq_of_parse_and_map(spi->dev.of_node, 0);
if (irq <= 0) {
dev_err(&spi->dev, "ERROR: invalid interrupt number, irq = %d\n",irq);
return -EBUSY;
}
spidev->irq = irq;
dev_info(&spi->dev, "used interrupt num is %d\n", spidev->irq);
ret = devm_request_irq(&spi->dev, spidev->irq, spidev_hand_shake_irq,
0, dev_name(&spi->dev), spidev);
if (ret < 0) {
dev_err(&spi->dev, "probe - cannot get IRQ (%d)\n", ret);
return ret;
}
return ret;
}
#endif
#ifdef TEST_SWAP_KERNEL_AND_USER
void spi_dev_send_dma_cfg_down(struct spi_device *spi)
{
struct spidev_data *spidev = spi_get_drvdata(spi);
spidev->dma_cfg_done = 1;
up(&spidev->sem_dma_cfg_done);
}
#else
void spi_dev_send_dma_cfg_down(struct spi_device *spi)
{
return;
}
#endif
/*-------------------------------------------------------------------------*/
static int spidev_probe(struct spi_device *spi)
{
struct spidev_data *spidev;
int status;
unsigned long minor;
u32 val;
/*
* spidev should never be referenced in DT without a specific
* compatible string, it is a Linux implementation thing
* rather than a description of the hardware.
*/
WARN(spi->dev.of_node &&
of_device_is_compatible(spi->dev.of_node, "spidev"),
"%pOF: buggy DT: spidev listed directly in DT\n", spi->dev.of_node);
spidev_probe_acpi(spi);
/* Allocate driver data */
spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
if (!spidev)
return -ENOMEM;
/* Initialize the driver data */
spidev->spi = spi;
spin_lock_init(&spidev->spi_lock);
mutex_init(&spidev->buf_lock);
INIT_LIST_HEAD(&spidev->device_entry);
if (device_property_read_u32(&spi->dev, "enable_dma",&val)) {
spi->dma_used = 0;
dev_err(&spi->dev,"enable_dma get failed");
}
else {
spi->dma_used = val;
dev_info(&spi->dev,"enable_dma = 0x%x",val);
}
if (device_property_read_u32(&spi->dev, "enable_trans_gap",&val)) {
spi->trans_gaped = 0;
dev_err(&spi->dev,"enable_trans_gap get failed");
}
else {
spi->trans_gaped = val;
dev_info(&spi->dev,"enable_trans_gap = 0x%x",val);
}
if (device_property_read_u32(&spi->dev, "trans_gap_num",&val)) {
spi->trans_gap_num = 0;
dev_err(&spi->dev,"trans_gap_num get failed");
}
else {
spi->trans_gap_num = val;
dev_info(&spi->dev,"trans_gap_num = 0x%x",val);
}
// yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme.
/* If we can allocate a minor number, hook up this device.
* Reusing minors is fine so long as udev or mdev is working.
*/
mutex_lock(&device_list_lock);
minor = find_first_zero_bit(minors, N_SPI_MINORS);
if (minor < N_SPI_MINORS) {
struct device *dev;
spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
dev = device_create(spidev_class, &spi->dev, spidev->devt,
spidev, "spidev%d.%d",
spi->master->bus_num, spi->chip_select);
status = PTR_ERR_OR_ZERO(dev);
} else {
dev_dbg(&spi->dev, "no minor number available!\n");
status = -ENODEV;
}
if (status == 0) {
set_bit(minor, minors);
list_add(&spidev->device_entry, &device_list);
}
mutex_unlock(&device_list_lock);
spidev->speed_hz = spi->max_speed_hz;
spidev->rd_from_rx_buffer = 0;
if (status == 0)
spi_set_drvdata(spi, spidev);
else
kfree(spidev);
spi_setup(spi);
if(0 == status && spi->master->slave)
device_init_wakeup(&spi->dev, true);
#ifdef SPIDEV_DEBUG
int ret = sysfs_create_groups(&spi->dev.kobj, attr_groups);
if (ret) {
dev_err(&spi->dev, "create test_kobj attr group fain error=%d\n", ret);
return ret;
}
ret = spidev_debug_test_init(spi);
if (ret) {
dev_err(&spi->dev, "spidev_debug_test_init error=%d\n", ret);
return ret;
}
#endif
#ifdef TEST_SWAP_KERNEL_AND_USER
int ret;
spidev->dma_cfg_done = 0;
spidev->pid = 0;
ret =spi_dev_pin_init_test(spi);
if(ret) {
dev_info(&spi->dev, "spi_dev_pin_init_test,ret=%d \n",ret);
return ret;
}
ret =spi_dev_irq_init_test(spi);
if(ret) {
dev_info(&spi->dev, "spi_dev_irq_init_test,ret=%d \n",ret);
return ret;
}
#endif
return status;
}
static int spidev_remove(struct spi_device *spi)
{
struct spidev_data *spidev = spi_get_drvdata(spi);
/* prevent new opens */
mutex_lock(&device_list_lock);
/* make sure ops on existing fds can abort cleanly */
spin_lock_irq(&spidev->spi_lock);
spidev->spi = NULL;
spin_unlock_irq(&spidev->spi_lock);
list_del(&spidev->device_entry);
device_destroy(spidev_class, spidev->devt);
clear_bit(MINOR(spidev->devt), minors);
if (spidev->users == 0)
kfree(spidev);
mutex_unlock(&device_list_lock);
return 0;
}
static struct spi_driver spidev_spi_driver = {
.driver = {
.name = "spidev",
.of_match_table = of_match_ptr(spidev_dt_ids),
.acpi_match_table = ACPI_PTR(spidev_acpi_ids),
},
.probe = spidev_probe,
.remove = spidev_remove,
/* NOTE: suspend/resume methods are not necessary here.
* We don't do anything except pass the requests to/from
* the underlying controller. The refrigerator handles
* most issues; the controller driver handles the rest.
*/
};
/*-------------------------------------------------------------------------*/
static int __init spidev_init(void)
{
int status;
/* Claim our 256 reserved device numbers. Then register a class
* that will key udev/mdev to add/remove /dev nodes. Last, register
* the driver which manages those device numbers.
*/
BUILD_BUG_ON(N_SPI_MINORS > 256);
status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
if (status < 0)
return status;
spidev_class = class_create(THIS_MODULE, "spidev");
if (IS_ERR(spidev_class)) {
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
return PTR_ERR(spidev_class);
}
status = spi_register_driver(&spidev_spi_driver);
if (status < 0) {
class_destroy(spidev_class);
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
}
return status;
}
module_init(spidev_init);
static void __exit spidev_exit(void)
{
spi_unregister_driver(&spidev_spi_driver);
class_destroy(spidev_class);
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
}
module_exit(spidev_exit);
MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
MODULE_DESCRIPTION("User mode SPI device interface");
MODULE_LICENSE("GPL");
MODULE_ALIAS("spi:spidev");