Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

SPI controller drivers: check for unsupported modes

Minor SPI controller driver updates: make the setup() methods reject
spi->mode bits they don't support, by masking aginst the inverse of bits
they *do* support. This insures against misbehavior later when new mode
bits get added.

Most controllers can't support SPI_LSB_FIRST; more handle SPI_CS_HIGH.
Support for all four SPI clock/transfer modes is routine.

Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

David Brownell and committed by
Linus Torvalds
dccd573b ff294cba

+66 -21
+1
drivers/spi/atmel_spi.c
··· 350 350 return ret; 351 351 } 352 352 353 + /* the spi->mode bits understood by this driver: */ 353 354 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH) 354 355 355 356 static int atmel_spi_setup(struct spi_device *spi)
+9
drivers/spi/au1550_spi.c
··· 280 280 return 0; 281 281 } 282 282 283 + /* the spi->mode bits understood by this driver: */ 284 + #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST) 285 + 283 286 static int au1550_spi_setup(struct spi_device *spi) 284 287 { 285 288 struct au1550_spi *hw = spi_master_get_devdata(spi->master); ··· 292 289 if (spi->bits_per_word < 4 || spi->bits_per_word > 24) { 293 290 dev_err(&spi->dev, "setup: invalid bits_per_word=%d\n", 294 291 spi->bits_per_word); 292 + return -EINVAL; 293 + } 294 + 295 + if (spi->mode & ~MODEBITS) { 296 + dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n", 297 + spi->mode & ~MODEBITS); 295 298 return -EINVAL; 296 299 } 297 300
+9
drivers/spi/mpc52xx_psc_spi.c
··· 270 270 spin_unlock_irq(&mps->lock); 271 271 } 272 272 273 + /* the spi->mode bits understood by this driver: */ 274 + #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST) 275 + 273 276 static int mpc52xx_psc_spi_setup(struct spi_device *spi) 274 277 { 275 278 struct mpc52xx_psc_spi *mps = spi_master_get_devdata(spi->master); ··· 281 278 282 279 if (spi->bits_per_word%8) 283 280 return -EINVAL; 281 + 282 + if (spi->mode & ~MODEBITS) { 283 + dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n", 284 + spi->mode & ~MODEBITS); 285 + return -EINVAL; 286 + } 284 287 285 288 if (!cs) { 286 289 cs = kzalloc(sizeof *cs, GFP_KERNEL);
+9
drivers/spi/omap_uwire.c
··· 445 445 return status; 446 446 } 447 447 448 + /* the spi->mode bits understood by this driver: */ 449 + #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH) 450 + 448 451 static int uwire_setup(struct spi_device *spi) 449 452 { 450 453 struct uwire_state *ust = spi->controller_state; 454 + 455 + if (spi->mode & ~MODEBITS) { 456 + dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n", 457 + spi->mode & ~MODEBITS); 458 + return -EINVAL; 459 + } 451 460 452 461 if (ust == NULL) { 453 462 ust = kzalloc(sizeof(*ust), GFP_KERNEL);
+9
drivers/spi/pxa2xx_spi.c
··· 1067 1067 return 0; 1068 1068 } 1069 1069 1070 + /* the spi->mode bits understood by this driver: */ 1071 + #define MODEBITS (SPI_CPOL | SPI_CPHA) 1072 + 1070 1073 static int setup(struct spi_device *spi) 1071 1074 { 1072 1075 struct pxa2xx_spi_chip *chip_info = NULL; ··· 1093 1090 dev_err(&spi->dev, "failed setup: ssp_type=%d, bits/wrd=%d " 1094 1091 "b/w not 4-16 for type PXA25x_SSP\n", 1095 1092 drv_data->ssp_type, spi->bits_per_word); 1093 + return -EINVAL; 1094 + } 1095 + 1096 + if (spi->mode & ~MODEBITS) { 1097 + dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n", 1098 + spi->mode & ~MODEBITS); 1096 1099 return -EINVAL; 1097 1100 } 1098 1101
+3 -5
drivers/spi/spi_bitbang.c
··· 187 187 188 188 bitbang = spi_master_get_devdata(spi->master); 189 189 190 - /* REVISIT: some systems will want to support devices using lsb-first 191 - * bit encodings on the wire. In pure software that would be trivial, 192 - * just bitbang_txrx_le_cphaX() routines shifting the other way, and 193 - * some hardware controllers also have this support. 190 + /* Bitbangers can support SPI_CS_HIGH, SPI_3WIRE, and so on; 191 + * add those to master->flags, and provide the other support. 194 192 */ 195 - if ((spi->mode & SPI_LSB_FIRST) != 0) 193 + if ((spi->mode & ~(SPI_CPOL|SPI_CPHA|bitbang->flags)) != 0) 196 194 return -EINVAL; 197 195 198 196 if (!cs) {
+9 -15
drivers/spi/spi_imx.c
··· 1163 1163 return -EINVAL; 1164 1164 } 1165 1165 1166 + /* the spi->mode bits understood by this driver: */ 1167 + #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH) 1168 + 1166 1169 /* On first setup bad values must free chip_data memory since will cause 1167 1170 spi_new_device to fail. Bad value setup from protocol driver are simply not 1168 1171 applied and notified to the calling driver. */ ··· 1176 1173 int first_setup = 0; 1177 1174 u32 tmp; 1178 1175 int status = 0; 1176 + 1177 + if (spi->mode & ~MODEBITS) { 1178 + dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n", 1179 + spi->mode & ~MODEBITS); 1180 + return -EINVAL; 1181 + } 1179 1182 1180 1183 /* Get controller data */ 1181 1184 chip_info = spi->controller_data; ··· 1254 1245 1255 1246 /* SPI mode */ 1256 1247 tmp = spi->mode; 1257 - if (tmp & SPI_LSB_FIRST) { 1258 - status = -EINVAL; 1259 - if (first_setup) { 1260 - dev_err(&spi->dev, 1261 - "setup - " 1262 - "HW doesn't support LSB first transfer\n"); 1263 - goto err_first_setup; 1264 - } else { 1265 - dev_err(&spi->dev, 1266 - "setup - " 1267 - "HW doesn't support LSB first transfer, " 1268 - "default to MSB first\n"); 1269 - spi->mode &= ~SPI_LSB_FIRST; 1270 - } 1271 - } 1272 1248 if (tmp & SPI_CS_HIGH) { 1273 1249 u32_EDIT(chip->control, 1274 1250 SPI_CONTROL_SSPOL, SPI_CONTROL_SSPOL_ACT_HIGH);
+9
drivers/spi/spi_mpc83xx.c
··· 232 232 return 0; 233 233 } 234 234 235 + /* the spi->mode bits understood by this driver: */ 236 + #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH) 237 + 235 238 static int mpc83xx_spi_setup(struct spi_device *spi) 236 239 { 237 240 struct spi_bitbang *bitbang; 238 241 struct mpc83xx_spi *mpc83xx_spi; 239 242 int retval; 243 + 244 + if (spi->mode & ~MODEBITS) { 245 + dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n", 246 + spi->mode & ~MODEBITS); 247 + return -EINVAL; 248 + } 240 249 241 250 if (!spi->max_speed_hz) 242 251 return -EINVAL;
+7 -1
drivers/spi/spi_s3c24xx.c
··· 146 146 return 0; 147 147 } 148 148 149 + /* the spi->mode bits understood by this driver: */ 150 + #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH) 151 + 149 152 static int s3c24xx_spi_setup(struct spi_device *spi) 150 153 { 151 154 int ret; ··· 156 153 if (!spi->bits_per_word) 157 154 spi->bits_per_word = 8; 158 155 159 - if ((spi->mode & SPI_LSB_FIRST) != 0) 156 + if (spi->mode & ~MODEBITS) { 157 + dev_dbg(&spi->dev, "setup: unsupported mode bits %x\n", 158 + spi->mode & ~MODEBITS); 160 159 return -EINVAL; 160 + } 161 161 162 162 ret = s3c24xx_spi_setupxfer(spi, NULL); 163 163 if (ret < 0) {
+1
include/linux/spi/spi_bitbang.h
··· 26 26 struct list_head queue; 27 27 u8 busy; 28 28 u8 use_dma; 29 + u8 flags; /* extra spi->mode support */ 29 30 30 31 struct spi_master *master; 31 32