1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Simple synchronous userspace interface to SPI devices
5 * Copyright (C) 2006 SWAPP
7 * Copyright (C) 2007 David Brownell (simplification, cleanup)
10 #include <linux/init.h>
11 #include <linux/ioctl.h>
13 #include <linux/device.h>
14 #include <linux/err.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/property.h>
21 #include <linux/slab.h>
22 #include <linux/compat.h>
24 #include <linux/spi/spi.h>
25 #include <linux/spi/spidev.h>
27 #include <linux/uaccess.h>
31 * This supports access to SPI devices using normal userspace I/O calls.
32 * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
33 * and often mask message boundaries, full SPI support requires full duplex
34 * transfers. There are several kinds of internal message boundaries to
35 * handle chipselect management and other protocol options.
37 * SPI has a character major number assigned. We allocate minor numbers
38 * dynamically using a bitmask. You must use hotplug tools, such as udev
39 * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
40 * nodes, since there is no fixed association of minor numbers with any
41 * particular SPI bus or device.
43 #define SPIDEV_MAJOR 153 /* assigned */
44 #define N_SPI_MINORS 32 /* ... up to 256 */
46 static DECLARE_BITMAP(minors, N_SPI_MINORS);
48 static_assert(N_SPI_MINORS > 0 && N_SPI_MINORS <= 256);
50 /* Bit masks for spi_device.mode management. Note that incorrect
51 * settings for some settings can cause *lots* of trouble for other
52 * devices on a shared bus:
54 * - CS_HIGH ... this device will be active when it shouldn't be
55 * - 3WIRE ... when active, it won't behave as it should
56 * - NO_CS ... there will be no explicit message boundaries; this
57 * is completely incompatible with the shared bus model
58 * - READY ... transfers may proceed when they shouldn't.
60 * REVISIT should changing those flags be privileged?
62 #define SPI_MODE_MASK (SPI_MODE_X_MASK | SPI_CS_HIGH \
63 | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
64 | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
65 | SPI_TX_QUAD | SPI_TX_OCTAL | SPI_RX_DUAL \
66 | SPI_RX_QUAD | SPI_RX_OCTAL \
72 struct spi_device *spi;
73 struct list_head device_entry;
75 /* TX/RX buffers are NULL unless this device is open (users > 0) */
76 struct mutex buf_lock;
83 static LIST_HEAD(device_list);
84 static DEFINE_MUTEX(device_list_lock);
86 static unsigned bufsiz = 4096;
87 module_param(bufsiz, uint, S_IRUGO);
88 MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
90 /*-------------------------------------------------------------------------*/
93 spidev_sync(struct spidev_data *spidev, struct spi_message *message)
96 struct spi_device *spi;
98 spin_lock_irq(&spidev->spi_lock);
100 spin_unlock_irq(&spidev->spi_lock);
105 status = spi_sync(spi, message);
108 status = message->actual_length;
113 static inline ssize_t
114 spidev_sync_write(struct spidev_data *spidev, size_t len)
116 struct spi_transfer t = {
117 .tx_buf = spidev->tx_buffer,
119 .speed_hz = spidev->speed_hz,
121 struct spi_message m;
123 spi_message_init(&m);
124 spi_message_add_tail(&t, &m);
125 return spidev_sync(spidev, &m);
128 static inline ssize_t
129 spidev_sync_read(struct spidev_data *spidev, size_t len)
131 struct spi_transfer t = {
132 .rx_buf = spidev->rx_buffer,
134 .speed_hz = spidev->speed_hz,
136 struct spi_message m;
138 spi_message_init(&m);
139 spi_message_add_tail(&t, &m);
140 return spidev_sync(spidev, &m);
143 /*-------------------------------------------------------------------------*/
145 /* Read-only message with current device setup */
147 spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
149 struct spidev_data *spidev;
152 /* chipselect only toggles at start or end of operation */
156 spidev = filp->private_data;
158 mutex_lock(&spidev->buf_lock);
159 status = spidev_sync_read(spidev, count);
161 unsigned long missing;
163 missing = copy_to_user(buf, spidev->rx_buffer, status);
164 if (missing == status)
167 status = status - missing;
169 mutex_unlock(&spidev->buf_lock);
174 /* Write-only message with current device setup */
176 spidev_write(struct file *filp, const char __user *buf,
177 size_t count, loff_t *f_pos)
179 struct spidev_data *spidev;
181 unsigned long missing;
183 /* chipselect only toggles at start or end of operation */
187 spidev = filp->private_data;
189 mutex_lock(&spidev->buf_lock);
190 missing = copy_from_user(spidev->tx_buffer, buf, count);
192 status = spidev_sync_write(spidev, count);
195 mutex_unlock(&spidev->buf_lock);
200 static int spidev_message(struct spidev_data *spidev,
201 struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
203 struct spi_message msg;
204 struct spi_transfer *k_xfers;
205 struct spi_transfer *k_tmp;
206 struct spi_ioc_transfer *u_tmp;
207 unsigned n, total, tx_total, rx_total;
209 int status = -EFAULT;
211 spi_message_init(&msg);
212 k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
216 /* Construct spi_message, copying any tx data to bounce buffer.
217 * We walk the array of user-provided transfers, using each one
218 * to initialize a kernel version of the same transfer.
220 tx_buf = spidev->tx_buffer;
221 rx_buf = spidev->rx_buffer;
225 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
227 n--, k_tmp++, u_tmp++) {
228 /* Ensure that also following allocations from rx_buf/tx_buf will meet
229 * DMA alignment requirements.
231 unsigned int len_aligned = ALIGN(u_tmp->len, ARCH_KMALLOC_MINALIGN);
233 k_tmp->len = u_tmp->len;
236 /* Since the function returns the total length of transfers
237 * on success, restrict the total to positive int values to
238 * avoid the return value looking like an error. Also check
239 * each transfer length to avoid arithmetic overflow.
241 if (total > INT_MAX || k_tmp->len > INT_MAX) {
247 /* this transfer needs space in RX bounce buffer */
248 rx_total += len_aligned;
249 if (rx_total > bufsiz) {
253 k_tmp->rx_buf = rx_buf;
254 rx_buf += len_aligned;
257 /* this transfer needs space in TX bounce buffer */
258 tx_total += len_aligned;
259 if (tx_total > bufsiz) {
263 k_tmp->tx_buf = tx_buf;
264 if (copy_from_user(tx_buf, (const u8 __user *)
265 (uintptr_t) u_tmp->tx_buf,
268 tx_buf += len_aligned;
271 k_tmp->cs_change = !!u_tmp->cs_change;
272 k_tmp->tx_nbits = u_tmp->tx_nbits;
273 k_tmp->rx_nbits = u_tmp->rx_nbits;
274 k_tmp->bits_per_word = u_tmp->bits_per_word;
275 k_tmp->delay.value = u_tmp->delay_usecs;
276 k_tmp->delay.unit = SPI_DELAY_UNIT_USECS;
277 k_tmp->speed_hz = u_tmp->speed_hz;
278 k_tmp->word_delay.value = u_tmp->word_delay_usecs;
279 k_tmp->word_delay.unit = SPI_DELAY_UNIT_USECS;
280 if (!k_tmp->speed_hz)
281 k_tmp->speed_hz = spidev->speed_hz;
283 dev_dbg(&spidev->spi->dev,
284 " xfer len %u %s%s%s%dbits %u usec %u usec %uHz\n",
286 k_tmp->rx_buf ? "rx " : "",
287 k_tmp->tx_buf ? "tx " : "",
288 k_tmp->cs_change ? "cs " : "",
289 k_tmp->bits_per_word ? : spidev->spi->bits_per_word,
291 k_tmp->word_delay.value,
292 k_tmp->speed_hz ? : spidev->spi->max_speed_hz);
294 spi_message_add_tail(k_tmp, &msg);
297 status = spidev_sync(spidev, &msg);
301 /* copy any rx data out of bounce buffer */
302 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
304 n--, k_tmp++, u_tmp++) {
306 if (copy_to_user((u8 __user *)
307 (uintptr_t) u_tmp->rx_buf, k_tmp->rx_buf,
321 static struct spi_ioc_transfer *
322 spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
327 /* Check type, command number and direction */
328 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC
329 || _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
330 || _IOC_DIR(cmd) != _IOC_WRITE)
331 return ERR_PTR(-ENOTTY);
333 tmp = _IOC_SIZE(cmd);
334 if ((tmp % sizeof(struct spi_ioc_transfer)) != 0)
335 return ERR_PTR(-EINVAL);
336 *n_ioc = tmp / sizeof(struct spi_ioc_transfer);
340 /* copy into scratch area */
341 return memdup_user(u_ioc, tmp);
345 spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
348 struct spidev_data *spidev;
349 struct spi_device *spi;
352 struct spi_ioc_transfer *ioc;
354 /* Check type and command number */
355 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
358 /* guard against device removal before, or while,
359 * we issue this ioctl.
361 spidev = filp->private_data;
362 spin_lock_irq(&spidev->spi_lock);
363 spi = spi_dev_get(spidev->spi);
364 spin_unlock_irq(&spidev->spi_lock);
369 /* use the buffer lock here for triple duty:
370 * - prevent I/O (from us) so calling spi_setup() is safe;
371 * - prevent concurrent SPI_IOC_WR_* from morphing
372 * data fields while SPI_IOC_RD_* reads them;
373 * - SPI_IOC_MESSAGE needs the buffer locked "normally".
375 mutex_lock(&spidev->buf_lock);
379 case SPI_IOC_RD_MODE:
380 case SPI_IOC_RD_MODE32:
384 struct spi_controller *ctlr = spi->controller;
386 if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
387 ctlr->cs_gpiods[spi->chip_select])
391 if (cmd == SPI_IOC_RD_MODE)
392 retval = put_user(tmp & SPI_MODE_MASK,
395 retval = put_user(tmp & SPI_MODE_MASK,
396 (__u32 __user *)arg);
398 case SPI_IOC_RD_LSB_FIRST:
399 retval = put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
402 case SPI_IOC_RD_BITS_PER_WORD:
403 retval = put_user(spi->bits_per_word, (__u8 __user *)arg);
405 case SPI_IOC_RD_MAX_SPEED_HZ:
406 retval = put_user(spidev->speed_hz, (__u32 __user *)arg);
410 case SPI_IOC_WR_MODE:
411 case SPI_IOC_WR_MODE32:
412 if (cmd == SPI_IOC_WR_MODE)
413 retval = get_user(tmp, (u8 __user *)arg);
415 retval = get_user(tmp, (u32 __user *)arg);
417 struct spi_controller *ctlr = spi->controller;
418 u32 save = spi->mode;
420 if (tmp & ~SPI_MODE_MASK) {
425 if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
426 ctlr->cs_gpiods[spi->chip_select])
429 tmp |= spi->mode & ~SPI_MODE_MASK;
430 spi->mode = tmp & SPI_MODE_USER_MASK;
431 retval = spi_setup(spi);
435 dev_dbg(&spi->dev, "spi mode %x\n", tmp);
438 case SPI_IOC_WR_LSB_FIRST:
439 retval = get_user(tmp, (__u8 __user *)arg);
441 u32 save = spi->mode;
444 spi->mode |= SPI_LSB_FIRST;
446 spi->mode &= ~SPI_LSB_FIRST;
447 retval = spi_setup(spi);
451 dev_dbg(&spi->dev, "%csb first\n",
455 case SPI_IOC_WR_BITS_PER_WORD:
456 retval = get_user(tmp, (__u8 __user *)arg);
458 u8 save = spi->bits_per_word;
460 spi->bits_per_word = tmp;
461 retval = spi_setup(spi);
463 spi->bits_per_word = save;
465 dev_dbg(&spi->dev, "%d bits per word\n", tmp);
468 case SPI_IOC_WR_MAX_SPEED_HZ: {
471 retval = get_user(tmp, (__u32 __user *)arg);
479 save = spi->max_speed_hz;
481 spi->max_speed_hz = tmp;
482 retval = spi_setup(spi);
484 spidev->speed_hz = tmp;
485 dev_dbg(&spi->dev, "%d Hz (max)\n", spidev->speed_hz);
488 spi->max_speed_hz = save;
492 /* segmented and/or full-duplex I/O request */
493 /* Check message and copy into scratch area */
494 ioc = spidev_get_ioc_message(cmd,
495 (struct spi_ioc_transfer __user *)arg, &n_ioc);
497 retval = PTR_ERR(ioc);
501 break; /* n_ioc is also 0 */
503 /* translate to spi_message, execute */
504 retval = spidev_message(spidev, ioc, n_ioc);
509 mutex_unlock(&spidev->buf_lock);
516 spidev_compat_ioc_message(struct file *filp, unsigned int cmd,
519 struct spi_ioc_transfer __user *u_ioc;
521 struct spidev_data *spidev;
522 struct spi_device *spi;
524 struct spi_ioc_transfer *ioc;
526 u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg);
528 /* guard against device removal before, or while,
529 * we issue this ioctl.
531 spidev = filp->private_data;
532 spin_lock_irq(&spidev->spi_lock);
533 spi = spi_dev_get(spidev->spi);
534 spin_unlock_irq(&spidev->spi_lock);
539 /* SPI_IOC_MESSAGE needs the buffer locked "normally" */
540 mutex_lock(&spidev->buf_lock);
542 /* Check message and copy into scratch area */
543 ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc);
545 retval = PTR_ERR(ioc);
549 goto done; /* n_ioc is also 0 */
551 /* Convert buffer pointers */
552 for (n = 0; n < n_ioc; n++) {
553 ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf);
554 ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf);
557 /* translate to spi_message, execute */
558 retval = spidev_message(spidev, ioc, n_ioc);
562 mutex_unlock(&spidev->buf_lock);
568 spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
570 if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC
571 && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0))
572 && _IOC_DIR(cmd) == _IOC_WRITE)
573 return spidev_compat_ioc_message(filp, cmd, arg);
575 return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
578 #define spidev_compat_ioctl NULL
579 #endif /* CONFIG_COMPAT */
581 static int spidev_open(struct inode *inode, struct file *filp)
583 struct spidev_data *spidev = NULL, *iter;
586 mutex_lock(&device_list_lock);
588 list_for_each_entry(iter, &device_list, device_entry) {
589 if (iter->devt == inode->i_rdev) {
597 pr_debug("spidev: nothing for minor %d\n", iminor(inode));
601 if (!spidev->tx_buffer) {
602 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
603 if (!spidev->tx_buffer) {
604 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
610 if (!spidev->rx_buffer) {
611 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
612 if (!spidev->rx_buffer) {
613 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
615 goto err_alloc_rx_buf;
620 filp->private_data = spidev;
621 stream_open(inode, filp);
623 mutex_unlock(&device_list_lock);
627 kfree(spidev->tx_buffer);
628 spidev->tx_buffer = NULL;
630 mutex_unlock(&device_list_lock);
634 static int spidev_release(struct inode *inode, struct file *filp)
636 struct spidev_data *spidev;
639 mutex_lock(&device_list_lock);
640 spidev = filp->private_data;
641 filp->private_data = NULL;
643 spin_lock_irq(&spidev->spi_lock);
644 /* ... after we unbound from the underlying device? */
645 dofree = (spidev->spi == NULL);
646 spin_unlock_irq(&spidev->spi_lock);
650 if (!spidev->users) {
652 kfree(spidev->tx_buffer);
653 spidev->tx_buffer = NULL;
655 kfree(spidev->rx_buffer);
656 spidev->rx_buffer = NULL;
661 spidev->speed_hz = spidev->spi->max_speed_hz;
663 #ifdef CONFIG_SPI_SLAVE
665 spi_slave_abort(spidev->spi);
667 mutex_unlock(&device_list_lock);
672 static const struct file_operations spidev_fops = {
673 .owner = THIS_MODULE,
674 /* REVISIT switch to aio primitives, so that userspace
675 * gets more complete API coverage. It'll simplify things
676 * too, except for the locking.
678 .write = spidev_write,
680 .unlocked_ioctl = spidev_ioctl,
681 .compat_ioctl = spidev_compat_ioctl,
683 .release = spidev_release,
687 /*-------------------------------------------------------------------------*/
689 /* The main reason to have this class is to make mdev/udev create the
690 * /dev/spidevB.C character device nodes exposing our userspace API.
691 * It also simplifies memory management.
694 static struct class *spidev_class;
696 static const struct spi_device_id spidev_spi_ids[] = {
697 { .name = "dh2228fv" },
698 { .name = "ltc2488" },
699 { .name = "sx1301" },
701 { .name = "dhcom-board" },
702 { .name = "m53cpld" },
703 { .name = "spi-petra" },
704 { .name = "spi-authenta" },
707 MODULE_DEVICE_TABLE(spi, spidev_spi_ids);
710 * spidev should never be referenced in DT without a specific compatible string,
711 * it is a Linux implementation thing rather than a description of the hardware.
713 static int spidev_of_check(struct device *dev)
715 if (device_property_match_string(dev, "compatible", "spidev") < 0)
718 dev_err(dev, "spidev listed directly in DT is not supported\n");
722 static const struct of_device_id spidev_dt_ids[] = {
723 { .compatible = "rohm,dh2228fv", .data = &spidev_of_check },
724 { .compatible = "lineartechnology,ltc2488", .data = &spidev_of_check },
725 { .compatible = "semtech,sx1301", .data = &spidev_of_check },
726 { .compatible = "lwn,bk4", .data = &spidev_of_check },
727 { .compatible = "dh,dhcom-board", .data = &spidev_of_check },
728 { .compatible = "menlo,m53cpld", .data = &spidev_of_check },
729 { .compatible = "cisco,spi-petra", .data = &spidev_of_check },
730 { .compatible = "micron,spi-authenta", .data = &spidev_of_check },
733 MODULE_DEVICE_TABLE(of, spidev_dt_ids);
735 /* Dummy SPI devices not to be used in production systems */
736 static int spidev_acpi_check(struct device *dev)
738 dev_warn(dev, "do not use this driver in production systems!\n");
742 static const struct acpi_device_id spidev_acpi_ids[] = {
744 * The ACPI SPT000* devices are only meant for development and
745 * testing. Systems used in production should have a proper ACPI
746 * description of the connected peripheral and they should also use
747 * a proper driver instead of poking directly to the SPI bus.
749 { "SPT0001", (kernel_ulong_t)&spidev_acpi_check },
750 { "SPT0002", (kernel_ulong_t)&spidev_acpi_check },
751 { "SPT0003", (kernel_ulong_t)&spidev_acpi_check },
754 MODULE_DEVICE_TABLE(acpi, spidev_acpi_ids);
756 /*-------------------------------------------------------------------------*/
758 static int spidev_probe(struct spi_device *spi)
760 int (*match)(struct device *dev);
761 struct spidev_data *spidev;
765 match = device_get_match_data(&spi->dev);
767 status = match(&spi->dev);
772 /* Allocate driver data */
773 spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
777 /* Initialize the driver data */
779 spin_lock_init(&spidev->spi_lock);
780 mutex_init(&spidev->buf_lock);
782 INIT_LIST_HEAD(&spidev->device_entry);
784 /* If we can allocate a minor number, hook up this device.
785 * Reusing minors is fine so long as udev or mdev is working.
787 mutex_lock(&device_list_lock);
788 minor = find_first_zero_bit(minors, N_SPI_MINORS);
789 if (minor < N_SPI_MINORS) {
792 spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
793 dev = device_create(spidev_class, &spi->dev, spidev->devt,
794 spidev, "spidev%d.%d",
795 spi->master->bus_num, spi->chip_select);
796 status = PTR_ERR_OR_ZERO(dev);
798 dev_dbg(&spi->dev, "no minor number available!\n");
802 set_bit(minor, minors);
803 list_add(&spidev->device_entry, &device_list);
805 mutex_unlock(&device_list_lock);
807 spidev->speed_hz = spi->max_speed_hz;
810 spi_set_drvdata(spi, spidev);
817 static void spidev_remove(struct spi_device *spi)
819 struct spidev_data *spidev = spi_get_drvdata(spi);
821 /* prevent new opens */
822 mutex_lock(&device_list_lock);
823 /* make sure ops on existing fds can abort cleanly */
824 spin_lock_irq(&spidev->spi_lock);
826 spin_unlock_irq(&spidev->spi_lock);
828 list_del(&spidev->device_entry);
829 device_destroy(spidev_class, spidev->devt);
830 clear_bit(MINOR(spidev->devt), minors);
831 if (spidev->users == 0)
833 mutex_unlock(&device_list_lock);
836 static struct spi_driver spidev_spi_driver = {
839 .of_match_table = spidev_dt_ids,
840 .acpi_match_table = spidev_acpi_ids,
842 .probe = spidev_probe,
843 .remove = spidev_remove,
844 .id_table = spidev_spi_ids,
846 /* NOTE: suspend/resume methods are not necessary here.
847 * We don't do anything except pass the requests to/from
848 * the underlying controller. The refrigerator handles
849 * most issues; the controller driver handles the rest.
853 /*-------------------------------------------------------------------------*/
855 static int __init spidev_init(void)
859 /* Claim our 256 reserved device numbers. Then register a class
860 * that will key udev/mdev to add/remove /dev nodes. Last, register
861 * the driver which manages those device numbers.
863 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
867 spidev_class = class_create(THIS_MODULE, "spidev");
868 if (IS_ERR(spidev_class)) {
869 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
870 return PTR_ERR(spidev_class);
873 status = spi_register_driver(&spidev_spi_driver);
875 class_destroy(spidev_class);
876 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
880 module_init(spidev_init);
882 static void __exit spidev_exit(void)
884 spi_unregister_driver(&spidev_spi_driver);
885 class_destroy(spidev_class);
886 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
888 module_exit(spidev_exit);
891 MODULE_DESCRIPTION("User mode SPI device interface");
892 MODULE_LICENSE("GPL");
893 MODULE_ALIAS("spi:spidev");