ACPI: Add _UID support for ACPI devices.
[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);
ccf78040 384 kfree(acpi_dev->pnp.unique_id);
1890a97a 385 kfree(acpi_dev);
1da177e4
LT
386}
387
5d9464a4 388static int acpi_bus_match(struct device *dev, struct device_driver *drv)
9e89dde2 389{
5d9464a4
PM
390 struct acpi_device *acpi_dev = to_acpi_device(dev);
391 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
1da177e4 392
29b71a1c 393 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
5d9464a4 394}
1da177e4 395
7eff2e7a 396static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
1da177e4 397{
5d9464a4 398 struct acpi_device *acpi_dev = to_acpi_device(dev);
7eff2e7a 399 int len;
5d9464a4 400
2b2ae7c7
TR
401 if (list_empty(&acpi_dev->pnp.ids))
402 return 0;
403
7eff2e7a
KS
404 if (add_uevent_var(env, "MODALIAS="))
405 return -ENOMEM;
406 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
407 sizeof(env->buf) - env->buflen);
408 if (len >= (sizeof(env->buf) - env->buflen))
409 return -ENOMEM;
410 env->buflen += len;
9e89dde2 411 return 0;
1da177e4
LT
412}
413
46ec8598
BH
414static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
415{
416 struct acpi_device *device = data;
417
418 device->driver->ops.notify(device, event);
419}
420
421static acpi_status acpi_device_notify_fixed(void *data)
422{
423 struct acpi_device *device = data;
424
53de5356
BH
425 /* Fixed hardware devices have no handles */
426 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
46ec8598
BH
427 return AE_OK;
428}
429
430static int acpi_device_install_notify_handler(struct acpi_device *device)
431{
432 acpi_status status;
46ec8598 433
ccba2a36 434 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
46ec8598
BH
435 status =
436 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
437 acpi_device_notify_fixed,
438 device);
ccba2a36 439 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
46ec8598
BH
440 status =
441 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
442 acpi_device_notify_fixed,
443 device);
444 else
445 status = acpi_install_notify_handler(device->handle,
446 ACPI_DEVICE_NOTIFY,
447 acpi_device_notify,
448 device);
449
450 if (ACPI_FAILURE(status))
451 return -EINVAL;
452 return 0;
453}
454
455static void acpi_device_remove_notify_handler(struct acpi_device *device)
456{
ccba2a36 457 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
46ec8598
BH
458 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
459 acpi_device_notify_fixed);
ccba2a36 460 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
46ec8598
BH
461 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
462 acpi_device_notify_fixed);
463 else
464 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
465 acpi_device_notify);
466}
467
5d9464a4
PM
468static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
469static int acpi_start_single_object(struct acpi_device *);
470static int acpi_device_probe(struct device * dev)
1da177e4 471{
5d9464a4
PM
472 struct acpi_device *acpi_dev = to_acpi_device(dev);
473 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
474 int ret;
475
476 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
477 if (!ret) {
c4168bff
LS
478 if (acpi_dev->bus_ops.acpi_op_start)
479 acpi_start_single_object(acpi_dev);
46ec8598
BH
480
481 if (acpi_drv->ops.notify) {
482 ret = acpi_device_install_notify_handler(acpi_dev);
483 if (ret) {
46ec8598
BH
484 if (acpi_drv->ops.remove)
485 acpi_drv->ops.remove(acpi_dev,
486 acpi_dev->removal_type);
487 return ret;
488 }
489 }
490
5d9464a4
PM
491 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
492 "Found driver [%s] for device [%s]\n",
493 acpi_drv->name, acpi_dev->pnp.bus_id));
494 get_device(dev);
495 }
496 return ret;
497}
1da177e4 498
5d9464a4
PM
499static int acpi_device_remove(struct device * dev)
500{
501 struct acpi_device *acpi_dev = to_acpi_device(dev);
502 struct acpi_driver *acpi_drv = acpi_dev->driver;
503
504 if (acpi_drv) {
46ec8598
BH
505 if (acpi_drv->ops.notify)
506 acpi_device_remove_notify_handler(acpi_dev);
5d9464a4 507 if (acpi_drv->ops.remove)
96333578 508 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
1da177e4 509 }
5d9464a4 510 acpi_dev->driver = NULL;
db89b4f0 511 acpi_dev->driver_data = NULL;
1da177e4 512
5d9464a4 513 put_device(dev);
9e89dde2
ZR
514 return 0;
515}
1da177e4 516
55955aad 517struct bus_type acpi_bus_type = {
9e89dde2 518 .name = "acpi",
5d9464a4
PM
519 .match = acpi_bus_match,
520 .probe = acpi_device_probe,
521 .remove = acpi_device_remove,
522 .uevent = acpi_device_uevent,
9e89dde2
ZR
523};
524
66b7ed40 525static int acpi_device_register(struct acpi_device *device)
1da177e4 526{
4be44fcd 527 int result;
e49bd2dd
ZR
528 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
529 int found = 0;
66b7ed40 530
9e89dde2
ZR
531 /*
532 * Linkage
533 * -------
534 * Link this device to its parent and siblings.
535 */
536 INIT_LIST_HEAD(&device->children);
537 INIT_LIST_HEAD(&device->node);
9e89dde2 538 INIT_LIST_HEAD(&device->wakeup_list);
1033f904
LT
539 INIT_LIST_HEAD(&device->physical_node_list);
540 mutex_init(&device->physical_node_lock);
1da177e4 541
e49bd2dd
ZR
542 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
543 if (!new_bus_id) {
544 printk(KERN_ERR PREFIX "Memory allocation error\n");
545 return -ENOMEM;
1da177e4 546 }
e49bd2dd 547
9090589d 548 mutex_lock(&acpi_device_lock);
e49bd2dd
ZR
549 /*
550 * Find suitable bus_id and instance number in acpi_bus_id_list
551 * If failed, create one and link it into acpi_bus_id_list
552 */
553 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
1131b938
BH
554 if (!strcmp(acpi_device_bus_id->bus_id,
555 acpi_device_hid(device))) {
556 acpi_device_bus_id->instance_no++;
e49bd2dd
ZR
557 found = 1;
558 kfree(new_bus_id);
559 break;
560 }
1da177e4 561 }
0c526d96 562 if (!found) {
e49bd2dd 563 acpi_device_bus_id = new_bus_id;
1131b938 564 strcpy(acpi_device_bus_id->bus_id, acpi_device_hid(device));
e49bd2dd
ZR
565 acpi_device_bus_id->instance_no = 0;
566 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
1da177e4 567 }
0794469d 568 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
1da177e4 569
33b57150 570 if (device->parent)
9e89dde2 571 list_add_tail(&device->node, &device->parent->children);
33b57150 572
9e89dde2
ZR
573 if (device->wakeup.flags.valid)
574 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
9090589d 575 mutex_unlock(&acpi_device_lock);
1da177e4 576
1890a97a 577 if (device->parent)
66b7ed40 578 device->dev.parent = &device->parent->dev;
1890a97a 579 device->dev.bus = &acpi_bus_type;
1890a97a 580 device->dev.release = &acpi_device_release;
8b12b922 581 result = device_register(&device->dev);
0c526d96 582 if (result) {
8b12b922 583 dev_err(&device->dev, "Error registering device\n");
e49bd2dd 584 goto end;
1da177e4 585 }
1da177e4 586
e49bd2dd 587 result = acpi_device_setup_files(device);
0c526d96 588 if (result)
0794469d
KS
589 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
590 dev_name(&device->dev));
1da177e4 591
96333578 592 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
1da177e4 593 return 0;
0c526d96 594end:
9090589d 595 mutex_lock(&acpi_device_lock);
33b57150 596 if (device->parent)
e49bd2dd 597 list_del(&device->node);
e49bd2dd 598 list_del(&device->wakeup_list);
9090589d 599 mutex_unlock(&acpi_device_lock);
e49bd2dd 600 return result;
1da177e4
LT
601}
602
9e89dde2
ZR
603static void acpi_device_unregister(struct acpi_device *device, int type)
604{
9090589d 605 mutex_lock(&acpi_device_lock);
33b57150 606 if (device->parent)
9e89dde2 607 list_del(&device->node);
1da177e4 608
9e89dde2 609 list_del(&device->wakeup_list);
9090589d 610 mutex_unlock(&acpi_device_lock);
1da177e4 611
9e89dde2 612 acpi_detach_data(device->handle, acpi_bus_data_handler);
1890a97a 613
f883d9db 614 acpi_device_remove_files(device);
1890a97a 615 device_unregister(&device->dev);
1da177e4
LT
616}
617
9e89dde2
ZR
618/* --------------------------------------------------------------------------
619 Driver Management
620 -------------------------------------------------------------------------- */
1da177e4 621/**
d758a8fa
RD
622 * acpi_bus_driver_init - add a device to a driver
623 * @device: the device to add and initialize
624 * @driver: driver for the device
625 *
0c526d96 626 * Used to initialize a device via its device driver. Called whenever a
1890a97a 627 * driver is bound to a device. Invokes the driver's add() ops.
1da177e4
LT
628 */
629static int
4be44fcd 630acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
1da177e4 631{
4be44fcd 632 int result = 0;
1da177e4 633
1da177e4 634 if (!device || !driver)
d550d98d 635 return -EINVAL;
1da177e4
LT
636
637 if (!driver->ops.add)
d550d98d 638 return -ENOSYS;
1da177e4
LT
639
640 result = driver->ops.add(device);
641 if (result) {
642 device->driver = NULL;
db89b4f0 643 device->driver_data = NULL;
d550d98d 644 return result;
1da177e4
LT
645 }
646
647 device->driver = driver;
648
649 /*
650 * TBD - Configuration Management: Assign resources to device based
651 * upon possible configuration and currently allocated resources.
652 */
653
4be44fcd
LB
654 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
655 "Driver successfully bound to device\n"));
d550d98d 656 return 0;
3fb02738
RS
657}
658
8713cbef 659static int acpi_start_single_object(struct acpi_device *device)
3fb02738
RS
660{
661 int result = 0;
662 struct acpi_driver *driver;
663
3fb02738
RS
664
665 if (!(driver = device->driver))
d550d98d 666 return 0;
3fb02738 667
1da177e4
LT
668 if (driver->ops.start) {
669 result = driver->ops.start(device);
670 if (result && driver->ops.remove)
671 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
1da177e4
LT
672 }
673
d550d98d 674 return result;
1da177e4
LT
675}
676
9e89dde2
ZR
677/**
678 * acpi_bus_register_driver - register a driver with the ACPI bus
679 * @driver: driver being registered
680 *
681 * Registers a driver with the ACPI bus. Searches the namespace for all
682 * devices that match the driver's criteria and binds. Returns zero for
683 * success or a negative error status for failure.
684 */
685int acpi_bus_register_driver(struct acpi_driver *driver)
1da177e4 686{
1890a97a 687 int ret;
1da177e4 688
9e89dde2
ZR
689 if (acpi_disabled)
690 return -ENODEV;
1890a97a
PM
691 driver->drv.name = driver->name;
692 driver->drv.bus = &acpi_bus_type;
693 driver->drv.owner = driver->owner;
1da177e4 694
1890a97a
PM
695 ret = driver_register(&driver->drv);
696 return ret;
9e89dde2 697}
1da177e4 698
9e89dde2
ZR
699EXPORT_SYMBOL(acpi_bus_register_driver);
700
701/**
702 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
703 * @driver: driver to unregister
704 *
705 * Unregisters a driver with the ACPI bus. Searches the namespace for all
706 * devices that match the driver's criteria and unbinds.
707 */
708void acpi_bus_unregister_driver(struct acpi_driver *driver)
709{
1890a97a 710 driver_unregister(&driver->drv);
9e89dde2
ZR
711}
712
713EXPORT_SYMBOL(acpi_bus_unregister_driver);
714
9e89dde2
ZR
715/* --------------------------------------------------------------------------
716 Device Enumeration
717 -------------------------------------------------------------------------- */
5c478f49
BH
718static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
719{
720 acpi_status status;
721 int ret;
722 struct acpi_device *device;
723
724 /*
725 * Fixed hardware devices do not appear in the namespace and do not
726 * have handles, but we fabricate acpi_devices for them, so we have
727 * to deal with them specially.
728 */
729 if (handle == NULL)
730 return acpi_root;
731
732 do {
733 status = acpi_get_parent(handle, &handle);
734 if (status == AE_NULL_ENTRY)
735 return NULL;
736 if (ACPI_FAILURE(status))
737 return acpi_root;
738
739 ret = acpi_bus_get_device(handle, &device);
740 if (ret == 0)
741 return device;
742 } while (1);
743}
744
9e89dde2
ZR
745acpi_status
746acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
747{
748 acpi_status status;
749 acpi_handle tmp;
750 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
751 union acpi_object *obj;
752
753 status = acpi_get_handle(handle, "_EJD", &tmp);
754 if (ACPI_FAILURE(status))
755 return status;
756
757 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
758 if (ACPI_SUCCESS(status)) {
759 obj = buffer.pointer;
3b5fee59
HM
760 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
761 ejd);
9e89dde2
ZR
762 kfree(buffer.pointer);
763 }
764 return status;
765}
766EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
767
8e4319c4 768void acpi_bus_data_handler(acpi_handle handle, void *context)
9e89dde2
ZR
769{
770
771 /* TBD */
772
773 return;
774}
775
9e89dde2
ZR
776static int acpi_bus_get_perf_flags(struct acpi_device *device)
777{
778 device->performance.state = ACPI_STATE_UNKNOWN;
779 return 0;
780}
781
782static acpi_status
b581a7f9
RW
783acpi_bus_extract_wakeup_device_power_package(acpi_handle handle,
784 struct acpi_device_wakeup *wakeup)
9e89dde2 785{
b581a7f9
RW
786 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
787 union acpi_object *package = NULL;
9e89dde2 788 union acpi_object *element = NULL;
b581a7f9
RW
789 acpi_status status;
790 int i = 0;
9e89dde2 791
b581a7f9 792 if (!wakeup)
9e89dde2
ZR
793 return AE_BAD_PARAMETER;
794
b581a7f9
RW
795 /* _PRW */
796 status = acpi_evaluate_object(handle, "_PRW", NULL, &buffer);
797 if (ACPI_FAILURE(status)) {
798 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
799 return status;
800 }
801
802 package = (union acpi_object *)buffer.pointer;
803
804 if (!package || (package->package.count < 2)) {
805 status = AE_BAD_DATA;
806 goto out;
807 }
808
9e89dde2 809 element = &(package->package.elements[0]);
b581a7f9
RW
810 if (!element) {
811 status = AE_BAD_DATA;
812 goto out;
813 }
9e89dde2
ZR
814 if (element->type == ACPI_TYPE_PACKAGE) {
815 if ((element->package.count < 2) ||
816 (element->package.elements[0].type !=
817 ACPI_TYPE_LOCAL_REFERENCE)
b581a7f9
RW
818 || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) {
819 status = AE_BAD_DATA;
820 goto out;
821 }
822 wakeup->gpe_device =
9e89dde2 823 element->package.elements[0].reference.handle;
b581a7f9 824 wakeup->gpe_number =
9e89dde2
ZR
825 (u32) element->package.elements[1].integer.value;
826 } else if (element->type == ACPI_TYPE_INTEGER) {
b581a7f9
RW
827 wakeup->gpe_device = NULL;
828 wakeup->gpe_number = element->integer.value;
829 } else {
830 status = AE_BAD_DATA;
831 goto out;
832 }
9e89dde2
ZR
833
834 element = &(package->package.elements[1]);
835 if (element->type != ACPI_TYPE_INTEGER) {
b581a7f9
RW
836 status = AE_BAD_DATA;
837 goto out;
9e89dde2 838 }
b581a7f9 839 wakeup->sleep_state = element->integer.value;
9e89dde2
ZR
840
841 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
b581a7f9
RW
842 status = AE_NO_MEMORY;
843 goto out;
9e89dde2 844 }
b581a7f9
RW
845 wakeup->resources.count = package->package.count - 2;
846 for (i = 0; i < wakeup->resources.count; i++) {
9e89dde2 847 element = &(package->package.elements[i + 2]);
b581a7f9
RW
848 if (element->type != ACPI_TYPE_LOCAL_REFERENCE) {
849 status = AE_BAD_DATA;
850 goto out;
851 }
9e89dde2 852
b581a7f9 853 wakeup->resources.handles[i] = element->reference.handle;
1da177e4 854 }
9e89dde2 855
bba63a29 856 acpi_setup_gpe_for_wake(handle, wakeup->gpe_device, wakeup->gpe_number);
9874647b 857
b581a7f9
RW
858 out:
859 kfree(buffer.pointer);
860
861 return status;
1da177e4
LT
862}
863
f517709d 864static void acpi_bus_set_run_wake_flags(struct acpi_device *device)
1da177e4 865{
29b71a1c
TR
866 struct acpi_device_id button_device_ids[] = {
867 {"PNP0C0D", 0},
868 {"PNP0C0C", 0},
869 {"PNP0C0E", 0},
870 {"", 0},
871 };
f517709d
RW
872 acpi_status status;
873 acpi_event_status event_status;
874
b67ea761 875 device->wakeup.flags.notifier_present = 0;
f517709d
RW
876
877 /* Power button, Lid switch always enable wakeup */
878 if (!acpi_match_device_ids(device, button_device_ids)) {
879 device->wakeup.flags.run_wake = 1;
f2b56bc8 880 device_set_wakeup_capable(&device->dev, true);
f517709d
RW
881 return;
882 }
883
e8e18c95
RW
884 status = acpi_get_gpe_status(device->wakeup.gpe_device,
885 device->wakeup.gpe_number,
886 &event_status);
f517709d
RW
887 if (status == AE_OK)
888 device->wakeup.flags.run_wake =
889 !!(event_status & ACPI_EVENT_FLAG_HANDLE);
890}
891
d57d09a4 892static void acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
f517709d 893{
d57d09a4 894 acpi_handle temp;
f517709d 895 acpi_status status = 0;
f517709d 896 int psw_error;
29b71a1c 897
d57d09a4
RW
898 /* Presence of _PRW indicates wake capable */
899 status = acpi_get_handle(device->handle, "_PRW", &temp);
900 if (ACPI_FAILURE(status))
901 return;
902
b581a7f9
RW
903 status = acpi_bus_extract_wakeup_device_power_package(device->handle,
904 &device->wakeup);
9e89dde2
ZR
905 if (ACPI_FAILURE(status)) {
906 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
d57d09a4 907 return;
9e89dde2 908 }
1da177e4 909
9e89dde2 910 device->wakeup.flags.valid = 1;
9b83ccd2 911 device->wakeup.prepare_count = 0;
f517709d 912 acpi_bus_set_run_wake_flags(device);
729b2bdb
ZY
913 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
914 * system for the ACPI device with the _PRW object.
915 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
916 * So it is necessary to call _DSW object first. Only when it is not
917 * present will the _PSW object used.
918 */
77e76609
RW
919 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
920 if (psw_error)
921 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
922 "error in _DSW or _PSW evaluation\n"));
1da177e4 923}
1da177e4 924
bf325f95
RW
925static void acpi_bus_add_power_resource(acpi_handle handle);
926
9e89dde2 927static int acpi_bus_get_power_flags(struct acpi_device *device)
1da177e4 928{
9e89dde2
ZR
929 acpi_status status = 0;
930 acpi_handle handle = NULL;
931 u32 i = 0;
1a365616 932
4be44fcd 933
9e89dde2
ZR
934 /*
935 * Power Management Flags
936 */
937 status = acpi_get_handle(device->handle, "_PSC", &handle);
938 if (ACPI_SUCCESS(status))
939 device->power.flags.explicit_get = 1;
940 status = acpi_get_handle(device->handle, "_IRC", &handle);
941 if (ACPI_SUCCESS(status))
942 device->power.flags.inrush_current = 1;
1da177e4 943
9e89dde2
ZR
944 /*
945 * Enumerate supported power management states
946 */
1cc0c998 947 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3_HOT; i++) {
9e89dde2
ZR
948 struct acpi_device_power_state *ps = &device->power.states[i];
949 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
1da177e4 950
9e89dde2
ZR
951 /* Evaluate "_PRx" to se if power resources are referenced */
952 acpi_evaluate_reference(device->handle, object_name, NULL,
953 &ps->resources);
954 if (ps->resources.count) {
bf325f95
RW
955 int j;
956
9e89dde2 957 device->power.flags.power_resources = 1;
bf325f95
RW
958 for (j = 0; j < ps->resources.count; j++)
959 acpi_bus_add_power_resource(ps->resources.handles[j]);
1da177e4 960 }
1da177e4 961
9e89dde2
ZR
962 /* Evaluate "_PSx" to see if we can do explicit sets */
963 object_name[2] = 'S';
964 status = acpi_get_handle(device->handle, object_name, &handle);
37239978 965 if (ACPI_SUCCESS(status))
9e89dde2 966 ps->flags.explicit_set = 1;
1da177e4 967
1cc0c998
LM
968 /*
969 * State is valid if there are means to put the device into it.
970 * D3hot is only valid if _PR3 present.
971 */
972 if (ps->resources.count ||
973 (ps->flags.explicit_set && i < ACPI_STATE_D3_HOT))
9e89dde2 974 ps->flags.valid = 1;
1da177e4 975
9e89dde2
ZR
976 ps->power = -1; /* Unknown - driver assigned */
977 ps->latency = -1; /* Unknown - driver assigned */
978 }
1da177e4 979
9e89dde2
ZR
980 /* Set defaults for D0 and D3 states (always valid) */
981 device->power.states[ACPI_STATE_D0].flags.valid = 1;
982 device->power.states[ACPI_STATE_D0].power = 100;
983 device->power.states[ACPI_STATE_D3].flags.valid = 1;
984 device->power.states[ACPI_STATE_D3].power = 0;
c8f7a62c 985
5c7dd710
RW
986 /* Set D3cold's explicit_set flag if _PS3 exists. */
987 if (device->power.states[ACPI_STATE_D3_HOT].flags.explicit_set)
988 device->power.states[ACPI_STATE_D3_COLD].flags.explicit_set = 1;
989
ade3e7fe 990 acpi_bus_init_power(device);
c8f7a62c 991
9e89dde2
ZR
992 return 0;
993}
c8f7a62c 994
4be44fcd 995static int acpi_bus_get_flags(struct acpi_device *device)
1da177e4 996{
4be44fcd
LB
997 acpi_status status = AE_OK;
998 acpi_handle temp = NULL;
1da177e4 999
1da177e4
LT
1000
1001 /* Presence of _STA indicates 'dynamic_status' */
1002 status = acpi_get_handle(device->handle, "_STA", &temp);
1003 if (ACPI_SUCCESS(status))
1004 device->flags.dynamic_status = 1;
1005
1da177e4
LT
1006 /* Presence of _RMV indicates 'removable' */
1007 status = acpi_get_handle(device->handle, "_RMV", &temp);
1008 if (ACPI_SUCCESS(status))
1009 device->flags.removable = 1;
1010
1011 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
1012 status = acpi_get_handle(device->handle, "_EJD", &temp);
1013 if (ACPI_SUCCESS(status))
1014 device->flags.ejectable = 1;
1015 else {
1016 status = acpi_get_handle(device->handle, "_EJ0", &temp);
1017 if (ACPI_SUCCESS(status))
1018 device->flags.ejectable = 1;
1019 }
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 }
ccf78040
LZ
1215 if (info->valid & ACPI_VALID_UID)
1216 device->pnp.unique_id = kstrdup(info->unique_id.string,
1217 GFP_KERNEL);
ae843332 1218
a83893ae
BH
1219 kfree(info);
1220
57f3674f
BH
1221 /*
1222 * Some devices don't reliably have _HIDs & _CIDs, so add
1223 * synthetic HIDs to make sure drivers can find them.
1224 */
c3d6de69 1225 if (acpi_is_video_device(device))
57f3674f 1226 acpi_add_id(device, ACPI_VIDEO_HID);
3620f2f2 1227 else if (ACPI_SUCCESS(acpi_bay_match(device)))
57f3674f 1228 acpi_add_id(device, ACPI_BAY_HID);
3620f2f2 1229 else if (ACPI_SUCCESS(acpi_dock_match(device)))
57f3674f 1230 acpi_add_id(device, ACPI_DOCK_HID);
222e82ac
DW
1231 else if (!acpi_ibm_smbus_match(device))
1232 acpi_add_id(device, ACPI_SMBUS_IBM_HID);
b7b30de5
BH
1233 else if (!acpi_device_hid(device) &&
1234 ACPI_IS_ROOT_DEVICE(device->parent)) {
1235 acpi_add_id(device, ACPI_BUS_HID); /* \_SB, LNXSYBUS */
1236 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1237 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1238 }
54735266 1239
1da177e4
LT
1240 break;
1241 case ACPI_BUS_TYPE_POWER:
57f3674f 1242 acpi_add_id(device, ACPI_POWER_HID);
1da177e4
LT
1243 break;
1244 case ACPI_BUS_TYPE_PROCESSOR:
57f3674f 1245 acpi_add_id(device, ACPI_PROCESSOR_OBJECT_HID);
1da177e4 1246 break;
1da177e4 1247 case ACPI_BUS_TYPE_THERMAL:
57f3674f 1248 acpi_add_id(device, ACPI_THERMAL_HID);
1da177e4
LT
1249 break;
1250 case ACPI_BUS_TYPE_POWER_BUTTON:
57f3674f 1251 acpi_add_id(device, ACPI_BUTTON_HID_POWERF);
1da177e4
LT
1252 break;
1253 case ACPI_BUS_TYPE_SLEEP_BUTTON:
57f3674f 1254 acpi_add_id(device, ACPI_BUTTON_HID_SLEEPF);
1da177e4
LT
1255 break;
1256 }
1da177e4
LT
1257}
1258
bc3b0772 1259static int acpi_device_set_context(struct acpi_device *device)
1da177e4 1260{
bc3b0772
BH
1261 acpi_status status;
1262
1da177e4
LT
1263 /*
1264 * Context
1265 * -------
1266 * Attach this 'struct acpi_device' to the ACPI object. This makes
bc3b0772
BH
1267 * resolutions from handle->device very efficient. Fixed hardware
1268 * devices have no handles, so we skip them.
1da177e4 1269 */
bc3b0772
BH
1270 if (!device->handle)
1271 return 0;
1da177e4 1272
bc3b0772
BH
1273 status = acpi_attach_data(device->handle,
1274 acpi_bus_data_handler, device);
1275 if (ACPI_SUCCESS(status))
1276 return 0;
1277
1278 printk(KERN_ERR PREFIX "Error attaching device data\n");
1279 return -ENODEV;
1da177e4
LT
1280}
1281
4be44fcd 1282static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1da177e4 1283{
1da177e4 1284 if (!dev)
d550d98d 1285 return -EINVAL;
1da177e4 1286
96333578 1287 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1890a97a 1288 device_release_driver(&dev->dev);
1da177e4
LT
1289
1290 if (!rmdevice)
d550d98d 1291 return 0;
1da177e4 1292
2786f6e3
RZ
1293 /*
1294 * unbind _ADR-Based Devices when hot removal
1295 */
1da177e4
LT
1296 if (dev->flags.bus_address) {
1297 if ((dev->parent) && (dev->parent->ops.unbind))
1298 dev->parent->ops.unbind(dev);
1299 }
1da177e4
LT
1300 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1301
d550d98d 1302 return 0;
1da177e4
LT
1303}
1304
5c478f49
BH
1305static int acpi_add_single_object(struct acpi_device **child,
1306 acpi_handle handle, int type,
778cbc1d 1307 unsigned long long sta,
5c478f49 1308 struct acpi_bus_ops *ops)
1da177e4 1309{
77c24888
BH
1310 int result;
1311 struct acpi_device *device;
29aaefa6 1312 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1da177e4 1313
36bcbec7 1314 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1da177e4 1315 if (!device) {
6468463a 1316 printk(KERN_ERR PREFIX "Memory allocation error\n");
d550d98d 1317 return -ENOMEM;
1da177e4 1318 }
1da177e4 1319
7f47fa6c 1320 INIT_LIST_HEAD(&device->pnp.ids);
caaa6efb 1321 device->device_type = type;
1da177e4 1322 device->handle = handle;
5c478f49 1323 device->parent = acpi_bus_get_parent(handle);
c4168bff 1324 device->bus_ops = *ops; /* workround for not call .start */
778cbc1d 1325 STRUCT_TO_INT(device->status) = sta;
c4168bff 1326
c7bcb4e9 1327 acpi_device_get_busid(device);
1da177e4
LT
1328
1329 /*
1330 * Flags
1331 * -----
778cbc1d
BH
1332 * Note that we only look for object handles -- cannot evaluate objects
1333 * until we know the device is present and properly initialized.
1da177e4
LT
1334 */
1335 result = acpi_bus_get_flags(device);
1336 if (result)
1337 goto end;
1338
1da177e4
LT
1339 /*
1340 * Initialize Device
1341 * -----------------
1342 * TBD: Synch with Core's enumeration/initialization process.
1343 */
c7bcb4e9 1344 acpi_device_set_id(device);
1da177e4
LT
1345
1346 /*
1347 * Power Management
1348 * ----------------
1349 */
1350 if (device->flags.power_manageable) {
1351 result = acpi_bus_get_power_flags(device);
1352 if (result)
1353 goto end;
1354 }
1355
4be44fcd 1356 /*
1da177e4
LT
1357 * Wakeup device management
1358 *-----------------------
1359 */
d57d09a4 1360 acpi_bus_get_wakeup_device_flags(device);
1da177e4
LT
1361
1362 /*
1363 * Performance Management
1364 * ----------------------
1365 */
1366 if (device->flags.performance_manageable) {
1367 result = acpi_bus_get_perf_flags(device);
1368 if (result)
1369 goto end;
1370 }
1371
bc3b0772 1372 if ((result = acpi_device_set_context(device)))
f61f9258 1373 goto end;
1da177e4 1374
66b7ed40 1375 result = acpi_device_register(device);
1da177e4
LT
1376
1377 /*
2786f6e3 1378 * Bind _ADR-Based Devices when hot add
1da177e4
LT
1379 */
1380 if (device->flags.bus_address) {
1381 if (device->parent && device->parent->ops.bind)
1382 device->parent->ops.bind(device);
1383 }
1384
0c526d96 1385end:
29aaefa6
BH
1386 if (!result) {
1387 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1388 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1389 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1390 (char *) buffer.pointer,
1391 device->parent ? dev_name(&device->parent->dev) :
1392 "(null)"));
1393 kfree(buffer.pointer);
1da177e4 1394 *child = device;
29aaefa6 1395 } else
718fb0de 1396 acpi_device_release(&device->dev);
1da177e4 1397
d550d98d 1398 return result;
1da177e4 1399}
1da177e4 1400
778cbc1d
BH
1401#define ACPI_STA_DEFAULT (ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED | \
1402 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING)
1403
bf325f95
RW
1404static void acpi_bus_add_power_resource(acpi_handle handle)
1405{
1406 struct acpi_bus_ops ops = {
1407 .acpi_op_add = 1,
1408 .acpi_op_start = 1,
1409 };
1410 struct acpi_device *device = NULL;
1411
1412 acpi_bus_get_device(handle, &device);
1413 if (!device)
1414 acpi_add_single_object(&device, handle, ACPI_BUS_TYPE_POWER,
1415 ACPI_STA_DEFAULT, &ops);
1416}
1417
778cbc1d
BH
1418static int acpi_bus_type_and_status(acpi_handle handle, int *type,
1419 unsigned long long *sta)
1da177e4 1420{
778cbc1d
BH
1421 acpi_status status;
1422 acpi_object_type acpi_type;
1da177e4 1423
778cbc1d 1424 status = acpi_get_type(handle, &acpi_type);
51a85faf 1425 if (ACPI_FAILURE(status))
778cbc1d 1426 return -ENODEV;
4be44fcd 1427
778cbc1d 1428 switch (acpi_type) {
51a85faf
BH
1429 case ACPI_TYPE_ANY: /* for ACPI_ROOT_OBJECT */
1430 case ACPI_TYPE_DEVICE:
778cbc1d
BH
1431 *type = ACPI_BUS_TYPE_DEVICE;
1432 status = acpi_bus_get_status_handle(handle, sta);
1433 if (ACPI_FAILURE(status))
1434 return -ENODEV;
51a85faf
BH
1435 break;
1436 case ACPI_TYPE_PROCESSOR:
778cbc1d
BH
1437 *type = ACPI_BUS_TYPE_PROCESSOR;
1438 status = acpi_bus_get_status_handle(handle, sta);
1439 if (ACPI_FAILURE(status))
1440 return -ENODEV;
51a85faf
BH
1441 break;
1442 case ACPI_TYPE_THERMAL:
778cbc1d
BH
1443 *type = ACPI_BUS_TYPE_THERMAL;
1444 *sta = ACPI_STA_DEFAULT;
51a85faf
BH
1445 break;
1446 case ACPI_TYPE_POWER:
778cbc1d
BH
1447 *type = ACPI_BUS_TYPE_POWER;
1448 *sta = ACPI_STA_DEFAULT;
51a85faf
BH
1449 break;
1450 default:
778cbc1d 1451 return -ENODEV;
51a85faf 1452 }
1da177e4 1453
778cbc1d
BH
1454 return 0;
1455}
1456
1457static acpi_status acpi_bus_check_add(acpi_handle handle, u32 lvl,
1458 void *context, void **return_value)
1459{
1460 struct acpi_bus_ops *ops = context;
778cbc1d
BH
1461 int type;
1462 unsigned long long sta;
e3b87f8a
BH
1463 struct acpi_device *device;
1464 acpi_status status;
778cbc1d
BH
1465 int result;
1466
1467 result = acpi_bus_type_and_status(handle, &type, &sta);
1468 if (result)
1469 return AE_OK;
1470
1471 if (!(sta & ACPI_STA_DEVICE_PRESENT) &&
b581a7f9 1472 !(sta & ACPI_STA_DEVICE_FUNCTIONING)) {
86e4e20e
RW
1473 struct acpi_device_wakeup wakeup;
1474 acpi_handle temp;
1475
1476 status = acpi_get_handle(handle, "_PRW", &temp);
1477 if (ACPI_SUCCESS(status))
1478 acpi_bus_extract_wakeup_device_power_package(handle,
1479 &wakeup);
778cbc1d 1480 return AE_CTRL_DEPTH;
b581a7f9 1481 }
778cbc1d 1482
e3b87f8a
BH
1483 /*
1484 * We may already have an acpi_device from a previous enumeration. If
1485 * so, we needn't add it again, but we may still have to start it.
1486 */
1487 device = NULL;
1488 acpi_bus_get_device(handle, &device);
1489 if (ops->acpi_op_add && !device)
1490 acpi_add_single_object(&device, handle, type, sta, ops);
1da177e4 1491
e3b87f8a 1492 if (!device)
51a85faf 1493 return AE_CTRL_DEPTH;
1da177e4 1494
51a85faf
BH
1495 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1496 status = acpi_start_single_object(device);
1da177e4 1497 if (ACPI_FAILURE(status))
51a85faf
BH
1498 return AE_CTRL_DEPTH;
1499 }
1da177e4 1500
51a85faf
BH
1501 if (!*return_value)
1502 *return_value = device;
1503 return AE_OK;
1504}
3fb02738 1505
51a85faf
BH
1506static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops,
1507 struct acpi_device **child)
1508{
1509 acpi_status status;
51a85faf 1510 void *device = NULL;
3fb02738 1511
51a85faf
BH
1512 status = acpi_bus_check_add(handle, 0, ops, &device);
1513 if (ACPI_SUCCESS(status))
1514 acpi_walk_namespace(ACPI_TYPE_ANY, handle, ACPI_UINT32_MAX,
2263576c 1515 acpi_bus_check_add, NULL, ops, &device);
1da177e4 1516
51a85faf
BH
1517 if (child)
1518 *child = device;
7779688f
TR
1519
1520 if (device)
1521 return 0;
1522 else
1523 return -ENODEV;
1da177e4 1524}
1da177e4 1525
7779688f
TR
1526/*
1527 * acpi_bus_add and acpi_bus_start
1528 *
1529 * scan a given ACPI tree and (probably recently hot-plugged)
1530 * create and add or starts found devices.
1531 *
1532 * If no devices were found -ENODEV is returned which does not
1533 * mean that this is a real error, there just have been no suitable
1534 * ACPI objects in the table trunk from which the kernel could create
1535 * a device and add/start an appropriate driver.
1536 */
1537
3fb02738 1538int
4be44fcd
LB
1539acpi_bus_add(struct acpi_device **child,
1540 struct acpi_device *parent, acpi_handle handle, int type)
3fb02738 1541{
3fb02738
RS
1542 struct acpi_bus_ops ops;
1543
c4168bff
LS
1544 memset(&ops, 0, sizeof(ops));
1545 ops.acpi_op_add = 1;
3fb02738 1546
7779688f 1547 return acpi_bus_scan(handle, &ops, child);
3fb02738
RS
1548}
1549EXPORT_SYMBOL(acpi_bus_add);
1550
4be44fcd 1551int acpi_bus_start(struct acpi_device *device)
3fb02738 1552{
3fb02738 1553 struct acpi_bus_ops ops;
a2100801 1554 int result;
3fb02738 1555
d2f6650a
TR
1556 if (!device)
1557 return -EINVAL;
1558
8e029bf0
BH
1559 memset(&ops, 0, sizeof(ops));
1560 ops.acpi_op_start = 1;
3fb02738 1561
a2100801
RW
1562 result = acpi_bus_scan(device->handle, &ops, NULL);
1563
3a37898d 1564 acpi_update_all_gpes();
a2100801
RW
1565
1566 return result;
3fb02738
RS
1567}
1568EXPORT_SYMBOL(acpi_bus_start);
1da177e4 1569
ceaba663 1570int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1da177e4 1571{
4be44fcd
LB
1572 acpi_status status;
1573 struct acpi_device *parent, *child;
1574 acpi_handle phandle, chandle;
1575 acpi_object_type type;
1576 u32 level = 1;
1577 int err = 0;
1578
1579 parent = start;
1da177e4
LT
1580 phandle = start->handle;
1581 child = chandle = NULL;
1582
1583 while ((level > 0) && parent && (!err)) {
1584 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
4be44fcd 1585 chandle, &chandle);
1da177e4
LT
1586
1587 /*
1588 * If this scope is exhausted then move our way back up.
1589 */
1590 if (ACPI_FAILURE(status)) {
1591 level--;
1592 chandle = phandle;
1593 acpi_get_parent(phandle, &phandle);
1594 child = parent;
1595 parent = parent->parent;
1596
1597 if (level == 0)
1598 err = acpi_bus_remove(child, rmdevice);
1599 else
1600 err = acpi_bus_remove(child, 1);
1601
1602 continue;
1603 }
1604
1605 status = acpi_get_type(chandle, &type);
1606 if (ACPI_FAILURE(status)) {
1607 continue;
1608 }
1609 /*
1610 * If there is a device corresponding to chandle then
1611 * parse it (depth-first).
1612 */
1613 if (acpi_bus_get_device(chandle, &child) == 0) {
1614 level++;
1615 phandle = chandle;
1616 chandle = NULL;
1617 parent = child;
1618 }
1619 continue;
1620 }
1621 return err;
1622}
ceaba663
KA
1623EXPORT_SYMBOL_GPL(acpi_bus_trim);
1624
e8b945c9 1625static int acpi_bus_scan_fixed(void)
1da177e4 1626{
4be44fcd
LB
1627 int result = 0;
1628 struct acpi_device *device = NULL;
c4168bff 1629 struct acpi_bus_ops ops;
1da177e4 1630
c4168bff
LS
1631 memset(&ops, 0, sizeof(ops));
1632 ops.acpi_op_add = 1;
1633 ops.acpi_op_start = 1;
1634
1da177e4
LT
1635 /*
1636 * Enumerate all fixed-feature devices.
1637 */
cee324b1 1638 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
5c478f49 1639 result = acpi_add_single_object(&device, NULL,
c4168bff 1640 ACPI_BUS_TYPE_POWER_BUTTON,
778cbc1d 1641 ACPI_STA_DEFAULT,
c4168bff 1642 &ops);
c10d7a13 1643 device_init_wakeup(&device->dev, true);
3fb02738 1644 }
1da177e4 1645
cee324b1 1646 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
5c478f49 1647 result = acpi_add_single_object(&device, NULL,
c4168bff 1648 ACPI_BUS_TYPE_SLEEP_BUTTON,
778cbc1d 1649 ACPI_STA_DEFAULT,
c4168bff 1650 &ops);
3fb02738 1651 }
1da177e4 1652
d550d98d 1653 return result;
1da177e4
LT
1654}
1655
e747f274 1656int __init acpi_scan_init(void)
1da177e4
LT
1657{
1658 int result;
3fb02738 1659 struct acpi_bus_ops ops;
1da177e4 1660
c4168bff
LS
1661 memset(&ops, 0, sizeof(ops));
1662 ops.acpi_op_add = 1;
1663 ops.acpi_op_start = 1;
1da177e4 1664
5b327265
PM
1665 result = bus_register(&acpi_bus_type);
1666 if (result) {
1667 /* We don't want to quit even if we failed to add suspend/resume */
1668 printk(KERN_ERR PREFIX "Could not register bus type\n");
1669 }
1670
97d9a9e9
RW
1671 acpi_power_init();
1672
1da177e4
LT
1673 /*
1674 * Enumerate devices in the ACPI namespace.
1675 */
51a85faf 1676 result = acpi_bus_scan(ACPI_ROOT_OBJECT, &ops, &acpi_root);
c04209a7 1677
c4168bff 1678 if (!result)
adc08e20 1679 result = acpi_bus_scan_fixed();
1da177e4
LT
1680
1681 if (result)
1682 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
a2100801 1683 else
3a37898d 1684 acpi_update_all_gpes();
1da177e4 1685
d550d98d 1686 return result;
1da177e4 1687}
This page took 0.90387 seconds and 5 git commands to generate.