33afe7887dd0437fc76bca46c8e176c31e4a2b21
[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
665         return sprintf(buf, "%lu\n", acpi_dev->pnp.sun);
666 }
667 static DEVICE_ATTR(sun, 0444, acpi_device_sun_show, NULL);
668
669 static ssize_t status_show(struct device *dev, struct device_attribute *attr,
670                                 char *buf) {
671         struct acpi_device *acpi_dev = to_acpi_device(dev);
672         acpi_status status;
673         unsigned long long sta;
674
675         status = acpi_evaluate_integer(acpi_dev->handle, "_STA", NULL, &sta);
676         if (ACPI_FAILURE(status))
677                 return -ENODEV;
678
679         return sprintf(buf, "%llu\n", sta);
680 }
681 static DEVICE_ATTR_RO(status);
682
683 static int acpi_device_setup_files(struct acpi_device *dev)
684 {
685         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
686         acpi_status status;
687         unsigned long long sun;
688         int result = 0;
689
690         /*
691          * Devices gotten from FADT don't have a "path" attribute
692          */
693         if (dev->handle) {
694                 result = device_create_file(&dev->dev, &dev_attr_path);
695                 if (result)
696                         goto end;
697         }
698
699         if (!list_empty(&dev->pnp.ids)) {
700                 result = device_create_file(&dev->dev, &dev_attr_hid);
701                 if (result)
702                         goto end;
703
704                 result = device_create_file(&dev->dev, &dev_attr_modalias);
705                 if (result)
706                         goto end;
707         }
708
709         /*
710          * If device has _STR, 'description' file is created
711          */
712         if (acpi_has_method(dev->handle, "_STR")) {
713                 status = acpi_evaluate_object(dev->handle, "_STR",
714                                         NULL, &buffer);
715                 if (ACPI_FAILURE(status))
716                         buffer.pointer = NULL;
717                 dev->pnp.str_obj = buffer.pointer;
718                 result = device_create_file(&dev->dev, &dev_attr_description);
719                 if (result)
720                         goto end;
721         }
722
723         if (dev->pnp.type.bus_address)
724                 result = device_create_file(&dev->dev, &dev_attr_adr);
725         if (dev->pnp.unique_id)
726                 result = device_create_file(&dev->dev, &dev_attr_uid);
727
728         status = acpi_evaluate_integer(dev->handle, "_SUN", NULL, &sun);
729         if (ACPI_SUCCESS(status)) {
730                 dev->pnp.sun = (unsigned long)sun;
731                 result = device_create_file(&dev->dev, &dev_attr_sun);
732                 if (result)
733                         goto end;
734         } else {
735                 dev->pnp.sun = (unsigned long)-1;
736         }
737
738         if (acpi_has_method(dev->handle, "_STA")) {
739                 result = device_create_file(&dev->dev, &dev_attr_status);
740                 if (result)
741                         goto end;
742         }
743
744         /*
745          * If device has _EJ0, 'eject' file is created that is used to trigger
746          * hot-removal function from userland.
747          */
748         if (acpi_has_method(dev->handle, "_EJ0")) {
749                 result = device_create_file(&dev->dev, &dev_attr_eject);
750                 if (result)
751                         return result;
752         }
753
754         if (dev->flags.power_manageable) {
755                 result = device_create_file(&dev->dev, &dev_attr_power_state);
756                 if (result)
757                         return result;
758
759                 if (dev->power.flags.power_resources)
760                         result = device_create_file(&dev->dev,
761                                                     &dev_attr_real_power_state);
762         }
763
764 end:
765         return result;
766 }
767
768 static void acpi_device_remove_files(struct acpi_device *dev)
769 {
770         if (dev->flags.power_manageable) {
771                 device_remove_file(&dev->dev, &dev_attr_power_state);
772                 if (dev->power.flags.power_resources)
773                         device_remove_file(&dev->dev,
774                                            &dev_attr_real_power_state);
775         }
776
777         /*
778          * If device has _STR, remove 'description' file
779          */
780         if (acpi_has_method(dev->handle, "_STR")) {
781                 kfree(dev->pnp.str_obj);
782                 device_remove_file(&dev->dev, &dev_attr_description);
783         }
784         /*
785          * If device has _EJ0, remove 'eject' file.
786          */
787         if (acpi_has_method(dev->handle, "_EJ0"))
788                 device_remove_file(&dev->dev, &dev_attr_eject);
789
790         if (acpi_has_method(dev->handle, "_SUN"))
791                 device_remove_file(&dev->dev, &dev_attr_sun);
792
793         if (dev->pnp.unique_id)
794                 device_remove_file(&dev->dev, &dev_attr_uid);
795         if (dev->pnp.type.bus_address)
796                 device_remove_file(&dev->dev, &dev_attr_adr);
797         device_remove_file(&dev->dev, &dev_attr_modalias);
798         device_remove_file(&dev->dev, &dev_attr_hid);
799         if (acpi_has_method(dev->handle, "_STA"))
800                 device_remove_file(&dev->dev, &dev_attr_status);
801         if (dev->handle)
802                 device_remove_file(&dev->dev, &dev_attr_path);
803 }
804 /* --------------------------------------------------------------------------
805                         ACPI Bus operations
806    -------------------------------------------------------------------------- */
807
808 static const struct acpi_device_id *__acpi_match_device(
809         struct acpi_device *device, const struct acpi_device_id *ids)
810 {
811         const struct acpi_device_id *id;
812         struct acpi_hardware_id *hwid;
813
814         /*
815          * If the device is not present, it is unnecessary to load device
816          * driver for it.
817          */
818         if (!device->status.present)
819                 return NULL;
820
821         for (id = ids; id->id[0]; id++)
822                 list_for_each_entry(hwid, &device->pnp.ids, list)
823                         if (!strcmp((char *) id->id, hwid->id))
824                                 return id;
825
826         return NULL;
827 }
828
829 /**
830  * acpi_match_device - Match a struct device against a given list of ACPI IDs
831  * @ids: Array of struct acpi_device_id object to match against.
832  * @dev: The device structure to match.
833  *
834  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
835  * object for that handle and use that object to match against a given list of
836  * device IDs.
837  *
838  * Return a pointer to the first matching ID on success or %NULL on failure.
839  */
840 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
841                                                const struct device *dev)
842 {
843         struct acpi_device *adev;
844         acpi_handle handle = ACPI_HANDLE(dev);
845
846         if (!ids || !handle || acpi_bus_get_device(handle, &adev))
847                 return NULL;
848
849         return __acpi_match_device(adev, ids);
850 }
851 EXPORT_SYMBOL_GPL(acpi_match_device);
852
853 int acpi_match_device_ids(struct acpi_device *device,
854                           const struct acpi_device_id *ids)
855 {
856         return __acpi_match_device(device, ids) ? 0 : -ENOENT;
857 }
858 EXPORT_SYMBOL(acpi_match_device_ids);
859
860 static void acpi_free_power_resources_lists(struct acpi_device *device)
861 {
862         int i;
863
864         if (device->wakeup.flags.valid)
865                 acpi_power_resources_list_free(&device->wakeup.resources);
866
867         if (!device->flags.power_manageable)
868                 return;
869
870         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
871                 struct acpi_device_power_state *ps = &device->power.states[i];
872                 acpi_power_resources_list_free(&ps->resources);
873         }
874 }
875
876 static void acpi_device_release(struct device *dev)
877 {
878         struct acpi_device *acpi_dev = to_acpi_device(dev);
879
880         acpi_free_pnp_ids(&acpi_dev->pnp);
881         acpi_free_power_resources_lists(acpi_dev);
882         kfree(acpi_dev);
883 }
884
885 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
886 {
887         struct acpi_device *acpi_dev = to_acpi_device(dev);
888         struct acpi_driver *acpi_drv = to_acpi_driver(drv);
889
890         return acpi_dev->flags.match_driver
891                 && !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
892 }
893
894 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
895 {
896         struct acpi_device *acpi_dev = to_acpi_device(dev);
897         int len;
898
899         if (list_empty(&acpi_dev->pnp.ids))
900                 return 0;
901
902         if (add_uevent_var(env, "MODALIAS="))
903                 return -ENOMEM;
904         len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
905                               sizeof(env->buf) - env->buflen);
906         if (len <= 0)
907                 return len;
908         env->buflen += len;
909         return 0;
910 }
911
912 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
913 {
914         struct acpi_device *device = data;
915
916         device->driver->ops.notify(device, event);
917 }
918
919 static acpi_status acpi_device_notify_fixed(void *data)
920 {
921         struct acpi_device *device = data;
922
923         /* Fixed hardware devices have no handles */
924         acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
925         return AE_OK;
926 }
927
928 static int acpi_device_install_notify_handler(struct acpi_device *device)
929 {
930         acpi_status status;
931
932         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
933                 status =
934                     acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
935                                                      acpi_device_notify_fixed,
936                                                      device);
937         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
938                 status =
939                     acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
940                                                      acpi_device_notify_fixed,
941                                                      device);
942         else
943                 status = acpi_install_notify_handler(device->handle,
944                                                      ACPI_DEVICE_NOTIFY,
945                                                      acpi_device_notify,
946                                                      device);
947
948         if (ACPI_FAILURE(status))
949                 return -EINVAL;
950         return 0;
951 }
952
953 static void acpi_device_remove_notify_handler(struct acpi_device *device)
954 {
955         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
956                 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
957                                                 acpi_device_notify_fixed);
958         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
959                 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
960                                                 acpi_device_notify_fixed);
961         else
962                 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
963                                            acpi_device_notify);
964 }
965
966 static int acpi_device_probe(struct device *dev)
967 {
968         struct acpi_device *acpi_dev = to_acpi_device(dev);
969         struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
970         int ret;
971
972         if (acpi_dev->handler)
973                 return -EINVAL;
974
975         if (!acpi_drv->ops.add)
976                 return -ENOSYS;
977
978         ret = acpi_drv->ops.add(acpi_dev);
979         if (ret)
980                 return ret;
981
982         acpi_dev->driver = acpi_drv;
983         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
984                           "Driver [%s] successfully bound to device [%s]\n",
985                           acpi_drv->name, acpi_dev->pnp.bus_id));
986
987         if (acpi_drv->ops.notify) {
988                 ret = acpi_device_install_notify_handler(acpi_dev);
989                 if (ret) {
990                         if (acpi_drv->ops.remove)
991                                 acpi_drv->ops.remove(acpi_dev);
992
993                         acpi_dev->driver = NULL;
994                         acpi_dev->driver_data = NULL;
995                         return ret;
996                 }
997         }
998
999         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found driver [%s] for device [%s]\n",
1000                           acpi_drv->name, acpi_dev->pnp.bus_id));
1001         get_device(dev);
1002         return 0;
1003 }
1004
1005 static int acpi_device_remove(struct device * dev)
1006 {
1007         struct acpi_device *acpi_dev = to_acpi_device(dev);
1008         struct acpi_driver *acpi_drv = acpi_dev->driver;
1009
1010         if (acpi_drv) {
1011                 if (acpi_drv->ops.notify)
1012                         acpi_device_remove_notify_handler(acpi_dev);
1013                 if (acpi_drv->ops.remove)
1014                         acpi_drv->ops.remove(acpi_dev);
1015         }
1016         acpi_dev->driver = NULL;
1017         acpi_dev->driver_data = NULL;
1018
1019         put_device(dev);
1020         return 0;
1021 }
1022
1023 struct bus_type acpi_bus_type = {
1024         .name           = "acpi",
1025         .match          = acpi_bus_match,
1026         .probe          = acpi_device_probe,
1027         .remove         = acpi_device_remove,
1028         .uevent         = acpi_device_uevent,
1029 };
1030
1031 static void acpi_device_del(struct acpi_device *device)
1032 {
1033         mutex_lock(&acpi_device_lock);
1034         if (device->parent)
1035                 list_del(&device->node);
1036
1037         list_del(&device->wakeup_list);
1038         mutex_unlock(&acpi_device_lock);
1039
1040         acpi_power_add_remove_device(device, false);
1041         acpi_device_remove_files(device);
1042         if (device->remove)
1043                 device->remove(device);
1044
1045         device_del(&device->dev);
1046 }
1047
1048 static LIST_HEAD(acpi_device_del_list);
1049 static DEFINE_MUTEX(acpi_device_del_lock);
1050
1051 static void acpi_device_del_work_fn(struct work_struct *work_not_used)
1052 {
1053         for (;;) {
1054                 struct acpi_device *adev;
1055
1056                 mutex_lock(&acpi_device_del_lock);
1057
1058                 if (list_empty(&acpi_device_del_list)) {
1059                         mutex_unlock(&acpi_device_del_lock);
1060                         break;
1061                 }
1062                 adev = list_first_entry(&acpi_device_del_list,
1063                                         struct acpi_device, del_list);
1064                 list_del(&adev->del_list);
1065
1066                 mutex_unlock(&acpi_device_del_lock);
1067
1068                 acpi_device_del(adev);
1069                 /*
1070                  * Drop references to all power resources that might have been
1071                  * used by the device.
1072                  */
1073                 acpi_power_transition(adev, ACPI_STATE_D3_COLD);
1074                 put_device(&adev->dev);
1075         }
1076 }
1077
1078 /**
1079  * acpi_scan_drop_device - Drop an ACPI device object.
1080  * @handle: Handle of an ACPI namespace node, not used.
1081  * @context: Address of the ACPI device object to drop.
1082  *
1083  * This is invoked by acpi_ns_delete_node() during the removal of the ACPI
1084  * namespace node the device object pointed to by @context is attached to.
1085  *
1086  * The unregistration is carried out asynchronously to avoid running
1087  * acpi_device_del() under the ACPICA's namespace mutex and the list is used to
1088  * ensure the correct ordering (the device objects must be unregistered in the
1089  * same order in which the corresponding namespace nodes are deleted).
1090  */
1091 static void acpi_scan_drop_device(acpi_handle handle, void *context)
1092 {
1093         static DECLARE_WORK(work, acpi_device_del_work_fn);
1094         struct acpi_device *adev = context;
1095
1096         mutex_lock(&acpi_device_del_lock);
1097
1098         /*
1099          * Use the ACPI hotplug workqueue which is ordered, so this work item
1100          * won't run after any hotplug work items submitted subsequently.  That
1101          * prevents attempts to register device objects identical to those being
1102          * deleted from happening concurrently (such attempts result from
1103          * hotplug events handled via the ACPI hotplug workqueue).  It also will
1104          * run after all of the work items submitted previosuly, which helps
1105          * those work items to ensure that they are not accessing stale device
1106          * objects.
1107          */
1108         if (list_empty(&acpi_device_del_list))
1109                 acpi_queue_hotplug_work(&work);
1110
1111         list_add_tail(&adev->del_list, &acpi_device_del_list);
1112         /* Make acpi_ns_validate_handle() return NULL for this handle. */
1113         adev->handle = INVALID_ACPI_HANDLE;
1114
1115         mutex_unlock(&acpi_device_del_lock);
1116 }
1117
1118 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
1119 {
1120         acpi_status status;
1121
1122         if (!device)
1123                 return -EINVAL;
1124
1125         status = acpi_get_data(handle, acpi_scan_drop_device, (void **)device);
1126         if (ACPI_FAILURE(status) || !*device) {
1127                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
1128                                   handle));
1129                 return -ENODEV;
1130         }
1131         return 0;
1132 }
1133 EXPORT_SYMBOL(acpi_bus_get_device);
1134
1135 int acpi_device_add(struct acpi_device *device,
1136                     void (*release)(struct device *))
1137 {
1138         int result;
1139         struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
1140         int found = 0;
1141
1142         if (device->handle) {
1143                 acpi_status status;
1144
1145                 status = acpi_attach_data(device->handle, acpi_scan_drop_device,
1146                                           device);
1147                 if (ACPI_FAILURE(status)) {
1148                         acpi_handle_err(device->handle,
1149                                         "Unable to attach device data\n");
1150                         return -ENODEV;
1151                 }
1152         }
1153
1154         /*
1155          * Linkage
1156          * -------
1157          * Link this device to its parent and siblings.
1158          */
1159         INIT_LIST_HEAD(&device->children);
1160         INIT_LIST_HEAD(&device->node);
1161         INIT_LIST_HEAD(&device->wakeup_list);
1162         INIT_LIST_HEAD(&device->physical_node_list);
1163         INIT_LIST_HEAD(&device->del_list);
1164         mutex_init(&device->physical_node_lock);
1165
1166         new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
1167         if (!new_bus_id) {
1168                 pr_err(PREFIX "Memory allocation error\n");
1169                 result = -ENOMEM;
1170                 goto err_detach;
1171         }
1172
1173         mutex_lock(&acpi_device_lock);
1174         /*
1175          * Find suitable bus_id and instance number in acpi_bus_id_list
1176          * If failed, create one and link it into acpi_bus_id_list
1177          */
1178         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1179                 if (!strcmp(acpi_device_bus_id->bus_id,
1180                             acpi_device_hid(device))) {
1181                         acpi_device_bus_id->instance_no++;
1182                         found = 1;
1183                         kfree(new_bus_id);
1184                         break;
1185                 }
1186         }
1187         if (!found) {
1188                 acpi_device_bus_id = new_bus_id;
1189                 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
1190                 acpi_device_bus_id->instance_no = 0;
1191                 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1192         }
1193         dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1194
1195         if (device->parent)
1196                 list_add_tail(&device->node, &device->parent->children);
1197
1198         if (device->wakeup.flags.valid)
1199                 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
1200         mutex_unlock(&acpi_device_lock);
1201
1202         if (device->parent)
1203                 device->dev.parent = &device->parent->dev;
1204         device->dev.bus = &acpi_bus_type;
1205         device->dev.release = release;
1206         result = device_add(&device->dev);
1207         if (result) {
1208                 dev_err(&device->dev, "Error registering device\n");
1209                 goto err;
1210         }
1211
1212         result = acpi_device_setup_files(device);
1213         if (result)
1214                 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
1215                        dev_name(&device->dev));
1216
1217         return 0;
1218
1219  err:
1220         mutex_lock(&acpi_device_lock);
1221         if (device->parent)
1222                 list_del(&device->node);
1223         list_del(&device->wakeup_list);
1224         mutex_unlock(&acpi_device_lock);
1225
1226  err_detach:
1227         acpi_detach_data(device->handle, acpi_scan_drop_device);
1228         return result;
1229 }
1230
1231 /* --------------------------------------------------------------------------
1232                                  Driver Management
1233    -------------------------------------------------------------------------- */
1234 /**
1235  * acpi_bus_register_driver - register a driver with the ACPI bus
1236  * @driver: driver being registered
1237  *
1238  * Registers a driver with the ACPI bus.  Searches the namespace for all
1239  * devices that match the driver's criteria and binds.  Returns zero for
1240  * success or a negative error status for failure.
1241  */
1242 int acpi_bus_register_driver(struct acpi_driver *driver)
1243 {
1244         int ret;
1245
1246         if (acpi_disabled)
1247                 return -ENODEV;
1248         driver->drv.name = driver->name;
1249         driver->drv.bus = &acpi_bus_type;
1250         driver->drv.owner = driver->owner;
1251
1252         ret = driver_register(&driver->drv);
1253         return ret;
1254 }
1255
1256 EXPORT_SYMBOL(acpi_bus_register_driver);
1257
1258 /**
1259  * acpi_bus_unregister_driver - unregisters a driver with the ACPI bus
1260  * @driver: driver to unregister
1261  *
1262  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
1263  * devices that match the driver's criteria and unbinds.
1264  */
1265 void acpi_bus_unregister_driver(struct acpi_driver *driver)
1266 {
1267         driver_unregister(&driver->drv);
1268 }
1269
1270 EXPORT_SYMBOL(acpi_bus_unregister_driver);
1271
1272 /* --------------------------------------------------------------------------
1273                                  Device Enumeration
1274    -------------------------------------------------------------------------- */
1275 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
1276 {
1277         struct acpi_device *device = NULL;
1278         acpi_status status;
1279
1280         /*
1281          * Fixed hardware devices do not appear in the namespace and do not
1282          * have handles, but we fabricate acpi_devices for them, so we have
1283          * to deal with them specially.
1284          */
1285         if (!handle)
1286                 return acpi_root;
1287
1288         do {
1289                 status = acpi_get_parent(handle, &handle);
1290                 if (ACPI_FAILURE(status))
1291                         return status == AE_NULL_ENTRY ? NULL : acpi_root;
1292         } while (acpi_bus_get_device(handle, &device));
1293         return device;
1294 }
1295
1296 acpi_status
1297 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
1298 {
1299         acpi_status status;
1300         acpi_handle tmp;
1301         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
1302         union acpi_object *obj;
1303
1304         status = acpi_get_handle(handle, "_EJD", &tmp);
1305         if (ACPI_FAILURE(status))
1306                 return status;
1307
1308         status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
1309         if (ACPI_SUCCESS(status)) {
1310                 obj = buffer.pointer;
1311                 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
1312                                          ejd);
1313                 kfree(buffer.pointer);
1314         }
1315         return status;
1316 }
1317 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
1318
1319 static int acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
1320                                         struct acpi_device_wakeup *wakeup)
1321 {
1322         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1323         union acpi_object *package = NULL;
1324         union acpi_object *element = NULL;
1325         acpi_status status;
1326         int err = -ENODATA;
1327
1328         if (!wakeup)
1329                 return -EINVAL;
1330
1331         INIT_LIST_HEAD(&wakeup->resources);
1332
1333         /* _PRW */
1334         status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
1335         if (ACPI_FAILURE(status)) {
1336                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
1337                 return err;
1338         }
1339
1340         package = (union acpi_object *)buffer.pointer;
1341
1342         if (!package || package->package.count < 2)
1343                 goto out;
1344
1345         element = &(package->package.elements[0]);
1346         if (!element)
1347                 goto out;
1348
1349         if (element->type == ACPI_TYPE_PACKAGE) {
1350                 if ((element->package.count < 2) ||
1351                     (element->package.elements[0].type !=
1352                      ACPI_TYPE_LOCAL_REFERENCE)
1353                     || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
1354                         goto out;
1355
1356                 wakeup->gpe_device =
1357                     element->package.elements[0].reference.handle;
1358                 wakeup->gpe_number =
1359                     (u32) element->package.elements[1].integer.value;
1360         } else if (element->type == ACPI_TYPE_INTEGER) {
1361                 wakeup->gpe_device = NULL;
1362                 wakeup->gpe_number = element->integer.value;
1363         } else {
1364                 goto out;
1365         }
1366
1367         element = &(package->package.elements[1]);
1368         if (element->type != ACPI_TYPE_INTEGER)
1369                 goto out;
1370
1371         wakeup->sleep_state = element->integer.value;
1372
1373         err = acpi_extract_power_resources(package, 2, &wakeup->resources);
1374         if (err)
1375                 goto out;
1376
1377         if (!list_empty(&wakeup->resources)) {
1378                 int sleep_state;
1379
1380                 err = acpi_power_wakeup_list_init(&wakeup->resources,
1381                                                   &sleep_state);
1382                 if (err) {
1383                         acpi_handle_warn(handle, "Retrieving current states "
1384                                          "of wakeup power resources failed\n");
1385                         acpi_power_resources_list_free(&wakeup->resources);
1386                         goto out;
1387                 }
1388                 if (sleep_state < wakeup->sleep_state) {
1389                         acpi_handle_warn(handle, "Overriding _PRW sleep state "
1390                                          "(S%d) by S%d from power resources\n",
1391                                          (int)wakeup->sleep_state, sleep_state);
1392                         wakeup->sleep_state = sleep_state;
1393                 }
1394         }
1395         acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
1396
1397  out:
1398         kfree(buffer.pointer);
1399         return err;
1400 }
1401
1402 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1403 {
1404         struct acpi_device_id button_device_ids[] = {
1405                 {"PNP0C0C", 0},
1406                 {"PNP0C0D", 0},
1407                 {"PNP0C0E", 0},
1408                 {"", 0},
1409         };
1410         acpi_status status;
1411         acpi_event_status event_status;
1412
1413         device->wakeup.flags.notifier_present = 0;
1414
1415         /* Power button, Lid switch always enable wakeup */
1416         if (!acpi_match_device_ids(device, button_device_ids)) {
1417                 device->wakeup.flags.run_wake = 1;
1418                 if (!acpi_match_device_ids(device, &button_device_ids[1])) {
1419                         /* Do not use Lid/sleep button for S5 wakeup */
1420                         if (device->wakeup.sleep_state == ACPI_STATE_S5)
1421                                 device->wakeup.sleep_state = ACPI_STATE_S4;
1422                 }
1423                 device_set_wakeup_capable(&device->dev, true);
1424                 return;
1425         }
1426
1427         status = acpi_get_gpe_status(device->wakeup.gpe_device,
1428                                         device->wakeup.gpe_number,
1429                                                 &event_status);
1430         if (status == AE_OK)
1431                 device->wakeup.flags.run_wake =
1432                                 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
1433 }
1434
1435 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
1436 {
1437         int err;
1438
1439         /* Presence of _PRW indicates wake capable */
1440         if (!acpi_has_method(device->handle, "_PRW"))
1441                 return;
1442
1443         err = acpi_bus_extract_wakeup_device_power_package(device->handle,
1444                                                            &device->wakeup);
1445         if (err) {
1446                 dev_err(&device->dev, "_PRW evaluation error: %d\n", err);
1447                 return;
1448         }
1449
1450         device->wakeup.flags.valid = 1;
1451         device->wakeup.prepare_count = 0;
1452         acpi_bus_set_run_wake_flags(device);
1453         /* Call _PSW/_DSW object to disable its ability to wake the sleeping
1454          * system for the ACPI device with the _PRW object.
1455          * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
1456          * So it is necessary to call _DSW object first. Only when it is not
1457          * present will the _PSW object used.
1458          */
1459         err = acpi_device_sleep_wake(device, 0, 0, 0);
1460         if (err)
1461                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1462                                 "error in _DSW or _PSW evaluation\n"));
1463 }
1464
1465 static void acpi_bus_init_power_state(struct acpi_device *device, int state)
1466 {
1467         struct acpi_device_power_state *ps = &device->power.states[state];
1468         char pathname[5] = { '_', 'P', 'R', '0' + state, '\0' };
1469         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1470         acpi_status status;
1471
1472         INIT_LIST_HEAD(&ps->resources);
1473
1474         /* Evaluate "_PRx" to get referenced power resources */
1475         status = acpi_evaluate_object(device->handle, pathname, NULL, &buffer);
1476         if (ACPI_SUCCESS(status)) {
1477                 union acpi_object *package = buffer.pointer;
1478
1479                 if (buffer.length && package
1480                     && package->type == ACPI_TYPE_PACKAGE
1481                     && package->package.count) {
1482                         int err = acpi_extract_power_resources(package, 0,
1483                                                                &ps->resources);
1484                         if (!err)
1485                                 device->power.flags.power_resources = 1;
1486                 }
1487                 ACPI_FREE(buffer.pointer);
1488         }
1489
1490         /* Evaluate "_PSx" to see if we can do explicit sets */
1491         pathname[2] = 'S';
1492         if (acpi_has_method(device->handle, pathname))
1493                 ps->flags.explicit_set = 1;
1494
1495         /*
1496          * State is valid if there are means to put the device into it.
1497          * D3hot is only valid if _PR3 present.
1498          */
1499         if (!list_empty(&ps->resources)
1500             || (ps->flags.explicit_set && state < ACPI_STATE_D3_HOT)) {
1501                 ps->flags.valid = 1;
1502                 ps->flags.os_accessible = 1;
1503         }
1504
1505         ps->power = -1;         /* Unknown - driver assigned */
1506         ps->latency = -1;       /* Unknown - driver assigned */
1507 }
1508
1509 static void acpi_bus_get_power_flags(struct acpi_device *device)
1510 {
1511         u32 i;
1512
1513         /* Presence of _PS0|_PR0 indicates 'power manageable' */
1514         if (!acpi_has_method(device->handle, "_PS0") &&
1515             !acpi_has_method(device->handle, "_PR0"))
1516                 return;
1517
1518         device->flags.power_manageable = 1;
1519
1520         /*
1521          * Power Management Flags
1522          */
1523         if (acpi_has_method(device->handle, "_PSC"))
1524                 device->power.flags.explicit_get = 1;
1525         if (acpi_has_method(device->handle, "_IRC"))
1526                 device->power.flags.inrush_current = 1;
1527
1528         /*
1529          * Enumerate supported power management states
1530          */
1531         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++)
1532                 acpi_bus_init_power_state(device, i);
1533
1534         INIT_LIST_HEAD(&device->power.states[ACPI_STATE_D3_COLD].resources);
1535
1536         /* Set defaults for D0 and D3 states (always valid) */
1537         device->power.states[ACPI_STATE_D0].flags.valid = 1;
1538         device->power.states[ACPI_STATE_D0].power = 100;
1539         device->power.states[ACPI_STATE_D3_COLD].flags.valid = 1;
1540         device->power.states[ACPI_STATE_D3_COLD].power = 0;
1541
1542         /* Set D3cold's explicit_set flag if _PS3 exists. */
1543         if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1544                 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1545
1546         /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1547         if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1548                         device->power.flags.power_resources)
1549                 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1550
1551         if (acpi_bus_init_power(device)) {
1552                 acpi_free_power_resources_lists(device);
1553                 device->flags.power_manageable = 0;
1554         }
1555 }
1556
1557 static void acpi_bus_get_flags(struct acpi_device *device)
1558 {
1559         /* Presence of _STA indicates 'dynamic_status' */
1560         if (acpi_has_method(device->handle, "_STA"))
1561                 device->flags.dynamic_status = 1;
1562
1563         /* Presence of _RMV indicates 'removable' */
1564         if (acpi_has_method(device->handle, "_RMV"))
1565                 device->flags.removable = 1;
1566
1567         /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1568         if (acpi_has_method(device->handle, "_EJD") ||
1569             acpi_has_method(device->handle, "_EJ0"))
1570                 device->flags.ejectable = 1;
1571 }
1572
1573 static void acpi_device_get_busid(struct acpi_device *device)
1574 {
1575         char bus_id[5] = { '?', 0 };
1576         struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1577         int i = 0;
1578
1579         /*
1580          * Bus ID
1581          * ------
1582          * The device's Bus ID is simply the object name.
1583          * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1584          */
1585         if (ACPI_IS_ROOT_DEVICE(device)) {
1586                 strcpy(device->pnp.bus_id, "ACPI");
1587                 return;
1588         }
1589
1590         switch (device->device_type) {
1591         case ACPI_BUS_TYPE_POWER_BUTTON:
1592                 strcpy(device->pnp.bus_id, "PWRF");
1593                 break;
1594         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1595                 strcpy(device->pnp.bus_id, "SLPF");
1596                 break;
1597         default:
1598                 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1599                 /* Clean up trailing underscores (if any) */
1600                 for (i = 3; i > 1; i--) {
1601                         if (bus_id[i] == '_')
1602                                 bus_id[i] = '\0';
1603                         else
1604                                 break;
1605                 }
1606                 strcpy(device->pnp.bus_id, bus_id);
1607                 break;
1608         }
1609 }
1610
1611 /*
1612  * acpi_ata_match - see if an acpi object is an ATA device
1613  *
1614  * If an acpi object has one of the ACPI ATA methods defined,
1615  * then we can safely call it an ATA device.
1616  */
1617 bool acpi_ata_match(acpi_handle handle)
1618 {
1619         return acpi_has_method(handle, "_GTF") ||
1620                acpi_has_method(handle, "_GTM") ||
1621                acpi_has_method(handle, "_STM") ||
1622                acpi_has_method(handle, "_SDD");
1623 }
1624
1625 /*
1626  * acpi_bay_match - see if an acpi object is an ejectable driver bay
1627  *
1628  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1629  * then we can safely call it an ejectable drive bay
1630  */
1631 bool acpi_bay_match(acpi_handle handle)
1632 {
1633         acpi_handle phandle;
1634
1635         if (!acpi_has_method(handle, "_EJ0"))
1636                 return false;
1637         if (acpi_ata_match(handle))
1638                 return true;
1639         if (ACPI_FAILURE(acpi_get_parent(handle, &phandle)))
1640                 return false;
1641
1642         return acpi_ata_match(phandle);
1643 }
1644
1645 /*
1646  * acpi_dock_match - see if an acpi object has a _DCK method
1647  */
1648 bool acpi_dock_match(acpi_handle handle)
1649 {
1650         return acpi_has_method(handle, "_DCK");
1651 }
1652
1653 const char *acpi_device_hid(struct acpi_device *device)
1654 {
1655         struct acpi_hardware_id *hid;
1656
1657         if (list_empty(&device->pnp.ids))
1658                 return dummy_hid;
1659
1660         hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1661         return hid->id;
1662 }
1663 EXPORT_SYMBOL(acpi_device_hid);
1664
1665 static void acpi_add_id(struct acpi_device_pnp *pnp, const char *dev_id)
1666 {
1667         struct acpi_hardware_id *id;
1668
1669         id = kmalloc(sizeof(*id), GFP_KERNEL);
1670         if (!id)
1671                 return;
1672
1673         id->id = kstrdup(dev_id, GFP_KERNEL);
1674         if (!id->id) {
1675                 kfree(id);
1676                 return;
1677         }
1678
1679         list_add_tail(&id->list, &pnp->ids);
1680         pnp->type.hardware_id = 1;
1681 }
1682
1683 /*
1684  * Old IBM workstations have a DSDT bug wherein the SMBus object
1685  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1686  * prefix.  Work around this.
1687  */
1688 static bool acpi_ibm_smbus_match(acpi_handle handle)
1689 {
1690         char node_name[ACPI_PATH_SEGMENT_LENGTH];
1691         struct acpi_buffer path = { sizeof(node_name), node_name };
1692
1693         if (!dmi_name_in_vendors("IBM"))
1694                 return false;
1695
1696         /* Look for SMBS object */
1697         if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &path)) ||
1698             strcmp("SMBS", path.pointer))
1699                 return false;
1700
1701         /* Does it have the necessary (but misnamed) methods? */
1702         if (acpi_has_method(handle, "SBI") &&
1703             acpi_has_method(handle, "SBR") &&
1704             acpi_has_method(handle, "SBW"))
1705                 return true;
1706
1707         return false;
1708 }
1709
1710 static void acpi_set_pnp_ids(acpi_handle handle, struct acpi_device_pnp *pnp,
1711                                 int device_type)
1712 {
1713         acpi_status status;
1714         struct acpi_device_info *info;
1715         struct acpi_pnp_device_id_list *cid_list;
1716         int i;
1717
1718         switch (device_type) {
1719         case ACPI_BUS_TYPE_DEVICE:
1720                 if (handle == ACPI_ROOT_OBJECT) {
1721                         acpi_add_id(pnp, ACPI_SYSTEM_HID);
1722                         break;
1723                 }
1724
1725                 status = acpi_get_object_info(handle, &info);
1726                 if (ACPI_FAILURE(status)) {
1727                         pr_err(PREFIX "%s: Error reading device info\n",
1728                                         __func__);
1729                         return;
1730                 }
1731
1732                 if (info->valid & ACPI_VALID_HID)
1733                         acpi_add_id(pnp, info->hardware_id.string);
1734                 if (info->valid & ACPI_VALID_CID) {
1735                         cid_list = &info->compatible_id_list;
1736                         for (i = 0; i < cid_list->count; i++)
1737                                 acpi_add_id(pnp, cid_list->ids[i].string);
1738                 }
1739                 if (info->valid & ACPI_VALID_ADR) {
1740                         pnp->bus_address = info->address;
1741                         pnp->type.bus_address = 1;
1742                 }
1743                 if (info->valid & ACPI_VALID_UID)
1744                         pnp->unique_id = kstrdup(info->unique_id.string,
1745                                                         GFP_KERNEL);
1746
1747                 kfree(info);
1748
1749                 /*
1750                  * Some devices don't reliably have _HIDs & _CIDs, so add
1751                  * synthetic HIDs to make sure drivers can find them.
1752                  */
1753                 if (acpi_is_video_device(handle))
1754                         acpi_add_id(pnp, ACPI_VIDEO_HID);
1755                 else if (acpi_bay_match(handle))
1756                         acpi_add_id(pnp, ACPI_BAY_HID);
1757                 else if (acpi_dock_match(handle))
1758                         acpi_add_id(pnp, ACPI_DOCK_HID);
1759                 else if (acpi_ibm_smbus_match(handle))
1760                         acpi_add_id(pnp, ACPI_SMBUS_IBM_HID);
1761                 else if (list_empty(&pnp->ids) && handle == ACPI_ROOT_OBJECT) {
1762                         acpi_add_id(pnp, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1763                         strcpy(pnp->device_name, ACPI_BUS_DEVICE_NAME);
1764                         strcpy(pnp->device_class, ACPI_BUS_CLASS);
1765                 }
1766
1767                 break;
1768         case ACPI_BUS_TYPE_POWER:
1769                 acpi_add_id(pnp, ACPI_POWER_HID);
1770                 break;
1771         case ACPI_BUS_TYPE_PROCESSOR:
1772                 acpi_add_id(pnp, ACPI_PROCESSOR_OBJECT_HID);
1773                 break;
1774         case ACPI_BUS_TYPE_THERMAL:
1775                 acpi_add_id(pnp, ACPI_THERMAL_HID);
1776                 break;
1777         case ACPI_BUS_TYPE_POWER_BUTTON:
1778                 acpi_add_id(pnp, ACPI_BUTTON_HID_POWERF);
1779                 break;
1780         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1781                 acpi_add_id(pnp, ACPI_BUTTON_HID_SLEEPF);
1782                 break;
1783         }
1784 }
1785
1786 void acpi_free_pnp_ids(struct acpi_device_pnp *pnp)
1787 {
1788         struct acpi_hardware_id *id, *tmp;
1789
1790         list_for_each_entry_safe(id, tmp, &pnp->ids, list) {
1791                 kfree(id->id);
1792                 kfree(id);
1793         }
1794         kfree(pnp->unique_id);
1795 }
1796
1797 void acpi_init_device_object(struct acpi_device *device, acpi_handle handle,
1798                              int type, unsigned long long sta)
1799 {
1800         INIT_LIST_HEAD(&device->pnp.ids);
1801         device->device_type = type;
1802         device->handle = handle;
1803         device->parent = acpi_bus_get_parent(handle);
1804         acpi_set_device_status(device, sta);
1805         acpi_device_get_busid(device);
1806         acpi_set_pnp_ids(handle, &device->pnp, type);
1807         acpi_bus_get_flags(device);
1808         device->flags.match_driver = false;
1809         device->flags.initialized = true;
1810         device->flags.visited = false;
1811         device_initialize(&device->dev);
1812         dev_set_uevent_suppress(&device->dev, true);
1813 }
1814
1815 void acpi_device_add_finalize(struct acpi_device *device)
1816 {
1817         dev_set_uevent_suppress(&device->dev, false);
1818         kobject_uevent(&device->dev.kobj, KOBJ_ADD);
1819 }
1820
1821 static int acpi_add_single_object(struct acpi_device **child,
1822                                   acpi_handle handle, int type,
1823                                   unsigned long long sta)
1824 {
1825         int result;
1826         struct acpi_device *device;
1827         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1828
1829         device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1830         if (!device) {
1831                 printk(KERN_ERR PREFIX "Memory allocation error\n");
1832                 return -ENOMEM;
1833         }
1834
1835         acpi_init_device_object(device, handle, type, sta);
1836         acpi_bus_get_power_flags(device);
1837         acpi_bus_get_wakeup_device_flags(device);
1838
1839         result = acpi_device_add(device, acpi_device_release);
1840         if (result) {
1841                 acpi_device_release(&device->dev);
1842                 return result;
1843         }
1844
1845         acpi_power_add_remove_device(device, true);
1846         acpi_device_add_finalize(device);
1847         acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1848         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Added %s [%s] parent %s\n",
1849                 dev_name(&device->dev), (char *) buffer.pointer,
1850                 device->parent ? dev_name(&device->parent->dev) : "(null)"));
1851         kfree(buffer.pointer);
1852         *child = device;
1853         return 0;
1854 }
1855
1856 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1857                                     unsigned long long *sta)
1858 {
1859         acpi_status status;
1860         acpi_object_type acpi_type;
1861
1862         status = acpi_get_type(handle, &acpi_type);
1863         if (ACPI_FAILURE(status))
1864                 return -ENODEV;
1865
1866         switch (acpi_type) {
1867         case ACPI_TYPE_ANY:             /* for ACPI_ROOT_OBJECT */
1868         case ACPI_TYPE_DEVICE:
1869                 *type = ACPI_BUS_TYPE_DEVICE;
1870                 status = acpi_bus_get_status_handle(handle, sta);
1871                 if (ACPI_FAILURE(status))
1872                         return -ENODEV;
1873                 break;
1874         case ACPI_TYPE_PROCESSOR:
1875                 *type = ACPI_BUS_TYPE_PROCESSOR;
1876                 status = acpi_bus_get_status_handle(handle, sta);
1877                 if (ACPI_FAILURE(status))
1878                         return -ENODEV;
1879                 break;
1880         case ACPI_TYPE_THERMAL:
1881                 *type = ACPI_BUS_TYPE_THERMAL;
1882                 *sta = ACPI_STA_DEFAULT;
1883                 break;
1884         case ACPI_TYPE_POWER:
1885                 *type = ACPI_BUS_TYPE_POWER;
1886                 *sta = ACPI_STA_DEFAULT;
1887                 break;
1888         default:
1889                 return -ENODEV;
1890         }
1891
1892         return 0;
1893 }
1894
1895 bool acpi_device_is_present(struct acpi_device *adev)
1896 {
1897         if (adev->status.present || adev->status.functional)
1898                 return true;
1899
1900         adev->flags.initialized = false;
1901         return false;
1902 }
1903
1904 static bool acpi_scan_handler_matching(struct acpi_scan_handler *handler,
1905                                        char *idstr,
1906                                        const struct acpi_device_id **matchid)
1907 {
1908         const struct acpi_device_id *devid;
1909
1910         for (devid = handler->ids; devid->id[0]; devid++)
1911                 if (!strcmp((char *)devid->id, idstr)) {
1912                         if (matchid)
1913                                 *matchid = devid;
1914
1915                         return true;
1916                 }
1917
1918         return false;
1919 }
1920
1921 static struct acpi_scan_handler *acpi_scan_match_handler(char *idstr,
1922                                         const struct acpi_device_id **matchid)
1923 {
1924         struct acpi_scan_handler *handler;
1925
1926         list_for_each_entry(handler, &acpi_scan_handlers_list, list_node)
1927                 if (acpi_scan_handler_matching(handler, idstr, matchid))
1928                         return handler;
1929
1930         return NULL;
1931 }
1932
1933 void acpi_scan_hotplug_enabled(struct acpi_hotplug_profile *hotplug, bool val)
1934 {
1935         if (!!hotplug->enabled == !!val)
1936                 return;
1937
1938         mutex_lock(&acpi_scan_lock);
1939
1940         hotplug->enabled = val;
1941
1942         mutex_unlock(&acpi_scan_lock);
1943 }
1944
1945 static void acpi_scan_init_hotplug(acpi_handle handle, int type)
1946 {
1947         struct acpi_device_pnp pnp = {};
1948         struct acpi_hardware_id *hwid;
1949         struct acpi_scan_handler *handler;
1950
1951         INIT_LIST_HEAD(&pnp.ids);
1952         acpi_set_pnp_ids(handle, &pnp, type);
1953
1954         if (!pnp.type.hardware_id)
1955                 goto out;
1956
1957         /*
1958          * This relies on the fact that acpi_install_notify_handler() will not
1959          * install the same notify handler routine twice for the same handle.
1960          */
1961         list_for_each_entry(hwid, &pnp.ids, list) {
1962                 handler = acpi_scan_match_handler(hwid->id, NULL);
1963                 if (handler) {
1964                         acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1965                                         acpi_hotplug_notify_cb, handler);
1966                         break;
1967                 }
1968         }
1969
1970 out:
1971         acpi_free_pnp_ids(&pnp);
1972 }
1973
1974 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl_not_used,
1975                                       void *not_used, void **return_value)
1976 {
1977         struct acpi_device *device = NULL;
1978         int type;
1979         unsigned long long sta;
1980         int result;
1981
1982         acpi_bus_get_device(handle, &device);
1983         if (device)
1984                 goto out;
1985
1986         result = acpi_bus_type_and_status(handle, &type, &sta);
1987         if (result)
1988                 return AE_OK;
1989
1990         if (type == ACPI_BUS_TYPE_POWER) {
1991                 acpi_add_power_resource(handle);
1992                 return AE_OK;
1993         }
1994
1995         acpi_scan_init_hotplug(handle, type);
1996
1997         acpi_add_single_object(&device, handle, type, sta);
1998         if (!device)
1999                 return AE_CTRL_DEPTH;
2000
2001  out:
2002         if (!*return_value)
2003                 *return_value = device;
2004
2005         return AE_OK;
2006 }
2007
2008 static int acpi_scan_attach_handler(struct acpi_device *device)
2009 {
2010         struct acpi_hardware_id *hwid;
2011         int ret = 0;
2012
2013         list_for_each_entry(hwid, &device->pnp.ids, list) {
2014                 const struct acpi_device_id *devid;
2015                 struct acpi_scan_handler *handler;
2016
2017                 handler = acpi_scan_match_handler(hwid->id, &devid);
2018                 if (handler) {
2019                         ret = handler->attach(device, devid);
2020                         if (ret > 0) {
2021                                 device->handler = handler;
2022                                 break;
2023                         } else if (ret < 0) {
2024                                 break;
2025                         }
2026                 }
2027         }
2028         return ret;
2029 }
2030
2031 static void acpi_bus_attach(struct acpi_device *device)
2032 {
2033         struct acpi_device *child;
2034         int ret;
2035
2036         acpi_bus_get_status(device);
2037         /* Skip devices that are not present. */
2038         if (!acpi_device_is_present(device)) {
2039                 device->flags.visited = false;
2040                 return;
2041         }
2042         if (device->handler)
2043                 goto ok;
2044
2045         if (!device->flags.initialized) {
2046                 acpi_bus_update_power(device, NULL);
2047                 device->flags.initialized = true;
2048         }
2049         device->flags.visited = false;
2050         ret = acpi_scan_attach_handler(device);
2051         if (ret < 0)
2052                 return;
2053
2054         device->flags.match_driver = true;
2055         if (!ret) {
2056                 ret = device_attach(&device->dev);
2057                 if (ret < 0)
2058                         return;
2059         }
2060         device->flags.visited = true;
2061
2062  ok:
2063         list_for_each_entry(child, &device->children, node)
2064                 acpi_bus_attach(child);
2065 }
2066
2067 /**
2068  * acpi_bus_scan - Add ACPI device node objects in a given namespace scope.
2069  * @handle: Root of the namespace scope to scan.
2070  *
2071  * Scan a given ACPI tree (probably recently hot-plugged) and create and add
2072  * found devices.
2073  *
2074  * If no devices were found, -ENODEV is returned, but it does not mean that
2075  * there has been a real error.  There just have been no suitable ACPI objects
2076  * in the table trunk from which the kernel could create a device and add an
2077  * appropriate driver.
2078  *
2079  * Must be called under acpi_scan_lock.
2080  */
2081 int acpi_bus_scan(acpi_handle handle)
2082 {
2083         void *device = NULL;
2084
2085         if (ACPI_SUCCESS(acpi_bus_check_add(handle, 0, NULL, &device)))
2086                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2087                                     acpi_bus_check_add, NULL, NULL, &device);
2088
2089         if (device) {
2090                 acpi_bus_attach(device);
2091                 return 0;
2092         }
2093         return -ENODEV;
2094 }
2095 EXPORT_SYMBOL(acpi_bus_scan);
2096
2097 /**
2098  * acpi_bus_trim - Detach scan handlers and drivers from ACPI device objects.
2099  * @adev: Root of the ACPI namespace scope to walk.
2100  *
2101  * Must be called under acpi_scan_lock.
2102  */
2103 void acpi_bus_trim(struct acpi_device *adev)
2104 {
2105         struct acpi_scan_handler *handler = adev->handler;
2106         struct acpi_device *child;
2107
2108         list_for_each_entry_reverse(child, &adev->children, node)
2109                 acpi_bus_trim(child);
2110
2111         adev->flags.match_driver = false;
2112         if (handler) {
2113                 if (handler->detach)
2114                         handler->detach(adev);
2115
2116                 adev->handler = NULL;
2117         } else {
2118                 device_release_driver(&adev->dev);
2119         }
2120         /*
2121          * Most likely, the device is going away, so put it into D3cold before
2122          * that.
2123          */
2124         acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
2125         adev->flags.initialized = false;
2126         adev->flags.visited = false;
2127 }
2128 EXPORT_SYMBOL_GPL(acpi_bus_trim);
2129
2130 static int acpi_bus_scan_fixed(void)
2131 {
2132         int result = 0;
2133
2134         /*
2135          * Enumerate all fixed-feature devices.
2136          */
2137         if (!(acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON)) {
2138                 struct acpi_device *device = NULL;
2139
2140                 result = acpi_add_single_object(&device, NULL,
2141                                                 ACPI_BUS_TYPE_POWER_BUTTON,
2142                                                 ACPI_STA_DEFAULT);
2143                 if (result)
2144                         return result;
2145
2146                 device->flags.match_driver = true;
2147                 result = device_attach(&device->dev);
2148                 if (result < 0)
2149                         return result;
2150
2151                 device_init_wakeup(&device->dev, true);
2152         }
2153
2154         if (!(acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON)) {
2155                 struct acpi_device *device = NULL;
2156
2157                 result = acpi_add_single_object(&device, NULL,
2158                                                 ACPI_BUS_TYPE_SLEEP_BUTTON,
2159                                                 ACPI_STA_DEFAULT);
2160                 if (result)
2161                         return result;
2162
2163                 device->flags.match_driver = true;
2164                 result = device_attach(&device->dev);
2165         }
2166
2167         return result < 0 ? result : 0;
2168 }
2169
2170 int __init acpi_scan_init(void)
2171 {
2172         int result;
2173
2174         result = bus_register(&acpi_bus_type);
2175         if (result) {
2176                 /* We don't want to quit even if we failed to add suspend/resume */
2177                 printk(KERN_ERR PREFIX "Could not register bus type\n");
2178         }
2179
2180         acpi_pci_root_init();
2181         acpi_pci_link_init();
2182         acpi_processor_init();
2183         acpi_platform_init();
2184         acpi_lpss_init();
2185         acpi_cmos_rtc_init();
2186         acpi_container_init();
2187         acpi_memory_hotplug_init();
2188         acpi_dock_init();
2189
2190         mutex_lock(&acpi_scan_lock);
2191         /*
2192          * Enumerate devices in the ACPI namespace.
2193          */
2194         result = acpi_bus_scan(ACPI_ROOT_OBJECT);
2195         if (result)
2196                 goto out;
2197
2198         result = acpi_bus_get_device(ACPI_ROOT_OBJECT, &acpi_root);
2199         if (result)
2200                 goto out;
2201
2202         result = acpi_bus_scan_fixed();
2203         if (result) {
2204                 acpi_detach_data(acpi_root->handle, acpi_scan_drop_device);
2205                 acpi_device_del(acpi_root);
2206                 put_device(&acpi_root->dev);
2207                 goto out;
2208         }
2209
2210         acpi_update_all_gpes();
2211
2212  out:
2213         mutex_unlock(&acpi_scan_lock);
2214         return result;
2215 }