Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Simple synchronous userspace interface to SPI devices
0004  *
0005  * Copyright (C) 2006 SWAPP
0006  *  Andrea Paterniani <a.paterniani@swapp-eng.it>
0007  * Copyright (C) 2007 David Brownell (simplification, cleanup)
0008  */
0009 
0010 #include <linux/init.h>
0011 #include <linux/ioctl.h>
0012 #include <linux/fs.h>
0013 #include <linux/device.h>
0014 #include <linux/err.h>
0015 #include <linux/list.h>
0016 #include <linux/errno.h>
0017 #include <linux/mod_devicetable.h>
0018 #include <linux/module.h>
0019 #include <linux/mutex.h>
0020 #include <linux/property.h>
0021 #include <linux/slab.h>
0022 #include <linux/compat.h>
0023 
0024 #include <linux/spi/spi.h>
0025 #include <linux/spi/spidev.h>
0026 
0027 #include <linux/uaccess.h>
0028 
0029 
0030 /*
0031  * This supports access to SPI devices using normal userspace I/O calls.
0032  * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
0033  * and often mask message boundaries, full SPI support requires full duplex
0034  * transfers.  There are several kinds of internal message boundaries to
0035  * handle chipselect management and other protocol options.
0036  *
0037  * SPI has a character major number assigned.  We allocate minor numbers
0038  * dynamically using a bitmask.  You must use hotplug tools, such as udev
0039  * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
0040  * nodes, since there is no fixed association of minor numbers with any
0041  * particular SPI bus or device.
0042  */
0043 #define SPIDEV_MAJOR            153 /* assigned */
0044 #define N_SPI_MINORS            32  /* ... up to 256 */
0045 
0046 static DECLARE_BITMAP(minors, N_SPI_MINORS);
0047 
0048 static_assert(N_SPI_MINORS > 0 && N_SPI_MINORS <= 256);
0049 
0050 /* Bit masks for spi_device.mode management.  Note that incorrect
0051  * settings for some settings can cause *lots* of trouble for other
0052  * devices on a shared bus:
0053  *
0054  *  - CS_HIGH ... this device will be active when it shouldn't be
0055  *  - 3WIRE ... when active, it won't behave as it should
0056  *  - NO_CS ... there will be no explicit message boundaries; this
0057  *  is completely incompatible with the shared bus model
0058  *  - READY ... transfers may proceed when they shouldn't.
0059  *
0060  * REVISIT should changing those flags be privileged?
0061  */
0062 #define SPI_MODE_MASK       (SPI_MODE_X_MASK | SPI_CS_HIGH \
0063                 | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
0064                 | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
0065                 | SPI_TX_QUAD | SPI_TX_OCTAL | SPI_RX_DUAL \
0066                 | SPI_RX_QUAD | SPI_RX_OCTAL \
0067                 | SPI_RX_CPHA_FLIP)
0068 
0069 struct spidev_data {
0070     dev_t           devt;
0071     spinlock_t      spi_lock;
0072     struct spi_device   *spi;
0073     struct list_head    device_entry;
0074 
0075     /* TX/RX buffers are NULL unless this device is open (users > 0) */
0076     struct mutex        buf_lock;
0077     unsigned        users;
0078     u8          *tx_buffer;
0079     u8          *rx_buffer;
0080     u32         speed_hz;
0081 };
0082 
0083 static LIST_HEAD(device_list);
0084 static DEFINE_MUTEX(device_list_lock);
0085 
0086 static unsigned bufsiz = 4096;
0087 module_param(bufsiz, uint, S_IRUGO);
0088 MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
0089 
0090 /*-------------------------------------------------------------------------*/
0091 
0092 static ssize_t
0093 spidev_sync(struct spidev_data *spidev, struct spi_message *message)
0094 {
0095     int status;
0096     struct spi_device *spi;
0097 
0098     spin_lock_irq(&spidev->spi_lock);
0099     spi = spidev->spi;
0100     spin_unlock_irq(&spidev->spi_lock);
0101 
0102     if (spi == NULL)
0103         status = -ESHUTDOWN;
0104     else
0105         status = spi_sync(spi, message);
0106 
0107     if (status == 0)
0108         status = message->actual_length;
0109 
0110     return status;
0111 }
0112 
0113 static inline ssize_t
0114 spidev_sync_write(struct spidev_data *spidev, size_t len)
0115 {
0116     struct spi_transfer t = {
0117             .tx_buf     = spidev->tx_buffer,
0118             .len        = len,
0119             .speed_hz   = spidev->speed_hz,
0120         };
0121     struct spi_message  m;
0122 
0123     spi_message_init(&m);
0124     spi_message_add_tail(&t, &m);
0125     return spidev_sync(spidev, &m);
0126 }
0127 
0128 static inline ssize_t
0129 spidev_sync_read(struct spidev_data *spidev, size_t len)
0130 {
0131     struct spi_transfer t = {
0132             .rx_buf     = spidev->rx_buffer,
0133             .len        = len,
0134             .speed_hz   = spidev->speed_hz,
0135         };
0136     struct spi_message  m;
0137 
0138     spi_message_init(&m);
0139     spi_message_add_tail(&t, &m);
0140     return spidev_sync(spidev, &m);
0141 }
0142 
0143 /*-------------------------------------------------------------------------*/
0144 
0145 /* Read-only message with current device setup */
0146 static ssize_t
0147 spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
0148 {
0149     struct spidev_data  *spidev;
0150     ssize_t         status;
0151 
0152     /* chipselect only toggles at start or end of operation */
0153     if (count > bufsiz)
0154         return -EMSGSIZE;
0155 
0156     spidev = filp->private_data;
0157 
0158     mutex_lock(&spidev->buf_lock);
0159     status = spidev_sync_read(spidev, count);
0160     if (status > 0) {
0161         unsigned long   missing;
0162 
0163         missing = copy_to_user(buf, spidev->rx_buffer, status);
0164         if (missing == status)
0165             status = -EFAULT;
0166         else
0167             status = status - missing;
0168     }
0169     mutex_unlock(&spidev->buf_lock);
0170 
0171     return status;
0172 }
0173 
0174 /* Write-only message with current device setup */
0175 static ssize_t
0176 spidev_write(struct file *filp, const char __user *buf,
0177         size_t count, loff_t *f_pos)
0178 {
0179     struct spidev_data  *spidev;
0180     ssize_t         status;
0181     unsigned long       missing;
0182 
0183     /* chipselect only toggles at start or end of operation */
0184     if (count > bufsiz)
0185         return -EMSGSIZE;
0186 
0187     spidev = filp->private_data;
0188 
0189     mutex_lock(&spidev->buf_lock);
0190     missing = copy_from_user(spidev->tx_buffer, buf, count);
0191     if (missing == 0)
0192         status = spidev_sync_write(spidev, count);
0193     else
0194         status = -EFAULT;
0195     mutex_unlock(&spidev->buf_lock);
0196 
0197     return status;
0198 }
0199 
0200 static int spidev_message(struct spidev_data *spidev,
0201         struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
0202 {
0203     struct spi_message  msg;
0204     struct spi_transfer *k_xfers;
0205     struct spi_transfer *k_tmp;
0206     struct spi_ioc_transfer *u_tmp;
0207     unsigned        n, total, tx_total, rx_total;
0208     u8          *tx_buf, *rx_buf;
0209     int         status = -EFAULT;
0210 
0211     spi_message_init(&msg);
0212     k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
0213     if (k_xfers == NULL)
0214         return -ENOMEM;
0215 
0216     /* Construct spi_message, copying any tx data to bounce buffer.
0217      * We walk the array of user-provided transfers, using each one
0218      * to initialize a kernel version of the same transfer.
0219      */
0220     tx_buf = spidev->tx_buffer;
0221     rx_buf = spidev->rx_buffer;
0222     total = 0;
0223     tx_total = 0;
0224     rx_total = 0;
0225     for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
0226             n;
0227             n--, k_tmp++, u_tmp++) {
0228         /* Ensure that also following allocations from rx_buf/tx_buf will meet
0229          * DMA alignment requirements.
0230          */
0231         unsigned int len_aligned = ALIGN(u_tmp->len, ARCH_KMALLOC_MINALIGN);
0232 
0233         k_tmp->len = u_tmp->len;
0234 
0235         total += k_tmp->len;
0236         /* Since the function returns the total length of transfers
0237          * on success, restrict the total to positive int values to
0238          * avoid the return value looking like an error.  Also check
0239          * each transfer length to avoid arithmetic overflow.
0240          */
0241         if (total > INT_MAX || k_tmp->len > INT_MAX) {
0242             status = -EMSGSIZE;
0243             goto done;
0244         }
0245 
0246         if (u_tmp->rx_buf) {
0247             /* this transfer needs space in RX bounce buffer */
0248             rx_total += len_aligned;
0249             if (rx_total > bufsiz) {
0250                 status = -EMSGSIZE;
0251                 goto done;
0252             }
0253             k_tmp->rx_buf = rx_buf;
0254             rx_buf += len_aligned;
0255         }
0256         if (u_tmp->tx_buf) {
0257             /* this transfer needs space in TX bounce buffer */
0258             tx_total += len_aligned;
0259             if (tx_total > bufsiz) {
0260                 status = -EMSGSIZE;
0261                 goto done;
0262             }
0263             k_tmp->tx_buf = tx_buf;
0264             if (copy_from_user(tx_buf, (const u8 __user *)
0265                         (uintptr_t) u_tmp->tx_buf,
0266                     u_tmp->len))
0267                 goto done;
0268             tx_buf += len_aligned;
0269         }
0270 
0271         k_tmp->cs_change = !!u_tmp->cs_change;
0272         k_tmp->tx_nbits = u_tmp->tx_nbits;
0273         k_tmp->rx_nbits = u_tmp->rx_nbits;
0274         k_tmp->bits_per_word = u_tmp->bits_per_word;
0275         k_tmp->delay.value = u_tmp->delay_usecs;
0276         k_tmp->delay.unit = SPI_DELAY_UNIT_USECS;
0277         k_tmp->speed_hz = u_tmp->speed_hz;
0278         k_tmp->word_delay.value = u_tmp->word_delay_usecs;
0279         k_tmp->word_delay.unit = SPI_DELAY_UNIT_USECS;
0280         if (!k_tmp->speed_hz)
0281             k_tmp->speed_hz = spidev->speed_hz;
0282 #ifdef VERBOSE
0283         dev_dbg(&spidev->spi->dev,
0284             "  xfer len %u %s%s%s%dbits %u usec %u usec %uHz\n",
0285             k_tmp->len,
0286             k_tmp->rx_buf ? "rx " : "",
0287             k_tmp->tx_buf ? "tx " : "",
0288             k_tmp->cs_change ? "cs " : "",
0289             k_tmp->bits_per_word ? : spidev->spi->bits_per_word,
0290             k_tmp->delay.value,
0291             k_tmp->word_delay.value,
0292             k_tmp->speed_hz ? : spidev->spi->max_speed_hz);
0293 #endif
0294         spi_message_add_tail(k_tmp, &msg);
0295     }
0296 
0297     status = spidev_sync(spidev, &msg);
0298     if (status < 0)
0299         goto done;
0300 
0301     /* copy any rx data out of bounce buffer */
0302     for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
0303             n;
0304             n--, k_tmp++, u_tmp++) {
0305         if (u_tmp->rx_buf) {
0306             if (copy_to_user((u8 __user *)
0307                     (uintptr_t) u_tmp->rx_buf, k_tmp->rx_buf,
0308                     u_tmp->len)) {
0309                 status = -EFAULT;
0310                 goto done;
0311             }
0312         }
0313     }
0314     status = total;
0315 
0316 done:
0317     kfree(k_xfers);
0318     return status;
0319 }
0320 
0321 static struct spi_ioc_transfer *
0322 spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
0323         unsigned *n_ioc)
0324 {
0325     u32 tmp;
0326 
0327     /* Check type, command number and direction */
0328     if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC
0329             || _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
0330             || _IOC_DIR(cmd) != _IOC_WRITE)
0331         return ERR_PTR(-ENOTTY);
0332 
0333     tmp = _IOC_SIZE(cmd);
0334     if ((tmp % sizeof(struct spi_ioc_transfer)) != 0)
0335         return ERR_PTR(-EINVAL);
0336     *n_ioc = tmp / sizeof(struct spi_ioc_transfer);
0337     if (*n_ioc == 0)
0338         return NULL;
0339 
0340     /* copy into scratch area */
0341     return memdup_user(u_ioc, tmp);
0342 }
0343 
0344 static long
0345 spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0346 {
0347     int         retval = 0;
0348     struct spidev_data  *spidev;
0349     struct spi_device   *spi;
0350     u32         tmp;
0351     unsigned        n_ioc;
0352     struct spi_ioc_transfer *ioc;
0353 
0354     /* Check type and command number */
0355     if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
0356         return -ENOTTY;
0357 
0358     /* guard against device removal before, or while,
0359      * we issue this ioctl.
0360      */
0361     spidev = filp->private_data;
0362     spin_lock_irq(&spidev->spi_lock);
0363     spi = spi_dev_get(spidev->spi);
0364     spin_unlock_irq(&spidev->spi_lock);
0365 
0366     if (spi == NULL)
0367         return -ESHUTDOWN;
0368 
0369     /* use the buffer lock here for triple duty:
0370      *  - prevent I/O (from us) so calling spi_setup() is safe;
0371      *  - prevent concurrent SPI_IOC_WR_* from morphing
0372      *    data fields while SPI_IOC_RD_* reads them;
0373      *  - SPI_IOC_MESSAGE needs the buffer locked "normally".
0374      */
0375     mutex_lock(&spidev->buf_lock);
0376 
0377     switch (cmd) {
0378     /* read requests */
0379     case SPI_IOC_RD_MODE:
0380         retval = put_user(spi->mode & SPI_MODE_MASK,
0381                     (__u8 __user *)arg);
0382         break;
0383     case SPI_IOC_RD_MODE32:
0384         retval = put_user(spi->mode & SPI_MODE_MASK,
0385                     (__u32 __user *)arg);
0386         break;
0387     case SPI_IOC_RD_LSB_FIRST:
0388         retval = put_user((spi->mode & SPI_LSB_FIRST) ?  1 : 0,
0389                     (__u8 __user *)arg);
0390         break;
0391     case SPI_IOC_RD_BITS_PER_WORD:
0392         retval = put_user(spi->bits_per_word, (__u8 __user *)arg);
0393         break;
0394     case SPI_IOC_RD_MAX_SPEED_HZ:
0395         retval = put_user(spidev->speed_hz, (__u32 __user *)arg);
0396         break;
0397 
0398     /* write requests */
0399     case SPI_IOC_WR_MODE:
0400     case SPI_IOC_WR_MODE32:
0401         if (cmd == SPI_IOC_WR_MODE)
0402             retval = get_user(tmp, (u8 __user *)arg);
0403         else
0404             retval = get_user(tmp, (u32 __user *)arg);
0405         if (retval == 0) {
0406             struct spi_controller *ctlr = spi->controller;
0407             u32 save = spi->mode;
0408 
0409             if (tmp & ~SPI_MODE_MASK) {
0410                 retval = -EINVAL;
0411                 break;
0412             }
0413 
0414             if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
0415                 ctlr->cs_gpiods[spi->chip_select])
0416                 tmp |= SPI_CS_HIGH;
0417 
0418             tmp |= spi->mode & ~SPI_MODE_MASK;
0419             spi->mode = tmp & SPI_MODE_USER_MASK;
0420             retval = spi_setup(spi);
0421             if (retval < 0)
0422                 spi->mode = save;
0423             else
0424                 dev_dbg(&spi->dev, "spi mode %x\n", tmp);
0425         }
0426         break;
0427     case SPI_IOC_WR_LSB_FIRST:
0428         retval = get_user(tmp, (__u8 __user *)arg);
0429         if (retval == 0) {
0430             u32 save = spi->mode;
0431 
0432             if (tmp)
0433                 spi->mode |= SPI_LSB_FIRST;
0434             else
0435                 spi->mode &= ~SPI_LSB_FIRST;
0436             retval = spi_setup(spi);
0437             if (retval < 0)
0438                 spi->mode = save;
0439             else
0440                 dev_dbg(&spi->dev, "%csb first\n",
0441                         tmp ? 'l' : 'm');
0442         }
0443         break;
0444     case SPI_IOC_WR_BITS_PER_WORD:
0445         retval = get_user(tmp, (__u8 __user *)arg);
0446         if (retval == 0) {
0447             u8  save = spi->bits_per_word;
0448 
0449             spi->bits_per_word = tmp;
0450             retval = spi_setup(spi);
0451             if (retval < 0)
0452                 spi->bits_per_word = save;
0453             else
0454                 dev_dbg(&spi->dev, "%d bits per word\n", tmp);
0455         }
0456         break;
0457     case SPI_IOC_WR_MAX_SPEED_HZ: {
0458         u32 save;
0459 
0460         retval = get_user(tmp, (__u32 __user *)arg);
0461         if (retval)
0462             break;
0463         if (tmp == 0) {
0464             retval = -EINVAL;
0465             break;
0466         }
0467 
0468         save = spi->max_speed_hz;
0469 
0470         spi->max_speed_hz = tmp;
0471         retval = spi_setup(spi);
0472         if (retval == 0) {
0473             spidev->speed_hz = tmp;
0474             dev_dbg(&spi->dev, "%d Hz (max)\n", spidev->speed_hz);
0475         }
0476 
0477         spi->max_speed_hz = save;
0478         break;
0479     }
0480     default:
0481         /* segmented and/or full-duplex I/O request */
0482         /* Check message and copy into scratch area */
0483         ioc = spidev_get_ioc_message(cmd,
0484                 (struct spi_ioc_transfer __user *)arg, &n_ioc);
0485         if (IS_ERR(ioc)) {
0486             retval = PTR_ERR(ioc);
0487             break;
0488         }
0489         if (!ioc)
0490             break;  /* n_ioc is also 0 */
0491 
0492         /* translate to spi_message, execute */
0493         retval = spidev_message(spidev, ioc, n_ioc);
0494         kfree(ioc);
0495         break;
0496     }
0497 
0498     mutex_unlock(&spidev->buf_lock);
0499     spi_dev_put(spi);
0500     return retval;
0501 }
0502 
0503 #ifdef CONFIG_COMPAT
0504 static long
0505 spidev_compat_ioc_message(struct file *filp, unsigned int cmd,
0506         unsigned long arg)
0507 {
0508     struct spi_ioc_transfer __user  *u_ioc;
0509     int             retval = 0;
0510     struct spidev_data      *spidev;
0511     struct spi_device       *spi;
0512     unsigned            n_ioc, n;
0513     struct spi_ioc_transfer     *ioc;
0514 
0515     u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg);
0516 
0517     /* guard against device removal before, or while,
0518      * we issue this ioctl.
0519      */
0520     spidev = filp->private_data;
0521     spin_lock_irq(&spidev->spi_lock);
0522     spi = spi_dev_get(spidev->spi);
0523     spin_unlock_irq(&spidev->spi_lock);
0524 
0525     if (spi == NULL)
0526         return -ESHUTDOWN;
0527 
0528     /* SPI_IOC_MESSAGE needs the buffer locked "normally" */
0529     mutex_lock(&spidev->buf_lock);
0530 
0531     /* Check message and copy into scratch area */
0532     ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc);
0533     if (IS_ERR(ioc)) {
0534         retval = PTR_ERR(ioc);
0535         goto done;
0536     }
0537     if (!ioc)
0538         goto done;  /* n_ioc is also 0 */
0539 
0540     /* Convert buffer pointers */
0541     for (n = 0; n < n_ioc; n++) {
0542         ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf);
0543         ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf);
0544     }
0545 
0546     /* translate to spi_message, execute */
0547     retval = spidev_message(spidev, ioc, n_ioc);
0548     kfree(ioc);
0549 
0550 done:
0551     mutex_unlock(&spidev->buf_lock);
0552     spi_dev_put(spi);
0553     return retval;
0554 }
0555 
0556 static long
0557 spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
0558 {
0559     if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC
0560             && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0))
0561             && _IOC_DIR(cmd) == _IOC_WRITE)
0562         return spidev_compat_ioc_message(filp, cmd, arg);
0563 
0564     return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
0565 }
0566 #else
0567 #define spidev_compat_ioctl NULL
0568 #endif /* CONFIG_COMPAT */
0569 
0570 static int spidev_open(struct inode *inode, struct file *filp)
0571 {
0572     struct spidev_data  *spidev = NULL, *iter;
0573     int         status = -ENXIO;
0574 
0575     mutex_lock(&device_list_lock);
0576 
0577     list_for_each_entry(iter, &device_list, device_entry) {
0578         if (iter->devt == inode->i_rdev) {
0579             status = 0;
0580             spidev = iter;
0581             break;
0582         }
0583     }
0584 
0585     if (!spidev) {
0586         pr_debug("spidev: nothing for minor %d\n", iminor(inode));
0587         goto err_find_dev;
0588     }
0589 
0590     if (!spidev->tx_buffer) {
0591         spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
0592         if (!spidev->tx_buffer) {
0593             dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
0594             status = -ENOMEM;
0595             goto err_find_dev;
0596         }
0597     }
0598 
0599     if (!spidev->rx_buffer) {
0600         spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
0601         if (!spidev->rx_buffer) {
0602             dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
0603             status = -ENOMEM;
0604             goto err_alloc_rx_buf;
0605         }
0606     }
0607 
0608     spidev->users++;
0609     filp->private_data = spidev;
0610     stream_open(inode, filp);
0611 
0612     mutex_unlock(&device_list_lock);
0613     return 0;
0614 
0615 err_alloc_rx_buf:
0616     kfree(spidev->tx_buffer);
0617     spidev->tx_buffer = NULL;
0618 err_find_dev:
0619     mutex_unlock(&device_list_lock);
0620     return status;
0621 }
0622 
0623 static int spidev_release(struct inode *inode, struct file *filp)
0624 {
0625     struct spidev_data  *spidev;
0626     int         dofree;
0627 
0628     mutex_lock(&device_list_lock);
0629     spidev = filp->private_data;
0630     filp->private_data = NULL;
0631 
0632     spin_lock_irq(&spidev->spi_lock);
0633     /* ... after we unbound from the underlying device? */
0634     dofree = (spidev->spi == NULL);
0635     spin_unlock_irq(&spidev->spi_lock);
0636 
0637     /* last close? */
0638     spidev->users--;
0639     if (!spidev->users) {
0640 
0641         kfree(spidev->tx_buffer);
0642         spidev->tx_buffer = NULL;
0643 
0644         kfree(spidev->rx_buffer);
0645         spidev->rx_buffer = NULL;
0646 
0647         if (dofree)
0648             kfree(spidev);
0649         else
0650             spidev->speed_hz = spidev->spi->max_speed_hz;
0651     }
0652 #ifdef CONFIG_SPI_SLAVE
0653     if (!dofree)
0654         spi_slave_abort(spidev->spi);
0655 #endif
0656     mutex_unlock(&device_list_lock);
0657 
0658     return 0;
0659 }
0660 
0661 static const struct file_operations spidev_fops = {
0662     .owner =    THIS_MODULE,
0663     /* REVISIT switch to aio primitives, so that userspace
0664      * gets more complete API coverage.  It'll simplify things
0665      * too, except for the locking.
0666      */
0667     .write =    spidev_write,
0668     .read =     spidev_read,
0669     .unlocked_ioctl = spidev_ioctl,
0670     .compat_ioctl = spidev_compat_ioctl,
0671     .open =     spidev_open,
0672     .release =  spidev_release,
0673     .llseek =   no_llseek,
0674 };
0675 
0676 /*-------------------------------------------------------------------------*/
0677 
0678 /* The main reason to have this class is to make mdev/udev create the
0679  * /dev/spidevB.C character device nodes exposing our userspace API.
0680  * It also simplifies memory management.
0681  */
0682 
0683 static struct class *spidev_class;
0684 
0685 static const struct spi_device_id spidev_spi_ids[] = {
0686     { .name = "dh2228fv" },
0687     { .name = "ltc2488" },
0688     { .name = "sx1301" },
0689     { .name = "bk4" },
0690     { .name = "dhcom-board" },
0691     { .name = "m53cpld" },
0692     { .name = "spi-petra" },
0693     { .name = "spi-authenta" },
0694     {},
0695 };
0696 MODULE_DEVICE_TABLE(spi, spidev_spi_ids);
0697 
0698 /*
0699  * spidev should never be referenced in DT without a specific compatible string,
0700  * it is a Linux implementation thing rather than a description of the hardware.
0701  */
0702 static int spidev_of_check(struct device *dev)
0703 {
0704     if (device_property_match_string(dev, "compatible", "spidev") < 0)
0705         return 0;
0706 
0707     dev_err(dev, "spidev listed directly in DT is not supported\n");
0708     return -EINVAL;
0709 }
0710 
0711 static const struct of_device_id spidev_dt_ids[] = {
0712     { .compatible = "rohm,dh2228fv", .data = &spidev_of_check },
0713     { .compatible = "lineartechnology,ltc2488", .data = &spidev_of_check },
0714     { .compatible = "semtech,sx1301", .data = &spidev_of_check },
0715     { .compatible = "lwn,bk4", .data = &spidev_of_check },
0716     { .compatible = "dh,dhcom-board", .data = &spidev_of_check },
0717     { .compatible = "menlo,m53cpld", .data = &spidev_of_check },
0718     { .compatible = "cisco,spi-petra", .data = &spidev_of_check },
0719     { .compatible = "micron,spi-authenta", .data = &spidev_of_check },
0720     {},
0721 };
0722 MODULE_DEVICE_TABLE(of, spidev_dt_ids);
0723 
0724 /* Dummy SPI devices not to be used in production systems */
0725 static int spidev_acpi_check(struct device *dev)
0726 {
0727     dev_warn(dev, "do not use this driver in production systems!\n");
0728     return 0;
0729 }
0730 
0731 static const struct acpi_device_id spidev_acpi_ids[] = {
0732     /*
0733      * The ACPI SPT000* devices are only meant for development and
0734      * testing. Systems used in production should have a proper ACPI
0735      * description of the connected peripheral and they should also use
0736      * a proper driver instead of poking directly to the SPI bus.
0737      */
0738     { "SPT0001", (kernel_ulong_t)&spidev_acpi_check },
0739     { "SPT0002", (kernel_ulong_t)&spidev_acpi_check },
0740     { "SPT0003", (kernel_ulong_t)&spidev_acpi_check },
0741     {},
0742 };
0743 MODULE_DEVICE_TABLE(acpi, spidev_acpi_ids);
0744 
0745 /*-------------------------------------------------------------------------*/
0746 
0747 static int spidev_probe(struct spi_device *spi)
0748 {
0749     int (*match)(struct device *dev);
0750     struct spidev_data  *spidev;
0751     int         status;
0752     unsigned long       minor;
0753 
0754     match = device_get_match_data(&spi->dev);
0755     if (match) {
0756         status = match(&spi->dev);
0757         if (status)
0758             return status;
0759     }
0760 
0761     /* Allocate driver data */
0762     spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
0763     if (!spidev)
0764         return -ENOMEM;
0765 
0766     /* Initialize the driver data */
0767     spidev->spi = spi;
0768     spin_lock_init(&spidev->spi_lock);
0769     mutex_init(&spidev->buf_lock);
0770 
0771     INIT_LIST_HEAD(&spidev->device_entry);
0772 
0773     /* If we can allocate a minor number, hook up this device.
0774      * Reusing minors is fine so long as udev or mdev is working.
0775      */
0776     mutex_lock(&device_list_lock);
0777     minor = find_first_zero_bit(minors, N_SPI_MINORS);
0778     if (minor < N_SPI_MINORS) {
0779         struct device *dev;
0780 
0781         spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
0782         dev = device_create(spidev_class, &spi->dev, spidev->devt,
0783                     spidev, "spidev%d.%d",
0784                     spi->master->bus_num, spi->chip_select);
0785         status = PTR_ERR_OR_ZERO(dev);
0786     } else {
0787         dev_dbg(&spi->dev, "no minor number available!\n");
0788         status = -ENODEV;
0789     }
0790     if (status == 0) {
0791         set_bit(minor, minors);
0792         list_add(&spidev->device_entry, &device_list);
0793     }
0794     mutex_unlock(&device_list_lock);
0795 
0796     spidev->speed_hz = spi->max_speed_hz;
0797 
0798     if (status == 0)
0799         spi_set_drvdata(spi, spidev);
0800     else
0801         kfree(spidev);
0802 
0803     return status;
0804 }
0805 
0806 static void spidev_remove(struct spi_device *spi)
0807 {
0808     struct spidev_data  *spidev = spi_get_drvdata(spi);
0809 
0810     /* prevent new opens */
0811     mutex_lock(&device_list_lock);
0812     /* make sure ops on existing fds can abort cleanly */
0813     spin_lock_irq(&spidev->spi_lock);
0814     spidev->spi = NULL;
0815     spin_unlock_irq(&spidev->spi_lock);
0816 
0817     list_del(&spidev->device_entry);
0818     device_destroy(spidev_class, spidev->devt);
0819     clear_bit(MINOR(spidev->devt), minors);
0820     if (spidev->users == 0)
0821         kfree(spidev);
0822     mutex_unlock(&device_list_lock);
0823 }
0824 
0825 static struct spi_driver spidev_spi_driver = {
0826     .driver = {
0827         .name =     "spidev",
0828         .of_match_table = spidev_dt_ids,
0829         .acpi_match_table = spidev_acpi_ids,
0830     },
0831     .probe =    spidev_probe,
0832     .remove =   spidev_remove,
0833     .id_table = spidev_spi_ids,
0834 
0835     /* NOTE:  suspend/resume methods are not necessary here.
0836      * We don't do anything except pass the requests to/from
0837      * the underlying controller.  The refrigerator handles
0838      * most issues; the controller driver handles the rest.
0839      */
0840 };
0841 
0842 /*-------------------------------------------------------------------------*/
0843 
0844 static int __init spidev_init(void)
0845 {
0846     int status;
0847 
0848     /* Claim our 256 reserved device numbers.  Then register a class
0849      * that will key udev/mdev to add/remove /dev nodes.  Last, register
0850      * the driver which manages those device numbers.
0851      */
0852     status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
0853     if (status < 0)
0854         return status;
0855 
0856     spidev_class = class_create(THIS_MODULE, "spidev");
0857     if (IS_ERR(spidev_class)) {
0858         unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
0859         return PTR_ERR(spidev_class);
0860     }
0861 
0862     status = spi_register_driver(&spidev_spi_driver);
0863     if (status < 0) {
0864         class_destroy(spidev_class);
0865         unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
0866     }
0867     return status;
0868 }
0869 module_init(spidev_init);
0870 
0871 static void __exit spidev_exit(void)
0872 {
0873     spi_unregister_driver(&spidev_spi_driver);
0874     class_destroy(spidev_class);
0875     unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
0876 }
0877 module_exit(spidev_exit);
0878 
0879 MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
0880 MODULE_DESCRIPTION("User mode SPI device interface");
0881 MODULE_LICENSE("GPL");
0882 MODULE_ALIAS("spi:spidev");