]> err.no Git - linux-2.6/blobdiff - drivers/spi/spi.c
V4L/DVB (3661): Add wm8739 stereo audio ADC i2c driver
[linux-2.6] / drivers / spi / spi.c
index 2ecb86cb36892fc83087fb66688e6154185d83b8..94f5e8ed83a7a8427105201aaa81f14ff5c710c7 100644 (file)
@@ -38,7 +38,7 @@ static void spidev_release(struct device *dev)
        if (spi->master->cleanup)
                spi->master->cleanup(spi);
 
-       class_device_put(&spi->master->cdev);
+       spi_master_put(spi->master);
        kfree(dev);
 }
 
@@ -198,7 +198,7 @@ spi_new_device(struct spi_master *master, struct spi_board_info *chip)
 
        /* NOTE:  caller did any chip->bus_num checks necessary */
 
-       if (!class_device_get(&master->cdev))
+       if (!spi_master_get(master))
                return NULL;
 
        proxy = kzalloc(sizeof *proxy, GFP_KERNEL);
@@ -244,7 +244,7 @@ spi_new_device(struct spi_master *master, struct spi_board_info *chip)
        return proxy;
 
 fail:
-       class_device_put(&master->cdev);
+       spi_master_put(master);
        kfree(proxy);
        return NULL;
 }
@@ -324,8 +324,6 @@ static void spi_master_release(struct class_device *cdev)
        struct spi_master *master;
 
        master = container_of(cdev, struct spi_master, cdev);
-       put_device(master->cdev.dev);
-       master->cdev.dev = NULL;
        kfree(master);
 }
 
@@ -339,8 +337,9 @@ 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; a pointer to this
- *     memory in the class_data field of the returned class_device
+ * @size: how much driver-private data to preallocate; the pointer to this
+ *     memory is in the class_data field of the returned class_device,
+ *     accessible with spi_master_get_devdata().
  *
  * This call is used only by SPI master controller drivers, which are the
  * only ones directly touching chip registers.  It's how they allocate
@@ -350,14 +349,17 @@ static struct class spi_master_class = {
  * master structure on success, else NULL.
  *
  * The caller is responsible for assigning the bus number and initializing
- * the master's methods before calling spi_add_master(), or else (on error)
- * calling class_device_put() to prevent a memory leak.
+ * the master's methods before calling spi_add_master(); and (after errors
+ * adding the device) calling spi_master_put() to prevent a memory leak.
  */
 struct spi_master * __init_or_module
 spi_alloc_master(struct device *dev, unsigned size)
 {
        struct spi_master       *master;
 
+       if (!dev)
+               return NULL;
+
        master = kzalloc(size + sizeof *master, SLAB_KERNEL);
        if (!master)
                return NULL;
@@ -365,7 +367,7 @@ spi_alloc_master(struct device *dev, unsigned size)
        class_device_initialize(&master->cdev);
        master->cdev.class = &spi_master_class;
        master->cdev.dev = get_device(dev);
-       class_set_devdata(&master->cdev, &master[1]);
+       spi_master_set_devdata(master, &master[1]);
 
        return master;
 }
@@ -387,6 +389,8 @@ EXPORT_SYMBOL_GPL(spi_alloc_master);
  *
  * This must be called from context that can sleep.  It returns zero on
  * success, else a negative error code (dropping the master's refcount).
+ * After a successful return, the caller is responsible for calling
+ * spi_unregister_master().
  */
 int __init_or_module
 spi_register_master(struct spi_master *master)
@@ -396,6 +400,9 @@ spi_register_master(struct spi_master *master)
        int                     status = -ENODEV;
        int                     dynamic = 0;
 
+       if (!dev)
+               return -ENODEV;
+
        /* convention:  dynamically assigned bus IDs count down from the max */
        if (master->bus_num == 0) {
                master->bus_num = atomic_dec_return(&dyn_bus_id);
@@ -425,7 +432,7 @@ EXPORT_SYMBOL_GPL(spi_register_master);
 static int __unregister(struct device *dev, void *unused)
 {
        /* note: before about 2.6.14-rc1 this would corrupt memory: */
-       device_unregister(dev);
+       spi_unregister_device(to_spi_device(dev));
        return 0;
 }
 
@@ -440,8 +447,8 @@ static int __unregister(struct device *dev, void *unused)
  */
 void spi_unregister_master(struct spi_master *master)
 {
-       class_device_unregister(&master->cdev);
        (void) device_for_each_child(master->cdev.dev, NULL, __unregister);
+       class_device_unregister(&master->cdev);
 }
 EXPORT_SYMBOL_GPL(spi_unregister_master);
 
@@ -472,6 +479,11 @@ EXPORT_SYMBOL_GPL(spi_busnum_to_master);
 
 /*-------------------------------------------------------------------------*/
 
+static void spi_complete(void *arg)
+{
+       complete(arg);
+}
+
 /**
  * spi_sync - blocking/synchronous SPI data transfers
  * @spi: device with which data will be exchanged
@@ -487,6 +499,9 @@ EXPORT_SYMBOL_GPL(spi_busnum_to_master);
  * by leaving it selected in anticipation that the next message will go
  * to the same chip.  (That may increase power usage.)
  *
+ * Also, the caller is guaranteeing that the memory associated with the
+ * message will not be freed before this call returns.
+ *
  * 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
@@ -497,7 +512,7 @@ int spi_sync(struct spi_device *spi, struct spi_message *message)
        DECLARE_COMPLETION(done);
        int status;
 
-       message->complete = (void (*)(void *)) complete;
+       message->complete = spi_complete;
        message->context = &done;
        status = spi_async(spi, message);
        if (status == 0)
@@ -524,9 +539,9 @@ static u8   *buf;
  * is zero for success, else a negative errno status code.
  * This call may only be used from a context that may sleep.
  *
- * Parameters to this routine are always copied using a small buffer,
- * large transfers should use use spi_{async,sync}() calls with
- * dma-safe buffers.
+ * Parameters to this routine are always copied using a small buffer;
+ * 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,
                const u8 *txbuf, unsigned n_tx,
@@ -546,6 +561,17 @@ int spi_write_then_read(struct spi_device *spi,
        if ((n_tx + n_rx) > SPI_BUFSIZ)
                return -EINVAL;
 
+       spi_message_init(&message);
+       memset(x, 0, sizeof x);
+       if (n_tx) {
+               x[0].len = n_tx;
+               spi_message_add_tail(&x[0], &message);
+       }
+       if (n_rx) {
+               x[1].len = n_rx;
+               spi_message_add_tail(&x[1], &message);
+       }
+
        /* ... unless someone else is using the pre-allocated buffer */
        if (down_trylock(&lock)) {
                local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
@@ -554,18 +580,11 @@ int spi_write_then_read(struct spi_device *spi,
        } else
                local_buf = buf;
 
-       memset(x, 0, sizeof x);
-
        memcpy(local_buf, txbuf, n_tx);
        x[0].tx_buf = local_buf;
-       x[0].len = n_tx;
-
        x[1].rx_buf = local_buf + n_tx;
-       x[1].len = n_rx;
 
        /* do the i/o */
-       message.transfers = x;
-       message.n_transfer = ARRAY_SIZE(x);
        status = spi_sync(spi, &message);
        if (status == 0) {
                memcpy(rxbuf, x[1].rx_buf, n_rx);