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