blob: 5b4d462117cf915c6e1f2a89d7604ec9ddaa712b [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 Brownf52fd662007-02-12 22:42:12 -050014ACPI_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"
Thomas Renninger29b71a12007-07-23 14:43:51 +020019#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define ACPI_BUS_DEVICE_NAME "System Bus"
21
22static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080023static LIST_HEAD(acpi_bus_id_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024DEFINE_SPINLOCK(acpi_device_lock);
25LIST_HEAD(acpi_wakeup_device_list);
26
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080027struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080028 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080029 unsigned int instance_no;
30 struct list_head node;
31};
Thomas Renninger29b71a12007-07-23 14:43:51 +020032
33/*
34 * Creates hid/cid(s) string needed for modalias and uevent
35 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
36 * char *modalias: "acpi:IBM0001:ACPI0001"
37*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020038static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
39 int size)
40{
Thomas Renninger29b71a12007-07-23 14:43:51 +020041 int len;
42
43 if (!acpi_dev->flags.hardware_id)
44 return -ENODEV;
45
46 len = snprintf(modalias, size, "acpi:%s:",
47 acpi_dev->pnp.hardware_id);
48 if (len < 0 || len >= size)
49 return -EINVAL;
50 size -= len;
51
52 if (acpi_dev->flags.compatible_ids) {
53 struct acpi_compatible_id_list *cid_list;
54 int i;
55 int count;
56
57 cid_list = acpi_dev->pnp.cid_list;
58 for (i = 0; i < cid_list->count; i++) {
59 count = snprintf(&modalias[len], size, "%s:",
60 cid_list->id[i].value);
61 if (count < 0 || count >= size) {
62 printk(KERN_ERR "acpi: %s cid[%i] exceeds event buffer size",
63 acpi_dev->pnp.device_name, i);
64 break;
65 }
66 len += count;
67 size -= count;
68 }
69 }
70
71 modalias[len] = '\0';
72 return len;
73}
74
75static ssize_t
76acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
77 struct acpi_device *acpi_dev = to_acpi_device(dev);
78 int len;
79
80 /* Device has no HID and no CID or string is >1024 */
81 len = create_modalias(acpi_dev, buf, 1024);
82 if (len <= 0)
83 return 0;
84 buf[len++] = '\n';
85 return len;
86}
87static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
88
Len Brown4be44fc2005-08-05 00:44:28 -040089static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct acpi_object_list arg_list;
92 union acpi_object arg;
93 acpi_status status = AE_OK;
94
95 /*
96 * TBD: evaluate _PS3?
97 */
98
99 if (lockable) {
100 arg_list.count = 1;
101 arg_list.pointer = &arg;
102 arg.type = ACPI_TYPE_INTEGER;
103 arg.integer.value = 0;
104 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
105 }
106
107 arg_list.count = 1;
108 arg_list.pointer = &arg;
109 arg.type = ACPI_TYPE_INTEGER;
110 arg.integer.value = 1;
111
112 /*
113 * TBD: _EJD support.
114 */
115
116 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
117 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400118 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120
Len Brown4be44fc2005-08-05 00:44:28 -0400121 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800125acpi_eject_store(struct device *d, struct device_attribute *attr,
126 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Len Brown4be44fc2005-08-05 00:44:28 -0400128 int result;
129 int ret = count;
130 int islockable;
131 acpi_status status;
132 acpi_handle handle;
133 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800134 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 if ((!count) || (buf[0] != '1')) {
137 return -EINVAL;
138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800140 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 ret = -ENODEV;
142 goto err;
143 }
144#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800145 status = acpi_get_type(acpi_device->handle, &type);
146 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 ret = -ENODEV;
148 goto err;
149 }
150
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800151 islockable = acpi_device->flags.lockable;
152 handle = acpi_device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800154 result = acpi_bus_trim(acpi_device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (!result)
157 result = acpi_eject_operation(handle, islockable);
158
159 if (result) {
160 ret = -EBUSY;
161 }
Len Brown4be44fc2005-08-05 00:44:28 -0400162 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800166static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800168static ssize_t
169acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
170 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800172 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
173}
174static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
175
176static ssize_t
177acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
178 struct acpi_device *acpi_dev = to_acpi_device(dev);
179 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
180 int result;
181
182 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
183 if(result)
184 goto end;
185
186 result = sprintf(buf, "%s\n", (char*)path.pointer);
187 kfree(path.pointer);
188 end:
189 return result;
190}
191static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
192
193static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800195 acpi_status status;
196 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800197 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800199 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800200 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800201 */
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800202 if(dev->handle) {
203 result = device_create_file(&dev->dev, &dev_attr_path);
204 if(result)
205 goto end;
206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800208 if(dev->flags.hardware_id) {
209 result = device_create_file(&dev->dev, &dev_attr_hid);
210 if(result)
211 goto end;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Thomas Renninger29b71a12007-07-23 14:43:51 +0200214 if (dev->flags.hardware_id || dev->flags.compatible_ids){
215 result = device_create_file(&dev->dev, &dev_attr_modalias);
216 if(result)
217 goto end;
218 }
219
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800220 /*
221 * If device has _EJ0, 'eject' file is created that is used to trigger
222 * hot-removal function from userland.
223 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800224 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
225 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800226 result = device_create_file(&dev->dev, &dev_attr_eject);
227 end:
228 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800229}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800231static void acpi_device_remove_files(struct acpi_device *dev)
232{
233 acpi_status status;
234 acpi_handle temp;
235
236 /*
237 * If device has _EJ0, 'eject' file is created that is used to trigger
238 * hot-removal function from userland.
239 */
240 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
241 if (ACPI_SUCCESS(status))
242 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800243
Thomas Renninger29b71a12007-07-23 14:43:51 +0200244 if (dev->flags.hardware_id || dev->flags.compatible_ids)
245 device_remove_file(&dev->dev, &dev_attr_modalias);
246
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800247 if(dev->flags.hardware_id)
248 device_remove_file(&dev->dev, &dev_attr_hid);
249 if(dev->handle)
250 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800251}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800253 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200255
256int acpi_match_device_ids(struct acpi_device *device,
257 const struct acpi_device_id *ids)
258{
259 const struct acpi_device_id *id;
260
261 if (device->flags.hardware_id) {
262 for (id = ids; id->id[0]; id++) {
263 if (!strcmp((char*)id->id, device->pnp.hardware_id))
264 return 0;
265 }
266 }
267
268 if (device->flags.compatible_ids) {
269 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
270 int i;
271
272 for (id = ids; id->id[0]; id++) {
273 /* compare multiple _CID entries against driver ids */
274 for (i = 0; i < cid_list->count; i++) {
275 if (!strcmp((char*)id->id,
276 cid_list->id[i].value))
277 return 0;
278 }
279 }
280 }
281
282 return -ENOENT;
283}
284EXPORT_SYMBOL(acpi_match_device_ids);
285
Patrick Mochel1890a972006-12-07 20:56:31 +0800286static void acpi_device_release(struct device *dev)
287{
288 struct acpi_device *acpi_dev = to_acpi_device(dev);
289
290 kfree(acpi_dev->pnp.cid_list);
291 kfree(acpi_dev);
292}
293
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800294static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800295{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800296 struct acpi_device *acpi_dev = to_acpi_device(dev);
297 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800298
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800299 if (acpi_drv && acpi_drv->ops.suspend)
300 return acpi_drv->ops.suspend(acpi_dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return 0;
302}
303
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800304static int acpi_device_resume(struct device *dev)
305{
306 struct acpi_device *acpi_dev = to_acpi_device(dev);
307 struct acpi_driver *acpi_drv = acpi_dev->driver;
308
309 if (acpi_drv && acpi_drv->ops.resume)
310 return acpi_drv->ops.resume(acpi_dev);
311 return 0;
312}
313
314static int acpi_bus_match(struct device *dev, struct device_driver *drv)
315{
316 struct acpi_device *acpi_dev = to_acpi_device(dev);
317 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
318
Thomas Renninger29b71a12007-07-23 14:43:51 +0200319 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800320}
321
Kay Sievers7eff2e72007-08-14 15:15:12 +0200322static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800323{
324 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200325 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800326
Kay Sievers7eff2e72007-08-14 15:15:12 +0200327 if (add_uevent_var(env, "MODALIAS="))
328 return -ENOMEM;
329 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
330 sizeof(env->buf) - env->buflen);
331 if (len >= (sizeof(env->buf) - env->buflen))
332 return -ENOMEM;
333 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return 0;
335}
336
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800337static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
338static int acpi_start_single_object(struct acpi_device *);
339static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800340{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800341 struct acpi_device *acpi_dev = to_acpi_device(dev);
342 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
343 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800345 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
346 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800347 if (acpi_dev->bus_ops.acpi_op_start)
348 acpi_start_single_object(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800349 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
350 "Found driver [%s] for device [%s]\n",
351 acpi_drv->name, acpi_dev->pnp.bus_id));
352 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800353 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800354 return ret;
355}
356
357static int acpi_device_remove(struct device * dev)
358{
359 struct acpi_device *acpi_dev = to_acpi_device(dev);
360 struct acpi_driver *acpi_drv = acpi_dev->driver;
361
362 if (acpi_drv) {
363 if (acpi_drv->ops.stop)
Li Shaohua96333572006-12-07 20:56:46 +0800364 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800365 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800366 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800367 }
368 acpi_dev->driver = NULL;
369 acpi_driver_data(dev) = NULL;
370
371 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800372 return 0;
373}
374
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800375static void acpi_device_shutdown(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800376{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800377 struct acpi_device *acpi_dev = to_acpi_device(dev);
378 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800379
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800380 if (acpi_drv && acpi_drv->ops.shutdown)
381 acpi_drv->ops.shutdown(acpi_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800383 return ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
David Brownell55955aa2007-05-08 00:28:35 -0700386struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800387 .name = "acpi",
388 .suspend = acpi_device_suspend,
389 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800390 .shutdown = acpi_device_shutdown,
391 .match = acpi_bus_match,
392 .probe = acpi_device_probe,
393 .remove = acpi_device_remove,
394 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395};
396
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800397static int acpi_device_register(struct acpi_device *device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct acpi_device *parent)
399{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800400 int result;
401 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
402 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /*
404 * Linkage
405 * -------
406 * Link this device to its parent and siblings.
407 */
408 INIT_LIST_HEAD(&device->children);
409 INIT_LIST_HEAD(&device->node);
410 INIT_LIST_HEAD(&device->g_list);
411 INIT_LIST_HEAD(&device->wakeup_list);
412
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800413 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
414 if (!new_bus_id) {
415 printk(KERN_ERR PREFIX "Memory allocation error\n");
416 return -ENOMEM;
417 }
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 spin_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800420 /*
421 * Find suitable bus_id and instance number in acpi_bus_id_list
422 * If failed, create one and link it into acpi_bus_id_list
423 */
424 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Zhang Ruibb095852007-01-04 15:03:18 +0800425 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800426 acpi_device_bus_id->instance_no ++;
427 found = 1;
428 kfree(new_bus_id);
429 break;
430 }
431 }
432 if(!found) {
433 acpi_device_bus_id = new_bus_id;
Zhang Ruibb095852007-01-04 15:03:18 +0800434 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800435 acpi_device_bus_id->instance_no = 0;
436 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
437 }
438 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (device->parent) {
441 list_add_tail(&device->node, &device->parent->children);
442 list_add_tail(&device->g_list, &device->parent->g_list);
443 } else
444 list_add_tail(&device->g_list, &acpi_device_list);
445 if (device->wakeup.flags.valid)
446 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
447 spin_unlock(&acpi_device_lock);
448
Patrick Mochel1890a972006-12-07 20:56:31 +0800449 if (device->parent)
450 device->dev.parent = &parent->dev;
451 device->dev.bus = &acpi_bus_type;
452 device_initialize(&device->dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800453 device->dev.release = &acpi_device_release;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800454 result = device_add(&device->dev);
455 if(result) {
456 printk("Error adding device %s", device->dev.bus_id);
457 goto end;
458 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800459
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800460 result = acpi_device_setup_files(device);
461 if(result)
462 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
463
Li Shaohua96333572006-12-07 20:56:46 +0800464 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800465 return 0;
466 end:
467 spin_lock(&acpi_device_lock);
468 if (device->parent) {
469 list_del(&device->node);
470 list_del(&device->g_list);
471 } else
472 list_del(&device->g_list);
473 list_del(&device->wakeup_list);
474 spin_unlock(&acpi_device_lock);
475 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478static void acpi_device_unregister(struct acpi_device *device, int type)
479{
480 spin_lock(&acpi_device_lock);
481 if (device->parent) {
482 list_del(&device->node);
483 list_del(&device->g_list);
484 } else
485 list_del(&device->g_list);
486
487 list_del(&device->wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 spin_unlock(&acpi_device_lock);
489
490 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800491
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800492 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800493 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Zhang Rui9e89dde2006-12-07 20:56:16 +0800496/* --------------------------------------------------------------------------
497 Driver Management
498 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500500 * acpi_bus_driver_init - add a device to a driver
501 * @device: the device to add and initialize
502 * @driver: driver for the device
503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800505 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
507static int
Len Brown4be44fc2005-08-05 00:44:28 -0400508acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Len Brown4be44fc2005-08-05 00:44:28 -0400510 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400514 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400517 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 result = driver->ops.add(device);
520 if (result) {
521 device->driver = NULL;
522 acpi_driver_data(device) = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400523 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525
526 device->driver = driver;
527
528 /*
529 * TBD - Configuration Management: Assign resources to device based
530 * upon possible configuration and currently allocated resources.
531 */
532
Len Brown4be44fc2005-08-05 00:44:28 -0400533 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
534 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400535 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700536}
537
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400538static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700539{
540 int result = 0;
541 struct acpi_driver *driver;
542
Rajesh Shah3fb02732005-04-28 00:25:52 -0700543
544 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400545 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (driver->ops.start) {
548 result = driver->ops.start(device);
549 if (result && driver->ops.remove)
550 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552
Patrick Mocheld550d982006-06-27 00:41:40 -0400553 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500557 * acpi_bus_register_driver - register a driver with the ACPI bus
558 * @driver: driver being registered
559 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500561 * devices that match the driver's criteria and binds. Returns zero for
562 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Len Brown4be44fc2005-08-05 00:44:28 -0400564int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Patrick Mochel1890a972006-12-07 20:56:31 +0800566 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800570 driver->drv.name = driver->name;
571 driver->drv.bus = &acpi_bus_type;
572 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Patrick Mochel1890a972006-12-07 20:56:31 +0800574 ret = driver_register(&driver->drv);
575 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Len Brown4be44fc2005-08-05 00:44:28 -0400578EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500581 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
582 * @driver: driver to unregister
583 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * Unregisters a driver with the ACPI bus. Searches the namespace for all
585 * devices that match the driver's criteria and unbinds.
586 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400587void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Patrick Mochel1890a972006-12-07 20:56:31 +0800589 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
Len Brown4be44fc2005-08-05 00:44:28 -0400591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592EXPORT_SYMBOL(acpi_bus_unregister_driver);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594/* --------------------------------------------------------------------------
595 Device Enumeration
596 -------------------------------------------------------------------------- */
Len Brownc8f7a62c2006-07-09 17:22:28 -0400597acpi_status
598acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
599{
600 acpi_status status;
601 acpi_handle tmp;
602 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
603 union acpi_object *obj;
604
605 status = acpi_get_handle(handle, "_EJD", &tmp);
606 if (ACPI_FAILURE(status))
607 return status;
608
609 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
610 if (ACPI_SUCCESS(status)) {
611 obj = buffer.pointer;
612 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
613 kfree(buffer.pointer);
614 }
615 return status;
616}
617EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
620{
621
622 /* TBD */
623
624 return;
625}
626
Zhang Rui9e89dde2006-12-07 20:56:16 +0800627static int acpi_bus_get_perf_flags(struct acpi_device *device)
628{
629 device->performance.state = ACPI_STATE_UNKNOWN;
630 return 0;
631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633static acpi_status
634acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
635 union acpi_object *package)
636{
637 int i = 0;
638 union acpi_object *element = NULL;
639
640 if (!device || !package || (package->package.count < 2))
641 return AE_BAD_PARAMETER;
642
643 element = &(package->package.elements[0]);
644 if (!element)
645 return AE_BAD_PARAMETER;
646 if (element->type == ACPI_TYPE_PACKAGE) {
647 if ((element->package.count < 2) ||
648 (element->package.elements[0].type !=
649 ACPI_TYPE_LOCAL_REFERENCE)
650 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
651 return AE_BAD_DATA;
652 device->wakeup.gpe_device =
653 element->package.elements[0].reference.handle;
654 device->wakeup.gpe_number =
655 (u32) element->package.elements[1].integer.value;
656 } else if (element->type == ACPI_TYPE_INTEGER) {
657 device->wakeup.gpe_number = element->integer.value;
658 } else
659 return AE_BAD_DATA;
660
661 element = &(package->package.elements[1]);
662 if (element->type != ACPI_TYPE_INTEGER) {
663 return AE_BAD_DATA;
664 }
665 device->wakeup.sleep_state = element->integer.value;
666
667 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
668 return AE_NO_MEMORY;
669 }
670 device->wakeup.resources.count = package->package.count - 2;
671 for (i = 0; i < device->wakeup.resources.count; i++) {
672 element = &(package->package.elements[i + 2]);
673 if (element->type != ACPI_TYPE_ANY) {
674 return AE_BAD_DATA;
675 }
676
677 device->wakeup.resources.handles[i] = element->reference.handle;
678 }
679
680 return AE_OK;
681}
682
683static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
684{
685 acpi_status status = 0;
686 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
687 union acpi_object *package = NULL;
688
Thomas Renninger29b71a12007-07-23 14:43:51 +0200689 struct acpi_device_id button_device_ids[] = {
690 {"PNP0C0D", 0},
691 {"PNP0C0C", 0},
692 {"PNP0C0E", 0},
693 {"", 0},
694 };
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 /* _PRW */
698 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
699 if (ACPI_FAILURE(status)) {
700 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
701 goto end;
702 }
703
704 package = (union acpi_object *)buffer.pointer;
705 status = acpi_bus_extract_wakeup_device_power_package(device, package);
706 if (ACPI_FAILURE(status)) {
707 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
708 goto end;
709 }
710
711 kfree(buffer.pointer);
712
713 device->wakeup.flags.valid = 1;
714 /* Power button, Lid switch always enable wakeup */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200715 if (!acpi_match_device_ids(device, button_device_ids))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 device->wakeup.flags.run_wake = 1;
717
718 end:
719 if (ACPI_FAILURE(status))
720 device->flags.wake_capable = 0;
721 return 0;
722}
723
Zhang Rui9e89dde2006-12-07 20:56:16 +0800724static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800726 acpi_status status = 0;
727 acpi_handle handle = NULL;
728 u32 i = 0;
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800732 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800734 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800736 device->power.flags.explicit_get = 1;
737 status = acpi_get_handle(device->handle, "_IRC", &handle);
738 if (ACPI_SUCCESS(status))
739 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800742 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800744 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
745 struct acpi_device_power_state *ps = &device->power.states[i];
746 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Zhang Rui9e89dde2006-12-07 20:56:16 +0800748 /* Evaluate "_PRx" to se if power resources are referenced */
749 acpi_evaluate_reference(device->handle, object_name, NULL,
750 &ps->resources);
751 if (ps->resources.count) {
752 device->power.flags.power_resources = 1;
753 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Zhang Rui9e89dde2006-12-07 20:56:16 +0800756 /* Evaluate "_PSx" to see if we can do explicit sets */
757 object_name[2] = 'S';
758 status = acpi_get_handle(device->handle, object_name, &handle);
759 if (ACPI_SUCCESS(status)) {
760 ps->flags.explicit_set = 1;
761 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800763
764 /* State is valid if we have some power control */
765 if (ps->resources.count || ps->flags.explicit_set)
766 ps->flags.valid = 1;
767
768 ps->power = -1; /* Unknown - driver assigned */
769 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Zhang Rui9e89dde2006-12-07 20:56:16 +0800772 /* Set defaults for D0 and D3 states (always valid) */
773 device->power.states[ACPI_STATE_D0].flags.valid = 1;
774 device->power.states[ACPI_STATE_D0].power = 100;
775 device->power.states[ACPI_STATE_D3].flags.valid = 1;
776 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Zhang Rui9e89dde2006-12-07 20:56:16 +0800778 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Zhang Rui9e89dde2006-12-07 20:56:16 +0800780 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 return 0;
783}
784
Len Brown4be44fc2005-08-05 00:44:28 -0400785static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Len Brown4be44fc2005-08-05 00:44:28 -0400787 acpi_status status = AE_OK;
788 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 /* Presence of _STA indicates 'dynamic_status' */
792 status = acpi_get_handle(device->handle, "_STA", &temp);
793 if (ACPI_SUCCESS(status))
794 device->flags.dynamic_status = 1;
795
796 /* Presence of _CID indicates 'compatible_ids' */
797 status = acpi_get_handle(device->handle, "_CID", &temp);
798 if (ACPI_SUCCESS(status))
799 device->flags.compatible_ids = 1;
800
801 /* Presence of _RMV indicates 'removable' */
802 status = acpi_get_handle(device->handle, "_RMV", &temp);
803 if (ACPI_SUCCESS(status))
804 device->flags.removable = 1;
805
806 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
807 status = acpi_get_handle(device->handle, "_EJD", &temp);
808 if (ACPI_SUCCESS(status))
809 device->flags.ejectable = 1;
810 else {
811 status = acpi_get_handle(device->handle, "_EJ0", &temp);
812 if (ACPI_SUCCESS(status))
813 device->flags.ejectable = 1;
814 }
815
816 /* Presence of _LCK indicates 'lockable' */
817 status = acpi_get_handle(device->handle, "_LCK", &temp);
818 if (ACPI_SUCCESS(status))
819 device->flags.lockable = 1;
820
821 /* Presence of _PS0|_PR0 indicates 'power manageable' */
822 status = acpi_get_handle(device->handle, "_PS0", &temp);
823 if (ACPI_FAILURE(status))
824 status = acpi_get_handle(device->handle, "_PR0", &temp);
825 if (ACPI_SUCCESS(status))
826 device->flags.power_manageable = 1;
827
828 /* Presence of _PRW indicates wake capable */
829 status = acpi_get_handle(device->handle, "_PRW", &temp);
830 if (ACPI_SUCCESS(status))
831 device->flags.wake_capable = 1;
832
833 /* TBD: Peformance management */
834
Patrick Mocheld550d982006-06-27 00:41:40 -0400835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Len Brown4be44fc2005-08-05 00:44:28 -0400838static void acpi_device_get_busid(struct acpi_device *device,
839 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
Len Brown4be44fc2005-08-05 00:44:28 -0400841 char bus_id[5] = { '?', 0 };
842 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
843 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /*
846 * Bus ID
847 * ------
848 * The device's Bus ID is simply the object name.
849 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
850 */
851 switch (type) {
852 case ACPI_BUS_TYPE_SYSTEM:
853 strcpy(device->pnp.bus_id, "ACPI");
854 break;
855 case ACPI_BUS_TYPE_POWER_BUTTON:
856 strcpy(device->pnp.bus_id, "PWRF");
857 break;
858 case ACPI_BUS_TYPE_SLEEP_BUTTON:
859 strcpy(device->pnp.bus_id, "SLPF");
860 break;
861 default:
862 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
863 /* Clean up trailing underscores (if any) */
864 for (i = 3; i > 1; i--) {
865 if (bus_id[i] == '_')
866 bus_id[i] = '\0';
867 else
868 break;
869 }
870 strcpy(device->pnp.bus_id, bus_id);
871 break;
872 }
873}
874
Zhang Ruiae843332006-12-07 20:57:10 +0800875static int
876acpi_video_bus_match(struct acpi_device *device)
877{
878 acpi_handle h_dummy1;
879 acpi_handle h_dummy2;
880 acpi_handle h_dummy3;
881
882
883 if (!device)
884 return -EINVAL;
885
886 /* Since there is no HID, CID for ACPI Video drivers, we have
887 * to check well known required nodes for each feature we support.
888 */
889
890 /* Does this device able to support video switching ? */
891 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
892 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
893 return 0;
894
895 /* Does this device able to retrieve a video ROM ? */
896 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
897 return 0;
898
899 /* Does this device able to configure which video head to be POSTed ? */
900 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
901 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
902 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
903 return 0;
904
905 return -ENODEV;
906}
907
Zhang Rui54735262007-01-11 02:09:09 -0500908/*
909 * acpi_bay_match - see if a device is an ejectable driver bay
910 *
911 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
912 * then we can safely call it an ejectable drive bay
913 */
914static int acpi_bay_match(struct acpi_device *device){
915 acpi_status status;
916 acpi_handle handle;
917 acpi_handle tmp;
918 acpi_handle phandle;
919
920 handle = device->handle;
921
922 status = acpi_get_handle(handle, "_EJ0", &tmp);
923 if (ACPI_FAILURE(status))
924 return -ENODEV;
925
926 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
927 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
928 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
929 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
930 return 0;
931
932 if (acpi_get_parent(handle, &phandle))
933 return -ENODEV;
934
935 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
936 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
937 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
938 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
939 return 0;
940
941 return -ENODEV;
942}
943
Len Brown4be44fc2005-08-05 00:44:28 -0400944static void acpi_device_set_id(struct acpi_device *device,
945 struct acpi_device *parent, acpi_handle handle,
946 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Len Brown4be44fc2005-08-05 00:44:28 -0400948 struct acpi_device_info *info;
949 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
950 char *hid = NULL;
951 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400953 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 switch (type) {
956 case ACPI_BUS_TYPE_DEVICE:
957 status = acpi_get_object_info(handle, &buffer);
958 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400959 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return;
961 }
962
963 info = buffer.pointer;
964 if (info->valid & ACPI_VALID_HID)
965 hid = info->hardware_id.value;
966 if (info->valid & ACPI_VALID_UID)
967 uid = info->unique_id.value;
968 if (info->valid & ACPI_VALID_CID)
969 cid_list = &info->compatibility_id;
970 if (info->valid & ACPI_VALID_ADR) {
971 device->pnp.bus_address = info->address;
972 device->flags.bus_address = 1;
973 }
Zhang Ruiae843332006-12-07 20:57:10 +0800974
975 if(!(info->valid & (ACPI_VALID_HID | ACPI_VALID_CID))){
976 status = acpi_video_bus_match(device);
977 if(ACPI_SUCCESS(status))
978 hid = ACPI_VIDEO_HID;
Zhang Rui54735262007-01-11 02:09:09 -0500979
980 status = acpi_bay_match(device);
981 if (ACPI_SUCCESS(status))
982 hid = ACPI_BAY_HID;
Zhang Ruiae843332006-12-07 20:57:10 +0800983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 break;
985 case ACPI_BUS_TYPE_POWER:
986 hid = ACPI_POWER_HID;
987 break;
988 case ACPI_BUS_TYPE_PROCESSOR:
989 hid = ACPI_PROCESSOR_HID;
990 break;
991 case ACPI_BUS_TYPE_SYSTEM:
992 hid = ACPI_SYSTEM_HID;
993 break;
994 case ACPI_BUS_TYPE_THERMAL:
995 hid = ACPI_THERMAL_HID;
996 break;
997 case ACPI_BUS_TYPE_POWER_BUTTON:
998 hid = ACPI_BUTTON_HID_POWERF;
999 break;
1000 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1001 hid = ACPI_BUTTON_HID_SLEEPF;
1002 break;
1003 }
1004
1005 /*
1006 * \_SB
1007 * ----
1008 * Fix for the system root bus device -- the only root-level device.
1009 */
Bob Moorec51a4de2005-11-17 13:07:00 -05001010 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 hid = ACPI_BUS_HID;
1012 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1013 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1014 }
1015
1016 if (hid) {
1017 strcpy(device->pnp.hardware_id, hid);
1018 device->flags.hardware_id = 1;
1019 }
1020 if (uid) {
1021 strcpy(device->pnp.unique_id, uid);
1022 device->flags.unique_id = 1;
1023 }
1024 if (cid_list) {
1025 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
1026 if (device->pnp.cid_list)
1027 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
1028 else
1029 printk(KERN_ERR "Memory allocation error\n");
1030 }
1031
Len Brown02438d82006-06-30 03:19:10 -04001032 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Len Brown4be44fc2005-08-05 00:44:28 -04001035static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
1037 acpi_status status = AE_OK;
1038 int result = 0;
1039 /*
1040 * Context
1041 * -------
1042 * Attach this 'struct acpi_device' to the ACPI object. This makes
1043 * resolutions from handle->device very efficient. Note that we need
1044 * to be careful with fixed-feature devices as they all attach to the
1045 * root object.
1046 */
Len Brown4be44fc2005-08-05 00:44:28 -04001047 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1049 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001050 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 if (ACPI_FAILURE(status)) {
1053 printk("Error attaching device data\n");
1054 result = -ENODEV;
1055 }
1056 }
1057 return result;
1058}
1059
Len Brown4be44fc2005-08-05 00:44:28 -04001060static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001063 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Li Shaohua96333572006-12-07 20:56:46 +08001065 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001066 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Rui Zhang2786f6e2006-12-21 02:21:13 -05001071 /*
1072 * unbind _ADR-Based Devices when hot removal
1073 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 if (dev->flags.bus_address) {
1075 if ((dev->parent) && (dev->parent->ops.unbind))
1076 dev->parent->ops.unbind(dev);
1077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1079
Patrick Mocheld550d982006-06-27 00:41:40 -04001080 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081}
1082
Rajesh Shah3fb02732005-04-28 00:25:52 -07001083static int
Len Brown4be44fc2005-08-05 00:44:28 -04001084acpi_add_single_object(struct acpi_device **child,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001085 struct acpi_device *parent, acpi_handle handle, int type,
1086 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Len Brown4be44fc2005-08-05 00:44:28 -04001088 int result = 0;
1089 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (!child)
Patrick Mocheld550d982006-06-27 00:41:40 -04001093 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Burman Yan36bcbec2006-12-19 12:56:11 -08001095 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001097 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001098 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 device->handle = handle;
1102 device->parent = parent;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001103 device->bus_ops = *ops; /* workround for not call .start */
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Len Brown4be44fc2005-08-05 00:44:28 -04001106 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 /*
1109 * Flags
1110 * -----
1111 * Get prior to calling acpi_bus_get_status() so we know whether
1112 * or not _STA is present. Note that we only look for object
1113 * handles -- cannot evaluate objects until we know the device is
1114 * present and properly initialized.
1115 */
1116 result = acpi_bus_get_flags(device);
1117 if (result)
1118 goto end;
1119
1120 /*
1121 * Status
1122 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001123 * See if the device is present. We always assume that non-Device
1124 * and non-Processor objects (e.g. thermal zones, power resources,
1125 * etc.) are present, functioning, etc. (at least when parent object
1126 * is present). Note that _STA has a different meaning for some
1127 * objects (e.g. power resources) so we need to be careful how we use
1128 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 */
1130 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001131 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 case ACPI_BUS_TYPE_DEVICE:
1133 result = acpi_bus_get_status(device);
1134 if (ACPI_FAILURE(result) || !device->status.present) {
1135 result = -ENOENT;
1136 goto end;
1137 }
1138 break;
1139 default:
Bjorn Helgaas0c0e8922007-04-25 14:20:58 -04001140 STRUCT_TO_INT(device->status) =
1141 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1142 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 break;
1144 }
1145
1146 /*
1147 * Initialize Device
1148 * -----------------
1149 * TBD: Synch with Core's enumeration/initialization process.
1150 */
1151
1152 /*
1153 * Hardware ID, Unique ID, & Bus Address
1154 * -------------------------------------
1155 */
Len Brown4be44fc2005-08-05 00:44:28 -04001156 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /*
1159 * Power Management
1160 * ----------------
1161 */
1162 if (device->flags.power_manageable) {
1163 result = acpi_bus_get_power_flags(device);
1164 if (result)
1165 goto end;
1166 }
1167
Len Brown4be44fc2005-08-05 00:44:28 -04001168 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 * Wakeup device management
1170 *-----------------------
1171 */
1172 if (device->flags.wake_capable) {
1173 result = acpi_bus_get_wakeup_device_flags(device);
1174 if (result)
1175 goto end;
1176 }
1177
1178 /*
1179 * Performance Management
1180 * ----------------------
1181 */
1182 if (device->flags.performance_manageable) {
1183 result = acpi_bus_get_perf_flags(device);
1184 if (result)
1185 goto end;
1186 }
1187
Len Brown4be44fc2005-08-05 00:44:28 -04001188 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 goto end;
1190
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001191 result = acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001194 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 */
1196 if (device->flags.bus_address) {
1197 if (device->parent && device->parent->ops.bind)
1198 device->parent->ops.bind(device);
1199 }
1200
Len Brown4be44fc2005-08-05 00:44:28 -04001201 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (!result)
1203 *child = device;
1204 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001205 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 kfree(device);
1207 }
1208
Patrick Mocheld550d982006-06-27 00:41:40 -04001209 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Len Brown4be44fc2005-08-05 00:44:28 -04001212static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Len Brown4be44fc2005-08-05 00:44:28 -04001214 acpi_status status = AE_OK;
1215 struct acpi_device *parent = NULL;
1216 struct acpi_device *child = NULL;
1217 acpi_handle phandle = NULL;
1218 acpi_handle chandle = NULL;
1219 acpi_object_type type = 0;
1220 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 if (!start)
Patrick Mocheld550d982006-06-27 00:41:40 -04001224 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 parent = start;
1227 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 /*
1230 * Parse through the ACPI namespace, identify all 'devices', and
1231 * create a new 'struct acpi_device' for each.
1232 */
1233 while ((level > 0) && parent) {
1234
1235 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001236 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /*
1239 * If this scope is exhausted then move our way back up.
1240 */
1241 if (ACPI_FAILURE(status)) {
1242 level--;
1243 chandle = phandle;
1244 acpi_get_parent(phandle, &phandle);
1245 if (parent->parent)
1246 parent = parent->parent;
1247 continue;
1248 }
1249
1250 status = acpi_get_type(chandle, &type);
1251 if (ACPI_FAILURE(status))
1252 continue;
1253
1254 /*
1255 * If this is a scope object then parse it (depth-first).
1256 */
1257 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1258 level++;
1259 phandle = chandle;
1260 chandle = NULL;
1261 continue;
1262 }
1263
1264 /*
1265 * We're only interested in objects that we consider 'devices'.
1266 */
1267 switch (type) {
1268 case ACPI_TYPE_DEVICE:
1269 type = ACPI_BUS_TYPE_DEVICE;
1270 break;
1271 case ACPI_TYPE_PROCESSOR:
1272 type = ACPI_BUS_TYPE_PROCESSOR;
1273 break;
1274 case ACPI_TYPE_THERMAL:
1275 type = ACPI_BUS_TYPE_THERMAL;
1276 break;
1277 case ACPI_TYPE_POWER:
1278 type = ACPI_BUS_TYPE_POWER;
1279 break;
1280 default:
1281 continue;
1282 }
1283
Rajesh Shah3fb02732005-04-28 00:25:52 -07001284 if (ops->acpi_op_add)
1285 status = acpi_add_single_object(&child, parent,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001286 chandle, type, ops);
Len Brown4be44fc2005-08-05 00:44:28 -04001287 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001288 status = acpi_bus_get_device(chandle, &child);
1289
Len Brown4be44fc2005-08-05 00:44:28 -04001290 if (ACPI_FAILURE(status))
1291 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001292
Li Shaohuac4168bf2006-12-07 20:56:41 +08001293 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001294 status = acpi_start_single_object(child);
1295 if (ACPI_FAILURE(status))
1296 continue;
1297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /*
1300 * If the device is present, enabled, and functioning then
1301 * parse its scope (depth-first). Note that we need to
1302 * represent absent devices to facilitate PnP notifications
1303 * -- but only the subtree head (not all of its children,
1304 * which will be enumerated when the parent is inserted).
1305 *
1306 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001307 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 */
1309 if (child->status.present) {
1310 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1311 NULL, NULL);
1312 if (ACPI_SUCCESS(status)) {
1313 level++;
1314 phandle = chandle;
1315 chandle = NULL;
1316 parent = child;
1317 }
1318 }
1319 }
1320
Patrick Mocheld550d982006-06-27 00:41:40 -04001321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Rajesh Shah3fb02732005-04-28 00:25:52 -07001324int
Len Brown4be44fc2005-08-05 00:44:28 -04001325acpi_bus_add(struct acpi_device **child,
1326 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001327{
1328 int result;
1329 struct acpi_bus_ops ops;
1330
Li Shaohuac4168bf2006-12-07 20:56:41 +08001331 memset(&ops, 0, sizeof(ops));
1332 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001333
Li Shaohuac4168bf2006-12-07 20:56:41 +08001334 result = acpi_add_single_object(child, parent, handle, type, &ops);
1335 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001336 result = acpi_bus_scan(*child, &ops);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001337
Patrick Mocheld550d982006-06-27 00:41:40 -04001338 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001339}
Len Brown4be44fc2005-08-05 00:44:28 -04001340
Rajesh Shah3fb02732005-04-28 00:25:52 -07001341EXPORT_SYMBOL(acpi_bus_add);
1342
Len Brown4be44fc2005-08-05 00:44:28 -04001343int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001344{
1345 int result;
1346 struct acpi_bus_ops ops;
1347
Rajesh Shah3fb02732005-04-28 00:25:52 -07001348
1349 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001350 return -EINVAL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001351
1352 result = acpi_start_single_object(device);
1353 if (!result) {
1354 memset(&ops, 0, sizeof(ops));
1355 ops.acpi_op_start = 1;
1356 result = acpi_bus_scan(device, &ops);
1357 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001358 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001359}
Len Brown4be44fc2005-08-05 00:44:28 -04001360
Rajesh Shah3fb02732005-04-28 00:25:52 -07001361EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
Kristen Accardiceaba662006-02-23 17:56:01 -08001363int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Len Brown4be44fc2005-08-05 00:44:28 -04001365 acpi_status status;
1366 struct acpi_device *parent, *child;
1367 acpi_handle phandle, chandle;
1368 acpi_object_type type;
1369 u32 level = 1;
1370 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
Len Brown4be44fc2005-08-05 00:44:28 -04001372 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 phandle = start->handle;
1374 child = chandle = NULL;
1375
1376 while ((level > 0) && parent && (!err)) {
1377 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001378 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /*
1381 * If this scope is exhausted then move our way back up.
1382 */
1383 if (ACPI_FAILURE(status)) {
1384 level--;
1385 chandle = phandle;
1386 acpi_get_parent(phandle, &phandle);
1387 child = parent;
1388 parent = parent->parent;
1389
1390 if (level == 0)
1391 err = acpi_bus_remove(child, rmdevice);
1392 else
1393 err = acpi_bus_remove(child, 1);
1394
1395 continue;
1396 }
1397
1398 status = acpi_get_type(chandle, &type);
1399 if (ACPI_FAILURE(status)) {
1400 continue;
1401 }
1402 /*
1403 * If there is a device corresponding to chandle then
1404 * parse it (depth-first).
1405 */
1406 if (acpi_bus_get_device(chandle, &child) == 0) {
1407 level++;
1408 phandle = chandle;
1409 chandle = NULL;
1410 parent = child;
1411 }
1412 continue;
1413 }
1414 return err;
1415}
Kristen Accardiceaba662006-02-23 17:56:01 -08001416EXPORT_SYMBOL_GPL(acpi_bus_trim);
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Len Brown4be44fc2005-08-05 00:44:28 -04001419static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420{
Len Brown4be44fc2005-08-05 00:44:28 -04001421 int result = 0;
1422 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001423 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -04001426 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Li Shaohuac4168bf2006-12-07 20:56:41 +08001428 memset(&ops, 0, sizeof(ops));
1429 ops.acpi_op_add = 1;
1430 ops.acpi_op_start = 1;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /*
1433 * Enumerate all fixed-feature devices.
1434 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001435 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001436 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001437 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001438 ACPI_BUS_TYPE_POWER_BUTTON,
1439 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001442 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001443 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001444 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001445 ACPI_BUS_TYPE_SLEEP_BUTTON,
1446 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Patrick Mocheld550d982006-06-27 00:41:40 -04001449 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452static int __init acpi_scan_init(void)
1453{
1454 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001455 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
1458 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Li Shaohuac4168bf2006-12-07 20:56:41 +08001461 memset(&ops, 0, sizeof(ops));
1462 ops.acpi_op_add = 1;
1463 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Patrick Mochel5b327262006-05-10 10:33:00 -04001465 result = bus_register(&acpi_bus_type);
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 bus type\n");
1469 }
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 /*
1472 * Create the root device in the bus's device tree
1473 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001474 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001475 ACPI_BUS_TYPE_SYSTEM, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (result)
1477 goto Done;
1478
1479 /*
1480 * Enumerate devices in the ACPI namespace.
1481 */
1482 result = acpi_bus_scan_fixed(acpi_root);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001483 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001484 result = acpi_bus_scan(acpi_root, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 if (result)
1487 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1488
Len Brown4be44fc2005-08-05 00:44:28 -04001489 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -04001490 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
1493subsys_initcall(acpi_scan_init);