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