diff options
author | Jaghathiswari Rankappagounder Natarajan <jaghu@google.com> | 2017-08-30 19:34:34 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2017-08-31 12:50:14 -0400 |
commit | b78165f028713121609dae40f10246da6293a281 (patch) | |
tree | b4323705fccb967873d6018b6f3c05c622309eba | |
parent | 2eb7954809bf26de27bc3a2fea4eef606bbf4482 (diff) |
drivers: w1: refactor w1_slave_show to make the temp reading functionality separate
Inside the w1_slave_show function refactor the code to read the temp
into a separate function.
Signed-off-by: Jaghathiswari Rankappagounder Natarajan <jaghu@google.com>
Acked-by: Guenter Roeck <linux@roeck-us.net>
Acked-by: Evgeniy Polyakov <zbr@ioremap.net>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r-- | drivers/w1/slaves/w1_therm.c | 82 |
1 files changed, 51 insertions, 31 deletions
diff --git a/drivers/w1/slaves/w1_therm.c b/drivers/w1/slaves/w1_therm.c index cb3fc3c6b0d1..faddc2488c91 100644 --- a/drivers/w1/slaves/w1_therm.c +++ b/drivers/w1/slaves/w1_therm.c | |||
@@ -59,6 +59,12 @@ struct w1_therm_family_data { | |||
59 | atomic_t refcnt; | 59 | atomic_t refcnt; |
60 | }; | 60 | }; |
61 | 61 | ||
62 | struct therm_info { | ||
63 | u8 rom[9]; | ||
64 | u8 crc; | ||
65 | u8 verdict; | ||
66 | }; | ||
67 | |||
62 | /* return the address of the refcnt in the family data */ | 68 | /* return the address of the refcnt in the family data */ |
63 | #define THERM_REFCNT(family_data) \ | 69 | #define THERM_REFCNT(family_data) \ |
64 | (&((struct w1_therm_family_data *)family_data)->refcnt) | 70 | (&((struct w1_therm_family_data *)family_data)->refcnt) |
@@ -422,33 +428,31 @@ static ssize_t w1_slave_store(struct device *device, | |||
422 | return ret ? : size; | 428 | return ret ? : size; |
423 | } | 429 | } |
424 | 430 | ||
425 | static ssize_t w1_slave_show(struct device *device, | 431 | static ssize_t read_therm(struct device *device, |
426 | struct device_attribute *attr, char *buf) | 432 | struct w1_slave *sl, struct therm_info *info) |
427 | { | 433 | { |
428 | struct w1_slave *sl = dev_to_w1_slave(device); | ||
429 | struct w1_master *dev = sl->master; | 434 | struct w1_master *dev = sl->master; |
430 | u8 rom[9], crc, verdict, external_power; | 435 | u8 external_power; |
431 | int i, ret, max_trying = 10; | 436 | int ret, max_trying = 10; |
432 | ssize_t c = PAGE_SIZE; | ||
433 | u8 *family_data = sl->family_data; | 437 | u8 *family_data = sl->family_data; |
434 | 438 | ||
435 | ret = mutex_lock_interruptible(&dev->bus_mutex); | 439 | ret = mutex_lock_interruptible(&dev->bus_mutex); |
436 | if (ret != 0) | 440 | if (ret != 0) |
437 | goto post_unlock; | 441 | goto error; |
438 | 442 | ||
439 | if (!sl->family_data) { | 443 | if (!family_data) { |
440 | ret = -ENODEV; | 444 | ret = -ENODEV; |
441 | goto pre_unlock; | 445 | goto mt_unlock; |
442 | } | 446 | } |
443 | 447 | ||
444 | /* prevent the slave from going away in sleep */ | 448 | /* prevent the slave from going away in sleep */ |
445 | atomic_inc(THERM_REFCNT(family_data)); | 449 | atomic_inc(THERM_REFCNT(family_data)); |
446 | memset(rom, 0, sizeof(rom)); | 450 | memset(info->rom, 0, sizeof(info->rom)); |
447 | 451 | ||
448 | while (max_trying--) { | 452 | while (max_trying--) { |
449 | 453 | ||
450 | verdict = 0; | 454 | info->verdict = 0; |
451 | crc = 0; | 455 | info->crc = 0; |
452 | 456 | ||
453 | if (!w1_reset_select_slave(sl)) { | 457 | if (!w1_reset_select_slave(sl)) { |
454 | int count = 0; | 458 | int count = 0; |
@@ -474,47 +478,69 @@ static ssize_t w1_slave_show(struct device *device, | |||
474 | sleep_rem = msleep_interruptible(tm); | 478 | sleep_rem = msleep_interruptible(tm); |
475 | if (sleep_rem != 0) { | 479 | if (sleep_rem != 0) { |
476 | ret = -EINTR; | 480 | ret = -EINTR; |
477 | goto post_unlock; | 481 | goto dec_refcnt; |
478 | } | 482 | } |
479 | 483 | ||
480 | ret = mutex_lock_interruptible(&dev->bus_mutex); | 484 | ret = mutex_lock_interruptible(&dev->bus_mutex); |
481 | if (ret != 0) | 485 | if (ret != 0) |
482 | goto post_unlock; | 486 | goto dec_refcnt; |
483 | } else if (!w1_strong_pullup) { | 487 | } else if (!w1_strong_pullup) { |
484 | sleep_rem = msleep_interruptible(tm); | 488 | sleep_rem = msleep_interruptible(tm); |
485 | if (sleep_rem != 0) { | 489 | if (sleep_rem != 0) { |
486 | ret = -EINTR; | 490 | ret = -EINTR; |
487 | goto pre_unlock; | 491 | goto dec_refcnt; |
488 | } | 492 | } |
489 | } | 493 | } |
490 | 494 | ||
491 | if (!w1_reset_select_slave(sl)) { | 495 | if (!w1_reset_select_slave(sl)) { |
492 | 496 | ||
493 | w1_write_8(dev, W1_READ_SCRATCHPAD); | 497 | w1_write_8(dev, W1_READ_SCRATCHPAD); |
494 | count = w1_read_block(dev, rom, 9); | 498 | count = w1_read_block(dev, info->rom, 9); |
495 | if (count != 9) { | 499 | if (count != 9) { |
496 | dev_warn(device, "w1_read_block() " | 500 | dev_warn(device, "w1_read_block() " |
497 | "returned %u instead of 9.\n", | 501 | "returned %u instead of 9.\n", |
498 | count); | 502 | count); |
499 | } | 503 | } |
500 | 504 | ||
501 | crc = w1_calc_crc8(rom, 8); | 505 | info->crc = w1_calc_crc8(info->rom, 8); |
502 | 506 | ||
503 | if (rom[8] == crc) | 507 | if (info->rom[8] == info->crc) |
504 | verdict = 1; | 508 | info->verdict = 1; |
505 | } | 509 | } |
506 | } | 510 | } |
507 | 511 | ||
508 | if (verdict) | 512 | if (info->verdict) |
509 | break; | 513 | break; |
510 | } | 514 | } |
511 | 515 | ||
516 | dec_refcnt: | ||
517 | atomic_dec(THERM_REFCNT(family_data)); | ||
518 | mt_unlock: | ||
519 | mutex_unlock(&dev->bus_mutex); | ||
520 | error: | ||
521 | return ret; | ||
522 | } | ||
523 | |||
524 | static ssize_t w1_slave_show(struct device *device, | ||
525 | struct device_attribute *attr, char *buf) | ||
526 | { | ||
527 | struct w1_slave *sl = dev_to_w1_slave(device); | ||
528 | struct therm_info info; | ||
529 | u8 *family_data = sl->family_data; | ||
530 | int ret, i; | ||
531 | ssize_t c = PAGE_SIZE; | ||
532 | u8 fid = sl->family->fid; | ||
533 | |||
534 | ret = read_therm(device, sl, &info); | ||
535 | if (ret) | ||
536 | return ret; | ||
537 | |||
512 | for (i = 0; i < 9; ++i) | 538 | for (i = 0; i < 9; ++i) |
513 | c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", rom[i]); | 539 | c -= snprintf(buf + PAGE_SIZE - c, c, "%02x ", info.rom[i]); |
514 | c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n", | 540 | c -= snprintf(buf + PAGE_SIZE - c, c, ": crc=%02x %s\n", |
515 | crc, (verdict) ? "YES" : "NO"); | 541 | info.crc, (info.verdict) ? "YES" : "NO"); |
516 | if (verdict) | 542 | if (info.verdict) |
517 | memcpy(family_data, rom, sizeof(rom)); | 543 | memcpy(family_data, info.rom, sizeof(info.rom)); |
518 | else | 544 | else |
519 | dev_warn(device, "Read failed CRC check\n"); | 545 | dev_warn(device, "Read failed CRC check\n"); |
520 | 546 | ||
@@ -523,14 +549,8 @@ static ssize_t w1_slave_show(struct device *device, | |||
523 | ((u8 *)family_data)[i]); | 549 | ((u8 *)family_data)[i]); |
524 | 550 | ||
525 | c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n", | 551 | c -= snprintf(buf + PAGE_SIZE - c, c, "t=%d\n", |
526 | w1_convert_temp(rom, sl->family->fid)); | 552 | w1_convert_temp(info.rom, fid)); |
527 | ret = PAGE_SIZE - c; | 553 | ret = PAGE_SIZE - c; |
528 | |||
529 | pre_unlock: | ||
530 | mutex_unlock(&dev->bus_mutex); | ||
531 | |||
532 | post_unlock: | ||
533 | atomic_dec(THERM_REFCNT(family_data)); | ||
534 | return ret; | 554 | return ret; |
535 | } | 555 | } |
536 | 556 | ||