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