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

Configure Feed

Select the types of activity you want to include in your feed.

Input: iqs5xx - switch to using cleanup functions

Start using __free() and guard() primitives to simplify the code and error
handling.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>

+64 -81
+64 -81
drivers/input/touchscreen/iqs5xx.c
··· 356 356 } 357 357 358 358 static int iqs5xx_bl_write(struct i2c_client *client, 359 - u16 bl_addr, u8 *pmap_data, u16 pmap_len) 359 + u16 bl_addr, const u8 *pmap_data, u16 pmap_len) 360 360 { 361 361 struct i2c_msg msg; 362 362 int ret, i; ··· 395 395 } 396 396 397 397 static int iqs5xx_bl_verify(struct i2c_client *client, 398 - u16 bl_addr, u8 *pmap_data, u16 pmap_len) 398 + u16 bl_addr, const u8 *pmap_data, u16 pmap_len) 399 399 { 400 400 struct i2c_msg msg; 401 401 int ret, i; ··· 446 446 if (!iqs5xx->dev_id_info.bl_status) 447 447 return 0; 448 448 449 - mutex_lock(&iqs5xx->lock); 449 + guard(mutex)(&iqs5xx->lock); 450 450 451 451 /* 452 452 * Addressing the device outside of a communication window prompts it 453 453 * to assert the RDY output, so disable the interrupt line to prevent 454 454 * the handler from servicing a false interrupt. 455 455 */ 456 - disable_irq(client->irq); 456 + guard(disable_irq)(&client->irq); 457 457 458 458 error1 = iqs5xx_write_byte(client, IQS5XX_SYS_CTRL1, state); 459 459 error2 = iqs5xx_write_byte(client, IQS5XX_END_COMM, 0); 460 460 461 461 usleep_range(50, 100); 462 - enable_irq(client->irq); 463 462 464 - mutex_unlock(&iqs5xx->lock); 465 - 466 - if (error1) 467 - return error1; 468 - 469 - return error2; 463 + return error1 ?: error2; 470 464 } 471 465 472 466 static int iqs5xx_open(struct input_dev *input) ··· 697 703 static int iqs5xx_fw_file_parse(struct i2c_client *client, 698 704 const char *fw_file, u8 *pmap) 699 705 { 700 - const struct firmware *fw; 701 706 struct iqs5xx_ihex_rec *rec; 702 707 size_t pos = 0; 703 708 int error, i; ··· 715 722 * Because the ihex2fw tool tolerates neither (1) nor (2), the slightly 716 723 * nonstandard ihex firmware is parsed directly by the driver. 717 724 */ 725 + const struct firmware *fw __free(firmware) = NULL; 718 726 error = request_firmware(&fw, fw_file, &client->dev); 719 727 if (error) { 720 728 dev_err(&client->dev, "Failed to request firmware %s: %d\n", ··· 726 732 do { 727 733 if (pos + sizeof(*rec) > fw->size) { 728 734 dev_err(&client->dev, "Insufficient firmware size\n"); 729 - error = -EINVAL; 730 - break; 735 + return -EINVAL; 731 736 } 732 737 rec = (struct iqs5xx_ihex_rec *)(fw->data + pos); 733 738 pos += sizeof(*rec); ··· 734 741 if (rec->start != ':') { 735 742 dev_err(&client->dev, "Invalid start at record %u\n", 736 743 rec_num); 737 - error = -EINVAL; 738 - break; 744 + return -EINVAL; 739 745 } 740 746 741 747 error = hex2bin(rec_hdr, rec->len, sizeof(rec_hdr)); 742 748 if (error) { 743 749 dev_err(&client->dev, "Invalid header at record %u\n", 744 750 rec_num); 745 - break; 751 + return error; 746 752 } 747 753 748 754 rec_len = *rec_hdr; ··· 750 758 751 759 if (pos + rec_len * 2 > fw->size) { 752 760 dev_err(&client->dev, "Insufficient firmware size\n"); 753 - error = -EINVAL; 754 - break; 761 + return -EINVAL; 755 762 } 756 763 pos += (rec_len * 2); 757 764 ··· 758 767 if (error) { 759 768 dev_err(&client->dev, "Invalid data at record %u\n", 760 769 rec_num); 761 - break; 770 + return error; 762 771 } 763 772 764 773 error = hex2bin(&rec_chksm, ··· 766 775 if (error) { 767 776 dev_err(&client->dev, "Invalid checksum at record %u\n", 768 777 rec_num); 769 - break; 778 + return error; 770 779 } 771 780 772 781 chksm = 0; ··· 791 800 dev_err(&client->dev, 792 801 "Invalid address at record %u\n", 793 802 rec_num); 794 - error = -EINVAL; 795 - } else { 796 - memcpy(pmap + rec_addr - IQS5XX_CHKSM, 797 - rec_data, rec_len); 803 + return -EINVAL; 798 804 } 805 + 806 + memcpy(pmap + rec_addr - IQS5XX_CHKSM, 807 + rec_data, rec_len); 799 808 break; 809 + 800 810 case IQS5XX_REC_TYPE_EOF: 801 811 break; 812 + 802 813 default: 803 814 dev_err(&client->dev, "Invalid type at record %u\n", 804 815 rec_num); 805 - error = -EINVAL; 816 + return -EINVAL; 806 817 } 807 - 808 - if (error) 809 - break; 810 818 811 819 rec_num++; 812 820 while (pos < fw->size) { ··· 815 825 } 816 826 } while (rec_type != IQS5XX_REC_TYPE_EOF); 817 827 818 - release_firmware(fw); 819 - 820 - return error; 828 + return 0; 821 829 } 822 830 823 - static int iqs5xx_fw_file_write(struct i2c_client *client, const char *fw_file) 831 + static int iqs5xx_update_firmware(struct iqs5xx_private *iqs5xx, const u8 *pmap) 824 832 { 825 - struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client); 826 - int error, error_init = 0; 827 - u8 *pmap; 828 - 829 - pmap = kzalloc(IQS5XX_PMAP_LEN, GFP_KERNEL); 830 - if (!pmap) 831 - return -ENOMEM; 832 - 833 - error = iqs5xx_fw_file_parse(client, fw_file, pmap); 834 - if (error) 835 - goto err_kfree; 836 - 837 - mutex_lock(&iqs5xx->lock); 838 - 839 - /* 840 - * Disable the interrupt line in case the first attempt(s) to enter the 841 - * bootloader don't happen quickly enough, in which case the device may 842 - * assert the RDY output until the next attempt. 843 - */ 844 - disable_irq(client->irq); 833 + struct i2c_client *client = iqs5xx->client; 834 + int error; 845 835 846 836 iqs5xx->dev_id_info.bl_status = 0; 847 837 ··· 829 859 if (error) { 830 860 error = iqs5xx_bl_open(client); 831 861 if (error) 832 - goto err_reset; 862 + return error; 833 863 } 834 864 835 865 error = iqs5xx_bl_write(client, IQS5XX_CHKSM, pmap, IQS5XX_PMAP_LEN); 836 866 if (error) 837 - goto err_reset; 867 + return error; 838 868 839 869 error = iqs5xx_bl_cmd(client, IQS5XX_BL_CMD_CRC, 0); 840 870 if (error) 841 - goto err_reset; 871 + return error; 842 872 843 873 error = iqs5xx_bl_verify(client, IQS5XX_CSTM, 844 874 pmap + IQS5XX_CHKSM_LEN + IQS5XX_APP_LEN, 845 875 IQS5XX_CSTM_LEN); 876 + if (error) 877 + return error; 846 878 847 - err_reset: 879 + return 0; 880 + } 881 + 882 + static int iqs5xx_fw_file_write(struct i2c_client *client, const char *fw_file) 883 + { 884 + struct iqs5xx_private *iqs5xx = i2c_get_clientdata(client); 885 + int error, error_init = 0; 886 + 887 + u8 *pmap __free(kfree) = kzalloc(IQS5XX_PMAP_LEN, GFP_KERNEL); 888 + if (!pmap) 889 + return -ENOMEM; 890 + 891 + error = iqs5xx_fw_file_parse(client, fw_file, pmap); 892 + if (error) 893 + return error; 894 + 895 + guard(mutex)(&iqs5xx->lock); 896 + 897 + /* 898 + * Disable the interrupt line in case the first attempt(s) to enter the 899 + * bootloader don't happen quickly enough, in which case the device may 900 + * assert the RDY output until the next attempt. 901 + */ 902 + guard(disable_irq)(&client->irq); 903 + 904 + error = iqs5xx_update_firmware(iqs5xx, pmap); 905 + 848 906 iqs5xx_reset(client); 849 907 usleep_range(15000, 15100); 850 908 ··· 880 882 if (!iqs5xx->dev_id_info.bl_status) 881 883 error_init = error_init ? : -EINVAL; 882 884 883 - enable_irq(client->irq); 884 - 885 - mutex_unlock(&iqs5xx->lock); 886 - 887 - err_kfree: 888 - kfree(pmap); 889 - 890 - return error ? : error_init; 885 + return error ?: error_init; 891 886 } 892 887 893 888 static ssize_t fw_file_store(struct device *dev, ··· 976 985 { 977 986 struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev); 978 987 struct input_dev *input = iqs5xx->input; 979 - int error = 0; 980 988 981 989 if (!input || device_may_wakeup(dev)) 982 - return error; 990 + return 0; 983 991 984 - mutex_lock(&input->mutex); 985 - 992 + guard(mutex)(&input->mutex); 986 993 if (input_device_enabled(input)) 987 - error = iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND); 994 + return iqs5xx_set_state(iqs5xx->client, IQS5XX_SUSPEND); 988 995 989 - mutex_unlock(&input->mutex); 990 - 991 - return error; 996 + return 0; 992 997 } 993 998 994 999 static int iqs5xx_resume(struct device *dev) 995 1000 { 996 1001 struct iqs5xx_private *iqs5xx = dev_get_drvdata(dev); 997 1002 struct input_dev *input = iqs5xx->input; 998 - int error = 0; 999 1003 1000 1004 if (!input || device_may_wakeup(dev)) 1001 - return error; 1005 + return 0; 1002 1006 1003 - mutex_lock(&input->mutex); 1004 - 1007 + guard(mutex)(&input->mutex); 1005 1008 if (input_device_enabled(input)) 1006 - error = iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME); 1009 + return iqs5xx_set_state(iqs5xx->client, IQS5XX_RESUME); 1007 1010 1008 - mutex_unlock(&input->mutex); 1009 - 1010 - return error; 1011 + return 0; 1011 1012 } 1012 1013 1013 1014 static DEFINE_SIMPLE_DEV_PM_OPS(iqs5xx_pm, iqs5xx_suspend, iqs5xx_resume);