aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/spi/spi.c')
-rw-r--r--drivers/spi/spi.c45
1 files changed, 23 insertions, 22 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index b31f4431849b..682a6a48fec3 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -485,6 +485,15 @@ void spi_unregister_master(struct spi_master *master)
485} 485}
486EXPORT_SYMBOL_GPL(spi_unregister_master); 486EXPORT_SYMBOL_GPL(spi_unregister_master);
487 487
488static int __spi_master_match(struct device *dev, void *data)
489{
490 struct spi_master *m;
491 u16 *bus_num = data;
492
493 m = container_of(dev, struct spi_master, dev);
494 return m->bus_num == *bus_num;
495}
496
488/** 497/**
489 * spi_busnum_to_master - look up master associated with bus_num 498 * spi_busnum_to_master - look up master associated with bus_num
490 * @bus_num: the master's bus number 499 * @bus_num: the master's bus number
@@ -499,17 +508,12 @@ struct spi_master *spi_busnum_to_master(u16 bus_num)
499{ 508{
500 struct device *dev; 509 struct device *dev;
501 struct spi_master *master = NULL; 510 struct spi_master *master = NULL;
502 struct spi_master *m; 511
503 512 dev = class_find_device(&spi_master_class, &bus_num,
504 down(&spi_master_class.sem); 513 __spi_master_match);
505 list_for_each_entry(dev, &spi_master_class.children, node) { 514 if (dev)
506 m = container_of(dev, struct spi_master, dev); 515 master = container_of(dev, struct spi_master, dev);
507 if (m->bus_num == bus_num) { 516 /* reference got in class_find_device */
508 master = spi_master_get(m);
509 break;
510 }
511 }
512 up(&spi_master_class.sem);
513 return master; 517 return master;
514} 518}
515EXPORT_SYMBOL_GPL(spi_busnum_to_master); 519EXPORT_SYMBOL_GPL(spi_busnum_to_master);
@@ -541,10 +545,7 @@ static void spi_complete(void *arg)
541 * Also, the caller is guaranteeing that the memory associated with the 545 * Also, the caller is guaranteeing that the memory associated with the
542 * message will not be freed before this call returns. 546 * message will not be freed before this call returns.
543 * 547 *
544 * The return value is a negative error code if the message could not be 548 * It returns zero on success, else a negative error code.
545 * submitted, else zero. When the value is zero, then message->status is
546 * also defined; it's the completion code for the transfer, either zero
547 * or a negative error code from the controller driver.
548 */ 549 */
549int spi_sync(struct spi_device *spi, struct spi_message *message) 550int spi_sync(struct spi_device *spi, struct spi_message *message)
550{ 551{
@@ -554,8 +555,10 @@ int spi_sync(struct spi_device *spi, struct spi_message *message)
554 message->complete = spi_complete; 555 message->complete = spi_complete;
555 message->context = &done; 556 message->context = &done;
556 status = spi_async(spi, message); 557 status = spi_async(spi, message);
557 if (status == 0) 558 if (status == 0) {
558 wait_for_completion(&done); 559 wait_for_completion(&done);
560 status = message->status;
561 }
559 message->context = NULL; 562 message->context = NULL;
560 return status; 563 return status;
561} 564}
@@ -589,7 +592,7 @@ int spi_write_then_read(struct spi_device *spi,
589 const u8 *txbuf, unsigned n_tx, 592 const u8 *txbuf, unsigned n_tx,
590 u8 *rxbuf, unsigned n_rx) 593 u8 *rxbuf, unsigned n_rx)
591{ 594{
592 static DECLARE_MUTEX(lock); 595 static DEFINE_MUTEX(lock);
593 596
594 int status; 597 int status;
595 struct spi_message message; 598 struct spi_message message;
@@ -615,7 +618,7 @@ int spi_write_then_read(struct spi_device *spi,
615 } 618 }
616 619
617 /* ... unless someone else is using the pre-allocated buffer */ 620 /* ... unless someone else is using the pre-allocated buffer */
618 if (down_trylock(&lock)) { 621 if (!mutex_trylock(&lock)) {
619 local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL); 622 local_buf = kmalloc(SPI_BUFSIZ, GFP_KERNEL);
620 if (!local_buf) 623 if (!local_buf)
621 return -ENOMEM; 624 return -ENOMEM;
@@ -628,13 +631,11 @@ int spi_write_then_read(struct spi_device *spi,
628 631
629 /* do the i/o */ 632 /* do the i/o */
630 status = spi_sync(spi, &message); 633 status = spi_sync(spi, &message);
631 if (status == 0) { 634 if (status == 0)
632 memcpy(rxbuf, x[1].rx_buf, n_rx); 635 memcpy(rxbuf, x[1].rx_buf, n_rx);
633 status = message.status;
634 }
635 636
636 if (x[0].tx_buf == buf) 637 if (x[0].tx_buf == buf)
637 up(&lock); 638 mutex_unlock(&lock);
638 else 639 else
639 kfree(local_buf); 640 kfree(local_buf);
640 641