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