2007-05-08 11:32:15 +04:00
|
|
|
/*
|
2011-06-06 11:16:30 +04:00
|
|
|
* Simple synchronous userspace interface to SPI devices
|
2007-05-08 11:32:15 +04:00
|
|
|
*
|
|
|
|
* Copyright (C) 2006 SWAPP
|
|
|
|
* Andrea Paterniani <a.paterniani@swapp-eng.it>
|
|
|
|
* Copyright (C) 2007 David Brownell (simplification, cleanup)
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/ioctl.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/device.h>
|
2008-06-06 09:45:50 +04:00
|
|
|
#include <linux/err.h>
|
2007-05-08 11:32:15 +04:00
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
#include <linux/slab.h>
|
2011-02-03 11:37:18 +03:00
|
|
|
#include <linux/compat.h>
|
2012-10-31 14:30:08 +04:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_device.h>
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
#include <linux/spi/spi.h>
|
|
|
|
#include <linux/spi/spidev.h>
|
|
|
|
|
2013-10-14 05:36:54 +04:00
|
|
|
#include <linux/uaccess.h>
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
tree-wide: fix comment/printk typos
"gadget", "through", "command", "maintain", "maintain", "controller", "address",
"between", "initiali[zs]e", "instead", "function", "select", "already",
"equal", "access", "management", "hierarchy", "registration", "interest",
"relative", "memory", "offset", "already",
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
2010-11-01 22:38:34 +03:00
|
|
|
* This supports access to SPI devices using normal userspace I/O calls.
|
2007-05-08 11:32:15 +04:00
|
|
|
* Note that while traditional UNIX/POSIX I/O semantics are half duplex,
|
|
|
|
* and often mask message boundaries, full SPI support requires full duplex
|
2009-10-22 23:34:29 +04:00
|
|
|
* transfers. There are several kinds of internal message boundaries to
|
2007-05-08 11:32:15 +04:00
|
|
|
* 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 */
|
|
|
|
|
2009-12-15 01:20:23 +03:00
|
|
|
static DECLARE_BITMAP(minors, N_SPI_MINORS);
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
|
2007-07-31 11:38:43 +04:00
|
|
|
/* Bit masks for spi_device.mode management. Note that incorrect
|
2009-06-30 22:41:26 +04:00
|
|
|
* settings for some settings can cause *lots* of trouble for other
|
|
|
|
* devices on a shared bus:
|
2007-07-31 11:38:43 +04:00
|
|
|
*
|
2009-06-30 22:41:26 +04:00
|
|
|
* - 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?
|
2007-07-31 11:38:43 +04:00
|
|
|
*/
|
|
|
|
#define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
|
2009-06-30 22:41:26 +04:00
|
|
|
| SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
|
2014-02-25 14:40:17 +04:00
|
|
|
| SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
|
|
|
|
| SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
struct spidev_data {
|
2008-06-06 09:45:50 +04:00
|
|
|
dev_t devt;
|
2008-05-24 00:05:03 +04:00
|
|
|
spinlock_t spi_lock;
|
2007-05-08 11:32:15 +04:00
|
|
|
struct spi_device *spi;
|
|
|
|
struct list_head device_entry;
|
|
|
|
|
2014-10-09 22:19:25 +04:00
|
|
|
/* TX/RX buffers are NULL unless this device is open (users > 0) */
|
2007-05-08 11:32:15 +04:00
|
|
|
struct mutex buf_lock;
|
|
|
|
unsigned users;
|
2014-10-09 22:19:25 +04:00
|
|
|
u8 *tx_buffer;
|
|
|
|
u8 *rx_buffer;
|
2014-11-08 13:28:10 +03:00
|
|
|
u32 speed_hz;
|
2007-05-08 11:32:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2008-05-24 00:05:03 +04:00
|
|
|
/*
|
|
|
|
* We can't use the standard synchronous wrappers for file I/O; we
|
|
|
|
* need to protect against async removal of the underlying spi_device.
|
|
|
|
*/
|
|
|
|
static void spidev_complete(void *arg)
|
|
|
|
{
|
|
|
|
complete(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
spidev_sync(struct spidev_data *spidev, struct spi_message *message)
|
|
|
|
{
|
|
|
|
DECLARE_COMPLETION_ONSTACK(done);
|
|
|
|
int status;
|
|
|
|
|
|
|
|
message->complete = spidev_complete;
|
|
|
|
message->context = &done;
|
|
|
|
|
|
|
|
spin_lock_irq(&spidev->spi_lock);
|
|
|
|
if (spidev->spi == NULL)
|
|
|
|
status = -ESHUTDOWN;
|
|
|
|
else
|
|
|
|
status = spi_async(spidev->spi, message);
|
|
|
|
spin_unlock_irq(&spidev->spi_lock);
|
|
|
|
|
|
|
|
if (status == 0) {
|
|
|
|
wait_for_completion(&done);
|
|
|
|
status = message->status;
|
|
|
|
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 = {
|
2014-10-09 22:19:25 +04:00
|
|
|
.tx_buf = spidev->tx_buffer,
|
2008-05-24 00:05:03 +04:00
|
|
|
.len = len,
|
2014-11-08 13:28:10 +03:00
|
|
|
.speed_hz = spidev->speed_hz,
|
2008-05-24 00:05:03 +04:00
|
|
|
};
|
|
|
|
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 = {
|
2014-10-09 22:19:25 +04:00
|
|
|
.rx_buf = spidev->rx_buffer,
|
2008-05-24 00:05:03 +04:00
|
|
|
.len = len,
|
2014-11-08 13:28:10 +03:00
|
|
|
.speed_hz = spidev->speed_hz,
|
2008-05-24 00:05:03 +04:00
|
|
|
};
|
|
|
|
struct spi_message m;
|
|
|
|
|
|
|
|
spi_message_init(&m);
|
|
|
|
spi_message_add_tail(&t, &m);
|
|
|
|
return spidev_sync(spidev, &m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2007-05-08 11:32:15 +04:00
|
|
|
/* 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 = 0;
|
|
|
|
|
|
|
|
/* chipselect only toggles at start or end of operation */
|
|
|
|
if (count > bufsiz)
|
|
|
|
return -EMSGSIZE;
|
|
|
|
|
|
|
|
spidev = filp->private_data;
|
|
|
|
|
|
|
|
mutex_lock(&spidev->buf_lock);
|
2008-05-24 00:05:03 +04:00
|
|
|
status = spidev_sync_read(spidev, count);
|
2008-07-04 20:59:56 +04:00
|
|
|
if (status > 0) {
|
2007-05-08 11:32:15 +04:00
|
|
|
unsigned long missing;
|
|
|
|
|
2014-10-09 22:19:25 +04:00
|
|
|
missing = copy_to_user(buf, spidev->rx_buffer, status);
|
2008-07-04 20:59:56 +04:00
|
|
|
if (missing == status)
|
2007-05-08 11:32:15 +04:00
|
|
|
status = -EFAULT;
|
|
|
|
else
|
2008-07-04 20:59:56 +04:00
|
|
|
status = status - missing;
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
|
|
|
mutex_unlock(&spidev->buf_lock);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 = 0;
|
|
|
|
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);
|
2014-10-09 22:19:25 +04:00
|
|
|
missing = copy_from_user(spidev->tx_buffer, buf, count);
|
2013-10-14 05:36:54 +04:00
|
|
|
if (missing == 0)
|
2008-05-24 00:05:03 +04:00
|
|
|
status = spidev_sync_write(spidev, count);
|
2013-10-14 05:36:54 +04:00
|
|
|
else
|
2007-05-08 11:32:15 +04:00
|
|
|
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;
|
2014-10-09 22:19:25 +04:00
|
|
|
u8 *tx_buf, *rx_buf;
|
2007-05-08 11:32:15 +04:00
|
|
|
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.
|
|
|
|
*/
|
2014-10-09 22:19:25 +04:00
|
|
|
tx_buf = spidev->tx_buffer;
|
|
|
|
rx_buf = spidev->rx_buffer;
|
2007-05-08 11:32:15 +04:00
|
|
|
total = 0;
|
|
|
|
for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
|
|
|
|
n;
|
|
|
|
n--, k_tmp++, u_tmp++) {
|
|
|
|
k_tmp->len = u_tmp->len;
|
|
|
|
|
2007-05-24 00:57:39 +04:00
|
|
|
total += k_tmp->len;
|
|
|
|
if (total > bufsiz) {
|
|
|
|
status = -EMSGSIZE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2007-05-08 11:32:15 +04:00
|
|
|
if (u_tmp->rx_buf) {
|
2014-10-09 22:19:25 +04:00
|
|
|
k_tmp->rx_buf = rx_buf;
|
2007-08-11 00:01:09 +04:00
|
|
|
if (!access_ok(VERIFY_WRITE, (u8 __user *)
|
2007-10-29 08:11:28 +03:00
|
|
|
(uintptr_t) u_tmp->rx_buf,
|
2007-08-11 00:01:09 +04:00
|
|
|
u_tmp->len))
|
2007-05-08 11:32:15 +04:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (u_tmp->tx_buf) {
|
2014-10-09 22:19:25 +04:00
|
|
|
k_tmp->tx_buf = tx_buf;
|
|
|
|
if (copy_from_user(tx_buf, (const u8 __user *)
|
2007-10-29 08:11:28 +03:00
|
|
|
(uintptr_t) u_tmp->tx_buf,
|
2007-05-08 11:32:15 +04:00
|
|
|
u_tmp->len))
|
|
|
|
goto done;
|
|
|
|
}
|
2014-10-09 22:19:25 +04:00
|
|
|
tx_buf += k_tmp->len;
|
|
|
|
rx_buf += k_tmp->len;
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
k_tmp->cs_change = !!u_tmp->cs_change;
|
2014-02-25 14:40:17 +04:00
|
|
|
k_tmp->tx_nbits = u_tmp->tx_nbits;
|
|
|
|
k_tmp->rx_nbits = u_tmp->rx_nbits;
|
2007-05-08 11:32:15 +04:00
|
|
|
k_tmp->bits_per_word = u_tmp->bits_per_word;
|
|
|
|
k_tmp->delay_usecs = u_tmp->delay_usecs;
|
|
|
|
k_tmp->speed_hz = u_tmp->speed_hz;
|
2014-11-08 13:28:10 +03:00
|
|
|
if (!k_tmp->speed_hz)
|
|
|
|
k_tmp->speed_hz = spidev->speed_hz;
|
2007-05-08 11:32:15 +04:00
|
|
|
#ifdef VERBOSE
|
2009-12-07 17:28:43 +03:00
|
|
|
dev_dbg(&spidev->spi->dev,
|
2007-05-08 11:32:15 +04:00
|
|
|
" xfer len %zd %s%s%s%dbits %u usec %uHz\n",
|
|
|
|
u_tmp->len,
|
|
|
|
u_tmp->rx_buf ? "rx " : "",
|
|
|
|
u_tmp->tx_buf ? "tx " : "",
|
|
|
|
u_tmp->cs_change ? "cs " : "",
|
2009-12-07 17:28:43 +03:00
|
|
|
u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
|
2007-05-08 11:32:15 +04:00
|
|
|
u_tmp->delay_usecs,
|
2009-12-07 17:28:43 +03:00
|
|
|
u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
|
2007-05-08 11:32:15 +04:00
|
|
|
#endif
|
|
|
|
spi_message_add_tail(k_tmp, &msg);
|
|
|
|
}
|
|
|
|
|
2008-05-24 00:05:03 +04:00
|
|
|
status = spidev_sync(spidev, &msg);
|
2007-05-08 11:32:15 +04:00
|
|
|
if (status < 0)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
/* copy any rx data out of bounce buffer */
|
2014-10-09 22:19:25 +04:00
|
|
|
rx_buf = spidev->rx_buffer;
|
2007-05-08 11:32:15 +04:00
|
|
|
for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
|
|
|
|
if (u_tmp->rx_buf) {
|
2007-07-17 15:04:04 +04:00
|
|
|
if (__copy_to_user((u8 __user *)
|
2014-10-09 22:19:25 +04:00
|
|
|
(uintptr_t) u_tmp->rx_buf, rx_buf,
|
2007-05-08 11:32:15 +04:00
|
|
|
u_tmp->len)) {
|
|
|
|
status = -EFAULT;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 22:19:25 +04:00
|
|
|
rx_buf += u_tmp->len;
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
|
|
|
status = total;
|
|
|
|
|
|
|
|
done:
|
|
|
|
kfree(k_xfers);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2015-01-30 21:43:33 +03:00
|
|
|
static struct spi_ioc_transfer *
|
|
|
|
spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
|
|
|
|
unsigned *n_ioc)
|
|
|
|
{
|
|
|
|
struct spi_ioc_transfer *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 */
|
|
|
|
ioc = kmalloc(tmp, GFP_KERNEL);
|
|
|
|
if (!ioc)
|
|
|
|
return ERR_PTR(-ENOMEM);
|
|
|
|
if (__copy_from_user(ioc, u_ioc, tmp)) {
|
|
|
|
kfree(ioc);
|
|
|
|
return ERR_PTR(-EFAULT);
|
|
|
|
}
|
|
|
|
return ioc;
|
|
|
|
}
|
|
|
|
|
2008-07-24 08:29:55 +04:00
|
|
|
static long
|
|
|
|
spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
2007-05-08 11:32:15 +04:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Check access direction once here; don't repeat below.
|
|
|
|
* IOC_DIR is from the user perspective, while access_ok is
|
|
|
|
* from the kernel perspective; so they look reversed.
|
|
|
|
*/
|
|
|
|
if (_IOC_DIR(cmd) & _IOC_READ)
|
|
|
|
err = !access_ok(VERIFY_WRITE,
|
|
|
|
(void __user *)arg, _IOC_SIZE(cmd));
|
|
|
|
if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
|
|
|
|
err = !access_ok(VERIFY_READ,
|
|
|
|
(void __user *)arg, _IOC_SIZE(cmd));
|
|
|
|
if (err)
|
|
|
|
return -EFAULT;
|
|
|
|
|
2008-05-24 00:05:03 +04:00
|
|
|
/* guard against device removal before, or while,
|
|
|
|
* we issue this ioctl.
|
|
|
|
*/
|
2007-05-08 11:32:15 +04:00
|
|
|
spidev = filp->private_data;
|
2008-05-24 00:05:03 +04:00
|
|
|
spin_lock_irq(&spidev->spi_lock);
|
|
|
|
spi = spi_dev_get(spidev->spi);
|
|
|
|
spin_unlock_irq(&spidev->spi_lock);
|
|
|
|
|
|
|
|
if (spi == NULL)
|
|
|
|
return -ESHUTDOWN;
|
2007-05-08 11:32:15 +04:00
|
|
|
|
2008-07-24 08:29:55 +04:00
|
|
|
/* 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);
|
|
|
|
|
2007-05-08 11:32:15 +04:00
|
|
|
switch (cmd) {
|
|
|
|
/* read requests */
|
|
|
|
case SPI_IOC_RD_MODE:
|
|
|
|
retval = __put_user(spi->mode & SPI_MODE_MASK,
|
|
|
|
(__u8 __user *)arg);
|
|
|
|
break;
|
2014-02-25 14:40:17 +04:00
|
|
|
case SPI_IOC_RD_MODE32:
|
|
|
|
retval = __put_user(spi->mode & SPI_MODE_MASK,
|
|
|
|
(__u32 __user *)arg);
|
|
|
|
break;
|
2007-05-08 11:32:15 +04:00
|
|
|
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:
|
2014-11-08 13:28:10 +03:00
|
|
|
retval = __put_user(spidev->speed_hz, (__u32 __user *)arg);
|
2007-05-08 11:32:15 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* write requests */
|
|
|
|
case SPI_IOC_WR_MODE:
|
2014-02-25 14:40:17 +04:00
|
|
|
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);
|
2007-05-08 11:32:15 +04:00
|
|
|
if (retval == 0) {
|
2014-02-25 14:40:16 +04:00
|
|
|
u32 save = spi->mode;
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
if (tmp & ~SPI_MODE_MASK) {
|
|
|
|
retval = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp |= spi->mode & ~SPI_MODE_MASK;
|
2014-02-25 14:40:17 +04:00
|
|
|
spi->mode = (u16)tmp;
|
2007-05-08 11:32:15 +04:00
|
|
|
retval = spi_setup(spi);
|
|
|
|
if (retval < 0)
|
|
|
|
spi->mode = save;
|
|
|
|
else
|
2014-02-25 14:40:17 +04:00
|
|
|
dev_dbg(&spi->dev, "spi mode %x\n", tmp);
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SPI_IOC_WR_LSB_FIRST:
|
|
|
|
retval = __get_user(tmp, (__u8 __user *)arg);
|
|
|
|
if (retval == 0) {
|
2014-02-25 14:40:16 +04:00
|
|
|
u32 save = spi->mode;
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
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);
|
2014-11-08 13:28:10 +03:00
|
|
|
if (retval >= 0)
|
|
|
|
spidev->speed_hz = tmp;
|
2007-05-08 11:32:15 +04:00
|
|
|
else
|
|
|
|
dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
|
2014-11-08 13:28:10 +03:00
|
|
|
spi->max_speed_hz = save;
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* segmented and/or full-duplex I/O request */
|
2015-01-30 21:43:33 +03:00
|
|
|
/* 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);
|
2007-05-08 11:32:15 +04:00
|
|
|
break;
|
|
|
|
}
|
2015-01-30 21:43:33 +03:00
|
|
|
if (!ioc)
|
|
|
|
break; /* n_ioc is also 0 */
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
/* translate to spi_message, execute */
|
|
|
|
retval = spidev_message(spidev, ioc, n_ioc);
|
|
|
|
kfree(ioc);
|
|
|
|
break;
|
|
|
|
}
|
2008-07-24 08:29:55 +04:00
|
|
|
|
|
|
|
mutex_unlock(&spidev->buf_lock);
|
2008-05-24 00:05:03 +04:00
|
|
|
spi_dev_put(spi);
|
2007-05-08 11:32:15 +04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2011-02-03 11:37:18 +03:00
|
|
|
#ifdef CONFIG_COMPAT
|
2015-01-30 21:43:33 +03:00
|
|
|
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);
|
|
|
|
if (!access_ok(VERIFY_READ, u_ioc, _IOC_SIZE(cmd)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2011-02-03 11:37:18 +03:00
|
|
|
static long
|
|
|
|
spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
|
|
|
|
{
|
2015-01-30 21:43:33 +03:00
|
|
|
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);
|
|
|
|
|
2011-02-03 11:37:18 +03:00
|
|
|
return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define spidev_compat_ioctl NULL
|
|
|
|
#endif /* CONFIG_COMPAT */
|
|
|
|
|
2007-05-08 11:32:15 +04:00
|
|
|
static int spidev_open(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct spidev_data *spidev;
|
|
|
|
int status = -ENXIO;
|
|
|
|
|
|
|
|
mutex_lock(&device_list_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(spidev, &device_list, device_entry) {
|
2008-06-06 09:45:50 +04:00
|
|
|
if (spidev->devt == inode->i_rdev) {
|
2007-05-08 11:32:15 +04:00
|
|
|
status = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-10-09 22:19:25 +04:00
|
|
|
|
|
|
|
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) {
|
2007-05-08 11:32:15 +04:00
|
|
|
dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
|
|
|
|
status = -ENOMEM;
|
2014-10-09 22:19:25 +04:00
|
|
|
goto err_find_dev;
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
|
|
|
}
|
2014-10-09 22:19:25 +04:00
|
|
|
|
|
|
|
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;
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
2014-10-09 22:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
spidev->users++;
|
|
|
|
filp->private_data = spidev;
|
|
|
|
nonseekable_open(inode, filp);
|
2007-05-08 11:32:15 +04:00
|
|
|
|
2014-10-09 22:19:25 +04:00
|
|
|
mutex_unlock(&device_list_lock);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_alloc_rx_buf:
|
|
|
|
kfree(spidev->tx_buffer);
|
|
|
|
spidev->tx_buffer = NULL;
|
|
|
|
err_find_dev:
|
2007-05-08 11:32:15 +04:00
|
|
|
mutex_unlock(&device_list_lock);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int spidev_release(struct inode *inode, struct file *filp)
|
|
|
|
{
|
|
|
|
struct spidev_data *spidev;
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
mutex_lock(&device_list_lock);
|
|
|
|
spidev = filp->private_data;
|
|
|
|
filp->private_data = NULL;
|
2008-06-06 09:45:50 +04:00
|
|
|
|
|
|
|
/* last close? */
|
2007-05-08 11:32:15 +04:00
|
|
|
spidev->users--;
|
|
|
|
if (!spidev->users) {
|
2008-06-06 09:45:50 +04:00
|
|
|
int dofree;
|
|
|
|
|
2014-10-09 22:19:25 +04:00
|
|
|
kfree(spidev->tx_buffer);
|
|
|
|
spidev->tx_buffer = NULL;
|
|
|
|
|
|
|
|
kfree(spidev->rx_buffer);
|
|
|
|
spidev->rx_buffer = NULL;
|
2008-06-06 09:45:50 +04:00
|
|
|
|
2014-11-08 13:28:10 +03:00
|
|
|
spidev->speed_hz = spidev->spi->max_speed_hz;
|
|
|
|
|
2008-06-06 09:45:50 +04:00
|
|
|
/* ... after we unbound from the underlying device? */
|
|
|
|
spin_lock_irq(&spidev->spi_lock);
|
|
|
|
dofree = (spidev->spi == NULL);
|
|
|
|
spin_unlock_irq(&spidev->spi_lock);
|
|
|
|
|
|
|
|
if (dofree)
|
|
|
|
kfree(spidev);
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
|
|
|
mutex_unlock(&device_list_lock);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2009-10-02 02:43:56 +04:00
|
|
|
static const struct file_operations spidev_fops = {
|
2007-05-08 11:32:15 +04:00
|
|
|
.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,
|
2008-07-24 08:29:55 +04:00
|
|
|
.unlocked_ioctl = spidev_ioctl,
|
2011-02-03 11:37:18 +03:00
|
|
|
.compat_ioctl = spidev_compat_ioctl,
|
2007-05-08 11:32:15 +04:00
|
|
|
.open = spidev_open,
|
|
|
|
.release = spidev_release,
|
llseek: automatically add .llseek fop
All file_operations should get a .llseek operation so we can make
nonseekable_open the default for future file operations without a
.llseek pointer.
The three cases that we can automatically detect are no_llseek, seq_lseek
and default_llseek. For cases where we can we can automatically prove that
the file offset is always ignored, we use noop_llseek, which maintains
the current behavior of not returning an error from a seek.
New drivers should normally not use noop_llseek but instead use no_llseek
and call nonseekable_open at open time. Existing drivers can be converted
to do the same when the maintainer knows for certain that no user code
relies on calling seek on the device file.
The generated code is often incorrectly indented and right now contains
comments that clarify for each added line why a specific variant was
chosen. In the version that gets submitted upstream, the comments will
be gone and I will manually fix the indentation, because there does not
seem to be a way to do that using coccinelle.
Some amount of new code is currently sitting in linux-next that should get
the same modifications, which I will do at the end of the merge window.
Many thanks to Julia Lawall for helping me learn to write a semantic
patch that does all this.
===== begin semantic patch =====
// This adds an llseek= method to all file operations,
// as a preparation for making no_llseek the default.
//
// The rules are
// - use no_llseek explicitly if we do nonseekable_open
// - use seq_lseek for sequential files
// - use default_llseek if we know we access f_pos
// - use noop_llseek if we know we don't access f_pos,
// but we still want to allow users to call lseek
//
@ open1 exists @
identifier nested_open;
@@
nested_open(...)
{
<+...
nonseekable_open(...)
...+>
}
@ open exists@
identifier open_f;
identifier i, f;
identifier open1.nested_open;
@@
int open_f(struct inode *i, struct file *f)
{
<+...
(
nonseekable_open(...)
|
nested_open(...)
)
...+>
}
@ read disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ read_no_fpos disable optional_qualifier exists @
identifier read_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t read_f(struct file *f, char *p, size_t s, loff_t *off)
{
... when != off
}
@ write @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
expression E;
identifier func;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
<+...
(
*off = E
|
*off += E
|
func(..., off, ...)
|
E = *off
)
...+>
}
@ write_no_fpos @
identifier write_f;
identifier f, p, s, off;
type ssize_t, size_t, loff_t;
@@
ssize_t write_f(struct file *f, const char *p, size_t s, loff_t *off)
{
... when != off
}
@ fops0 @
identifier fops;
@@
struct file_operations fops = {
...
};
@ has_llseek depends on fops0 @
identifier fops0.fops;
identifier llseek_f;
@@
struct file_operations fops = {
...
.llseek = llseek_f,
...
};
@ has_read depends on fops0 @
identifier fops0.fops;
identifier read_f;
@@
struct file_operations fops = {
...
.read = read_f,
...
};
@ has_write depends on fops0 @
identifier fops0.fops;
identifier write_f;
@@
struct file_operations fops = {
...
.write = write_f,
...
};
@ has_open depends on fops0 @
identifier fops0.fops;
identifier open_f;
@@
struct file_operations fops = {
...
.open = open_f,
...
};
// use no_llseek if we call nonseekable_open
////////////////////////////////////////////
@ nonseekable1 depends on !has_llseek && has_open @
identifier fops0.fops;
identifier nso ~= "nonseekable_open";
@@
struct file_operations fops = {
... .open = nso, ...
+.llseek = no_llseek, /* nonseekable */
};
@ nonseekable2 depends on !has_llseek @
identifier fops0.fops;
identifier open.open_f;
@@
struct file_operations fops = {
... .open = open_f, ...
+.llseek = no_llseek, /* open uses nonseekable */
};
// use seq_lseek for sequential files
/////////////////////////////////////
@ seq depends on !has_llseek @
identifier fops0.fops;
identifier sr ~= "seq_read";
@@
struct file_operations fops = {
... .read = sr, ...
+.llseek = seq_lseek, /* we have seq_read */
};
// use default_llseek if there is a readdir
///////////////////////////////////////////
@ fops1 depends on !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier readdir_e;
@@
// any other fop is used that changes pos
struct file_operations fops = {
... .readdir = readdir_e, ...
+.llseek = default_llseek, /* readdir is present */
};
// use default_llseek if at least one of read/write touches f_pos
/////////////////////////////////////////////////////////////////
@ fops2 depends on !fops1 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read.read_f;
@@
// read fops use offset
struct file_operations fops = {
... .read = read_f, ...
+.llseek = default_llseek, /* read accesses f_pos */
};
@ fops3 depends on !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write.write_f;
@@
// write fops use offset
struct file_operations fops = {
... .write = write_f, ...
+ .llseek = default_llseek, /* write accesses f_pos */
};
// Use noop_llseek if neither read nor write accesses f_pos
///////////////////////////////////////////////////////////
@ fops4 depends on !fops1 && !fops2 && !fops3 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
identifier write_no_fpos.write_f;
@@
// write fops use offset
struct file_operations fops = {
...
.write = write_f,
.read = read_f,
...
+.llseek = noop_llseek, /* read and write both use no f_pos */
};
@ depends on has_write && !has_read && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier write_no_fpos.write_f;
@@
struct file_operations fops = {
... .write = write_f, ...
+.llseek = noop_llseek, /* write uses no f_pos */
};
@ depends on has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
identifier read_no_fpos.read_f;
@@
struct file_operations fops = {
... .read = read_f, ...
+.llseek = noop_llseek, /* read uses no f_pos */
};
@ depends on !has_read && !has_write && !fops1 && !fops2 && !has_llseek && !nonseekable1 && !nonseekable2 && !seq @
identifier fops0.fops;
@@
struct file_operations fops = {
...
+.llseek = noop_llseek, /* no read or write fn */
};
===== End semantic patch =====
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Cc: Julia Lawall <julia@diku.dk>
Cc: Christoph Hellwig <hch@infradead.org>
2010-08-15 20:52:59 +04:00
|
|
|
.llseek = no_llseek,
|
2007-05-08 11:32:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2008-06-06 09:45:50 +04:00
|
|
|
static struct class *spidev_class;
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2012-12-07 20:57:14 +04:00
|
|
|
static int spidev_probe(struct spi_device *spi)
|
2007-05-08 11:32:15 +04:00
|
|
|
{
|
|
|
|
struct spidev_data *spidev;
|
|
|
|
int status;
|
|
|
|
unsigned long minor;
|
|
|
|
|
|
|
|
/* Allocate driver data */
|
|
|
|
spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
|
|
|
|
if (!spidev)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
/* Initialize the driver data */
|
|
|
|
spidev->spi = spi;
|
2008-05-24 00:05:03 +04:00
|
|
|
spin_lock_init(&spidev->spi_lock);
|
2007-05-08 11:32:15 +04:00
|
|
|
mutex_init(&spidev->buf_lock);
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&spidev->device_entry);
|
|
|
|
|
|
|
|
/* 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);
|
2007-05-16 10:57:05 +04:00
|
|
|
minor = find_first_zero_bit(minors, N_SPI_MINORS);
|
2007-05-08 11:32:15 +04:00
|
|
|
if (minor < N_SPI_MINORS) {
|
2008-06-06 09:45:50 +04:00
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
|
2008-07-22 07:03:34 +04:00
|
|
|
dev = device_create(spidev_class, &spi->dev, spidev->devt,
|
|
|
|
spidev, "spidev%d.%d",
|
|
|
|
spi->master->bus_num, spi->chip_select);
|
2013-07-15 05:50:32 +04:00
|
|
|
status = PTR_ERR_OR_ZERO(dev);
|
2007-05-08 11:32:15 +04:00
|
|
|
} 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);
|
|
|
|
|
2014-11-08 13:28:10 +03:00
|
|
|
spidev->speed_hz = spi->max_speed_hz;
|
|
|
|
|
2008-12-02 00:13:52 +03:00
|
|
|
if (status == 0)
|
|
|
|
spi_set_drvdata(spi, spidev);
|
|
|
|
else
|
2007-05-08 11:32:15 +04:00
|
|
|
kfree(spidev);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2012-12-07 20:57:14 +04:00
|
|
|
static int spidev_remove(struct spi_device *spi)
|
2007-05-08 11:32:15 +04:00
|
|
|
{
|
2008-06-06 09:45:50 +04:00
|
|
|
struct spidev_data *spidev = spi_get_drvdata(spi);
|
2007-05-08 11:32:15 +04:00
|
|
|
|
2008-05-24 00:05:03 +04:00
|
|
|
/* make sure ops on existing fds can abort cleanly */
|
|
|
|
spin_lock_irq(&spidev->spi_lock);
|
|
|
|
spidev->spi = NULL;
|
|
|
|
spin_unlock_irq(&spidev->spi_lock);
|
2007-05-08 11:32:15 +04:00
|
|
|
|
2008-05-24 00:05:03 +04:00
|
|
|
/* prevent new opens */
|
|
|
|
mutex_lock(&device_list_lock);
|
2007-05-08 11:32:15 +04:00
|
|
|
list_del(&spidev->device_entry);
|
2008-06-06 09:45:50 +04:00
|
|
|
device_destroy(spidev_class, spidev->devt);
|
|
|
|
clear_bit(MINOR(spidev->devt), minors);
|
|
|
|
if (spidev->users == 0)
|
|
|
|
kfree(spidev);
|
2007-05-08 11:32:15 +04:00
|
|
|
mutex_unlock(&device_list_lock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-31 14:30:08 +04:00
|
|
|
static const struct of_device_id spidev_dt_ids[] = {
|
2012-10-31 14:30:09 +04:00
|
|
|
{ .compatible = "rohm,dh2228fv" },
|
2012-10-31 14:30:08 +04:00
|
|
|
{},
|
|
|
|
};
|
|
|
|
|
|
|
|
MODULE_DEVICE_TABLE(of, spidev_dt_ids);
|
|
|
|
|
2009-12-15 01:20:22 +03:00
|
|
|
static struct spi_driver spidev_spi_driver = {
|
2007-05-08 11:32:15 +04:00
|
|
|
.driver = {
|
|
|
|
.name = "spidev",
|
|
|
|
.owner = THIS_MODULE,
|
2012-10-31 14:30:08 +04:00
|
|
|
.of_match_table = of_match_ptr(spidev_dt_ids),
|
2007-05-08 11:32:15 +04:00
|
|
|
},
|
|
|
|
.probe = spidev_probe,
|
2012-12-07 20:57:14 +04:00
|
|
|
.remove = spidev_remove,
|
2007-05-08 11:32:15 +04:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2008-06-06 09:45:50 +04:00
|
|
|
spidev_class = class_create(THIS_MODULE, "spidev");
|
|
|
|
if (IS_ERR(spidev_class)) {
|
2009-12-15 01:20:22 +03:00
|
|
|
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
|
2008-06-06 09:45:50 +04:00
|
|
|
return PTR_ERR(spidev_class);
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
|
|
|
|
2009-12-15 01:20:22 +03:00
|
|
|
status = spi_register_driver(&spidev_spi_driver);
|
2007-05-08 11:32:15 +04:00
|
|
|
if (status < 0) {
|
2008-06-06 09:45:50 +04:00
|
|
|
class_destroy(spidev_class);
|
2009-12-15 01:20:22 +03:00
|
|
|
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
module_init(spidev_init);
|
|
|
|
|
|
|
|
static void __exit spidev_exit(void)
|
|
|
|
{
|
2009-12-15 01:20:22 +03:00
|
|
|
spi_unregister_driver(&spidev_spi_driver);
|
2008-06-06 09:45:50 +04:00
|
|
|
class_destroy(spidev_class);
|
2009-12-15 01:20:22 +03:00
|
|
|
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
|
2007-05-08 11:32:15 +04:00
|
|
|
}
|
|
|
|
module_exit(spidev_exit);
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
|
|
|
|
MODULE_DESCRIPTION("User mode SPI device interface");
|
|
|
|
MODULE_LICENSE("GPL");
|
2009-09-23 03:46:08 +04:00
|
|
|
MODULE_ALIAS("spi:spidev");
|