diff options
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r-- | drivers/spi/spi.c | 80 |
1 files changed, 53 insertions, 27 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 349ebba4b199..d0b28bba38be 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c | |||
@@ -58,6 +58,11 @@ static ssize_t | |||
58 | modalias_show(struct device *dev, struct device_attribute *a, char *buf) | 58 | modalias_show(struct device *dev, struct device_attribute *a, char *buf) |
59 | { | 59 | { |
60 | const struct spi_device *spi = to_spi_device(dev); | 60 | const struct spi_device *spi = to_spi_device(dev); |
61 | int len; | ||
62 | |||
63 | len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1); | ||
64 | if (len != -ENODEV) | ||
65 | return len; | ||
61 | 66 | ||
62 | return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); | 67 | return sprintf(buf, "%s%s\n", SPI_MODULE_PREFIX, spi->modalias); |
63 | } | 68 | } |
@@ -114,6 +119,11 @@ static int spi_match_device(struct device *dev, struct device_driver *drv) | |||
114 | static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) | 119 | static int spi_uevent(struct device *dev, struct kobj_uevent_env *env) |
115 | { | 120 | { |
116 | const struct spi_device *spi = to_spi_device(dev); | 121 | const struct spi_device *spi = to_spi_device(dev); |
122 | int rc; | ||
123 | |||
124 | rc = acpi_device_uevent_modalias(dev, env); | ||
125 | if (rc != -ENODEV) | ||
126 | return rc; | ||
117 | 127 | ||
118 | add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); | 128 | add_uevent_var(env, "MODALIAS=%s%s", SPI_MODULE_PREFIX, spi->modalias); |
119 | return 0; | 129 | return 0; |
@@ -370,6 +380,17 @@ static void spi_dev_set_name(struct spi_device *spi) | |||
370 | spi->chip_select); | 380 | spi->chip_select); |
371 | } | 381 | } |
372 | 382 | ||
383 | static int spi_dev_check(struct device *dev, void *data) | ||
384 | { | ||
385 | struct spi_device *spi = to_spi_device(dev); | ||
386 | struct spi_device *new_spi = data; | ||
387 | |||
388 | if (spi->master == new_spi->master && | ||
389 | spi->chip_select == new_spi->chip_select) | ||
390 | return -EBUSY; | ||
391 | return 0; | ||
392 | } | ||
393 | |||
373 | /** | 394 | /** |
374 | * spi_add_device - Add spi_device allocated with spi_alloc_device | 395 | * spi_add_device - Add spi_device allocated with spi_alloc_device |
375 | * @spi: spi_device to register | 396 | * @spi: spi_device to register |
@@ -384,7 +405,6 @@ int spi_add_device(struct spi_device *spi) | |||
384 | static DEFINE_MUTEX(spi_add_lock); | 405 | static DEFINE_MUTEX(spi_add_lock); |
385 | struct spi_master *master = spi->master; | 406 | struct spi_master *master = spi->master; |
386 | struct device *dev = master->dev.parent; | 407 | struct device *dev = master->dev.parent; |
387 | struct device *d; | ||
388 | int status; | 408 | int status; |
389 | 409 | ||
390 | /* Chipselects are numbered 0..max; validate. */ | 410 | /* Chipselects are numbered 0..max; validate. */ |
@@ -404,12 +424,10 @@ int spi_add_device(struct spi_device *spi) | |||
404 | */ | 424 | */ |
405 | mutex_lock(&spi_add_lock); | 425 | mutex_lock(&spi_add_lock); |
406 | 426 | ||
407 | d = bus_find_device_by_name(&spi_bus_type, NULL, dev_name(&spi->dev)); | 427 | status = bus_for_each_dev(&spi_bus_type, NULL, spi, spi_dev_check); |
408 | if (d != NULL) { | 428 | if (status) { |
409 | dev_err(dev, "chipselect %d already in use\n", | 429 | dev_err(dev, "chipselect %d already in use\n", |
410 | spi->chip_select); | 430 | spi->chip_select); |
411 | put_device(d); | ||
412 | status = -EBUSY; | ||
413 | goto done; | 431 | goto done; |
414 | } | 432 | } |
415 | 433 | ||
@@ -591,8 +609,10 @@ static int spi_transfer_one_message(struct spi_master *master, | |||
591 | goto out; | 609 | goto out; |
592 | } | 610 | } |
593 | 611 | ||
594 | if (ret > 0) | 612 | if (ret > 0) { |
613 | ret = 0; | ||
595 | wait_for_completion(&master->xfer_completion); | 614 | wait_for_completion(&master->xfer_completion); |
615 | } | ||
596 | 616 | ||
597 | trace_spi_transfer_stop(msg, xfer); | 617 | trace_spi_transfer_stop(msg, xfer); |
598 | 618 | ||
@@ -632,7 +652,7 @@ out: | |||
632 | * | 652 | * |
633 | * Called by SPI drivers using the core transfer_one_message() | 653 | * Called by SPI drivers using the core transfer_one_message() |
634 | * implementation to notify it that the current interrupt driven | 654 | * implementation to notify it that the current interrupt driven |
635 | * transfer has finised and the next one may be scheduled. | 655 | * transfer has finished and the next one may be scheduled. |
636 | */ | 656 | */ |
637 | void spi_finalize_current_transfer(struct spi_master *master) | 657 | void spi_finalize_current_transfer(struct spi_master *master) |
638 | { | 658 | { |
@@ -685,7 +705,7 @@ static void spi_pump_messages(struct kthread_work *work) | |||
685 | } | 705 | } |
686 | /* Extract head of queue */ | 706 | /* Extract head of queue */ |
687 | master->cur_msg = | 707 | master->cur_msg = |
688 | list_entry(master->queue.next, struct spi_message, queue); | 708 | list_first_entry(&master->queue, struct spi_message, queue); |
689 | 709 | ||
690 | list_del_init(&master->cur_msg->queue); | 710 | list_del_init(&master->cur_msg->queue); |
691 | if (master->busy) | 711 | if (master->busy) |
@@ -791,11 +811,8 @@ struct spi_message *spi_get_next_queued_message(struct spi_master *master) | |||
791 | 811 | ||
792 | /* get a pointer to the next message, if any */ | 812 | /* get a pointer to the next message, if any */ |
793 | spin_lock_irqsave(&master->queue_lock, flags); | 813 | spin_lock_irqsave(&master->queue_lock, flags); |
794 | if (list_empty(&master->queue)) | 814 | next = list_first_entry_or_null(&master->queue, struct spi_message, |
795 | next = NULL; | 815 | queue); |
796 | else | ||
797 | next = list_entry(master->queue.next, | ||
798 | struct spi_message, queue); | ||
799 | spin_unlock_irqrestore(&master->queue_lock, flags); | 816 | spin_unlock_irqrestore(&master->queue_lock, flags); |
800 | 817 | ||
801 | return next; | 818 | return next; |
@@ -1596,15 +1613,11 @@ int spi_setup(struct spi_device *spi) | |||
1596 | } | 1613 | } |
1597 | EXPORT_SYMBOL_GPL(spi_setup); | 1614 | EXPORT_SYMBOL_GPL(spi_setup); |
1598 | 1615 | ||
1599 | static int __spi_async(struct spi_device *spi, struct spi_message *message) | 1616 | static int __spi_validate(struct spi_device *spi, struct spi_message *message) |
1600 | { | 1617 | { |
1601 | struct spi_master *master = spi->master; | 1618 | struct spi_master *master = spi->master; |
1602 | struct spi_transfer *xfer; | 1619 | struct spi_transfer *xfer; |
1603 | 1620 | ||
1604 | message->spi = spi; | ||
1605 | |||
1606 | trace_spi_message_submit(message); | ||
1607 | |||
1608 | if (list_empty(&message->transfers)) | 1621 | if (list_empty(&message->transfers)) |
1609 | return -EINVAL; | 1622 | return -EINVAL; |
1610 | if (!message->complete) | 1623 | if (!message->complete) |
@@ -1667,9 +1680,8 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message) | |||
1667 | if (xfer->rx_buf && !xfer->rx_nbits) | 1680 | if (xfer->rx_buf && !xfer->rx_nbits) |
1668 | xfer->rx_nbits = SPI_NBITS_SINGLE; | 1681 | xfer->rx_nbits = SPI_NBITS_SINGLE; |
1669 | /* check transfer tx/rx_nbits: | 1682 | /* check transfer tx/rx_nbits: |
1670 | * 1. keep the value is not out of single, dual and quad | 1683 | * 1. check the value matches one of single, dual and quad |
1671 | * 2. keep tx/rx_nbits is contained by mode in spi_device | 1684 | * 2. check tx/rx_nbits match the mode in spi_device |
1672 | * 3. if SPI_3WIRE, tx/rx_nbits should be in single | ||
1673 | */ | 1685 | */ |
1674 | if (xfer->tx_buf) { | 1686 | if (xfer->tx_buf) { |
1675 | if (xfer->tx_nbits != SPI_NBITS_SINGLE && | 1687 | if (xfer->tx_nbits != SPI_NBITS_SINGLE && |
@@ -1682,9 +1694,6 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message) | |||
1682 | if ((xfer->tx_nbits == SPI_NBITS_QUAD) && | 1694 | if ((xfer->tx_nbits == SPI_NBITS_QUAD) && |
1683 | !(spi->mode & SPI_TX_QUAD)) | 1695 | !(spi->mode & SPI_TX_QUAD)) |
1684 | return -EINVAL; | 1696 | return -EINVAL; |
1685 | if ((spi->mode & SPI_3WIRE) && | ||
1686 | (xfer->tx_nbits != SPI_NBITS_SINGLE)) | ||
1687 | return -EINVAL; | ||
1688 | } | 1697 | } |
1689 | /* check transfer rx_nbits */ | 1698 | /* check transfer rx_nbits */ |
1690 | if (xfer->rx_buf) { | 1699 | if (xfer->rx_buf) { |
@@ -1698,13 +1707,22 @@ static int __spi_async(struct spi_device *spi, struct spi_message *message) | |||
1698 | if ((xfer->rx_nbits == SPI_NBITS_QUAD) && | 1707 | if ((xfer->rx_nbits == SPI_NBITS_QUAD) && |
1699 | !(spi->mode & SPI_RX_QUAD)) | 1708 | !(spi->mode & SPI_RX_QUAD)) |
1700 | return -EINVAL; | 1709 | return -EINVAL; |
1701 | if ((spi->mode & SPI_3WIRE) && | ||
1702 | (xfer->rx_nbits != SPI_NBITS_SINGLE)) | ||
1703 | return -EINVAL; | ||
1704 | } | 1710 | } |
1705 | } | 1711 | } |
1706 | 1712 | ||
1707 | message->status = -EINPROGRESS; | 1713 | message->status = -EINPROGRESS; |
1714 | |||
1715 | return 0; | ||
1716 | } | ||
1717 | |||
1718 | static int __spi_async(struct spi_device *spi, struct spi_message *message) | ||
1719 | { | ||
1720 | struct spi_master *master = spi->master; | ||
1721 | |||
1722 | message->spi = spi; | ||
1723 | |||
1724 | trace_spi_message_submit(message); | ||
1725 | |||
1708 | return master->transfer(spi, message); | 1726 | return master->transfer(spi, message); |
1709 | } | 1727 | } |
1710 | 1728 | ||
@@ -1743,6 +1761,10 @@ int spi_async(struct spi_device *spi, struct spi_message *message) | |||
1743 | int ret; | 1761 | int ret; |
1744 | unsigned long flags; | 1762 | unsigned long flags; |
1745 | 1763 | ||
1764 | ret = __spi_validate(spi, message); | ||
1765 | if (ret != 0) | ||
1766 | return ret; | ||
1767 | |||
1746 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); | 1768 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
1747 | 1769 | ||
1748 | if (master->bus_lock_flag) | 1770 | if (master->bus_lock_flag) |
@@ -1791,6 +1813,10 @@ int spi_async_locked(struct spi_device *spi, struct spi_message *message) | |||
1791 | int ret; | 1813 | int ret; |
1792 | unsigned long flags; | 1814 | unsigned long flags; |
1793 | 1815 | ||
1816 | ret = __spi_validate(spi, message); | ||
1817 | if (ret != 0) | ||
1818 | return ret; | ||
1819 | |||
1794 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); | 1820 | spin_lock_irqsave(&master->bus_lock_spinlock, flags); |
1795 | 1821 | ||
1796 | ret = __spi_async(spi, message); | 1822 | ret = __spi_async(spi, message); |