]> err.no Git - linux-2.6/blobdiff - drivers/spi/spi.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[linux-2.6] / drivers / spi / spi.c
index 6657331eed9360b6323267261a9e7d080965a2e7..e84d21597943d85d1752270526f43232221f1e7c 100644 (file)
@@ -23,6 +23,7 @@
 #include <linux/device.h>
 #include <linux/init.h>
 #include <linux/cache.h>
+#include <linux/mutex.h>
 #include <linux/spi/spi.h>
 
 
@@ -152,6 +153,11 @@ static void spi_drv_shutdown(struct device *dev)
        sdrv->shutdown(to_spi_device(dev));
 }
 
+/**
+ * spi_register_driver - register a SPI driver
+ * @sdrv: the driver to register
+ * Context: can sleep
+ */
 int spi_register_driver(struct spi_driver *sdrv)
 {
        sdrv->driver.bus = &spi_bus_type;
@@ -180,14 +186,22 @@ struct boardinfo {
 };
 
 static LIST_HEAD(board_list);
-static DECLARE_MUTEX(board_lock);
+static DEFINE_MUTEX(board_lock);
 
 
-/* On typical mainboards, this is purely internal; and it's not needed
+/**
+ * spi_new_device - instantiate one new SPI device
+ * @master: Controller to which device is connected
+ * @chip: Describes the SPI device
+ * Context: can sleep
+ *
+ * On typical mainboards, this is purely internal; and it's not needed
  * after board init creates the hard-wired devices.  Some development
  * platforms may not be able to use spi_register_board_info though, and
  * this is exported so that for example a USB or parport based adapter
  * driver could add devices (which it would learn about out-of-band).
+ *
+ * Returns the new device, or NULL.
  */
 struct spi_device *spi_new_device(struct spi_master *master,
                                  struct spi_board_info *chip)
@@ -196,7 +210,20 @@ struct spi_device *spi_new_device(struct spi_master *master,
        struct device           *dev = master->cdev.dev;
        int                     status;
 
-       /* NOTE:  caller did any chip->bus_num checks necessary */
+       /* NOTE:  caller did any chip->bus_num checks necessary.
+        *
+        * Also, unless we change the return value convention to use
+        * error-or-pointer (not NULL-or-pointer), troubleshootability
+        * suggests syslogged diagnostics are best here (ugh).
+        */
+
+       /* Chipselects are numbered 0..max; validate. */
+       if (chip->chip_select >= master->num_chipselect) {
+               dev_err(dev, "cs%d > max %d\n",
+                       chip->chip_select,
+                       master->num_chipselect);
+               return NULL;
+       }
 
        if (!spi_master_get(master))
                return NULL;
@@ -224,10 +251,10 @@ struct spi_device *spi_new_device(struct spi_master *master,
        proxy->controller_state = NULL;
        proxy->dev.release = spidev_release;
 
-       /* drivers may modify this default i/o setup */
+       /* drivers may modify this initial i/o setup */
        status = master->setup(proxy);
        if (status < 0) {
-               dev_dbg(dev, "can't %s %s, status %d\n",
+               dev_err(dev, "can't %s %s, status %d\n",
                                "setup", proxy->dev.bus_id, status);
                goto fail;
        }
@@ -237,7 +264,7 @@ struct spi_device *spi_new_device(struct spi_master *master,
         */
        status = device_register(&proxy->dev);
        if (status < 0) {
-               dev_dbg(dev, "can't %s %s, status %d\n",
+               dev_err(dev, "can't %s %s, status %d\n",
                                "add", proxy->dev.bus_id, status);
                goto fail;
        }
@@ -251,7 +278,12 @@ fail:
 }
 EXPORT_SYMBOL_GPL(spi_new_device);
 
-/*
+/**
+ * spi_register_board_info - register SPI devices for a given board
+ * @info: array of chip descriptors
+ * @n: how many descriptors are provided
+ * Context: can sleep
+ *
  * Board-specific early init code calls this (probably during arch_initcall)
  * with segments of the SPI device table.  Any device nodes are created later,
  * after the relevant parent SPI controller (bus_num) is defined.  We keep
@@ -276,9 +308,9 @@ spi_register_board_info(struct spi_board_info const *info, unsigned n)
        bi->n_board_info = n;
        memcpy(bi->board_info, info, n * sizeof *info);
 
-       down(&board_lock);
+       mutex_lock(&board_lock);
        list_add_tail(&bi->list, &board_list);
-       up(&board_lock);
+       mutex_unlock(&board_lock);
        return 0;
 }
 
@@ -286,13 +318,11 @@ spi_register_board_info(struct spi_board_info const *info, unsigned n)
  * creates board info from kernel command lines
  */
 
-static void __init_or_module
-scan_boardinfo(struct spi_master *master)
+static void scan_boardinfo(struct spi_master *master)
 {
        struct boardinfo        *bi;
-       struct device           *dev = master->cdev.dev;
 
-       down(&board_lock);
+       mutex_lock(&board_lock);
        list_for_each_entry(bi, &board_list, list) {
                struct spi_board_info   *chip = bi->board_info;
                unsigned                n;
@@ -300,21 +330,13 @@ scan_boardinfo(struct spi_master *master)
                for (n = bi->n_board_info; n > 0; n--, chip++) {
                        if (chip->bus_num != master->bus_num)
                                continue;
-                       /* some controllers only have one chip, so they
-                        * might not use chipselects.  otherwise, the
-                        * chipselects are numbered 0..max.
+                       /* NOTE: this relies on spi_new_device to
+                        * issue diagnostics when given bogus inputs
                         */
-                       if (chip->chip_select >= master->num_chipselect
-                                       && master->num_chipselect) {
-                               dev_dbg(dev, "cs%d > max %d\n",
-                                       chip->chip_select,
-                                       master->num_chipselect);
-                               continue;
-                       }
                        (void) spi_new_device(master, chip);
                }
        }
-       up(&board_lock);
+       mutex_unlock(&board_lock);
 }
 
 /*-------------------------------------------------------------------------*/
@@ -337,9 +359,10 @@ static struct class spi_master_class = {
 /**
  * spi_alloc_master - allocate SPI master controller
  * @dev: the controller, possibly using the platform_bus
- * @size: how much driver-private data to preallocate; the pointer to this
+ * @size: how much zeroed driver-private data to allocate; the pointer to this
  *     memory is in the class_data field of the returned class_device,
  *     accessible with spi_master_get_devdata().
+ * Context: can sleep
  *
  * This call is used only by SPI master controller drivers, which are the
  * only ones directly touching chip registers.  It's how they allocate
@@ -375,6 +398,7 @@ EXPORT_SYMBOL_GPL(spi_alloc_master);
 /**
  * spi_register_master - register SPI master controller
  * @master: initialized master, originally from spi_alloc_master()
+ * Context: can sleep
  *
  * SPI master controllers connect to their drivers using some non-SPI bus,
  * such as the platform bus.  The final stage of probe() in that code
@@ -393,7 +417,7 @@ EXPORT_SYMBOL_GPL(spi_alloc_master);
  */
 int spi_register_master(struct spi_master *master)
 {
-       static atomic_t         dyn_bus_id = ATOMIC_INIT((1<<16) - 1);
+       static atomic_t         dyn_bus_id = ATOMIC_INIT((1<<15) - 1);
        struct device           *dev = master->cdev.dev;
        int                     status = -ENODEV;
        int                     dynamic = 0;
@@ -401,8 +425,17 @@ int spi_register_master(struct spi_master *master)
        if (!dev)
                return -ENODEV;
 
+       /* even if it's just one always-selected device, there must
+        * be at least one chipselect
+        */
+       if (master->num_chipselect == 0)
+               return -EINVAL;
+
        /* convention:  dynamically assigned bus IDs count down from the max */
        if (master->bus_num < 0) {
+               /* FIXME switch to an IDR based scheme, something like
+                * I2C now uses, so we can't run out of "dynamic" IDs
+                */
                master->bus_num = atomic_dec_return(&dyn_bus_id);
                dynamic = 1;
        }
@@ -437,6 +470,7 @@ static int __unregister(struct device *dev, void *unused)
 /**
  * spi_unregister_master - unregister SPI master controller
  * @master: the master being unregistered
+ * Context: can sleep
  *
  * This call is used only by SPI master controller drivers, which are the
  * only ones directly touching chip registers.
@@ -455,6 +489,7 @@ EXPORT_SYMBOL_GPL(spi_unregister_master);
 /**
  * spi_busnum_to_master - look up master associated with bus_num
  * @bus_num: the master's bus number
+ * Context: can sleep
  *
  * This call may be used with devices that are registered after
  * arch init time.  It returns a refcounted pointer to the relevant
@@ -492,6 +527,7 @@ static void spi_complete(void *arg)
  * spi_sync - blocking/synchronous SPI data transfers
  * @spi: device with which data will be exchanged
  * @message: describes the data transfers
+ * Context: can sleep
  *
  * This call may only be used from a context that may sleep.  The sleep
  * is non-interruptible, and has no timeout.  Low-overhead controller
@@ -508,7 +544,7 @@ static void spi_complete(void *arg)
  *
  * The return value is a negative error code if the message could not be
  * submitted, else zero.  When the value is zero, then message->status is
- * also defined:  it's the completion code for the transfer, either zero
+ * also defined;  it's the completion code for the transfer, either zero
  * or a negative error code from the controller driver.
  */
 int spi_sync(struct spi_device *spi, struct spi_message *message)
@@ -538,6 +574,7 @@ static u8   *buf;
  * @n_tx: size of txbuf, in bytes
  * @rxbuf: buffer into which data will be read
  * @n_rx: size of rxbuf, in bytes (need not be dma-safe)
+ * Context: can sleep
  *
  * This performs a half duplex MicroWire style transaction with the
  * device, sending txbuf and then reading rxbuf.  The return value
@@ -545,7 +582,8 @@ static u8   *buf;
  * This call may only be used from a context that may sleep.
  *
  * Parameters to this routine are always copied using a small buffer;
- * performance-sensitive or bulk transfer code should instead use
+ * portable code should never use this for more than 32 bytes.
+ * Performance-sensitive or bulk transfer code should instead use
  * spi_{async,sync}() calls with dma-safe buffers.
  */
 int spi_write_then_read(struct spi_device *spi,