ACPI: move acpi_no_s4_hw_signature() declaration into #ifdef CONFIG_HIBERNATION
[deliverable/linux.git] / drivers / acpi / scan.c
CommitLineData
1da177e4
LT
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>
5a0e3ad6 7#include <linux/slab.h>
9b6d97b6 8#include <linux/kernel.h>
1da177e4 9#include <linux/acpi.h>
74523c90
AK
10#include <linux/signal.h>
11#include <linux/kthread.h>
222e82ac 12#include <linux/dmi.h>
d1efe3c3 13#include <linux/nls.h>
1da177e4
LT
14
15#include <acpi/acpi_drivers.h>
1da177e4 16
e60cc7a6
BH
17#include "internal.h"
18
1da177e4 19#define _COMPONENT ACPI_BUS_COMPONENT
f52fd66d 20ACPI_MODULE_NAME("scan");
1da177e4 21#define STRUCT_TO_INT(s) (*((int*)&s))
4be44fcd 22extern struct acpi_device *acpi_root;
1da177e4
LT
23
24#define ACPI_BUS_CLASS "system_bus"
29b71a1c 25#define ACPI_BUS_HID "LNXSYBUS"
1da177e4
LT
26#define ACPI_BUS_DEVICE_NAME "System Bus"
27
859ac9a4
BH
28#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
29
620e112c 30static const char *dummy_hid = "device";
2b2ae7c7 31
1da177e4 32static LIST_HEAD(acpi_device_list);
e49bd2dd 33static LIST_HEAD(acpi_bus_id_list);
9090589d 34DEFINE_MUTEX(acpi_device_lock);
1da177e4
LT
35LIST_HEAD(acpi_wakeup_device_list);
36
e49bd2dd 37struct acpi_device_bus_id{
bb095854 38 char bus_id[15];
e49bd2dd
ZR
39 unsigned int instance_no;
40 struct list_head node;
1da177e4 41};
29b71a1c
TR
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*/
b3e572d2
AB
48static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
49 int size)
50{
29b71a1c 51 int len;
5c9fcb5d 52 int count;
7f47fa6c 53 struct acpi_hardware_id *id;
29b71a1c 54
2b2ae7c7
TR
55 if (list_empty(&acpi_dev->pnp.ids))
56 return 0;
57
5c9fcb5d 58 len = snprintf(modalias, size, "acpi:");
29b71a1c
TR
59 size -= len;
60
7f47fa6c
BH
61 list_for_each_entry(id, &acpi_dev->pnp.ids, list) {
62 count = snprintf(&modalias[len], size, "%s:", id->id);
5c9fcb5d
ZR
63 if (count < 0 || count >= size)
64 return -EINVAL;
65 len += count;
66 size -= count;
67 }
68
29b71a1c
TR
69 modalias[len] = '\0';
70 return len;
71}
72
73static ssize_t
74acpi_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}
85static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
86
c4753e57
TK
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 */
95void acpi_bus_hot_remove_device(void *context)
1da177e4 96{
c4753e57 97 struct acpi_eject_event *ej_event = (struct acpi_eject_event *) context;
26d46867 98 struct acpi_device *device;
c4753e57 99 acpi_handle handle = ej_event->handle;
b3c450c3 100 acpi_handle temp;
1da177e4
LT
101 struct acpi_object_list arg_list;
102 union acpi_object arg;
103 acpi_status status = AE_OK;
c4753e57 104 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
1da177e4 105
26d46867 106 if (acpi_bus_get_device(handle, &device))
c4753e57 107 goto err_out;
26d46867
ZR
108
109 if (!device)
c4753e57 110 goto err_out;
26d46867
ZR
111
112 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
0794469d 113 "Hot-removing device %s...\n", dev_name(&device->dev)));
26d46867
ZR
114
115 if (acpi_bus_trim(device, 1)) {
55ac9a01
LM
116 printk(KERN_ERR PREFIX
117 "Removing device failed\n");
c4753e57 118 goto err_out;
26d46867 119 }
1da177e4 120
b3c450c3
TK
121 /* device has been freed */
122 device = NULL;
123
26d46867
ZR
124 /* power off device */
125 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
126 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
55ac9a01
LM
127 printk(KERN_WARNING PREFIX
128 "Power-off device failed\n");
26d46867 129
b3c450c3 130 if (ACPI_SUCCESS(acpi_get_handle(handle, "_LCK", &temp))) {
1da177e4
LT
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 }
1da177e4 137
1da177e4
LT
138 arg_list.count = 1;
139 arg_list.pointer = &arg;
140 arg.type = ACPI_TYPE_INTEGER;
141 arg.integer.value = 1;
1da177e4 142
1da177e4
LT
143 /*
144 * TBD: _EJD support.
145 */
1da177e4 146 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
c4753e57
TK
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 }
1da177e4 153
c4753e57
TK
154 kfree(context);
155 return;
1da177e4 156
c4753e57
TK
157err_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);
c8d72a5e 162 return;
1da177e4
LT
163}
164
1da177e4 165static ssize_t
f883d9db
PM
166acpi_eject_store(struct device *d, struct device_attribute *attr,
167 const char *buf, size_t count)
1da177e4 168{
4be44fcd 169 int ret = count;
4be44fcd 170 acpi_status status;
4be44fcd 171 acpi_object_type type = 0;
f883d9db 172 struct acpi_device *acpi_device = to_acpi_device(d);
c4753e57 173 struct acpi_eject_event *ej_event;
1da177e4 174
1da177e4
LT
175 if ((!count) || (buf[0] != '1')) {
176 return -EINVAL;
177 }
1da177e4 178#ifndef FORCE_EJECT
f883d9db 179 if (acpi_device->driver == NULL) {
1da177e4
LT
180 ret = -ENODEV;
181 goto err;
182 }
183#endif
f883d9db
PM
184 status = acpi_get_type(acpi_device->handle, &type);
185 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
1da177e4
LT
186 ret = -ENODEV;
187 goto err;
188 }
1da177e4 189
c4753e57
TK
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);
74523c90 209err:
1da177e4 210 return ret;
1da177e4
LT
211}
212
f883d9db 213static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
1da177e4 214
e49bd2dd
ZR
215static ssize_t
216acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
217 struct acpi_device *acpi_dev = to_acpi_device(dev);
1da177e4 218
ea8d82fd 219 return sprintf(buf, "%s\n", acpi_device_hid(acpi_dev));
1da177e4 220}
e49bd2dd 221static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
1da177e4 222
e49bd2dd
ZR
223static ssize_t
224acpi_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;
1da177e4 228
e49bd2dd 229 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
0c526d96 230 if (result)
e49bd2dd 231 goto end;
1da177e4 232
e49bd2dd
ZR
233 result = sprintf(buf, "%s\n", (char*)path.pointer);
234 kfree(path.pointer);
0c526d96 235end:
e49bd2dd 236 return result;
1da177e4 237}
e49bd2dd 238static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
1da177e4 239
d1efe3c3
LO
240/* sysfs file that shows description text from the ACPI _STR method */
241static 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}
264static DEVICE_ATTR(description, 0444, description_show, NULL);
265
e49bd2dd 266static int acpi_device_setup_files(struct acpi_device *dev)
1da177e4 267{
d1efe3c3 268 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
f883d9db
PM
269 acpi_status status;
270 acpi_handle temp;
e49bd2dd 271 int result = 0;
1da177e4
LT
272
273 /*
e49bd2dd 274 * Devices gotten from FADT don't have a "path" attribute
1da177e4 275 */
0c526d96 276 if (dev->handle) {
e49bd2dd 277 result = device_create_file(&dev->dev, &dev_attr_path);
0c526d96 278 if (result)
e49bd2dd 279 goto end;
1da177e4
LT
280 }
281
2b2ae7c7
TR
282 if (!list_empty(&dev->pnp.ids)) {
283 result = device_create_file(&dev->dev, &dev_attr_hid);
284 if (result)
285 goto end;
1da177e4 286
2b2ae7c7
TR
287 result = device_create_file(&dev->dev, &dev_attr_modalias);
288 if (result)
289 goto end;
290 }
29b71a1c 291
d1efe3c3
LO
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
e49bd2dd
ZR
307 /*
308 * If device has _EJ0, 'eject' file is created that is used to trigger
309 * hot-removal function from userland.
310 */
f883d9db
PM
311 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
312 if (ACPI_SUCCESS(status))
e49bd2dd 313 result = device_create_file(&dev->dev, &dev_attr_eject);
0c526d96 314end:
e49bd2dd 315 return result;
1da177e4
LT
316}
317
f883d9db 318static void acpi_device_remove_files(struct acpi_device *dev)
1da177e4 319{
f883d9db
PM
320 acpi_status status;
321 acpi_handle temp;
1da177e4 322
f883d9db 323 /*
d1efe3c3
LO
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.
f883d9db
PM
333 */
334 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
335 if (ACPI_SUCCESS(status))
336 device_remove_file(&dev->dev, &dev_attr_eject);
1da177e4 337
1131b938
BH
338 device_remove_file(&dev->dev, &dev_attr_modalias);
339 device_remove_file(&dev->dev, &dev_attr_hid);
0c526d96 340 if (dev->handle)
e49bd2dd 341 device_remove_file(&dev->dev, &dev_attr_path);
1da177e4 342}
1da177e4 343/* --------------------------------------------------------------------------
9e89dde2 344 ACPI Bus operations
1da177e4 345 -------------------------------------------------------------------------- */
29b71a1c
TR
346
347int acpi_match_device_ids(struct acpi_device *device,
348 const struct acpi_device_id *ids)
349{
350 const struct acpi_device_id *id;
7f47fa6c 351 struct acpi_hardware_id *hwid;
29b71a1c 352
39a0ad87
ZY
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
7f47fa6c
BH
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))
29b71a1c 363 return 0;
29b71a1c
TR
364
365 return -ENOENT;
366}
367EXPORT_SYMBOL(acpi_match_device_ids);
368
7f47fa6c
BH
369static 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
1890a97a 379static void acpi_device_release(struct device *dev)
1da177e4 380{
1890a97a 381 struct acpi_device *acpi_dev = to_acpi_device(dev);
1da177e4 382
7f47fa6c 383 acpi_free_ids(acpi_dev);
1890a97a 384 kfree(acpi_dev);
1da177e4
LT
385}
386
5d9464a4 387static int acpi_bus_match(struct device *dev, struct device_driver *drv)
9e89dde2 388{
5d9464a4
PM
389 struct acpi_device *acpi_dev = to_acpi_device(dev);
390 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1da177e4 391
29b71a1c 392 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
5d9464a4 393}
1da177e4 394
7eff2e7a 395static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4 396{
5d9464a4 397 struct acpi_device *acpi_dev = to_acpi_device(dev);
7eff2e7a 398 int len;
5d9464a4 399
2b2ae7c7
TR
400 if (list_empty(&acpi_dev->pnp.ids))
401 return 0;
402
7eff2e7a
KS
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;
9e89dde2 410 return 0;
1da177e4
LT
411}
412
46ec8598
BH
413static 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
420static acpi_status acpi_device_notify_fixed(void *data)
421{
422 struct acpi_device *device = data;
423
53de5356
BH
424 /* Fixed hardware devices have no handles */
425 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
46ec8598
BH
426 return AE_OK;
427}
428
429static int acpi_device_install_notify_handler(struct acpi_device *device)
430{
431 acpi_status status;
46ec8598 432
ccba2a36 433 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
46ec8598
BH
434 status =
435 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
436 acpi_device_notify_fixed,
437 device);
ccba2a36 438 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
46ec8598
BH
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
454static void acpi_device_remove_notify_handler(struct acpi_device *device)
455{
ccba2a36 456 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
46ec8598
BH
457 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
458 acpi_device_notify_fixed);
ccba2a36 459 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
46ec8598
BH
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
5d9464a4
PM
467static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
468static int acpi_start_single_object(struct acpi_device *);
469static int acpi_device_probe(struct device * dev)
1da177e4 470{
5d9464a4
PM
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) {
c4168bff
LS
477 if (acpi_dev->bus_ops.acpi_op_start)
478 acpi_start_single_object(acpi_dev);
46ec8598
BH
479
480 if (acpi_drv->ops.notify) {
481 ret = acpi_device_install_notify_handler(acpi_dev);
482 if (ret) {
46ec8598
BH
483 if (acpi_drv->ops.remove)
484 acpi_drv->ops.remove(acpi_dev,
485 acpi_dev->removal_type);
486 return ret;
487 }
488 }
489
5d9464a4
PM
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}
1da177e4 497
5d9464a4
PM
498static 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) {
46ec8598
BH
504 if (acpi_drv->ops.notify)
505 acpi_device_remove_notify_handler(acpi_dev);
5d9464a4 506 if (acpi_drv->ops.remove)
96333578 507 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
1da177e4 508 }
5d9464a4 509 acpi_dev->driver = NULL;
db89b4f0 510 acpi_dev->driver_data = NULL;
1da177e4 511
5d9464a4 512 put_device(dev);
9e89dde2
ZR
513 return 0;
514}
1da177e4 515
55955aad 516struct bus_type acpi_bus_type = {
9e89dde2 517 .name = "acpi",
5d9464a4
PM
518 .match = acpi_bus_match,
519 .probe = acpi_device_probe,
520 .remove = acpi_device_remove,
521 .uevent = acpi_device_uevent,
9e89dde2
ZR
522};
523
66b7ed40 524static int acpi_device_register(struct acpi_device *device)
1da177e4 525{
4be44fcd 526 int result;
e49bd2dd
ZR
527 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
528 int found = 0;
66b7ed40 529
9e89dde2
ZR
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);
9e89dde2 537 INIT_LIST_HEAD(&device->wakeup_list);
1033f904
LT
538 INIT_LIST_HEAD(&device->physical_node_list);
539 mutex_init(&device->physical_node_lock);
1da177e4 540
e49bd2dd
ZR
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;
1da177e4 545 }
e49bd2dd 546
9090589d 547 mutex_lock(&acpi_device_lock);
e49bd2dd
ZR
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) {
1131b938
BH
553 if (!strcmp(acpi_device_bus_id->bus_id,
554 acpi_device_hid(device))) {
555 acpi_device_bus_id->instance_no++;
e49bd2dd
ZR
556 found = 1;
557 kfree(new_bus_id);
558 break;
559 }
1da177e4 560 }
0c526d96 561 if (!found) {
e49bd2dd 562 acpi_device_bus_id = new_bus_id;
1131b938 563 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
e49bd2dd
ZR
564 acpi_device_bus_id->instance_no = 0;
565 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1da177e4 566 }
0794469d 567 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1da177e4 568
33b57150 569 if (device->parent)
9e89dde2 570 list_add_tail(&device->node, &device->parent->children);
33b57150 571
9e89dde2
ZR
572 if (device->wakeup.flags.valid)
573 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
9090589d 574 mutex_unlock(&acpi_device_lock);
1da177e4 575
1890a97a 576 if (device->parent)
66b7ed40 577 device->dev.parent = &device->parent->dev;
1890a97a 578 device->dev.bus = &acpi_bus_type;
1890a97a 579 device->dev.release = &acpi_device_release;
8b12b922 580 result = device_register(&device->dev);
0c526d96 581 if (result) {
8b12b922 582 dev_err(&device->dev, "Error registering device\n");
e49bd2dd 583 goto end;
1da177e4 584 }
1da177e4 585
e49bd2dd 586 result = acpi_device_setup_files(device);
0c526d96 587 if (result)
0794469d
KS
588 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
589 dev_name(&device->dev));
1da177e4 590
96333578 591 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
1da177e4 592 return 0;
0c526d96 593end:
9090589d 594 mutex_lock(&acpi_device_lock);
33b57150 595 if (device->parent)
e49bd2dd 596 list_del(&device->node);
e49bd2dd 597 list_del(&device->wakeup_list);
9090589d 598 mutex_unlock(&acpi_device_lock);
e49bd2dd 599 return result;
1da177e4
LT
600}
601
9e89dde2
ZR
602static void acpi_device_unregister(struct acpi_device *device, int type)
603{
9090589d 604 mutex_lock(&acpi_device_lock);
33b57150 605 if (device->parent)
9e89dde2 606 list_del(&device->node);
1da177e4 607
9e89dde2 608 list_del(&device->wakeup_list);
9090589d 609 mutex_unlock(&acpi_device_lock);
1da177e4 610
9e89dde2 611 acpi_detach_data(device->handle, acpi_bus_data_handler);
1890a97a 612
f883d9db 613 acpi_device_remove_files(device);
1890a97a 614 device_unregister(&device->dev);
1da177e4
LT
615}
616
9e89dde2
ZR
617/* --------------------------------------------------------------------------
618 Driver Management
619 -------------------------------------------------------------------------- */
1da177e4 620/**
d758a8fa
RD
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 *
0c526d96 625 * Used to initialize a device via its device driver. Called whenever a
1890a97a 626 * driver is bound to a device. Invokes the driver's add() ops.
1da177e4
LT
627 */
628static int
4be44fcd 629acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
1da177e4 630{
4be44fcd 631 int result = 0;
1da177e4 632
1da177e4 633 if (!device || !driver)
d550d98d 634 return -EINVAL;
1da177e4
LT
635
636 if (!driver->ops.add)
d550d98d 637 return -ENOSYS;
1da177e4
LT
638
639 result = driver->ops.add(device);
640 if (result) {
641 device->driver = NULL;
db89b4f0 642 device->driver_data = NULL;
d550d98d 643 return result;
1da177e4
LT
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
4be44fcd
LB
653 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
654 "Driver successfully bound to device\n"));
d550d98d 655 return 0;
3fb02738
RS
656}
657
8713cbef 658static int acpi_start_single_object(struct acpi_device *device)
3fb02738
RS
659{
660 int result = 0;
661 struct acpi_driver *driver;
662
3fb02738
RS
663
664 if (!(driver = device->driver))
d550d98d 665 return 0;
3fb02738 666
1da177e4
LT
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);
1da177e4
LT
671 }
672
d550d98d 673 return result;
1da177e4
LT
674}
675
9e89dde2
ZR
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 */
684int acpi_bus_register_driver(struct acpi_driver *driver)
1da177e4 685{
1890a97a 686 int ret;
1da177e4 687
9e89dde2
ZR
688 if (acpi_disabled)
689 return -ENODEV;
1890a97a
PM
690 driver->drv.name = driver->name;
691 driver->drv.bus = &acpi_bus_type;
692 driver->drv.owner = driver->owner;
1da177e4 693
1890a97a
PM
694 ret = driver_register(&driver->drv);
695 return ret;
9e89dde2 696}
1da177e4 697
9e89dde2
ZR
698EXPORT_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 */
707void acpi_bus_unregister_driver(struct acpi_driver *driver)
708{
1890a97a 709 driver_unregister(&driver->drv);
9e89dde2
ZR
710}
711
712EXPORT_SYMBOL(acpi_bus_unregister_driver);
713
9e89dde2
ZR
714/* --------------------------------------------------------------------------
715 Device Enumeration
716 -------------------------------------------------------------------------- */
5c478f49
BH
717static 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
9e89dde2
ZR
744acpi_status
745acpi_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;
3b5fee59
HM
759 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
760 ejd);
9e89dde2
ZR
761 kfree(buffer.pointer);
762 }
763 return status;
764}
765EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
766
8e4319c4 767void acpi_bus_data_handler(acpi_handle handle, void *context)
9e89dde2
ZR
768{
769
770 /* TBD */
771
772 return;
773}
774
9e89dde2
ZR
775static int acpi_bus_get_perf_flags(struct acpi_device *device)
776{
777 device->performance.state = ACPI_STATE_UNKNOWN;
778 return 0;
779}
780
781static acpi_status
b581a7f9
RW
782acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
783 struct acpi_device_wakeup *wakeup)
9e89dde2 784{
b581a7f9
RW
785 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
786 union acpi_object *package = NULL;
9e89dde2 787 union acpi_object *element = NULL;
b581a7f9
RW
788 acpi_status status;
789 int i = 0;
9e89dde2 790
b581a7f9 791 if (!wakeup)
9e89dde2
ZR
792 return AE_BAD_PARAMETER;
793
b581a7f9
RW
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
9e89dde2 808 element = &(package->package.elements[0]);
b581a7f9
RW
809 if (!element) {
810 status = AE_BAD_DATA;
811 goto out;
812 }
9e89dde2
ZR
813 if (element->type == ACPI_TYPE_PACKAGE) {
814 if ((element->package.count < 2) ||
815 (element->package.elements[0].type !=
816 ACPI_TYPE_LOCAL_REFERENCE)
b581a7f9
RW
817 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
818 status = AE_BAD_DATA;
819 goto out;
820 }
821 wakeup->gpe_device =
9e89dde2 822 element->package.elements[0].reference.handle;
b581a7f9 823 wakeup->gpe_number =
9e89dde2
ZR
824 (u32) element->package.elements[1].integer.value;
825 } else if (element->type == ACPI_TYPE_INTEGER) {
b581a7f9
RW
826 wakeup->gpe_device = NULL;
827 wakeup->gpe_number = element->integer.value;
828 } else {
829 status = AE_BAD_DATA;
830 goto out;
831 }
9e89dde2
ZR
832
833 element = &(package->package.elements[1]);
834 if (element->type != ACPI_TYPE_INTEGER) {
b581a7f9
RW
835 status = AE_BAD_DATA;
836 goto out;
9e89dde2 837 }
b581a7f9 838 wakeup->sleep_state = element->integer.value;
9e89dde2
ZR
839
840 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
b581a7f9
RW
841 status = AE_NO_MEMORY;
842 goto out;
9e89dde2 843 }
b581a7f9
RW
844 wakeup->resources.count = package->package.count - 2;
845 for (i = 0; i < wakeup->resources.count; i++) {
9e89dde2 846 element = &(package->package.elements[i + 2]);
b581a7f9
RW
847 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
848 status = AE_BAD_DATA;
849 goto out;
850 }
9e89dde2 851
b581a7f9 852 wakeup->resources.handles[i] = element->reference.handle;
1da177e4 853 }
9e89dde2 854
bba63a29 855 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
9874647b 856
b581a7f9
RW
857 out:
858 kfree(buffer.pointer);
859
860 return status;
1da177e4
LT
861}
862
f517709d 863static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1da177e4 864{
29b71a1c
TR
865 struct acpi_device_id button_device_ids[] = {
866 {"PNP0C0D", 0},
867 {"PNP0C0C", 0},
868 {"PNP0C0E", 0},
869 {"", 0},
870 };
f517709d
RW
871 acpi_status status;
872 acpi_event_status event_status;
873
b67ea761 874 device->wakeup.flags.notifier_present = 0;
f517709d
RW
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;
f2b56bc8 879 device_set_wakeup_capable(&device->dev, true);
f517709d
RW
880 return;
881 }
882
e8e18c95
RW
883 status = acpi_get_gpe_status(device->wakeup.gpe_device,
884 device->wakeup.gpe_number,
885 &event_status);
f517709d
RW
886 if (status == AE_OK)
887 device->wakeup.flags.run_wake =
888 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
889}
890
d57d09a4 891static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
f517709d 892{
d57d09a4 893 acpi_handle temp;
f517709d 894 acpi_status status = 0;
f517709d 895 int psw_error;
29b71a1c 896
d57d09a4
RW
897 /* Presence of _PRW indicates wake capable */
898 status = acpi_get_handle(device->handle, "_PRW", &temp);
899 if (ACPI_FAILURE(status))
900 return;
901
b581a7f9
RW
902 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
903 &device->wakeup);
9e89dde2
ZR
904 if (ACPI_FAILURE(status)) {
905 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
d57d09a4 906 return;
9e89dde2 907 }
1da177e4 908
9e89dde2 909 device->wakeup.flags.valid = 1;
9b83ccd2 910 device->wakeup.prepare_count = 0;
f517709d 911 acpi_bus_set_run_wake_flags(device);
729b2bdb
ZY
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 */
77e76609
RW
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"));
1da177e4 922}
1da177e4 923
bf325f95
RW
924static void acpi_bus_add_power_resource(acpi_handle handle);
925
9e89dde2 926static int acpi_bus_get_power_flags(struct acpi_device *device)
1da177e4 927{
9e89dde2
ZR
928 acpi_status status = 0;
929 acpi_handle handle = NULL;
930 u32 i = 0;
1a365616 931
4be44fcd 932
9e89dde2
ZR
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;
1da177e4 942
9e89dde2
ZR
943 /*
944 * Enumerate supported power management states
945 */
1cc0c998 946 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
9e89dde2
ZR
947 struct acpi_device_power_state *ps = &device->power.states[i];
948 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
1da177e4 949
9e89dde2
ZR
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) {
bf325f95
RW
954 int j;
955
9e89dde2 956 device->power.flags.power_resources = 1;
bf325f95
RW
957 for (j = 0; j < ps->resources.count; j++)
958 acpi_bus_add_power_resource(ps->resources.handles[j]);
1da177e4 959 }
1da177e4 960
9e89dde2
ZR
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);
37239978 964 if (ACPI_SUCCESS(status))
9e89dde2 965 ps->flags.explicit_set = 1;
1da177e4 966
1cc0c998
LM
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))
9e89dde2 973 ps->flags.valid = 1;
1da177e4 974
9e89dde2
ZR
975 ps->power = -1; /* Unknown - driver assigned */
976 ps->latency = -1; /* Unknown - driver assigned */
977 }
1da177e4 978
9e89dde2
ZR
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;
c8f7a62c 984
5c7dd710
RW
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
ade3e7fe 989 acpi_bus_init_power(device);
c8f7a62c 990
9e89dde2
ZR
991 return 0;
992}
c8f7a62c 993
4be44fcd 994static int acpi_bus_get_flags(struct acpi_device *device)
1da177e4 995{
4be44fcd
LB
996 acpi_status status = AE_OK;
997 acpi_handle temp = NULL;
1da177e4 998
1da177e4
LT
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
1da177e4
LT
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
7bed50c5
RW
1020 /* Power resources cannot be power manageable. */
1021 if (device->device_type == ACPI_BUS_TYPE_POWER)
1022 return 0;
1023
1da177e4
LT
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
3c5f9be4 1031 /* TBD: Performance management */
1da177e4 1032
d550d98d 1033 return 0;
1da177e4
LT
1034}
1035
c7bcb4e9 1036static void acpi_device_get_busid(struct acpi_device *device)
1da177e4 1037{
4be44fcd
LB
1038 char bus_id[5] = { '?', 0 };
1039 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
1040 int i = 0;
1da177e4
LT
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 */
859ac9a4 1048 if (ACPI_IS_ROOT_DEVICE(device)) {
1da177e4 1049 strcpy(device->pnp.bus_id, "ACPI");
859ac9a4
BH
1050 return;
1051 }
1052
1053 switch (device->device_type) {
1da177e4
LT
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:
66b7ed40 1061 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
1da177e4
LT
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
54735266
ZR
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 */
1080static 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
3620f2f2
FS
1110/*
1111 * acpi_dock_match - see if a device has a _DCK method
1112 */
1113static int acpi_dock_match(struct acpi_device *device)
1114{
1115 acpi_handle tmp;
1116 return acpi_get_handle(device->handle, "_DCK", &tmp);
1117}
1118
620e112c 1119const char *acpi_device_hid(struct acpi_device *device)
15b8dd53 1120{
7f47fa6c 1121 struct acpi_hardware_id *hid;
15b8dd53 1122
2b2ae7c7
TR
1123 if (list_empty(&device->pnp.ids))
1124 return dummy_hid;
1125
7f47fa6c
BH
1126 hid = list_first_entry(&device->pnp.ids, struct acpi_hardware_id, list);
1127 return hid->id;
1128}
1129EXPORT_SYMBOL(acpi_device_hid);
15b8dd53 1130
7f47fa6c
BH
1131static void acpi_add_id(struct acpi_device *device, const char *dev_id)
1132{
1133 struct acpi_hardware_id *id;
15b8dd53 1134
7f47fa6c
BH
1135 id = kmalloc(sizeof(*id), GFP_KERNEL);
1136 if (!id)
1137 return;
15b8dd53 1138
581de59e 1139 id->id = kstrdup(dev_id, GFP_KERNEL);
7f47fa6c
BH
1140 if (!id->id) {
1141 kfree(id);
1142 return;
15b8dd53
BM
1143 }
1144
7f47fa6c 1145 list_add_tail(&id->list, &device->pnp.ids);
15b8dd53
BM
1146}
1147
222e82ac
DW
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 */
1153static 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;
1178out:
1179 kfree(path.pointer);
1180 return result;
1181}
1182
c7bcb4e9 1183static void acpi_device_set_id(struct acpi_device *device)
1da177e4 1184{
4be44fcd 1185 acpi_status status;
57f3674f
BH
1186 struct acpi_device_info *info;
1187 struct acpica_device_id_list *cid_list;
7f47fa6c 1188 int i;
1da177e4 1189
c7bcb4e9 1190 switch (device->device_type) {
1da177e4 1191 case ACPI_BUS_TYPE_DEVICE:
859ac9a4 1192 if (ACPI_IS_ROOT_DEVICE(device)) {
57f3674f 1193 acpi_add_id(device, ACPI_SYSTEM_HID);
859ac9a4
BH
1194 break;
1195 }
1196
66b7ed40 1197 status = acpi_get_object_info(device->handle, &info);
1da177e4 1198 if (ACPI_FAILURE(status)) {
96b2dd1f 1199 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1da177e4
LT
1200 return;
1201 }
1202
1da177e4 1203 if (info->valid & ACPI_VALID_HID)
57f3674f
BH
1204 acpi_add_id(device, info->hardware_id.string);
1205 if (info->valid & ACPI_VALID_CID) {
15b8dd53 1206 cid_list = &info->compatible_id_list;
57f3674f
BH
1207 for (i = 0; i < cid_list->count; i++)
1208 acpi_add_id(device, cid_list->ids[i].string);
1209 }
1da177e4
LT
1210 if (info->valid & ACPI_VALID_ADR) {
1211 device->pnp.bus_address = info->address;
1212 device->flags.bus_address = 1;
1213 }
ae843332 1214
a83893ae
BH
1215 kfree(info);
1216
57f3674f
BH
1217 /*
1218 * Some devices don't reliably have _HIDs & _CIDs, so add
1219 * synthetic HIDs to make sure drivers can find them.
1220 */
c3d6de69 1221 if (acpi_is_video_device(device))
57f3674f 1222 acpi_add_id(device, ACPI_VIDEO_HID);
3620f2f2 1223 else if (ACPI_SUCCESS(acpi_bay_match(device)))
57f3674f 1224 acpi_add_id(device, ACPI_BAY_HID);
3620f2f2 1225 else if (ACPI_SUCCESS(acpi_dock_match(device)))
57f3674f 1226 acpi_add_id(device, ACPI_DOCK_HID);
222e82ac
DW
1227 else if (!acpi_ibm_smbus_match(device))
1228 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
b7b30de5
BH
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 }
54735266 1235
1da177e4
LT
1236 break;
1237 case ACPI_BUS_TYPE_POWER:
57f3674f 1238 acpi_add_id(device, ACPI_POWER_HID);
1da177e4
LT
1239 break;
1240 case ACPI_BUS_TYPE_PROCESSOR:
57f3674f 1241 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1da177e4 1242 break;
1da177e4 1243 case ACPI_BUS_TYPE_THERMAL:
57f3674f 1244 acpi_add_id(device, ACPI_THERMAL_HID);
1da177e4
LT
1245 break;
1246 case ACPI_BUS_TYPE_POWER_BUTTON:
57f3674f 1247 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1da177e4
LT
1248 break;
1249 case ACPI_BUS_TYPE_SLEEP_BUTTON:
57f3674f 1250 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1da177e4
LT
1251 break;
1252 }
1da177e4
LT
1253}
1254
bc3b0772 1255static int acpi_device_set_context(struct acpi_device *device)
1da177e4 1256{
bc3b0772
BH
1257 acpi_status status;
1258
1da177e4
LT
1259 /*
1260 * Context
1261 * -------
1262 * Attach this 'struct acpi_device' to the ACPI object. This makes
bc3b0772
BH
1263 * resolutions from handle->device very efficient. Fixed hardware
1264 * devices have no handles, so we skip them.
1da177e4 1265 */
bc3b0772
BH
1266 if (!device->handle)
1267 return 0;
1da177e4 1268
bc3b0772
BH
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;
1da177e4
LT
1276}
1277
4be44fcd 1278static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1da177e4 1279{
1da177e4 1280 if (!dev)
d550d98d 1281 return -EINVAL;
1da177e4 1282
96333578 1283 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1890a97a 1284 device_release_driver(&dev->dev);
1da177e4
LT
1285
1286 if (!rmdevice)
d550d98d 1287 return 0;
1da177e4 1288
2786f6e3
RZ
1289 /*
1290 * unbind _ADR-Based Devices when hot removal
1291 */
1da177e4
LT
1292 if (dev->flags.bus_address) {
1293 if ((dev->parent) && (dev->parent->ops.unbind))
1294 dev->parent->ops.unbind(dev);
1295 }
1da177e4
LT
1296 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1297
d550d98d 1298 return 0;
1da177e4
LT
1299}
1300
5c478f49
BH
1301static int acpi_add_single_object(struct acpi_device **child,
1302 acpi_handle handle, int type,
778cbc1d 1303 unsigned long long sta,
5c478f49 1304 struct acpi_bus_ops *ops)
1da177e4 1305{
77c24888
BH
1306 int result;
1307 struct acpi_device *device;
29aaefa6 1308 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 1309
36bcbec7 1310 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1da177e4 1311 if (!device) {
6468463a 1312 printk(KERN_ERR PREFIX "Memory allocation error\n");
d550d98d 1313 return -ENOMEM;
1da177e4 1314 }
1da177e4 1315
7f47fa6c 1316 INIT_LIST_HEAD(&device->pnp.ids);
caaa6efb 1317 device->device_type = type;
1da177e4 1318 device->handle = handle;
5c478f49 1319 device->parent = acpi_bus_get_parent(handle);
c4168bff 1320 device->bus_ops = *ops; /* workround for not call .start */
778cbc1d 1321 STRUCT_TO_INT(device->status) = sta;
c4168bff 1322
c7bcb4e9 1323 acpi_device_get_busid(device);
1da177e4
LT
1324
1325 /*
1326 * Flags
1327 * -----
778cbc1d
BH
1328 * Note that we only look for object handles -- cannot evaluate objects
1329 * until we know the device is present and properly initialized.
1da177e4
LT
1330 */
1331 result = acpi_bus_get_flags(device);
1332 if (result)
1333 goto end;
1334
1da177e4
LT
1335 /*
1336 * Initialize Device
1337 * -----------------
1338 * TBD: Synch with Core's enumeration/initialization process.
1339 */
c7bcb4e9 1340 acpi_device_set_id(device);
1da177e4
LT
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
4be44fcd 1352 /*
1da177e4
LT
1353 * Wakeup device management
1354 *-----------------------
1355 */
d57d09a4 1356 acpi_bus_get_wakeup_device_flags(device);
1da177e4
LT
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
bc3b0772 1368 if ((result = acpi_device_set_context(device)))
f61f9258 1369 goto end;
1da177e4 1370
66b7ed40 1371 result = acpi_device_register(device);
1da177e4
LT
1372
1373 /*
2786f6e3 1374 * Bind _ADR-Based Devices when hot add
1da177e4
LT
1375 */
1376 if (device->flags.bus_address) {
1377 if (device->parent && device->parent->ops.bind)
1378 device->parent->ops.bind(device);
1379 }
1380
0c526d96 1381end:
29aaefa6
BH
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);
1da177e4 1390 *child = device;
29aaefa6 1391 } else
718fb0de 1392 acpi_device_release(&device->dev);
1da177e4 1393
d550d98d 1394 return result;
1da177e4 1395}
1da177e4 1396
778cbc1d
BH
1397#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1398 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1399
bf325f95
RW
1400static 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
778cbc1d
BH
1414static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1415 unsigned long long *sta)
1da177e4 1416{
778cbc1d
BH
1417 acpi_status status;
1418 acpi_object_type acpi_type;
1da177e4 1419
778cbc1d 1420 status = acpi_get_type(handle, &acpi_type);
51a85faf 1421 if (ACPI_FAILURE(status))
778cbc1d 1422 return -ENODEV;
4be44fcd 1423
778cbc1d 1424 switch (acpi_type) {
51a85faf
BH
1425 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1426 case ACPI_TYPE_DEVICE:
778cbc1d
BH
1427 *type = ACPI_BUS_TYPE_DEVICE;
1428 status = acpi_bus_get_status_handle(handle, sta);
1429 if (ACPI_FAILURE(status))
1430 return -ENODEV;
51a85faf
BH
1431 break;
1432 case ACPI_TYPE_PROCESSOR:
778cbc1d
BH
1433 *type = ACPI_BUS_TYPE_PROCESSOR;
1434 status = acpi_bus_get_status_handle(handle, sta);
1435 if (ACPI_FAILURE(status))
1436 return -ENODEV;
51a85faf
BH
1437 break;
1438 case ACPI_TYPE_THERMAL:
778cbc1d
BH
1439 *type = ACPI_BUS_TYPE_THERMAL;
1440 *sta = ACPI_STA_DEFAULT;
51a85faf
BH
1441 break;
1442 case ACPI_TYPE_POWER:
778cbc1d
BH
1443 *type = ACPI_BUS_TYPE_POWER;
1444 *sta = ACPI_STA_DEFAULT;
51a85faf
BH
1445 break;
1446 default:
778cbc1d 1447 return -ENODEV;
51a85faf 1448 }
1da177e4 1449
778cbc1d
BH
1450 return 0;
1451}
1452
1453static 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;
778cbc1d
BH
1457 int type;
1458 unsigned long long sta;
e3b87f8a
BH
1459 struct acpi_device *device;
1460 acpi_status status;
778cbc1d
BH
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) &&
b581a7f9 1468 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
86e4e20e
RW
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);
778cbc1d 1476 return AE_CTRL_DEPTH;
b581a7f9 1477 }
778cbc1d 1478
e3b87f8a
BH
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);
1da177e4 1487
e3b87f8a 1488 if (!device)
51a85faf 1489 return AE_CTRL_DEPTH;
1da177e4 1490
51a85faf
BH
1491 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1492 status = acpi_start_single_object(device);
1da177e4 1493 if (ACPI_FAILURE(status))
51a85faf
BH
1494 return AE_CTRL_DEPTH;
1495 }
1da177e4 1496
51a85faf
BH
1497 if (!*return_value)
1498 *return_value = device;
1499 return AE_OK;
1500}
3fb02738 1501
51a85faf
BH
1502static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1503 struct acpi_device **child)
1504{
1505 acpi_status status;
51a85faf 1506 void *device = NULL;
3fb02738 1507
51a85faf
BH
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,
2263576c 1511 acpi_bus_check_add, NULL, ops, &device);
1da177e4 1512
51a85faf
BH
1513 if (child)
1514 *child = device;
7779688f
TR
1515
1516 if (device)
1517 return 0;
1518 else
1519 return -ENODEV;
1da177e4 1520}
1da177e4 1521
7779688f
TR
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
3fb02738 1534int
4be44fcd
LB
1535acpi_bus_add(struct acpi_device **child,
1536 struct acpi_device *parent, acpi_handle handle, int type)
3fb02738 1537{
3fb02738
RS
1538 struct acpi_bus_ops ops;
1539
c4168bff
LS
1540 memset(&ops, 0, sizeof(ops));
1541 ops.acpi_op_add = 1;
3fb02738 1542
7779688f 1543 return acpi_bus_scan(handle, &ops, child);
3fb02738
RS
1544}
1545EXPORT_SYMBOL(acpi_bus_add);
1546
4be44fcd 1547int acpi_bus_start(struct acpi_device *device)
3fb02738 1548{
3fb02738 1549 struct acpi_bus_ops ops;
a2100801 1550 int result;
3fb02738 1551
d2f6650a
TR
1552 if (!device)
1553 return -EINVAL;
1554
8e029bf0
BH
1555 memset(&ops, 0, sizeof(ops));
1556 ops.acpi_op_start = 1;
3fb02738 1557
a2100801
RW
1558 result = acpi_bus_scan(device->handle, &ops, NULL);
1559
3a37898d 1560 acpi_update_all_gpes();
a2100801
RW
1561
1562 return result;
3fb02738
RS
1563}
1564EXPORT_SYMBOL(acpi_bus_start);
1da177e4 1565
ceaba663 1566int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1da177e4 1567{
4be44fcd
LB
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;
1da177e4
LT
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,
4be44fcd 1581 chandle, &chandle);
1da177e4
LT
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}
ceaba663
KA
1619EXPORT_SYMBOL_GPL(acpi_bus_trim);
1620
e8b945c9 1621static int acpi_bus_scan_fixed(void)
1da177e4 1622{
4be44fcd
LB
1623 int result = 0;
1624 struct acpi_device *device = NULL;
c4168bff 1625 struct acpi_bus_ops ops;
1da177e4 1626
c4168bff
LS
1627 memset(&ops, 0, sizeof(ops));
1628 ops.acpi_op_add = 1;
1629 ops.acpi_op_start = 1;
1630
1da177e4
LT
1631 /*
1632 * Enumerate all fixed-feature devices.
1633 */
cee324b1 1634 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
5c478f49 1635 result = acpi_add_single_object(&device, NULL,
c4168bff 1636 ACPI_BUS_TYPE_POWER_BUTTON,
778cbc1d 1637 ACPI_STA_DEFAULT,
c4168bff 1638 &ops);
c10d7a13 1639 device_init_wakeup(&device->dev, true);
3fb02738 1640 }
1da177e4 1641
cee324b1 1642 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
5c478f49 1643 result = acpi_add_single_object(&device, NULL,
c4168bff 1644 ACPI_BUS_TYPE_SLEEP_BUTTON,
778cbc1d 1645 ACPI_STA_DEFAULT,
c4168bff 1646 &ops);
3fb02738 1647 }
1da177e4 1648
d550d98d 1649 return result;
1da177e4
LT
1650}
1651
e747f274 1652int __init acpi_scan_init(void)
1da177e4
LT
1653{
1654 int result;
3fb02738 1655 struct acpi_bus_ops ops;
1da177e4 1656
c4168bff
LS
1657 memset(&ops, 0, sizeof(ops));
1658 ops.acpi_op_add = 1;
1659 ops.acpi_op_start = 1;
1da177e4 1660
5b327265
PM
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
97d9a9e9
RW
1667 acpi_power_init();
1668
1da177e4
LT
1669 /*
1670 * Enumerate devices in the ACPI namespace.
1671 */
51a85faf 1672 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
c04209a7 1673
c4168bff 1674 if (!result)
adc08e20 1675 result = acpi_bus_scan_fixed();
1da177e4
LT
1676
1677 if (result)
1678 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
a2100801 1679 else
3a37898d 1680 acpi_update_all_gpes();
1da177e4 1681
d550d98d 1682 return result;
1da177e4 1683}
This page took 0.946655 seconds and 5 git commands to generate.