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