Correct .gbs.conf settings
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / acpi / scan.c
1 /*
2  * scan.c - support for transforming the ACPI namespace into individual objects
3  */
4
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/slab.h>
8 #include <linux/kernel.h>
9 #include <linux/acpi.h>
10 #include <linux/signal.h>
11 #include <linux/kthread.h>
12 #include <linux/dmi.h>
13 #include <linux/nls.h>
14
15 #include <asm/pgtable.h>
16
17 #include "internal.h"
18
19 #define _COMPONENT              ACPI_BUS_COMPONENT
20 ACPI_MODULE_NAME("scan");
21 extern struct acpi_device *acpi_root;
22
23 #define ACPI_BUS_CLASS                  "system_bus"
24 #define ACPI_BUS_HID                    "LNXSYBUS"
25 #define ACPI_BUS_DEVICE_NAME            "System Bus"
26
27 #define ACPI_IS_ROOT_DEVICE(device)    (!(device)->parent)
28
29 #define INVALID_ACPI_HANDLE     ((acpi_handle)empty_zero_page)
30
31 /*
32  * If set, devices will be hot-removed even if they cannot be put offline
33  * gracefully (from the kernel's standpoint).
34  */
35 bool acpi_force_hot_remove;
36
37 static const char *dummy_hid = "device";
38
39 static LIST_HEAD(acpi_bus_id_list);
40 static DEFINE_MUTEX(acpi_scan_lock);
41 static LIST_HEAD(acpi_scan_handlers_list);
42 DEFINE_MUTEX(acpi_device_lock);
43 LIST_HEAD(acpi_wakeup_device_list);
44
45 struct acpi_device_bus_id{
46         char bus_id[15];
47         unsigned int instance_no;
48         struct list_head node;
49 };
50
51 void acpi_scan_lock_acquire(void)
52 {
53         mutex_lock(&acpi_scan_lock);
54 }
55 EXPORT_SYMBOL_GPL(acpi_scan_lock_acquire);
56
57 void acpi_scan_lock_release(void)
58 {
59         mutex_unlock(&acpi_scan_lock);
60 }
61 EXPORT_SYMBOL_GPL(acpi_scan_lock_release);
62
63 int acpi_scan_add_handler(struct acpi_scan_handler *handler)
64 {
65         if (!handler || !handler->attach)
66                 return -EINVAL;
67
68         list_add_tail(&handler->list_node, &acpi_scan_handlers_list);
69         return 0;
70 }
71
72 int acpi_scan_add_handler_with_hotplug(struct acpi_scan_handler *handler,
73                                        const char *hotplug_profile_name)
74 {
75         int error;
76
77         error = acpi_scan_add_handler(handler);
78         if (error)
79                 return error;
80
81         acpi_sysfs_add_hotplug_profile(&handler->hotplug, hotplug_profile_name);
82         return 0;
83 }
84
85 /*
86  * Creates hid/cid(s) string needed for modalias and uevent
87  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
88  * char *modalias: "acpi:IBM0001:ACPI0001"
89  * Return: 0: no _HID and no _CID
90  *         -EINVAL: output error
91  *         -ENOMEM: output is truncated
92 */
93 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
94                            int size)
95 {
96         int len;
97         int count;
98         struct acpi_hardware_id *id;
99
100         if (list_empty(&acpi_dev->pnp.ids))
101                 return 0;
102
103         len = snprintf(modalias, size, "acpi:");
104         size -= len;
105
106         list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
107                 count = snprintf(&modalias[len], size, "%s:", id->id);
108                 if (count < 0)
109                         return -EINVAL;
110                 if (count >= size)
111                         return -ENOMEM;
112                 len += count;
113                 size -= count;
114         }
115
116         modalias[len] = '\0';
117         return len;
118 }
119
120 /*
121  * Creates uevent modalias field for ACPI enumerated devices.
122  * Because the other buses does not support ACPI HIDs & CIDs.
123  * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
124  * "acpi:IBM0001:ACPI0001"
125  */
126 int acpi_device_uevent_modalias(struct device *dev, struct kobj_uevent_env *env)
127 {
128         struct acpi_device *acpi_dev;
129         int len;
130
131         acpi_dev = ACPI_COMPANION(dev);
132         if (!acpi_dev)
133                 return -ENODEV;
134
135         /* Fall back to bus specific way of modalias exporting */
136         if (list_empty(&acpi_dev->pnp.ids))
137                 return -ENODEV;
138
139         if (add_uevent_var(env, "MODALIAS="))
140                 return -ENOMEM;
141         len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
142                                 sizeof(env->buf) - env->buflen);
143         if (len <= 0)
144                 return len;
145         env->buflen += len;
146         return 0;
147 }
148 EXPORT_SYMBOL_GPL(acpi_device_uevent_modalias);
149
150 /*
151  * Creates modalias sysfs attribute for ACPI enumerated devices.
152  * Because the other buses does not support ACPI HIDs & CIDs.
153  * e.g. for a device with hid:IBM0001 and cid:ACPI0001 you get:
154  * "acpi:IBM0001:ACPI0001"
155  */
156 int acpi_device_modalias(struct device *dev, char *buf, int size)
157 {
158         struct acpi_device *acpi_dev;
159         int len;
160
161         acpi_dev = ACPI_COMPANION(dev);
162         if (!acpi_dev)
163                 return -ENODEV;
164
165         /* Fall back to bus specific way of modalias exporting */
166         if (list_empty(&acpi_dev->pnp.ids))
167                 return -ENODEV;
168
169         len = create_modalias(acpi_dev, buf, size -1);
170         if (len <= 0)
171                 return len;
172         buf[len++] = '\n';
173         return len;
174 }
175 EXPORT_SYMBOL_GPL(acpi_device_modalias);
176
177 static ssize_t
178 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
179         struct acpi_device *acpi_dev = to_acpi_device(dev);
180         int len;
181
182         len = create_modalias(acpi_dev, buf, 1024);
183         if (len <= 0)
184                 return len;
185         buf[len++] = '\n';
186         return len;
187 }
188 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
189
190 bool acpi_scan_is_offline(struct acpi_device *adev, bool uevent)
191 {
192         struct acpi_device_physical_node *pn;
193         bool offline = true;
194
195         mutex_lock(&adev->physical_node_lock);
196
197         list_for_each_entry(pn, &adev->physical_node_list, node)
198                 if (device_supports_offline(pn->dev) && !pn->dev->offline) {
199                         if (uevent)
200                                 kobject_uevent(&pn->dev->kobj, KOBJ_CHANGE);
201
202                         offline = false;
203                         break;
204                 }
205
206         mutex_unlock(&adev->physical_node_lock);
207         return offline;
208 }
209
210 static acpi_status acpi_bus_offline(acpi_handle handle, u32 lvl, void *data,
211                                     void **ret_p)
212 {
213         struct acpi_device *device = NULL;
214         struct acpi_device_physical_node *pn;
215         bool second_pass = (bool)data;
216         acpi_status status = AE_OK;
217
218         if (acpi_bus_get_device(handle, &device))
219                 return AE_OK;
220
221         if (device->handler && !device->handler->hotplug.enabled) {
222                 *ret_p = &device->dev;
223                 return AE_SUPPORT;
224         }
225
226         mutex_lock(&device->physical_node_lock);
227
228         list_for_each_entry(pn, &device->physical_node_list, node) {
229                 int ret;
230
231                 if (second_pass) {
232                         /* Skip devices offlined by the first pass. */
233                         if (pn->put_online)
234                                 continue;
235                 } else {
236                         pn->put_online = false;
237                 }
238                 ret = device_offline(pn->dev);
239                 if (acpi_force_hot_remove)
240                         continue;
241
242                 if (ret >= 0) {
243                         pn->put_online = !ret;
244                 } else {
245                         *ret_p = pn->dev;
246                         if (second_pass) {
247                                 status = AE_ERROR;
248                                 break;
249                         }
250                 }
251         }
252
253         mutex_unlock(&device->physical_node_lock);
254
255         return status;
256 }
257
258 static acpi_status acpi_bus_online(acpi_handle handle, u32 lvl, void *data,
259                                    void **ret_p)
260 {
261         struct acpi_device *device = NULL;
262         struct acpi_device_physical_node *pn;
263
264         if (acpi_bus_get_device(handle, &device))
265                 return AE_OK;
266
267         mutex_lock(&device->physical_node_lock);
268
269         list_for_each_entry(pn, &device->physical_node_list, node)
270                 if (pn->put_online) {
271                         device_online(pn->dev);
272                         pn->put_online = false;
273                 }
274
275         mutex_unlock(&device->physical_node_lock);
276
277         return AE_OK;
278 }
279
280 static int acpi_scan_try_to_offline(struct acpi_device *device)
281 {
282         acpi_handle handle = device->handle;
283         struct device *errdev = NULL;
284         acpi_status status;
285
286         /*
287          * Carry out two passes here and ignore errors in the first pass,
288          * because if the devices in question are memory blocks and
289          * CONFIG_MEMCG is set, one of the blocks may hold data structures
290          * that the other blocks depend on, but it is not known in advance which
291          * block holds them.
292          *
293          * If the first pass is successful, the second one isn't needed, though.
294          */
295         status = acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
296                                      NULL, acpi_bus_offline, (void *)false,
297                                      (void **)&errdev);
298         if (status == AE_SUPPORT) {
299                 dev_warn(errdev, "Offline disabled.\n");
300                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
301                                     acpi_bus_online, NULL, NULL, NULL);
302                 return -EPERM;
303         }
304         acpi_bus_offline(handle, 0, (void *)false, (void **)&errdev);
305         if (errdev) {
306                 errdev = NULL;
307                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
308                                     NULL, acpi_bus_offline, (void *)true,
309                                     (void **)&errdev);
310                 if (!errdev || acpi_force_hot_remove)
311                         acpi_bus_offline(handle, 0, (void *)true,
312                                          (void **)&errdev);
313
314                 if (errdev && !acpi_force_hot_remove) {
315                         dev_warn(errdev, "Offline failed.\n");
316                         acpi_bus_online(handle, 0, NULL, NULL);
317                         acpi_walk_namespace(ACPI_TYPE_ANY, handle,
318                                             ACPI_UINT32_MAX, acpi_bus_online,
319                                             NULL, NULL, NULL);
320                         return -EBUSY;
321                 }
322         }
323         return 0;
324 }
325
326 static int acpi_scan_hot_remove(struct acpi_device *device)
327 {
328         acpi_handle handle = device->handle;
329         unsigned long long sta;
330         acpi_status status;
331
332         if (device->handler && device->handler->hotplug.demand_offline
333             && !acpi_force_hot_remove) {
334                 if (!acpi_scan_is_offline(device, true))
335                         return -EBUSY;
336         } else {
337                 int error = acpi_scan_try_to_offline(device);
338                 if (error)
339                         return error;
340         }
341
342         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
343                 "Hot-removing device %s...\n", dev_name(&device->dev)));
344
345         acpi_bus_trim(device);
346
347         acpi_evaluate_lck(handle, 0);
348         /*
349          * TBD: _EJD support.
350          */
351         status = acpi_evaluate_ej0(handle);
352         if (status == AE_NOT_FOUND)
353                 return -ENODEV;
354         else if (ACPI_FAILURE(status))
355                 return -EIO;
356
357         /*
358          * Verify if eject was indeed successful.  If not, log an error
359          * message.  No need to call _OST since _EJ0 call was made OK.
360          */
361         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
362         if (ACPI_FAILURE(status)) {
363                 acpi_handle_warn(handle,
364                         "Status check after eject failed (0x%x)\n", status);
365         } else if (sta & ACPI_STA_DEVICE_ENABLED) {
366                 acpi_handle_warn(handle,
367                         "Eject incomplete - status 0x%llx\n", sta);
368         }
369
370         return 0;
371 }
372
373 static int acpi_scan_device_not_present(struct acpi_device *adev)
374 {
375         if (!acpi_device_enumerated(adev)) {
376                 dev_warn(&adev->dev, "Still not present\n");
377                 return -EALREADY;
378         }
379         acpi_bus_trim(adev);
380         return 0;
381 }
382
383 static int acpi_scan_device_check(struct acpi_device *adev)
384 {
385         int error;
386
387         acpi_bus_get_status(adev);
388         if (adev->status.present || adev->status.functional) {
389                 /*
390                  * This function is only called for device objects for which
391                  * matching scan handlers exist.  The only situation in which
392                  * the scan handler is not attached to this device object yet
393                  * is when the device has just appeared (either it wasn't
394                  * present at all before or it was removed and then added
395                  * again).
396                  */
397                 if (adev->handler) {
398                         dev_warn(&adev->dev, "Already enumerated\n");
399                         return -EALREADY;
400                 }
401                 error = acpi_bus_scan(adev->handle);
402                 if (error) {
403                         dev_warn(&adev->dev, "Namespace scan failure\n");
404                         return error;
405                 }
406                 if (!adev->handler) {
407                         dev_warn(&adev->dev, "Enumeration failure\n");
408                         error = -ENODEV;
409                 }
410         } else {
411                 error = acpi_scan_device_not_present(adev);
412         }
413         return error;
414 }
415
416 static int acpi_scan_bus_check(struct acpi_device *adev)
417 {
418         struct acpi_scan_handler *handler = adev->handler;
419         struct acpi_device *child;
420         int error;
421
422         acpi_bus_get_status(adev);
423         if (!(adev->status.present || adev->status.functional)) {
424                 acpi_scan_device_not_present(adev);
425                 return 0;
426         }
427         if (handler && handler->hotplug.scan_dependent)
428                 return handler->hotplug.scan_dependent(adev);
429
430         error = acpi_bus_scan(adev->handle);
431         if (error) {
432                 dev_warn(&adev->dev, "Namespace scan failure\n");
433                 return error;
434         }
435         list_for_each_entry(child, &adev->children, node) {
436                 error = acpi_scan_bus_check(child);
437                 if (error)
438                         return error;
439         }
440         return 0;
441 }
442
443 static void acpi_device_hotplug(void *data, u32 src)
444 {
445         u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
446         struct acpi_device *adev = data;
447         int error;
448
449         lock_device_hotplug();
450         mutex_lock(&acpi_scan_lock);
451
452         /*
453          * The device object's ACPI handle cannot become invalid as long as we
454          * are holding acpi_scan_lock, but it may have become invalid before
455          * that lock was acquired.
456          */
457         if (adev->handle == INVALID_ACPI_HANDLE)
458                 goto out;
459
460         switch (src) {
461         case ACPI_NOTIFY_BUS_CHECK:
462                 error = acpi_scan_bus_check(adev);
463                 break;
464         case ACPI_NOTIFY_DEVICE_CHECK:
465                 error = acpi_scan_device_check(adev);
466                 break;
467         case ACPI_NOTIFY_EJECT_REQUEST:
468         case ACPI_OST_EC_OSPM_EJECT:
469                 error = acpi_scan_hot_remove(adev);
470                 break;
471         default:
472                 error = -EINVAL;
473                 break;
474         }
475         if (!error)
476                 ost_code = ACPI_OST_SC_SUCCESS;
477
478  out:
479         acpi_evaluate_hotplug_ost(adev->handle, src, ost_code, NULL);
480         put_device(&adev->dev);
481         mutex_unlock(&acpi_scan_lock);
482         unlock_device_hotplug();
483 }
484
485 static void acpi_hotplug_notify_cb(acpi_handle handle, u32 type, void *data)
486 {
487         u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE;
488         struct acpi_device *adev;
489         acpi_status status;
490
491         if (acpi_bus_get_device(handle, &adev))
492                 goto err_out;
493
494         switch (type) {
495         case ACPI_NOTIFY_BUS_CHECK:
496                 acpi_handle_debug(handle, "ACPI_NOTIFY_BUS_CHECK event\n");
497                 break;
498         case ACPI_NOTIFY_DEVICE_CHECK:
499                 acpi_handle_debug(handle, "ACPI_NOTIFY_DEVICE_CHECK event\n");
500                 break;
501         case ACPI_NOTIFY_EJECT_REQUEST:
502                 acpi_handle_debug(handle, "ACPI_NOTIFY_EJECT_REQUEST event\n");
503                 if (!adev->handler)
504                         goto err_out;
505
506                 if (!adev->handler->hotplug.enabled) {
507                         acpi_handle_err(handle, "Eject disabled\n");
508                         ost_code = ACPI_OST_SC_EJECT_NOT_SUPPORTED;
509                         goto err_out;
510                 }
511                 acpi_evaluate_hotplug_ost(handle, ACPI_NOTIFY_EJECT_REQUEST,
512                                           ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
513                 break;
514         default:
515                 /* non-hotplug event; possibly handled by other handler */
516                 return;
517         }
518         get_device(&adev->dev);
519         status = acpi_hotplug_execute(acpi_device_hotplug, adev, type);
520         if (ACPI_SUCCESS(status))
521                 return;
522
523         put_device(&adev->dev);
524
525  err_out:
526         acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
527 }
528
529 static ssize_t real_power_state_show(struct device *dev,
530                                      struct device_attribute *attr, char *buf)
531 {
532         struct acpi_device *adev = to_acpi_device(dev);
533         int state;
534         int ret;
535
536         ret = acpi_device_get_power(adev, &state);
537         if (ret)
538                 return ret;
539
540         return sprintf(buf, "%s\n", acpi_power_state_string(state));
541 }
542
543 static DEVICE_ATTR(real_power_state, 0444, real_power_state_show, NULL);
544
545 static ssize_t power_state_show(struct device *dev,
546                                 struct device_attribute *attr, char *buf)
547 {
548         struct acpi_device *adev = to_acpi_device(dev);
549
550         return sprintf(buf, "%s\n", acpi_power_state_string(adev->power.state));
551 }
552
553 static DEVICE_ATTR(power_state, 0444, power_state_show, NULL);
554
555 static ssize_t
556 acpi_eject_store(struct device *d, struct device_attribute *attr,
557                 const char *buf, size_t count)
558 {
559         struct acpi_device *acpi_device = to_acpi_device(d);
560         acpi_object_type not_used;
561         acpi_status status;
562
563         if (!count || buf[0] != '1')
564                 return -EINVAL;
565
566         if ((!acpi_device->handler || !acpi_device->handler->hotplug.enabled)
567             && !acpi_device->driver)
568                 return -ENODEV;
569
570         status = acpi_get_type(acpi_device->handle, &not_used);
571         if (ACPI_FAILURE(status) || !acpi_device->flags.ejectable)
572                 return -ENODEV;
573
574         acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
575                                   ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
576         get_device(&acpi_device->dev);
577         status = acpi_hotplug_execute(acpi_device_hotplug, acpi_device,
578                                       ACPI_OST_EC_OSPM_EJECT);
579         if (ACPI_SUCCESS(status))
580                 return count;
581
582         put_device(&acpi_device->dev);
583         acpi_evaluate_hotplug_ost(acpi_device->handle, ACPI_OST_EC_OSPM_EJECT,
584                                   ACPI_OST_SC_NON_SPECIFIC_FAILURE, NULL);
585         return status == AE_NO_MEMORY ? -ENOMEM : -EAGAIN;
586 }
587
588 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
589
590 static ssize_t
591 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
592         struct acpi_device *acpi_dev = to_acpi_device(dev);
593
594         return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
595 }
596 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
597
598 static ssize_t acpi_device_uid_show(struct device *dev,
599                                     struct device_attribute *attr, char *buf)
600 {
601         struct acpi_device *acpi_dev = to_acpi_device(dev);
602
603         return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
604 }
605 static DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
606
607 static ssize_t acpi_device_adr_show(struct device *dev,
608                                     struct device_attribute *attr, char *buf)
609 {
610         struct acpi_device *acpi_dev = to_acpi_device(dev);
611
612         return sprintf(buf, "0x%08x\n",
613                        (unsigned int)(acpi_dev->pnp.bus_address));
614 }
615 static DEVICE_ATTR(adr, 0444, acpi_device_adr_show, NULL);
616
617 static ssize_t
618 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
619         struct acpi_device *acpi_dev = to_acpi_device(dev);
620         struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
621         int result;
622
623         result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
624         if (result)
625                 goto end;
626
627         result = sprintf(buf, "%s\n", (char*)path.pointer);
628         kfree(path.pointer);
629 end:
630         return result;
631 }
632 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
633
634 /* sysfs file that shows description text from the ACPI _STR method */
635 static ssize_t description_show(struct device *dev,
636                                 struct device_attribute *attr,
637                                 char *buf) {
638         struct acpi_device *acpi_dev = to_acpi_device(dev);
639         int result;
640
641         if (acpi_dev->pnp.str_obj == NULL)
642                 return 0;
643
644         /*
645          * The _STR object contains a Unicode identifier for a device.
646          * We need to convert to utf-8 so it can be displayed.
647          */
648         result = utf16s_to_utf8s(
649                 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
650                 acpi_dev->pnp.str_obj->buffer.length,
651                 UTF16_LITTLE_ENDIAN, buf,
652                 PAGE_SIZE);
653
654         buf[result++] = '\n';
655
656         return result;
657 }
658 static DEVICE_ATTR(description, 0444, description_show, NULL);
659
660 static ssize_t
661 acpi_device_sun_show(struct device *dev, struct device_attribute *attr,
662                      char *buf) {
663         struct acpi_device *acpi_dev = to_acpi_device(dev);
664         acpi_status status;
665         unsigned long long sun;
666
667         status = acpi_evaluate_integer(acpi_dev->handle, "_SUN", NULL, &sun);
668         if (ACPI_FAILURE(status))
669                 return -ENODEV;
670
671         return sprintf(buf, "%llu\n", sun);
672 }
673 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
674
675 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
676                                 char *buf) {
677         struct acpi_device *acpi_dev = to_acpi_device(dev);
678         acpi_status status;
679         unsigned long long sta;
680
681         status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
682         if (ACPI_FAILURE(status))
683                 return -ENODEV;
684
685         return sprintf(buf, "%llu\n", sta);
686 }
687 static DEVICE_ATTR_RO(status);
688
689 static int acpi_device_setup_files(struct acpi_device *dev)
690 {
691         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
692         acpi_status status;
693         int result = 0;
694
695         /*
696          * Devices gotten from FADT don't have a "path" attribute
697          */
698         if (dev->handle) {
699                 result = device_create_file(&dev->dev, &dev_attr_path);
700                 if (result)
701                         goto end;
702         }
703
704         if (!list_empty(&dev->pnp.ids)) {
705                 result = device_create_file(&dev->dev, &dev_attr_hid);
706                 if (result)
707                         goto end;
708
709                 result = device_create_file(&dev->dev, &dev_attr_modalias);
710                 if (result)
711                         goto end;
712         }
713
714         /*
715          * If device has _STR, 'description' file is created
716          */
717         if (acpi_has_method(dev->handle, "_STR")) {
718                 status = acpi_evaluate_object(dev->handle, "_STR",
719                                         NULL, &buffer);
720                 if (ACPI_FAILURE(status))
721                         buffer.pointer = NULL;
722                 dev->pnp.str_obj = buffer.pointer;
723                 result = device_create_file(&dev->dev, &dev_attr_description);
724                 if (result)
725                         goto end;
726         }
727
728         if (dev->pnp.type.bus_address)
729                 result = device_create_file(&dev->dev, &dev_attr_adr);
730         if (dev->pnp.unique_id)
731                 result = device_create_file(&dev->dev, &dev_attr_uid);
732
733         if (acpi_has_method(dev->handle, "_SUN")) {
734                 result = device_create_file(&dev->dev, &dev_attr_sun);
735                 if (result)
736                         goto end;
737         }
738
739         if (acpi_has_method(dev->handle, "_STA")) {
740                 result = device_create_file(&dev->dev, &dev_attr_status);
741                 if (result)
742                         goto end;
743         }
744
745         /*
746          * If device has _EJ0, 'eject' file is created that is used to trigger
747          * hot-removal function from userland.
748          */
749         if (acpi_has_method(dev->handle, "_EJ0")) {
750                 result = device_create_file(&dev->dev, &dev_attr_eject);
751                 if (result)
752                         return result;
753         }
754
755         if (dev->flags.power_manageable) {
756                 result = device_create_file(&dev->dev, &dev_attr_power_state);
757                 if (result)
758                         return result;
759
760                 if (dev->power.flags.power_resources)
761                         result = device_create_file(&dev->dev,
762                                                     &dev_attr_real_power_state);
763         }
764
765 end:
766         return result;
767 }
768
769 static void acpi_device_remove_files(struct acpi_device *dev)
770 {
771         if (dev->flags.power_manageable) {
772                 device_remove_file(&dev->dev, &dev_attr_power_state);
773                 if (dev->power.flags.power_resources)
774                         device_remove_file(&dev->dev,
775                                            &dev_attr_real_power_state);
776         }
777
778         /*
779          * If device has _STR, remove 'description' file
780          */
781         if (acpi_has_method(dev->handle, "_STR")) {
782                 kfree(dev->pnp.str_obj);
783                 device_remove_file(&dev->dev, &dev_attr_description);
784         }
785         /*
786          * If device has _EJ0, remove 'eject' file.
787          */
788         if (acpi_has_method(dev->handle, "_EJ0"))
789                 device_remove_file(&dev->dev, &dev_attr_eject);
790
791         if (acpi_has_method(dev->handle, "_SUN"))
792                 device_remove_file(&dev->dev, &dev_attr_sun);
793
794         if (dev->pnp.unique_id)
795                 device_remove_file(&dev->dev, &dev_attr_uid);
796         if (dev->pnp.type.bus_address)
797                 device_remove_file(&dev->dev, &dev_attr_adr);
798         device_remove_file(&dev->dev, &dev_attr_modalias);
799         device_remove_file(&dev->dev, &dev_attr_hid);
800         if (acpi_has_method(dev->handle, "_STA"))
801                 device_remove_file(&dev->dev, &dev_attr_status);
802         if (dev->handle)
803                 device_remove_file(&dev->dev, &dev_attr_path);
804 }
805 /* --------------------------------------------------------------------------
806                         ACPI Bus operations
807    -------------------------------------------------------------------------- */
808
809 static const struct acpi_device_id *__acpi_match_device(
810         struct acpi_device *device, const struct acpi_device_id *ids)
811 {
812         const struct acpi_device_id *id;
813         struct acpi_hardware_id *hwid;
814
815         /*
816          * If the device is not present, it is unnecessary to load device
817          * driver for it.
818          */
819         if (!device->status.present)
820                 return NULL;
821
822         for (id = ids; id->id[0]; id++)
823                 list_for_each_entry(hwid, &device->pnp.ids, list)
824                         if (!strcmp((char *) id->id, hwid->id))
825                                 return id;
826
827         return NULL;
828 }
829
830 /**
831  * acpi_match_device - Match a struct device against a given list of ACPI IDs
832  * @ids: Array of struct acpi_device_id object to match against.
833  * @dev: The device structure to match.
834  *
835  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
836  * object for that handle and use that object to match against a given list of
837  * device IDs.
838  *
839  * Return a pointer to the first matching ID on success or %NULL on failure.
840  */
841 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
842                                                const struct device *dev)
843 {
844         struct acpi_device *adev;
845         acpi_handle handle = ACPI_HANDLE(dev);
846
847         if (!ids || !handle || acpi_bus_get_device(handle, &adev))
848                 return NULL;
849
850         return __acpi_match_device(adev, ids);
851 }
852 EXPORT_SYMBOL_GPL(acpi_match_device);
853
854 int acpi_match_device_ids(struct acpi_device *device,
855                           const struct acpi_device_id *ids)
856 {
857         return __acpi_match_device(device, ids) ? 0 : -ENOENT;
858 }
859 EXPORT_SYMBOL(acpi_match_device_ids);
860
861 static void acpi_free_power_resources_lists(struct acpi_device *device)
862 {
863         int i;
864
865         if (device->wakeup.flags.valid)
866                 acpi_power_resources_list_free(&device->wakeup.resources);
867
868         if (!device->flags.power_manageable)
869                 return;
870
871         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
872                 struct acpi_device_power_state *ps = &device->power.states[i];
873                 acpi_power_resources_list_free(&ps->resources);
874         }
875 }
876
877 static void acpi_device_release(struct device *dev)
878 {
879         struct acpi_device *acpi_dev = to_acpi_device(dev);
880
881         acpi_free_pnp_ids(&acpi_dev->pnp);
882         acpi_free_power_resources_lists(acpi_dev);
883         kfree(acpi_dev);
884 }
885
886 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
887 {
888         struct acpi_device *acpi_dev = to_acpi_device(dev);
889         struct acpi_driver *acpi_drv = to_acpi_driver(drv);
890
891         return acpi_dev->flags.match_driver
892                 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
893 }
894
895 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
896 {
897         struct acpi_device *acpi_dev = to_acpi_device(dev);
898         int len;
899
900         if (list_empty(&acpi_dev->pnp.ids))
901                 return 0;
902
903         if (add_uevent_var(env, "MODALIAS="))
904                 return -ENOMEM;
905         len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
906                               sizeof(env->buf) - env->buflen);
907         if (len <= 0)
908                 return len;
909         env->buflen += len;
910         return 0;
911 }
912
913 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
914 {
915         struct acpi_device *device = data;
916
917         device->driver->ops.notify(device, event);
918 }
919
920 static void acpi_device_notify_fixed(void *data)
921 {
922         struct acpi_device *device = data;
923
924         /* Fixed hardware devices have no handles */
925         acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
926 }
927
928 static acpi_status acpi_device_fixed_event(void *data)
929 {
930         acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_device_notify_fixed, data);
931         return AE_OK;
932 }
933
934 static int acpi_device_install_notify_handler(struct acpi_device *device)
935 {
936         acpi_status status;
937
938         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
939                 status =
940                     acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
941                                                      acpi_device_fixed_event,
942                                                      device);
943         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
944                 status =
945                     acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
946                                                      acpi_device_fixed_event,
947                                                      device);
948         else
949                 status = acpi_install_notify_handler(device->handle,
950                                                      ACPI_DEVICE_NOTIFY,
951                                                      acpi_device_notify,
952                                                      device);
953
954         if (ACPI_FAILURE(status))
955                 return -EINVAL;
956         return 0;
957 }
958
959 static void acpi_device_remove_notify_handler(struct acpi_device *device)
960 {
961         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
962                 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
963                                                 acpi_device_fixed_event);
964         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
965                 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
966                                                 acpi_device_fixed_event);
967         else
968                 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
969                                            acpi_device_notify);
970 }
971
972 static int acpi_device_probe(struct device *dev)
973 {
974         struct acpi_device *acpi_dev = to_acpi_device(dev);
975         struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
976         int ret;
977
978         if (acpi_dev->handler)
979                 return -EINVAL;
980
981         if (!acpi_drv->ops.add)
982                 return -ENOSYS;
983
984         ret = acpi_drv->ops.add(acpi_dev);
985         if (ret)
986                 return ret;
987
988         acpi_dev->driver = acpi_drv;
989         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
990                           "Driver [%s] successfully bound to device [%s]\n",
991                           acpi_drv->name, acpi_dev->pnp.bus_id));
992
993         if (acpi_drv->ops.notify) {
994                 ret = acpi_device_install_notify_handler(acpi_dev);
995                 if (ret) {
996                         if (acpi_drv->ops.remove)
997                                 acpi_drv->ops.remove(acpi_dev);
998
999                         acpi_dev->driver = NULL;
1000                         acpi_dev->driver_data = NULL;
1001                         return ret;
1002                 }
1003         }
1004
1005         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1006                           acpi_drv->name, acpi_dev->pnp.bus_id));
1007         get_device(dev);
1008         return 0;
1009 }
1010
1011 static int acpi_device_remove(struct device * dev)
1012 {
1013         struct acpi_device *acpi_dev = to_acpi_device(dev);
1014         struct acpi_driver *acpi_drv = acpi_dev->driver;
1015
1016         if (acpi_drv) {
1017                 if (acpi_drv->ops.notify)
1018                         acpi_device_remove_notify_handler(acpi_dev);
1019                 if (acpi_drv->ops.remove)
1020                         acpi_drv->ops.remove(acpi_dev);
1021         }
1022         acpi_dev->driver = NULL;
1023         acpi_dev->driver_data = NULL;
1024
1025         put_device(dev);
1026         return 0;
1027 }
1028
1029 struct bus_type acpi_bus_type = {
1030         .name           = "acpi",
1031         .match          = acpi_bus_match,
1032         .probe          = acpi_device_probe,
1033         .remove         = acpi_device_remove,
1034         .uevent         = acpi_device_uevent,
1035 };
1036
1037 static void acpi_device_del(struct acpi_device *device)
1038 {
1039         mutex_lock(&acpi_device_lock);
1040         if (device->parent)
1041                 list_del(&device->node);
1042
1043         list_del(&device->wakeup_list);
1044         mutex_unlock(&acpi_device_lock);
1045
1046         acpi_power_add_remove_device(device, false);
1047         acpi_device_remove_files(device);
1048         if (device->remove)
1049                 device->remove(device);
1050
1051         device_del(&device->dev);
1052 }
1053
1054 static LIST_HEAD(acpi_device_del_list);
1055 static DEFINE_MUTEX(acpi_device_del_lock);
1056
1057 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1058 {
1059         for (;;) {
1060                 struct acpi_device *adev;
1061
1062                 mutex_lock(&acpi_device_del_lock);
1063
1064                 if (list_empty(&acpi_device_del_list)) {
1065                         mutex_unlock(&acpi_device_del_lock);
1066                         break;
1067                 }
1068                 adev = list_first_entry(&acpi_device_del_list,
1069                                         struct acpi_device, del_list);
1070                 list_del(&adev->del_list);
1071
1072                 mutex_unlock(&acpi_device_del_lock);
1073
1074                 acpi_device_del(adev);
1075                 /*
1076                  * Drop references to all power resources that might have been
1077                  * used by the device.
1078                  */
1079                 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1080                 put_device(&adev->dev);
1081         }
1082 }
1083
1084 /**
1085  * acpi_scan_drop_device - Drop an ACPI device object.
1086  * @handle: Handle of an ACPI namespace node, not used.
1087  * @context: Address of the ACPI device object to drop.
1088  *
1089  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1090  * namespace node the device object pointed to by @context is attached to.
1091  *
1092  * The unregistration is carried out asynchronously to avoid running
1093  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1094  * ensure the correct ordering (the device objects must be unregistered in the
1095  * same order in which the corresponding namespace nodes are deleted).
1096  */
1097 static void acpi_scan_drop_device(acpi_handle handle, void *context)
1098 {
1099         static DECLARE_WORK(work, acpi_device_del_work_fn);
1100         struct acpi_device *adev = context;
1101
1102         mutex_lock(&acpi_device_del_lock);
1103
1104         /*
1105          * Use the ACPI hotplug workqueue which is ordered, so this work item
1106          * won't run after any hotplug work items submitted subsequently.  That
1107          * prevents attempts to register device objects identical to those being
1108          * deleted from happening concurrently (such attempts result from
1109          * hotplug events handled via the ACPI hotplug workqueue).  It also will
1110          * run after all of the work items submitted previosuly, which helps
1111          * those work items to ensure that they are not accessing stale device
1112          * objects.
1113          */
1114         if (list_empty(&acpi_device_del_list))
1115                 acpi_queue_hotplug_work(&work);
1116
1117         list_add_tail(&adev->del_list, &acpi_device_del_list);
1118         /* Make acpi_ns_validate_handle() return NULL for this handle. */
1119         adev->handle = INVALID_ACPI_HANDLE;
1120
1121         mutex_unlock(&acpi_device_del_lock);
1122 }
1123
1124 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1125 {
1126         acpi_status status;
1127
1128         if (!device)
1129                 return -EINVAL;
1130
1131         status = acpi_get_data(handle, acpi_scan_drop_device, (void **)device);
1132         if (ACPI_FAILURE(status) || !*device) {
1133                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1134                                   handle));
1135                 return -ENODEV;
1136         }
1137         return 0;
1138 }
1139 EXPORT_SYMBOL(acpi_bus_get_device);
1140
1141 int acpi_device_add(struct acpi_device *device,
1142                     void (*release)(struct device *))
1143 {
1144         int result;
1145         struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1146         int found = 0;
1147
1148         if (device->handle) {
1149                 acpi_status status;
1150
1151                 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
1152                                           device);
1153                 if (ACPI_FAILURE(status)) {
1154                         acpi_handle_err(device->handle,
1155                                         "Unable to attach device data\n");
1156                         return -ENODEV;
1157                 }
1158         }
1159
1160         /*
1161          * Linkage
1162          * -------
1163          * Link this device to its parent and siblings.
1164          */
1165         INIT_LIST_HEAD(&device->children);
1166         INIT_LIST_HEAD(&device->node);
1167         INIT_LIST_HEAD(&device->wakeup_list);
1168         INIT_LIST_HEAD(&device->physical_node_list);
1169         INIT_LIST_HEAD(&device->del_list);
1170         mutex_init(&device->physical_node_lock);
1171
1172         new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1173         if (!new_bus_id) {
1174                 pr_err(PREFIX "Memory allocation error\n");
1175                 result = -ENOMEM;
1176                 goto err_detach;
1177         }
1178
1179         mutex_lock(&acpi_device_lock);
1180         /*
1181          * Find suitable bus_id and instance number in acpi_bus_id_list
1182          * If failed, create one and link it into acpi_bus_id_list
1183          */
1184         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1185                 if (!strcmp(acpi_device_bus_id->bus_id,
1186                             acpi_device_hid(device))) {
1187                         acpi_device_bus_id->instance_no++;
1188                         found = 1;
1189                         kfree(new_bus_id);
1190                         break;
1191                 }
1192         }
1193         if (!found) {
1194                 acpi_device_bus_id = new_bus_id;
1195                 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1196                 acpi_device_bus_id->instance_no = 0;
1197                 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1198         }
1199         dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1200
1201         if (device->parent)
1202                 list_add_tail(&device->node, &device->parent->children);
1203
1204         if (device->wakeup.flags.valid)
1205                 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1206         mutex_unlock(&acpi_device_lock);
1207
1208         if (device->parent)
1209                 device->dev.parent = &device->parent->dev;
1210         device->dev.bus = &acpi_bus_type;
1211         device->dev.release = release;
1212         result = device_add(&device->dev);
1213         if (result) {
1214                 dev_err(&device->dev, "Error registering device\n");
1215                 goto err;
1216         }
1217
1218         result = acpi_device_setup_files(device);
1219         if (result)
1220                 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1221                        dev_name(&device->dev));
1222
1223         return 0;
1224
1225  err:
1226         mutex_lock(&acpi_device_lock);
1227         if (device->parent)
1228                 list_del(&device->node);
1229         list_del(&device->wakeup_list);
1230         mutex_unlock(&acpi_device_lock);
1231
1232  err_detach:
1233         acpi_detach_data(device->handle, acpi_scan_drop_device);
1234         return result;
1235 }
1236
1237 /* --------------------------------------------------------------------------
1238                                  Driver Management
1239    -------------------------------------------------------------------------- */
1240 /**
1241  * acpi_bus_register_driver - register a driver with the ACPI bus
1242  * @driver: driver being registered
1243  *
1244  * Registers a driver with the ACPI bus.  Searches the namespace for all
1245  * devices that match the driver's criteria and binds.  Returns zero for
1246  * success or a negative error status for failure.
1247  */
1248 int acpi_bus_register_driver(struct acpi_driver *driver)
1249 {
1250         int ret;
1251
1252         if (acpi_disabled)
1253                 return -ENODEV;
1254         driver->drv.name = driver->name;
1255         driver->drv.bus = &acpi_bus_type;
1256         driver->drv.owner = driver->owner;
1257
1258         ret = driver_register(&driver->drv);
1259         return ret;
1260 }
1261
1262 EXPORT_SYMBOL(acpi_bus_register_driver);
1263
1264 /**
1265  * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1266  * @driver: driver to unregister
1267  *
1268  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
1269  * devices that match the driver's criteria and unbinds.
1270  */
1271 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1272 {
1273         driver_unregister(&driver->drv);
1274 }
1275
1276 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1277
1278 /* --------------------------------------------------------------------------
1279                                  Device Enumeration
1280    -------------------------------------------------------------------------- */
1281 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1282 {
1283         struct acpi_device *device = NULL;
1284         acpi_status status;
1285
1286         /*
1287          * Fixed hardware devices do not appear in the namespace and do not
1288          * have handles, but we fabricate acpi_devices for them, so we have
1289          * to deal with them specially.
1290          */
1291         if (!handle)
1292                 return acpi_root;
1293
1294         do {
1295                 status = acpi_get_parent(handle, &handle);
1296                 if (ACPI_FAILURE(status))
1297                         return status == AE_NULL_ENTRY ? NULL : acpi_root;
1298         } while (acpi_bus_get_device(handle, &device));
1299         return device;
1300 }
1301
1302 acpi_status
1303 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1304 {
1305         acpi_status status;
1306         acpi_handle tmp;
1307         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1308         union acpi_object *obj;
1309
1310         status = acpi_get_handle(handle, "_EJD", &tmp);
1311         if (ACPI_FAILURE(status))
1312                 return status;
1313
1314         status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1315         if (ACPI_SUCCESS(status)) {
1316                 obj = buffer.pointer;
1317                 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1318                                          ejd);
1319                 kfree(buffer.pointer);
1320         }
1321         return status;
1322 }
1323 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1324
1325 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1326                                         struct acpi_device_wakeup *wakeup)
1327 {
1328         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1329         union acpi_object *package = NULL;
1330         union acpi_object *element = NULL;
1331         acpi_status status;
1332         int err = -ENODATA;
1333
1334         if (!wakeup)
1335                 return -EINVAL;
1336
1337         INIT_LIST_HEAD(&wakeup->resources);
1338
1339         /* _PRW */
1340         status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1341         if (ACPI_FAILURE(status)) {
1342                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1343                 return err;
1344         }
1345
1346         package = (union acpi_object *)buffer.pointer;
1347
1348         if (!package || package->package.count < 2)
1349                 goto out;
1350
1351         element = &(package->package.elements[0]);
1352         if (!element)
1353                 goto out;
1354
1355         if (element->type == ACPI_TYPE_PACKAGE) {
1356                 if ((element->package.count < 2) ||
1357                     (element->package.elements[0].type !=
1358                      ACPI_TYPE_LOCAL_REFERENCE)
1359                     || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1360                         goto out;
1361
1362                 wakeup->gpe_device =
1363                     element->package.elements[0].reference.handle;
1364                 wakeup->gpe_number =
1365                     (u32) element->package.elements[1].integer.value;
1366         } else if (element->type == ACPI_TYPE_INTEGER) {
1367                 wakeup->gpe_device = NULL;
1368                 wakeup->gpe_number = element->integer.value;
1369         } else {
1370                 goto out;
1371         }
1372
1373         element = &(package->package.elements[1]);
1374         if (element->type != ACPI_TYPE_INTEGER)
1375                 goto out;
1376
1377         wakeup->sleep_state = element->integer.value;
1378
1379         err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1380         if (err)
1381                 goto out;
1382
1383         if (!list_empty(&wakeup->resources)) {
1384                 int sleep_state;
1385
1386                 err = acpi_power_wakeup_list_init(&wakeup->resources,
1387                                                   &sleep_state);
1388                 if (err) {
1389                         acpi_handle_warn(handle, "Retrieving current states "
1390                                          "of wakeup power resources failed\n");
1391                         acpi_power_resources_list_free(&wakeup->resources);
1392                         goto out;
1393                 }
1394                 if (sleep_state < wakeup->sleep_state) {
1395                         acpi_handle_warn(handle, "Overriding _PRW sleep state "
1396                                          "(S%d) by S%d from power resources\n",
1397                                          (int)wakeup->sleep_state, sleep_state);
1398                         wakeup->sleep_state = sleep_state;
1399                 }
1400         }
1401         acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1402
1403  out:
1404         kfree(buffer.pointer);
1405         return err;
1406 }
1407
1408 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1409 {
1410         struct acpi_device_id button_device_ids[] = {
1411                 {"PNP0C0C", 0},
1412                 {"PNP0C0D", 0},
1413                 {"PNP0C0E", 0},
1414                 {"", 0},
1415         };
1416         acpi_status status;
1417         acpi_event_status event_status;
1418
1419         device->wakeup.flags.notifier_present = 0;
1420
1421         /* Power button, Lid switch always enable wakeup */
1422         if (!acpi_match_device_ids(device, button_device_ids)) {
1423                 device->wakeup.flags.run_wake = 1;
1424                 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1425                         /* Do not use Lid/sleep button for S5 wakeup */
1426                         if (device->wakeup.sleep_state == ACPI_STATE_S5)
1427                                 device->wakeup.sleep_state = ACPI_STATE_S4;
1428                 }
1429                 device_set_wakeup_capable(&device->dev, true);
1430                 return;
1431         }
1432
1433         status = acpi_get_gpe_status(device->wakeup.gpe_device,
1434                                         device->wakeup.gpe_number,
1435                                                 &event_status);
1436         if (status == AE_OK)
1437                 device->wakeup.flags.run_wake =
1438                                 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1439 }
1440
1441 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1442 {
1443         int err;
1444
1445         /* Presence of _PRW indicates wake capable */
1446         if (!acpi_has_method(device->handle, "_PRW"))
1447                 return;
1448
1449         err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1450                                                            &device->wakeup);
1451         if (err) {
1452                 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1453                 return;
1454         }
1455
1456         device->wakeup.flags.valid = 1;
1457         device->wakeup.prepare_count = 0;
1458         acpi_bus_set_run_wake_flags(device);
1459         /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1460          * system for the ACPI device with the _PRW object.
1461          * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1462          * So it is necessary to call _DSW object first. Only when it is not
1463          * present will the _PSW object used.
1464          */
1465         err = acpi_device_sleep_wake(device, 0, 0, 0);
1466         if (err)
1467                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1468                                 "error in _DSW or _PSW evaluation\n"));
1469 }
1470
1471 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1472 {
1473         struct acpi_device_power_state *ps = &device->power.states[state];
1474         char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1475         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1476         acpi_status status;
1477
1478         INIT_LIST_HEAD(&ps->resources);
1479
1480         /* Evaluate "_PRx" to get referenced power resources */
1481         status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1482         if (ACPI_SUCCESS(status)) {
1483                 union acpi_object *package = buffer.pointer;
1484
1485                 if (buffer.length && package
1486                     && package->type == ACPI_TYPE_PACKAGE
1487                     && package->package.count) {
1488                         int err = acpi_extract_power_resources(package, 0,
1489                                                                &ps->resources);
1490                         if (!err)
1491                                 device->power.flags.power_resources = 1;
1492                 }
1493                 ACPI_FREE(buffer.pointer);
1494         }
1495
1496         /* Evaluate "_PSx" to see if we can do explicit sets */
1497         pathname[2] = 'S';
1498         if (acpi_has_method(device->handle, pathname))
1499                 ps->flags.explicit_set = 1;
1500
1501         /*
1502          * State is valid if there are means to put the device into it.
1503          * D3hot is only valid if _PR3 present.
1504          */
1505         if (!list_empty(&ps->resources)
1506             || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1507                 ps->flags.valid = 1;
1508                 ps->flags.os_accessible = 1;
1509         }
1510
1511         ps->power = -1;         /* Unknown - driver assigned */
1512         ps->latency = -1;       /* Unknown - driver assigned */
1513 }
1514
1515 static void acpi_bus_get_power_flags(struct acpi_device *device)
1516 {
1517         u32 i;
1518
1519         /* Presence of _PS0|_PR0 indicates 'power manageable' */
1520         if (!acpi_has_method(device->handle, "_PS0") &&
1521             !acpi_has_method(device->handle, "_PR0"))
1522                 return;
1523
1524         device->flags.power_manageable = 1;
1525
1526         /*
1527          * Power Management Flags
1528          */
1529         if (acpi_has_method(device->handle, "_PSC"))
1530                 device->power.flags.explicit_get = 1;
1531         if (acpi_has_method(device->handle, "_IRC"))
1532                 device->power.flags.inrush_current = 1;
1533
1534         /*
1535          * Enumerate supported power management states
1536          */
1537         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1538                 acpi_bus_init_power_state(device, i);
1539
1540         INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1541
1542         /* Set defaults for D0 and D3 states (always valid) */
1543         device->power.states[ACPI_STATE_D0].flags.valid = 1;
1544         device->power.states[ACPI_STATE_D0].power = 100;
1545         device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1546         device->power.states[ACPI_STATE_D3_COLD].power = 0;
1547
1548         /* Set D3cold's explicit_set flag if _PS3 exists. */
1549         if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1550                 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1551
1552         /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1553         if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1554                         device->power.flags.power_resources)
1555                 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1556
1557         if (acpi_bus_init_power(device)) {
1558                 acpi_free_power_resources_lists(device);
1559                 device->flags.power_manageable = 0;
1560         }
1561 }
1562
1563 static void acpi_bus_get_flags(struct acpi_device *device)
1564 {
1565         /* Presence of _STA indicates 'dynamic_status' */
1566         if (acpi_has_method(device->handle, "_STA"))
1567                 device->flags.dynamic_status = 1;
1568
1569         /* Presence of _RMV indicates 'removable' */
1570         if (acpi_has_method(device->handle, "_RMV"))
1571                 device->flags.removable = 1;
1572
1573         /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1574         if (acpi_has_method(device->handle, "_EJD") ||
1575             acpi_has_method(device->handle, "_EJ0"))
1576                 device->flags.ejectable = 1;
1577 }
1578
1579 static void acpi_device_get_busid(struct acpi_device *device)
1580 {
1581         char bus_id[5] = { '?', 0 };
1582         struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1583         int i = 0;
1584
1585         /*
1586          * Bus ID
1587          * ------
1588          * The device's Bus ID is simply the object name.
1589          * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1590          */
1591         if (ACPI_IS_ROOT_DEVICE(device)) {
1592                 strcpy(device->pnp.bus_id, "ACPI");
1593                 return;
1594         }
1595
1596         switch (device->device_type) {
1597         case ACPI_BUS_TYPE_POWER_BUTTON:
1598                 strcpy(device->pnp.bus_id, "PWRF");
1599                 break;
1600         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1601                 strcpy(device->pnp.bus_id, "SLPF");
1602                 break;
1603         default:
1604                 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1605                 /* Clean up trailing underscores (if any) */
1606                 for (i = 3; i > 1; i--) {
1607                         if (bus_id[i] == '_')
1608                                 bus_id[i] = '\0';
1609                         else
1610                                 break;
1611                 }
1612                 strcpy(device->pnp.bus_id, bus_id);
1613                 break;
1614         }
1615 }
1616
1617 /*
1618  * acpi_ata_match - see if an acpi object is an ATA device
1619  *
1620  * If an acpi object has one of the ACPI ATA methods defined,
1621  * then we can safely call it an ATA device.
1622  */
1623 bool acpi_ata_match(acpi_handle handle)
1624 {
1625         return acpi_has_method(handle, "_GTF") ||
1626                acpi_has_method(handle, "_GTM") ||
1627                acpi_has_method(handle, "_STM") ||
1628                acpi_has_method(handle, "_SDD");
1629 }
1630
1631 /*
1632  * acpi_bay_match - see if an acpi object is an ejectable driver bay
1633  *
1634  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1635  * then we can safely call it an ejectable drive bay
1636  */
1637 bool acpi_bay_match(acpi_handle handle)
1638 {
1639         acpi_handle phandle;
1640
1641         if (!acpi_has_method(handle, "_EJ0"))
1642                 return false;
1643         if (acpi_ata_match(handle))
1644                 return true;
1645         if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1646                 return false;
1647
1648         return acpi_ata_match(phandle);
1649 }
1650
1651 /*
1652  * acpi_dock_match - see if an acpi object has a _DCK method
1653  */
1654 bool acpi_dock_match(acpi_handle handle)
1655 {
1656         return acpi_has_method(handle, "_DCK");
1657 }
1658
1659 const char *acpi_device_hid(struct acpi_device *device)
1660 {
1661         struct acpi_hardware_id *hid;
1662
1663         if (list_empty(&device->pnp.ids))
1664                 return dummy_hid;
1665
1666         hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1667         return hid->id;
1668 }
1669 EXPORT_SYMBOL(acpi_device_hid);
1670
1671 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1672 {
1673         struct acpi_hardware_id *id;
1674
1675         id = kmalloc(sizeof(*id), GFP_KERNEL);
1676         if (!id)
1677                 return;
1678
1679         id->id = kstrdup(dev_id, GFP_KERNEL);
1680         if (!id->id) {
1681                 kfree(id);
1682                 return;
1683         }
1684
1685         list_add_tail(&id->list, &pnp->ids);
1686         pnp->type.hardware_id = 1;
1687 }
1688
1689 /*
1690  * Old IBM workstations have a DSDT bug wherein the SMBus object
1691  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1692  * prefix.  Work around this.
1693  */
1694 static bool acpi_ibm_smbus_match(acpi_handle handle)
1695 {
1696         char node_name[ACPI_PATH_SEGMENT_LENGTH];
1697         struct acpi_buffer path = { sizeof(node_name), node_name };
1698
1699         if (!dmi_name_in_vendors("IBM"))
1700                 return false;
1701
1702         /* Look for SMBS object */
1703         if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1704             strcmp("SMBS", path.pointer))
1705                 return false;
1706
1707         /* Does it have the necessary (but misnamed) methods? */
1708         if (acpi_has_method(handle, "SBI") &&
1709             acpi_has_method(handle, "SBR") &&
1710             acpi_has_method(handle, "SBW"))
1711                 return true;
1712
1713         return false;
1714 }
1715
1716 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1717                                 int device_type)
1718 {
1719         acpi_status status;
1720         struct acpi_device_info *info;
1721         struct acpi_pnp_device_id_list *cid_list;
1722         int i;
1723
1724         switch (device_type) {
1725         case ACPI_BUS_TYPE_DEVICE:
1726                 if (handle == ACPI_ROOT_OBJECT) {
1727                         acpi_add_id(pnp, ACPI_SYSTEM_HID);
1728                         break;
1729                 }
1730
1731                 status = acpi_get_object_info(handle, &info);
1732                 if (ACPI_FAILURE(status)) {
1733                         pr_err(PREFIX "%s: Error reading device info\n",
1734                                         __func__);
1735                         return;
1736                 }
1737
1738                 if (info->valid & ACPI_VALID_HID)
1739                         acpi_add_id(pnp, info->hardware_id.string);
1740                 if (info->valid & ACPI_VALID_CID) {
1741                         cid_list = &info->compatible_id_list;
1742                         for (i = 0; i < cid_list->count; i++)
1743                                 acpi_add_id(pnp, cid_list->ids[i].string);
1744                 }
1745                 if (info->valid & ACPI_VALID_ADR) {
1746                         pnp->bus_address = info->address;
1747                         pnp->type.bus_address = 1;
1748                 }
1749                 if (info->valid & ACPI_VALID_UID)
1750                         pnp->unique_id = kstrdup(info->unique_id.string,
1751                                                         GFP_KERNEL);
1752
1753                 kfree(info);
1754
1755                 /*
1756                  * Some devices don't reliably have _HIDs & _CIDs, so add
1757                  * synthetic HIDs to make sure drivers can find them.
1758                  */
1759                 if (acpi_is_video_device(handle))
1760                         acpi_add_id(pnp, ACPI_VIDEO_HID);
1761                 else if (acpi_bay_match(handle))
1762                         acpi_add_id(pnp, ACPI_BAY_HID);
1763                 else if (acpi_dock_match(handle))
1764                         acpi_add_id(pnp, ACPI_DOCK_HID);
1765                 else if (acpi_ibm_smbus_match(handle))
1766                         acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1767                 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1768                         acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1769                         strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1770                         strcpy(pnp->device_class, ACPI_BUS_CLASS);
1771                 }
1772
1773                 break;
1774         case ACPI_BUS_TYPE_POWER:
1775                 acpi_add_id(pnp, ACPI_POWER_HID);
1776                 break;
1777         case ACPI_BUS_TYPE_PROCESSOR:
1778                 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1779                 break;
1780         case ACPI_BUS_TYPE_THERMAL:
1781                 acpi_add_id(pnp, ACPI_THERMAL_HID);
1782                 break;
1783         case ACPI_BUS_TYPE_POWER_BUTTON:
1784                 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1785                 break;
1786         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1787                 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1788                 break;
1789         }
1790 }
1791
1792 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1793 {
1794         struct acpi_hardware_id *id, *tmp;
1795
1796         list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1797                 kfree(id->id);
1798                 kfree(id);
1799         }
1800         kfree(pnp->unique_id);
1801 }
1802
1803 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1804                              int type, unsigned long long sta)
1805 {
1806         INIT_LIST_HEAD(&device->pnp.ids);
1807         device->device_type = type;
1808         device->handle = handle;
1809         device->parent = acpi_bus_get_parent(handle);
1810         acpi_set_device_status(device, sta);
1811         acpi_device_get_busid(device);
1812         acpi_set_pnp_ids(handle, &device->pnp, type);
1813         acpi_bus_get_flags(device);
1814         device->flags.match_driver = false;
1815         device->flags.initialized = true;
1816         device->flags.visited = false;
1817         device_initialize(&device->dev);
1818         dev_set_uevent_suppress(&device->dev, true);
1819 }
1820
1821 void acpi_device_add_finalize(struct acpi_device *device)
1822 {
1823         dev_set_uevent_suppress(&device->dev, false);
1824         kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1825 }
1826
1827 static int acpi_add_single_object(struct acpi_device **child,
1828                                   acpi_handle handle, int type,
1829                                   unsigned long long sta)
1830 {
1831         int result;
1832         struct acpi_device *device;
1833         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1834
1835         device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1836         if (!device) {
1837                 printk(KERN_ERR PREFIX "Memory allocation error\n");
1838                 return -ENOMEM;
1839         }
1840
1841         acpi_init_device_object(device, handle, type, sta);
1842         acpi_bus_get_power_flags(device);
1843         acpi_bus_get_wakeup_device_flags(device);
1844
1845         result = acpi_device_add(device, acpi_device_release);
1846         if (result) {
1847                 acpi_device_release(&device->dev);
1848                 return result;
1849         }
1850
1851         acpi_power_add_remove_device(device, true);
1852         acpi_device_add_finalize(device);
1853         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1854         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1855                 dev_name(&device->dev), (char *) buffer.pointer,
1856                 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1857         kfree(buffer.pointer);
1858         *child = device;
1859         return 0;
1860 }
1861
1862 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1863                                     unsigned long long *sta)
1864 {
1865         acpi_status status;
1866         acpi_object_type acpi_type;
1867
1868         status = acpi_get_type(handle, &acpi_type);
1869         if (ACPI_FAILURE(status))
1870                 return -ENODEV;
1871
1872         switch (acpi_type) {
1873         case ACPI_TYPE_ANY:             /* for ACPI_ROOT_OBJECT */
1874         case ACPI_TYPE_DEVICE:
1875                 *type = ACPI_BUS_TYPE_DEVICE;
1876                 status = acpi_bus_get_status_handle(handle, sta);
1877                 if (ACPI_FAILURE(status))
1878                         return -ENODEV;
1879                 break;
1880         case ACPI_TYPE_PROCESSOR:
1881                 *type = ACPI_BUS_TYPE_PROCESSOR;
1882                 status = acpi_bus_get_status_handle(handle, sta);
1883                 if (ACPI_FAILURE(status))
1884                         return -ENODEV;
1885                 break;
1886         case ACPI_TYPE_THERMAL:
1887                 *type = ACPI_BUS_TYPE_THERMAL;
1888                 *sta = ACPI_STA_DEFAULT;
1889                 break;
1890         case ACPI_TYPE_POWER:
1891                 *type = ACPI_BUS_TYPE_POWER;
1892                 *sta = ACPI_STA_DEFAULT;
1893                 break;
1894         default:
1895                 return -ENODEV;
1896         }
1897
1898         return 0;
1899 }
1900
1901 bool acpi_device_is_present(struct acpi_device *adev)
1902 {
1903         if (adev->status.present || adev->status.functional)
1904                 return true;
1905
1906         adev->flags.initialized = false;
1907         return false;
1908 }
1909
1910 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1911                                        char *idstr,
1912                                        const struct acpi_device_id **matchid)
1913 {
1914         const struct acpi_device_id *devid;
1915
1916         for (devid = handler->ids; devid->id[0]; devid++)
1917                 if (!strcmp((char *)devid->id, idstr)) {
1918                         if (matchid)
1919                                 *matchid = devid;
1920
1921                         return true;
1922                 }
1923
1924         return false;
1925 }
1926
1927 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1928                                         const struct acpi_device_id **matchid)
1929 {
1930         struct acpi_scan_handler *handler;
1931
1932         list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1933                 if (acpi_scan_handler_matching(handler, idstr, matchid))
1934                         return handler;
1935
1936         return NULL;
1937 }
1938
1939 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1940 {
1941         if (!!hotplug->enabled == !!val)
1942                 return;
1943
1944         mutex_lock(&acpi_scan_lock);
1945
1946         hotplug->enabled = val;
1947
1948         mutex_unlock(&acpi_scan_lock);
1949 }
1950
1951 static void acpi_scan_init_hotplug(acpi_handle handle, int type)
1952 {
1953         struct acpi_device_pnp pnp = {};
1954         struct acpi_hardware_id *hwid;
1955         struct acpi_scan_handler *handler;
1956
1957         INIT_LIST_HEAD(&pnp.ids);
1958         acpi_set_pnp_ids(handle, &pnp, type);
1959
1960         if (!pnp.type.hardware_id)
1961                 goto out;
1962
1963         /*
1964          * This relies on the fact that acpi_install_notify_handler() will not
1965          * install the same notify handler routine twice for the same handle.
1966          */
1967         list_for_each_entry(hwid, &pnp.ids, list) {
1968                 handler = acpi_scan_match_handler(hwid->id, NULL);
1969                 if (handler) {
1970                         acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1971                                         acpi_hotplug_notify_cb, handler);
1972                         break;
1973                 }
1974         }
1975
1976 out:
1977         acpi_free_pnp_ids(&pnp);
1978 }
1979
1980 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1981                                       void *not_used, void **return_value)
1982 {
1983         struct acpi_device *device = NULL;
1984         int type;
1985         unsigned long long sta;
1986         int result;
1987
1988         acpi_bus_get_device(handle, &device);
1989         if (device)
1990                 goto out;
1991
1992         result = acpi_bus_type_and_status(handle, &type, &sta);
1993         if (result)
1994                 return AE_OK;
1995
1996         if (type == ACPI_BUS_TYPE_POWER) {
1997                 acpi_add_power_resource(handle);
1998                 return AE_OK;
1999         }
2000
2001         acpi_scan_init_hotplug(handle, type);
2002
2003         acpi_add_single_object(&device, handle, type, sta);
2004         if (!device)
2005                 return AE_CTRL_DEPTH;
2006
2007  out:
2008         if (!*return_value)
2009                 *return_value = device;
2010
2011         return AE_OK;
2012 }
2013
2014 static int acpi_scan_attach_handler(struct acpi_device *device)
2015 {
2016         struct acpi_hardware_id *hwid;
2017         int ret = 0;
2018
2019         list_for_each_entry(hwid, &device->pnp.ids, list) {
2020                 const struct acpi_device_id *devid;
2021                 struct acpi_scan_handler *handler;
2022
2023                 handler = acpi_scan_match_handler(hwid->id, &devid);
2024                 if (handler) {
2025                         ret = handler->attach(device, devid);
2026                         if (ret > 0) {
2027                                 device->handler = handler;
2028                                 break;
2029                         } else if (ret < 0) {
2030                                 break;
2031                         }
2032                 }
2033         }
2034         return ret;
2035 }
2036
2037 static void acpi_bus_attach(struct acpi_device *device)
2038 {
2039         struct acpi_device *child;
2040         int ret;
2041
2042         acpi_bus_get_status(device);
2043         /* Skip devices that are not present. */
2044         if (!acpi_device_is_present(device)) {
2045                 device->flags.visited = false;
2046                 return;
2047         }
2048         if (device->handler)
2049                 goto ok;
2050
2051         if (!device->flags.initialized) {
2052                 acpi_bus_update_power(device, NULL);
2053                 device->flags.initialized = true;
2054         }
2055         device->flags.visited = false;
2056         ret = acpi_scan_attach_handler(device);
2057         if (ret < 0)
2058                 return;
2059
2060         device->flags.match_driver = true;
2061         if (!ret) {
2062                 ret = device_attach(&device->dev);
2063                 if (ret < 0)
2064                         return;
2065         }
2066         device->flags.visited = true;
2067
2068  ok:
2069         list_for_each_entry(child, &device->children, node)
2070                 acpi_bus_attach(child);
2071
2072         if (device->handler && device->handler->hotplug.notify_online)
2073                 device->handler->hotplug.notify_online(device);
2074 }
2075
2076 /**
2077  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2078  * @handle: Root of the namespace scope to scan.
2079  *
2080  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2081  * found devices.
2082  *
2083  * If no devices were found, -ENODEV is returned, but it does not mean that
2084  * there has been a real error.  There just have been no suitable ACPI objects
2085  * in the table trunk from which the kernel could create a device and add an
2086  * appropriate driver.
2087  *
2088  * Must be called under acpi_scan_lock.
2089  */
2090 int acpi_bus_scan(acpi_handle handle)
2091 {
2092         void *device = NULL;
2093
2094         if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
2095                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2096                                     acpi_bus_check_add, NULL, NULL, &device);
2097
2098         if (device) {
2099                 acpi_bus_attach(device);
2100                 return 0;
2101         }
2102         return -ENODEV;
2103 }
2104 EXPORT_SYMBOL(acpi_bus_scan);
2105
2106 /**
2107  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2108  * @adev: Root of the ACPI namespace scope to walk.
2109  *
2110  * Must be called under acpi_scan_lock.
2111  */
2112 void acpi_bus_trim(struct acpi_device *adev)
2113 {
2114         struct acpi_scan_handler *handler = adev->handler;
2115         struct acpi_device *child;
2116
2117         list_for_each_entry_reverse(child, &adev->children, node)
2118                 acpi_bus_trim(child);
2119
2120         adev->flags.match_driver = false;
2121         if (handler) {
2122                 if (handler->detach)
2123                         handler->detach(adev);
2124
2125                 adev->handler = NULL;
2126         } else {
2127                 device_release_driver(&adev->dev);
2128         }
2129         /*
2130          * Most likely, the device is going away, so put it into D3cold before
2131          * that.
2132          */
2133         acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2134         adev->flags.initialized = false;
2135         adev->flags.visited = false;
2136 }
2137 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2138
2139 static int acpi_bus_scan_fixed(void)
2140 {
2141         int result = 0;
2142
2143         /*
2144          * Enumerate all fixed-feature devices.
2145          */
2146         if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2147                 struct acpi_device *device = NULL;
2148
2149                 result = acpi_add_single_object(&device, NULL,
2150                                                 ACPI_BUS_TYPE_POWER_BUTTON,
2151                                                 ACPI_STA_DEFAULT);
2152                 if (result)
2153                         return result;
2154
2155                 device->flags.match_driver = true;
2156                 result = device_attach(&device->dev);
2157                 if (result < 0)
2158                         return result;
2159
2160                 device_init_wakeup(&device->dev, true);
2161         }
2162
2163         if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2164                 struct acpi_device *device = NULL;
2165
2166                 result = acpi_add_single_object(&device, NULL,
2167                                                 ACPI_BUS_TYPE_SLEEP_BUTTON,
2168                                                 ACPI_STA_DEFAULT);
2169                 if (result)
2170                         return result;
2171
2172                 device->flags.match_driver = true;
2173                 result = device_attach(&device->dev);
2174         }
2175
2176         return result < 0 ? result : 0;
2177 }
2178
2179 int __init acpi_scan_init(void)
2180 {
2181         int result;
2182
2183         result = bus_register(&acpi_bus_type);
2184         if (result) {
2185                 /* We don't want to quit even if we failed to add suspend/resume */
2186                 printk(KERN_ERR PREFIX "Could not register bus type\n");
2187         }
2188
2189         acpi_pci_root_init();
2190         acpi_pci_link_init();
2191         acpi_processor_init();
2192         acpi_platform_init();
2193         acpi_lpss_init();
2194         acpi_cmos_rtc_init();
2195         acpi_container_init();
2196         acpi_memory_hotplug_init();
2197         acpi_dock_init();
2198
2199         mutex_lock(&acpi_scan_lock);
2200         /*
2201          * Enumerate devices in the ACPI namespace.
2202          */
2203         result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2204         if (result)
2205                 goto out;
2206
2207         result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2208         if (result)
2209                 goto out;
2210
2211         result = acpi_bus_scan_fixed();
2212         if (result) {
2213                 acpi_detach_data(acpi_root->handle, acpi_scan_drop_device);
2214                 acpi_device_del(acpi_root);
2215                 put_device(&acpi_root->dev);
2216                 goto out;
2217         }
2218
2219         acpi_update_all_gpes();
2220
2221  out:
2222         mutex_unlock(&acpi_scan_lock);
2223         return result;
2224 }