From 148ddb8a6f1b3f9da50e338588568bc495db49d5 Mon Sep 17 00:00:00 2001 From: Ian Lepore Date: Tue, 2 Jun 2015 16:07:28 +0000 Subject: [PATCH] Add a missing wakeup when releasing ownership of the SPI hardware. Also, validate the chipselect parameter before grabbing ownership of the hardware, and report timeout errors after releasing it. PR: 200584 --- sys/arm/broadcom/bcm2835/bcm2835_spi.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/sys/arm/broadcom/bcm2835/bcm2835_spi.c b/sys/arm/broadcom/bcm2835/bcm2835_spi.c index 3692c6770ce..99de5e7d6c3 100644 --- a/sys/arm/broadcom/bcm2835/bcm2835_spi.c +++ b/sys/arm/broadcom/bcm2835/bcm2835_spi.c @@ -427,6 +427,15 @@ bcm_spi_transfer(device_t dev, device_t child, struct spi_command *cmd) KASSERT(cmd->tx_data_sz == cmd->rx_data_sz, ("TX/RX data sizes should be equal")); + /* Get the proper chip select for this child. */ + spibus_get_cs(child, &cs); + if (cs < 0 || cs > 2) { + device_printf(dev, + "Invalid chip select %d requested by %s\n", cs, + device_get_nameunit(child)); + return (EINVAL); + } + BCM_SPI_LOCK(sc); /* If the controller is in use wait until it is available. */ @@ -441,16 +450,6 @@ bcm_spi_transfer(device_t dev, device_t child, struct spi_command *cmd) SPI_CS_CLEAR_RXFIFO | SPI_CS_CLEAR_TXFIFO, SPI_CS_CLEAR_RXFIFO | SPI_CS_CLEAR_TXFIFO); - /* Get the proper chip select for this child. */ - spibus_get_cs(child, &cs); - if (cs < 0 || cs > 2) { - device_printf(dev, - "Invalid chip select %d requested by %s\n", cs, - device_get_nameunit(child)); - BCM_SPI_UNLOCK(sc); - return (EINVAL); - } - /* Save a pointer to the SPI command. */ sc->sc_cmd = cmd; sc->sc_read = 0; @@ -471,8 +470,10 @@ bcm_spi_transfer(device_t dev, device_t child, struct spi_command *cmd) /* Make sure the SPI engine and interrupts are disabled. */ bcm_spi_modifyreg(sc, SPI_CS, SPI_CS_TA | SPI_CS_INTR | SPI_CS_INTD, 0); - /* Clear the controller flags. */ + /* Release the controller and wakeup the next thread waiting for it. */ sc->sc_flags = 0; + wakeup_one(dev); + BCM_SPI_UNLOCK(sc); /* * Check for transfer timeout. The SPI controller doesn't @@ -483,8 +484,6 @@ bcm_spi_transfer(device_t dev, device_t child, struct spi_command *cmd) err = EIO; } - BCM_SPI_UNLOCK(sc); - return (err); }