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