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

iio: accel: mma9551_core: use size in words for word buffers

Change the prototype for the mma9551_read/write_*_words functions
to receive the length of the buffer in words (instead of bytes) since
we are using a word buffer. This will prevent users from sending an
odd number of bytes for a word array.

Signed-off-by: Irina Tirdea <irina.tirdea@intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Irina Tirdea and committed by
Jonathan Cameron
c0d901cc 1ca0259b

+18 -18
+12 -15
drivers/iio/accel/mma9551_core.c
··· 373 373 * @client: I2C client 374 374 * @app_id: Application ID 375 375 * @reg: Application register 376 - * @len: Length of array to read in bytes 376 + * @len: Length of array to read (in words) 377 377 * @buf: Array of words to read 378 378 * 379 379 * Read multiple configuration registers (word-sized registers). ··· 388 388 u16 reg, u8 len, u16 *buf) 389 389 { 390 390 int ret, i; 391 - int len_words = len / sizeof(u16); 392 391 __be16 be_buf[MMA9551_MAX_MAILBOX_DATA_REGS / 2]; 393 392 394 - if (len_words > ARRAY_SIZE(be_buf)) { 393 + if (len > ARRAY_SIZE(be_buf)) { 395 394 dev_err(&client->dev, "Invalid buffer size %d\n", len); 396 395 return -EINVAL; 397 396 } 398 397 399 398 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_CONFIG, 400 - reg, NULL, 0, (u8 *) be_buf, len); 399 + reg, NULL, 0, (u8 *)be_buf, len * sizeof(u16)); 401 400 if (ret < 0) 402 401 return ret; 403 402 404 - for (i = 0; i < len_words; i++) 403 + for (i = 0; i < len; i++) 405 404 buf[i] = be16_to_cpu(be_buf[i]); 406 405 407 406 return 0; ··· 412 413 * @client: I2C client 413 414 * @app_id: Application ID 414 415 * @reg: Application register 415 - * @len: Length of array to read in bytes 416 + * @len: Length of array to read (in words) 416 417 * @buf: Array of words to read 417 418 * 418 419 * Read multiple status registers (word-sized registers). ··· 427 428 u16 reg, u8 len, u16 *buf) 428 429 { 429 430 int ret, i; 430 - int len_words = len / sizeof(u16); 431 431 __be16 be_buf[MMA9551_MAX_MAILBOX_DATA_REGS / 2]; 432 432 433 - if (len_words > ARRAY_SIZE(be_buf)) { 433 + if (len > ARRAY_SIZE(be_buf)) { 434 434 dev_err(&client->dev, "Invalid buffer size %d\n", len); 435 435 return -EINVAL; 436 436 } 437 437 438 438 ret = mma9551_transfer(client, app_id, MMA9551_CMD_READ_STATUS, 439 - reg, NULL, 0, (u8 *) be_buf, len); 439 + reg, NULL, 0, (u8 *)be_buf, len * sizeof(u16)); 440 440 if (ret < 0) 441 441 return ret; 442 442 443 - for (i = 0; i < len_words; i++) 443 + for (i = 0; i < len; i++) 444 444 buf[i] = be16_to_cpu(be_buf[i]); 445 445 446 446 return 0; ··· 451 453 * @client: I2C client 452 454 * @app_id: Application ID 453 455 * @reg: Application register 454 - * @len: Length of array to write in bytes 456 + * @len: Length of array to write (in words) 455 457 * @buf: Array of words to write 456 458 * 457 459 * Write multiple configuration registers (word-sized registers). ··· 466 468 u16 reg, u8 len, u16 *buf) 467 469 { 468 470 int i; 469 - int len_words = len / sizeof(u16); 470 471 __be16 be_buf[(MMA9551_MAX_MAILBOX_DATA_REGS - 1) / 2]; 471 472 472 - if (len_words > ARRAY_SIZE(be_buf)) { 473 + if (len > ARRAY_SIZE(be_buf)) { 473 474 dev_err(&client->dev, "Invalid buffer size %d\n", len); 474 475 return -EINVAL; 475 476 } 476 477 477 - for (i = 0; i < len_words; i++) 478 + for (i = 0; i < len; i++) 478 479 be_buf[i] = cpu_to_be16(buf[i]); 479 480 480 481 return mma9551_transfer(client, app_id, MMA9551_CMD_WRITE_CONFIG, 481 - reg, (u8 *) be_buf, len, NULL, 0); 482 + reg, (u8 *)be_buf, len * sizeof(u16), NULL, 0); 482 483 } 483 484 EXPORT_SYMBOL(mma9551_write_config_words); 484 485
+6 -3
drivers/iio/accel/mma9553.c
··· 322 322 int ret; 323 323 324 324 ret = mma9551_read_status_words(data->client, MMA9551_APPID_PEDOMETER, 325 - MMA9553_REG_STATUS, sizeof(u32), buf); 325 + MMA9553_REG_STATUS, ARRAY_SIZE(buf), 326 + buf); 326 327 if (ret < 0) { 327 328 dev_err(&data->client->dev, 328 329 "error reading status and stepcnt\n"); ··· 398 397 ret = 399 398 mma9551_read_config_words(data->client, MMA9551_APPID_PEDOMETER, 400 399 MMA9553_REG_CONF_SLEEPMIN, 401 - sizeof(data->conf), (u16 *) &data->conf); 400 + sizeof(data->conf) / sizeof(u16), 401 + (u16 *)&data->conf); 402 402 if (ret < 0) { 403 403 dev_err(&data->client->dev, 404 404 "failed to read configuration registers\n"); ··· 432 430 ret = 433 431 mma9551_write_config_words(data->client, MMA9551_APPID_PEDOMETER, 434 432 MMA9553_REG_CONF_SLEEPMIN, 435 - sizeof(data->conf), (u16 *) &data->conf); 433 + sizeof(data->conf) / sizeof(u16), 434 + (u16 *)&data->conf); 436 435 if (ret < 0) { 437 436 dev_err(&data->client->dev, 438 437 "failed to write configuration registers\n");