24 #include <linux/module.h>
27 #include <linux/device.h>
29 #include <linux/list.h>
30 #include <linux/errno.h>
32 #include <linux/slab.h>
38 #include <asm/uaccess.h>
54 #define SPIDEV_MAJOR 153
55 #define N_SPI_MINORS 32
72 #define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
73 | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
74 | SPI_NO_CS | SPI_READY)
91 static unsigned bufsiz = 4096;
101 static void spidev_complete(
void *
arg)
112 message->
complete = spidev_complete;
132 spidev_sync_write(
struct spidev_data *spidev,
size_t len)
140 spi_message_init(&
m);
141 spi_message_add_tail(&t, &
m);
142 return spidev_sync(spidev, &
m);
146 spidev_sync_read(
struct spidev_data *spidev,
size_t len)
154 spi_message_init(&
m);
155 spi_message_add_tail(&t, &
m);
156 return spidev_sync(spidev, &
m);
163 spidev_read(
struct file *filp,
char __user *
buf,
size_t count, loff_t *f_pos)
175 status = spidev_sync_read(spidev, count);
177 unsigned long missing;
180 if (missing == status)
183 status = status - missing;
192 spidev_write(
struct file *filp,
const char __user *buf,
193 size_t count, loff_t *f_pos)
197 unsigned long missing;
208 status = spidev_sync_write(spidev, count);
216 static int spidev_message(
struct spidev_data *spidev,
227 spi_message_init(&
msg);
228 k_xfers = kcalloc(n_xfers,
sizeof(*k_tmp),
GFP_KERNEL);
238 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
240 n--, k_tmp++, u_tmp++) {
241 k_tmp->len = u_tmp->
len;
244 if (total > bufsiz) {
271 " xfer len %zd %s%s%s%dbits %u usec %uHz\n",
273 u_tmp->
rx_buf ?
"rx " :
"",
274 u_tmp->
tx_buf ?
"tx " :
"",
280 spi_message_add_tail(k_tmp, &
msg);
283 status = spidev_sync(spidev, &
msg);
289 for (n = n_xfers, u_tmp = u_xfers;
n; n--, u_tmp++) {
308 spidev_ioctl(
struct file *filp,
unsigned int cmd,
unsigned long arg)
340 spi = spi_dev_get(spidev->
spi);
418 dev_dbg(&spi->
dev,
"%d bits per word\n", tmp);
465 retval = spidev_message(spidev, ioc, n_ioc);
479 return spidev_ioctl(filp, cmd, (
unsigned long)compat_ptr(arg));
482 #define spidev_compat_ioctl NULL
512 pr_debug(
"spidev: nothing for minor %d\n", iminor(inode));
529 if (!spidev->
users) {
537 dofree = (spidev->
spi ==
NULL);
554 .write = spidev_write,
556 .unlocked_ioctl = spidev_ioctl,
559 .release = spidev_release,
570 static struct class *spidev_class;
581 spidev = kzalloc(
sizeof(*spidev),
GFP_KERNEL);
602 spidev,
"spidev%d.%d",
604 status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
606 dev_dbg(&spi->
dev,
"no minor number available!\n");
616 spi_set_drvdata(spi, spidev);
630 spi_set_drvdata(spi,
NULL);
638 if (spidev->
users == 0)
645 static struct spi_driver spidev_spi_driver = {
650 .probe = spidev_probe,
662 static int __init spidev_init(
void)
671 status = register_chrdev(
SPIDEV_MAJOR,
"spi", &spidev_fops);
676 if (IS_ERR(spidev_class)) {
678 return PTR_ERR(spidev_class);
690 static void __exit spidev_exit(
void)
692 spi_unregister_driver(&spidev_spi_driver);