[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 the input layer, ALSA, networking, MTD, the character device framework, 364 or other Linux subsystems. 365 366 367 How do I write an "SPI Master Controller Driver"? 368 -------------------------------------------------
··· 363 the input layer, ALSA, networking, MTD, the character device framework, 364 or other Linux subsystems. 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 + 382 383 How do I write an "SPI Master Controller Driver"? 384 -------------------------------------------------
+28 -17
drivers/spi/spi.c
··· 38 if (spi->master->cleanup) 39 spi->master->cleanup(spi); 40 41 - class_device_put(&spi->master->cdev); 42 kfree(dev); 43 } 44 ··· 90 int value; 91 struct spi_driver *drv = to_spi_driver(dev->driver); 92 93 - if (!drv || !drv->suspend) 94 return 0; 95 96 /* suspend will stop irqs and dma; no more i/o */ ··· 105 int value; 106 struct spi_driver *drv = to_spi_driver(dev->driver); 107 108 - if (!drv || !drv->resume) 109 return 0; 110 111 /* resume may restart the i/o queue */ ··· 198 199 /* NOTE: caller did any chip->bus_num checks necessary */ 200 201 - if (!class_device_get(&master->cdev)) 202 return NULL; 203 204 proxy = kzalloc(sizeof *proxy, GFP_KERNEL); ··· 244 return proxy; 245 246 fail: 247 - class_device_put(&master->cdev); 248 kfree(proxy); 249 return NULL; 250 } ··· 324 struct spi_master *master; 325 326 master = container_of(cdev, struct spi_master, cdev); 327 - put_device(master->cdev.dev); 328 - master->cdev.dev = NULL; 329 kfree(master); 330 } 331 ··· 337 /** 338 * spi_alloc_master - allocate SPI master controller 339 * @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 * 343 * This call is used only by SPI master controller drivers, which are the 344 * only ones directly touching chip registers. It's how they allocate ··· 349 * master structure on success, else NULL. 350 * 351 * 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. 354 */ 355 struct spi_master * __init_or_module 356 spi_alloc_master(struct device *dev, unsigned size) 357 { 358 struct spi_master *master; 359 360 master = kzalloc(size + sizeof *master, SLAB_KERNEL); 361 if (!master) ··· 367 class_device_initialize(&master->cdev); 368 master->cdev.class = &spi_master_class; 369 master->cdev.dev = get_device(dev); 370 - class_set_devdata(&master->cdev, &master[1]); 371 372 return master; 373 } ··· 389 * 390 * This must be called from context that can sleep. It returns zero on 391 * success, else a negative error code (dropping the master's refcount). 392 */ 393 int __init_or_module 394 spi_register_master(struct spi_master *master) ··· 399 struct device *dev = master->cdev.dev; 400 int status = -ENODEV; 401 int dynamic = 0; 402 403 /* convention: dynamically assigned bus IDs count down from the max */ 404 if (master->bus_num == 0) { ··· 432 static int __unregister(struct device *dev, void *unused) 433 { 434 /* note: before about 2.6.14-rc1 this would corrupt memory: */ 435 - device_unregister(dev); 436 return 0; 437 } 438 ··· 447 */ 448 void spi_unregister_master(struct spi_master *master) 449 { 450 - class_device_unregister(&master->cdev); 451 (void) device_for_each_child(master->cdev.dev, NULL, __unregister); 452 } 453 EXPORT_SYMBOL_GPL(spi_unregister_master); 454 ··· 495 * by leaving it selected in anticipation that the next message will go 496 * to the same chip. (That may increase power usage.) 497 * 498 * The return value is a negative error code if the message could not be 499 * submitted, else zero. When the value is zero, then message->status is 500 * also defined: it's the completion code for the transfer, either zero ··· 535 * is zero for success, else a negative errno status code. 536 * This call may only be used from a context that may sleep. 537 * 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. 541 */ 542 int spi_write_then_read(struct spi_device *spi, 543 const u8 *txbuf, unsigned n_tx,
··· 38 if (spi->master->cleanup) 39 spi->master->cleanup(spi); 40 41 + spi_master_put(spi->master); 42 kfree(dev); 43 } 44 ··· 90 int value; 91 struct spi_driver *drv = to_spi_driver(dev->driver); 92 93 + if (!drv->suspend) 94 return 0; 95 96 /* suspend will stop irqs and dma; no more i/o */ ··· 105 int value; 106 struct spi_driver *drv = to_spi_driver(dev->driver); 107 108 + if (!drv->resume) 109 return 0; 110 111 /* resume may restart the i/o queue */ ··· 198 199 /* NOTE: caller did any chip->bus_num checks necessary */ 200 201 + if (!spi_master_get(master)) 202 return NULL; 203 204 proxy = kzalloc(sizeof *proxy, GFP_KERNEL); ··· 244 return proxy; 245 246 fail: 247 + spi_master_put(master); 248 kfree(proxy); 249 return NULL; 250 } ··· 324 struct spi_master *master; 325 326 master = container_of(cdev, struct spi_master, cdev); 327 kfree(master); 328 } 329 ··· 339 /** 340 * spi_alloc_master - allocate SPI master controller 341 * @dev: the controller, possibly using the platform_bus 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(). 345 * 346 * This call is used only by SPI master controller drivers, which are the 347 * only ones directly touching chip registers. It's how they allocate ··· 350 * master structure on success, else NULL. 351 * 352 * The caller is responsible for assigning the bus number and initializing 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. 355 */ 356 struct spi_master * __init_or_module 357 spi_alloc_master(struct device *dev, unsigned size) 358 { 359 struct spi_master *master; 360 + 361 + if (!dev) 362 + return NULL; 363 364 master = kzalloc(size + sizeof *master, SLAB_KERNEL); 365 if (!master) ··· 365 class_device_initialize(&master->cdev); 366 master->cdev.class = &spi_master_class; 367 master->cdev.dev = get_device(dev); 368 + spi_master_set_devdata(master, &master[1]); 369 370 return master; 371 } ··· 387 * 388 * This must be called from context that can sleep. It returns zero on 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 */ 393 int __init_or_module 394 spi_register_master(struct spi_master *master) ··· 395 struct device *dev = master->cdev.dev; 396 int status = -ENODEV; 397 int dynamic = 0; 398 + 399 + if (!dev) 400 + return -ENODEV; 401 402 /* convention: dynamically assigned bus IDs count down from the max */ 403 if (master->bus_num == 0) { ··· 425 static int __unregister(struct device *dev, void *unused) 426 { 427 /* note: before about 2.6.14-rc1 this would corrupt memory: */ 428 + spi_unregister_device(to_spi_device(dev)); 429 return 0; 430 } 431 ··· 440 */ 441 void spi_unregister_master(struct spi_master *master) 442 { 443 (void) device_for_each_child(master->cdev.dev, NULL, __unregister); 444 + class_device_unregister(&master->cdev); 445 + master->cdev.dev = NULL; 446 } 447 EXPORT_SYMBOL_GPL(spi_unregister_master); 448 ··· 487 * by leaving it selected in anticipation that the next message will go 488 * to the same chip. (That may increase power usage.) 489 * 490 + * Also, the caller is guaranteeing that the memory associated with the 491 + * message will not be freed before this call returns. 492 + * 493 * The return value is a negative error code if the message could not be 494 * submitted, else zero. When the value is zero, then message->status is 495 * also defined: it's the completion code for the transfer, either zero ··· 524 * is zero for success, else a negative errno status code. 525 * This call may only be used from a context that may sleep. 526 * 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. 530 */ 531 int spi_write_then_read(struct spi_device *spi, 532 const u8 *txbuf, unsigned n_tx,
+69 -6
include/linux/spi/spi.h
··· 60 u8 mode; 61 #define SPI_CPHA 0x01 /* clock phase */ 62 #define SPI_CPOL 0x02 /* clock polarity */ 63 - #define SPI_MODE_0 (0|0) 64 - #define SPI_MODE_1 (0|SPI_CPHA) /* (original MicroWire) */ 65 #define SPI_MODE_2 (SPI_CPOL|0) 66 #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 67 #define SPI_CS_HIGH 0x04 /* chipselect active high? */ ··· 209 void (*cleanup)(const struct spi_device *spi); 210 }; 211 212 /* the spi driver core manages memory for the spi_master classdev */ 213 extern struct spi_master * 214 spi_alloc_master(struct device *host, unsigned size); ··· 295 * stay selected until the next transfer. This is purely a performance 296 * hint; the controller driver may need to select a different device 297 * for the next message. 298 */ 299 struct spi_transfer { 300 /* it's ok if tx_buf == rx_buf (right?) 301 * for MicroWire, one buffer must be null 302 - * buffers must work with dma_*map_single() calls 303 */ 304 const void *tx_buf; 305 void *rx_buf; ··· 332 * @status: zero for success, else negative errno 333 * @queue: for use by whichever driver currently owns the message 334 * @state: for use by whichever driver currently owns the message 335 */ 336 struct spi_message { 337 struct spi_transfer *transfers; ··· 371 void *state; 372 }; 373 374 /** 375 * spi_setup -- setup SPI mode and clock rate 376 * @spi: the device whose settings are being modified ··· 421 * The completion callback is invoked in a context which can't sleep. 422 * Before that invocation, the value of message->status is undefined. 423 * When the callback is issued, message->status holds either zero (to 424 - * indicate complete success) or a negative error code. 425 * 426 * Note that although all messages to a spi_device are handled in 427 * FIFO order, messages may go to different devices in other orders. ··· 506 return spi_sync(spi, &m); 507 } 508 509 extern int spi_write_then_read(struct spi_device *spi, 510 const u8 *txbuf, unsigned n_tx, 511 u8 *rxbuf, unsigned n_rx); ··· 617 618 619 /* 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. 622 */ 623 extern struct spi_device * 624 spi_new_device(struct spi_master *, struct spi_board_info *);
··· 60 u8 mode; 61 #define SPI_CPHA 0x01 /* clock phase */ 62 #define SPI_CPOL 0x02 /* clock polarity */ 63 + #define SPI_MODE_0 (0|0) /* (original MicroWire) */ 64 + #define SPI_MODE_1 (0|SPI_CPHA) 65 #define SPI_MODE_2 (SPI_CPOL|0) 66 #define SPI_MODE_3 (SPI_CPOL|SPI_CPHA) 67 #define SPI_CS_HIGH 0x04 /* chipselect active high? */ ··· 209 void (*cleanup)(const struct spi_device *spi); 210 }; 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 + 236 /* the spi driver core manages memory for the spi_master classdev */ 237 extern struct spi_master * 238 spi_alloc_master(struct device *host, unsigned size); ··· 271 * stay selected until the next transfer. This is purely a performance 272 * hint; the controller driver may need to select a different device 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. 279 */ 280 struct spi_transfer { 281 /* it's ok if tx_buf == rx_buf (right?) 282 * for MicroWire, one buffer must be null 283 + * buffers must work with dma_*map_single() calls, unless 284 + * spi_message.is_dma_mapped reports a pre-existing mapping 285 */ 286 const void *tx_buf; 287 void *rx_buf; ··· 302 * @status: zero for success, else negative errno 303 * @queue: for use by whichever driver currently owns the message 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. 310 */ 311 struct spi_message { 312 struct spi_transfer *transfers; ··· 336 void *state; 337 }; 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 + 362 /** 363 * spi_setup -- setup SPI mode and clock rate 364 * @spi: the device whose settings are being modified ··· 363 * The completion callback is invoked in a context which can't sleep. 364 * Before that invocation, the value of message->status is undefined. 365 * When the callback is issued, message->status holds either zero (to 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. 370 * 371 * Note that although all messages to a spi_device are handled in 372 * FIFO order, messages may go to different devices in other orders. ··· 445 return spi_sync(spi, &m); 446 } 447 448 + /* this copies txbuf and rxbuf data; for small transfers only! */ 449 extern int spi_write_then_read(struct spi_device *spi, 450 const u8 *txbuf, unsigned n_tx, 451 u8 *rxbuf, unsigned n_rx); ··· 555 556 557 /* If you're hotplugging an adapter with devices (parport, usb, etc) 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(). 561 */ 562 extern struct spi_device * 563 spi_new_device(struct spi_master *, struct spi_board_info *);