Merge branch 'acpica'
[cascardo/linux.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 static const char *dummy_hid = "device";
31
32 /*
33  * The following ACPI IDs are known to be suitable for representing as
34  * platform devices.
35  */
36 static const struct acpi_device_id acpi_platform_device_ids[] = {
37
38         { "PNP0D40" },
39
40         { }
41 };
42
43 static LIST_HEAD(acpi_device_list);
44 static LIST_HEAD(acpi_bus_id_list);
45 DEFINE_MUTEX(acpi_device_lock);
46 LIST_HEAD(acpi_wakeup_device_list);
47
48 struct acpi_device_bus_id{
49         char bus_id[15];
50         unsigned int instance_no;
51         struct list_head node;
52 };
53
54 /*
55  * Creates hid/cid(s) string needed for modalias and uevent
56  * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
57  * char *modalias: "acpi:IBM0001:ACPI0001"
58 */
59 static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
60                            int size)
61 {
62         int len;
63         int count;
64         struct acpi_hardware_id *id;
65
66         if (list_empty(&acpi_dev->pnp.ids))
67                 return 0;
68
69         len = snprintf(modalias, size, "acpi:");
70         size -= len;
71
72         list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
73                 count = snprintf(&modalias[len], size, "%s:", id->id);
74                 if (count < 0 || count >= size)
75                         return -EINVAL;
76                 len += count;
77                 size -= count;
78         }
79
80         modalias[len] = '\0';
81         return len;
82 }
83
84 static ssize_t
85 acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
86         struct acpi_device *acpi_dev = to_acpi_device(dev);
87         int len;
88
89         /* Device has no HID and no CID or string is >1024 */
90         len = create_modalias(acpi_dev, buf, 1024);
91         if (len <= 0)
92                 return 0;
93         buf[len++] = '\n';
94         return len;
95 }
96 static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
97
98 /**
99  * acpi_bus_hot_remove_device: hot-remove a device and its children
100  * @context: struct acpi_eject_event pointer (freed in this func)
101  *
102  * Hot-remove a device and its children. This function frees up the
103  * memory space passed by arg context, so that the caller may call
104  * this function asynchronously through acpi_os_hotplug_execute().
105  */
106 void acpi_bus_hot_remove_device(void *context)
107 {
108         struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
109         struct acpi_device *device;
110         acpi_handle handle = ej_event->handle;
111         struct acpi_object_list arg_list;
112         union acpi_object arg;
113         acpi_status status = AE_OK;
114         u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
115
116         if (acpi_bus_get_device(handle, &device))
117                 goto err_out;
118
119         if (!device)
120                 goto err_out;
121
122         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
123                 "Hot-removing device %s...\n", dev_name(&device->dev)));
124
125         if (acpi_bus_trim(device, 1)) {
126                 printk(KERN_ERR PREFIX
127                                 "Removing device failed\n");
128                 goto err_out;
129         }
130
131         /* power off device */
132         status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
133         if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
134                 printk(KERN_WARNING PREFIX
135                                 "Power-off device failed\n");
136
137         if (device->flags.lockable) {
138                 arg_list.count = 1;
139                 arg_list.pointer = &arg;
140                 arg.type = ACPI_TYPE_INTEGER;
141                 arg.integer.value = 0;
142                 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
143         }
144
145         arg_list.count = 1;
146         arg_list.pointer = &arg;
147         arg.type = ACPI_TYPE_INTEGER;
148         arg.integer.value = 1;
149
150         /*
151          * TBD: _EJD support.
152          */
153         status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
154         if (ACPI_FAILURE(status)) {
155                 if (status != AE_NOT_FOUND)
156                         printk(KERN_WARNING PREFIX
157                                         "Eject device failed\n");
158                 goto err_out;
159         }
160
161         kfree(context);
162         return;
163
164 err_out:
165         /* Inform firmware the hot-remove operation has completed w/ error */
166         (void) acpi_evaluate_hotplug_ost(handle,
167                                 ej_event->event, ost_code, NULL);
168         kfree(context);
169         return;
170 }
171
172 static ssize_t
173 acpi_eject_store(struct device *d, struct device_attribute *attr,
174                 const char *buf, size_t count)
175 {
176         int ret = count;
177         acpi_status status;
178         acpi_object_type type = 0;
179         struct acpi_device *acpi_device = to_acpi_device(d);
180         struct acpi_eject_event *ej_event;
181
182         if ((!count) || (buf[0] != '1')) {
183                 return -EINVAL;
184         }
185 #ifndef FORCE_EJECT
186         if (acpi_device->driver == NULL) {
187                 ret = -ENODEV;
188                 goto err;
189         }
190 #endif
191         status = acpi_get_type(acpi_device->handle, &type);
192         if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
193                 ret = -ENODEV;
194                 goto err;
195         }
196
197         ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
198         if (!ej_event) {
199                 ret = -ENOMEM;
200                 goto err;
201         }
202
203         ej_event->handle = acpi_device->handle;
204         if (acpi_device->flags.eject_pending) {
205                 /* event originated from ACPI eject notification */
206                 ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
207                 acpi_device->flags.eject_pending = 0;
208         } else {
209                 /* event originated from user */
210                 ej_event->event = ACPI_OST_EC_OSPM_EJECT;
211                 (void) acpi_evaluate_hotplug_ost(ej_event->handle,
212                         ej_event->event, ACPI_OST_SC_EJECT_IN_PROGRESS, NULL);
213         }
214
215         acpi_os_hotplug_execute(acpi_bus_hot_remove_device, (void *)ej_event);
216 err:
217         return ret;
218 }
219
220 static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
221
222 static ssize_t
223 acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
224         struct acpi_device *acpi_dev = to_acpi_device(dev);
225
226         return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
227 }
228 static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
229
230 static ssize_t
231 acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
232         struct acpi_device *acpi_dev = to_acpi_device(dev);
233         struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
234         int result;
235
236         result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
237         if (result)
238                 goto end;
239
240         result = sprintf(buf, "%s\n", (char*)path.pointer);
241         kfree(path.pointer);
242 end:
243         return result;
244 }
245 static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
246
247 /* sysfs file that shows description text from the ACPI _STR method */
248 static ssize_t description_show(struct device *dev,
249                                 struct device_attribute *attr,
250                                 char *buf) {
251         struct acpi_device *acpi_dev = to_acpi_device(dev);
252         int result;
253
254         if (acpi_dev->pnp.str_obj == NULL)
255                 return 0;
256
257         /*
258          * The _STR object contains a Unicode identifier for a device.
259          * We need to convert to utf-8 so it can be displayed.
260          */
261         result = utf16s_to_utf8s(
262                 (wchar_t *)acpi_dev->pnp.str_obj->buffer.pointer,
263                 acpi_dev->pnp.str_obj->buffer.length,
264                 UTF16_LITTLE_ENDIAN, buf,
265                 PAGE_SIZE);
266
267         buf[result++] = '\n';
268
269         return result;
270 }
271 static DEVICE_ATTR(description, 0444, description_show, NULL);
272
273 static int acpi_device_setup_files(struct acpi_device *dev)
274 {
275         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
276         acpi_status status;
277         acpi_handle temp;
278         int result = 0;
279
280         /*
281          * Devices gotten from FADT don't have a "path" attribute
282          */
283         if (dev->handle) {
284                 result = device_create_file(&dev->dev, &dev_attr_path);
285                 if (result)
286                         goto end;
287         }
288
289         if (!list_empty(&dev->pnp.ids)) {
290                 result = device_create_file(&dev->dev, &dev_attr_hid);
291                 if (result)
292                         goto end;
293
294                 result = device_create_file(&dev->dev, &dev_attr_modalias);
295                 if (result)
296                         goto end;
297         }
298
299         /*
300          * If device has _STR, 'description' file is created
301          */
302         status = acpi_get_handle(dev->handle, "_STR", &temp);
303         if (ACPI_SUCCESS(status)) {
304                 status = acpi_evaluate_object(dev->handle, "_STR",
305                                         NULL, &buffer);
306                 if (ACPI_FAILURE(status))
307                         buffer.pointer = NULL;
308                 dev->pnp.str_obj = buffer.pointer;
309                 result = device_create_file(&dev->dev, &dev_attr_description);
310                 if (result)
311                         goto end;
312         }
313
314         /*
315          * If device has _EJ0, 'eject' file is created that is used to trigger
316          * hot-removal function from userland.
317          */
318         status = acpi_get_handle(dev->handle, "_EJ0", &temp);
319         if (ACPI_SUCCESS(status))
320                 result = device_create_file(&dev->dev, &dev_attr_eject);
321 end:
322         return result;
323 }
324
325 static void acpi_device_remove_files(struct acpi_device *dev)
326 {
327         acpi_status status;
328         acpi_handle temp;
329
330         /*
331          * If device has _STR, remove 'description' file
332          */
333         status = acpi_get_handle(dev->handle, "_STR", &temp);
334         if (ACPI_SUCCESS(status)) {
335                 kfree(dev->pnp.str_obj);
336                 device_remove_file(&dev->dev, &dev_attr_description);
337         }
338         /*
339          * If device has _EJ0, remove 'eject' file.
340          */
341         status = acpi_get_handle(dev->handle, "_EJ0", &temp);
342         if (ACPI_SUCCESS(status))
343                 device_remove_file(&dev->dev, &dev_attr_eject);
344
345         device_remove_file(&dev->dev, &dev_attr_modalias);
346         device_remove_file(&dev->dev, &dev_attr_hid);
347         if (dev->handle)
348                 device_remove_file(&dev->dev, &dev_attr_path);
349 }
350 /* --------------------------------------------------------------------------
351                         ACPI Bus operations
352    -------------------------------------------------------------------------- */
353
354 static const struct acpi_device_id *__acpi_match_device(
355         struct acpi_device *device, const struct acpi_device_id *ids)
356 {
357         const struct acpi_device_id *id;
358         struct acpi_hardware_id *hwid;
359
360         /*
361          * If the device is not present, it is unnecessary to load device
362          * driver for it.
363          */
364         if (!device->status.present)
365                 return NULL;
366
367         for (id = ids; id->id[0]; id++)
368                 list_for_each_entry(hwid, &device->pnp.ids, list)
369                         if (!strcmp((char *) id->id, hwid->id))
370                                 return id;
371
372         return NULL;
373 }
374
375 /**
376  * acpi_match_device - Match a struct device against a given list of ACPI IDs
377  * @ids: Array of struct acpi_device_id object to match against.
378  * @dev: The device structure to match.
379  *
380  * Check if @dev has a valid ACPI handle and if there is a struct acpi_device
381  * object for that handle and use that object to match against a given list of
382  * device IDs.
383  *
384  * Return a pointer to the first matching ID on success or %NULL on failure.
385  */
386 const struct acpi_device_id *acpi_match_device(const struct acpi_device_id *ids,
387                                                const struct device *dev)
388 {
389         struct acpi_device *adev;
390
391         if (!ids || !ACPI_HANDLE(dev)
392             || ACPI_FAILURE(acpi_bus_get_device(ACPI_HANDLE(dev), &adev)))
393                 return NULL;
394
395         return __acpi_match_device(adev, ids);
396 }
397 EXPORT_SYMBOL_GPL(acpi_match_device);
398
399 int acpi_match_device_ids(struct acpi_device *device,
400                           const struct acpi_device_id *ids)
401 {
402         return __acpi_match_device(device, ids) ? 0 : -ENOENT;
403 }
404 EXPORT_SYMBOL(acpi_match_device_ids);
405
406 static void acpi_free_ids(struct acpi_device *device)
407 {
408         struct acpi_hardware_id *id, *tmp;
409
410         list_for_each_entry_safe(id, tmp, &device->pnp.ids, list) {
411                 kfree(id->id);
412                 kfree(id);
413         }
414 }
415
416 static void acpi_device_release(struct device *dev)
417 {
418         struct acpi_device *acpi_dev = to_acpi_device(dev);
419
420         acpi_free_ids(acpi_dev);
421         kfree(acpi_dev);
422 }
423
424 static int acpi_bus_match(struct device *dev, struct device_driver *drv)
425 {
426         struct acpi_device *acpi_dev = to_acpi_device(dev);
427         struct acpi_driver *acpi_drv = to_acpi_driver(drv);
428
429         return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
430 }
431
432 static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
433 {
434         struct acpi_device *acpi_dev = to_acpi_device(dev);
435         int len;
436
437         if (list_empty(&acpi_dev->pnp.ids))
438                 return 0;
439
440         if (add_uevent_var(env, "MODALIAS="))
441                 return -ENOMEM;
442         len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
443                               sizeof(env->buf) - env->buflen);
444         if (len >= (sizeof(env->buf) - env->buflen))
445                 return -ENOMEM;
446         env->buflen += len;
447         return 0;
448 }
449
450 static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
451 {
452         struct acpi_device *device = data;
453
454         device->driver->ops.notify(device, event);
455 }
456
457 static acpi_status acpi_device_notify_fixed(void *data)
458 {
459         struct acpi_device *device = data;
460
461         /* Fixed hardware devices have no handles */
462         acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
463         return AE_OK;
464 }
465
466 static int acpi_device_install_notify_handler(struct acpi_device *device)
467 {
468         acpi_status status;
469
470         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
471                 status =
472                     acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
473                                                      acpi_device_notify_fixed,
474                                                      device);
475         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
476                 status =
477                     acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
478                                                      acpi_device_notify_fixed,
479                                                      device);
480         else
481                 status = acpi_install_notify_handler(device->handle,
482                                                      ACPI_DEVICE_NOTIFY,
483                                                      acpi_device_notify,
484                                                      device);
485
486         if (ACPI_FAILURE(status))
487                 return -EINVAL;
488         return 0;
489 }
490
491 static void acpi_device_remove_notify_handler(struct acpi_device *device)
492 {
493         if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
494                 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
495                                                 acpi_device_notify_fixed);
496         else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
497                 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
498                                                 acpi_device_notify_fixed);
499         else
500                 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
501                                            acpi_device_notify);
502 }
503
504 static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
505 static int acpi_start_single_object(struct acpi_device *);
506 static int acpi_device_probe(struct device * dev)
507 {
508         struct acpi_device *acpi_dev = to_acpi_device(dev);
509         struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
510         int ret;
511
512         ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
513         if (!ret) {
514                 if (acpi_dev->bus_ops.acpi_op_start)
515                         acpi_start_single_object(acpi_dev);
516
517                 if (acpi_drv->ops.notify) {
518                         ret = acpi_device_install_notify_handler(acpi_dev);
519                         if (ret) {
520                                 if (acpi_drv->ops.remove)
521                                         acpi_drv->ops.remove(acpi_dev,
522                                                      acpi_dev->removal_type);
523                                 return ret;
524                         }
525                 }
526
527                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
528                         "Found driver [%s] for device [%s]\n",
529                         acpi_drv->name, acpi_dev->pnp.bus_id));
530                 get_device(dev);
531         }
532         return ret;
533 }
534
535 static int acpi_device_remove(struct device * dev)
536 {
537         struct acpi_device *acpi_dev = to_acpi_device(dev);
538         struct acpi_driver *acpi_drv = acpi_dev->driver;
539
540         if (acpi_drv) {
541                 if (acpi_drv->ops.notify)
542                         acpi_device_remove_notify_handler(acpi_dev);
543                 if (acpi_drv->ops.remove)
544                         acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
545         }
546         acpi_dev->driver = NULL;
547         acpi_dev->driver_data = NULL;
548
549         put_device(dev);
550         return 0;
551 }
552
553 struct bus_type acpi_bus_type = {
554         .name           = "acpi",
555         .match          = acpi_bus_match,
556         .probe          = acpi_device_probe,
557         .remove         = acpi_device_remove,
558         .uevent         = acpi_device_uevent,
559 };
560
561 static int acpi_device_register(struct acpi_device *device)
562 {
563         int result;
564         struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
565         int found = 0;
566
567         /*
568          * Linkage
569          * -------
570          * Link this device to its parent and siblings.
571          */
572         INIT_LIST_HEAD(&device->children);
573         INIT_LIST_HEAD(&device->node);
574         INIT_LIST_HEAD(&device->wakeup_list);
575         INIT_LIST_HEAD(&device->physical_node_list);
576         mutex_init(&device->physical_node_lock);
577
578         new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
579         if (!new_bus_id) {
580                 printk(KERN_ERR PREFIX "Memory allocation error\n");
581                 return -ENOMEM;
582         }
583
584         mutex_lock(&acpi_device_lock);
585         /*
586          * Find suitable bus_id and instance number in acpi_bus_id_list
587          * If failed, create one and link it into acpi_bus_id_list
588          */
589         list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
590                 if (!strcmp(acpi_device_bus_id->bus_id,
591                             acpi_device_hid(device))) {
592                         acpi_device_bus_id->instance_no++;
593                         found = 1;
594                         kfree(new_bus_id);
595                         break;
596                 }
597         }
598         if (!found) {
599                 acpi_device_bus_id = new_bus_id;
600                 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
601                 acpi_device_bus_id->instance_no = 0;
602                 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
603         }
604         dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
605
606         if (device->parent)
607                 list_add_tail(&device->node, &device->parent->children);
608
609         if (device->wakeup.flags.valid)
610                 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
611         mutex_unlock(&acpi_device_lock);
612
613         if (device->parent)
614                 device->dev.parent = &device->parent->dev;
615         device->dev.bus = &acpi_bus_type;
616         device->dev.release = &acpi_device_release;
617         result = device_register(&device->dev);
618         if (result) {
619                 dev_err(&device->dev, "Error registering device\n");
620                 goto end;
621         }
622
623         result = acpi_device_setup_files(device);
624         if (result)
625                 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
626                        dev_name(&device->dev));
627
628         device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
629         return 0;
630 end:
631         mutex_lock(&acpi_device_lock);
632         if (device->parent)
633                 list_del(&device->node);
634         list_del(&device->wakeup_list);
635         mutex_unlock(&acpi_device_lock);
636         return result;
637 }
638
639 static void acpi_device_unregister(struct acpi_device *device, int type)
640 {
641         mutex_lock(&acpi_device_lock);
642         if (device->parent)
643                 list_del(&device->node);
644
645         list_del(&device->wakeup_list);
646         mutex_unlock(&acpi_device_lock);
647
648         acpi_detach_data(device->handle, acpi_bus_data_handler);
649
650         acpi_device_remove_files(device);
651         device_unregister(&device->dev);
652 }
653
654 /* --------------------------------------------------------------------------
655                                  Driver Management
656    -------------------------------------------------------------------------- */
657 /**
658  * acpi_bus_driver_init - add a device to a driver
659  * @device: the device to add and initialize
660  * @driver: driver for the device
661  *
662  * Used to initialize a device via its device driver.  Called whenever a
663  * driver is bound to a device.  Invokes the driver's add() ops.
664  */
665 static int
666 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
667 {
668         int result = 0;
669
670         if (!device || !driver)
671                 return -EINVAL;
672
673         if (!driver->ops.add)
674                 return -ENOSYS;
675
676         result = driver->ops.add(device);
677         if (result) {
678                 device->driver = NULL;
679                 device->driver_data = NULL;
680                 return result;
681         }
682
683         device->driver = driver;
684
685         /*
686          * TBD - Configuration Management: Assign resources to device based
687          * upon possible configuration and currently allocated resources.
688          */
689
690         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
691                           "Driver successfully bound to device\n"));
692         return 0;
693 }
694
695 static int acpi_start_single_object(struct acpi_device *device)
696 {
697         int result = 0;
698         struct acpi_driver *driver;
699
700
701         if (!(driver = device->driver))
702                 return 0;
703
704         if (driver->ops.start) {
705                 result = driver->ops.start(device);
706                 if (result && driver->ops.remove)
707                         driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
708         }
709
710         return result;
711 }
712
713 /**
714  * acpi_bus_register_driver - register a driver with the ACPI bus
715  * @driver: driver being registered
716  *
717  * Registers a driver with the ACPI bus.  Searches the namespace for all
718  * devices that match the driver's criteria and binds.  Returns zero for
719  * success or a negative error status for failure.
720  */
721 int acpi_bus_register_driver(struct acpi_driver *driver)
722 {
723         int ret;
724
725         if (acpi_disabled)
726                 return -ENODEV;
727         driver->drv.name = driver->name;
728         driver->drv.bus = &acpi_bus_type;
729         driver->drv.owner = driver->owner;
730
731         ret = driver_register(&driver->drv);
732         return ret;
733 }
734
735 EXPORT_SYMBOL(acpi_bus_register_driver);
736
737 /**
738  * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
739  * @driver: driver to unregister
740  *
741  * Unregisters a driver with the ACPI bus.  Searches the namespace for all
742  * devices that match the driver's criteria and unbinds.
743  */
744 void acpi_bus_unregister_driver(struct acpi_driver *driver)
745 {
746         driver_unregister(&driver->drv);
747 }
748
749 EXPORT_SYMBOL(acpi_bus_unregister_driver);
750
751 /* --------------------------------------------------------------------------
752                                  Device Enumeration
753    -------------------------------------------------------------------------- */
754 static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
755 {
756         acpi_status status;
757         int ret;
758         struct acpi_device *device;
759
760         /*
761          * Fixed hardware devices do not appear in the namespace and do not
762          * have handles, but we fabricate acpi_devices for them, so we have
763          * to deal with them specially.
764          */
765         if (handle == NULL)
766                 return acpi_root;
767
768         do {
769                 status = acpi_get_parent(handle, &handle);
770                 if (status == AE_NULL_ENTRY)
771                         return NULL;
772                 if (ACPI_FAILURE(status))
773                         return acpi_root;
774
775                 ret = acpi_bus_get_device(handle, &device);
776                 if (ret == 0)
777                         return device;
778         } while (1);
779 }
780
781 acpi_status
782 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
783 {
784         acpi_status status;
785         acpi_handle tmp;
786         struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
787         union acpi_object *obj;
788
789         status = acpi_get_handle(handle, "_EJD", &tmp);
790         if (ACPI_FAILURE(status))
791                 return status;
792
793         status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
794         if (ACPI_SUCCESS(status)) {
795                 obj = buffer.pointer;
796                 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
797                                          ejd);
798                 kfree(buffer.pointer);
799         }
800         return status;
801 }
802 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
803
804 void acpi_bus_data_handler(acpi_handle handle, void *context)
805 {
806
807         /* TBD */
808
809         return;
810 }
811
812 static int acpi_bus_get_perf_flags(struct acpi_device *device)
813 {
814         device->performance.state = ACPI_STATE_UNKNOWN;
815         return 0;
816 }
817
818 static acpi_status
819 acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
820                                              struct acpi_device_wakeup *wakeup)
821 {
822         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
823         union acpi_object *package = NULL;
824         union acpi_object *element = NULL;
825         acpi_status status;
826         int i = 0;
827
828         if (!wakeup)
829                 return AE_BAD_PARAMETER;
830
831         /* _PRW */
832         status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
833         if (ACPI_FAILURE(status)) {
834                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
835                 return status;
836         }
837
838         package = (union acpi_object *)buffer.pointer;
839
840         if (!package || (package->package.count < 2)) {
841                 status = AE_BAD_DATA;
842                 goto out;
843         }
844
845         element = &(package->package.elements[0]);
846         if (!element) {
847                 status = AE_BAD_DATA;
848                 goto out;
849         }
850         if (element->type == ACPI_TYPE_PACKAGE) {
851                 if ((element->package.count < 2) ||
852                     (element->package.elements[0].type !=
853                      ACPI_TYPE_LOCAL_REFERENCE)
854                     || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
855                         status = AE_BAD_DATA;
856                         goto out;
857                 }
858                 wakeup->gpe_device =
859                     element->package.elements[0].reference.handle;
860                 wakeup->gpe_number =
861                     (u32) element->package.elements[1].integer.value;
862         } else if (element->type == ACPI_TYPE_INTEGER) {
863                 wakeup->gpe_device = NULL;
864                 wakeup->gpe_number = element->integer.value;
865         } else {
866                 status = AE_BAD_DATA;
867                 goto out;
868         }
869
870         element = &(package->package.elements[1]);
871         if (element->type != ACPI_TYPE_INTEGER) {
872                 status = AE_BAD_DATA;
873                 goto out;
874         }
875         wakeup->sleep_state = element->integer.value;
876
877         if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
878                 status = AE_NO_MEMORY;
879                 goto out;
880         }
881         wakeup->resources.count = package->package.count - 2;
882         for (i = 0; i < wakeup->resources.count; i++) {
883                 element = &(package->package.elements[i + 2]);
884                 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
885                         status = AE_BAD_DATA;
886                         goto out;
887                 }
888
889                 wakeup->resources.handles[i] = element->reference.handle;
890         }
891
892         acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
893
894  out:
895         kfree(buffer.pointer);
896
897         return status;
898 }
899
900 static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
901 {
902         struct acpi_device_id button_device_ids[] = {
903                 {"PNP0C0D", 0},
904                 {"PNP0C0C", 0},
905                 {"PNP0C0E", 0},
906                 {"", 0},
907         };
908         acpi_status status;
909         acpi_event_status event_status;
910
911         device->wakeup.flags.notifier_present = 0;
912
913         /* Power button, Lid switch always enable wakeup */
914         if (!acpi_match_device_ids(device, button_device_ids)) {
915                 device->wakeup.flags.run_wake = 1;
916                 device_set_wakeup_capable(&device->dev, true);
917                 return;
918         }
919
920         status = acpi_get_gpe_status(device->wakeup.gpe_device,
921                                         device->wakeup.gpe_number,
922                                                 &event_status);
923         if (status == AE_OK)
924                 device->wakeup.flags.run_wake =
925                                 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
926 }
927
928 static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
929 {
930         acpi_handle temp;
931         acpi_status status = 0;
932         int psw_error;
933
934         /* Presence of _PRW indicates wake capable */
935         status = acpi_get_handle(device->handle, "_PRW", &temp);
936         if (ACPI_FAILURE(status))
937                 return;
938
939         status = acpi_bus_extract_wakeup_device_power_package(device->handle,
940                                                               &device->wakeup);
941         if (ACPI_FAILURE(status)) {
942                 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
943                 return;
944         }
945
946         device->wakeup.flags.valid = 1;
947         device->wakeup.prepare_count = 0;
948         acpi_bus_set_run_wake_flags(device);
949         /* Call _PSW/_DSW object to disable its ability to wake the sleeping
950          * system for the ACPI device with the _PRW object.
951          * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
952          * So it is necessary to call _DSW object first. Only when it is not
953          * present will the _PSW object used.
954          */
955         psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
956         if (psw_error)
957                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
958                                 "error in _DSW or _PSW evaluation\n"));
959 }
960
961 static void acpi_bus_add_power_resource(acpi_handle handle);
962
963 static int acpi_bus_get_power_flags(struct acpi_device *device)
964 {
965         acpi_status status = 0;
966         acpi_handle handle = NULL;
967         u32 i = 0;
968
969
970         /*
971          * Power Management Flags
972          */
973         status = acpi_get_handle(device->handle, "_PSC", &handle);
974         if (ACPI_SUCCESS(status))
975                 device->power.flags.explicit_get = 1;
976         status = acpi_get_handle(device->handle, "_IRC", &handle);
977         if (ACPI_SUCCESS(status))
978                 device->power.flags.inrush_current = 1;
979
980         /*
981          * Enumerate supported power management states
982          */
983         for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
984                 struct acpi_device_power_state *ps = &device->power.states[i];
985                 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
986
987                 /* Evaluate "_PRx" to se if power resources are referenced */
988                 acpi_evaluate_reference(device->handle, object_name, NULL,
989                                         &ps->resources);
990                 if (ps->resources.count) {
991                         int j;
992
993                         device->power.flags.power_resources = 1;
994                         for (j = 0; j < ps->resources.count; j++)
995                                 acpi_bus_add_power_resource(ps->resources.handles[j]);
996                 }
997
998                 /* Evaluate "_PSx" to see if we can do explicit sets */
999                 object_name[2] = 'S';
1000                 status = acpi_get_handle(device->handle, object_name, &handle);
1001                 if (ACPI_SUCCESS(status))
1002                         ps->flags.explicit_set = 1;
1003
1004                 /*
1005                  * State is valid if there are means to put the device into it.
1006                  * D3hot is only valid if _PR3 present.
1007                  */
1008                 if (ps->resources.count ||
1009                     (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT)) {
1010                         ps->flags.valid = 1;
1011                         ps->flags.os_accessible = 1;
1012                 }
1013
1014                 ps->power = -1; /* Unknown - driver assigned */
1015                 ps->latency = -1;       /* Unknown - driver assigned */
1016         }
1017
1018         /* Set defaults for D0 and D3 states (always valid) */
1019         device->power.states[ACPI_STATE_D0].flags.valid = 1;
1020         device->power.states[ACPI_STATE_D0].power = 100;
1021         device->power.states[ACPI_STATE_D3].flags.valid = 1;
1022         device->power.states[ACPI_STATE_D3].power = 0;
1023
1024         /* Set D3cold's explicit_set flag if _PS3 exists. */
1025         if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
1026                 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
1027
1028         /* Presence of _PS3 or _PRx means we can put the device into D3 cold */
1029         if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set ||
1030                         device->power.flags.power_resources)
1031                 device->power.states[ACPI_STATE_D3_COLD].flags.os_accessible = 1;
1032
1033         acpi_bus_init_power(device);
1034
1035         return 0;
1036 }
1037
1038 static int acpi_bus_get_flags(struct acpi_device *device)
1039 {
1040         acpi_status status = AE_OK;
1041         acpi_handle temp = NULL;
1042
1043
1044         /* Presence of _STA indicates 'dynamic_status' */
1045         status = acpi_get_handle(device->handle, "_STA", &temp);
1046         if (ACPI_SUCCESS(status))
1047                 device->flags.dynamic_status = 1;
1048
1049         /* Presence of _RMV indicates 'removable' */
1050         status = acpi_get_handle(device->handle, "_RMV", &temp);
1051         if (ACPI_SUCCESS(status))
1052                 device->flags.removable = 1;
1053
1054         /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1055         status = acpi_get_handle(device->handle, "_EJD", &temp);
1056         if (ACPI_SUCCESS(status))
1057                 device->flags.ejectable = 1;
1058         else {
1059                 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1060                 if (ACPI_SUCCESS(status))
1061                         device->flags.ejectable = 1;
1062         }
1063
1064         /* Presence of _LCK indicates 'lockable' */
1065         status = acpi_get_handle(device->handle, "_LCK", &temp);
1066         if (ACPI_SUCCESS(status))
1067                 device->flags.lockable = 1;
1068
1069         /* Power resources cannot be power manageable. */
1070         if (device->device_type == ACPI_BUS_TYPE_POWER)
1071                 return 0;
1072
1073         /* Presence of _PS0|_PR0 indicates 'power manageable' */
1074         status = acpi_get_handle(device->handle, "_PS0", &temp);
1075         if (ACPI_FAILURE(status))
1076                 status = acpi_get_handle(device->handle, "_PR0", &temp);
1077         if (ACPI_SUCCESS(status))
1078                 device->flags.power_manageable = 1;
1079
1080         /* TBD: Performance management */
1081
1082         return 0;
1083 }
1084
1085 static void acpi_device_get_busid(struct acpi_device *device)
1086 {
1087         char bus_id[5] = { '?', 0 };
1088         struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1089         int i = 0;
1090
1091         /*
1092          * Bus ID
1093          * ------
1094          * The device's Bus ID is simply the object name.
1095          * TBD: Shouldn't this value be unique (within the ACPI namespace)?
1096          */
1097         if (ACPI_IS_ROOT_DEVICE(device)) {
1098                 strcpy(device->pnp.bus_id, "ACPI");
1099                 return;
1100         }
1101
1102         switch (device->device_type) {
1103         case ACPI_BUS_TYPE_POWER_BUTTON:
1104                 strcpy(device->pnp.bus_id, "PWRF");
1105                 break;
1106         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1107                 strcpy(device->pnp.bus_id, "SLPF");
1108                 break;
1109         default:
1110                 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1111                 /* Clean up trailing underscores (if any) */
1112                 for (i = 3; i > 1; i--) {
1113                         if (bus_id[i] == '_')
1114                                 bus_id[i] = '\0';
1115                         else
1116                                 break;
1117                 }
1118                 strcpy(device->pnp.bus_id, bus_id);
1119                 break;
1120         }
1121 }
1122
1123 /*
1124  * acpi_bay_match - see if a device is an ejectable driver bay
1125  *
1126  * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
1127  * then we can safely call it an ejectable drive bay
1128  */
1129 static int acpi_bay_match(struct acpi_device *device){
1130         acpi_status status;
1131         acpi_handle handle;
1132         acpi_handle tmp;
1133         acpi_handle phandle;
1134
1135         handle = device->handle;
1136
1137         status = acpi_get_handle(handle, "_EJ0", &tmp);
1138         if (ACPI_FAILURE(status))
1139                 return -ENODEV;
1140
1141         if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1142                 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1143                 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1144                 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1145                 return 0;
1146
1147         if (acpi_get_parent(handle, &phandle))
1148                 return -ENODEV;
1149
1150         if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1151                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1152                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1153                 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1154                 return 0;
1155
1156         return -ENODEV;
1157 }
1158
1159 /*
1160  * acpi_dock_match - see if a device has a _DCK method
1161  */
1162 static int acpi_dock_match(struct acpi_device *device)
1163 {
1164         acpi_handle tmp;
1165         return acpi_get_handle(device->handle, "_DCK", &tmp);
1166 }
1167
1168 const char *acpi_device_hid(struct acpi_device *device)
1169 {
1170         struct acpi_hardware_id *hid;
1171
1172         if (list_empty(&device->pnp.ids))
1173                 return dummy_hid;
1174
1175         hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1176         return hid->id;
1177 }
1178 EXPORT_SYMBOL(acpi_device_hid);
1179
1180 static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1181 {
1182         struct acpi_hardware_id *id;
1183
1184         id = kmalloc(sizeof(*id), GFP_KERNEL);
1185         if (!id)
1186                 return;
1187
1188         id->id = kstrdup(dev_id, GFP_KERNEL);
1189         if (!id->id) {
1190                 kfree(id);
1191                 return;
1192         }
1193
1194         list_add_tail(&id->list, &device->pnp.ids);
1195 }
1196
1197 /*
1198  * Old IBM workstations have a DSDT bug wherein the SMBus object
1199  * lacks the SMBUS01 HID and the methods do not have the necessary "_"
1200  * prefix.  Work around this.
1201  */
1202 static int acpi_ibm_smbus_match(struct acpi_device *device)
1203 {
1204         acpi_handle h_dummy;
1205         struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
1206         int result;
1207
1208         if (!dmi_name_in_vendors("IBM"))
1209                 return -ENODEV;
1210
1211         /* Look for SMBS object */
1212         result = acpi_get_name(device->handle, ACPI_SINGLE_NAME, &path);
1213         if (result)
1214                 return result;
1215
1216         if (strcmp("SMBS", path.pointer)) {
1217                 result = -ENODEV;
1218                 goto out;
1219         }
1220
1221         /* Does it have the necessary (but misnamed) methods? */
1222         result = -ENODEV;
1223         if (ACPI_SUCCESS(acpi_get_handle(device->handle, "SBI", &h_dummy)) &&
1224             ACPI_SUCCESS(acpi_get_handle(device->handle, "SBR", &h_dummy)) &&
1225             ACPI_SUCCESS(acpi_get_handle(device->handle, "SBW", &h_dummy)))
1226                 result = 0;
1227 out:
1228         kfree(path.pointer);
1229         return result;
1230 }
1231
1232 static void acpi_device_set_id(struct acpi_device *device)
1233 {
1234         acpi_status status;
1235         struct acpi_device_info *info;
1236         struct acpi_pnp_device_id_list *cid_list;
1237         int i;
1238
1239         switch (device->device_type) {
1240         case ACPI_BUS_TYPE_DEVICE:
1241                 if (ACPI_IS_ROOT_DEVICE(device)) {
1242                         acpi_add_id(device, ACPI_SYSTEM_HID);
1243                         break;
1244                 }
1245
1246                 status = acpi_get_object_info(device->handle, &info);
1247                 if (ACPI_FAILURE(status)) {
1248                         printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1249                         return;
1250                 }
1251
1252                 if (info->valid & ACPI_VALID_HID)
1253                         acpi_add_id(device, info->hardware_id.string);
1254                 if (info->valid & ACPI_VALID_CID) {
1255                         cid_list = &info->compatible_id_list;
1256                         for (i = 0; i < cid_list->count; i++)
1257                                 acpi_add_id(device, cid_list->ids[i].string);
1258                 }
1259                 if (info->valid & ACPI_VALID_ADR) {
1260                         device->pnp.bus_address = info->address;
1261                         device->flags.bus_address = 1;
1262                 }
1263
1264                 kfree(info);
1265
1266                 /*
1267                  * Some devices don't reliably have _HIDs & _CIDs, so add
1268                  * synthetic HIDs to make sure drivers can find them.
1269                  */
1270                 if (acpi_is_video_device(device))
1271                         acpi_add_id(device, ACPI_VIDEO_HID);
1272                 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1273                         acpi_add_id(device, ACPI_BAY_HID);
1274                 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1275                         acpi_add_id(device, ACPI_DOCK_HID);
1276                 else if (!acpi_ibm_smbus_match(device))
1277                         acpi_add_id(device, ACPI_SMBUS_IBM_HID);
1278                 else if (!acpi_device_hid(device) &&
1279                          ACPI_IS_ROOT_DEVICE(device->parent)) {
1280                         acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1281                         strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1282                         strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1283                 }
1284
1285                 break;
1286         case ACPI_BUS_TYPE_POWER:
1287                 acpi_add_id(device, ACPI_POWER_HID);
1288                 break;
1289         case ACPI_BUS_TYPE_PROCESSOR:
1290                 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1291                 break;
1292         case ACPI_BUS_TYPE_THERMAL:
1293                 acpi_add_id(device, ACPI_THERMAL_HID);
1294                 break;
1295         case ACPI_BUS_TYPE_POWER_BUTTON:
1296                 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1297                 break;
1298         case ACPI_BUS_TYPE_SLEEP_BUTTON:
1299                 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1300                 break;
1301         }
1302 }
1303
1304 static int acpi_device_set_context(struct acpi_device *device)
1305 {
1306         acpi_status status;
1307
1308         /*
1309          * Context
1310          * -------
1311          * Attach this 'struct acpi_device' to the ACPI object.  This makes
1312          * resolutions from handle->device very efficient.  Fixed hardware
1313          * devices have no handles, so we skip them.
1314          */
1315         if (!device->handle)
1316                 return 0;
1317
1318         status = acpi_attach_data(device->handle,
1319                                   acpi_bus_data_handler, device);
1320         if (ACPI_SUCCESS(status))
1321                 return 0;
1322
1323         printk(KERN_ERR PREFIX "Error attaching device data\n");
1324         return -ENODEV;
1325 }
1326
1327 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1328 {
1329         if (!dev)
1330                 return -EINVAL;
1331
1332         dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1333         device_release_driver(&dev->dev);
1334
1335         if (!rmdevice)
1336                 return 0;
1337
1338         /*
1339          * unbind _ADR-Based Devices when hot removal
1340          */
1341         if (dev->flags.bus_address) {
1342                 if ((dev->parent) && (dev->parent->ops.unbind))
1343                         dev->parent->ops.unbind(dev);
1344         }
1345         acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1346
1347         return 0;
1348 }
1349
1350 static int acpi_add_single_object(struct acpi_device **child,
1351                                   acpi_handle handle, int type,
1352                                   unsigned long long sta,
1353                                   struct acpi_bus_ops *ops)
1354 {
1355         int result;
1356         struct acpi_device *device;
1357         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1358
1359         device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1360         if (!device) {
1361                 printk(KERN_ERR PREFIX "Memory allocation error\n");
1362                 return -ENOMEM;
1363         }
1364
1365         INIT_LIST_HEAD(&device->pnp.ids);
1366         device->device_type = type;
1367         device->handle = handle;
1368         device->parent = acpi_bus_get_parent(handle);
1369         device->bus_ops = *ops; /* workround for not call .start */
1370         STRUCT_TO_INT(device->status) = sta;
1371
1372         acpi_device_get_busid(device);
1373
1374         /*
1375          * Flags
1376          * -----
1377          * Note that we only look for object handles -- cannot evaluate objects
1378          * until we know the device is present and properly initialized.
1379          */
1380         result = acpi_bus_get_flags(device);
1381         if (result)
1382                 goto end;
1383
1384         /*
1385          * Initialize Device
1386          * -----------------
1387          * TBD: Synch with Core's enumeration/initialization process.
1388          */
1389         acpi_device_set_id(device);
1390
1391         /*
1392          * Power Management
1393          * ----------------
1394          */
1395         if (device->flags.power_manageable) {
1396                 result = acpi_bus_get_power_flags(device);
1397                 if (result)
1398                         goto end;
1399         }
1400
1401         /*
1402          * Wakeup device management
1403          *-----------------------
1404          */
1405         acpi_bus_get_wakeup_device_flags(device);
1406
1407         /*
1408          * Performance Management
1409          * ----------------------
1410          */
1411         if (device->flags.performance_manageable) {
1412                 result = acpi_bus_get_perf_flags(device);
1413                 if (result)
1414                         goto end;
1415         }
1416
1417         if ((result = acpi_device_set_context(device)))
1418                 goto end;
1419
1420         result = acpi_device_register(device);
1421
1422         /*
1423          * Bind _ADR-Based Devices when hot add
1424          */
1425         if (device->flags.bus_address) {
1426                 if (device->parent && device->parent->ops.bind)
1427                         device->parent->ops.bind(device);
1428         }
1429
1430 end:
1431         if (!result) {
1432                 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1433                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1434                         "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1435                          (char *) buffer.pointer,
1436                          device->parent ? dev_name(&device->parent->dev) :
1437                                           "(null)"));
1438                 kfree(buffer.pointer);
1439                 *child = device;
1440         } else
1441                 acpi_device_release(&device->dev);
1442
1443         return result;
1444 }
1445
1446 #define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1447                           ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING)
1448
1449 static void acpi_bus_add_power_resource(acpi_handle handle)
1450 {
1451         struct acpi_bus_ops ops = {
1452                 .acpi_op_add = 1,
1453                 .acpi_op_start = 1,
1454         };
1455         struct acpi_device *device = NULL;
1456
1457         acpi_bus_get_device(handle, &device);
1458         if (!device)
1459                 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1460                                         ACPI_STA_DEFAULT, &ops);
1461 }
1462
1463 static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1464                                     unsigned long long *sta)
1465 {
1466         acpi_status status;
1467         acpi_object_type acpi_type;
1468
1469         status = acpi_get_type(handle, &acpi_type);
1470         if (ACPI_FAILURE(status))
1471                 return -ENODEV;
1472
1473         switch (acpi_type) {
1474         case ACPI_TYPE_ANY:             /* for ACPI_ROOT_OBJECT */
1475         case ACPI_TYPE_DEVICE:
1476                 *type = ACPI_BUS_TYPE_DEVICE;
1477                 status = acpi_bus_get_status_handle(handle, sta);
1478                 if (ACPI_FAILURE(status))
1479                         return -ENODEV;
1480                 break;
1481         case ACPI_TYPE_PROCESSOR:
1482                 *type = ACPI_BUS_TYPE_PROCESSOR;
1483                 status = acpi_bus_get_status_handle(handle, sta);
1484                 if (ACPI_FAILURE(status))
1485                         return -ENODEV;
1486                 break;
1487         case ACPI_TYPE_THERMAL:
1488                 *type = ACPI_BUS_TYPE_THERMAL;
1489                 *sta = ACPI_STA_DEFAULT;
1490                 break;
1491         case ACPI_TYPE_POWER:
1492                 *type = ACPI_BUS_TYPE_POWER;
1493                 *sta = ACPI_STA_DEFAULT;
1494                 break;
1495         default:
1496                 return -ENODEV;
1497         }
1498
1499         return 0;
1500 }
1501
1502 static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1503                                       void *context, void **return_value)
1504 {
1505         struct acpi_bus_ops *ops = context;
1506         int type;
1507         unsigned long long sta;
1508         struct acpi_device *device;
1509         acpi_status status;
1510         int result;
1511
1512         result = acpi_bus_type_and_status(handle, &type, &sta);
1513         if (result)
1514                 return AE_OK;
1515
1516         if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
1517             !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
1518                 struct acpi_device_wakeup wakeup;
1519                 acpi_handle temp;
1520
1521                 status = acpi_get_handle(handle, "_PRW", &temp);
1522                 if (ACPI_SUCCESS(status))
1523                         acpi_bus_extract_wakeup_device_power_package(handle,
1524                                                                      &wakeup);
1525                 return AE_CTRL_DEPTH;
1526         }
1527
1528         /*
1529          * We may already have an acpi_device from a previous enumeration.  If
1530          * so, we needn't add it again, but we may still have to start it.
1531          */
1532         device = NULL;
1533         acpi_bus_get_device(handle, &device);
1534         if (ops->acpi_op_add && !device) {
1535                 acpi_add_single_object(&device, handle, type, sta, ops);
1536                 /* Is the device a known good platform device? */
1537                 if (device
1538                     && !acpi_match_device_ids(device, acpi_platform_device_ids))
1539                         acpi_create_platform_device(device);
1540         }
1541
1542         if (!device)
1543                 return AE_CTRL_DEPTH;
1544
1545         if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1546                 status = acpi_start_single_object(device);
1547                 if (ACPI_FAILURE(status))
1548                         return AE_CTRL_DEPTH;
1549         }
1550
1551         if (!*return_value)
1552                 *return_value = device;
1553         return AE_OK;
1554 }
1555
1556 static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1557                          struct acpi_device **child)
1558 {
1559         acpi_status status;
1560         void *device = NULL;
1561
1562         status = acpi_bus_check_add(handle, 0, ops, &device);
1563         if (ACPI_SUCCESS(status))
1564                 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
1565                                     acpi_bus_check_add, NULL, ops, &device);
1566
1567         if (child)
1568                 *child = device;
1569
1570         if (device)
1571                 return 0;
1572         else
1573                 return -ENODEV;
1574 }
1575
1576 /*
1577  * acpi_bus_add and acpi_bus_start
1578  *
1579  * scan a given ACPI tree and (probably recently hot-plugged)
1580  * create and add or starts found devices.
1581  *
1582  * If no devices were found -ENODEV is returned which does not
1583  * mean that this is a real error, there just have been no suitable
1584  * ACPI objects in the table trunk from which the kernel could create
1585  * a device and add/start an appropriate driver.
1586  */
1587
1588 int
1589 acpi_bus_add(struct acpi_device **child,
1590              struct acpi_device *parent, acpi_handle handle, int type)
1591 {
1592         struct acpi_bus_ops ops;
1593
1594         memset(&ops, 0, sizeof(ops));
1595         ops.acpi_op_add = 1;
1596
1597         return acpi_bus_scan(handle, &ops, child);
1598 }
1599 EXPORT_SYMBOL(acpi_bus_add);
1600
1601 int acpi_bus_start(struct acpi_device *device)
1602 {
1603         struct acpi_bus_ops ops;
1604         int result;
1605
1606         if (!device)
1607                 return -EINVAL;
1608
1609         memset(&ops, 0, sizeof(ops));
1610         ops.acpi_op_start = 1;
1611
1612         result = acpi_bus_scan(device->handle, &ops, NULL);
1613
1614         acpi_update_all_gpes();
1615
1616         return result;
1617 }
1618 EXPORT_SYMBOL(acpi_bus_start);
1619
1620 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1621 {
1622         acpi_status status;
1623         struct acpi_device *parent, *child;
1624         acpi_handle phandle, chandle;
1625         acpi_object_type type;
1626         u32 level = 1;
1627         int err = 0;
1628
1629         parent = start;
1630         phandle = start->handle;
1631         child = chandle = NULL;
1632
1633         while ((level > 0) && parent && (!err)) {
1634                 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1635                                               chandle, &chandle);
1636
1637                 /*
1638                  * If this scope is exhausted then move our way back up.
1639                  */
1640                 if (ACPI_FAILURE(status)) {
1641                         level--;
1642                         chandle = phandle;
1643                         acpi_get_parent(phandle, &phandle);
1644                         child = parent;
1645                         parent = parent->parent;
1646
1647                         if (level == 0)
1648                                 err = acpi_bus_remove(child, rmdevice);
1649                         else
1650                                 err = acpi_bus_remove(child, 1);
1651
1652                         continue;
1653                 }
1654
1655                 status = acpi_get_type(chandle, &type);
1656                 if (ACPI_FAILURE(status)) {
1657                         continue;
1658                 }
1659                 /*
1660                  * If there is a device corresponding to chandle then
1661                  * parse it (depth-first).
1662                  */
1663                 if (acpi_bus_get_device(chandle, &child) == 0) {
1664                         level++;
1665                         phandle = chandle;
1666                         chandle = NULL;
1667                         parent = child;
1668                 }
1669                 continue;
1670         }
1671         return err;
1672 }
1673 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1674
1675 static int acpi_bus_scan_fixed(void)
1676 {
1677         int result = 0;
1678         struct acpi_device *device = NULL;
1679         struct acpi_bus_ops ops;
1680
1681         memset(&ops, 0, sizeof(ops));
1682         ops.acpi_op_add = 1;
1683         ops.acpi_op_start = 1;
1684
1685         /*
1686          * Enumerate all fixed-feature devices.
1687          */
1688         if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1689                 result = acpi_add_single_object(&device, NULL,
1690                                                 ACPI_BUS_TYPE_POWER_BUTTON,
1691                                                 ACPI_STA_DEFAULT,
1692                                                 &ops);
1693                 device_init_wakeup(&device->dev, true);
1694         }
1695
1696         if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1697                 result = acpi_add_single_object(&device, NULL,
1698                                                 ACPI_BUS_TYPE_SLEEP_BUTTON,
1699                                                 ACPI_STA_DEFAULT,
1700                                                 &ops);
1701         }
1702
1703         return result;
1704 }
1705
1706 int __init acpi_scan_init(void)
1707 {
1708         int result;
1709         struct acpi_bus_ops ops;
1710
1711         memset(&ops, 0, sizeof(ops));
1712         ops.acpi_op_add = 1;
1713         ops.acpi_op_start = 1;
1714
1715         result = bus_register(&acpi_bus_type);
1716         if (result) {
1717                 /* We don't want to quit even if we failed to add suspend/resume */
1718                 printk(KERN_ERR PREFIX "Could not register bus type\n");
1719         }
1720
1721         acpi_power_init();
1722
1723         /*
1724          * Enumerate devices in the ACPI namespace.
1725          */
1726         result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
1727
1728         if (!result)
1729                 result = acpi_bus_scan_fixed();
1730
1731         if (result)
1732                 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1733         else
1734                 acpi_update_all_gpes();
1735
1736         return result;
1737 }