blob: be74347d135435e5c5b4508b8144528bc58a0459 [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*/
38int create_modalias(struct acpi_device *acpi_dev, char *modalias, int size){
39
40 int len;
41
42 if (!acpi_dev->flags.hardware_id)
43 return -ENODEV;
44
45 len = snprintf(modalias, size, "acpi:%s:",
46 acpi_dev->pnp.hardware_id);
47 if (len < 0 || len >= size)
48 return -EINVAL;
49 size -= len;
50
51 if (acpi_dev->flags.compatible_ids) {
52 struct acpi_compatible_id_list *cid_list;
53 int i;
54 int count;
55
56 cid_list = acpi_dev->pnp.cid_list;
57 for (i = 0; i < cid_list->count; i++) {
58 count = snprintf(&modalias[len], size, "%s:",
59 cid_list->id[i].value);
60 if (count < 0 || count >= size) {
61 printk(KERN_ERR "acpi: %s cid[%i] exceeds event buffer size",
62 acpi_dev->pnp.device_name, i);
63 break;
64 }
65 len += count;
66 size -= count;
67 }
68 }
69
70 modalias[len] = '\0';
71 return len;
72}
73
74static ssize_t
75acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
76 struct acpi_device *acpi_dev = to_acpi_device(dev);
77 int len;
78
79 /* Device has no HID and no CID or string is >1024 */
80 len = create_modalias(acpi_dev, buf, 1024);
81 if (len <= 0)
82 return 0;
83 buf[len++] = '\n';
84 return len;
85}
86static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
87
Len Brown4be44fc2005-08-05 00:44:28 -040088static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct acpi_object_list arg_list;
91 union acpi_object arg;
92 acpi_status status = AE_OK;
93
94 /*
95 * TBD: evaluate _PS3?
96 */
97
98 if (lockable) {
99 arg_list.count = 1;
100 arg_list.pointer = &arg;
101 arg.type = ACPI_TYPE_INTEGER;
102 arg.integer.value = 0;
103 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
104 }
105
106 arg_list.count = 1;
107 arg_list.pointer = &arg;
108 arg.type = ACPI_TYPE_INTEGER;
109 arg.integer.value = 1;
110
111 /*
112 * TBD: _EJD support.
113 */
114
115 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
116 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400117 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
119
Len Brown4be44fc2005-08-05 00:44:28 -0400120 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800124acpi_eject_store(struct device *d, struct device_attribute *attr,
125 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Len Brown4be44fc2005-08-05 00:44:28 -0400127 int result;
128 int ret = count;
129 int islockable;
130 acpi_status status;
131 acpi_handle handle;
132 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800133 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if ((!count) || (buf[0] != '1')) {
136 return -EINVAL;
137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800139 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 ret = -ENODEV;
141 goto err;
142 }
143#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800144 status = acpi_get_type(acpi_device->handle, &type);
145 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 ret = -ENODEV;
147 goto err;
148 }
149
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800150 islockable = acpi_device->flags.lockable;
151 handle = acpi_device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800153 result = acpi_bus_trim(acpi_device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 if (!result)
156 result = acpi_eject_operation(handle, islockable);
157
158 if (result) {
159 ret = -EBUSY;
160 }
Len Brown4be44fc2005-08-05 00:44:28 -0400161 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800165static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800167static ssize_t
168acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
169 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800171 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
172}
173static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
174
175static ssize_t
176acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
177 struct acpi_device *acpi_dev = to_acpi_device(dev);
178 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
179 int result;
180
181 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
182 if(result)
183 goto end;
184
185 result = sprintf(buf, "%s\n", (char*)path.pointer);
186 kfree(path.pointer);
187 end:
188 return result;
189}
190static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
191
192static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800194 acpi_status status;
195 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800196 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800198 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800199 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800200 */
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800201 if(dev->handle) {
202 result = device_create_file(&dev->dev, &dev_attr_path);
203 if(result)
204 goto end;
205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800207 if(dev->flags.hardware_id) {
208 result = device_create_file(&dev->dev, &dev_attr_hid);
209 if(result)
210 goto end;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Thomas Renninger29b71a12007-07-23 14:43:51 +0200213 if (dev->flags.hardware_id || dev->flags.compatible_ids){
214 result = device_create_file(&dev->dev, &dev_attr_modalias);
215 if(result)
216 goto end;
217 }
218
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800219 /*
220 * If device has _EJ0, 'eject' file is created that is used to trigger
221 * hot-removal function from userland.
222 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800223 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
224 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800225 result = device_create_file(&dev->dev, &dev_attr_eject);
226 end:
227 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800230static void acpi_device_remove_files(struct acpi_device *dev)
231{
232 acpi_status status;
233 acpi_handle temp;
234
235 /*
236 * If device has _EJ0, 'eject' file is created that is used to trigger
237 * hot-removal function from userland.
238 */
239 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
240 if (ACPI_SUCCESS(status))
241 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800242
Thomas Renninger29b71a12007-07-23 14:43:51 +0200243 if (dev->flags.hardware_id || dev->flags.compatible_ids)
244 device_remove_file(&dev->dev, &dev_attr_modalias);
245
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800246 if(dev->flags.hardware_id)
247 device_remove_file(&dev->dev, &dev_attr_hid);
248 if(dev->handle)
249 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800252 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200254
255int acpi_match_device_ids(struct acpi_device *device,
256 const struct acpi_device_id *ids)
257{
258 const struct acpi_device_id *id;
259
260 if (device->flags.hardware_id) {
261 for (id = ids; id->id[0]; id++) {
262 if (!strcmp((char*)id->id, device->pnp.hardware_id))
263 return 0;
264 }
265 }
266
267 if (device->flags.compatible_ids) {
268 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
269 int i;
270
271 for (id = ids; id->id[0]; id++) {
272 /* compare multiple _CID entries against driver ids */
273 for (i = 0; i < cid_list->count; i++) {
274 if (!strcmp((char*)id->id,
275 cid_list->id[i].value))
276 return 0;
277 }
278 }
279 }
280
281 return -ENOENT;
282}
283EXPORT_SYMBOL(acpi_match_device_ids);
284
Patrick Mochel1890a972006-12-07 20:56:31 +0800285static void acpi_device_release(struct device *dev)
286{
287 struct acpi_device *acpi_dev = to_acpi_device(dev);
288
289 kfree(acpi_dev->pnp.cid_list);
290 kfree(acpi_dev);
291}
292
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800293static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800294{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800295 struct acpi_device *acpi_dev = to_acpi_device(dev);
296 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800297
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800298 if (acpi_drv && acpi_drv->ops.suspend)
299 return acpi_drv->ops.suspend(acpi_dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return 0;
301}
302
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800303static int acpi_device_resume(struct device *dev)
304{
305 struct acpi_device *acpi_dev = to_acpi_device(dev);
306 struct acpi_driver *acpi_drv = acpi_dev->driver;
307
308 if (acpi_drv && acpi_drv->ops.resume)
309 return acpi_drv->ops.resume(acpi_dev);
310 return 0;
311}
312
313static int acpi_bus_match(struct device *dev, struct device_driver *drv)
314{
315 struct acpi_device *acpi_dev = to_acpi_device(dev);
316 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
317
Thomas Renninger29b71a12007-07-23 14:43:51 +0200318 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800319}
320
321static int acpi_device_uevent(struct device *dev, char **envp, int num_envp,
Thomas Renninger29b71a12007-07-23 14:43:51 +0200322 char *buffer, int buffer_size)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800323{
324 struct acpi_device *acpi_dev = to_acpi_device(dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800325
Thomas Renninger29b71a12007-07-23 14:43:51 +0200326 strcpy(buffer, "MODALIAS=");
327 if (create_modalias(acpi_dev, buffer + 9, buffer_size - 9) > 0) {
328 envp[0] = buffer;
329 envp[1] = NULL;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return 0;
332}
333
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800334static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
335static int acpi_start_single_object(struct acpi_device *);
336static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800337{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800338 struct acpi_device *acpi_dev = to_acpi_device(dev);
339 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
340 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800342 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
343 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800344 if (acpi_dev->bus_ops.acpi_op_start)
345 acpi_start_single_object(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800346 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
347 "Found driver [%s] for device [%s]\n",
348 acpi_drv->name, acpi_dev->pnp.bus_id));
349 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800350 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800351 return ret;
352}
353
354static int acpi_device_remove(struct device * dev)
355{
356 struct acpi_device *acpi_dev = to_acpi_device(dev);
357 struct acpi_driver *acpi_drv = acpi_dev->driver;
358
359 if (acpi_drv) {
360 if (acpi_drv->ops.stop)
Li Shaohua96333572006-12-07 20:56:46 +0800361 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800362 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800363 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800364 }
365 acpi_dev->driver = NULL;
366 acpi_driver_data(dev) = NULL;
367
368 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800369 return 0;
370}
371
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800372static void acpi_device_shutdown(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800373{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800374 struct acpi_device *acpi_dev = to_acpi_device(dev);
375 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800376
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800377 if (acpi_drv && acpi_drv->ops.shutdown)
378 acpi_drv->ops.shutdown(acpi_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800380 return ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
David Brownell55955aa2007-05-08 00:28:35 -0700383struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800384 .name = "acpi",
385 .suspend = acpi_device_suspend,
386 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800387 .shutdown = acpi_device_shutdown,
388 .match = acpi_bus_match,
389 .probe = acpi_device_probe,
390 .remove = acpi_device_remove,
391 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392};
393
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800394static int acpi_device_register(struct acpi_device *device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct acpi_device *parent)
396{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800397 int result;
398 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
399 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /*
401 * Linkage
402 * -------
403 * Link this device to its parent and siblings.
404 */
405 INIT_LIST_HEAD(&device->children);
406 INIT_LIST_HEAD(&device->node);
407 INIT_LIST_HEAD(&device->g_list);
408 INIT_LIST_HEAD(&device->wakeup_list);
409
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800410 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
411 if (!new_bus_id) {
412 printk(KERN_ERR PREFIX "Memory allocation error\n");
413 return -ENOMEM;
414 }
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 spin_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800417 /*
418 * Find suitable bus_id and instance number in acpi_bus_id_list
419 * If failed, create one and link it into acpi_bus_id_list
420 */
421 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Zhang Ruibb095852007-01-04 15:03:18 +0800422 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800423 acpi_device_bus_id->instance_no ++;
424 found = 1;
425 kfree(new_bus_id);
426 break;
427 }
428 }
429 if(!found) {
430 acpi_device_bus_id = new_bus_id;
Zhang Ruibb095852007-01-04 15:03:18 +0800431 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800432 acpi_device_bus_id->instance_no = 0;
433 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
434 }
435 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (device->parent) {
438 list_add_tail(&device->node, &device->parent->children);
439 list_add_tail(&device->g_list, &device->parent->g_list);
440 } else
441 list_add_tail(&device->g_list, &acpi_device_list);
442 if (device->wakeup.flags.valid)
443 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
444 spin_unlock(&acpi_device_lock);
445
Patrick Mochel1890a972006-12-07 20:56:31 +0800446 if (device->parent)
447 device->dev.parent = &parent->dev;
448 device->dev.bus = &acpi_bus_type;
449 device_initialize(&device->dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800450 device->dev.release = &acpi_device_release;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800451 result = device_add(&device->dev);
452 if(result) {
453 printk("Error adding device %s", device->dev.bus_id);
454 goto end;
455 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800456
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800457 result = acpi_device_setup_files(device);
458 if(result)
459 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
460
Li Shaohua96333572006-12-07 20:56:46 +0800461 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800462 return 0;
463 end:
464 spin_lock(&acpi_device_lock);
465 if (device->parent) {
466 list_del(&device->node);
467 list_del(&device->g_list);
468 } else
469 list_del(&device->g_list);
470 list_del(&device->wakeup_list);
471 spin_unlock(&acpi_device_lock);
472 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475static void acpi_device_unregister(struct acpi_device *device, int type)
476{
477 spin_lock(&acpi_device_lock);
478 if (device->parent) {
479 list_del(&device->node);
480 list_del(&device->g_list);
481 } else
482 list_del(&device->g_list);
483
484 list_del(&device->wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 spin_unlock(&acpi_device_lock);
486
487 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800488
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800489 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800490 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
Zhang Rui9e89dde2006-12-07 20:56:16 +0800493/* --------------------------------------------------------------------------
494 Driver Management
495 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500497 * acpi_bus_driver_init - add a device to a driver
498 * @device: the device to add and initialize
499 * @driver: driver for the device
500 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800502 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 */
504static int
Len Brown4be44fc2005-08-05 00:44:28 -0400505acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Len Brown4be44fc2005-08-05 00:44:28 -0400507 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400511 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400514 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 result = driver->ops.add(device);
517 if (result) {
518 device->driver = NULL;
519 acpi_driver_data(device) = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400520 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
523 device->driver = driver;
524
525 /*
526 * TBD - Configuration Management: Assign resources to device based
527 * upon possible configuration and currently allocated resources.
528 */
529
Len Brown4be44fc2005-08-05 00:44:28 -0400530 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
531 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400532 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700533}
534
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400535static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700536{
537 int result = 0;
538 struct acpi_driver *driver;
539
Rajesh Shah3fb02732005-04-28 00:25:52 -0700540
541 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400542 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (driver->ops.start) {
545 result = driver->ops.start(device);
546 if (result && driver->ops.remove)
547 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
Patrick Mocheld550d982006-06-27 00:41:40 -0400550 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500554 * acpi_bus_register_driver - register a driver with the ACPI bus
555 * @driver: driver being registered
556 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500558 * devices that match the driver's criteria and binds. Returns zero for
559 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
Len Brown4be44fc2005-08-05 00:44:28 -0400561int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Patrick Mochel1890a972006-12-07 20:56:31 +0800563 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400566 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800567 driver->drv.name = driver->name;
568 driver->drv.bus = &acpi_bus_type;
569 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Patrick Mochel1890a972006-12-07 20:56:31 +0800571 ret = driver_register(&driver->drv);
572 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Len Brown4be44fc2005-08-05 00:44:28 -0400575EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500578 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
579 * @driver: driver to unregister
580 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 * Unregisters a driver with the ACPI bus. Searches the namespace for all
582 * devices that match the driver's criteria and unbinds.
583 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400584void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Patrick Mochel1890a972006-12-07 20:56:31 +0800586 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
Len Brown4be44fc2005-08-05 00:44:28 -0400588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589EXPORT_SYMBOL(acpi_bus_unregister_driver);
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591/* --------------------------------------------------------------------------
592 Device Enumeration
593 -------------------------------------------------------------------------- */
Len Brownc8f7a622006-07-09 17:22:28 -0400594acpi_status
595acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
596{
597 acpi_status status;
598 acpi_handle tmp;
599 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
600 union acpi_object *obj;
601
602 status = acpi_get_handle(handle, "_EJD", &tmp);
603 if (ACPI_FAILURE(status))
604 return status;
605
606 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
607 if (ACPI_SUCCESS(status)) {
608 obj = buffer.pointer;
609 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
610 kfree(buffer.pointer);
611 }
612 return status;
613}
614EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
617{
618
619 /* TBD */
620
621 return;
622}
623
Zhang Rui9e89dde2006-12-07 20:56:16 +0800624static int acpi_bus_get_perf_flags(struct acpi_device *device)
625{
626 device->performance.state = ACPI_STATE_UNKNOWN;
627 return 0;
628}
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630static acpi_status
631acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
632 union acpi_object *package)
633{
634 int i = 0;
635 union acpi_object *element = NULL;
636
637 if (!device || !package || (package->package.count < 2))
638 return AE_BAD_PARAMETER;
639
640 element = &(package->package.elements[0]);
641 if (!element)
642 return AE_BAD_PARAMETER;
643 if (element->type == ACPI_TYPE_PACKAGE) {
644 if ((element->package.count < 2) ||
645 (element->package.elements[0].type !=
646 ACPI_TYPE_LOCAL_REFERENCE)
647 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
648 return AE_BAD_DATA;
649 device->wakeup.gpe_device =
650 element->package.elements[0].reference.handle;
651 device->wakeup.gpe_number =
652 (u32) element->package.elements[1].integer.value;
653 } else if (element->type == ACPI_TYPE_INTEGER) {
654 device->wakeup.gpe_number = element->integer.value;
655 } else
656 return AE_BAD_DATA;
657
658 element = &(package->package.elements[1]);
659 if (element->type != ACPI_TYPE_INTEGER) {
660 return AE_BAD_DATA;
661 }
662 device->wakeup.sleep_state = element->integer.value;
663
664 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
665 return AE_NO_MEMORY;
666 }
667 device->wakeup.resources.count = package->package.count - 2;
668 for (i = 0; i < device->wakeup.resources.count; i++) {
669 element = &(package->package.elements[i + 2]);
670 if (element->type != ACPI_TYPE_ANY) {
671 return AE_BAD_DATA;
672 }
673
674 device->wakeup.resources.handles[i] = element->reference.handle;
675 }
676
677 return AE_OK;
678}
679
680static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
681{
682 acpi_status status = 0;
683 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
684 union acpi_object *package = NULL;
685
Thomas Renninger29b71a12007-07-23 14:43:51 +0200686 struct acpi_device_id button_device_ids[] = {
687 {"PNP0C0D", 0},
688 {"PNP0C0C", 0},
689 {"PNP0C0E", 0},
690 {"", 0},
691 };
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* _PRW */
695 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
696 if (ACPI_FAILURE(status)) {
697 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
698 goto end;
699 }
700
701 package = (union acpi_object *)buffer.pointer;
702 status = acpi_bus_extract_wakeup_device_power_package(device, package);
703 if (ACPI_FAILURE(status)) {
704 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
705 goto end;
706 }
707
708 kfree(buffer.pointer);
709
710 device->wakeup.flags.valid = 1;
711 /* Power button, Lid switch always enable wakeup */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200712 if (!acpi_match_device_ids(device, button_device_ids))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 device->wakeup.flags.run_wake = 1;
714
715 end:
716 if (ACPI_FAILURE(status))
717 device->flags.wake_capable = 0;
718 return 0;
719}
720
Zhang Rui9e89dde2006-12-07 20:56:16 +0800721static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800723 acpi_status status = 0;
724 acpi_handle handle = NULL;
725 u32 i = 0;
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800729 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800731 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800733 device->power.flags.explicit_get = 1;
734 status = acpi_get_handle(device->handle, "_IRC", &handle);
735 if (ACPI_SUCCESS(status))
736 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800739 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800741 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
742 struct acpi_device_power_state *ps = &device->power.states[i];
743 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Zhang Rui9e89dde2006-12-07 20:56:16 +0800745 /* Evaluate "_PRx" to se if power resources are referenced */
746 acpi_evaluate_reference(device->handle, object_name, NULL,
747 &ps->resources);
748 if (ps->resources.count) {
749 device->power.flags.power_resources = 1;
750 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Zhang Rui9e89dde2006-12-07 20:56:16 +0800753 /* Evaluate "_PSx" to see if we can do explicit sets */
754 object_name[2] = 'S';
755 status = acpi_get_handle(device->handle, object_name, &handle);
756 if (ACPI_SUCCESS(status)) {
757 ps->flags.explicit_set = 1;
758 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800760
761 /* State is valid if we have some power control */
762 if (ps->resources.count || ps->flags.explicit_set)
763 ps->flags.valid = 1;
764
765 ps->power = -1; /* Unknown - driver assigned */
766 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Zhang Rui9e89dde2006-12-07 20:56:16 +0800769 /* Set defaults for D0 and D3 states (always valid) */
770 device->power.states[ACPI_STATE_D0].flags.valid = 1;
771 device->power.states[ACPI_STATE_D0].power = 100;
772 device->power.states[ACPI_STATE_D3].flags.valid = 1;
773 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Zhang Rui9e89dde2006-12-07 20:56:16 +0800775 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Zhang Rui9e89dde2006-12-07 20:56:16 +0800777 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 return 0;
780}
781
Len Brown4be44fc2005-08-05 00:44:28 -0400782static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
Len Brown4be44fc2005-08-05 00:44:28 -0400784 acpi_status status = AE_OK;
785 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 /* Presence of _STA indicates 'dynamic_status' */
789 status = acpi_get_handle(device->handle, "_STA", &temp);
790 if (ACPI_SUCCESS(status))
791 device->flags.dynamic_status = 1;
792
793 /* Presence of _CID indicates 'compatible_ids' */
794 status = acpi_get_handle(device->handle, "_CID", &temp);
795 if (ACPI_SUCCESS(status))
796 device->flags.compatible_ids = 1;
797
798 /* Presence of _RMV indicates 'removable' */
799 status = acpi_get_handle(device->handle, "_RMV", &temp);
800 if (ACPI_SUCCESS(status))
801 device->flags.removable = 1;
802
803 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
804 status = acpi_get_handle(device->handle, "_EJD", &temp);
805 if (ACPI_SUCCESS(status))
806 device->flags.ejectable = 1;
807 else {
808 status = acpi_get_handle(device->handle, "_EJ0", &temp);
809 if (ACPI_SUCCESS(status))
810 device->flags.ejectable = 1;
811 }
812
813 /* Presence of _LCK indicates 'lockable' */
814 status = acpi_get_handle(device->handle, "_LCK", &temp);
815 if (ACPI_SUCCESS(status))
816 device->flags.lockable = 1;
817
818 /* Presence of _PS0|_PR0 indicates 'power manageable' */
819 status = acpi_get_handle(device->handle, "_PS0", &temp);
820 if (ACPI_FAILURE(status))
821 status = acpi_get_handle(device->handle, "_PR0", &temp);
822 if (ACPI_SUCCESS(status))
823 device->flags.power_manageable = 1;
824
825 /* Presence of _PRW indicates wake capable */
826 status = acpi_get_handle(device->handle, "_PRW", &temp);
827 if (ACPI_SUCCESS(status))
828 device->flags.wake_capable = 1;
829
830 /* TBD: Peformance management */
831
Patrick Mocheld550d982006-06-27 00:41:40 -0400832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Len Brown4be44fc2005-08-05 00:44:28 -0400835static void acpi_device_get_busid(struct acpi_device *device,
836 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Len Brown4be44fc2005-08-05 00:44:28 -0400838 char bus_id[5] = { '?', 0 };
839 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
840 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 /*
843 * Bus ID
844 * ------
845 * The device's Bus ID is simply the object name.
846 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
847 */
848 switch (type) {
849 case ACPI_BUS_TYPE_SYSTEM:
850 strcpy(device->pnp.bus_id, "ACPI");
851 break;
852 case ACPI_BUS_TYPE_POWER_BUTTON:
853 strcpy(device->pnp.bus_id, "PWRF");
854 break;
855 case ACPI_BUS_TYPE_SLEEP_BUTTON:
856 strcpy(device->pnp.bus_id, "SLPF");
857 break;
858 default:
859 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
860 /* Clean up trailing underscores (if any) */
861 for (i = 3; i > 1; i--) {
862 if (bus_id[i] == '_')
863 bus_id[i] = '\0';
864 else
865 break;
866 }
867 strcpy(device->pnp.bus_id, bus_id);
868 break;
869 }
870}
871
Zhang Ruiae843332006-12-07 20:57:10 +0800872static int
873acpi_video_bus_match(struct acpi_device *device)
874{
875 acpi_handle h_dummy1;
876 acpi_handle h_dummy2;
877 acpi_handle h_dummy3;
878
879
880 if (!device)
881 return -EINVAL;
882
883 /* Since there is no HID, CID for ACPI Video drivers, we have
884 * to check well known required nodes for each feature we support.
885 */
886
887 /* Does this device able to support video switching ? */
888 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
889 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
890 return 0;
891
892 /* Does this device able to retrieve a video ROM ? */
893 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
894 return 0;
895
896 /* Does this device able to configure which video head to be POSTed ? */
897 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
898 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
899 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
900 return 0;
901
902 return -ENODEV;
903}
904
Zhang Rui54735262007-01-11 02:09:09 -0500905/*
906 * acpi_bay_match - see if a device is an ejectable driver bay
907 *
908 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
909 * then we can safely call it an ejectable drive bay
910 */
911static int acpi_bay_match(struct acpi_device *device){
912 acpi_status status;
913 acpi_handle handle;
914 acpi_handle tmp;
915 acpi_handle phandle;
916
917 handle = device->handle;
918
919 status = acpi_get_handle(handle, "_EJ0", &tmp);
920 if (ACPI_FAILURE(status))
921 return -ENODEV;
922
923 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
924 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
925 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
926 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
927 return 0;
928
929 if (acpi_get_parent(handle, &phandle))
930 return -ENODEV;
931
932 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
933 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
934 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
935 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
936 return 0;
937
938 return -ENODEV;
939}
940
Len Brown4be44fc2005-08-05 00:44:28 -0400941static void acpi_device_set_id(struct acpi_device *device,
942 struct acpi_device *parent, acpi_handle handle,
943 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Len Brown4be44fc2005-08-05 00:44:28 -0400945 struct acpi_device_info *info;
946 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
947 char *hid = NULL;
948 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400950 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 switch (type) {
953 case ACPI_BUS_TYPE_DEVICE:
954 status = acpi_get_object_info(handle, &buffer);
955 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400956 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return;
958 }
959
960 info = buffer.pointer;
961 if (info->valid & ACPI_VALID_HID)
962 hid = info->hardware_id.value;
963 if (info->valid & ACPI_VALID_UID)
964 uid = info->unique_id.value;
965 if (info->valid & ACPI_VALID_CID)
966 cid_list = &info->compatibility_id;
967 if (info->valid & ACPI_VALID_ADR) {
968 device->pnp.bus_address = info->address;
969 device->flags.bus_address = 1;
970 }
Zhang Ruiae843332006-12-07 20:57:10 +0800971
972 if(!(info->valid & (ACPI_VALID_HID | ACPI_VALID_CID))){
973 status = acpi_video_bus_match(device);
974 if(ACPI_SUCCESS(status))
975 hid = ACPI_VIDEO_HID;
Zhang Rui54735262007-01-11 02:09:09 -0500976
977 status = acpi_bay_match(device);
978 if (ACPI_SUCCESS(status))
979 hid = ACPI_BAY_HID;
Zhang Ruiae843332006-12-07 20:57:10 +0800980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 break;
982 case ACPI_BUS_TYPE_POWER:
983 hid = ACPI_POWER_HID;
984 break;
985 case ACPI_BUS_TYPE_PROCESSOR:
986 hid = ACPI_PROCESSOR_HID;
987 break;
988 case ACPI_BUS_TYPE_SYSTEM:
989 hid = ACPI_SYSTEM_HID;
990 break;
991 case ACPI_BUS_TYPE_THERMAL:
992 hid = ACPI_THERMAL_HID;
993 break;
994 case ACPI_BUS_TYPE_POWER_BUTTON:
995 hid = ACPI_BUTTON_HID_POWERF;
996 break;
997 case ACPI_BUS_TYPE_SLEEP_BUTTON:
998 hid = ACPI_BUTTON_HID_SLEEPF;
999 break;
1000 }
1001
1002 /*
1003 * \_SB
1004 * ----
1005 * Fix for the system root bus device -- the only root-level device.
1006 */
Bob Moorec51a4de2005-11-17 13:07:00 -05001007 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 hid = ACPI_BUS_HID;
1009 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1010 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1011 }
1012
1013 if (hid) {
1014 strcpy(device->pnp.hardware_id, hid);
1015 device->flags.hardware_id = 1;
1016 }
1017 if (uid) {
1018 strcpy(device->pnp.unique_id, uid);
1019 device->flags.unique_id = 1;
1020 }
1021 if (cid_list) {
1022 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
1023 if (device->pnp.cid_list)
1024 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
1025 else
1026 printk(KERN_ERR "Memory allocation error\n");
1027 }
1028
Len Brown02438d82006-06-30 03:19:10 -04001029 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Len Brown4be44fc2005-08-05 00:44:28 -04001032static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
1034 acpi_status status = AE_OK;
1035 int result = 0;
1036 /*
1037 * Context
1038 * -------
1039 * Attach this 'struct acpi_device' to the ACPI object. This makes
1040 * resolutions from handle->device very efficient. Note that we need
1041 * to be careful with fixed-feature devices as they all attach to the
1042 * root object.
1043 */
Len Brown4be44fc2005-08-05 00:44:28 -04001044 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1046 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001047 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 if (ACPI_FAILURE(status)) {
1050 printk("Error attaching device data\n");
1051 result = -ENODEV;
1052 }
1053 }
1054 return result;
1055}
1056
Len Brown4be44fc2005-08-05 00:44:28 -04001057static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001060 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Li Shaohua96333572006-12-07 20:56:46 +08001062 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001063 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Rui Zhang2786f6e2006-12-21 02:21:13 -05001068 /*
1069 * unbind _ADR-Based Devices when hot removal
1070 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (dev->flags.bus_address) {
1072 if ((dev->parent) && (dev->parent->ops.unbind))
1073 dev->parent->ops.unbind(dev);
1074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1076
Patrick Mocheld550d982006-06-27 00:41:40 -04001077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Rajesh Shah3fb02732005-04-28 00:25:52 -07001080static int
Len Brown4be44fc2005-08-05 00:44:28 -04001081acpi_add_single_object(struct acpi_device **child,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001082 struct acpi_device *parent, acpi_handle handle, int type,
1083 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Len Brown4be44fc2005-08-05 00:44:28 -04001085 int result = 0;
1086 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 if (!child)
Patrick Mocheld550d982006-06-27 00:41:40 -04001090 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Burman Yan36bcbec2006-12-19 12:56:11 -08001092 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001094 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001095 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 device->handle = handle;
1099 device->parent = parent;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001100 device->bus_ops = *ops; /* workround for not call .start */
1101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Len Brown4be44fc2005-08-05 00:44:28 -04001103 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 /*
1106 * Flags
1107 * -----
1108 * Get prior to calling acpi_bus_get_status() so we know whether
1109 * or not _STA is present. Note that we only look for object
1110 * handles -- cannot evaluate objects until we know the device is
1111 * present and properly initialized.
1112 */
1113 result = acpi_bus_get_flags(device);
1114 if (result)
1115 goto end;
1116
1117 /*
1118 * Status
1119 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001120 * See if the device is present. We always assume that non-Device
1121 * and non-Processor objects (e.g. thermal zones, power resources,
1122 * etc.) are present, functioning, etc. (at least when parent object
1123 * is present). Note that _STA has a different meaning for some
1124 * objects (e.g. power resources) so we need to be careful how we use
1125 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 */
1127 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001128 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 case ACPI_BUS_TYPE_DEVICE:
1130 result = acpi_bus_get_status(device);
1131 if (ACPI_FAILURE(result) || !device->status.present) {
1132 result = -ENOENT;
1133 goto end;
1134 }
1135 break;
1136 default:
Bjorn Helgaas0c0e8922007-04-25 14:20:58 -04001137 STRUCT_TO_INT(device->status) =
1138 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1139 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 break;
1141 }
1142
1143 /*
1144 * Initialize Device
1145 * -----------------
1146 * TBD: Synch with Core's enumeration/initialization process.
1147 */
1148
1149 /*
1150 * Hardware ID, Unique ID, & Bus Address
1151 * -------------------------------------
1152 */
Len Brown4be44fc2005-08-05 00:44:28 -04001153 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 /*
1156 * Power Management
1157 * ----------------
1158 */
1159 if (device->flags.power_manageable) {
1160 result = acpi_bus_get_power_flags(device);
1161 if (result)
1162 goto end;
1163 }
1164
Len Brown4be44fc2005-08-05 00:44:28 -04001165 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 * Wakeup device management
1167 *-----------------------
1168 */
1169 if (device->flags.wake_capable) {
1170 result = acpi_bus_get_wakeup_device_flags(device);
1171 if (result)
1172 goto end;
1173 }
1174
1175 /*
1176 * Performance Management
1177 * ----------------------
1178 */
1179 if (device->flags.performance_manageable) {
1180 result = acpi_bus_get_perf_flags(device);
1181 if (result)
1182 goto end;
1183 }
1184
Len Brown4be44fc2005-08-05 00:44:28 -04001185 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 goto end;
1187
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001188 result = acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001191 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 */
1193 if (device->flags.bus_address) {
1194 if (device->parent && device->parent->ops.bind)
1195 device->parent->ops.bind(device);
1196 }
1197
Len Brown4be44fc2005-08-05 00:44:28 -04001198 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 if (!result)
1200 *child = device;
1201 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001202 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 kfree(device);
1204 }
1205
Patrick Mocheld550d982006-06-27 00:41:40 -04001206 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Len Brown4be44fc2005-08-05 00:44:28 -04001209static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Len Brown4be44fc2005-08-05 00:44:28 -04001211 acpi_status status = AE_OK;
1212 struct acpi_device *parent = NULL;
1213 struct acpi_device *child = NULL;
1214 acpi_handle phandle = NULL;
1215 acpi_handle chandle = NULL;
1216 acpi_object_type type = 0;
1217 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 if (!start)
Patrick Mocheld550d982006-06-27 00:41:40 -04001221 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223 parent = start;
1224 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 /*
1227 * Parse through the ACPI namespace, identify all 'devices', and
1228 * create a new 'struct acpi_device' for each.
1229 */
1230 while ((level > 0) && parent) {
1231
1232 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001233 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /*
1236 * If this scope is exhausted then move our way back up.
1237 */
1238 if (ACPI_FAILURE(status)) {
1239 level--;
1240 chandle = phandle;
1241 acpi_get_parent(phandle, &phandle);
1242 if (parent->parent)
1243 parent = parent->parent;
1244 continue;
1245 }
1246
1247 status = acpi_get_type(chandle, &type);
1248 if (ACPI_FAILURE(status))
1249 continue;
1250
1251 /*
1252 * If this is a scope object then parse it (depth-first).
1253 */
1254 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1255 level++;
1256 phandle = chandle;
1257 chandle = NULL;
1258 continue;
1259 }
1260
1261 /*
1262 * We're only interested in objects that we consider 'devices'.
1263 */
1264 switch (type) {
1265 case ACPI_TYPE_DEVICE:
1266 type = ACPI_BUS_TYPE_DEVICE;
1267 break;
1268 case ACPI_TYPE_PROCESSOR:
1269 type = ACPI_BUS_TYPE_PROCESSOR;
1270 break;
1271 case ACPI_TYPE_THERMAL:
1272 type = ACPI_BUS_TYPE_THERMAL;
1273 break;
1274 case ACPI_TYPE_POWER:
1275 type = ACPI_BUS_TYPE_POWER;
1276 break;
1277 default:
1278 continue;
1279 }
1280
Rajesh Shah3fb02732005-04-28 00:25:52 -07001281 if (ops->acpi_op_add)
1282 status = acpi_add_single_object(&child, parent,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001283 chandle, type, ops);
Len Brown4be44fc2005-08-05 00:44:28 -04001284 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001285 status = acpi_bus_get_device(chandle, &child);
1286
Len Brown4be44fc2005-08-05 00:44:28 -04001287 if (ACPI_FAILURE(status))
1288 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001289
Li Shaohuac4168bf2006-12-07 20:56:41 +08001290 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001291 status = acpi_start_single_object(child);
1292 if (ACPI_FAILURE(status))
1293 continue;
1294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /*
1297 * If the device is present, enabled, and functioning then
1298 * parse its scope (depth-first). Note that we need to
1299 * represent absent devices to facilitate PnP notifications
1300 * -- but only the subtree head (not all of its children,
1301 * which will be enumerated when the parent is inserted).
1302 *
1303 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001304 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 */
1306 if (child->status.present) {
1307 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1308 NULL, NULL);
1309 if (ACPI_SUCCESS(status)) {
1310 level++;
1311 phandle = chandle;
1312 chandle = NULL;
1313 parent = child;
1314 }
1315 }
1316 }
1317
Patrick Mocheld550d982006-06-27 00:41:40 -04001318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Rajesh Shah3fb02732005-04-28 00:25:52 -07001321int
Len Brown4be44fc2005-08-05 00:44:28 -04001322acpi_bus_add(struct acpi_device **child,
1323 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001324{
1325 int result;
1326 struct acpi_bus_ops ops;
1327
Li Shaohuac4168bf2006-12-07 20:56:41 +08001328 memset(&ops, 0, sizeof(ops));
1329 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001330
Li Shaohuac4168bf2006-12-07 20:56:41 +08001331 result = acpi_add_single_object(child, parent, handle, type, &ops);
1332 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001333 result = acpi_bus_scan(*child, &ops);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001334
Patrick Mocheld550d982006-06-27 00:41:40 -04001335 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001336}
Len Brown4be44fc2005-08-05 00:44:28 -04001337
Rajesh Shah3fb02732005-04-28 00:25:52 -07001338EXPORT_SYMBOL(acpi_bus_add);
1339
Len Brown4be44fc2005-08-05 00:44:28 -04001340int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001341{
1342 int result;
1343 struct acpi_bus_ops ops;
1344
Rajesh Shah3fb02732005-04-28 00:25:52 -07001345
1346 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001347 return -EINVAL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001348
1349 result = acpi_start_single_object(device);
1350 if (!result) {
1351 memset(&ops, 0, sizeof(ops));
1352 ops.acpi_op_start = 1;
1353 result = acpi_bus_scan(device, &ops);
1354 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001355 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001356}
Len Brown4be44fc2005-08-05 00:44:28 -04001357
Rajesh Shah3fb02732005-04-28 00:25:52 -07001358EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Kristen Accardiceaba662006-02-23 17:56:01 -08001360int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361{
Len Brown4be44fc2005-08-05 00:44:28 -04001362 acpi_status status;
1363 struct acpi_device *parent, *child;
1364 acpi_handle phandle, chandle;
1365 acpi_object_type type;
1366 u32 level = 1;
1367 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Len Brown4be44fc2005-08-05 00:44:28 -04001369 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 phandle = start->handle;
1371 child = chandle = NULL;
1372
1373 while ((level > 0) && parent && (!err)) {
1374 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001375 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 /*
1378 * If this scope is exhausted then move our way back up.
1379 */
1380 if (ACPI_FAILURE(status)) {
1381 level--;
1382 chandle = phandle;
1383 acpi_get_parent(phandle, &phandle);
1384 child = parent;
1385 parent = parent->parent;
1386
1387 if (level == 0)
1388 err = acpi_bus_remove(child, rmdevice);
1389 else
1390 err = acpi_bus_remove(child, 1);
1391
1392 continue;
1393 }
1394
1395 status = acpi_get_type(chandle, &type);
1396 if (ACPI_FAILURE(status)) {
1397 continue;
1398 }
1399 /*
1400 * If there is a device corresponding to chandle then
1401 * parse it (depth-first).
1402 */
1403 if (acpi_bus_get_device(chandle, &child) == 0) {
1404 level++;
1405 phandle = chandle;
1406 chandle = NULL;
1407 parent = child;
1408 }
1409 continue;
1410 }
1411 return err;
1412}
Kristen Accardiceaba662006-02-23 17:56:01 -08001413EXPORT_SYMBOL_GPL(acpi_bus_trim);
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Len Brown4be44fc2005-08-05 00:44:28 -04001416static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Len Brown4be44fc2005-08-05 00:44:28 -04001418 int result = 0;
1419 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001420 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
1422 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -04001423 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Li Shaohuac4168bf2006-12-07 20:56:41 +08001425 memset(&ops, 0, sizeof(ops));
1426 ops.acpi_op_add = 1;
1427 ops.acpi_op_start = 1;
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 /*
1430 * Enumerate all fixed-feature devices.
1431 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001432 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001433 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001434 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001435 ACPI_BUS_TYPE_POWER_BUTTON,
1436 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001439 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001440 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001441 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001442 ACPI_BUS_TYPE_SLEEP_BUTTON,
1443 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Patrick Mocheld550d982006-06-27 00:41:40 -04001446 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449static int __init acpi_scan_init(void)
1450{
1451 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001452 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Li Shaohuac4168bf2006-12-07 20:56:41 +08001458 memset(&ops, 0, sizeof(ops));
1459 ops.acpi_op_add = 1;
1460 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Patrick Mochel5b327262006-05-10 10:33:00 -04001462 result = bus_register(&acpi_bus_type);
1463 if (result) {
1464 /* We don't want to quit even if we failed to add suspend/resume */
1465 printk(KERN_ERR PREFIX "Could not register bus type\n");
1466 }
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 /*
1469 * Create the root device in the bus's device tree
1470 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001471 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001472 ACPI_BUS_TYPE_SYSTEM, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (result)
1474 goto Done;
1475
1476 /*
1477 * Enumerate devices in the ACPI namespace.
1478 */
1479 result = acpi_bus_scan_fixed(acpi_root);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001480 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001481 result = acpi_bus_scan(acpi_root, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
1483 if (result)
1484 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1485
Len Brown4be44fc2005-08-05 00:44:28 -04001486 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -04001487 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488}
1489
1490subsys_initcall(acpi_scan_init);