blob: 5b42948ea97f4419ba317269c2d3d37e1e5f9adc [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>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04007#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/acpi.h>
9
10#include <acpi/acpi_drivers.h>
11#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#define _COMPONENT ACPI_BUS_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040014ACPI_MODULE_NAME("scan")
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040016extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#define ACPI_BUS_CLASS "system_bus"
19#define ACPI_BUS_HID "ACPI_BUS"
20#define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
21#define ACPI_BUS_DEVICE_NAME "System Bus"
22
23static LIST_HEAD(acpi_device_list);
24DEFINE_SPINLOCK(acpi_device_lock);
25LIST_HEAD(acpi_wakeup_device_list);
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Len Brown4be44fc2005-08-05 00:44:28 -040028static void acpi_device_release(struct kobject *kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Len Brown4be44fc2005-08-05 00:44:28 -040030 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
Jesper Juhl6044ec82005-11-07 01:01:32 -080031 kfree(dev->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 kfree(dev);
33}
34
35struct acpi_device_attribute {
36 struct attribute attr;
Len Brown4be44fc2005-08-05 00:44:28 -040037 ssize_t(*show) (struct acpi_device *, char *);
38 ssize_t(*store) (struct acpi_device *, const char *, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
41typedef void acpi_device_sysfs_files(struct kobject *,
Len Brown4be44fc2005-08-05 00:44:28 -040042 const struct attribute *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44static void setup_sys_fs_device_files(struct acpi_device *dev,
Len Brown4be44fc2005-08-05 00:44:28 -040045 acpi_device_sysfs_files * func);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define create_sysfs_device_files(dev) \
48 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
49#define remove_sysfs_device_files(dev) \
50 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
51
Zhang Rui1d268b02006-12-07 20:56:19 +080052#define to_acpi_dev(n) container_of(n, struct acpi_device, kobj)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
54
55static ssize_t acpi_device_attr_show(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040056 struct attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Zhang Rui1d268b02006-12-07 20:56:19 +080058 struct acpi_device *device = to_acpi_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050060 return attribute->show ? attribute->show(device, buf) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62static ssize_t acpi_device_attr_store(struct kobject *kobj,
Len Brown4be44fc2005-08-05 00:44:28 -040063 struct attribute *attr, const char *buf,
64 size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Zhang Rui1d268b02006-12-07 20:56:19 +080066 struct acpi_device *device = to_acpi_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct acpi_device_attribute *attribute = to_handle_attr(attr);
Dmitry Torokhov70f28172005-04-29 01:27:34 -050068 return attribute->store ? attribute->store(device, buf, len) : -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
71static struct sysfs_ops acpi_device_sysfs_ops = {
Len Brown4be44fc2005-08-05 00:44:28 -040072 .show = acpi_device_attr_show,
73 .store = acpi_device_attr_store,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
76static struct kobj_type ktype_acpi_ns = {
Len Brown4be44fc2005-08-05 00:44:28 -040077 .sysfs_ops = &acpi_device_sysfs_ops,
78 .release = acpi_device_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079};
80
Kay Sievers312c0042005-11-16 09:00:00 +010081static int namespace_uevent(struct kset *kset, struct kobject *kobj,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 char **envp, int num_envp, char *buffer,
83 int buffer_size)
84{
Zhang Rui1d268b02006-12-07 20:56:19 +080085 struct acpi_device *dev = to_acpi_dev(kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int i = 0;
87 int len = 0;
88
89 if (!dev->driver)
90 return 0;
91
Kay Sievers312c0042005-11-16 09:00:00 +010092 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
93 "PHYSDEVDRIVER=%s", dev->driver->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return -ENOMEM;
95
96 envp[i] = NULL;
97
98 return 0;
99}
100
Kay Sievers312c0042005-11-16 09:00:00 +0100101static struct kset_uevent_ops namespace_uevent_ops = {
102 .uevent = &namespace_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103};
104
105static struct kset acpi_namespace_kset = {
Len Brown4be44fc2005-08-05 00:44:28 -0400106 .kobj = {
107 .name = "namespace",
108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 .subsys = &acpi_subsys,
Len Brown4be44fc2005-08-05 00:44:28 -0400110 .ktype = &ktype_acpi_ns,
Kay Sievers312c0042005-11-16 09:00:00 +0100111 .uevent_ops = &namespace_uevent_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112};
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/* --------------------------------------------------------------------------
Kay Sievers312c0042005-11-16 09:00:00 +0100115 ACPI sysfs device file support
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 -------------------------------------------------------------------------- */
Len Brown4be44fc2005-08-05 00:44:28 -0400117static ssize_t acpi_eject_store(struct acpi_device *device,
118 const char *buf, size_t count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120#define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \
121static struct acpi_device_attribute acpi_device_attr_##_name = \
122 __ATTR(_name, _mode, _show, _store)
123
124ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
125
126/**
127 * setup_sys_fs_device_files - sets up the device files under device namespace
Martin Waitz67be2dd2005-05-01 08:59:26 -0700128 * @dev: acpi_device object
129 * @func: function pointer to create or destroy the device file
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131static void
Len Brown4be44fc2005-08-05 00:44:28 -0400132setup_sys_fs_device_files(struct acpi_device *dev,
133 acpi_device_sysfs_files * func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Len Brown4be44fc2005-08-05 00:44:28 -0400135 acpi_status status;
136 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /*
139 * If device has _EJ0, 'eject' file is created that is used to trigger
140 * hot-removal function from userland.
141 */
142 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
143 if (ACPI_SUCCESS(status))
Len Brown4be44fc2005-08-05 00:44:28 -0400144 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Len Brown4be44fc2005-08-05 00:44:28 -0400147static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 struct acpi_object_list arg_list;
150 union acpi_object arg;
151 acpi_status status = AE_OK;
152
153 /*
154 * TBD: evaluate _PS3?
155 */
156
157 if (lockable) {
158 arg_list.count = 1;
159 arg_list.pointer = &arg;
160 arg.type = ACPI_TYPE_INTEGER;
161 arg.integer.value = 0;
162 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
163 }
164
165 arg_list.count = 1;
166 arg_list.pointer = &arg;
167 arg.type = ACPI_TYPE_INTEGER;
168 arg.integer.value = 1;
169
170 /*
171 * TBD: _EJD support.
172 */
173
174 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
175 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400176 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178
Len Brown4be44fc2005-08-05 00:44:28 -0400179 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static ssize_t
183acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
184{
Len Brown4be44fc2005-08-05 00:44:28 -0400185 int result;
186 int ret = count;
187 int islockable;
188 acpi_status status;
189 acpi_handle handle;
190 acpi_object_type type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if ((!count) || (buf[0] != '1')) {
193 return -EINVAL;
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#ifndef FORCE_EJECT
196 if (device->driver == NULL) {
197 ret = -ENODEV;
198 goto err;
199 }
200#endif
201 status = acpi_get_type(device->handle, &type);
Len Brown4be44fc2005-08-05 00:44:28 -0400202 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 ret = -ENODEV;
204 goto err;
205 }
206
207 islockable = device->flags.lockable;
208 handle = device->handle;
209
Ashok Rajeefa27a2006-03-28 17:04:00 -0500210 result = acpi_bus_trim(device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if (!result)
213 result = acpi_eject_operation(handle, islockable);
214
215 if (result) {
216 ret = -EBUSY;
217 }
Len Brown4be44fc2005-08-05 00:44:28 -0400218 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return ret;
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800223 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 -------------------------------------------------------------------------- */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800225static int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
226{
227 struct acpi_device * dev, * next;
228 int result;
229
230 spin_lock(&acpi_device_lock);
231 list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
232 if (dev->driver && dev->driver->ops.suspend) {
233 spin_unlock(&acpi_device_lock);
234 result = dev->driver->ops.suspend(dev, 0);
235 if (result) {
236 printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
237 acpi_device_name(dev),
238 acpi_device_bid(dev), result);
239 }
240 spin_lock(&acpi_device_lock);
241 }
242 }
243 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return 0;
245}
246
Zhang Rui9e89dde2006-12-07 20:56:16 +0800247static int acpi_device_suspend(struct device * dev, pm_message_t state)
248{
Zhang Rui1d268b02006-12-07 20:56:19 +0800249 struct acpi_device * acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Zhang Rui9e89dde2006-12-07 20:56:16 +0800251 /*
252 * For now, we should only register 1 generic device -
253 * the ACPI root device - and from there, we walk the
254 * tree of ACPI devices to suspend each one using the
255 * ACPI driver methods.
256 */
257 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
258 root_suspend(acpi_dev, state);
259 return 0;
260}
261
262static int root_resume(struct acpi_device * acpi_dev)
263{
264 struct acpi_device * dev, * next;
265 int result;
266
267 spin_lock(&acpi_device_lock);
268 list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
269 if (dev->driver && dev->driver->ops.resume) {
270 spin_unlock(&acpi_device_lock);
271 result = dev->driver->ops.resume(dev, 0);
272 if (result) {
273 printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
274 acpi_device_name(dev),
275 acpi_device_bid(dev), result);
276 }
277 spin_lock(&acpi_device_lock);
278 }
279 }
280 spin_unlock(&acpi_device_lock);
281 return 0;
282}
283
284static int acpi_device_resume(struct device * dev)
285{
Zhang Rui1d268b02006-12-07 20:56:19 +0800286 struct acpi_device * acpi_dev = to_acpi_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800287
288 /*
289 * For now, we should only register 1 generic device -
290 * the ACPI root device - and from there, we walk the
291 * tree of ACPI devices to resume each one using the
292 * ACPI driver methods.
293 */
294 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
295 root_resume(acpi_dev);
296 return 0;
297}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500300 * acpi_bus_match - match device IDs to driver's supported IDs
301 * @device: the device that we are trying to match to a driver
302 * @driver: driver whose device id table is being checked
303 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
305 * matches the specified driver's criteria.
306 */
307static int
Len Brown4be44fc2005-08-05 00:44:28 -0400308acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 if (driver && driver->ops.match)
311 return driver->ops.match(device, driver);
312 return acpi_match_ids(device, driver->ids);
313}
314
Zhang Rui9e89dde2006-12-07 20:56:16 +0800315static struct bus_type acpi_bus_type = {
316 .name = "acpi",
317 .suspend = acpi_device_suspend,
318 .resume = acpi_device_resume,
319};
320
321static void acpi_device_register(struct acpi_device *device,
322 struct acpi_device *parent)
323{
324 int err;
325
326 /*
327 * Linkage
328 * -------
329 * Link this device to its parent and siblings.
330 */
331 INIT_LIST_HEAD(&device->children);
332 INIT_LIST_HEAD(&device->node);
333 INIT_LIST_HEAD(&device->g_list);
334 INIT_LIST_HEAD(&device->wakeup_list);
335
336 spin_lock(&acpi_device_lock);
337 if (device->parent) {
338 list_add_tail(&device->node, &device->parent->children);
339 list_add_tail(&device->g_list, &device->parent->g_list);
340 } else
341 list_add_tail(&device->g_list, &acpi_device_list);
342 if (device->wakeup.flags.valid)
343 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
344 spin_unlock(&acpi_device_lock);
345
346 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
347 if (parent)
348 device->kobj.parent = &parent->kobj;
349 device->kobj.ktype = &ktype_acpi_ns;
350 device->kobj.kset = &acpi_namespace_kset;
351 err = kobject_register(&device->kobj);
352 if (err < 0)
353 printk(KERN_WARNING "%s: kobject_register error: %d\n",
354 __FUNCTION__, err);
355 create_sysfs_device_files(device);
356}
357
358static void acpi_device_unregister(struct acpi_device *device, int type)
359{
360 spin_lock(&acpi_device_lock);
361 if (device->parent) {
362 list_del(&device->node);
363 list_del(&device->g_list);
364 } else
365 list_del(&device->g_list);
366
367 list_del(&device->wakeup_list);
368
369 spin_unlock(&acpi_device_lock);
370
371 acpi_detach_data(device->handle, acpi_bus_data_handler);
372 remove_sysfs_device_files(device);
373 kobject_unregister(&device->kobj);
374}
375
376/* --------------------------------------------------------------------------
377 Driver Management
378 -------------------------------------------------------------------------- */
379static LIST_HEAD(acpi_bus_drivers);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500382 * acpi_bus_driver_init - add a device to a driver
383 * @device: the device to add and initialize
384 * @driver: driver for the device
385 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * Used to initialize a device via its device driver. Called whenever a
387 * driver is bound to a device. Invokes the driver's add() and start() ops.
388 */
389static int
Len Brown4be44fc2005-08-05 00:44:28 -0400390acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Len Brown4be44fc2005-08-05 00:44:28 -0400392 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400396 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400399 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 result = driver->ops.add(device);
402 if (result) {
403 device->driver = NULL;
404 acpi_driver_data(device) = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400405 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 device->driver = driver;
409
410 /*
411 * TBD - Configuration Management: Assign resources to device based
412 * upon possible configuration and currently allocated resources.
413 */
414
Len Brown4be44fc2005-08-05 00:44:28 -0400415 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
416 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400417 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700418}
419
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400420static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700421{
422 int result = 0;
423 struct acpi_driver *driver;
424
Rajesh Shah3fb02732005-04-28 00:25:52 -0700425
426 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400427 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 if (driver->ops.start) {
430 result = driver->ops.start(device);
431 if (result && driver->ops.remove)
432 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
Patrick Mocheld550d982006-06-27 00:41:40 -0400435 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500438static void acpi_driver_attach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Len Brown4be44fc2005-08-05 00:44:28 -0400440 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 spin_lock(&acpi_device_lock);
444 list_for_each_safe(node, next, &acpi_device_list) {
Len Brown4be44fc2005-08-05 00:44:28 -0400445 struct acpi_device *dev =
446 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (dev->driver || !dev->status.present)
449 continue;
450 spin_unlock(&acpi_device_lock);
451
452 if (!acpi_bus_match(dev, drv)) {
453 if (!acpi_bus_driver_init(dev, drv)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -0700454 acpi_start_single_object(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 atomic_inc(&drv->references);
Len Brown4be44fc2005-08-05 00:44:28 -0400456 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
457 "Found driver [%s] for device [%s]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 drv->name, dev->pnp.bus_id));
459 }
460 }
461 spin_lock(&acpi_device_lock);
462 }
463 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464}
465
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400466static void acpi_driver_detach(struct acpi_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Len Brown4be44fc2005-08-05 00:44:28 -0400468 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400472 list_for_each_safe(node, next, &acpi_device_list) {
473 struct acpi_device *dev =
474 container_of(node, struct acpi_device, g_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (dev->driver == drv) {
477 spin_unlock(&acpi_device_lock);
478 if (drv->ops.remove)
Len Brown4be44fc2005-08-05 00:44:28 -0400479 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 spin_lock(&acpi_device_lock);
481 dev->driver = NULL;
482 dev->driver_data = NULL;
483 atomic_dec(&drv->references);
484 }
485 }
486 spin_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500490 * acpi_bus_register_driver - register a driver with the ACPI bus
491 * @driver: driver being registered
492 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500494 * devices that match the driver's criteria and binds. Returns zero for
495 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 */
Len Brown4be44fc2005-08-05 00:44:28 -0400497int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400501 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 spin_lock(&acpi_device_lock);
504 list_add_tail(&driver->node, &acpi_bus_drivers);
505 spin_unlock(&acpi_device_lock);
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500506 acpi_driver_attach(driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Patrick Mocheld550d982006-06-27 00:41:40 -0400508 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Len Brown4be44fc2005-08-05 00:44:28 -0400511EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500514 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
515 * @driver: driver to unregister
516 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 * Unregisters a driver with the ACPI bus. Searches the namespace for all
518 * devices that match the driver's criteria and unbinds.
519 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400520void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Bjorn Helgaas1a365612006-03-28 17:04:00 -0500522 acpi_driver_detach(driver);
523
524 if (!atomic_read(&driver->references)) {
525 spin_lock(&acpi_device_lock);
526 list_del_init(&driver->node);
527 spin_unlock(&acpi_device_lock);
528 }
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400529 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
Len Brown4be44fc2005-08-05 00:44:28 -0400531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532EXPORT_SYMBOL(acpi_bus_unregister_driver);
533
534/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500535 * acpi_bus_find_driver - check if there is a driver installed for the device
536 * @device: device that we are trying to find a supporting driver for
537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * Parses the list of registered drivers looking for a driver applicable for
539 * the specified device.
540 */
Len Brown4be44fc2005-08-05 00:44:28 -0400541static int acpi_bus_find_driver(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Len Brown4be44fc2005-08-05 00:44:28 -0400543 int result = 0;
544 struct list_head *node, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 spin_lock(&acpi_device_lock);
Len Brown4be44fc2005-08-05 00:44:28 -0400548 list_for_each_safe(node, next, &acpi_bus_drivers) {
549 struct acpi_driver *driver =
550 container_of(node, struct acpi_driver, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 atomic_inc(&driver->references);
553 spin_unlock(&acpi_device_lock);
554 if (!acpi_bus_match(device, driver)) {
555 result = acpi_bus_driver_init(device, driver);
556 if (!result)
557 goto Done;
558 }
559 atomic_dec(&driver->references);
560 spin_lock(&acpi_device_lock);
561 }
562 spin_unlock(&acpi_device_lock);
563
Len Brown4be44fc2005-08-05 00:44:28 -0400564 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -0400565 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/* --------------------------------------------------------------------------
569 Device Enumeration
570 -------------------------------------------------------------------------- */
Len Brownc8f7a622006-07-09 17:22:28 -0400571acpi_status
572acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
573{
574 acpi_status status;
575 acpi_handle tmp;
576 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
577 union acpi_object *obj;
578
579 status = acpi_get_handle(handle, "_EJD", &tmp);
580 if (ACPI_FAILURE(status))
581 return status;
582
583 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
584 if (ACPI_SUCCESS(status)) {
585 obj = buffer.pointer;
586 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
587 kfree(buffer.pointer);
588 }
589 return status;
590}
591EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
592
Zhang Rui9e89dde2006-12-07 20:56:16 +0800593void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
594{
595
596 /* TBD */
597
598 return;
599}
600
601int acpi_match_ids(struct acpi_device *device, char *ids)
602{
603 if (device->flags.hardware_id)
604 if (strstr(ids, device->pnp.hardware_id))
605 return 0;
606
607 if (device->flags.compatible_ids) {
608 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
609 int i;
610
611 /* compare multiple _CID entries against driver ids */
612 for (i = 0; i < cid_list->count; i++) {
613 if (strstr(ids, cid_list->id[i].value))
614 return 0;
615 }
616 }
617 return -ENOENT;
618}
619
620static int acpi_bus_get_perf_flags(struct acpi_device *device)
621{
622 device->performance.state = ACPI_STATE_UNKNOWN;
623 return 0;
624}
625
626static acpi_status
627acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
628 union acpi_object *package)
629{
630 int i = 0;
631 union acpi_object *element = NULL;
632
633 if (!device || !package || (package->package.count < 2))
634 return AE_BAD_PARAMETER;
635
636 element = &(package->package.elements[0]);
637 if (!element)
638 return AE_BAD_PARAMETER;
639 if (element->type == ACPI_TYPE_PACKAGE) {
640 if ((element->package.count < 2) ||
641 (element->package.elements[0].type !=
642 ACPI_TYPE_LOCAL_REFERENCE)
643 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
644 return AE_BAD_DATA;
645 device->wakeup.gpe_device =
646 element->package.elements[0].reference.handle;
647 device->wakeup.gpe_number =
648 (u32) element->package.elements[1].integer.value;
649 } else if (element->type == ACPI_TYPE_INTEGER) {
650 device->wakeup.gpe_number = element->integer.value;
651 } else
652 return AE_BAD_DATA;
653
654 element = &(package->package.elements[1]);
655 if (element->type != ACPI_TYPE_INTEGER) {
656 return AE_BAD_DATA;
657 }
658 device->wakeup.sleep_state = element->integer.value;
659
660 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
661 return AE_NO_MEMORY;
662 }
663 device->wakeup.resources.count = package->package.count - 2;
664 for (i = 0; i < device->wakeup.resources.count; i++) {
665 element = &(package->package.elements[i + 2]);
666 if (element->type != ACPI_TYPE_ANY) {
667 return AE_BAD_DATA;
668 }
669
670 device->wakeup.resources.handles[i] = element->reference.handle;
671 }
672
673 return AE_OK;
674}
675
676static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
677{
678 acpi_status status = 0;
679 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
680 union acpi_object *package = NULL;
681
682
683 /* _PRW */
684 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
685 if (ACPI_FAILURE(status)) {
686 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
687 goto end;
688 }
689
690 package = (union acpi_object *)buffer.pointer;
691 status = acpi_bus_extract_wakeup_device_power_package(device, package);
692 if (ACPI_FAILURE(status)) {
693 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
694 goto end;
695 }
696
697 kfree(buffer.pointer);
698
699 device->wakeup.flags.valid = 1;
700 /* Power button, Lid switch always enable wakeup */
701 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
702 device->wakeup.flags.run_wake = 1;
703
704 end:
705 if (ACPI_FAILURE(status))
706 device->flags.wake_capable = 0;
707 return 0;
708}
709
710static int acpi_bus_get_power_flags(struct acpi_device *device)
711{
712 acpi_status status = 0;
713 acpi_handle handle = NULL;
714 u32 i = 0;
715
716
717 /*
718 * Power Management Flags
719 */
720 status = acpi_get_handle(device->handle, "_PSC", &handle);
721 if (ACPI_SUCCESS(status))
722 device->power.flags.explicit_get = 1;
723 status = acpi_get_handle(device->handle, "_IRC", &handle);
724 if (ACPI_SUCCESS(status))
725 device->power.flags.inrush_current = 1;
726
727 /*
728 * Enumerate supported power management states
729 */
730 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
731 struct acpi_device_power_state *ps = &device->power.states[i];
732 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
733
734 /* Evaluate "_PRx" to se if power resources are referenced */
735 acpi_evaluate_reference(device->handle, object_name, NULL,
736 &ps->resources);
737 if (ps->resources.count) {
738 device->power.flags.power_resources = 1;
739 ps->flags.valid = 1;
740 }
741
742 /* Evaluate "_PSx" to see if we can do explicit sets */
743 object_name[2] = 'S';
744 status = acpi_get_handle(device->handle, object_name, &handle);
745 if (ACPI_SUCCESS(status)) {
746 ps->flags.explicit_set = 1;
747 ps->flags.valid = 1;
748 }
749
750 /* State is valid if we have some power control */
751 if (ps->resources.count || ps->flags.explicit_set)
752 ps->flags.valid = 1;
753
754 ps->power = -1; /* Unknown - driver assigned */
755 ps->latency = -1; /* Unknown - driver assigned */
756 }
757
758 /* Set defaults for D0 and D3 states (always valid) */
759 device->power.states[ACPI_STATE_D0].flags.valid = 1;
760 device->power.states[ACPI_STATE_D0].power = 100;
761 device->power.states[ACPI_STATE_D3].flags.valid = 1;
762 device->power.states[ACPI_STATE_D3].power = 0;
763
764 /* TBD: System wake support and resource requirements. */
765
766 device->power.state = ACPI_STATE_UNKNOWN;
767
768 return 0;
769}
Len Brownc8f7a622006-07-09 17:22:28 -0400770
Len Brown4be44fc2005-08-05 00:44:28 -0400771static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Len Brown4be44fc2005-08-05 00:44:28 -0400773 acpi_status status = AE_OK;
774 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /* Presence of _STA indicates 'dynamic_status' */
778 status = acpi_get_handle(device->handle, "_STA", &temp);
779 if (ACPI_SUCCESS(status))
780 device->flags.dynamic_status = 1;
781
782 /* Presence of _CID indicates 'compatible_ids' */
783 status = acpi_get_handle(device->handle, "_CID", &temp);
784 if (ACPI_SUCCESS(status))
785 device->flags.compatible_ids = 1;
786
787 /* Presence of _RMV indicates 'removable' */
788 status = acpi_get_handle(device->handle, "_RMV", &temp);
789 if (ACPI_SUCCESS(status))
790 device->flags.removable = 1;
791
792 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
793 status = acpi_get_handle(device->handle, "_EJD", &temp);
794 if (ACPI_SUCCESS(status))
795 device->flags.ejectable = 1;
796 else {
797 status = acpi_get_handle(device->handle, "_EJ0", &temp);
798 if (ACPI_SUCCESS(status))
799 device->flags.ejectable = 1;
800 }
801
802 /* Presence of _LCK indicates 'lockable' */
803 status = acpi_get_handle(device->handle, "_LCK", &temp);
804 if (ACPI_SUCCESS(status))
805 device->flags.lockable = 1;
806
807 /* Presence of _PS0|_PR0 indicates 'power manageable' */
808 status = acpi_get_handle(device->handle, "_PS0", &temp);
809 if (ACPI_FAILURE(status))
810 status = acpi_get_handle(device->handle, "_PR0", &temp);
811 if (ACPI_SUCCESS(status))
812 device->flags.power_manageable = 1;
813
814 /* Presence of _PRW indicates wake capable */
815 status = acpi_get_handle(device->handle, "_PRW", &temp);
816 if (ACPI_SUCCESS(status))
817 device->flags.wake_capable = 1;
818
819 /* TBD: Peformance management */
820
Patrick Mocheld550d982006-06-27 00:41:40 -0400821 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
Len Brown4be44fc2005-08-05 00:44:28 -0400824static void acpi_device_get_busid(struct acpi_device *device,
825 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Len Brown4be44fc2005-08-05 00:44:28 -0400827 char bus_id[5] = { '?', 0 };
828 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
829 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /*
832 * Bus ID
833 * ------
834 * The device's Bus ID is simply the object name.
835 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
836 */
837 switch (type) {
838 case ACPI_BUS_TYPE_SYSTEM:
839 strcpy(device->pnp.bus_id, "ACPI");
840 break;
841 case ACPI_BUS_TYPE_POWER_BUTTON:
842 strcpy(device->pnp.bus_id, "PWRF");
843 break;
844 case ACPI_BUS_TYPE_SLEEP_BUTTON:
845 strcpy(device->pnp.bus_id, "SLPF");
846 break;
847 default:
848 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
849 /* Clean up trailing underscores (if any) */
850 for (i = 3; i > 1; i--) {
851 if (bus_id[i] == '_')
852 bus_id[i] = '\0';
853 else
854 break;
855 }
856 strcpy(device->pnp.bus_id, bus_id);
857 break;
858 }
859}
860
Len Brown4be44fc2005-08-05 00:44:28 -0400861static void acpi_device_set_id(struct acpi_device *device,
862 struct acpi_device *parent, acpi_handle handle,
863 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
Len Brown4be44fc2005-08-05 00:44:28 -0400865 struct acpi_device_info *info;
866 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
867 char *hid = NULL;
868 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400870 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 switch (type) {
873 case ACPI_BUS_TYPE_DEVICE:
874 status = acpi_get_object_info(handle, &buffer);
875 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400876 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return;
878 }
879
880 info = buffer.pointer;
881 if (info->valid & ACPI_VALID_HID)
882 hid = info->hardware_id.value;
883 if (info->valid & ACPI_VALID_UID)
884 uid = info->unique_id.value;
885 if (info->valid & ACPI_VALID_CID)
886 cid_list = &info->compatibility_id;
887 if (info->valid & ACPI_VALID_ADR) {
888 device->pnp.bus_address = info->address;
889 device->flags.bus_address = 1;
890 }
891 break;
892 case ACPI_BUS_TYPE_POWER:
893 hid = ACPI_POWER_HID;
894 break;
895 case ACPI_BUS_TYPE_PROCESSOR:
896 hid = ACPI_PROCESSOR_HID;
897 break;
898 case ACPI_BUS_TYPE_SYSTEM:
899 hid = ACPI_SYSTEM_HID;
900 break;
901 case ACPI_BUS_TYPE_THERMAL:
902 hid = ACPI_THERMAL_HID;
903 break;
904 case ACPI_BUS_TYPE_POWER_BUTTON:
905 hid = ACPI_BUTTON_HID_POWERF;
906 break;
907 case ACPI_BUS_TYPE_SLEEP_BUTTON:
908 hid = ACPI_BUTTON_HID_SLEEPF;
909 break;
910 }
911
912 /*
913 * \_SB
914 * ----
915 * Fix for the system root bus device -- the only root-level device.
916 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500917 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 hid = ACPI_BUS_HID;
919 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
920 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
921 }
922
923 if (hid) {
924 strcpy(device->pnp.hardware_id, hid);
925 device->flags.hardware_id = 1;
926 }
927 if (uid) {
928 strcpy(device->pnp.unique_id, uid);
929 device->flags.unique_id = 1;
930 }
931 if (cid_list) {
932 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
933 if (device->pnp.cid_list)
934 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
935 else
936 printk(KERN_ERR "Memory allocation error\n");
937 }
938
Len Brown02438d82006-06-30 03:19:10 -0400939 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Len Brown4be44fc2005-08-05 00:44:28 -0400942static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
944 acpi_status status = AE_OK;
945 int result = 0;
946 /*
947 * Context
948 * -------
949 * Attach this 'struct acpi_device' to the ACPI object. This makes
950 * resolutions from handle->device very efficient. Note that we need
951 * to be careful with fixed-feature devices as they all attach to the
952 * root object.
953 */
Len Brown4be44fc2005-08-05 00:44:28 -0400954 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
956 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400957 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 if (ACPI_FAILURE(status)) {
960 printk("Error attaching device data\n");
961 result = -ENODEV;
962 }
963 }
964 return result;
965}
966
Len Brown4be44fc2005-08-05 00:44:28 -0400967static void acpi_device_get_debug_info(struct acpi_device *device,
968 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400971 char *type_string = NULL;
972 char name[80] = { '?', '\0' };
973 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 switch (type) {
976 case ACPI_BUS_TYPE_DEVICE:
977 type_string = "Device";
978 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
979 break;
980 case ACPI_BUS_TYPE_POWER:
981 type_string = "Power Resource";
982 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
983 break;
984 case ACPI_BUS_TYPE_PROCESSOR:
985 type_string = "Processor";
986 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
987 break;
988 case ACPI_BUS_TYPE_SYSTEM:
989 type_string = "System";
990 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
991 break;
992 case ACPI_BUS_TYPE_THERMAL:
993 type_string = "Thermal Zone";
994 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
995 break;
996 case ACPI_BUS_TYPE_POWER_BUTTON:
997 type_string = "Power Button";
998 sprintf(name, "PWRB");
999 break;
1000 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1001 type_string = "Sleep Button";
1002 sprintf(name, "SLPB");
1003 break;
1004 }
1005
1006 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -04001007#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Len Brown4be44fc2005-08-05 00:44:28 -04001010static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Len Brown4be44fc2005-08-05 00:44:28 -04001012 int result = 0;
1013 struct acpi_driver *driver;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001017 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 driver = dev->driver;
1020
1021 if ((driver) && (driver->ops.remove)) {
1022
1023 if (driver->ops.stop) {
1024 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
1025 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001026 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
1029 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
1030 if (result) {
Patrick Mocheld550d982006-06-27 00:41:40 -04001031 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
1034 atomic_dec(&dev->driver->references);
1035 dev->driver = NULL;
1036 acpi_driver_data(dev) = NULL;
1037 }
1038
1039 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (dev->flags.bus_address) {
1043 if ((dev->parent) && (dev->parent->ops.unbind))
1044 dev->parent->ops.unbind(dev);
1045 }
Len Brown4be44fc2005-08-05 00:44:28 -04001046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1048
Patrick Mocheld550d982006-06-27 00:41:40 -04001049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
Rajesh Shah3fb02732005-04-28 00:25:52 -07001052static int
Len Brown4be44fc2005-08-05 00:44:28 -04001053acpi_add_single_object(struct acpi_device **child,
1054 struct acpi_device *parent, acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Len Brown4be44fc2005-08-05 00:44:28 -04001056 int result = 0;
1057 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
1060 if (!child)
Patrick Mocheld550d982006-06-27 00:41:40 -04001061 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
1064 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001065 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001066 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 memset(device, 0, sizeof(struct acpi_device));
1069
1070 device->handle = handle;
1071 device->parent = parent;
1072
Len Brown4be44fc2005-08-05 00:44:28 -04001073 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
1075 /*
1076 * Flags
1077 * -----
1078 * Get prior to calling acpi_bus_get_status() so we know whether
1079 * or not _STA is present. Note that we only look for object
1080 * handles -- cannot evaluate objects until we know the device is
1081 * present and properly initialized.
1082 */
1083 result = acpi_bus_get_flags(device);
1084 if (result)
1085 goto end;
1086
1087 /*
1088 * Status
1089 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001090 * See if the device is present. We always assume that non-Device
1091 * and non-Processor objects (e.g. thermal zones, power resources,
1092 * etc.) are present, functioning, etc. (at least when parent object
1093 * is present). Note that _STA has a different meaning for some
1094 * objects (e.g. power resources) so we need to be careful how we use
1095 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 */
1097 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001098 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 case ACPI_BUS_TYPE_DEVICE:
1100 result = acpi_bus_get_status(device);
1101 if (ACPI_FAILURE(result) || !device->status.present) {
1102 result = -ENOENT;
1103 goto end;
1104 }
1105 break;
1106 default:
1107 STRUCT_TO_INT(device->status) = 0x0F;
1108 break;
1109 }
1110
1111 /*
1112 * Initialize Device
1113 * -----------------
1114 * TBD: Synch with Core's enumeration/initialization process.
1115 */
1116
1117 /*
1118 * Hardware ID, Unique ID, & Bus Address
1119 * -------------------------------------
1120 */
Len Brown4be44fc2005-08-05 00:44:28 -04001121 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
1124 * Power Management
1125 * ----------------
1126 */
1127 if (device->flags.power_manageable) {
1128 result = acpi_bus_get_power_flags(device);
1129 if (result)
1130 goto end;
1131 }
1132
Len Brown4be44fc2005-08-05 00:44:28 -04001133 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 * Wakeup device management
1135 *-----------------------
1136 */
1137 if (device->flags.wake_capable) {
1138 result = acpi_bus_get_wakeup_device_flags(device);
1139 if (result)
1140 goto end;
1141 }
1142
1143 /*
1144 * Performance Management
1145 * ----------------------
1146 */
1147 if (device->flags.performance_manageable) {
1148 result = acpi_bus_get_perf_flags(device);
1149 if (result)
1150 goto end;
1151 }
1152
Len Brown4be44fc2005-08-05 00:44:28 -04001153 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 goto end;
1155
Len Brown4be44fc2005-08-05 00:44:28 -04001156 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Len Brown4be44fc2005-08-05 00:44:28 -04001158 acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 /*
1161 * Bind _ADR-Based Devices
1162 * -----------------------
1163 * If there's a a bus address (_ADR) then we utilize the parent's
1164 * 'bind' function (if exists) to bind the ACPI- and natively-
1165 * enumerated device representations.
1166 */
1167 if (device->flags.bus_address) {
1168 if (device->parent && device->parent->ops.bind)
1169 device->parent->ops.bind(device);
1170 }
1171
1172 /*
1173 * Locate & Attach Driver
1174 * ----------------------
1175 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1176 * to see if there's a driver installed for this kind of device. Note
1177 * that drivers can install before or after a device is enumerated.
1178 *
1179 * TBD: Assumes LDM provides driver hot-plug capability.
1180 */
Thomas Renningerbd7ce5b2005-10-03 10:39:00 -07001181 acpi_bus_find_driver(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Len Brown4be44fc2005-08-05 00:44:28 -04001183 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 if (!result)
1185 *child = device;
1186 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001187 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 kfree(device);
1189 }
1190
Patrick Mocheld550d982006-06-27 00:41:40 -04001191 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Len Brown4be44fc2005-08-05 00:44:28 -04001194static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Len Brown4be44fc2005-08-05 00:44:28 -04001196 acpi_status status = AE_OK;
1197 struct acpi_device *parent = NULL;
1198 struct acpi_device *child = NULL;
1199 acpi_handle phandle = NULL;
1200 acpi_handle chandle = NULL;
1201 acpi_object_type type = 0;
1202 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 if (!start)
Patrick Mocheld550d982006-06-27 00:41:40 -04001206 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 parent = start;
1209 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Parse through the ACPI namespace, identify all 'devices', and
1213 * create a new 'struct acpi_device' for each.
1214 */
1215 while ((level > 0) && parent) {
1216
1217 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001218 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /*
1221 * If this scope is exhausted then move our way back up.
1222 */
1223 if (ACPI_FAILURE(status)) {
1224 level--;
1225 chandle = phandle;
1226 acpi_get_parent(phandle, &phandle);
1227 if (parent->parent)
1228 parent = parent->parent;
1229 continue;
1230 }
1231
1232 status = acpi_get_type(chandle, &type);
1233 if (ACPI_FAILURE(status))
1234 continue;
1235
1236 /*
1237 * If this is a scope object then parse it (depth-first).
1238 */
1239 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1240 level++;
1241 phandle = chandle;
1242 chandle = NULL;
1243 continue;
1244 }
1245
1246 /*
1247 * We're only interested in objects that we consider 'devices'.
1248 */
1249 switch (type) {
1250 case ACPI_TYPE_DEVICE:
1251 type = ACPI_BUS_TYPE_DEVICE;
1252 break;
1253 case ACPI_TYPE_PROCESSOR:
1254 type = ACPI_BUS_TYPE_PROCESSOR;
1255 break;
1256 case ACPI_TYPE_THERMAL:
1257 type = ACPI_BUS_TYPE_THERMAL;
1258 break;
1259 case ACPI_TYPE_POWER:
1260 type = ACPI_BUS_TYPE_POWER;
1261 break;
1262 default:
1263 continue;
1264 }
1265
Rajesh Shah3fb02732005-04-28 00:25:52 -07001266 if (ops->acpi_op_add)
1267 status = acpi_add_single_object(&child, parent,
Len Brown4be44fc2005-08-05 00:44:28 -04001268 chandle, type);
1269 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001270 status = acpi_bus_get_device(chandle, &child);
1271
Len Brown4be44fc2005-08-05 00:44:28 -04001272 if (ACPI_FAILURE(status))
1273 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001274
1275 if (ops->acpi_op_start) {
1276 status = acpi_start_single_object(child);
1277 if (ACPI_FAILURE(status))
1278 continue;
1279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 /*
1282 * If the device is present, enabled, and functioning then
1283 * parse its scope (depth-first). Note that we need to
1284 * represent absent devices to facilitate PnP notifications
1285 * -- but only the subtree head (not all of its children,
1286 * which will be enumerated when the parent is inserted).
1287 *
1288 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001289 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
1291 if (child->status.present) {
1292 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1293 NULL, NULL);
1294 if (ACPI_SUCCESS(status)) {
1295 level++;
1296 phandle = chandle;
1297 chandle = NULL;
1298 parent = child;
1299 }
1300 }
1301 }
1302
Patrick Mocheld550d982006-06-27 00:41:40 -04001303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Rajesh Shah3fb02732005-04-28 00:25:52 -07001306int
Len Brown4be44fc2005-08-05 00:44:28 -04001307acpi_bus_add(struct acpi_device **child,
1308 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001309{
1310 int result;
1311 struct acpi_bus_ops ops;
1312
Rajesh Shah3fb02732005-04-28 00:25:52 -07001313
1314 result = acpi_add_single_object(child, parent, handle, type);
1315 if (!result) {
1316 memset(&ops, 0, sizeof(ops));
1317 ops.acpi_op_add = 1;
1318 result = acpi_bus_scan(*child, &ops);
1319 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001320 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001321}
Len Brown4be44fc2005-08-05 00:44:28 -04001322
Rajesh Shah3fb02732005-04-28 00:25:52 -07001323EXPORT_SYMBOL(acpi_bus_add);
1324
Len Brown4be44fc2005-08-05 00:44:28 -04001325int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001326{
1327 int result;
1328 struct acpi_bus_ops ops;
1329
Rajesh Shah3fb02732005-04-28 00:25:52 -07001330
1331 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001332 return -EINVAL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001333
1334 result = acpi_start_single_object(device);
1335 if (!result) {
1336 memset(&ops, 0, sizeof(ops));
1337 ops.acpi_op_start = 1;
1338 result = acpi_bus_scan(device, &ops);
1339 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001340 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001341}
Len Brown4be44fc2005-08-05 00:44:28 -04001342
Rajesh Shah3fb02732005-04-28 00:25:52 -07001343EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Kristen Accardiceaba662006-02-23 17:56:01 -08001345int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Len Brown4be44fc2005-08-05 00:44:28 -04001347 acpi_status status;
1348 struct acpi_device *parent, *child;
1349 acpi_handle phandle, chandle;
1350 acpi_object_type type;
1351 u32 level = 1;
1352 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Len Brown4be44fc2005-08-05 00:44:28 -04001354 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 phandle = start->handle;
1356 child = chandle = NULL;
1357
1358 while ((level > 0) && parent && (!err)) {
1359 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001360 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 /*
1363 * If this scope is exhausted then move our way back up.
1364 */
1365 if (ACPI_FAILURE(status)) {
1366 level--;
1367 chandle = phandle;
1368 acpi_get_parent(phandle, &phandle);
1369 child = parent;
1370 parent = parent->parent;
1371
1372 if (level == 0)
1373 err = acpi_bus_remove(child, rmdevice);
1374 else
1375 err = acpi_bus_remove(child, 1);
1376
1377 continue;
1378 }
1379
1380 status = acpi_get_type(chandle, &type);
1381 if (ACPI_FAILURE(status)) {
1382 continue;
1383 }
1384 /*
1385 * If there is a device corresponding to chandle then
1386 * parse it (depth-first).
1387 */
1388 if (acpi_bus_get_device(chandle, &child) == 0) {
1389 level++;
1390 phandle = chandle;
1391 chandle = NULL;
1392 parent = child;
1393 }
1394 continue;
1395 }
1396 return err;
1397}
Kristen Accardiceaba662006-02-23 17:56:01 -08001398EXPORT_SYMBOL_GPL(acpi_bus_trim);
1399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
Len Brown4be44fc2005-08-05 00:44:28 -04001401static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Len Brown4be44fc2005-08-05 00:44:28 -04001403 int result = 0;
1404 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -04001408 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /*
1411 * Enumerate all fixed-feature devices.
1412 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001413 if (acpi_fadt.pwr_button == 0) {
1414 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001415 NULL,
1416 ACPI_BUS_TYPE_POWER_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001417 if (!result)
1418 result = acpi_start_single_object(device);
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Rajesh Shah3fb02732005-04-28 00:25:52 -07001421 if (acpi_fadt.sleep_button == 0) {
1422 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001423 NULL,
1424 ACPI_BUS_TYPE_SLEEP_BUTTON);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001425 if (!result)
1426 result = acpi_start_single_object(device);
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Patrick Mocheld550d982006-06-27 00:41:40 -04001429 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432static int __init acpi_scan_init(void)
1433{
1434 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001435 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04001441 result = kset_register(&acpi_namespace_kset);
1442 if (result < 0)
1443 printk(KERN_ERR PREFIX "kset_register error: %d\n", result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Patrick Mochel5b327262006-05-10 10:33:00 -04001445 result = bus_register(&acpi_bus_type);
1446 if (result) {
1447 /* We don't want to quit even if we failed to add suspend/resume */
1448 printk(KERN_ERR PREFIX "Could not register bus type\n");
1449 }
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 /*
1452 * Create the root device in the bus's device tree
1453 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001454 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Len Brown4be44fc2005-08-05 00:44:28 -04001455 ACPI_BUS_TYPE_SYSTEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (result)
1457 goto Done;
1458
Rajesh Shah3fb02732005-04-28 00:25:52 -07001459 result = acpi_start_single_object(acpi_root);
Patrick Mochel5b327262006-05-10 10:33:00 -04001460 if (result)
1461 goto Done;
1462
1463 acpi_root->dev.bus = &acpi_bus_type;
1464 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1465 result = device_register(&acpi_root->dev);
1466 if (result) {
1467 /* We don't want to quit even if we failed to add suspend/resume */
1468 printk(KERN_ERR PREFIX "Could not register device\n");
1469 }
Rajesh Shah3fb02732005-04-28 00:25:52 -07001470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /*
1472 * Enumerate devices in the ACPI namespace.
1473 */
1474 result = acpi_bus_scan_fixed(acpi_root);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001475 if (!result) {
1476 memset(&ops, 0, sizeof(ops));
1477 ops.acpi_op_add = 1;
1478 ops.acpi_op_start = 1;
1479 result = acpi_bus_scan(acpi_root, &ops);
1480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
1482 if (result)
1483 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1484
Len Brown4be44fc2005-08-05 00:44:28 -04001485 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -04001486 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
1489subsys_initcall(acpi_scan_init);