netns xfrm: fix "ip xfrm state|policy count" misreport
[safe/jmp/linux-2.6] / drivers / spi / spidev.c
index ddbe1a5..ea1bec3 100644 (file)
@@ -30,7 +30,6 @@
 #include <linux/errno.h>
 #include <linux/mutex.h>
 #include <linux/slab.h>
-#include <linux/smp_lock.h>
 
 #include <linux/spi/spi.h>
 #include <linux/spi/spidev.h>
@@ -42,7 +41,7 @@
  * This supports acccess to SPI devices using normal userspace I/O calls.
  * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
  * and often mask message boundaries, full SPI support requires full duplex
- * transfers.  There are several kinds of of internal message boundaries to
+ * transfers.  There are several kinds of internal message boundaries to
  * handle chipselect management and other protocol options.
  *
  * SPI has a character major number assigned.  We allocate minor numbers
 #define SPIDEV_MAJOR                   153     /* assigned */
 #define N_SPI_MINORS                   32      /* ... up to 256 */
 
-static unsigned long   minors[N_SPI_MINORS / BITS_PER_LONG];
+static DECLARE_BITMAP(minors, N_SPI_MINORS);
 
 
 /* Bit masks for spi_device.mode management.  Note that incorrect
- * settings for CS_HIGH and 3WIRE can cause *lots* of trouble for other
- * devices on a shared bus:  CS_HIGH, because this device will be
- * active when it shouldn't be;  3WIRE, because when active it won't
- * behave as it should.
+ * settings for some settings can cause *lots* of trouble for other
+ * devices on a shared bus:
  *
- * REVISIT should changing those two modes be privileged?
+ *  - CS_HIGH ... this device will be active when it shouldn't be
+ *  - 3WIRE ... when active, it won't behave as it should
+ *  - NO_CS ... there will be no explicit message boundaries; this
+ *     is completely incompatible with the shared bus model
+ *  - READY ... transfers may proceed when they shouldn't.
+ *
+ * REVISIT should changing those flags be privileged?
  */
 #define SPI_MODE_MASK          (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
-                               | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP)
+                               | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
+                               | SPI_NO_CS | SPI_READY)
 
 struct spidev_data {
        dev_t                   devt;
@@ -228,7 +232,6 @@ static int spidev_message(struct spidev_data *spidev,
         * We walk the array of user-provided transfers, using each one
         * to initialize a kernel version of the same transfer.
         */
-       mutex_lock(&spidev->buf_lock);
        buf = spidev->buffer;
        total = 0;
        for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
@@ -263,15 +266,15 @@ static int spidev_message(struct spidev_data *spidev,
                k_tmp->delay_usecs = u_tmp->delay_usecs;
                k_tmp->speed_hz = u_tmp->speed_hz;
 #ifdef VERBOSE
-               dev_dbg(&spi->dev,
+               dev_dbg(&spidev->spi->dev,
                        "  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 " : "",
-                       u_tmp->bits_per_word ? : spi->bits_per_word,
+                       u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
                        u_tmp->delay_usecs,
-                       u_tmp->speed_hz ? : spi->max_speed_hz);
+                       u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
 #endif
                spi_message_add_tail(k_tmp, &msg);
        }
@@ -296,14 +299,12 @@ static int spidev_message(struct spidev_data *spidev,
        status = total;
 
 done:
-       mutex_unlock(&spidev->buf_lock);
        kfree(k_xfers);
        return status;
 }
 
-static int
-spidev_ioctl(struct inode *inode, struct file *filp,
-               unsigned int cmd, unsigned long arg)
+static long
+spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
        int                     err = 0;
        int                     retval = 0;
@@ -341,6 +342,14 @@ spidev_ioctl(struct inode *inode, struct file *filp,
        if (spi == NULL)
                return -ESHUTDOWN;
 
+       /* use the buffer lock here for triple duty:
+        *  - prevent I/O (from us) so calling spi_setup() is safe;
+        *  - prevent concurrent SPI_IOC_WR_* from morphing
+        *    data fields while SPI_IOC_RD_* reads them;
+        *  - SPI_IOC_MESSAGE needs the buffer locked "normally".
+        */
+       mutex_lock(&spidev->buf_lock);
+
        switch (cmd) {
        /* read requests */
        case SPI_IOC_RD_MODE:
@@ -456,6 +465,8 @@ spidev_ioctl(struct inode *inode, struct file *filp,
                kfree(ioc);
                break;
        }
+
+       mutex_unlock(&spidev->buf_lock);
        spi_dev_put(spi);
        return retval;
 }
@@ -465,7 +476,6 @@ static int spidev_open(struct inode *inode, struct file *filp)
        struct spidev_data      *spidev;
        int                     status = -ENXIO;
 
-       lock_kernel();
        mutex_lock(&device_list_lock);
 
        list_for_each_entry(spidev, &device_list, device_entry) {
@@ -491,7 +501,6 @@ static int spidev_open(struct inode *inode, struct file *filp)
                pr_debug("spidev: nothing for minor %d\n", iminor(inode));
 
        mutex_unlock(&device_list_lock);
-       unlock_kernel();
        return status;
 }
 
@@ -525,7 +534,7 @@ static int spidev_release(struct inode *inode, struct file *filp)
        return status;
 }
 
-static struct file_operations spidev_fops = {
+static const struct file_operations spidev_fops = {
        .owner =        THIS_MODULE,
        /* REVISIT switch to aio primitives, so that userspace
         * gets more complete API coverage.  It'll simplify things
@@ -533,7 +542,7 @@ static struct file_operations spidev_fops = {
         */
        .write =        spidev_write,
        .read =         spidev_read,
-       .ioctl =        spidev_ioctl,
+       .unlocked_ioctl = spidev_ioctl,
        .open =         spidev_open,
        .release =      spidev_release,
 };
@@ -549,7 +558,7 @@ static struct class *spidev_class;
 
 /*-------------------------------------------------------------------------*/
 
-static int spidev_probe(struct spi_device *spi)
+static int __devinit spidev_probe(struct spi_device *spi)
 {
        struct spidev_data      *spidev;
        int                     status;
@@ -577,8 +586,8 @@ static int spidev_probe(struct spi_device *spi)
 
                spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
                dev = device_create(spidev_class, &spi->dev, spidev->devt,
-                               "spidev%d.%d",
-                               spi->master->bus_num, spi->chip_select);
+                                   spidev, "spidev%d.%d",
+                                   spi->master->bus_num, spi->chip_select);
                status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
        } else {
                dev_dbg(&spi->dev, "no minor number available!\n");
@@ -586,18 +595,19 @@ static int spidev_probe(struct spi_device *spi)
        }
        if (status == 0) {
                set_bit(minor, minors);
-               spi_set_drvdata(spi, spidev);
                list_add(&spidev->device_entry, &device_list);
        }
        mutex_unlock(&device_list_lock);
 
-       if (status != 0)
+       if (status == 0)
+               spi_set_drvdata(spi, spidev);
+       else
                kfree(spidev);
 
        return status;
 }
 
-static int spidev_remove(struct spi_device *spi)
+static int __devexit spidev_remove(struct spi_device *spi)
 {
        struct spidev_data      *spidev = spi_get_drvdata(spi);
 
@@ -619,7 +629,7 @@ static int spidev_remove(struct spi_device *spi)
        return 0;
 }
 
-static struct spi_driver spidev_spi = {
+static struct spi_driver spidev_spi_driver = {
        .driver = {
                .name =         "spidev",
                .owner =        THIS_MODULE,
@@ -651,14 +661,14 @@ static int __init spidev_init(void)
 
        spidev_class = class_create(THIS_MODULE, "spidev");
        if (IS_ERR(spidev_class)) {
-               unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
+               unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
                return PTR_ERR(spidev_class);
        }
 
-       status = spi_register_driver(&spidev_spi);
+       status = spi_register_driver(&spidev_spi_driver);
        if (status < 0) {
                class_destroy(spidev_class);
-               unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
+               unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
        }
        return status;
 }
@@ -666,12 +676,13 @@ module_init(spidev_init);
 
 static void __exit spidev_exit(void)
 {
-       spi_unregister_driver(&spidev_spi);
+       spi_unregister_driver(&spidev_spi_driver);
        class_destroy(spidev_class);
-       unregister_chrdev(SPIDEV_MAJOR, spidev_spi.driver.name);
+       unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
 }
 module_exit(spidev_exit);
 
 MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
 MODULE_DESCRIPTION("User mode SPI device interface");
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("spi:spidev");