History

spi: bugfix spi_add_device() with duplicate chipselects

When reviewing a recent patch I noticed a potential trouble spot in the registration of new SPI devices. The SPI master driver is told to set the device up before adding it to the driver model, so that it's always properly set up when probe() is called. (This is important, because in the case of inverted chipselects, this device can make the bus misbehave until it's properly deselected. It's got to be set up even if no driver binds to the device.) The trouble spot is that it doesn't first verify that no other device has been added using that chipselect. If such a device has been added, its configuration gets trashed. (Fortunately this has not been a common error!) The fix here adds an explicit check, and a mutex to protect the relevant critical region. [akpm@linux-foundation.org: make the lock local to spi_add_device()] 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>

David Brownell committed 18 years ago

Commit e48880e1 parent 7d455e01 file · +29 −11Browse files

Unified · Split

drivers/spi/spi.c+29−11
@@ -226,10 +226,11 @@ EXPORT_SYMBOL_GPL(spi_alloc_device); · 225 unmodified lines
226 * Companion function to spi_alloc_device. Devices allocated with226 * Companion function to spi_alloc_device. Devices allocated with
227 * spi_alloc_device can be added onto the spi bus with this function.227 * spi_alloc_device can be added onto the spi bus with this function.
228 *228 *
229 * Returns 0 on success; non-zero on failure229 * Returns 0 on success; negative errno on failure
230 */230 */
231int spi_add_device(struct spi_device *spi)231int spi_add_device(struct spi_device *spi)
232{232{
233 static DEFINE_MUTEX(spi_add_lock);
233 struct device *dev = spi->master->dev.parent;234 struct device *dev = spi->master->dev.parent;
234 int status;235 int status;
235 236
@@ -246,26 +247,43 @@ int spi_add_device(struct spi_device *spi) · 10 unmodified lines
246 "%s.%u", spi->master->dev.bus_id,247 "%s.%u", spi->master->dev.bus_id,
247 spi->chip_select);248 spi->chip_select);
248 249
249 /* drivers may modify this initial i/o setup */250
251 /* We need to make sure there's no other device with this
252 * chipselect **BEFORE** we call setup(), else we'll trash
253 * its configuration. Lock against concurrent add() calls.
254 */
255 mutex_lock(&spi_add_lock);
256
257 if (bus_find_device_by_name(&spi_bus_type, NULL, spi->dev.bus_id)
258 != NULL) {
259 dev_err(dev, "chipselect %d already in use\n",
260 spi->chip_select);
261 status = -EBUSY;
262 goto done;
263 }
264
265 /* Drivers may modify this initial i/o setup, but will
266 * normally rely on the device being setup. Devices
267 * using SPI_CS_HIGH can't coexist well otherwise...
268 */
250 status = spi->master->setup(spi);269 status = spi->master->setup(spi);
251 if (status < 0) {270 if (status < 0) {
252 dev_err(dev, "can't %s %s, status %d\n",271 dev_err(dev, "can't %s %s, status %d\n",
253 "setup", spi->dev.bus_id, status);272 "setup", spi->dev.bus_id, status);
254 return status;273 goto done;
255 }274 }
256 275
257 /* driver core catches callers that misbehave by defining276 /* Device may be bound to an active driver when this returns */
258 * devices that already exist.
259 */
260 status = device_add(&spi->dev);277 status = device_add(&spi->dev);
261 if (status < 0) {278 if (status < 0)
262 dev_err(dev, "can't %s %s, status %d\n",279 dev_err(dev, "can't %s %s, status %d\n",
263 "add", spi->dev.bus_id, status);280 "add", spi->dev.bus_id, status);
264 return status;281 else
265 }282 dev_dbg(dev, "registered child %s\n", spi->dev.bus_id);
266 283
267 dev_dbg(dev, "registered child %s\n", spi->dev.bus_id);284done:
268 return 0;285 mutex_unlock(&spi_add_lock);
286 return status;
269}287}
270EXPORT_SYMBOL_GPL(spi_add_device);288EXPORT_SYMBOL_GPL(spi_add_device);
271 289

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t