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