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

[media] rc-core: simplify sysfs code

Simplify and cleanup the sysfs code a bit.

[m.chehab@samsung.com: rebased and fixed a CodingStyle issue]
Signed-off-by: David Härdeman <david@hardeman.nu>

Signed-off-by: Mauro Carvalho Chehab <m.chehab@samsung.com>

authored by

David Härdeman and committed by
Mauro Carvalho Chehab
da6e162d 0bc56cbe

+151 -121
+7
drivers/media/rc/ir-raw.c
··· 240 240 return protocols; 241 241 } 242 242 243 + static int change_protocol(struct rc_dev *dev, u64 *rc_type) 244 + { 245 + /* the caller will update dev->enabled_protocols */ 246 + return 0; 247 + } 248 + 243 249 /* 244 250 * Used to (un)register raw event clients 245 251 */ ··· 263 257 264 258 dev->raw->dev = dev; 265 259 rc_set_enabled_protocols(dev, ~0); 260 + dev->change_protocol = change_protocol; 266 261 rc = kfifo_alloc(&dev->raw->kfifo, 267 262 sizeof(struct ir_raw_event) * MAX_IR_EVENT_SIZE, 268 263 GFP_KERNEL);
+144 -121
drivers/media/rc/rc-main.c
··· 830 830 /** 831 831 * show_protocols() - shows the current/wakeup IR protocol(s) 832 832 * @device: the device descriptor 833 - * @mattr: the device attribute struct (unused) 833 + * @mattr: the device attribute struct 834 834 * @buf: a pointer to the output buffer 835 835 * 836 836 * This routine is a callback routine for input read the IR protocol type(s). ··· 856 856 857 857 mutex_lock(&dev->lock); 858 858 859 - enabled = dev->enabled_protocols[fattr->type]; 860 - if (dev->driver_type == RC_DRIVER_SCANCODE || 861 - fattr->type == RC_FILTER_WAKEUP) 862 - allowed = dev->allowed_protocols[fattr->type]; 863 - else if (dev->raw) 864 - allowed = ir_raw_get_allowed_protocols(); 865 - else { 866 - mutex_unlock(&dev->lock); 867 - return -ENODEV; 859 + if (fattr->type == RC_FILTER_NORMAL) { 860 + enabled = dev->enabled_protocols[RC_FILTER_NORMAL]; 861 + if (dev->raw) 862 + allowed = ir_raw_get_allowed_protocols(); 863 + else 864 + allowed = dev->allowed_protocols[RC_FILTER_NORMAL]; 865 + } else { 866 + enabled = dev->enabled_protocols[RC_FILTER_WAKEUP]; 867 + allowed = dev->allowed_protocols[RC_FILTER_WAKEUP]; 868 868 } 869 869 870 - IR_dprintk(1, "allowed - 0x%llx, enabled - 0x%llx\n", 871 - (long long)allowed, 872 - (long long)enabled); 870 + mutex_unlock(&dev->lock); 871 + 872 + IR_dprintk(1, "%s: allowed - 0x%llx, enabled - 0x%llx\n", 873 + __func__, (long long)allowed, (long long)enabled); 873 874 874 875 for (i = 0; i < ARRAY_SIZE(proto_names); i++) { 875 876 if (allowed & enabled & proto_names[i].type) ··· 886 885 tmp--; 887 886 *tmp = '\n'; 888 887 889 - mutex_unlock(&dev->lock); 890 - 891 888 return tmp + 1 - buf; 892 889 } 893 890 894 891 /** 895 - * store_protocols() - changes the current/wakeup IR protocol(s) 896 - * @device: the device descriptor 897 - * @mattr: the device attribute struct (unused) 898 - * @buf: a pointer to the input buffer 899 - * @len: length of the input buffer 892 + * parse_protocol_change() - parses a protocol change request 893 + * @protocols: pointer to the bitmask of current protocols 894 + * @buf: pointer to the buffer with a list of changes 900 895 * 901 - * This routine is for changing the IR protocol type. 902 - * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols. 903 - * Writing "+proto" will add a protocol to the list of enabled protocols. 904 - * Writing "-proto" will remove a protocol from the list of enabled protocols. 896 + * Writing "+proto" will add a protocol to the protocol mask. 897 + * Writing "-proto" will remove a protocol from protocol mask. 905 898 * Writing "proto" will enable only "proto". 906 899 * Writing "none" will disable all protocols. 907 - * Returns -EINVAL if an invalid protocol combination or unknown protocol name 908 - * is used, otherwise @len. 909 - * 910 - * dev->lock is taken to guard against races between device 911 - * registration, store_protocols and show_protocols. 900 + * Returns the number of changes performed or a negative error code. 912 901 */ 913 - static ssize_t store_protocols(struct device *device, 914 - struct device_attribute *mattr, 915 - const char *data, 916 - size_t len) 902 + static int parse_protocol_change(u64 *protocols, const char *buf) 917 903 { 918 - struct rc_dev *dev = to_rc_dev(device); 919 - struct rc_filter_attribute *fattr = to_rc_filter_attr(mattr); 920 - bool enable, disable; 921 904 const char *tmp; 922 - u64 old_type, type; 905 + unsigned count = 0; 906 + bool enable, disable; 923 907 u64 mask; 924 - int rc, i, count = 0; 925 - ssize_t ret; 926 - int (*change_protocol)(struct rc_dev *dev, u64 *rc_type); 927 - int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter); 928 - struct rc_scancode_filter local_filter, *filter; 908 + int i; 929 909 930 - /* Device is being removed */ 931 - if (!dev) 932 - return -EINVAL; 933 - 934 - mutex_lock(&dev->lock); 935 - 936 - if (dev->driver_type != RC_DRIVER_SCANCODE && !dev->raw) { 937 - IR_dprintk(1, "Protocol switching not supported\n"); 938 - ret = -EINVAL; 939 - goto out; 940 - } 941 - old_type = dev->enabled_protocols[fattr->type]; 942 - type = old_type; 943 - 944 - while ((tmp = strsep((char **) &data, " \n")) != NULL) { 910 + while ((tmp = strsep((char **)&buf, " \n")) != NULL) { 945 911 if (!*tmp) 946 912 break; 947 913 ··· 934 966 935 967 if (i == ARRAY_SIZE(proto_names)) { 936 968 IR_dprintk(1, "Unknown protocol: '%s'\n", tmp); 937 - ret = -EINVAL; 938 - goto out; 969 + return -EINVAL; 939 970 } 940 971 941 972 count++; 942 973 943 974 if (enable) 944 - type |= mask; 975 + *protocols |= mask; 945 976 else if (disable) 946 - type &= ~mask; 977 + *protocols &= ~mask; 947 978 else 948 - type = mask; 979 + *protocols = mask; 949 980 } 950 981 951 982 if (!count) { 952 983 IR_dprintk(1, "Protocol not specified\n"); 953 - ret = -EINVAL; 984 + return -EINVAL; 985 + } 986 + 987 + return count; 988 + } 989 + 990 + /** 991 + * store_protocols() - changes the current/wakeup IR protocol(s) 992 + * @device: the device descriptor 993 + * @mattr: the device attribute struct 994 + * @buf: a pointer to the input buffer 995 + * @len: length of the input buffer 996 + * 997 + * This routine is for changing the IR protocol type. 998 + * It is trigged by writing to /sys/class/rc/rc?/[wakeup_]protocols. 999 + * See parse_protocol_change() for the valid commands. 1000 + * Returns @len on success or a negative error code. 1001 + * 1002 + * dev->lock is taken to guard against races between device 1003 + * registration, store_protocols and show_protocols. 1004 + */ 1005 + static ssize_t store_protocols(struct device *device, 1006 + struct device_attribute *mattr, 1007 + const char *buf, size_t len) 1008 + { 1009 + struct rc_dev *dev = to_rc_dev(device); 1010 + struct rc_filter_attribute *fattr = to_rc_filter_attr(mattr); 1011 + u64 *current_protocols; 1012 + int (*change_protocol)(struct rc_dev *dev, u64 *rc_type); 1013 + struct rc_scancode_filter *filter; 1014 + int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter); 1015 + u64 old_protocols, new_protocols; 1016 + ssize_t rc; 1017 + 1018 + /* Device is being removed */ 1019 + if (!dev) 1020 + return -EINVAL; 1021 + 1022 + if (fattr->type == RC_FILTER_NORMAL) { 1023 + IR_dprintk(1, "Normal protocol change requested\n"); 1024 + current_protocols = &dev->enabled_protocols[RC_FILTER_NORMAL]; 1025 + change_protocol = dev->change_protocol; 1026 + filter = &dev->scancode_filters[RC_FILTER_NORMAL]; 1027 + set_filter = dev->s_filter; 1028 + } else { 1029 + IR_dprintk(1, "Wakeup protocol change requested\n"); 1030 + current_protocols = &dev->enabled_protocols[RC_FILTER_WAKEUP]; 1031 + change_protocol = dev->change_wakeup_protocol; 1032 + filter = &dev->scancode_filters[RC_FILTER_WAKEUP]; 1033 + set_filter = dev->s_wakeup_filter; 1034 + } 1035 + 1036 + if (!change_protocol) { 1037 + IR_dprintk(1, "Protocol switching not supported\n"); 1038 + return -EINVAL; 1039 + } 1040 + 1041 + mutex_lock(&dev->lock); 1042 + 1043 + old_protocols = *current_protocols; 1044 + new_protocols = old_protocols; 1045 + rc = parse_protocol_change(&new_protocols, buf); 1046 + if (rc < 0) 1047 + goto out; 1048 + 1049 + rc = change_protocol(dev, &new_protocols); 1050 + if (rc < 0) { 1051 + IR_dprintk(1, "Error setting protocols to 0x%llx\n", 1052 + (long long)new_protocols); 954 1053 goto out; 955 1054 } 956 1055 957 - change_protocol = (fattr->type == RC_FILTER_NORMAL) 958 - ? dev->change_protocol : dev->change_wakeup_protocol; 959 - if (change_protocol) { 960 - rc = change_protocol(dev, &type); 961 - if (rc < 0) { 962 - IR_dprintk(1, "Error setting protocols to 0x%llx\n", 963 - (long long)type); 964 - ret = -EINVAL; 965 - goto out; 966 - } 1056 + if (new_protocols == old_protocols) { 1057 + rc = len; 1058 + goto out; 967 1059 } 968 1060 969 - dev->enabled_protocols[fattr->type] = type; 970 - IR_dprintk(1, "Current protocol(s): 0x%llx\n", 971 - (long long)type); 1061 + *current_protocols = new_protocols; 1062 + IR_dprintk(1, "Protocols changed to 0x%llx\n", (long long)new_protocols); 972 1063 973 1064 /* 974 1065 * If the protocol is changed the filter needs updating. 975 1066 * Try setting the same filter with the new protocol (if any). 976 1067 * Fall back to clearing the filter. 977 1068 */ 978 - filter = &dev->scancode_filters[fattr->type]; 979 - set_filter = (fattr->type == RC_FILTER_NORMAL) 980 - ? dev->s_filter : dev->s_wakeup_filter; 1069 + if (set_filter && filter->mask) { 1070 + if (new_protocols) 1071 + rc = set_filter(dev, filter); 1072 + else 1073 + rc = -1; 981 1074 982 - if (set_filter && old_type != type && filter->mask) { 983 - local_filter = *filter; 984 - if (!type) { 985 - /* no protocol => clear filter */ 986 - ret = -1; 987 - } else { 988 - /* hardware filtering => try setting, otherwise clear */ 989 - ret = set_filter(dev, &local_filter); 1075 + if (rc < 0) { 1076 + filter->data = 0; 1077 + filter->mask = 0; 1078 + set_filter(dev, filter); 990 1079 } 991 - if (ret < 0) { 992 - /* clear the filter */ 993 - local_filter.data = 0; 994 - local_filter.mask = 0; 995 - set_filter(dev, &local_filter); 996 - } 997 - 998 - /* commit the new filter */ 999 - *filter = local_filter; 1000 1080 } 1001 1081 1002 - ret = len; 1082 + rc = len; 1003 1083 1004 1084 out: 1005 1085 mutex_unlock(&dev->lock); 1006 - return ret; 1086 + return rc; 1007 1087 } 1008 1088 1009 1089 /** ··· 1077 1061 { 1078 1062 struct rc_dev *dev = to_rc_dev(device); 1079 1063 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr); 1064 + struct rc_scancode_filter *filter; 1080 1065 u32 val; 1081 1066 1082 1067 /* Device is being removed */ 1083 1068 if (!dev) 1084 1069 return -EINVAL; 1085 1070 1086 - mutex_lock(&dev->lock); 1087 - if ((fattr->type == RC_FILTER_NORMAL && !dev->s_filter) || 1088 - (fattr->type == RC_FILTER_WAKEUP && !dev->s_wakeup_filter)) 1089 - val = 0; 1090 - else if (fattr->mask) 1091 - val = dev->scancode_filters[fattr->type].mask; 1071 + if (fattr->type == RC_FILTER_NORMAL) 1072 + filter = &dev->scancode_filters[RC_FILTER_NORMAL]; 1092 1073 else 1093 - val = dev->scancode_filters[fattr->type].data; 1074 + filter = &dev->scancode_filters[RC_FILTER_WAKEUP]; 1075 + 1076 + mutex_lock(&dev->lock); 1077 + if (fattr->mask) 1078 + val = filter->mask; 1079 + else 1080 + val = filter->data; 1094 1081 mutex_unlock(&dev->lock); 1095 1082 1096 1083 return sprintf(buf, "%#x\n", val); ··· 1120 1101 */ 1121 1102 static ssize_t store_filter(struct device *device, 1122 1103 struct device_attribute *attr, 1123 - const char *buf, 1124 - size_t count) 1104 + const char *buf, size_t len) 1125 1105 { 1126 1106 struct rc_dev *dev = to_rc_dev(device); 1127 1107 struct rc_filter_attribute *fattr = to_rc_filter_attr(attr); 1128 - struct rc_scancode_filter local_filter, *filter; 1108 + struct rc_scancode_filter new_filter, *filter; 1129 1109 int ret; 1130 1110 unsigned long val; 1131 1111 int (*set_filter)(struct rc_dev *dev, struct rc_scancode_filter *filter); 1112 + u64 *enabled_protocols; 1132 1113 1133 1114 /* Device is being removed */ 1134 1115 if (!dev) ··· 1138 1119 if (ret < 0) 1139 1120 return ret; 1140 1121 1141 - /* Can the scancode filter be set? */ 1142 - set_filter = (fattr->type == RC_FILTER_NORMAL) ? dev->s_filter : 1143 - dev->s_wakeup_filter; 1122 + if (fattr->type == RC_FILTER_NORMAL) { 1123 + set_filter = dev->s_filter; 1124 + enabled_protocols = &dev->enabled_protocols[RC_FILTER_NORMAL]; 1125 + filter = &dev->scancode_filters[RC_FILTER_NORMAL]; 1126 + } else { 1127 + set_filter = dev->s_wakeup_filter; 1128 + enabled_protocols = &dev->enabled_protocols[RC_FILTER_WAKEUP]; 1129 + filter = &dev->scancode_filters[RC_FILTER_WAKEUP]; 1130 + } 1131 + 1144 1132 if (!set_filter) 1145 1133 return -EINVAL; 1146 1134 1147 1135 mutex_lock(&dev->lock); 1148 1136 1149 - /* Tell the driver about the new filter */ 1150 - filter = &dev->scancode_filters[fattr->type]; 1151 - local_filter = *filter; 1137 + new_filter = *filter; 1152 1138 if (fattr->mask) 1153 - local_filter.mask = val; 1139 + new_filter.mask = val; 1154 1140 else 1155 - local_filter.data = val; 1141 + new_filter.data = val; 1156 1142 1157 - if (!dev->enabled_protocols[fattr->type] && local_filter.mask) { 1143 + if (!*enabled_protocols && val) { 1158 1144 /* refuse to set a filter unless a protocol is enabled */ 1159 1145 ret = -EINVAL; 1160 1146 goto unlock; 1161 1147 } 1162 1148 1163 - ret = set_filter(dev, &local_filter); 1149 + ret = set_filter(dev, &new_filter); 1164 1150 if (ret < 0) 1165 1151 goto unlock; 1166 1152 1167 - /* Success, commit the new filter */ 1168 - *filter = local_filter; 1153 + *filter = new_filter; 1169 1154 1170 1155 unlock: 1171 1156 mutex_unlock(&dev->lock); 1172 - return (ret < 0) ? ret : count; 1157 + return (ret < 0) ? ret : len; 1173 1158 } 1174 1159 1175 1160 static void rc_dev_release(struct device *device)