OSDN Git Service

PCI: keystone: Fix link training retries initiation
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / acpi / device_sysfs.c
1 /*
2  * drivers/acpi/device_sysfs.c - ACPI device sysfs attributes and modalias.
3  *
4  * Copyright (C) 2015, Intel Corp.
5  * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
6  * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License version 2 as published
12  *  by the Free Software Foundation.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20  */
21
22 #include <linux/acpi.h>
23 #include <linux/device.h>
24 #include <linux/export.h>
25 #include <linux/nls.h>
26
27 #include "internal.h"
28
29 static ssize_t acpi_object_path(acpi_handle handle, char *buf)
30 {
31         struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
32         int result;
33
34         result = acpi_get_name(handle, ACPI_FULL_PATHNAME, &path);
35         if (result)
36                 return result;
37
38         result = sprintf(buf, "%s\n", (char*)path.pointer);
39         kfree(path.pointer);
40         return result;
41 }
42
43 struct acpi_data_node_attr {
44         struct attribute attr;
45         ssize_t (*show)(struct acpi_data_node *, char *);
46         ssize_t (*store)(struct acpi_data_node *, const char *, size_t count);
47 };
48
49 #define DATA_NODE_ATTR(_name)                   \
50         static struct acpi_data_node_attr data_node_##_name =   \
51                 __ATTR(_name, 0444, data_node_show_##_name, NULL)
52
53 static ssize_t data_node_show_path(struct acpi_data_node *dn, char *buf)
54 {
55         return acpi_object_path(dn->handle, buf);
56 }
57
58 DATA_NODE_ATTR(path);
59
60 static struct attribute *acpi_data_node_default_attrs[] = {
61         &data_node_path.attr,
62         NULL
63 };
64
65 #define to_data_node(k) container_of(k, struct acpi_data_node, kobj)
66 #define to_attr(a) container_of(a, struct acpi_data_node_attr, attr)
67
68 static ssize_t acpi_data_node_attr_show(struct kobject *kobj,
69                                         struct attribute *attr, char *buf)
70 {
71         struct acpi_data_node *dn = to_data_node(kobj);
72         struct acpi_data_node_attr *dn_attr = to_attr(attr);
73
74         return dn_attr->show ? dn_attr->show(dn, buf) : -ENXIO;
75 }
76
77 static const struct sysfs_ops acpi_data_node_sysfs_ops = {
78         .show   = acpi_data_node_attr_show,
79 };
80
81 static void acpi_data_node_release(struct kobject *kobj)
82 {
83         struct acpi_data_node *dn = to_data_node(kobj);
84         complete(&dn->kobj_done);
85 }
86
87 static struct kobj_type acpi_data_node_ktype = {
88         .sysfs_ops = &acpi_data_node_sysfs_ops,
89         .default_attrs = acpi_data_node_default_attrs,
90         .release = acpi_data_node_release,
91 };
92
93 static void acpi_expose_nondev_subnodes(struct kobject *kobj,
94                                         struct acpi_device_data *data)
95 {
96         struct list_head *list = &data->subnodes;
97         struct acpi_data_node *dn;
98
99         if (list_empty(list))
100                 return;
101
102         list_for_each_entry(dn, list, sibling) {
103                 int ret;
104
105                 init_completion(&dn->kobj_done);
106                 ret = kobject_init_and_add(&dn->kobj, &acpi_data_node_ktype,
107                                            kobj, "%s", dn->name);
108                 if (ret)
109                         acpi_handle_err(dn->handle, "Failed to expose (%d)\n", ret);
110                 else
111                         acpi_expose_nondev_subnodes(&dn->kobj, &dn->data);
112         }
113 }
114
115 static void acpi_hide_nondev_subnodes(struct acpi_device_data *data)
116 {
117         struct list_head *list = &data->subnodes;
118         struct acpi_data_node *dn;
119
120         if (list_empty(list))
121                 return;
122
123         list_for_each_entry_reverse(dn, list, sibling) {
124                 acpi_hide_nondev_subnodes(&dn->data);
125                 kobject_put(&dn->kobj);
126         }
127 }
128
129 /**
130  * create_pnp_modalias - Create hid/cid(s) string for modalias and uevent
131  * @acpi_dev: ACPI device object.
132  * @modalias: Buffer to print into.
133  * @size: Size of the buffer.
134  *
135  * Creates hid/cid(s) string needed for modalias and uevent
136  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
137  * char *modalias: "acpi:IBM0001:ACPI0001"
138  * Return: 0: no _HID and no _CID
139  *         -EINVAL: output error
140  *         -ENOMEM: output is truncated
141 */
142 static int create_pnp_modalias(struct acpi_device *acpi_dev, char *modalias,
143                                int size)
144 {
145         int len;
146         int count;
147         struct acpi_hardware_id *id;
148
149         /* Avoid unnecessarily loading modules for non present devices. */
150         if (!acpi_device_is_present(acpi_dev))
151                 return 0;
152
153         /*
154          * Since we skip ACPI_DT_NAMESPACE_HID from the modalias below, 0 should
155          * be returned if ACPI_DT_NAMESPACE_HID is the only ACPI/PNP ID in the
156          * device's list.
157          */
158         count = 0;
159         list_for_each_entry(id, &acpi_dev->pnp.ids, list)
160                 if (strcmp(id->id, ACPI_DT_NAMESPACE_HID))
161                         count++;
162
163         if (!count)
164                 return 0;
165
166         len = snprintf(modalias, size, "acpi:");
167         if (len <= 0)
168                 return len;
169
170         size -= len;
171
172         list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
173                 if (!strcmp(id->id, ACPI_DT_NAMESPACE_HID))
174                         continue;
175
176                 count = snprintf(&modalias[len], size, "%s:", id->id);
177                 if (count < 0)
178                         return -EINVAL;
179
180                 if (count >= size)
181                         return -ENOMEM;
182
183                 len += count;
184                 size -= count;
185         }
186         modalias[len] = '\0';
187         return len;
188 }
189
190 /**
191  * create_of_modalias - Creates DT compatible string for modalias and uevent
192  * @acpi_dev: ACPI device object.
193  * @modalias: Buffer to print into.
194  * @size: Size of the buffer.
195  *
196  * Expose DT compatible modalias as of:NnameTCcompatible.  This function should
197  * only be called for devices having ACPI_DT_NAMESPACE_HID in their list of
198  * ACPI/PNP IDs.
199  */
200 static int create_of_modalias(struct acpi_device *acpi_dev, char *modalias,
201                               int size)
202 {
203         struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER };
204         const union acpi_object *of_compatible, *obj;
205         acpi_status status;
206         int len, count;
207         int i, nval;
208         char *c;
209
210         status = acpi_get_name(acpi_dev->handle, ACPI_SINGLE_NAME, &buf);
211         if (ACPI_FAILURE(status))
212                 return -ENODEV;
213
214         /* DT strings are all in lower case */
215         for (c = buf.pointer; *c != '\0'; c++)
216                 *c = tolower(*c);
217
218         len = snprintf(modalias, size, "of:N%sT", (char *)buf.pointer);
219         ACPI_FREE(buf.pointer);
220
221         if (len <= 0)
222                 return len;
223
224         of_compatible = acpi_dev->data.of_compatible;
225         if (of_compatible->type == ACPI_TYPE_PACKAGE) {
226                 nval = of_compatible->package.count;
227                 obj = of_compatible->package.elements;
228         } else { /* Must be ACPI_TYPE_STRING. */
229                 nval = 1;
230                 obj = of_compatible;
231         }
232         for (i = 0; i < nval; i++, obj++) {
233                 count = snprintf(&modalias[len], size, "C%s",
234                                  obj->string.pointer);
235                 if (count < 0)
236                         return -EINVAL;
237
238                 if (count >= size)
239                         return -ENOMEM;
240
241                 len += count;
242                 size -= count;
243         }
244         modalias[len] = '\0';
245         return len;
246 }
247
248 int __acpi_device_uevent_modalias(struct acpi_device *adev,
249                                   struct kobj_uevent_env *env)
250 {
251         int len;
252
253         if (!adev)
254                 return -ENODEV;
255
256         if (list_empty(&adev->pnp.ids))
257                 return 0;
258
259         if (add_uevent_var(env, "MODALIAS="))
260                 return -ENOMEM;
261
262         len = create_pnp_modalias(adev, &env->buf[env->buflen - 1],
263                                   sizeof(env->buf) - env->buflen);
264         if (len < 0)
265                 return len;
266
267         env->buflen += len;
268         if (!adev->data.of_compatible)
269                 return 0;
270
271         if (len > 0 && add_uevent_var(env, "MODALIAS="))
272                 return -ENOMEM;
273
274         len = create_of_modalias(adev, &env->buf[env->buflen - 1],
275                                  sizeof(env->buf) - env->buflen);
276         if (len < 0)
277                 return len;
278
279         env->buflen += len;
280
281         return 0;
282 }
283
284 /**
285  * acpi_device_uevent_modalias - uevent modalias for ACPI-enumerated devices.
286  *
287  * Create the uevent modalias field for ACPI-enumerated devices.
288  *
289  * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
290  * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
291  */
292 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
293 {
294         return __acpi_device_uevent_modalias(acpi_companion_match(dev), env);
295 }
296 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
297
298 static int __acpi_device_modalias(struct acpi_device *adev, char *buf, int size)
299 {
300         int len, count;
301
302         if (!adev)
303                 return -ENODEV;
304
305         if (list_empty(&adev->pnp.ids))
306                 return 0;
307
308         len = create_pnp_modalias(adev, buf, size - 1);
309         if (len < 0) {
310                 return len;
311         } else if (len > 0) {
312                 buf[len++] = '\n';
313                 size -= len;
314         }
315         if (!adev->data.of_compatible)
316                 return len;
317
318         count = create_of_modalias(adev, buf + len, size - 1);
319         if (count < 0) {
320                 return count;
321         } else if (count > 0) {
322                 len += count;
323                 buf[len++] = '\n';
324         }
325
326         return len;
327 }
328
329 /**
330  * acpi_device_modalias - modalias sysfs attribute for ACPI-enumerated devices.
331  *
332  * Create the modalias sysfs attribute for ACPI-enumerated devices.
333  *
334  * Because other buses do not support ACPI HIDs & CIDs, e.g. for a device with
335  * hid:IBM0001 and cid:ACPI0001 you get: "acpi:IBM0001:ACPI0001".
336  */
337 int acpi_device_modalias(struct device *dev, char *buf, int size)
338 {
339         return __acpi_device_modalias(acpi_companion_match(dev), buf, size);
340 }
341 EXPORT_SYMBOL_GPL(acpi_device_modalias);
342
343 static ssize_t
344 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
345         return __acpi_device_modalias(to_acpi_device(dev), buf, 1024);
346 }
347 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
348
349 static ssize_t real_power_state_show(struct device *dev,
350                                      struct device_attribute *attr, char *buf)
351 {
352         struct acpi_device *adev = to_acpi_device(dev);
353         int state;
354         int ret;
355
356         ret = acpi_device_get_power(adev, &state);
357         if (ret)
358                 return ret;
359
360         return sprintf(buf, "%s\n", acpi_power_state_string(state));
361 }
362
363 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
364
365 static ssize_t power_state_show(struct device *dev,
366                                 struct device_attribute *attr, char *buf)
367 {
368         struct acpi_device *adev = to_acpi_device(dev);
369
370         return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
371 }
372
373 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
374
375 static ssize_t
376 acpi_eject_store(struct device *d, struct device_attribute *attr,
377                 const char *buf, size_t count)
378 {
379         struct acpi_device *acpi_device = to_acpi_device(d);
380         acpi_object_type not_used;
381         acpi_status status;
382
383         if (!count || buf[0] != '1')
384                 return -EINVAL;
385
386         if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
387             && !acpi_device->driver)
388                 return -ENODEV;
389
390         status = acpi_get_type(acpi_device->handle, &not_used);
391         if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
392                 return -ENODEV;
393
394         get_device(&acpi_device->dev);
395         status = acpi_hotplug_schedule(acpi_device, ACPI_OST_EC_OSPM_EJECT);
396         if (ACPI_SUCCESS(status))
397                 return count;
398
399         put_device(&acpi_device->dev);
400         acpi_evaluate_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
401                           ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
402         return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
403 }
404
405 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
406
407 static ssize_t
408 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
409         struct acpi_device *acpi_dev = to_acpi_device(dev);
410
411         return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
412 }
413 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
414
415 static ssize_t acpi_device_uid_show(struct device *dev,
416                                     struct device_attribute *attr, char *buf)
417 {
418         struct acpi_device *acpi_dev = to_acpi_device(dev);
419
420         return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
421 }
422 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
423
424 static ssize_t acpi_device_adr_show(struct device *dev,
425                                     struct device_attribute *attr, char *buf)
426 {
427         struct acpi_device *acpi_dev = to_acpi_device(dev);
428
429         return sprintf(buf, "0x%08x\n",
430                        (unsigned int)(acpi_dev->pnp.bus_address));
431 }
432 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
433
434 static ssize_t acpi_device_path_show(struct device *dev,
435                                      struct device_attribute *attr, char *buf)
436 {
437         struct acpi_device *acpi_dev = to_acpi_device(dev);
438
439         return acpi_object_path(acpi_dev->handle, buf);
440 }
441 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
442
443 /* sysfs file that shows description text from the ACPI _STR method */
444 static ssize_t description_show(struct device *dev,
445                                 struct device_attribute *attr,
446                                 char *buf) {
447         struct acpi_device *acpi_dev = to_acpi_device(dev);
448         int result;
449
450         if (acpi_dev->pnp.str_obj == NULL)
451                 return 0;
452
453         /*
454          * The _STR object contains a Unicode identifier for a device.
455          * We need to convert to utf-8 so it can be displayed.
456          */
457         result = utf16s_to_utf8s(
458                 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
459                 acpi_dev->pnp.str_obj->buffer.length,
460                 UTF16_LITTLE_ENDIAN, buf,
461                 PAGE_SIZE);
462
463         buf[result++] = '\n';
464
465         return result;
466 }
467 static DEVICE_ATTR(description, 0444, description_show, NULL);
468
469 static ssize_t
470 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
471                      char *buf) {
472         struct acpi_device *acpi_dev = to_acpi_device(dev);
473         acpi_status status;
474         unsigned long long sun;
475
476         status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
477         if (ACPI_FAILURE(status))
478                 return -ENODEV;
479
480         return sprintf(buf, "%llu\n", sun);
481 }
482 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
483
484 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
485                                 char *buf) {
486         struct acpi_device *acpi_dev = to_acpi_device(dev);
487         acpi_status status;
488         unsigned long long sta;
489
490         status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
491         if (ACPI_FAILURE(status))
492                 return -ENODEV;
493
494         return sprintf(buf, "%llu\n", sta);
495 }
496 static DEVICE_ATTR_RO(status);
497
498 /**
499  * acpi_device_setup_files - Create sysfs attributes of an ACPI device.
500  * @dev: ACPI device object.
501  */
502 int acpi_device_setup_files(struct acpi_device *dev)
503 {
504         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
505         acpi_status status;
506         int result = 0;
507
508         /*
509          * Devices gotten from FADT don't have a "path" attribute
510          */
511         if (dev->handle) {
512                 result = device_create_file(&dev->dev, &dev_attr_path);
513                 if (result)
514                         goto end;
515         }
516
517         if (!list_empty(&dev->pnp.ids)) {
518                 result = device_create_file(&dev->dev, &dev_attr_hid);
519                 if (result)
520                         goto end;
521
522                 result = device_create_file(&dev->dev, &dev_attr_modalias);
523                 if (result)
524                         goto end;
525         }
526
527         /*
528          * If device has _STR, 'description' file is created
529          */
530         if (acpi_has_method(dev->handle, "_STR")) {
531                 status = acpi_evaluate_object(dev->handle, "_STR",
532                                         NULL, &buffer);
533                 if (ACPI_FAILURE(status))
534                         buffer.pointer = NULL;
535                 dev->pnp.str_obj = buffer.pointer;
536                 result = device_create_file(&dev->dev, &dev_attr_description);
537                 if (result)
538                         goto end;
539         }
540
541         if (dev->pnp.type.bus_address)
542                 result = device_create_file(&dev->dev, &dev_attr_adr);
543         if (dev->pnp.unique_id)
544                 result = device_create_file(&dev->dev, &dev_attr_uid);
545
546         if (acpi_has_method(dev->handle, "_SUN")) {
547                 result = device_create_file(&dev->dev, &dev_attr_sun);
548                 if (result)
549                         goto end;
550         }
551
552         if (acpi_has_method(dev->handle, "_STA")) {
553                 result = device_create_file(&dev->dev, &dev_attr_status);
554                 if (result)
555                         goto end;
556         }
557
558         /*
559          * If device has _EJ0, 'eject' file is created that is used to trigger
560          * hot-removal function from userland.
561          */
562         if (acpi_has_method(dev->handle, "_EJ0")) {
563                 result = device_create_file(&dev->dev, &dev_attr_eject);
564                 if (result)
565                         return result;
566         }
567
568         if (dev->flags.power_manageable) {
569                 result = device_create_file(&dev->dev, &dev_attr_power_state);
570                 if (result)
571                         return result;
572
573                 if (dev->power.flags.power_resources)
574                         result = device_create_file(&dev->dev,
575                                                     &dev_attr_real_power_state);
576         }
577
578         acpi_expose_nondev_subnodes(&dev->dev.kobj, &dev->data);
579
580 end:
581         return result;
582 }
583
584 /**
585  * acpi_device_remove_files - Remove sysfs attributes of an ACPI device.
586  * @dev: ACPI device object.
587  */
588 void acpi_device_remove_files(struct acpi_device *dev)
589 {
590         acpi_hide_nondev_subnodes(&dev->data);
591
592         if (dev->flags.power_manageable) {
593                 device_remove_file(&dev->dev, &dev_attr_power_state);
594                 if (dev->power.flags.power_resources)
595                         device_remove_file(&dev->dev,
596                                            &dev_attr_real_power_state);
597         }
598
599         /*
600          * If device has _STR, remove 'description' file
601          */
602         if (acpi_has_method(dev->handle, "_STR")) {
603                 kfree(dev->pnp.str_obj);
604                 device_remove_file(&dev->dev, &dev_attr_description);
605         }
606         /*
607          * If device has _EJ0, remove 'eject' file.
608          */
609         if (acpi_has_method(dev->handle, "_EJ0"))
610                 device_remove_file(&dev->dev, &dev_attr_eject);
611
612         if (acpi_has_method(dev->handle, "_SUN"))
613                 device_remove_file(&dev->dev, &dev_attr_sun);
614
615         if (dev->pnp.unique_id)
616                 device_remove_file(&dev->dev, &dev_attr_uid);
617         if (dev->pnp.type.bus_address)
618                 device_remove_file(&dev->dev, &dev_attr_adr);
619         device_remove_file(&dev->dev, &dev_attr_modalias);
620         device_remove_file(&dev->dev, &dev_attr_hid);
621         if (acpi_has_method(dev->handle, "_STA"))
622                 device_remove_file(&dev->dev, &dev_attr_status);
623         if (dev->handle)
624                 device_remove_file(&dev->dev, &dev_attr_path);
625 }