blob: 0b5aaf059c9b4b14a85f0fe784499063cabc584c [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>
Alok N Kataria74523c92008-06-13 12:54:24 -04009#include <linux/signal.h>
10#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <acpi/acpi_drivers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Bjorn Helgaase60cc7a2009-03-13 12:08:26 -060014#include "internal.h"
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050017ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040019extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020022#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#define ACPI_BUS_DEVICE_NAME "System Bus"
24
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +000025#define ACPI_IS_ROOT_DEVICE(device) (!(device)->parent)
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2d2006-12-08 17:23:43 +080028static LIST_HEAD(acpi_bus_id_list);
Shaohua Li90905892009-04-07 10:24:29 +080029DEFINE_MUTEX(acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030LIST_HEAD(acpi_wakeup_device_list);
31
Zhang Ruie49bd2d2006-12-08 17:23:43 +080032struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080033 char bus_id[15];
Zhang Ruie49bd2d2006-12-08 17:23:43 +080034 unsigned int instance_no;
35 struct list_head node;
36};
Thomas Renninger29b71a12007-07-23 14:43:51 +020037
38/*
39 * Creates hid/cid(s) string needed for modalias and uevent
40 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
41 * char *modalias: "acpi:IBM0001:ACPI0001"
42*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020043static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
44 int size)
45{
Thomas Renninger29b71a12007-07-23 14:43:51 +020046 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080047 int count;
Thomas Renninger29b71a12007-07-23 14:43:51 +020048
Zhang Rui5c9fcb52008-03-20 16:40:32 +080049 if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +020050 return -ENODEV;
51
Zhang Rui5c9fcb52008-03-20 16:40:32 +080052 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020053 size -= len;
54
Zhang Rui5c9fcb52008-03-20 16:40:32 +080055 if (acpi_dev->flags.hardware_id) {
56 count = snprintf(&modalias[len], size, "%s:",
57 acpi_dev->pnp.hardware_id);
58 if (count < 0 || count >= size)
59 return -EINVAL;
60 len += count;
61 size -= count;
62 }
63
Thomas Renninger29b71a12007-07-23 14:43:51 +020064 if (acpi_dev->flags.compatible_ids) {
Bob Moore15b8dd52009-06-29 13:39:29 +080065 struct acpica_device_id_list *cid_list;
Thomas Renninger29b71a12007-07-23 14:43:51 +020066 int i;
Thomas Renninger29b71a12007-07-23 14:43:51 +020067
68 cid_list = acpi_dev->pnp.cid_list;
69 for (i = 0; i < cid_list->count; i++) {
70 count = snprintf(&modalias[len], size, "%s:",
Bob Moore15b8dd52009-06-29 13:39:29 +080071 cid_list->ids[i].string);
Thomas Renninger29b71a12007-07-23 14:43:51 +020072 if (count < 0 || count >= size) {
Len Brown87ecd5c2008-01-01 14:00:00 -050073 printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
Thomas Renninger29b71a12007-07-23 14:43:51 +020074 acpi_dev->pnp.device_name, i);
75 break;
76 }
77 len += count;
78 size -= count;
79 }
80 }
81
82 modalias[len] = '\0';
83 return len;
84}
85
86static ssize_t
87acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
88 struct acpi_device *acpi_dev = to_acpi_device(dev);
89 int len;
90
91 /* Device has no HID and no CID or string is >1024 */
92 len = create_modalias(acpi_dev, buf, 1024);
93 if (len <= 0)
94 return 0;
95 buf[len++] = '\n';
96 return len;
97}
98static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
99
Zhang Ruic8d72a52009-06-22 11:31:16 +0800100static void acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Zhang Rui26d46862008-04-29 02:35:48 -0400102 struct acpi_device *device;
103 acpi_handle handle = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct acpi_object_list arg_list;
105 union acpi_object arg;
106 acpi_status status = AE_OK;
107
Zhang Rui26d46862008-04-29 02:35:48 -0400108 if (acpi_bus_get_device(handle, &device))
Zhang Ruic8d72a52009-06-22 11:31:16 +0800109 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Zhang Rui26d46862008-04-29 02:35:48 -0400111 if (!device)
Zhang Ruic8d72a52009-06-22 11:31:16 +0800112 return;
Zhang Rui26d46862008-04-29 02:35:48 -0400113
114 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Kay Sievers07944692008-10-30 01:18:59 +0100115 "Hot-removing device %s...\n", dev_name(&device->dev)));
Zhang Rui26d46862008-04-29 02:35:48 -0400116
117 if (acpi_bus_trim(device, 1)) {
Lin Ming55ac9a02008-09-28 14:51:56 +0800118 printk(KERN_ERR PREFIX
119 "Removing device failed\n");
Zhang Ruic8d72a52009-06-22 11:31:16 +0800120 return;
Zhang Rui26d46862008-04-29 02:35:48 -0400121 }
122
123 /* power off device */
124 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
125 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
Lin Ming55ac9a02008-09-28 14:51:56 +0800126 printk(KERN_WARNING PREFIX
127 "Power-off device failed\n");
Zhang Rui26d46862008-04-29 02:35:48 -0400128
129 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 arg_list.count = 1;
131 arg_list.pointer = &arg;
132 arg.type = ACPI_TYPE_INTEGER;
133 arg.integer.value = 0;
134 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
135 }
136
137 arg_list.count = 1;
138 arg_list.pointer = &arg;
139 arg.type = ACPI_TYPE_INTEGER;
140 arg.integer.value = 1;
141
142 /*
143 * TBD: _EJD support.
144 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Zhang Rui26d46862008-04-29 02:35:48 -0400146 if (ACPI_FAILURE(status))
Zhang Ruic8d72a52009-06-22 11:31:16 +0800147 printk(KERN_WARNING PREFIX
148 "Eject device failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Zhang Ruic8d72a52009-06-22 11:31:16 +0800150 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800154acpi_eject_store(struct device *d, struct device_attribute *attr,
155 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Len Brown4be44fc2005-08-05 00:44:28 -0400157 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400158 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400159 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800160 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if ((!count) || (buf[0] != '1')) {
163 return -EINVAL;
164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800166 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 ret = -ENODEV;
168 goto err;
169 }
170#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800171 status = acpi_get_type(acpi_device->handle, &type);
172 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 ret = -ENODEV;
174 goto err;
175 }
176
Zhang Ruic8d72a52009-06-22 11:31:16 +0800177 acpi_os_hotplug_execute(acpi_bus_hot_remove_device, acpi_device->handle);
Alok N Kataria74523c92008-06-13 12:54:24 -0400178err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800182static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800184static ssize_t
185acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
186 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800188 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
189}
190static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
191
192static ssize_t
193acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
194 struct acpi_device *acpi_dev = to_acpi_device(dev);
195 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
196 int result;
197
198 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600199 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800200 goto end;
201
202 result = sprintf(buf, "%s\n", (char*)path.pointer);
203 kfree(path.pointer);
Alex Chiang0c526d92009-05-14 08:31:37 -0600204end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800205 return result;
206}
207static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
208
209static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800211 acpi_status status;
212 acpi_handle temp;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800213 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800215 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800216 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800217 */
Alex Chiang0c526d92009-05-14 08:31:37 -0600218 if (dev->handle) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800219 result = device_create_file(&dev->dev, &dev_attr_path);
Alex Chiang0c526d92009-05-14 08:31:37 -0600220 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800221 goto end;
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Alex Chiang0c526d92009-05-14 08:31:37 -0600224 if (dev->flags.hardware_id) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800225 result = device_create_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600226 if (result)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800227 goto end;
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Alex Chiang0c526d92009-05-14 08:31:37 -0600230 if (dev->flags.hardware_id || dev->flags.compatible_ids) {
Thomas Renninger29b71a12007-07-23 14:43:51 +0200231 result = device_create_file(&dev->dev, &dev_attr_modalias);
Alex Chiang0c526d92009-05-14 08:31:37 -0600232 if (result)
Thomas Renninger29b71a12007-07-23 14:43:51 +0200233 goto end;
234 }
235
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800236 /*
237 * If device has _EJ0, 'eject' file is created that is used to trigger
238 * hot-removal function from userland.
239 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800240 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
241 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800242 result = device_create_file(&dev->dev, &dev_attr_eject);
Alex Chiang0c526d92009-05-14 08:31:37 -0600243end:
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800244 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800245}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800247static void acpi_device_remove_files(struct acpi_device *dev)
248{
249 acpi_status status;
250 acpi_handle temp;
251
252 /*
253 * If device has _EJ0, 'eject' file is created that is used to trigger
254 * hot-removal function from userland.
255 */
256 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
257 if (ACPI_SUCCESS(status))
258 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800259
Thomas Renninger29b71a12007-07-23 14:43:51 +0200260 if (dev->flags.hardware_id || dev->flags.compatible_ids)
261 device_remove_file(&dev->dev, &dev_attr_modalias);
262
Alex Chiang0c526d92009-05-14 08:31:37 -0600263 if (dev->flags.hardware_id)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800264 device_remove_file(&dev->dev, &dev_attr_hid);
Alex Chiang0c526d92009-05-14 08:31:37 -0600265 if (dev->handle)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800266 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800269 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200271
272int acpi_match_device_ids(struct acpi_device *device,
273 const struct acpi_device_id *ids)
274{
275 const struct acpi_device_id *id;
276
Zhao Yakui39a0ad82008-08-11 13:40:22 +0800277 /*
278 * If the device is not present, it is unnecessary to load device
279 * driver for it.
280 */
281 if (!device->status.present)
282 return -ENODEV;
283
Thomas Renninger29b71a12007-07-23 14:43:51 +0200284 if (device->flags.hardware_id) {
285 for (id = ids; id->id[0]; id++) {
286 if (!strcmp((char*)id->id, device->pnp.hardware_id))
287 return 0;
288 }
289 }
290
291 if (device->flags.compatible_ids) {
Bob Moore15b8dd52009-06-29 13:39:29 +0800292 struct acpica_device_id_list *cid_list = device->pnp.cid_list;
Thomas Renninger29b71a12007-07-23 14:43:51 +0200293 int i;
294
295 for (id = ids; id->id[0]; id++) {
296 /* compare multiple _CID entries against driver ids */
297 for (i = 0; i < cid_list->count; i++) {
298 if (!strcmp((char*)id->id,
Bob Moore15b8dd52009-06-29 13:39:29 +0800299 cid_list->ids[i].string))
Thomas Renninger29b71a12007-07-23 14:43:51 +0200300 return 0;
301 }
302 }
303 }
304
305 return -ENOENT;
306}
307EXPORT_SYMBOL(acpi_match_device_ids);
308
Patrick Mochel1890a972006-12-07 20:56:31 +0800309static void acpi_device_release(struct device *dev)
310{
311 struct acpi_device *acpi_dev = to_acpi_device(dev);
312
313 kfree(acpi_dev->pnp.cid_list);
Hugh Dickins718fb0d2009-08-06 23:18:12 +0000314 if (acpi_dev->flags.hardware_id)
315 kfree(acpi_dev->pnp.hardware_id);
316 if (acpi_dev->flags.unique_id)
317 kfree(acpi_dev->pnp.unique_id);
Patrick Mochel1890a972006-12-07 20:56:31 +0800318 kfree(acpi_dev);
319}
320
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800321static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800322{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800323 struct acpi_device *acpi_dev = to_acpi_device(dev);
324 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800325
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800326 if (acpi_drv && acpi_drv->ops.suspend)
327 return acpi_drv->ops.suspend(acpi_dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329}
330
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800331static int acpi_device_resume(struct device *dev)
332{
333 struct acpi_device *acpi_dev = to_acpi_device(dev);
334 struct acpi_driver *acpi_drv = acpi_dev->driver;
335
336 if (acpi_drv && acpi_drv->ops.resume)
337 return acpi_drv->ops.resume(acpi_dev);
338 return 0;
339}
340
341static int acpi_bus_match(struct device *dev, struct device_driver *drv)
342{
343 struct acpi_device *acpi_dev = to_acpi_device(dev);
344 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
345
Thomas Renninger29b71a12007-07-23 14:43:51 +0200346 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800347}
348
Kay Sievers7eff2e72007-08-14 15:15:12 +0200349static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800350{
351 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200352 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800353
Kay Sievers7eff2e72007-08-14 15:15:12 +0200354 if (add_uevent_var(env, "MODALIAS="))
355 return -ENOMEM;
356 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
357 sizeof(env->buf) - env->buflen);
358 if (len >= (sizeof(env->buf) - env->buflen))
359 return -ENOMEM;
360 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
362}
363
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000364static void acpi_device_notify(acpi_handle handle, u32 event, void *data)
365{
366 struct acpi_device *device = data;
367
368 device->driver->ops.notify(device, event);
369}
370
371static acpi_status acpi_device_notify_fixed(void *data)
372{
373 struct acpi_device *device = data;
374
Bjorn Helgaas53de5352009-08-31 22:32:20 +0000375 /* Fixed hardware devices have no handles */
376 acpi_device_notify(NULL, ACPI_FIXED_HARDWARE_EVENT, device);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000377 return AE_OK;
378}
379
380static int acpi_device_install_notify_handler(struct acpi_device *device)
381{
382 acpi_status status;
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000383
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000384 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000385 status =
386 acpi_install_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
387 acpi_device_notify_fixed,
388 device);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000389 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000390 status =
391 acpi_install_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
392 acpi_device_notify_fixed,
393 device);
394 else
395 status = acpi_install_notify_handler(device->handle,
396 ACPI_DEVICE_NOTIFY,
397 acpi_device_notify,
398 device);
399
400 if (ACPI_FAILURE(status))
401 return -EINVAL;
402 return 0;
403}
404
405static void acpi_device_remove_notify_handler(struct acpi_device *device)
406{
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000407 if (device->device_type == ACPI_BUS_TYPE_POWER_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000408 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON,
409 acpi_device_notify_fixed);
Bjorn Helgaasccba2a32009-09-21 19:29:15 +0000410 else if (device->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON)
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000411 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON,
412 acpi_device_notify_fixed);
413 else
414 acpi_remove_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
415 acpi_device_notify);
416}
417
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800418static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
419static int acpi_start_single_object(struct acpi_device *);
420static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800421{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800422 struct acpi_device *acpi_dev = to_acpi_device(dev);
423 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
424 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800426 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
427 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800428 if (acpi_dev->bus_ops.acpi_op_start)
429 acpi_start_single_object(acpi_dev);
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000430
431 if (acpi_drv->ops.notify) {
432 ret = acpi_device_install_notify_handler(acpi_dev);
433 if (ret) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000434 if (acpi_drv->ops.remove)
435 acpi_drv->ops.remove(acpi_dev,
436 acpi_dev->removal_type);
437 return ret;
438 }
439 }
440
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800441 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
442 "Found driver [%s] for device [%s]\n",
443 acpi_drv->name, acpi_dev->pnp.bus_id));
444 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800445 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800446 return ret;
447}
448
449static int acpi_device_remove(struct device * dev)
450{
451 struct acpi_device *acpi_dev = to_acpi_device(dev);
452 struct acpi_driver *acpi_drv = acpi_dev->driver;
453
454 if (acpi_drv) {
Bjorn Helgaas46ec8592009-03-30 17:48:13 +0000455 if (acpi_drv->ops.notify)
456 acpi_device_remove_notify_handler(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800457 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800458 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800459 }
460 acpi_dev->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700461 acpi_dev->driver_data = NULL;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800462
463 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800464 return 0;
465}
466
David Brownell55955aa2007-05-08 00:28:35 -0700467struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800468 .name = "acpi",
469 .suspend = acpi_device_suspend,
470 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800471 .match = acpi_bus_match,
472 .probe = acpi_device_probe,
473 .remove = acpi_device_remove,
474 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475};
476
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000477static int acpi_device_register(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800479 int result;
480 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
481 int found = 0;
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 /*
484 * Linkage
485 * -------
486 * Link this device to its parent and siblings.
487 */
488 INIT_LIST_HEAD(&device->children);
489 INIT_LIST_HEAD(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 INIT_LIST_HEAD(&device->wakeup_list);
491
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800492 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
493 if (!new_bus_id) {
494 printk(KERN_ERR PREFIX "Memory allocation error\n");
495 return -ENOMEM;
496 }
497
Shaohua Li90905892009-04-07 10:24:29 +0800498 mutex_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800499 /*
500 * Find suitable bus_id and instance number in acpi_bus_id_list
501 * If failed, create one and link it into acpi_bus_id_list
502 */
503 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Zhang Ruibb095852007-01-04 15:03:18 +0800504 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800505 acpi_device_bus_id->instance_no ++;
506 found = 1;
507 kfree(new_bus_id);
508 break;
509 }
510 }
Alex Chiang0c526d92009-05-14 08:31:37 -0600511 if (!found) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800512 acpi_device_bus_id = new_bus_id;
Zhang Ruibb095852007-01-04 15:03:18 +0800513 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800514 acpi_device_bus_id->instance_no = 0;
515 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
516 }
Kay Sievers07944692008-10-30 01:18:59 +0100517 dev_set_name(&device->dev, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800518
Len Brown33b57152008-12-15 22:09:26 -0500519 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 list_add_tail(&device->node, &device->parent->children);
Len Brown33b57152008-12-15 22:09:26 -0500521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (device->wakeup.flags.valid)
523 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
Shaohua Li90905892009-04-07 10:24:29 +0800524 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Patrick Mochel1890a972006-12-07 20:56:31 +0800526 if (device->parent)
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000527 device->dev.parent = &device->parent->dev;
Patrick Mochel1890a972006-12-07 20:56:31 +0800528 device->dev.bus = &acpi_bus_type;
Patrick Mochel1890a972006-12-07 20:56:31 +0800529 device->dev.release = &acpi_device_release;
Alex Chiang8b12b922009-05-14 08:31:32 -0600530 result = device_register(&device->dev);
Alex Chiang0c526d92009-05-14 08:31:37 -0600531 if (result) {
Alex Chiang8b12b922009-05-14 08:31:32 -0600532 dev_err(&device->dev, "Error registering device\n");
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800533 goto end;
534 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800535
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800536 result = acpi_device_setup_files(device);
Alex Chiang0c526d92009-05-14 08:31:37 -0600537 if (result)
Kay Sievers07944692008-10-30 01:18:59 +0100538 printk(KERN_ERR PREFIX "Error creating sysfs interface for device %s\n",
539 dev_name(&device->dev));
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800540
Li Shaohua96333572006-12-07 20:56:46 +0800541 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800542 return 0;
Alex Chiang0c526d92009-05-14 08:31:37 -0600543end:
Shaohua Li90905892009-04-07 10:24:29 +0800544 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500545 if (device->parent)
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800546 list_del(&device->node);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800547 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800548 mutex_unlock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800549 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552static void acpi_device_unregister(struct acpi_device *device, int type)
553{
Shaohua Li90905892009-04-07 10:24:29 +0800554 mutex_lock(&acpi_device_lock);
Len Brown33b57152008-12-15 22:09:26 -0500555 if (device->parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 list_del(&device->node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 list_del(&device->wakeup_list);
Shaohua Li90905892009-04-07 10:24:29 +0800559 mutex_unlock(&acpi_device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800562
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800563 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800564 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Zhang Rui9e89dde2006-12-07 20:56:16 +0800567/* --------------------------------------------------------------------------
568 Driver Management
569 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500571 * acpi_bus_driver_init - add a device to a driver
572 * @device: the device to add and initialize
573 * @driver: driver for the device
574 *
Alex Chiang0c526d92009-05-14 08:31:37 -0600575 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800576 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
578static int
Len Brown4be44fc2005-08-05 00:44:28 -0400579acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
Len Brown4be44fc2005-08-05 00:44:28 -0400581 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400584 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400587 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 result = driver->ops.add(device);
590 if (result) {
591 device->driver = NULL;
Pavel Machekdb89b4f2008-09-22 14:37:34 -0700592 device->driver_data = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400593 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596 device->driver = driver;
597
598 /*
599 * TBD - Configuration Management: Assign resources to device based
600 * upon possible configuration and currently allocated resources.
601 */
602
Len Brown4be44fc2005-08-05 00:44:28 -0400603 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
604 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400605 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700606}
607
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400608static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700609{
610 int result = 0;
611 struct acpi_driver *driver;
612
Rajesh Shah3fb02732005-04-28 00:25:52 -0700613
614 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400615 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 if (driver->ops.start) {
618 result = driver->ops.start(device);
619 if (result && driver->ops.remove)
620 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Patrick Mocheld550d982006-06-27 00:41:40 -0400623 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500627 * acpi_bus_register_driver - register a driver with the ACPI bus
628 * @driver: driver being registered
629 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500631 * devices that match the driver's criteria and binds. Returns zero for
632 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 */
Len Brown4be44fc2005-08-05 00:44:28 -0400634int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Patrick Mochel1890a972006-12-07 20:56:31 +0800636 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400639 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800640 driver->drv.name = driver->name;
641 driver->drv.bus = &acpi_bus_type;
642 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Patrick Mochel1890a972006-12-07 20:56:31 +0800644 ret = driver_register(&driver->drv);
645 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Len Brown4be44fc2005-08-05 00:44:28 -0400648EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500651 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
652 * @driver: driver to unregister
653 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 * Unregisters a driver with the ACPI bus. Searches the namespace for all
655 * devices that match the driver's criteria and unbinds.
656 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400657void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Patrick Mochel1890a972006-12-07 20:56:31 +0800659 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
Len Brown4be44fc2005-08-05 00:44:28 -0400661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662EXPORT_SYMBOL(acpi_bus_unregister_driver);
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664/* --------------------------------------------------------------------------
665 Device Enumeration
666 -------------------------------------------------------------------------- */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +0000667static struct acpi_device *acpi_bus_get_parent(acpi_handle handle)
668{
669 acpi_status status;
670 int ret;
671 struct acpi_device *device;
672
673 /*
674 * Fixed hardware devices do not appear in the namespace and do not
675 * have handles, but we fabricate acpi_devices for them, so we have
676 * to deal with them specially.
677 */
678 if (handle == NULL)
679 return acpi_root;
680
681 do {
682 status = acpi_get_parent(handle, &handle);
683 if (status == AE_NULL_ENTRY)
684 return NULL;
685 if (ACPI_FAILURE(status))
686 return acpi_root;
687
688 ret = acpi_bus_get_device(handle, &device);
689 if (ret == 0)
690 return device;
691 } while (1);
692}
693
Len Brownc8f7a622006-07-09 17:22:28 -0400694acpi_status
695acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
696{
697 acpi_status status;
698 acpi_handle tmp;
699 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
700 union acpi_object *obj;
701
702 status = acpi_get_handle(handle, "_EJD", &tmp);
703 if (ACPI_FAILURE(status))
704 return status;
705
706 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
707 if (ACPI_SUCCESS(status)) {
708 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100709 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
710 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400711 kfree(buffer.pointer);
712 }
713 return status;
714}
715EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
716
Bob Moore8e4319c2009-06-29 13:43:27 +0800717void acpi_bus_data_handler(acpi_handle handle, void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
719
720 /* TBD */
721
722 return;
723}
724
Zhang Rui9e89dde2006-12-07 20:56:16 +0800725static int acpi_bus_get_perf_flags(struct acpi_device *device)
726{
727 device->performance.state = ACPI_STATE_UNKNOWN;
728 return 0;
729}
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731static acpi_status
732acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
733 union acpi_object *package)
734{
735 int i = 0;
736 union acpi_object *element = NULL;
737
738 if (!device || !package || (package->package.count < 2))
739 return AE_BAD_PARAMETER;
740
741 element = &(package->package.elements[0]);
742 if (!element)
743 return AE_BAD_PARAMETER;
744 if (element->type == ACPI_TYPE_PACKAGE) {
745 if ((element->package.count < 2) ||
746 (element->package.elements[0].type !=
747 ACPI_TYPE_LOCAL_REFERENCE)
748 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
749 return AE_BAD_DATA;
750 device->wakeup.gpe_device =
751 element->package.elements[0].reference.handle;
752 device->wakeup.gpe_number =
753 (u32) element->package.elements[1].integer.value;
754 } else if (element->type == ACPI_TYPE_INTEGER) {
755 device->wakeup.gpe_number = element->integer.value;
756 } else
757 return AE_BAD_DATA;
758
759 element = &(package->package.elements[1]);
760 if (element->type != ACPI_TYPE_INTEGER) {
761 return AE_BAD_DATA;
762 }
763 device->wakeup.sleep_state = element->integer.value;
764
765 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
766 return AE_NO_MEMORY;
767 }
768 device->wakeup.resources.count = package->package.count - 2;
769 for (i = 0; i < device->wakeup.resources.count; i++) {
770 element = &(package->package.elements[i + 2]);
Bob Moorecd0b2242008-04-10 19:06:43 +0400771 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 device->wakeup.resources.handles[i] = element->reference.handle;
775 }
776
777 return AE_OK;
778}
779
780static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
781{
782 acpi_status status = 0;
783 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
784 union acpi_object *package = NULL;
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200785 int psw_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Thomas Renninger29b71a12007-07-23 14:43:51 +0200787 struct acpi_device_id button_device_ids[] = {
788 {"PNP0C0D", 0},
789 {"PNP0C0C", 0},
790 {"PNP0C0E", 0},
791 {"", 0},
792 };
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /* _PRW */
795 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
796 if (ACPI_FAILURE(status)) {
797 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
798 goto end;
799 }
800
801 package = (union acpi_object *)buffer.pointer;
802 status = acpi_bus_extract_wakeup_device_power_package(device, package);
803 if (ACPI_FAILURE(status)) {
804 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
805 goto end;
806 }
807
808 kfree(buffer.pointer);
809
810 device->wakeup.flags.valid = 1;
Rafael J. Wysocki9b83ccd2009-09-08 23:15:31 +0200811 device->wakeup.prepare_count = 0;
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800812 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
813 * system for the ACPI device with the _PRW object.
814 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
815 * So it is necessary to call _DSW object first. Only when it is not
816 * present will the _PSW object used.
817 */
Rafael J. Wysocki77e76602008-07-07 03:33:34 +0200818 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
819 if (psw_error)
820 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
821 "error in _DSW or _PSW evaluation\n"));
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 /* Power button, Lid switch always enable wakeup */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200824 if (!acpi_match_device_ids(device, button_device_ids))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 device->wakeup.flags.run_wake = 1;
826
Alex Chiang0c526d92009-05-14 08:31:37 -0600827end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (ACPI_FAILURE(status))
829 device->flags.wake_capable = 0;
830 return 0;
831}
832
Zhang Rui9e89dde2006-12-07 20:56:16 +0800833static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800835 acpi_status status = 0;
836 acpi_handle handle = NULL;
837 u32 i = 0;
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800841 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800843 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800845 device->power.flags.explicit_get = 1;
846 status = acpi_get_handle(device->handle, "_IRC", &handle);
847 if (ACPI_SUCCESS(status))
848 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800851 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800853 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
854 struct acpi_device_power_state *ps = &device->power.states[i];
855 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Zhang Rui9e89dde2006-12-07 20:56:16 +0800857 /* Evaluate "_PRx" to se if power resources are referenced */
858 acpi_evaluate_reference(device->handle, object_name, NULL,
859 &ps->resources);
860 if (ps->resources.count) {
861 device->power.flags.power_resources = 1;
862 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Zhang Rui9e89dde2006-12-07 20:56:16 +0800865 /* Evaluate "_PSx" to see if we can do explicit sets */
866 object_name[2] = 'S';
867 status = acpi_get_handle(device->handle, object_name, &handle);
868 if (ACPI_SUCCESS(status)) {
869 ps->flags.explicit_set = 1;
870 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800872
873 /* State is valid if we have some power control */
874 if (ps->resources.count || ps->flags.explicit_set)
875 ps->flags.valid = 1;
876
877 ps->power = -1; /* Unknown - driver assigned */
878 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Zhang Rui9e89dde2006-12-07 20:56:16 +0800881 /* Set defaults for D0 and D3 states (always valid) */
882 device->power.states[ACPI_STATE_D0].flags.valid = 1;
883 device->power.states[ACPI_STATE_D0].power = 100;
884 device->power.states[ACPI_STATE_D3].flags.valid = 1;
885 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Zhang Rui9e89dde2006-12-07 20:56:16 +0800887 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Zhang Rui9e89dde2006-12-07 20:56:16 +0800889 device->power.state = ACPI_STATE_UNKNOWN;
Zhao Yakuia51e1452008-08-11 14:55:05 +0800890 acpi_bus_get_power(device->handle, &(device->power.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 return 0;
893}
894
Len Brown4be44fc2005-08-05 00:44:28 -0400895static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Len Brown4be44fc2005-08-05 00:44:28 -0400897 acpi_status status = AE_OK;
898 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* Presence of _STA indicates 'dynamic_status' */
902 status = acpi_get_handle(device->handle, "_STA", &temp);
903 if (ACPI_SUCCESS(status))
904 device->flags.dynamic_status = 1;
905
906 /* Presence of _CID indicates 'compatible_ids' */
907 status = acpi_get_handle(device->handle, "_CID", &temp);
908 if (ACPI_SUCCESS(status))
909 device->flags.compatible_ids = 1;
910
911 /* Presence of _RMV indicates 'removable' */
912 status = acpi_get_handle(device->handle, "_RMV", &temp);
913 if (ACPI_SUCCESS(status))
914 device->flags.removable = 1;
915
916 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
917 status = acpi_get_handle(device->handle, "_EJD", &temp);
918 if (ACPI_SUCCESS(status))
919 device->flags.ejectable = 1;
920 else {
921 status = acpi_get_handle(device->handle, "_EJ0", &temp);
922 if (ACPI_SUCCESS(status))
923 device->flags.ejectable = 1;
924 }
925
926 /* Presence of _LCK indicates 'lockable' */
927 status = acpi_get_handle(device->handle, "_LCK", &temp);
928 if (ACPI_SUCCESS(status))
929 device->flags.lockable = 1;
930
931 /* Presence of _PS0|_PR0 indicates 'power manageable' */
932 status = acpi_get_handle(device->handle, "_PS0", &temp);
933 if (ACPI_FAILURE(status))
934 status = acpi_get_handle(device->handle, "_PR0", &temp);
935 if (ACPI_SUCCESS(status))
936 device->flags.power_manageable = 1;
937
938 /* Presence of _PRW indicates wake capable */
939 status = acpi_get_handle(device->handle, "_PRW", &temp);
940 if (ACPI_SUCCESS(status))
941 device->flags.wake_capable = 1;
942
Joe Perches3c5f9be2008-02-03 17:06:17 +0200943 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Patrick Mocheld550d982006-06-27 00:41:40 -0400945 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +0000948static void acpi_device_get_busid(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Len Brown4be44fc2005-08-05 00:44:28 -0400950 char bus_id[5] = { '?', 0 };
951 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
952 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 /*
955 * Bus ID
956 * ------
957 * The device's Bus ID is simply the object name.
958 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
959 */
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +0000960 if (ACPI_IS_ROOT_DEVICE(device)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 strcpy(device->pnp.bus_id, "ACPI");
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +0000962 return;
963 }
964
965 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 case ACPI_BUS_TYPE_POWER_BUTTON:
967 strcpy(device->pnp.bus_id, "PWRF");
968 break;
969 case ACPI_BUS_TYPE_SLEEP_BUTTON:
970 strcpy(device->pnp.bus_id, "SLPF");
971 break;
972 default:
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +0000973 acpi_get_name(device->handle, ACPI_SINGLE_NAME, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /* Clean up trailing underscores (if any) */
975 for (i = 3; i > 1; i--) {
976 if (bus_id[i] == '_')
977 bus_id[i] = '\0';
978 else
979 break;
980 }
981 strcpy(device->pnp.bus_id, bus_id);
982 break;
983 }
984}
985
Zhang Rui54735262007-01-11 02:09:09 -0500986/*
987 * acpi_bay_match - see if a device is an ejectable driver bay
988 *
989 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
990 * then we can safely call it an ejectable drive bay
991 */
992static int acpi_bay_match(struct acpi_device *device){
993 acpi_status status;
994 acpi_handle handle;
995 acpi_handle tmp;
996 acpi_handle phandle;
997
998 handle = device->handle;
999
1000 status = acpi_get_handle(handle, "_EJ0", &tmp);
1001 if (ACPI_FAILURE(status))
1002 return -ENODEV;
1003
1004 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
1005 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
1006 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
1007 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
1008 return 0;
1009
1010 if (acpi_get_parent(handle, &phandle))
1011 return -ENODEV;
1012
1013 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
1014 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
1015 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
1016 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
1017 return 0;
1018
1019 return -ENODEV;
1020}
1021
Frank Seidel3620f2f2007-12-07 13:20:34 +01001022/*
1023 * acpi_dock_match - see if a device has a _DCK method
1024 */
1025static int acpi_dock_match(struct acpi_device *device)
1026{
1027 acpi_handle tmp;
1028 return acpi_get_handle(device->handle, "_DCK", &tmp);
1029}
1030
Bob Moore15b8dd52009-06-29 13:39:29 +08001031static struct acpica_device_id_list*
1032acpi_add_cid(
1033 struct acpi_device_info *info,
1034 struct acpica_device_id *new_cid)
1035{
1036 struct acpica_device_id_list *cid;
1037 char *next_id_string;
1038 acpi_size cid_length;
1039 acpi_size new_cid_length;
1040 u32 i;
1041
1042
1043 /* Allocate new CID list with room for the new CID */
1044
1045 if (!new_cid)
1046 new_cid_length = info->compatible_id_list.list_size;
1047 else if (info->compatible_id_list.list_size)
1048 new_cid_length = info->compatible_id_list.list_size +
1049 new_cid->length + sizeof(struct acpica_device_id);
1050 else
1051 new_cid_length = sizeof(struct acpica_device_id_list) + new_cid->length;
1052
1053 cid = ACPI_ALLOCATE_ZEROED(new_cid_length);
1054 if (!cid) {
1055 return NULL;
1056 }
1057
1058 cid->list_size = new_cid_length;
1059 cid->count = info->compatible_id_list.count;
1060 if (new_cid)
1061 cid->count++;
1062 next_id_string = (char *) cid->ids + (cid->count * sizeof(struct acpica_device_id));
1063
1064 /* Copy all existing CIDs */
1065
1066 for (i = 0; i < info->compatible_id_list.count; i++) {
1067 cid_length = info->compatible_id_list.ids[i].length;
1068 cid->ids[i].string = next_id_string;
1069 cid->ids[i].length = cid_length;
1070
1071 ACPI_MEMCPY(next_id_string, info->compatible_id_list.ids[i].string,
1072 cid_length);
1073
1074 next_id_string += cid_length;
1075 }
1076
1077 /* Append the new CID */
1078
1079 if (new_cid) {
1080 cid->ids[i].string = next_id_string;
1081 cid->ids[i].length = new_cid->length;
1082
1083 ACPI_MEMCPY(next_id_string, new_cid->string, new_cid->length);
1084 }
1085
1086 return cid;
1087}
1088
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001089static void acpi_device_set_id(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
Bob Moore15b8dd52009-06-29 13:39:29 +08001091 struct acpi_device_info *info = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001092 char *hid = NULL;
1093 char *uid = NULL;
Bob Moore15b8dd52009-06-29 13:39:29 +08001094 struct acpica_device_id_list *cid_list = NULL;
1095 char *cid_add = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001096 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001098 switch (device->device_type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 case ACPI_BUS_TYPE_DEVICE:
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001100 if (ACPI_IS_ROOT_DEVICE(device)) {
1101 hid = ACPI_SYSTEM_HID;
1102 break;
1103 }
1104
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001105 status = acpi_get_object_info(device->handle, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001107 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return;
1109 }
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (info->valid & ACPI_VALID_HID)
Bob Moore15b8dd52009-06-29 13:39:29 +08001112 hid = info->hardware_id.string;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (info->valid & ACPI_VALID_UID)
Bob Moore15b8dd52009-06-29 13:39:29 +08001114 uid = info->unique_id.string;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (info->valid & ACPI_VALID_CID)
Bob Moore15b8dd52009-06-29 13:39:29 +08001116 cid_list = &info->compatible_id_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (info->valid & ACPI_VALID_ADR) {
1118 device->pnp.bus_address = info->address;
1119 device->flags.bus_address = 1;
1120 }
Zhang Ruiae843332006-12-07 20:57:10 +08001121
Frank Seidel3620f2f2007-12-07 13:20:34 +01001122 /* If we have a video/bay/dock device, add our selfdefined
1123 HID to the CID list. Like that the video/bay/dock drivers
1124 will get autoloaded and the device might still match
1125 against another driver.
1126 */
Thomas Renningerc3d6de62008-08-01 17:37:55 +02001127 if (acpi_is_video_device(device))
Frank Seidel3620f2f2007-12-07 13:20:34 +01001128 cid_add = ACPI_VIDEO_HID;
1129 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1130 cid_add = ACPI_BAY_HID;
1131 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1132 cid_add = ACPI_DOCK_HID;
Zhang Rui54735262007-01-11 02:09:09 -05001133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 break;
1135 case ACPI_BUS_TYPE_POWER:
1136 hid = ACPI_POWER_HID;
1137 break;
1138 case ACPI_BUS_TYPE_PROCESSOR:
Myron Stowead93a762008-11-04 14:52:55 -07001139 hid = ACPI_PROCESSOR_OBJECT_HID;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 case ACPI_BUS_TYPE_THERMAL:
1142 hid = ACPI_THERMAL_HID;
1143 break;
1144 case ACPI_BUS_TYPE_POWER_BUTTON:
1145 hid = ACPI_BUTTON_HID_POWERF;
1146 break;
1147 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1148 hid = ACPI_BUTTON_HID_SLEEPF;
1149 break;
1150 }
1151
Alex Chiang0c526d92009-05-14 08:31:37 -06001152 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 * \_SB
1154 * ----
1155 * Fix for the system root bus device -- the only root-level device.
1156 */
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001157 if (((acpi_handle)device->parent == ACPI_ROOT_OBJECT) &&
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001158 (device->device_type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 hid = ACPI_BUS_HID;
1160 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1161 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1162 }
1163
1164 if (hid) {
Bob Moore15b8dd52009-06-29 13:39:29 +08001165 device->pnp.hardware_id = ACPI_ALLOCATE_ZEROED(strlen (hid) + 1);
1166 if (device->pnp.hardware_id) {
1167 strcpy(device->pnp.hardware_id, hid);
1168 device->flags.hardware_id = 1;
Frank Seidel3620f2f2007-12-07 13:20:34 +01001169 }
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001170 }
1171 if (!device->flags.hardware_id)
1172 device->pnp.hardware_id = "";
Bob Moore15b8dd52009-06-29 13:39:29 +08001173
1174 if (uid) {
1175 device->pnp.unique_id = ACPI_ALLOCATE_ZEROED(strlen (uid) + 1);
1176 if (device->pnp.unique_id) {
1177 strcpy(device->pnp.unique_id, uid);
1178 device->flags.unique_id = 1;
1179 }
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001180 }
1181 if (!device->flags.unique_id)
1182 device->pnp.unique_id = "";
Bob Moore15b8dd52009-06-29 13:39:29 +08001183
1184 if (cid_list || cid_add) {
1185 struct acpica_device_id_list *list;
1186
1187 if (cid_add) {
1188 struct acpica_device_id cid;
1189 cid.length = strlen (cid_add) + 1;
1190 cid.string = cid_add;
1191
1192 list = acpi_add_cid(info, &cid);
1193 } else {
1194 list = acpi_add_cid(info, NULL);
1195 }
Frank Seidel3620f2f2007-12-07 13:20:34 +01001196
1197 if (list) {
Frank Seidel3620f2f2007-12-07 13:20:34 +01001198 device->pnp.cid_list = list;
Bob Moore15b8dd52009-06-29 13:39:29 +08001199 if (cid_add)
1200 device->flags.compatible_ids = 1;
1201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203
Bob Moore15b8dd52009-06-29 13:39:29 +08001204 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205}
1206
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001207static int acpi_device_set_context(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001209 acpi_status status;
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Context
1213 * -------
1214 * Attach this 'struct acpi_device' to the ACPI object. This makes
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001215 * resolutions from handle->device very efficient. Fixed hardware
1216 * devices have no handles, so we skip them.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001218 if (!device->handle)
1219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001221 status = acpi_attach_data(device->handle,
1222 acpi_bus_data_handler, device);
1223 if (ACPI_SUCCESS(status))
1224 return 0;
1225
1226 printk(KERN_ERR PREFIX "Error attaching device data\n");
1227 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Len Brown4be44fc2005-08-05 00:44:28 -04001230static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
Li Shaohua96333572006-12-07 20:56:46 +08001235 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001236 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Rui Zhang2786f6e2006-12-21 02:21:13 -05001241 /*
1242 * unbind _ADR-Based Devices when hot removal
1243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (dev->flags.bus_address) {
1245 if ((dev->parent) && (dev->parent->ops.unbind))
1246 dev->parent->ops.unbind(dev);
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1249
Patrick Mocheld550d982006-06-27 00:41:40 -04001250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}
1252
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001253static int acpi_add_single_object(struct acpi_device **child,
1254 acpi_handle handle, int type,
1255 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Bjorn Helgaas77c24882009-09-21 19:29:30 +00001257 int result;
1258 struct acpi_device *device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001259 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Burman Yan36bcbec2006-12-19 12:56:11 -08001261 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001263 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001264 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Bjorn Helgaascaaa6ef2009-09-21 19:29:10 +00001267 device->device_type = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 device->handle = handle;
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001269 device->parent = acpi_bus_get_parent(handle);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001270 device->bus_ops = *ops; /* workround for not call .start */
1271
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001272 acpi_device_get_busid(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 /*
1275 * Flags
1276 * -----
1277 * Get prior to calling acpi_bus_get_status() so we know whether
1278 * or not _STA is present. Note that we only look for object
1279 * handles -- cannot evaluate objects until we know the device is
1280 * present and properly initialized.
1281 */
1282 result = acpi_bus_get_flags(device);
1283 if (result)
1284 goto end;
1285
1286 /*
1287 * Status
1288 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001289 * See if the device is present. We always assume that non-Device
1290 * and non-Processor objects (e.g. thermal zones, power resources,
1291 * etc.) are present, functioning, etc. (at least when parent object
1292 * is present). Note that _STA has a different meaning for some
1293 * objects (e.g. power resources) so we need to be careful how we use
1294 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 */
1296 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001297 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 case ACPI_BUS_TYPE_DEVICE:
1299 result = acpi_bus_get_status(device);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001300 if (ACPI_FAILURE(result)) {
1301 result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 goto end;
1303 }
Zhao Yakui39a0ad82008-08-11 13:40:22 +08001304 /*
1305 * When the device is neither present nor functional, the
1306 * device should not be added to Linux ACPI device tree.
1307 * When the status of the device is not present but functinal,
1308 * it should be added to Linux ACPI tree. For example : bay
1309 * device , dock device.
1310 * In such conditions it is unncessary to check whether it is
1311 * bay device or dock device.
1312 */
1313 if (!device->status.present && !device->status.functional) {
1314 result = -ENODEV;
1315 goto end;
Frank Seidel3620f2f2007-12-07 13:20:34 +01001316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 break;
1318 default:
Bjorn Helgaas0c0e8922007-04-25 14:20:58 -04001319 STRUCT_TO_INT(device->status) =
1320 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1321 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 break;
1323 }
1324
1325 /*
1326 * Initialize Device
1327 * -----------------
1328 * TBD: Synch with Core's enumeration/initialization process.
1329 */
1330
1331 /*
1332 * Hardware ID, Unique ID, & Bus Address
1333 * -------------------------------------
1334 */
Bjorn Helgaasc7bcb4e2009-09-21 19:29:25 +00001335 acpi_device_set_id(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /*
1338 * Power Management
1339 * ----------------
1340 */
1341 if (device->flags.power_manageable) {
1342 result = acpi_bus_get_power_flags(device);
1343 if (result)
1344 goto end;
1345 }
1346
Len Brown4be44fc2005-08-05 00:44:28 -04001347 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 * Wakeup device management
1349 *-----------------------
1350 */
1351 if (device->flags.wake_capable) {
1352 result = acpi_bus_get_wakeup_device_flags(device);
1353 if (result)
1354 goto end;
1355 }
1356
1357 /*
1358 * Performance Management
1359 * ----------------------
1360 */
1361 if (device->flags.performance_manageable) {
1362 result = acpi_bus_get_perf_flags(device);
1363 if (result)
1364 goto end;
1365 }
1366
Bjorn Helgaasbc3b0772009-09-21 19:29:20 +00001367 if ((result = acpi_device_set_context(device)))
Len Brownf61f9252009-09-05 13:33:23 -04001368 goto end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Bjorn Helgaas66b7ed42009-09-21 19:29:05 +00001370 result = acpi_device_register(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001373 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 */
1375 if (device->flags.bus_address) {
1376 if (device->parent && device->parent->ops.bind)
1377 device->parent->ops.bind(device);
1378 }
1379
Alex Chiang0c526d92009-05-14 08:31:37 -06001380end:
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001381 if (!result) {
1382 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
1383 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1384 "Adding %s [%s] parent %s\n", dev_name(&device->dev),
1385 (char *) buffer.pointer,
1386 device->parent ? dev_name(&device->parent->dev) :
1387 "(null)"));
1388 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 *child = device;
Bjorn Helgaas29aaefa2009-09-21 19:28:54 +00001390 } else
Hugh Dickins718fb0d2009-08-06 23:18:12 +00001391 acpi_device_release(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Patrick Mocheld550d982006-06-27 00:41:40 -04001393 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001396static int acpi_bus_scan(acpi_handle handle, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Len Brown4be44fc2005-08-05 00:44:28 -04001398 acpi_status status = AE_OK;
1399 struct acpi_device *parent = NULL;
1400 struct acpi_device *child = NULL;
1401 acpi_handle phandle = NULL;
1402 acpi_handle chandle = NULL;
1403 acpi_object_type type = 0;
1404 u32 level = 1;
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001405 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001407 /*
1408 * We must have an acpi_device for the starting node already, and
1409 * we scan its children.
1410 */
1411 phandle = handle;
1412 ret = acpi_bus_get_device(phandle, &parent);
1413 if (ret)
1414 return ret;
Len Brown4be44fc2005-08-05 00:44:28 -04001415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 /*
1417 * Parse through the ACPI namespace, identify all 'devices', and
1418 * create a new 'struct acpi_device' for each.
1419 */
1420 while ((level > 0) && parent) {
1421
1422 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001423 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 /*
1426 * If this scope is exhausted then move our way back up.
1427 */
1428 if (ACPI_FAILURE(status)) {
1429 level--;
1430 chandle = phandle;
1431 acpi_get_parent(phandle, &phandle);
1432 if (parent->parent)
1433 parent = parent->parent;
1434 continue;
1435 }
1436
1437 status = acpi_get_type(chandle, &type);
1438 if (ACPI_FAILURE(status))
1439 continue;
1440
1441 /*
1442 * If this is a scope object then parse it (depth-first).
1443 */
1444 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1445 level++;
1446 phandle = chandle;
1447 chandle = NULL;
1448 continue;
1449 }
1450
1451 /*
1452 * We're only interested in objects that we consider 'devices'.
1453 */
1454 switch (type) {
1455 case ACPI_TYPE_DEVICE:
1456 type = ACPI_BUS_TYPE_DEVICE;
1457 break;
1458 case ACPI_TYPE_PROCESSOR:
1459 type = ACPI_BUS_TYPE_PROCESSOR;
1460 break;
1461 case ACPI_TYPE_THERMAL:
1462 type = ACPI_BUS_TYPE_THERMAL;
1463 break;
1464 case ACPI_TYPE_POWER:
1465 type = ACPI_BUS_TYPE_POWER;
1466 break;
1467 default:
1468 continue;
1469 }
1470
Rajesh Shah3fb02732005-04-28 00:25:52 -07001471 if (ops->acpi_op_add)
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001472 status = acpi_add_single_object(&child, chandle, type,
1473 ops);
Len Brown4be44fc2005-08-05 00:44:28 -04001474 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001475 status = acpi_bus_get_device(chandle, &child);
1476
Len Brown4be44fc2005-08-05 00:44:28 -04001477 if (ACPI_FAILURE(status))
1478 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001479
Li Shaohuac4168bf2006-12-07 20:56:41 +08001480 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001481 status = acpi_start_single_object(child);
1482 if (ACPI_FAILURE(status))
1483 continue;
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /*
1487 * If the device is present, enabled, and functioning then
1488 * parse its scope (depth-first). Note that we need to
1489 * represent absent devices to facilitate PnP notifications
1490 * -- but only the subtree head (not all of its children,
1491 * which will be enumerated when the parent is inserted).
1492 *
1493 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001494 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 */
Zhao Yakui39a0ad82008-08-11 13:40:22 +08001496 /*
1497 * When the device is not present but functional, it is also
1498 * necessary to scan the children of this device.
1499 */
1500 if (child->status.present || (!child->status.present &&
1501 child->status.functional)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1503 NULL, NULL);
1504 if (ACPI_SUCCESS(status)) {
1505 level++;
1506 phandle = chandle;
1507 chandle = NULL;
1508 parent = child;
1509 }
1510 }
1511 }
1512
Patrick Mocheld550d982006-06-27 00:41:40 -04001513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Rajesh Shah3fb02732005-04-28 00:25:52 -07001516int
Len Brown4be44fc2005-08-05 00:44:28 -04001517acpi_bus_add(struct acpi_device **child,
1518 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001519{
1520 int result;
1521 struct acpi_bus_ops ops;
1522
Li Shaohuac4168bf2006-12-07 20:56:41 +08001523 memset(&ops, 0, sizeof(ops));
1524 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001525
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001526 result = acpi_add_single_object(child, handle, type, &ops);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001527 if (!result)
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001528 result = acpi_bus_scan((*child)->handle, &ops);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001529
Patrick Mocheld550d982006-06-27 00:41:40 -04001530 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001531}
1532EXPORT_SYMBOL(acpi_bus_add);
1533
Len Brown4be44fc2005-08-05 00:44:28 -04001534int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001535{
1536 int result;
1537 struct acpi_bus_ops ops;
1538
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001539 memset(&ops, 0, sizeof(ops));
1540 ops.acpi_op_start = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001541
1542 result = acpi_start_single_object(device);
Bjorn Helgaas8e029bf2009-09-21 19:29:40 +00001543 if (!result)
1544 result = acpi_bus_scan(device->handle, &ops);
1545
Patrick Mocheld550d982006-06-27 00:41:40 -04001546 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001547}
1548EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Kristen Accardiceaba662006-02-23 17:56:01 -08001550int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Len Brown4be44fc2005-08-05 00:44:28 -04001552 acpi_status status;
1553 struct acpi_device *parent, *child;
1554 acpi_handle phandle, chandle;
1555 acpi_object_type type;
1556 u32 level = 1;
1557 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
Len Brown4be44fc2005-08-05 00:44:28 -04001559 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 phandle = start->handle;
1561 child = chandle = NULL;
1562
1563 while ((level > 0) && parent && (!err)) {
1564 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001565 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 /*
1568 * If this scope is exhausted then move our way back up.
1569 */
1570 if (ACPI_FAILURE(status)) {
1571 level--;
1572 chandle = phandle;
1573 acpi_get_parent(phandle, &phandle);
1574 child = parent;
1575 parent = parent->parent;
1576
1577 if (level == 0)
1578 err = acpi_bus_remove(child, rmdevice);
1579 else
1580 err = acpi_bus_remove(child, 1);
1581
1582 continue;
1583 }
1584
1585 status = acpi_get_type(chandle, &type);
1586 if (ACPI_FAILURE(status)) {
1587 continue;
1588 }
1589 /*
1590 * If there is a device corresponding to chandle then
1591 * parse it (depth-first).
1592 */
1593 if (acpi_bus_get_device(chandle, &child) == 0) {
1594 level++;
1595 phandle = chandle;
1596 chandle = NULL;
1597 parent = child;
1598 }
1599 continue;
1600 }
1601 return err;
1602}
Kristen Accardiceaba662006-02-23 17:56:01 -08001603EXPORT_SYMBOL_GPL(acpi_bus_trim);
1604
Bjorn Helgaase8b945c2009-09-21 19:28:59 +00001605static int acpi_bus_scan_fixed(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Len Brown4be44fc2005-08-05 00:44:28 -04001607 int result = 0;
1608 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001609 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Li Shaohuac4168bf2006-12-07 20:56:41 +08001611 memset(&ops, 0, sizeof(ops));
1612 ops.acpi_op_add = 1;
1613 ops.acpi_op_start = 1;
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 /*
1616 * Enumerate all fixed-feature devices.
1617 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001618 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001619 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001620 ACPI_BUS_TYPE_POWER_BUTTON,
1621 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001624 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001625 result = acpi_add_single_object(&device, NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001626 ACPI_BUS_TYPE_SLEEP_BUTTON,
1627 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Patrick Mocheld550d982006-06-27 00:41:40 -04001630 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Bjorn Helgaase747f272009-03-24 16:49:43 -06001633int __init acpi_scan_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
1635 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001636 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Li Shaohuac4168bf2006-12-07 20:56:41 +08001638 memset(&ops, 0, sizeof(ops));
1639 ops.acpi_op_add = 1;
1640 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Patrick Mochel5b327262006-05-10 10:33:00 -04001642 result = bus_register(&acpi_bus_type);
1643 if (result) {
1644 /* We don't want to quit even if we failed to add suspend/resume */
1645 printk(KERN_ERR PREFIX "Could not register bus type\n");
1646 }
1647
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 /*
1649 * Create the root device in the bus's device tree
1650 */
Bjorn Helgaas5c478f42009-09-21 19:29:35 +00001651 result = acpi_add_single_object(&acpi_root, ACPI_ROOT_OBJECT,
Bjorn Helgaas859ac9a2009-09-21 19:29:50 +00001652 ACPI_BUS_TYPE_DEVICE, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (result)
1654 goto Done;
1655
1656 /*
1657 * Enumerate devices in the ACPI namespace.
1658 */
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001659 result = acpi_bus_scan(acpi_root->handle, &ops);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001660
Li Shaohuac4168bf2006-12-07 20:56:41 +08001661 if (!result)
Bjorn Helgaasadc08e22009-09-21 19:29:45 +00001662 result = acpi_bus_scan_fixed();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 if (result)
1665 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1666
Alex Chiang0c526d92009-05-14 08:31:37 -06001667Done:
Patrick Mocheld550d982006-06-27 00:41:40 -04001668 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669}