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