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