blob: f8316a05ede72fe459a66341343f5cd40fa6ae09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
7#include <linux/acpi.h>
8
9#include <acpi/acpi_drivers.h>
10#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#define _COMPONENT ACPI_BUS_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040013ACPI_MODULE_NAME("scan")
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040015extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#define ACPI_BUS_CLASS "system_bus"
18#define ACPI_BUS_HID "ACPI_BUS"
19#define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
20#define ACPI_BUS_DEVICE_NAME "System Bus"
21
22static LIST_HEAD(acpi_device_list);
23DEFINE_SPINLOCK(acpi_device_lock);
24LIST_HEAD(acpi_wakeup_device_list);
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Len Brown4be44fc2005-08-05 00:44:28 -040027static void acpi_device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Len Brown4be44fc2005-08-05 00:44:28 -040029 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
Jesper Juhl6044ec82005-11-07 01:01:32 -080030 kfree(dev->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 kfree(dev);
32}
33
34struct acpi_device_attribute {
35 struct attribute attr;
Len Brown4be44fc2005-08-05 00:44:28 -040036 ssize_t(*show) (struct acpi_device *, char *);
37 ssize_t(*store) (struct acpi_device *, const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40typedef void acpi_device_sysfs_files(struct kobject *,
Len Brown4be44fc2005-08-05 00:44:28 -040041 const struct attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43static void setup_sys_fs_device_files(struct acpi_device *dev,
Len Brown4be44fc2005-08-05 00:44:28 -040044 acpi_device_sysfs_files * func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#define create_sysfs_device_files(dev) \
47 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
48#define remove_sysfs_device_files(dev) \
49 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
52#define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
53
54static ssize_t acpi_device_attr_show(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040055 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct acpi_device *device = to_acpi_device(kobj);
58 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050059 return attribute->show ? attribute->show(device, buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61static ssize_t acpi_device_attr_store(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040062 struct attribute *attr, const char *buf,
63 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
65 struct acpi_device *device = to_acpi_device(kobj);
66 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050067 return attribute->store ? attribute->store(device, buf, len) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
70static struct sysfs_ops acpi_device_sysfs_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -040071 .show = acpi_device_attr_show,
72 .store = acpi_device_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75static struct kobj_type ktype_acpi_ns = {
Len Brown4be44fc2005-08-05 00:44:28 -040076 .sysfs_ops = &acpi_device_sysfs_ops,
77 .release = acpi_device_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Kay Sievers312c0042005-11-16 09:00:00 +010080static int namespace_uevent(struct kset *kset, struct kobject *kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 char **envp, int num_envp, char *buffer,
82 int buffer_size)
83{
84 struct acpi_device *dev = to_acpi_device(kobj);
85 int i = 0;
86 int len = 0;
87
88 if (!dev->driver)
89 return 0;
90
Kay Sievers312c0042005-11-16 09:00:00 +010091 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
92 "PHYSDEVDRIVER=%s", dev->driver->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 return -ENOMEM;
94
95 envp[i] = NULL;
96
97 return 0;
98}
99
Kay Sievers312c0042005-11-16 09:00:00 +0100100static struct kset_uevent_ops namespace_uevent_ops = {
101 .uevent = &namespace_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102};
103
104static struct kset acpi_namespace_kset = {
Len Brown4be44fc2005-08-05 00:44:28 -0400105 .kobj = {
106 .name = "namespace",
107 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .subsys = &acpi_subsys,
Len Brown4be44fc2005-08-05 00:44:28 -0400109 .ktype = &ktype_acpi_ns,
Kay Sievers312c0042005-11-16 09:00:00 +0100110 .uevent_ops = &namespace_uevent_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111};
112
Len Brown4be44fc2005-08-05 00:44:28 -0400113static void acpi_device_register(struct acpi_device *device,
114 struct acpi_device *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 /*
117 * Linkage
118 * -------
119 * Link this device to its parent and siblings.
120 */
121 INIT_LIST_HEAD(&device->children);
122 INIT_LIST_HEAD(&device->node);
123 INIT_LIST_HEAD(&device->g_list);
124 INIT_LIST_HEAD(&device->wakeup_list);
125
126 spin_lock(&acpi_device_lock);
127 if (device->parent) {
128 list_add_tail(&device->node, &device->parent->children);
Len Brown4be44fc2005-08-05 00:44:28 -0400129 list_add_tail(&device->g_list, &device->parent->g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 } else
Len Brown4be44fc2005-08-05 00:44:28 -0400131 list_add_tail(&device->g_list, &acpi_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (device->wakeup.flags.valid)
Len Brown4be44fc2005-08-05 00:44:28 -0400133 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 spin_unlock(&acpi_device_lock);
135
Len Brown4be44fc2005-08-05 00:44:28 -0400136 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (parent)
138 device->kobj.parent = &parent->kobj;
139 device->kobj.ktype = &ktype_acpi_ns;
140 device->kobj.kset = &acpi_namespace_kset;
141 kobject_register(&device->kobj);
142 create_sysfs_device_files(device);
143}
144
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400145static void acpi_device_unregister(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 spin_lock(&acpi_device_lock);
148 if (device->parent) {
149 list_del(&device->node);
150 list_del(&device->g_list);
151 } else
152 list_del(&device->g_list);
153
154 list_del(&device->wakeup_list);
155
156 spin_unlock(&acpi_device_lock);
157
158 acpi_detach_data(device->handle, acpi_bus_data_handler);
159 remove_sysfs_device_files(device);
160 kobject_unregister(&device->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Len Brown4be44fc2005-08-05 00:44:28 -0400163void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 ACPI_FUNCTION_TRACE("acpi_bus_data_handler");
166
167 /* TBD */
168
169 return_VOID;
170}
171
Len Brown4be44fc2005-08-05 00:44:28 -0400172static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Len Brown4be44fc2005-08-05 00:44:28 -0400174 acpi_status status = 0;
175 acpi_handle handle = NULL;
176 u32 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 ACPI_FUNCTION_TRACE("acpi_bus_get_power_flags");
179
180 /*
181 * Power Management Flags
182 */
183 status = acpi_get_handle(device->handle, "_PSC", &handle);
184 if (ACPI_SUCCESS(status))
185 device->power.flags.explicit_get = 1;
186 status = acpi_get_handle(device->handle, "_IRC", &handle);
187 if (ACPI_SUCCESS(status))
188 device->power.flags.inrush_current = 1;
189
190 /*
191 * Enumerate supported power management states
192 */
193 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
194 struct acpi_device_power_state *ps = &device->power.states[i];
Len Brown4be44fc2005-08-05 00:44:28 -0400195 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 /* Evaluate "_PRx" to se if power resources are referenced */
198 acpi_evaluate_reference(device->handle, object_name, NULL,
Len Brown4be44fc2005-08-05 00:44:28 -0400199 &ps->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (ps->resources.count) {
201 device->power.flags.power_resources = 1;
202 ps->flags.valid = 1;
203 }
204
205 /* Evaluate "_PSx" to see if we can do explicit sets */
206 object_name[2] = 'S';
207 status = acpi_get_handle(device->handle, object_name, &handle);
208 if (ACPI_SUCCESS(status)) {
209 ps->flags.explicit_set = 1;
210 ps->flags.valid = 1;
211 }
212
213 /* State is valid if we have some power control */
214 if (ps->resources.count || ps->flags.explicit_set)
215 ps->flags.valid = 1;
216
Len Brown4be44fc2005-08-05 00:44:28 -0400217 ps->power = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 ps->latency = -1; /* Unknown - driver assigned */
219 }
220
221 /* Set defaults for D0 and D3 states (always valid) */
222 device->power.states[ACPI_STATE_D0].flags.valid = 1;
223 device->power.states[ACPI_STATE_D0].power = 100;
224 device->power.states[ACPI_STATE_D3].flags.valid = 1;
225 device->power.states[ACPI_STATE_D3].power = 0;
226
227 /* TBD: System wake support and resource requirements. */
228
229 device->power.state = ACPI_STATE_UNKNOWN;
230
231 return_VALUE(0);
232}
233
Len Brown4be44fc2005-08-05 00:44:28 -0400234int acpi_match_ids(struct acpi_device *device, char *ids)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 if (device->flags.hardware_id)
237 if (strstr(ids, device->pnp.hardware_id))
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (device->flags.compatible_ids) {
241 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
242 int i;
243
244 /* compare multiple _CID entries against driver ids */
Len Brown4be44fc2005-08-05 00:44:28 -0400245 for (i = 0; i < cid_list->count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 if (strstr(ids, cid_list->id[i].value))
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 }
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500250 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400254acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
255 union acpi_object *package)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Len Brown4be44fc2005-08-05 00:44:28 -0400257 int i = 0;
258 union acpi_object *element = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (!device || !package || (package->package.count < 2))
261 return AE_BAD_PARAMETER;
262
263 element = &(package->package.elements[0]);
264 if (!element)
265 return AE_BAD_PARAMETER;
266 if (element->type == ACPI_TYPE_PACKAGE) {
267 if ((element->package.count < 2) ||
Len Brown4be44fc2005-08-05 00:44:28 -0400268 (element->package.elements[0].type !=
269 ACPI_TYPE_LOCAL_REFERENCE)
270 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return AE_BAD_DATA;
Len Brown4be44fc2005-08-05 00:44:28 -0400272 device->wakeup.gpe_device =
273 element->package.elements[0].reference.handle;
274 device->wakeup.gpe_number =
275 (u32) element->package.elements[1].integer.value;
276 } else if (element->type == ACPI_TYPE_INTEGER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 device->wakeup.gpe_number = element->integer.value;
Len Brown4be44fc2005-08-05 00:44:28 -0400278 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return AE_BAD_DATA;
280
281 element = &(package->package.elements[1]);
282 if (element->type != ACPI_TYPE_INTEGER) {
283 return AE_BAD_DATA;
284 }
285 device->wakeup.sleep_state = element->integer.value;
286
287 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
288 return AE_NO_MEMORY;
289 }
290 device->wakeup.resources.count = package->package.count - 2;
Len Brown4be44fc2005-08-05 00:44:28 -0400291 for (i = 0; i < device->wakeup.resources.count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 element = &(package->package.elements[i + 2]);
Len Brown4be44fc2005-08-05 00:44:28 -0400293 if (element->type != ACPI_TYPE_ANY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 return AE_BAD_DATA;
295 }
296
297 device->wakeup.resources.handles[i] = element->reference.handle;
298 }
299
300 return AE_OK;
301}
302
Len Brown4be44fc2005-08-05 00:44:28 -0400303static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Len Brown4be44fc2005-08-05 00:44:28 -0400305 acpi_status status = 0;
306 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
307 union acpi_object *package = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 ACPI_FUNCTION_TRACE("acpi_bus_get_wakeup_flags");
310
311 /* _PRW */
312 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
313 if (ACPI_FAILURE(status)) {
314 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PRW\n"));
315 goto end;
316 }
317
Len Brown4be44fc2005-08-05 00:44:28 -0400318 package = (union acpi_object *)buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 status = acpi_bus_extract_wakeup_device_power_package(device, package);
320 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400321 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
322 "Error extracting _PRW package\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 goto end;
324 }
325
326 acpi_os_free(buffer.pointer);
327
328 device->wakeup.flags.valid = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400329 /* Power button, Lid switch always enable wakeup */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
331 device->wakeup.flags.run_wake = 1;
332
Len Brown4be44fc2005-08-05 00:44:28 -0400333 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if (ACPI_FAILURE(status))
335 device->flags.wake_capable = 0;
336 return_VALUE(0);
337}
338
339/* --------------------------------------------------------------------------
Kay Sievers312c0042005-11-16 09:00:00 +0100340 ACPI sysfs device file support
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400342static ssize_t acpi_eject_store(struct acpi_device *device,
343 const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345#define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
346static struct acpi_device_attribute acpi_device_attr_##_name = \
347 __ATTR(_name, _mode, _show, _store)
348
349ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
350
351/**
352 * setup_sys_fs_device_files - sets up the device files under device namespace
Martin Waitz67be2dd2005-05-01 08:59:26 -0700353 * @dev: acpi_device object
354 * @func: function pointer to create or destroy the device file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
356static void
Len Brown4be44fc2005-08-05 00:44:28 -0400357setup_sys_fs_device_files(struct acpi_device *dev,
358 acpi_device_sysfs_files * func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Len Brown4be44fc2005-08-05 00:44:28 -0400360 acpi_status status;
361 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 /*
364 * If device has _EJ0, 'eject' file is created that is used to trigger
365 * hot-removal function from userland.
366 */
367 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
368 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400369 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Len Brown4be44fc2005-08-05 00:44:28 -0400372static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 struct acpi_object_list arg_list;
375 union acpi_object arg;
376 acpi_status status = AE_OK;
377
378 /*
379 * TBD: evaluate _PS3?
380 */
381
382 if (lockable) {
383 arg_list.count = 1;
384 arg_list.pointer = &arg;
385 arg.type = ACPI_TYPE_INTEGER;
386 arg.integer.value = 0;
387 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
388 }
389
390 arg_list.count = 1;
391 arg_list.pointer = &arg;
392 arg.type = ACPI_TYPE_INTEGER;
393 arg.integer.value = 1;
394
395 /*
396 * TBD: _EJD support.
397 */
398
399 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
400 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400401 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
Len Brown4be44fc2005-08-05 00:44:28 -0400404 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static ssize_t
408acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
409{
Len Brown4be44fc2005-08-05 00:44:28 -0400410 int result;
411 int ret = count;
412 int islockable;
413 acpi_status status;
414 acpi_handle handle;
415 acpi_object_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 if ((!count) || (buf[0] != '1')) {
418 return -EINVAL;
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#ifndef FORCE_EJECT
421 if (device->driver == NULL) {
422 ret = -ENODEV;
423 goto err;
424 }
425#endif
426 status = acpi_get_type(device->handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400427 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 ret = -ENODEV;
429 goto err;
430 }
431
432 islockable = device->flags.lockable;
433 handle = device->handle;
434
Ashok Rajeefa27a2006-03-28 17:04:00 -0500435 result = acpi_bus_trim(device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 if (!result)
438 result = acpi_eject_operation(handle, islockable);
439
440 if (result) {
441 ret = -EBUSY;
442 }
Len Brown4be44fc2005-08-05 00:44:28 -0400443 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return ret;
445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/* --------------------------------------------------------------------------
448 Performance Management
449 -------------------------------------------------------------------------- */
450
Len Brown4be44fc2005-08-05 00:44:28 -0400451static int acpi_bus_get_perf_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 device->performance.state = ACPI_STATE_UNKNOWN;
454 return 0;
455}
456
457/* --------------------------------------------------------------------------
458 Driver Management
459 -------------------------------------------------------------------------- */
460
461static LIST_HEAD(acpi_bus_drivers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500464 * acpi_bus_match - match device IDs to driver's supported IDs
465 * @device: the device that we are trying to match to a driver
466 * @driver: driver whose device id table is being checked
467 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
469 * matches the specified driver's criteria.
470 */
471static int
Len Brown4be44fc2005-08-05 00:44:28 -0400472acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 if (driver && driver->ops.match)
475 return driver->ops.match(device, driver);
476 return acpi_match_ids(device, driver->ids);
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500480 * acpi_bus_driver_init - add a device to a driver
481 * @device: the device to add and initialize
482 * @driver: driver for the device
483 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 * Used to initialize a device via its device driver. Called whenever a
485 * driver is bound to a device. Invokes the driver's add() and start() ops.
486 */
487static int
Len Brown4be44fc2005-08-05 00:44:28 -0400488acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Len Brown4be44fc2005-08-05 00:44:28 -0400490 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 ACPI_FUNCTION_TRACE("acpi_bus_driver_init");
493
494 if (!device || !driver)
495 return_VALUE(-EINVAL);
496
497 if (!driver->ops.add)
498 return_VALUE(-ENOSYS);
499
500 result = driver->ops.add(device);
501 if (result) {
502 device->driver = NULL;
503 acpi_driver_data(device) = NULL;
504 return_VALUE(result);
505 }
506
507 device->driver = driver;
508
509 /*
510 * TBD - Configuration Management: Assign resources to device based
511 * upon possible configuration and currently allocated resources.
512 */
513
Len Brown4be44fc2005-08-05 00:44:28 -0400514 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
515 "Driver successfully bound to device\n"));
Rajesh Shah3fb02732005-04-28 00:25:52 -0700516 return_VALUE(0);
517}
518
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400519static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700520{
521 int result = 0;
522 struct acpi_driver *driver;
523
524 ACPI_FUNCTION_TRACE("acpi_start_single_object");
525
526 if (!(driver = device->driver))
527 return_VALUE(0);
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (driver->ops.start) {
530 result = driver->ops.start(device);
531 if (result && driver->ops.remove)
532 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
Rajesh Shah3fb02732005-04-28 00:25:52 -0700535 return_VALUE(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500538static void acpi_driver_attach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Len Brown4be44fc2005-08-05 00:44:28 -0400540 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 ACPI_FUNCTION_TRACE("acpi_driver_attach");
543
544 spin_lock(&acpi_device_lock);
545 list_for_each_safe(node, next, &acpi_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400546 struct acpi_device *dev =
547 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 if (dev->driver || !dev->status.present)
550 continue;
551 spin_unlock(&acpi_device_lock);
552
553 if (!acpi_bus_match(dev, drv)) {
554 if (!acpi_bus_driver_init(dev, drv)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -0700555 acpi_start_single_object(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 atomic_inc(&drv->references);
Len Brown4be44fc2005-08-05 00:44:28 -0400557 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
558 "Found driver [%s] for device [%s]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 drv->name, dev->pnp.bus_id));
560 }
561 }
562 spin_lock(&acpi_device_lock);
563 }
564 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400567static void acpi_driver_detach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Len Brown4be44fc2005-08-05 00:44:28 -0400569 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 ACPI_FUNCTION_TRACE("acpi_driver_detach");
572
573 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400574 list_for_each_safe(node, next, &acpi_device_list) {
575 struct acpi_device *dev =
576 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 if (dev->driver == drv) {
579 spin_unlock(&acpi_device_lock);
580 if (drv->ops.remove)
Len Brown4be44fc2005-08-05 00:44:28 -0400581 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 spin_lock(&acpi_device_lock);
583 dev->driver = NULL;
584 dev->driver_data = NULL;
585 atomic_dec(&drv->references);
586 }
587 }
588 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500592 * acpi_bus_register_driver - register a driver with the ACPI bus
593 * @driver: driver being registered
594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500596 * devices that match the driver's criteria and binds. Returns zero for
597 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 */
Len Brown4be44fc2005-08-05 00:44:28 -0400599int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 ACPI_FUNCTION_TRACE("acpi_bus_register_driver");
602
603 if (acpi_disabled)
604 return_VALUE(-ENODEV);
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 spin_lock(&acpi_device_lock);
607 list_add_tail(&driver->node, &acpi_bus_drivers);
608 spin_unlock(&acpi_device_lock);
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500609 acpi_driver_attach(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500611 return_VALUE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Len Brown4be44fc2005-08-05 00:44:28 -0400614EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500617 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
618 * @driver: driver to unregister
619 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 * Unregisters a driver with the ACPI bus. Searches the namespace for all
621 * devices that match the driver's criteria and unbinds.
622 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400623void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500625 acpi_driver_detach(driver);
626
627 if (!atomic_read(&driver->references)) {
628 spin_lock(&acpi_device_lock);
629 list_del_init(&driver->node);
630 spin_unlock(&acpi_device_lock);
631 }
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400632 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
Len Brown4be44fc2005-08-05 00:44:28 -0400634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635EXPORT_SYMBOL(acpi_bus_unregister_driver);
636
637/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500638 * acpi_bus_find_driver - check if there is a driver installed for the device
639 * @device: device that we are trying to find a supporting driver for
640 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * Parses the list of registered drivers looking for a driver applicable for
642 * the specified device.
643 */
Len Brown4be44fc2005-08-05 00:44:28 -0400644static int acpi_bus_find_driver(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Len Brown4be44fc2005-08-05 00:44:28 -0400646 int result = 0;
647 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 ACPI_FUNCTION_TRACE("acpi_bus_find_driver");
650
651 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400652 list_for_each_safe(node, next, &acpi_bus_drivers) {
653 struct acpi_driver *driver =
654 container_of(node, struct acpi_driver, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 atomic_inc(&driver->references);
657 spin_unlock(&acpi_device_lock);
658 if (!acpi_bus_match(device, driver)) {
659 result = acpi_bus_driver_init(device, driver);
660 if (!result)
661 goto Done;
662 }
663 atomic_dec(&driver->references);
664 spin_lock(&acpi_device_lock);
665 }
666 spin_unlock(&acpi_device_lock);
667
Len Brown4be44fc2005-08-05 00:44:28 -0400668 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return_VALUE(result);
670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/* --------------------------------------------------------------------------
673 Device Enumeration
674 -------------------------------------------------------------------------- */
675
Len Brown4be44fc2005-08-05 00:44:28 -0400676static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Len Brown4be44fc2005-08-05 00:44:28 -0400678 acpi_status status = AE_OK;
679 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 ACPI_FUNCTION_TRACE("acpi_bus_get_flags");
682
683 /* Presence of _STA indicates 'dynamic_status' */
684 status = acpi_get_handle(device->handle, "_STA", &temp);
685 if (ACPI_SUCCESS(status))
686 device->flags.dynamic_status = 1;
687
688 /* Presence of _CID indicates 'compatible_ids' */
689 status = acpi_get_handle(device->handle, "_CID", &temp);
690 if (ACPI_SUCCESS(status))
691 device->flags.compatible_ids = 1;
692
693 /* Presence of _RMV indicates 'removable' */
694 status = acpi_get_handle(device->handle, "_RMV", &temp);
695 if (ACPI_SUCCESS(status))
696 device->flags.removable = 1;
697
698 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
699 status = acpi_get_handle(device->handle, "_EJD", &temp);
700 if (ACPI_SUCCESS(status))
701 device->flags.ejectable = 1;
702 else {
703 status = acpi_get_handle(device->handle, "_EJ0", &temp);
704 if (ACPI_SUCCESS(status))
705 device->flags.ejectable = 1;
706 }
707
708 /* Presence of _LCK indicates 'lockable' */
709 status = acpi_get_handle(device->handle, "_LCK", &temp);
710 if (ACPI_SUCCESS(status))
711 device->flags.lockable = 1;
712
713 /* Presence of _PS0|_PR0 indicates 'power manageable' */
714 status = acpi_get_handle(device->handle, "_PS0", &temp);
715 if (ACPI_FAILURE(status))
716 status = acpi_get_handle(device->handle, "_PR0", &temp);
717 if (ACPI_SUCCESS(status))
718 device->flags.power_manageable = 1;
719
720 /* Presence of _PRW indicates wake capable */
721 status = acpi_get_handle(device->handle, "_PRW", &temp);
722 if (ACPI_SUCCESS(status))
723 device->flags.wake_capable = 1;
724
725 /* TBD: Peformance management */
726
727 return_VALUE(0);
728}
729
Len Brown4be44fc2005-08-05 00:44:28 -0400730static void acpi_device_get_busid(struct acpi_device *device,
731 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Len Brown4be44fc2005-08-05 00:44:28 -0400733 char bus_id[5] = { '?', 0 };
734 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
735 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 /*
738 * Bus ID
739 * ------
740 * The device's Bus ID is simply the object name.
741 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
742 */
743 switch (type) {
744 case ACPI_BUS_TYPE_SYSTEM:
745 strcpy(device->pnp.bus_id, "ACPI");
746 break;
747 case ACPI_BUS_TYPE_POWER_BUTTON:
748 strcpy(device->pnp.bus_id, "PWRF");
749 break;
750 case ACPI_BUS_TYPE_SLEEP_BUTTON:
751 strcpy(device->pnp.bus_id, "SLPF");
752 break;
753 default:
754 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
755 /* Clean up trailing underscores (if any) */
756 for (i = 3; i > 1; i--) {
757 if (bus_id[i] == '_')
758 bus_id[i] = '\0';
759 else
760 break;
761 }
762 strcpy(device->pnp.bus_id, bus_id);
763 break;
764 }
765}
766
Len Brown4be44fc2005-08-05 00:44:28 -0400767static void acpi_device_set_id(struct acpi_device *device,
768 struct acpi_device *parent, acpi_handle handle,
769 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Len Brown4be44fc2005-08-05 00:44:28 -0400771 struct acpi_device_info *info;
772 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
773 char *hid = NULL;
774 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400776 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778 switch (type) {
779 case ACPI_BUS_TYPE_DEVICE:
780 status = acpi_get_object_info(handle, &buffer);
781 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400782 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return;
784 }
785
786 info = buffer.pointer;
787 if (info->valid & ACPI_VALID_HID)
788 hid = info->hardware_id.value;
789 if (info->valid & ACPI_VALID_UID)
790 uid = info->unique_id.value;
791 if (info->valid & ACPI_VALID_CID)
792 cid_list = &info->compatibility_id;
793 if (info->valid & ACPI_VALID_ADR) {
794 device->pnp.bus_address = info->address;
795 device->flags.bus_address = 1;
796 }
797 break;
798 case ACPI_BUS_TYPE_POWER:
799 hid = ACPI_POWER_HID;
800 break;
801 case ACPI_BUS_TYPE_PROCESSOR:
802 hid = ACPI_PROCESSOR_HID;
803 break;
804 case ACPI_BUS_TYPE_SYSTEM:
805 hid = ACPI_SYSTEM_HID;
806 break;
807 case ACPI_BUS_TYPE_THERMAL:
808 hid = ACPI_THERMAL_HID;
809 break;
810 case ACPI_BUS_TYPE_POWER_BUTTON:
811 hid = ACPI_BUTTON_HID_POWERF;
812 break;
813 case ACPI_BUS_TYPE_SLEEP_BUTTON:
814 hid = ACPI_BUTTON_HID_SLEEPF;
815 break;
816 }
817
818 /*
819 * \_SB
820 * ----
821 * Fix for the system root bus device -- the only root-level device.
822 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500823 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 hid = ACPI_BUS_HID;
825 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
826 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
827 }
828
829 if (hid) {
830 strcpy(device->pnp.hardware_id, hid);
831 device->flags.hardware_id = 1;
832 }
833 if (uid) {
834 strcpy(device->pnp.unique_id, uid);
835 device->flags.unique_id = 1;
836 }
837 if (cid_list) {
838 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
839 if (device->pnp.cid_list)
840 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
841 else
842 printk(KERN_ERR "Memory allocation error\n");
843 }
844
845 acpi_os_free(buffer.pointer);
846}
847
Len Brown4be44fc2005-08-05 00:44:28 -0400848static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 acpi_status status = AE_OK;
851 int result = 0;
852 /*
853 * Context
854 * -------
855 * Attach this 'struct acpi_device' to the ACPI object. This makes
856 * resolutions from handle->device very efficient. Note that we need
857 * to be careful with fixed-feature devices as they all attach to the
858 * root object.
859 */
Len Brown4be44fc2005-08-05 00:44:28 -0400860 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
862 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400863 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 if (ACPI_FAILURE(status)) {
866 printk("Error attaching device data\n");
867 result = -ENODEV;
868 }
869 }
870 return result;
871}
872
Len Brown4be44fc2005-08-05 00:44:28 -0400873static void acpi_device_get_debug_info(struct acpi_device *device,
874 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
876#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400877 char *type_string = NULL;
878 char name[80] = { '?', '\0' };
879 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 switch (type) {
882 case ACPI_BUS_TYPE_DEVICE:
883 type_string = "Device";
884 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
885 break;
886 case ACPI_BUS_TYPE_POWER:
887 type_string = "Power Resource";
888 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
889 break;
890 case ACPI_BUS_TYPE_PROCESSOR:
891 type_string = "Processor";
892 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
893 break;
894 case ACPI_BUS_TYPE_SYSTEM:
895 type_string = "System";
896 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
897 break;
898 case ACPI_BUS_TYPE_THERMAL:
899 type_string = "Thermal Zone";
900 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
901 break;
902 case ACPI_BUS_TYPE_POWER_BUTTON:
903 type_string = "Power Button";
904 sprintf(name, "PWRB");
905 break;
906 case ACPI_BUS_TYPE_SLEEP_BUTTON:
907 type_string = "Sleep Button";
908 sprintf(name, "SLPB");
909 break;
910 }
911
912 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -0400913#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914}
915
Len Brown4be44fc2005-08-05 00:44:28 -0400916static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Len Brown4be44fc2005-08-05 00:44:28 -0400918 int result = 0;
919 struct acpi_driver *driver;
920
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 ACPI_FUNCTION_TRACE("acpi_bus_remove");
922
923 if (!dev)
924 return_VALUE(-EINVAL);
925
926 driver = dev->driver;
927
928 if ((driver) && (driver->ops.remove)) {
929
930 if (driver->ops.stop) {
931 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
932 if (result)
933 return_VALUE(result);
934 }
935
936 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
937 if (result) {
938 return_VALUE(result);
939 }
940
941 atomic_dec(&dev->driver->references);
942 dev->driver = NULL;
943 acpi_driver_data(dev) = NULL;
944 }
945
946 if (!rmdevice)
947 return_VALUE(0);
948
949 if (dev->flags.bus_address) {
950 if ((dev->parent) && (dev->parent->ops.unbind))
951 dev->parent->ops.unbind(dev);
952 }
Len Brown4be44fc2005-08-05 00:44:28 -0400953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
955
956 return_VALUE(0);
957}
958
Rajesh Shah3fb02732005-04-28 00:25:52 -0700959static int
Len Brown4be44fc2005-08-05 00:44:28 -0400960acpi_add_single_object(struct acpi_device **child,
961 struct acpi_device *parent, acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Len Brown4be44fc2005-08-05 00:44:28 -0400963 int result = 0;
964 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Rajesh Shah3fb02732005-04-28 00:25:52 -0700966 ACPI_FUNCTION_TRACE("acpi_add_single_object");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 if (!child)
969 return_VALUE(-EINVAL);
970
971 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
972 if (!device) {
973 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Memory allocation error\n"));
974 return_VALUE(-ENOMEM);
975 }
976 memset(device, 0, sizeof(struct acpi_device));
977
978 device->handle = handle;
979 device->parent = parent;
980
Len Brown4be44fc2005-08-05 00:44:28 -0400981 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 /*
984 * Flags
985 * -----
986 * Get prior to calling acpi_bus_get_status() so we know whether
987 * or not _STA is present. Note that we only look for object
988 * handles -- cannot evaluate objects until we know the device is
989 * present and properly initialized.
990 */
991 result = acpi_bus_get_flags(device);
992 if (result)
993 goto end;
994
995 /*
996 * Status
997 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -0500998 * See if the device is present. We always assume that non-Device
999 * and non-Processor objects (e.g. thermal zones, power resources,
1000 * etc.) are present, functioning, etc. (at least when parent object
1001 * is present). Note that _STA has a different meaning for some
1002 * objects (e.g. power resources) so we need to be careful how we use
1003 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 */
1005 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001006 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 case ACPI_BUS_TYPE_DEVICE:
1008 result = acpi_bus_get_status(device);
1009 if (ACPI_FAILURE(result) || !device->status.present) {
1010 result = -ENOENT;
1011 goto end;
1012 }
1013 break;
1014 default:
1015 STRUCT_TO_INT(device->status) = 0x0F;
1016 break;
1017 }
1018
1019 /*
1020 * Initialize Device
1021 * -----------------
1022 * TBD: Synch with Core's enumeration/initialization process.
1023 */
1024
1025 /*
1026 * Hardware ID, Unique ID, & Bus Address
1027 * -------------------------------------
1028 */
Len Brown4be44fc2005-08-05 00:44:28 -04001029 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 /*
1032 * Power Management
1033 * ----------------
1034 */
1035 if (device->flags.power_manageable) {
1036 result = acpi_bus_get_power_flags(device);
1037 if (result)
1038 goto end;
1039 }
1040
Len Brown4be44fc2005-08-05 00:44:28 -04001041 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 * Wakeup device management
1043 *-----------------------
1044 */
1045 if (device->flags.wake_capable) {
1046 result = acpi_bus_get_wakeup_device_flags(device);
1047 if (result)
1048 goto end;
1049 }
1050
1051 /*
1052 * Performance Management
1053 * ----------------------
1054 */
1055 if (device->flags.performance_manageable) {
1056 result = acpi_bus_get_perf_flags(device);
1057 if (result)
1058 goto end;
1059 }
1060
Len Brown4be44fc2005-08-05 00:44:28 -04001061 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto end;
1063
Len Brown4be44fc2005-08-05 00:44:28 -04001064 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Len Brown4be44fc2005-08-05 00:44:28 -04001066 acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /*
1069 * Bind _ADR-Based Devices
1070 * -----------------------
1071 * If there's a a bus address (_ADR) then we utilize the parent's
1072 * 'bind' function (if exists) to bind the ACPI- and natively-
1073 * enumerated device representations.
1074 */
1075 if (device->flags.bus_address) {
1076 if (device->parent && device->parent->ops.bind)
1077 device->parent->ops.bind(device);
1078 }
1079
1080 /*
1081 * Locate & Attach Driver
1082 * ----------------------
1083 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1084 * to see if there's a driver installed for this kind of device. Note
1085 * that drivers can install before or after a device is enumerated.
1086 *
1087 * TBD: Assumes LDM provides driver hot-plug capability.
1088 */
Thomas Renningerbd7ce5b2005-10-03 10:39:00 -07001089 acpi_bus_find_driver(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Len Brown4be44fc2005-08-05 00:44:28 -04001091 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 if (!result)
1093 *child = device;
1094 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001095 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 kfree(device);
1097 }
1098
1099 return_VALUE(result);
1100}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Len Brown4be44fc2005-08-05 00:44:28 -04001102static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Len Brown4be44fc2005-08-05 00:44:28 -04001104 acpi_status status = AE_OK;
1105 struct acpi_device *parent = NULL;
1106 struct acpi_device *child = NULL;
1107 acpi_handle phandle = NULL;
1108 acpi_handle chandle = NULL;
1109 acpi_object_type type = 0;
1110 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 ACPI_FUNCTION_TRACE("acpi_bus_scan");
1113
1114 if (!start)
1115 return_VALUE(-EINVAL);
1116
1117 parent = start;
1118 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 /*
1121 * Parse through the ACPI namespace, identify all 'devices', and
1122 * create a new 'struct acpi_device' for each.
1123 */
1124 while ((level > 0) && parent) {
1125
1126 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001127 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 /*
1130 * If this scope is exhausted then move our way back up.
1131 */
1132 if (ACPI_FAILURE(status)) {
1133 level--;
1134 chandle = phandle;
1135 acpi_get_parent(phandle, &phandle);
1136 if (parent->parent)
1137 parent = parent->parent;
1138 continue;
1139 }
1140
1141 status = acpi_get_type(chandle, &type);
1142 if (ACPI_FAILURE(status))
1143 continue;
1144
1145 /*
1146 * If this is a scope object then parse it (depth-first).
1147 */
1148 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1149 level++;
1150 phandle = chandle;
1151 chandle = NULL;
1152 continue;
1153 }
1154
1155 /*
1156 * We're only interested in objects that we consider 'devices'.
1157 */
1158 switch (type) {
1159 case ACPI_TYPE_DEVICE:
1160 type = ACPI_BUS_TYPE_DEVICE;
1161 break;
1162 case ACPI_TYPE_PROCESSOR:
1163 type = ACPI_BUS_TYPE_PROCESSOR;
1164 break;
1165 case ACPI_TYPE_THERMAL:
1166 type = ACPI_BUS_TYPE_THERMAL;
1167 break;
1168 case ACPI_TYPE_POWER:
1169 type = ACPI_BUS_TYPE_POWER;
1170 break;
1171 default:
1172 continue;
1173 }
1174
Rajesh Shah3fb02732005-04-28 00:25:52 -07001175 if (ops->acpi_op_add)
1176 status = acpi_add_single_object(&child, parent,
Len Brown4be44fc2005-08-05 00:44:28 -04001177 chandle, type);
1178 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001179 status = acpi_bus_get_device(chandle, &child);
1180
Len Brown4be44fc2005-08-05 00:44:28 -04001181 if (ACPI_FAILURE(status))
1182 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001183
1184 if (ops->acpi_op_start) {
1185 status = acpi_start_single_object(child);
1186 if (ACPI_FAILURE(status))
1187 continue;
1188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
1191 * If the device is present, enabled, and functioning then
1192 * parse its scope (depth-first). Note that we need to
1193 * represent absent devices to facilitate PnP notifications
1194 * -- but only the subtree head (not all of its children,
1195 * which will be enumerated when the parent is inserted).
1196 *
1197 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001198 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 */
1200 if (child->status.present) {
1201 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1202 NULL, NULL);
1203 if (ACPI_SUCCESS(status)) {
1204 level++;
1205 phandle = chandle;
1206 chandle = NULL;
1207 parent = child;
1208 }
1209 }
1210 }
1211
1212 return_VALUE(0);
1213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Rajesh Shah3fb02732005-04-28 00:25:52 -07001215int
Len Brown4be44fc2005-08-05 00:44:28 -04001216acpi_bus_add(struct acpi_device **child,
1217 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001218{
1219 int result;
1220 struct acpi_bus_ops ops;
1221
1222 ACPI_FUNCTION_TRACE("acpi_bus_add");
1223
1224 result = acpi_add_single_object(child, parent, handle, type);
1225 if (!result) {
1226 memset(&ops, 0, sizeof(ops));
1227 ops.acpi_op_add = 1;
1228 result = acpi_bus_scan(*child, &ops);
1229 }
1230 return_VALUE(result);
1231}
Len Brown4be44fc2005-08-05 00:44:28 -04001232
Rajesh Shah3fb02732005-04-28 00:25:52 -07001233EXPORT_SYMBOL(acpi_bus_add);
1234
Len Brown4be44fc2005-08-05 00:44:28 -04001235int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001236{
1237 int result;
1238 struct acpi_bus_ops ops;
1239
1240 ACPI_FUNCTION_TRACE("acpi_bus_start");
1241
1242 if (!device)
1243 return_VALUE(-EINVAL);
1244
1245 result = acpi_start_single_object(device);
1246 if (!result) {
1247 memset(&ops, 0, sizeof(ops));
1248 ops.acpi_op_start = 1;
1249 result = acpi_bus_scan(device, &ops);
1250 }
1251 return_VALUE(result);
1252}
Len Brown4be44fc2005-08-05 00:44:28 -04001253
Rajesh Shah3fb02732005-04-28 00:25:52 -07001254EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Kristen Accardiceaba662006-02-23 17:56:01 -08001256int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Len Brown4be44fc2005-08-05 00:44:28 -04001258 acpi_status status;
1259 struct acpi_device *parent, *child;
1260 acpi_handle phandle, chandle;
1261 acpi_object_type type;
1262 u32 level = 1;
1263 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Len Brown4be44fc2005-08-05 00:44:28 -04001265 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 phandle = start->handle;
1267 child = chandle = NULL;
1268
1269 while ((level > 0) && parent && (!err)) {
1270 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001271 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
1273 /*
1274 * If this scope is exhausted then move our way back up.
1275 */
1276 if (ACPI_FAILURE(status)) {
1277 level--;
1278 chandle = phandle;
1279 acpi_get_parent(phandle, &phandle);
1280 child = parent;
1281 parent = parent->parent;
1282
1283 if (level == 0)
1284 err = acpi_bus_remove(child, rmdevice);
1285 else
1286 err = acpi_bus_remove(child, 1);
1287
1288 continue;
1289 }
1290
1291 status = acpi_get_type(chandle, &type);
1292 if (ACPI_FAILURE(status)) {
1293 continue;
1294 }
1295 /*
1296 * If there is a device corresponding to chandle then
1297 * parse it (depth-first).
1298 */
1299 if (acpi_bus_get_device(chandle, &child) == 0) {
1300 level++;
1301 phandle = chandle;
1302 chandle = NULL;
1303 parent = child;
1304 }
1305 continue;
1306 }
1307 return err;
1308}
Kristen Accardiceaba662006-02-23 17:56:01 -08001309EXPORT_SYMBOL_GPL(acpi_bus_trim);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Len Brown4be44fc2005-08-05 00:44:28 -04001312static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Len Brown4be44fc2005-08-05 00:44:28 -04001314 int result = 0;
1315 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 ACPI_FUNCTION_TRACE("acpi_bus_scan_fixed");
1318
1319 if (!root)
1320 return_VALUE(-ENODEV);
1321
1322 /*
1323 * Enumerate all fixed-feature devices.
1324 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001325 if (acpi_fadt.pwr_button == 0) {
1326 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001327 NULL,
1328 ACPI_BUS_TYPE_POWER_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001329 if (!result)
1330 result = acpi_start_single_object(device);
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
Rajesh Shah3fb02732005-04-28 00:25:52 -07001333 if (acpi_fadt.sleep_button == 0) {
1334 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001335 NULL,
1336 ACPI_BUS_TYPE_SLEEP_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001337 if (!result)
1338 result = acpi_start_single_object(device);
1339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 return_VALUE(result);
1342}
1343
Patrick Mochel5b327262006-05-10 10:33:00 -04001344
Len Brown531881d2006-05-15 03:06:41 -04001345static inline struct acpi_device * to_acpi_dev(struct device * dev)
Patrick Mochel5b327262006-05-10 10:33:00 -04001346{
1347 return container_of(dev, struct acpi_device, dev);
1348}
1349
1350
Len Brown531881d2006-05-15 03:06:41 -04001351static int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
Patrick Mochel5b327262006-05-10 10:33:00 -04001352{
1353 struct acpi_device * dev, * next;
1354 int result;
1355
1356 spin_lock(&acpi_device_lock);
1357 list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
1358 if (dev->driver && dev->driver->ops.suspend) {
1359 spin_unlock(&acpi_device_lock);
Patrick Mochel5b327262006-05-10 10:33:00 -04001360 result = dev->driver->ops.suspend(dev, 0);
1361 if (result) {
1362 printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
1363 acpi_device_name(dev),
1364 acpi_device_bid(dev), result);
1365 }
1366 spin_lock(&acpi_device_lock);
1367 }
1368 }
1369 spin_unlock(&acpi_device_lock);
1370 return 0;
1371}
1372
1373
1374static int acpi_device_suspend(struct device * dev, pm_message_t state)
1375{
1376 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1377
1378 /*
1379 * For now, we should only register 1 generic device -
1380 * the ACPI root device - and from there, we walk the
1381 * tree of ACPI devices to suspend each one using the
1382 * ACPI driver methods.
1383 */
1384 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
Len Brown531881d2006-05-15 03:06:41 -04001385 root_suspend(acpi_dev, state);
Patrick Mochel5b327262006-05-10 10:33:00 -04001386 return 0;
1387}
1388
1389
1390
1391static int root_resume(struct acpi_device * acpi_dev)
1392{
1393 struct acpi_device * dev, * next;
1394 int result;
1395
1396 spin_lock(&acpi_device_lock);
1397 list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
1398 if (dev->driver && dev->driver->ops.resume) {
1399 spin_unlock(&acpi_device_lock);
Patrick Mochel5b327262006-05-10 10:33:00 -04001400 result = dev->driver->ops.resume(dev, 0);
1401 if (result) {
1402 printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
1403 acpi_device_name(dev),
1404 acpi_device_bid(dev), result);
1405 }
1406 spin_lock(&acpi_device_lock);
1407 }
1408 }
1409 spin_unlock(&acpi_device_lock);
1410 return 0;
1411}
1412
1413
1414static int acpi_device_resume(struct device * dev)
1415{
1416 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1417
1418 /*
1419 * For now, we should only register 1 generic device -
1420 * the ACPI root device - and from there, we walk the
1421 * tree of ACPI devices to resume each one using the
1422 * ACPI driver methods.
1423 */
1424 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
1425 root_resume(acpi_dev);
1426 return 0;
1427}
1428
1429
1430struct bus_type acpi_bus_type = {
1431 .name = "acpi",
1432 .suspend = acpi_device_suspend,
1433 .resume = acpi_device_resume,
1434};
1435
1436
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438static int __init acpi_scan_init(void)
1439{
1440 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001441 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 ACPI_FUNCTION_TRACE("acpi_scan_init");
1444
1445 if (acpi_disabled)
1446 return_VALUE(0);
1447
1448 kset_register(&acpi_namespace_kset);
1449
Patrick Mochel5b327262006-05-10 10:33:00 -04001450 result = bus_register(&acpi_bus_type);
1451 if (result) {
1452 /* We don't want to quit even if we failed to add suspend/resume */
1453 printk(KERN_ERR PREFIX "Could not register bus type\n");
1454 }
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 /*
1457 * Create the root device in the bus's device tree
1458 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001459 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -04001460 ACPI_BUS_TYPE_SYSTEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (result)
1462 goto Done;
1463
Rajesh Shah3fb02732005-04-28 00:25:52 -07001464 result = acpi_start_single_object(acpi_root);
Patrick Mochel5b327262006-05-10 10:33:00 -04001465 if (result)
1466 goto Done;
1467
1468 acpi_root->dev.bus = &acpi_bus_type;
1469 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1470 result = device_register(&acpi_root->dev);
1471 if (result) {
1472 /* We don't want to quit even if we failed to add suspend/resume */
1473 printk(KERN_ERR PREFIX "Could not register device\n");
1474 }
Rajesh Shah3fb02732005-04-28 00:25:52 -07001475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 /*
1477 * Enumerate devices in the ACPI namespace.
1478 */
1479 result = acpi_bus_scan_fixed(acpi_root);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001480 if (!result) {
1481 memset(&ops, 0, sizeof(ops));
1482 ops.acpi_op_add = 1;
1483 ops.acpi_op_start = 1;
1484 result = acpi_bus_scan(acpi_root, &ops);
1485 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 if (result)
1488 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1489
Len Brown4be44fc2005-08-05 00:44:28 -04001490 Done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return_VALUE(result);
1492}
1493
1494subsys_initcall(acpi_scan_init);