Merge branch 'acpi-scan'

* acpi-scan:
ACPI / scan: Rework modalias creation when "compatible" is present
ACPI / scan: Take the PRP0001 position in the list of IDs into account
ACPI / scan: Simplify acpi_match_device()
ACPI / scan: Generalize of_compatible matching
ACPI / scan: fix fixed event handler return value

+232 -156
+232 -156
drivers/acpi/scan.c
··· 114 return 0; 115 } 116 117 - /* 118 * Creates hid/cid(s) string needed for modalias and uevent 119 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: 120 * char *modalias: "acpi:IBM0001:ACPI0001" ··· 127 * -EINVAL: output error 128 * -ENOMEM: output is truncated 129 */ 130 - static int create_modalias(struct acpi_device *acpi_dev, char *modalias, 131 - int size) 132 { 133 int len; 134 int count; 135 struct acpi_hardware_id *id; 136 137 - if (list_empty(&acpi_dev->pnp.ids)) 138 return 0; 139 140 - /* 141 - * If the device has PRP0001 we expose DT compatible modalias 142 - * instead in form of of:NnameTCcompatible. 143 - */ 144 - if (acpi_dev->data.of_compatible) { 145 - struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 146 - const union acpi_object *of_compatible, *obj; 147 - int i, nval; 148 - char *c; 149 150 - acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf); 151 - /* DT strings are all in lower case */ 152 - for (c = buf.pointer; *c != '\0'; c++) 153 - *c = tolower(*c); 154 155 - len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer); 156 - ACPI_FREE(buf.pointer); 157 158 - of_compatible = acpi_dev->data.of_compatible; 159 - if (of_compatible->type == ACPI_TYPE_PACKAGE) { 160 - nval = of_compatible->package.count; 161 - obj = of_compatible->package.elements; 162 - } else { /* Must be ACPI_TYPE_STRING. */ 163 - nval = 1; 164 - obj = of_compatible; 165 - } 166 - for (i = 0; i < nval; i++, obj++) { 167 - count = snprintf(&modalias[len], size, "C%s", 168 - obj->string.pointer); 169 - if (count < 0) 170 - return -EINVAL; 171 - if (count >= size) 172 - return -ENOMEM; 173 174 - len += count; 175 - size -= count; 176 - } 177 - } else { 178 - len = snprintf(modalias, size, "acpi:"); 179 - size -= len; 180 181 - list_for_each_entry(id, &acpi_dev->pnp.ids, list) { 182 - count = snprintf(&modalias[len], size, "%s:", id->id); 183 - if (count < 0) 184 - return -EINVAL; 185 - if (count >= size) 186 - return -ENOMEM; 187 - len += count; 188 - size -= count; 189 - } 190 } 191 192 modalias[len] = '\0'; 193 return len; 194 } ··· 229 * 230 * Check if the given device has an ACPI companion and if that companion has 231 * a valid list of PNP IDs, and if the device is the first (primary) physical 232 - * device associated with it. 233 * 234 * If multiple physical devices are attached to a single ACPI companion, we need 235 * to be careful. The usage scenario for this kind of relationship is that all ··· 244 * resources available from it but they will be matched normally using functions 245 * provided by their bus types (and analogously for their modalias). 246 */ 247 - static bool acpi_companion_match(const struct device *dev) 248 { 249 struct acpi_device *adev; 250 - bool ret; 251 252 adev = ACPI_COMPANION(dev); 253 if (!adev) 254 - return false; 255 256 if (list_empty(&adev->pnp.ids)) 257 - return false; 258 259 mutex_lock(&adev->physical_node_lock); 260 if (list_empty(&adev->physical_node_list)) { 261 - ret = false; 262 } else { 263 const struct acpi_device_physical_node *node; 264 265 node = list_first_entry(&adev->physical_node_list, 266 struct acpi_device_physical_node, node); 267 - ret = node->dev == dev; 268 } 269 mutex_unlock(&adev->physical_node_lock); 270 271 - return ret; 272 } 273 274 /* ··· 315 */ 316 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) 317 { 318 - int len; 319 - 320 - if (!acpi_companion_match(dev)) 321 - return -ENODEV; 322 - 323 - if (add_uevent_var(env, "MODALIAS=")) 324 - return -ENOMEM; 325 - len = create_modalias(ACPI_COMPANION(dev), &env->buf[env->buflen - 1], 326 - sizeof(env->buf) - env->buflen); 327 - if (len <= 0) 328 - return len; 329 - env->buflen += len; 330 - return 0; 331 } 332 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias); 333 334 /* 335 * Creates modalias sysfs attribute for ACPI enumerated devices. ··· 358 */ 359 int acpi_device_modalias(struct device *dev, char *buf, int size) 360 { 361 - int len; 362 - 363 - if (!acpi_companion_match(dev)) 364 - return -ENODEV; 365 - 366 - len = create_modalias(ACPI_COMPANION(dev), buf, size -1); 367 - if (len <= 0) 368 - return len; 369 - buf[len++] = '\n'; 370 - return len; 371 } 372 EXPORT_SYMBOL_GPL(acpi_device_modalias); 373 374 static ssize_t 375 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) { 376 - struct acpi_device *acpi_dev = to_acpi_device(dev); 377 - int len; 378 - 379 - len = create_modalias(acpi_dev, buf, 1024); 380 - if (len <= 0) 381 - return len; 382 - buf[len++] = '\n'; 383 - return len; 384 } 385 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); 386 ··· 969 ACPI Bus operations 970 -------------------------------------------------------------------------- */ 971 972 static const struct acpi_device_id *__acpi_match_device( 973 - struct acpi_device *device, const struct acpi_device_id *ids) 974 { 975 const struct acpi_device_id *id; 976 struct acpi_hardware_id *hwid; ··· 1022 * If the device is not present, it is unnecessary to load device 1023 * driver for it. 1024 */ 1025 - if (!device->status.present) 1026 return NULL; 1027 1028 - for (id = ids; id->id[0]; id++) 1029 - list_for_each_entry(hwid, &device->pnp.ids, list) 1030 if (!strcmp((char *) id->id, hwid->id)) 1031 return id; 1032 1033 return NULL; 1034 } 1035 ··· 1060 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids, 1061 const struct device *dev) 1062 { 1063 - struct acpi_device *adev; 1064 - acpi_handle handle = ACPI_HANDLE(dev); 1065 - 1066 - if (!ids || !handle || acpi_bus_get_device(handle, &adev)) 1067 - return NULL; 1068 - 1069 - if (!acpi_companion_match(dev)) 1070 - return NULL; 1071 - 1072 - return __acpi_match_device(adev, ids); 1073 } 1074 EXPORT_SYMBOL_GPL(acpi_match_device); 1075 1076 int acpi_match_device_ids(struct acpi_device *device, 1077 const struct acpi_device_id *ids) 1078 { 1079 - return __acpi_match_device(device, ids) ? 0 : -ENOENT; 1080 } 1081 EXPORT_SYMBOL(acpi_match_device_ids); 1082 - 1083 - /* Performs match against special "PRP0001" shoehorn ACPI ID */ 1084 - static bool acpi_of_driver_match_device(struct device *dev, 1085 - const struct device_driver *drv) 1086 - { 1087 - const union acpi_object *of_compatible, *obj; 1088 - struct acpi_device *adev; 1089 - int i, nval; 1090 - 1091 - adev = ACPI_COMPANION(dev); 1092 - if (!adev) 1093 - return false; 1094 - 1095 - of_compatible = adev->data.of_compatible; 1096 - if (!drv->of_match_table || !of_compatible) 1097 - return false; 1098 - 1099 - if (of_compatible->type == ACPI_TYPE_PACKAGE) { 1100 - nval = of_compatible->package.count; 1101 - obj = of_compatible->package.elements; 1102 - } else { /* Must be ACPI_TYPE_STRING. */ 1103 - nval = 1; 1104 - obj = of_compatible; 1105 - } 1106 - /* Now we can look for the driver DT compatible strings */ 1107 - for (i = 0; i < nval; i++, obj++) { 1108 - const struct of_device_id *id; 1109 - 1110 - for (id = drv->of_match_table; id->compatible[0]; id++) 1111 - if (!strcasecmp(obj->string.pointer, id->compatible)) 1112 - return true; 1113 - } 1114 - 1115 - return false; 1116 - } 1117 1118 bool acpi_driver_match_device(struct device *dev, 1119 const struct device_driver *drv) 1120 { 1121 if (!drv->acpi_match_table) 1122 - return acpi_of_driver_match_device(dev, drv); 1123 1124 - return !!acpi_match_device(drv->acpi_match_table, dev); 1125 } 1126 EXPORT_SYMBOL_GPL(acpi_driver_match_device); 1127 ··· 1120 1121 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) 1122 { 1123 - struct acpi_device *acpi_dev = to_acpi_device(dev); 1124 - int len; 1125 - 1126 - if (list_empty(&acpi_dev->pnp.ids)) 1127 - return 0; 1128 - 1129 - if (add_uevent_var(env, "MODALIAS=")) 1130 - return -ENOMEM; 1131 - len = create_modalias(acpi_dev, &env->buf[env->buflen - 1], 1132 - sizeof(env->buf) - env->buflen); 1133 - if (len <= 0) 1134 - return len; 1135 - env->buflen += len; 1136 - return 0; 1137 } 1138 1139 static void acpi_device_notify(acpi_handle handle, u32 event, void *data) ··· 1138 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device); 1139 } 1140 1141 - static acpi_status acpi_device_fixed_event(void *data) 1142 { 1143 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data); 1144 - return AE_OK; 1145 } 1146 1147 static int acpi_device_install_notify_handler(struct acpi_device *device)
··· 114 return 0; 115 } 116 117 + /** 118 + * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent 119 + * @acpi_dev: ACPI device object. 120 + * @modalias: Buffer to print into. 121 + * @size: Size of the buffer. 122 + * 123 * Creates hid/cid(s) string needed for modalias and uevent 124 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get: 125 * char *modalias: "acpi:IBM0001:ACPI0001" ··· 122 * -EINVAL: output error 123 * -ENOMEM: output is truncated 124 */ 125 + static int create_pnp_modalias(struct acpi_device *acpi_dev, char *modalias, 126 + int size) 127 { 128 int len; 129 int count; 130 struct acpi_hardware_id *id; 131 132 + /* 133 + * Since we skip PRP0001 from the modalias below, 0 should be returned 134 + * if PRP0001 is the only ACPI/PNP ID in the device's list. 135 + */ 136 + count = 0; 137 + list_for_each_entry(id, &acpi_dev->pnp.ids, list) 138 + if (strcmp(id->id, "PRP0001")) 139 + count++; 140 + 141 + if (!count) 142 return 0; 143 144 + len = snprintf(modalias, size, "acpi:"); 145 + if (len <= 0) 146 + return len; 147 148 + size -= len; 149 150 + list_for_each_entry(id, &acpi_dev->pnp.ids, list) { 151 + if (!strcmp(id->id, "PRP0001")) 152 + continue; 153 154 + count = snprintf(&modalias[len], size, "%s:", id->id); 155 + if (count < 0) 156 + return -EINVAL; 157 158 + if (count >= size) 159 + return -ENOMEM; 160 161 + len += count; 162 + size -= count; 163 } 164 + modalias[len] = '\0'; 165 + return len; 166 + } 167 168 + /** 169 + * create_of_modalias - Creates DT compatible string for modalias and uevent 170 + * @acpi_dev: ACPI device object. 171 + * @modalias: Buffer to print into. 172 + * @size: Size of the buffer. 173 + * 174 + * Expose DT compatible modalias as of:NnameTCcompatible. This function should 175 + * only be called for devices having PRP0001 in their list of ACPI/PNP IDs. 176 + */ 177 + static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias, 178 + int size) 179 + { 180 + struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 181 + const union acpi_object *of_compatible, *obj; 182 + int len, count; 183 + int i, nval; 184 + char *c; 185 + 186 + acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf); 187 + /* DT strings are all in lower case */ 188 + for (c = buf.pointer; *c != '\0'; c++) 189 + *c = tolower(*c); 190 + 191 + len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer); 192 + ACPI_FREE(buf.pointer); 193 + 194 + if (len <= 0) 195 + return len; 196 + 197 + of_compatible = acpi_dev->data.of_compatible; 198 + if (of_compatible->type == ACPI_TYPE_PACKAGE) { 199 + nval = of_compatible->package.count; 200 + obj = of_compatible->package.elements; 201 + } else { /* Must be ACPI_TYPE_STRING. */ 202 + nval = 1; 203 + obj = of_compatible; 204 + } 205 + for (i = 0; i < nval; i++, obj++) { 206 + count = snprintf(&modalias[len], size, "C%s", 207 + obj->string.pointer); 208 + if (count < 0) 209 + return -EINVAL; 210 + 211 + if (count >= size) 212 + return -ENOMEM; 213 + 214 + len += count; 215 + size -= count; 216 + } 217 modalias[len] = '\0'; 218 return len; 219 } ··· 194 * 195 * Check if the given device has an ACPI companion and if that companion has 196 * a valid list of PNP IDs, and if the device is the first (primary) physical 197 + * device associated with it. Return the companion pointer if that's the case 198 + * or NULL otherwise. 199 * 200 * If multiple physical devices are attached to a single ACPI companion, we need 201 * to be careful. The usage scenario for this kind of relationship is that all ··· 208 * resources available from it but they will be matched normally using functions 209 * provided by their bus types (and analogously for their modalias). 210 */ 211 + static struct acpi_device *acpi_companion_match(const struct device *dev) 212 { 213 struct acpi_device *adev; 214 215 adev = ACPI_COMPANION(dev); 216 if (!adev) 217 + return NULL; 218 219 if (list_empty(&adev->pnp.ids)) 220 + return NULL; 221 222 mutex_lock(&adev->physical_node_lock); 223 if (list_empty(&adev->physical_node_list)) { 224 + adev = NULL; 225 } else { 226 const struct acpi_device_physical_node *node; 227 228 node = list_first_entry(&adev->physical_node_list, 229 struct acpi_device_physical_node, node); 230 + if (node->dev != dev) 231 + adev = NULL; 232 } 233 mutex_unlock(&adev->physical_node_lock); 234 235 + return adev; 236 + } 237 + 238 + static int __acpi_device_uevent_modalias(struct acpi_device *adev, 239 + struct kobj_uevent_env *env) 240 + { 241 + int len; 242 + 243 + if (!adev) 244 + return -ENODEV; 245 + 246 + if (list_empty(&adev->pnp.ids)) 247 + return 0; 248 + 249 + if (add_uevent_var(env, "MODALIAS=")) 250 + return -ENOMEM; 251 + 252 + len = create_pnp_modalias(adev, &env->buf[env->buflen - 1], 253 + sizeof(env->buf) - env->buflen); 254 + if (len < 0) 255 + return len; 256 + 257 + env->buflen += len; 258 + if (!adev->data.of_compatible) 259 + return 0; 260 + 261 + if (len > 0 && add_uevent_var(env, "MODALIAS=")) 262 + return -ENOMEM; 263 + 264 + len = create_of_modalias(adev, &env->buf[env->buflen - 1], 265 + sizeof(env->buf) - env->buflen); 266 + if (len < 0) 267 + return len; 268 + 269 + env->buflen += len; 270 + 271 + return 0; 272 } 273 274 /* ··· 243 */ 244 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env) 245 { 246 + return __acpi_device_uevent_modalias(acpi_companion_match(dev), env); 247 } 248 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias); 249 + 250 + static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size) 251 + { 252 + int len, count; 253 + 254 + if (!adev) 255 + return -ENODEV; 256 + 257 + if (list_empty(&adev->pnp.ids)) 258 + return 0; 259 + 260 + len = create_pnp_modalias(adev, buf, size - 1); 261 + if (len < 0) { 262 + return len; 263 + } else if (len > 0) { 264 + buf[len++] = '\n'; 265 + size -= len; 266 + } 267 + if (!adev->data.of_compatible) 268 + return len; 269 + 270 + count = create_of_modalias(adev, buf + len, size - 1); 271 + if (count < 0) { 272 + return count; 273 + } else if (count > 0) { 274 + len += count; 275 + buf[len++] = '\n'; 276 + } 277 + 278 + return len; 279 + } 280 281 /* 282 * Creates modalias sysfs attribute for ACPI enumerated devices. ··· 267 */ 268 int acpi_device_modalias(struct device *dev, char *buf, int size) 269 { 270 + return __acpi_device_modalias(acpi_companion_match(dev), buf, size); 271 } 272 EXPORT_SYMBOL_GPL(acpi_device_modalias); 273 274 static ssize_t 275 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) { 276 + return __acpi_device_modalias(to_acpi_device(dev), buf, 1024); 277 } 278 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL); 279 ··· 894 ACPI Bus operations 895 -------------------------------------------------------------------------- */ 896 897 + /** 898 + * acpi_of_match_device - Match device object using the "compatible" property. 899 + * @adev: ACPI device object to match. 900 + * @of_match_table: List of device IDs to match against. 901 + * 902 + * If @dev has an ACPI companion which has the special PRP0001 device ID in its 903 + * list of identifiers and a _DSD object with the "compatible" property, use 904 + * that property to match against the given list of identifiers. 905 + */ 906 + static bool acpi_of_match_device(struct acpi_device *adev, 907 + const struct of_device_id *of_match_table) 908 + { 909 + const union acpi_object *of_compatible, *obj; 910 + int i, nval; 911 + 912 + if (!adev) 913 + return false; 914 + 915 + of_compatible = adev->data.of_compatible; 916 + if (!of_match_table || !of_compatible) 917 + return false; 918 + 919 + if (of_compatible->type == ACPI_TYPE_PACKAGE) { 920 + nval = of_compatible->package.count; 921 + obj = of_compatible->package.elements; 922 + } else { /* Must be ACPI_TYPE_STRING. */ 923 + nval = 1; 924 + obj = of_compatible; 925 + } 926 + /* Now we can look for the driver DT compatible strings */ 927 + for (i = 0; i < nval; i++, obj++) { 928 + const struct of_device_id *id; 929 + 930 + for (id = of_match_table; id->compatible[0]; id++) 931 + if (!strcasecmp(obj->string.pointer, id->compatible)) 932 + return true; 933 + } 934 + 935 + return false; 936 + } 937 + 938 static const struct acpi_device_id *__acpi_match_device( 939 + struct acpi_device *device, 940 + const struct acpi_device_id *ids, 941 + const struct of_device_id *of_ids) 942 { 943 const struct acpi_device_id *id; 944 struct acpi_hardware_id *hwid; ··· 904 * If the device is not present, it is unnecessary to load device 905 * driver for it. 906 */ 907 + if (!device || !device->status.present) 908 return NULL; 909 910 + list_for_each_entry(hwid, &device->pnp.ids, list) { 911 + /* First, check the ACPI/PNP IDs provided by the caller. */ 912 + for (id = ids; id->id[0]; id++) 913 if (!strcmp((char *) id->id, hwid->id)) 914 return id; 915 916 + /* 917 + * Next, check the special "PRP0001" ID and try to match the 918 + * "compatible" property if found. 919 + * 920 + * The id returned by the below is not valid, but the only 921 + * caller passing non-NULL of_ids here is only interested in 922 + * whether or not the return value is NULL. 923 + */ 924 + if (!strcmp("PRP0001", hwid->id) 925 + && acpi_of_match_device(device, of_ids)) 926 + return id; 927 + } 928 return NULL; 929 } 930 ··· 929 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids, 930 const struct device *dev) 931 { 932 + return __acpi_match_device(acpi_companion_match(dev), ids, NULL); 933 } 934 EXPORT_SYMBOL_GPL(acpi_match_device); 935 936 int acpi_match_device_ids(struct acpi_device *device, 937 const struct acpi_device_id *ids) 938 { 939 + return __acpi_match_device(device, ids, NULL) ? 0 : -ENOENT; 940 } 941 EXPORT_SYMBOL(acpi_match_device_ids); 942 943 bool acpi_driver_match_device(struct device *dev, 944 const struct device_driver *drv) 945 { 946 if (!drv->acpi_match_table) 947 + return acpi_of_match_device(ACPI_COMPANION(dev), 948 + drv->of_match_table); 949 950 + return !!__acpi_match_device(acpi_companion_match(dev), 951 + drv->acpi_match_table, drv->of_match_table); 952 } 953 EXPORT_SYMBOL_GPL(acpi_driver_match_device); 954 ··· 1031 1032 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env) 1033 { 1034 + return __acpi_device_uevent_modalias(to_acpi_device(dev), env); 1035 } 1036 1037 static void acpi_device_notify(acpi_handle handle, u32 event, void *data) ··· 1062 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device); 1063 } 1064 1065 + static u32 acpi_device_fixed_event(void *data) 1066 { 1067 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data); 1068 + return ACPI_INTERRUPT_HANDLED; 1069 } 1070 1071 static int acpi_device_install_notify_handler(struct acpi_device *device)