Skip to content

Commit 4c8684f

Browse files
committed
Merge tag 'spi-fix-v5.13-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi
Pull spi fixes from Mark Brown: "A small set of SPI fixes that have come up since the merge window, all fairly small fixes for rare cases" * tag 'spi-fix-v5.13-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi: spi: stm32-qspi: Always wait BUSY bit to be cleared in stm32_qspi_wait_cmd() spi: spi-zynq-qspi: Fix some wrong goto jumps & missing error code spi: Cleanup on failure of initial setup spi: bcm2835: Fix out-of-bounds access with more than 4 slaves
2 parents 9b1111f + d38fa9a commit 4c8684f

File tree

8 files changed

+70
-25
lines changed

8 files changed

+70
-25
lines changed

drivers/spi/spi-bcm2835.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
#define BCM2835_SPI_FIFO_SIZE 64
6969
#define BCM2835_SPI_FIFO_SIZE_3_4 48
7070
#define BCM2835_SPI_DMA_MIN_LENGTH 96
71-
#define BCM2835_SPI_NUM_CS 4 /* raise as necessary */
71+
#define BCM2835_SPI_NUM_CS 24 /* raise as necessary */
7272
#define BCM2835_SPI_MODE_BITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH \
7373
| SPI_NO_CS | SPI_3WIRE)
7474

@@ -1195,6 +1195,12 @@ static int bcm2835_spi_setup(struct spi_device *spi)
11951195
struct gpio_chip *chip;
11961196
u32 cs;
11971197

1198+
if (spi->chip_select >= BCM2835_SPI_NUM_CS) {
1199+
dev_err(&spi->dev, "only %d chip-selects supported\n",
1200+
BCM2835_SPI_NUM_CS - 1);
1201+
return -EINVAL;
1202+
}
1203+
11981204
/*
11991205
* Precalculate SPI slave's CS register value for ->prepare_message():
12001206
* The driver always uses software-controlled GPIO chip select, hence
@@ -1288,7 +1294,7 @@ static int bcm2835_spi_probe(struct platform_device *pdev)
12881294
ctlr->use_gpio_descriptors = true;
12891295
ctlr->mode_bits = BCM2835_SPI_MODE_BITS;
12901296
ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
1291-
ctlr->num_chipselect = BCM2835_SPI_NUM_CS;
1297+
ctlr->num_chipselect = 3;
12921298
ctlr->setup = bcm2835_spi_setup;
12931299
ctlr->transfer_one = bcm2835_spi_transfer_one;
12941300
ctlr->handle_err = bcm2835_spi_handle_err;

drivers/spi/spi-bitbang.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ int spi_bitbang_setup(struct spi_device *spi)
184184
{
185185
struct spi_bitbang_cs *cs = spi->controller_state;
186186
struct spi_bitbang *bitbang;
187+
bool initial_setup = false;
188+
int retval;
187189

188190
bitbang = spi_master_get_devdata(spi->master);
189191

@@ -192,22 +194,30 @@ int spi_bitbang_setup(struct spi_device *spi)
192194
if (!cs)
193195
return -ENOMEM;
194196
spi->controller_state = cs;
197+
initial_setup = true;
195198
}
196199

197200
/* per-word shift register access, in hardware or bitbanging */
198201
cs->txrx_word = bitbang->txrx_word[spi->mode & (SPI_CPOL|SPI_CPHA)];
199-
if (!cs->txrx_word)
200-
return -EINVAL;
202+
if (!cs->txrx_word) {
203+
retval = -EINVAL;
204+
goto err_free;
205+
}
201206

202207
if (bitbang->setup_transfer) {
203-
int retval = bitbang->setup_transfer(spi, NULL);
208+
retval = bitbang->setup_transfer(spi, NULL);
204209
if (retval < 0)
205-
return retval;
210+
goto err_free;
206211
}
207212

208213
dev_dbg(&spi->dev, "%s, %u nsec/bit\n", __func__, 2 * cs->nsecs);
209214

210215
return 0;
216+
217+
err_free:
218+
if (initial_setup)
219+
kfree(cs);
220+
return retval;
211221
}
212222
EXPORT_SYMBOL_GPL(spi_bitbang_setup);
213223

drivers/spi/spi-fsl-spi.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ static int fsl_spi_setup(struct spi_device *spi)
440440
{
441441
struct mpc8xxx_spi *mpc8xxx_spi;
442442
struct fsl_spi_reg __iomem *reg_base;
443+
bool initial_setup = false;
443444
int retval;
444445
u32 hw_mode;
445446
struct spi_mpc8xxx_cs *cs = spi_get_ctldata(spi);
@@ -452,6 +453,7 @@ static int fsl_spi_setup(struct spi_device *spi)
452453
if (!cs)
453454
return -ENOMEM;
454455
spi_set_ctldata(spi, cs);
456+
initial_setup = true;
455457
}
456458
mpc8xxx_spi = spi_master_get_devdata(spi->master);
457459

@@ -475,6 +477,8 @@ static int fsl_spi_setup(struct spi_device *spi)
475477
retval = fsl_spi_setup_transfer(spi, NULL);
476478
if (retval < 0) {
477479
cs->hw_mode = hw_mode; /* Restore settings */
480+
if (initial_setup)
481+
kfree(cs);
478482
return retval;
479483
}
480484

drivers/spi/spi-omap-uwire.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,15 +424,22 @@ static int uwire_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
424424
static int uwire_setup(struct spi_device *spi)
425425
{
426426
struct uwire_state *ust = spi->controller_state;
427+
bool initial_setup = false;
428+
int status;
427429

428430
if (ust == NULL) {
429431
ust = kzalloc(sizeof(*ust), GFP_KERNEL);
430432
if (ust == NULL)
431433
return -ENOMEM;
432434
spi->controller_state = ust;
435+
initial_setup = true;
433436
}
434437

435-
return uwire_setup_transfer(spi, NULL);
438+
status = uwire_setup_transfer(spi, NULL);
439+
if (status && initial_setup)
440+
kfree(ust);
441+
442+
return status;
436443
}
437444

438445
static void uwire_cleanup(struct spi_device *spi)

drivers/spi/spi-omap2-mcspi.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,22 @@ static void omap2_mcspi_release_dma(struct spi_master *master)
10321032
}
10331033
}
10341034

1035+
static void omap2_mcspi_cleanup(struct spi_device *spi)
1036+
{
1037+
struct omap2_mcspi_cs *cs;
1038+
1039+
if (spi->controller_state) {
1040+
/* Unlink controller state from context save list */
1041+
cs = spi->controller_state;
1042+
list_del(&cs->node);
1043+
1044+
kfree(cs);
1045+
}
1046+
}
1047+
10351048
static int omap2_mcspi_setup(struct spi_device *spi)
10361049
{
1050+
bool initial_setup = false;
10371051
int ret;
10381052
struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
10391053
struct omap2_mcspi_regs *ctx = &mcspi->ctx;
@@ -1051,35 +1065,28 @@ static int omap2_mcspi_setup(struct spi_device *spi)
10511065
spi->controller_state = cs;
10521066
/* Link this to context save list */
10531067
list_add_tail(&cs->node, &ctx->cs);
1068+
initial_setup = true;
10541069
}
10551070

10561071
ret = pm_runtime_get_sync(mcspi->dev);
10571072
if (ret < 0) {
10581073
pm_runtime_put_noidle(mcspi->dev);
1074+
if (initial_setup)
1075+
omap2_mcspi_cleanup(spi);
10591076

10601077
return ret;
10611078
}
10621079

10631080
ret = omap2_mcspi_setup_transfer(spi, NULL);
1081+
if (ret && initial_setup)
1082+
omap2_mcspi_cleanup(spi);
1083+
10641084
pm_runtime_mark_last_busy(mcspi->dev);
10651085
pm_runtime_put_autosuspend(mcspi->dev);
10661086

10671087
return ret;
10681088
}
10691089

1070-
static void omap2_mcspi_cleanup(struct spi_device *spi)
1071-
{
1072-
struct omap2_mcspi_cs *cs;
1073-
1074-
if (spi->controller_state) {
1075-
/* Unlink controller state from context save list */
1076-
cs = spi->controller_state;
1077-
list_del(&cs->node);
1078-
1079-
kfree(cs);
1080-
}
1081-
}
1082-
10831090
static irqreturn_t omap2_mcspi_irq_handler(int irq, void *data)
10841091
{
10851092
struct omap2_mcspi *mcspi = data;

drivers/spi/spi-pxa2xx.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,8 @@ static int setup_cs(struct spi_device *spi, struct chip_data *chip,
12541254
chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
12551255

12561256
err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1257+
if (err)
1258+
gpiod_put(chip->gpiod_cs);
12571259
}
12581260

12591261
return err;
@@ -1267,6 +1269,7 @@ static int setup(struct spi_device *spi)
12671269
struct driver_data *drv_data =
12681270
spi_controller_get_devdata(spi->controller);
12691271
uint tx_thres, tx_hi_thres, rx_thres;
1272+
int err;
12701273

12711274
switch (drv_data->ssp_type) {
12721275
case QUARK_X1000_SSP:
@@ -1413,7 +1416,11 @@ static int setup(struct spi_device *spi)
14131416
if (drv_data->ssp_type == CE4100_SSP)
14141417
return 0;
14151418

1416-
return setup_cs(spi, chip, chip_info);
1419+
err = setup_cs(spi, chip, chip_info);
1420+
if (err)
1421+
kfree(chip);
1422+
1423+
return err;
14171424
}
14181425

14191426
static void cleanup(struct spi_device *spi)

drivers/spi/spi-stm32-qspi.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ static int stm32_qspi_wait_cmd(struct stm32_qspi *qspi,
294294
int err = 0;
295295

296296
if (!op->data.nbytes)
297-
return stm32_qspi_wait_nobusy(qspi);
297+
goto wait_nobusy;
298298

299299
if (readl_relaxed(qspi->io_base + QSPI_SR) & SR_TCF)
300300
goto out;
@@ -315,6 +315,9 @@ static int stm32_qspi_wait_cmd(struct stm32_qspi *qspi,
315315
out:
316316
/* clear flags */
317317
writel_relaxed(FCR_CTCF | FCR_CTEF, qspi->io_base + QSPI_FCR);
318+
wait_nobusy:
319+
if (!err)
320+
err = stm32_qspi_wait_nobusy(qspi);
318321

319322
return err;
320323
}

drivers/spi/spi-zynq-qspi.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -678,23 +678,24 @@ static int zynq_qspi_probe(struct platform_device *pdev)
678678
xqspi->irq = platform_get_irq(pdev, 0);
679679
if (xqspi->irq <= 0) {
680680
ret = -ENXIO;
681-
goto remove_master;
681+
goto clk_dis_all;
682682
}
683683
ret = devm_request_irq(&pdev->dev, xqspi->irq, zynq_qspi_irq,
684684
0, pdev->name, xqspi);
685685
if (ret != 0) {
686686
ret = -ENXIO;
687687
dev_err(&pdev->dev, "request_irq failed\n");
688-
goto remove_master;
688+
goto clk_dis_all;
689689
}
690690

691691
ret = of_property_read_u32(np, "num-cs",
692692
&num_cs);
693693
if (ret < 0) {
694694
ctlr->num_chipselect = 1;
695695
} else if (num_cs > ZYNQ_QSPI_MAX_NUM_CS) {
696+
ret = -EINVAL;
696697
dev_err(&pdev->dev, "only 2 chip selects are available\n");
697-
goto remove_master;
698+
goto clk_dis_all;
698699
} else {
699700
ctlr->num_chipselect = num_cs;
700701
}

0 commit comments

Comments
 (0)