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