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
226226 * Companion function to spi_alloc_device. Devices allocated with
227227 * spi_alloc_device can be added onto the spi bus with this function.
228228 *
229− * Returns 0 on success; non-zero on failure
229+ * Returns 0 on success; negative errno on failure
230230 */
231231 int spi_add_device(struct spi_device *spi)
232232 {
233+ static DEFINE_MUTEX(spi_add_lock);
233234 struct device *dev = spi->master->dev.parent;
234235 int status;
235236
@@ -246,26 +247,43 @@ int spi_add_device(struct spi_device *spi) · 10 unmodified lines
246247 "%s.%u", spi->master->dev.bus_id,
247248 spi->chip_select);
248249
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+ */
250269 status = spi->master->setup(spi);
251270 if (status < 0) {
252271 dev_err(dev, "can't %s %s, status %d\n",
253272 "setup", spi->dev.bus_id, status);
254− return status;
273+ goto done;
255274 }
256275
257− /* driver core catches callers that misbehave by defining
258− * devices that already exist.
259− */
276+ /* Device may be bound to an active driver when this returns */
260277 status = device_add(&spi->dev);
261− if (status < 0) {
278+ if (status < 0)
262279 dev_err(dev, "can't %s %s, status %d\n",
263280 "add", spi->dev.bus_id, status);
264− return status;
265− }
281+ else
282+ dev_dbg(dev, "registered child %s\n", spi->dev.bus_id);
266283
267− dev_dbg(dev, "registered child %s\n", spi->dev.bus_id);
268− return 0;
284+done:
285+ mutex_unlock(&spi_add_lock);
286+ return status;
269287 }
270288 EXPORT_SYMBOL_GPL(spi_add_device);
271289

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