[PATCH] SPI core tweaks, bugfix

This includes various updates to the SPI core:

- Fixes a driver model refcount bug in spi_unregister_master() paths.

- The spi_master structures now have wrappers which help keep drivers
from needing class-level get/put for device data or for refcounts.

- Check for a few setup errors that would cause oopsing later.

- Docs say more about memory management. Highlights the use of DMA-safe
i/o buffers, and zero-initializing spi_message and such metadata.

- Provide a simple alloc/free for spi_message and its spi_transfer;
this is only one of the possible memory management policies.

Nothing to break code that already works.

Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by David Brownell and committed by Greg Kroah-Hartman 0c868461 b885244e

+113 -23
+16
Documentation/spi/spi-summary
··· 363 363 the input layer, ALSA, networking, MTD, the character device framework, 364 364 or other Linux subsystems. 365 365 366 + Note that there are two types of memory your driver must manage as part 367 + of interacting with SPI devices. 368 + 369 + - I/O buffers use the usual Linux rules, and must be DMA-safe. 370 + You'd normally allocate them from the heap or free page pool. 371 + Don't use the stack, or anything that's declared "static". 372 + 373 + - The spi_message and spi_transfer metadata used to glue those 374 + I/O buffers into a group of protocol transactions. These can 375 + be allocated anywhere it's convenient, including as part of 376 + other allocate-once driver data structures. Zero-init these. 377 + 378 + If you like, spi_message_alloc() and spi_message_free() convenience 379 + routines are available to allocate and zero-initialize an spi_message 380 + with several transfers. 381 + 366 382 367 383 How do I write an "SPI Master Controller Driver"? 368 384 -------------------------------------------------
+28 -17
drivers/spi/spi.c
··· 38 38 if (spi->master->cleanup) 39 39 spi->master->cleanup(spi); 40 40 41 - class_device_put(&spi->master->cdev); 41 + spi_master_put(spi->master); 42 42 kfree(dev); 43 43 } 44 44 ··· 90 90 int value; 91 91 struct spi_driver *drv = to_spi_driver(dev->driver); 92 92 93 - if (!drv || !drv->suspend) 93 + if (!drv->suspend) 94 94 return 0; 95 95 96 96 /* suspend will stop irqs and dma; no more i/o */ ··· 105 105 int value; 106 106 struct spi_driver *drv = to_spi_driver(dev->driver); 107 107 108 - if (!drv || !drv->resume) 108 + if (!drv->resume) 109 109 return 0; 110 110 111 111 /* resume may restart the i/o queue */ ··· 198 198 199 199 /* NOTE: caller did any chip->bus_num checks necessary */ 200 200 201 - if (!class_device_get(&master->cdev)) 201 + if (!spi_master_get(master)) 202 202 return NULL; 203 203 204 204 proxy = kzalloc(sizeof *proxy, GFP_KERNEL); ··· 244 244 return proxy; 245 245 246 246 fail: 247 - class_device_put(&master->cdev); 247 + spi_master_put(master); 248 248 kfree(proxy); 249 249 return NULL; 250 250 } ··· 324 324 struct spi_master *master; 325 325 326 326 master = container_of(cdev, struct spi_master, cdev); 327 - put_device(master->cdev.dev); 328 - master->cdev.dev = NULL; 329 327 kfree(master); 330 328 } 331 329 ··· 337 339 /** 338 340 * spi_alloc_master - allocate SPI master controller 339 341 * @dev: the controller, possibly using the platform_bus 340 - * @size: how much driver-private data to preallocate; a pointer to this 341 - * memory in the class_data field of the returned class_device 342 + * @size: how much driver-private data to preallocate; the pointer to this 343 + * memory is in the class_data field of the returned class_device, 344 + * accessible with spi_master_get_devdata(). 342 345 * 343 346 * This call is used only by SPI master controller drivers, which are the 344 347 * only ones directly touching chip registers. It's how they allocate ··· 349 350 * master structure on success, else NULL. 350 351 * 351 352 * The caller is responsible for assigning the bus number and initializing 352 - * the master's methods before calling spi_add_master(), or else (on error) 353 - * calling class_device_put() to prevent a memory leak. 353 + * the master's methods before calling spi_add_master(); and (after errors 354 + * adding the device) calling spi_master_put() to prevent a memory leak. 354 355 */ 355 356 struct spi_master * __init_or_module 356 357 spi_alloc_master(struct device *dev, unsigned size) 357 358 { 358 359 struct spi_master *master; 360 + 361 + if (!dev) 362 + return NULL; 359 363 360 364 master = kzalloc(size + sizeof *master, SLAB_KERNEL); 361 365 if (!master) ··· 367 365 class_device_initialize(&master->cdev); 368 366 master->cdev.class = &spi_master_class; 369 367 master->cdev.dev = get_device(dev); 370 - class_set_devdata(&master->cdev, &master[1]); 368 + spi_master_set_devdata(master, &master[1]); 371 369 372 370 return master; 373 371 } ··· 389 387 * 390 388 * This must be called from context that can sleep. It returns zero on 391 389 * success, else a negative error code (dropping the master's refcount). 390 + * After a successful return, the caller is responsible for calling 391 + * spi_unregister_master(). 392 392 */ 393 393 int __init_or_module 394 394 spi_register_master(struct spi_master *master) ··· 399 395 struct device *dev = master->cdev.dev; 400 396 int status = -ENODEV; 401 397 int dynamic = 0; 398 + 399 + if (!dev) 400 + return -ENODEV; 402 401 403 402 /* convention: dynamically assigned bus IDs count down from the max */ 404 403 if (master->bus_num == 0) { ··· 432 425 static int __unregister(struct device *dev, void *unused) 433 426 { 434 427 /* note: before about 2.6.14-rc1 this would corrupt memory: */ 435 - device_unregister(dev); 428 + spi_unregister_device(to_spi_device(dev)); 436 429 return 0; 437 430 } 438 431 ··· 447 440 */ 448 441 void spi_unregister_master(struct spi_master *master) 449 442 { 450 - class_device_unregister(&master->cdev); 451 443 (void) device_for_each_child(master->cdev.dev, NULL, __unregister); 444 + class_device_unregister(&master->cdev); 445 + master->cdev.dev = NULL; 452 446 } 453 447 EXPORT_SYMBOL_GPL(spi_unregister_master); 454 448 ··· 495 487 * by leaving it selected in anticipation that the next message will go 496 488 * to the same chip. (That may increase power usage.) 497 489 * 490 + * Also, the caller is guaranteeing that the memory associated with the 491 + * message will not be freed before this call returns. 492 + * 498 493 * The return value is a negative error code if the message could not be 499 494 * submitted, else zero. When the value is zero, then message->status is 500 495 * also defined: it's the completion code for the transfer, either zero ··· 535 524 * is zero for success, else a negative errno status code. 536 525 * This call may only be used from a context that may sleep. 537 526 * 538 - * Parameters to this routine are always copied using a small buffer, 539 - * large transfers should use use spi_{async,sync}() calls with 540 - * dma-safe buffers. 527 + * Parameters to this routine are always copied using a small buffer; 528 + * performance-sensitive or bulk transfer code should instead use 529 + * spi_{async,sync}() calls with dma-safe buffers. 541 530 */ 542 531 int spi_write_then_read(struct spi_device *spi, 543 532 const u8 *txbuf, unsigned n_tx,
+69 -6
include/linux/spi/spi.h
··· 60 60 u8 mode; 61 61 #define SPI_CPHA 0x01 /* clock phase */ 62 62 #define SPI_CPOL 0x02 /* clock polarity */ 63 - #define SPI_MODE_0 (0|0) 64 - #define SPI_MODE_1 (0|SPI_CPHA) /* (original MicroWire) */ 63 + #define SPI_MODE_0 (0|0) /* (original MicroWire) */ 64 + #define SPI_MODE_1 (0|SPI_CPHA) 65 65 #define SPI_MODE_2 (SPI_CPOL|0) 66 66 #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 67 67 #define SPI_CS_HIGH 0x04 /* chipselect active high? */ ··· 209 209 void (*cleanup)(const struct spi_device *spi); 210 210 }; 211 211 212 + static inline void *spi_master_get_devdata(struct spi_master *master) 213 + { 214 + return class_get_devdata(&master->cdev); 215 + } 216 + 217 + static inline void spi_master_set_devdata(struct spi_master *master, void *data) 218 + { 219 + class_set_devdata(&master->cdev, data); 220 + } 221 + 222 + static inline struct spi_master *spi_master_get(struct spi_master *master) 223 + { 224 + if (!master || !class_device_get(&master->cdev)) 225 + return NULL; 226 + return master; 227 + } 228 + 229 + static inline void spi_master_put(struct spi_master *master) 230 + { 231 + if (master) 232 + class_device_put(&master->cdev); 233 + } 234 + 235 + 212 236 /* the spi driver core manages memory for the spi_master classdev */ 213 237 extern struct spi_master * 214 238 spi_alloc_master(struct device *host, unsigned size); ··· 295 271 * stay selected until the next transfer. This is purely a performance 296 272 * hint; the controller driver may need to select a different device 297 273 * for the next message. 274 + * 275 + * The code that submits an spi_message (and its spi_transfers) 276 + * to the lower layers is responsible for managing its memory. 277 + * Zero-initialize every field you don't set up explicitly, to 278 + * insulate against future API updates. 298 279 */ 299 280 struct spi_transfer { 300 281 /* it's ok if tx_buf == rx_buf (right?) 301 282 * for MicroWire, one buffer must be null 302 - * buffers must work with dma_*map_single() calls 283 + * buffers must work with dma_*map_single() calls, unless 284 + * spi_message.is_dma_mapped reports a pre-existing mapping 303 285 */ 304 286 const void *tx_buf; 305 287 void *rx_buf; ··· 332 302 * @status: zero for success, else negative errno 333 303 * @queue: for use by whichever driver currently owns the message 334 304 * @state: for use by whichever driver currently owns the message 305 + * 306 + * The code that submits an spi_message (and its spi_transfers) 307 + * to the lower layers is responsible for managing its memory. 308 + * Zero-initialize every field you don't set up explicitly, to 309 + * insulate against future API updates. 335 310 */ 336 311 struct spi_message { 337 312 struct spi_transfer *transfers; ··· 371 336 void *state; 372 337 }; 373 338 339 + /* It's fine to embed message and transaction structures in other data 340 + * structures so long as you don't free them while they're in use. 341 + */ 342 + 343 + static inline struct spi_message *spi_message_alloc(unsigned ntrans, gfp_t flags) 344 + { 345 + struct spi_message *m; 346 + 347 + m = kzalloc(sizeof(struct spi_message) 348 + + ntrans * sizeof(struct spi_transfer), 349 + flags); 350 + if (m) { 351 + m->transfers = (void *)(m + 1); 352 + m->n_transfer = ntrans; 353 + } 354 + return m; 355 + } 356 + 357 + static inline void spi_message_free(struct spi_message *m) 358 + { 359 + kfree(m); 360 + } 361 + 374 362 /** 375 363 * spi_setup -- setup SPI mode and clock rate 376 364 * @spi: the device whose settings are being modified ··· 421 363 * The completion callback is invoked in a context which can't sleep. 422 364 * Before that invocation, the value of message->status is undefined. 423 365 * When the callback is issued, message->status holds either zero (to 424 - * indicate complete success) or a negative error code. 366 + * indicate complete success) or a negative error code. After that 367 + * callback returns, the driver which issued the transfer request may 368 + * deallocate the associated memory; it's no longer in use by any SPI 369 + * core or controller driver code. 425 370 * 426 371 * Note that although all messages to a spi_device are handled in 427 372 * FIFO order, messages may go to different devices in other orders. ··· 506 445 return spi_sync(spi, &m); 507 446 } 508 447 448 + /* this copies txbuf and rxbuf data; for small transfers only! */ 509 449 extern int spi_write_then_read(struct spi_device *spi, 510 450 const u8 *txbuf, unsigned n_tx, 511 451 u8 *rxbuf, unsigned n_rx); ··· 617 555 618 556 619 557 /* If you're hotplugging an adapter with devices (parport, usb, etc) 620 - * use spi_new_device() to describe each device. You would then call 621 - * spi_unregister_device() to start making that device vanish. 558 + * use spi_new_device() to describe each device. You can also call 559 + * spi_unregister_device() to start making that device vanish, but 560 + * normally that would be handled by spi_unregister_master(). 622 561 */ 623 562 extern struct spi_device * 624 563 spi_new_device(struct spi_master *, struct spi_board_info *);