blob: 9a84ed250d9f76fcc7df01fadbafacc71a770fda [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>
Zhang Rui26d46862008-04-29 02:35:48 -04009#include <asm/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
11#include <acpi/acpi_drivers.h>
12#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050015ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040017extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020020#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#define ACPI_BUS_DEVICE_NAME "System Bus"
22
23static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2d2006-12-08 17:23:43 +080024static LIST_HEAD(acpi_bus_id_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025DEFINE_SPINLOCK(acpi_device_lock);
26LIST_HEAD(acpi_wakeup_device_list);
27
Zhang Ruie49bd2d2006-12-08 17:23:43 +080028struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080029 char bus_id[15];
Zhang Ruie49bd2d2006-12-08 17:23:43 +080030 unsigned int instance_no;
31 struct list_head node;
32};
Thomas Renninger29b71a12007-07-23 14:43:51 +020033
34/*
35 * Creates hid/cid(s) string needed for modalias and uevent
36 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
37 * char *modalias: "acpi:IBM0001:ACPI0001"
38*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020039static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
40 int size)
41{
Thomas Renninger29b71a12007-07-23 14:43:51 +020042 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080043 int count;
Thomas Renninger29b71a12007-07-23 14:43:51 +020044
Zhang Rui5c9fcb52008-03-20 16:40:32 +080045 if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +020046 return -ENODEV;
47
Zhang Rui5c9fcb52008-03-20 16:40:32 +080048 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020049 size -= len;
50
Zhang Rui5c9fcb52008-03-20 16:40:32 +080051 if (acpi_dev->flags.hardware_id) {
52 count = snprintf(&modalias[len], size, "%s:",
53 acpi_dev->pnp.hardware_id);
54 if (count < 0 || count >= size)
55 return -EINVAL;
56 len += count;
57 size -= count;
58 }
59
Thomas Renninger29b71a12007-07-23 14:43:51 +020060 if (acpi_dev->flags.compatible_ids) {
61 struct acpi_compatible_id_list *cid_list;
62 int i;
Thomas Renninger29b71a12007-07-23 14:43:51 +020063
64 cid_list = acpi_dev->pnp.cid_list;
65 for (i = 0; i < cid_list->count; i++) {
66 count = snprintf(&modalias[len], size, "%s:",
67 cid_list->id[i].value);
68 if (count < 0 || count >= size) {
Len Brown87ecd5c2008-01-01 14:00:00 -050069 printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
Thomas Renninger29b71a12007-07-23 14:43:51 +020070 acpi_dev->pnp.device_name, i);
71 break;
72 }
73 len += count;
74 size -= count;
75 }
76 }
77
78 modalias[len] = '\0';
79 return len;
80}
81
82static ssize_t
83acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
84 struct acpi_device *acpi_dev = to_acpi_device(dev);
85 int len;
86
87 /* Device has no HID and no CID or string is >1024 */
88 len = create_modalias(acpi_dev, buf, 1024);
89 if (len <= 0)
90 return 0;
91 buf[len++] = '\n';
92 return len;
93}
94static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
95
Zhang Rui26d46862008-04-29 02:35:48 -040096static int acpi_bus_hot_remove_device(void *context)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Zhang Rui26d46862008-04-29 02:35:48 -040098 struct acpi_device *device;
99 acpi_handle handle = context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct acpi_object_list arg_list;
101 union acpi_object arg;
102 acpi_status status = AE_OK;
103
Zhang Rui26d46862008-04-29 02:35:48 -0400104 if (acpi_bus_get_device(handle, &device))
105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Zhang Rui26d46862008-04-29 02:35:48 -0400107 if (!device)
108 return 0;
109
110 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
111 "Hot-removing device %s...\n", device->dev.bus_id));
112
113
114 if (acpi_bus_trim(device, 1)) {
115 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
116 "Removing device failed\n"));
117 return -1;
118 }
119
120 /* power off device */
121 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
122 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
123 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
124 "Power-off device failed\n"));
125
126 if (device->flags.lockable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 arg_list.count = 1;
128 arg_list.pointer = &arg;
129 arg.type = ACPI_TYPE_INTEGER;
130 arg.integer.value = 0;
131 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
132 }
133
134 arg_list.count = 1;
135 arg_list.pointer = &arg;
136 arg.type = ACPI_TYPE_INTEGER;
137 arg.integer.value = 1;
138
139 /*
140 * TBD: _EJD support.
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
Zhang Rui26d46862008-04-29 02:35:48 -0400143 if (ACPI_FAILURE(status))
144 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Zhang Rui26d46862008-04-29 02:35:48 -0400146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800150acpi_eject_store(struct device *d, struct device_attribute *attr,
151 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Len Brown4be44fc2005-08-05 00:44:28 -0400153 int ret = count;
Len Brown4be44fc2005-08-05 00:44:28 -0400154 acpi_status status;
Len Brown4be44fc2005-08-05 00:44:28 -0400155 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800156 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 if ((!count) || (buf[0] != '1')) {
159 return -EINVAL;
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800162 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 ret = -ENODEV;
164 goto err;
165 }
166#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800167 status = acpi_get_type(acpi_device->handle, &type);
168 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 ret = -ENODEV;
170 goto err;
171 }
172
Zhang Rui26d46862008-04-29 02:35:48 -0400173 /* remove the device in another thread to fix the deadlock issue */
174 ret = kernel_thread(acpi_bus_hot_remove_device,
175 acpi_device->handle, SIGCHLD);
Len Brown4be44fc2005-08-05 00:44:28 -0400176 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800180static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800182static ssize_t
183acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
184 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800186 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
187}
188static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
189
190static ssize_t
191acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
192 struct acpi_device *acpi_dev = to_acpi_device(dev);
193 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
194 int result;
195
196 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
197 if(result)
198 goto end;
199
200 result = sprintf(buf, "%s\n", (char*)path.pointer);
201 kfree(path.pointer);
202 end:
203 return result;
204}
205static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
206
207static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800209 acpi_status status;
210 acpi_handle temp;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800211 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800213 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800214 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800215 */
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800216 if(dev->handle) {
217 result = device_create_file(&dev->dev, &dev_attr_path);
218 if(result)
219 goto end;
220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800222 if(dev->flags.hardware_id) {
223 result = device_create_file(&dev->dev, &dev_attr_hid);
224 if(result)
225 goto end;
226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Thomas Renninger29b71a12007-07-23 14:43:51 +0200228 if (dev->flags.hardware_id || dev->flags.compatible_ids){
229 result = device_create_file(&dev->dev, &dev_attr_modalias);
230 if(result)
231 goto end;
232 }
233
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800234 /*
235 * If device has _EJ0, 'eject' file is created that is used to trigger
236 * hot-removal function from userland.
237 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800238 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
239 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800240 result = device_create_file(&dev->dev, &dev_attr_eject);
241 end:
242 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800243}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800245static void acpi_device_remove_files(struct acpi_device *dev)
246{
247 acpi_status status;
248 acpi_handle temp;
249
250 /*
251 * If device has _EJ0, 'eject' file is created that is used to trigger
252 * hot-removal function from userland.
253 */
254 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
255 if (ACPI_SUCCESS(status))
256 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800257
Thomas Renninger29b71a12007-07-23 14:43:51 +0200258 if (dev->flags.hardware_id || dev->flags.compatible_ids)
259 device_remove_file(&dev->dev, &dev_attr_modalias);
260
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800261 if(dev->flags.hardware_id)
262 device_remove_file(&dev->dev, &dev_attr_hid);
263 if(dev->handle)
264 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800265}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800267 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200269
270int acpi_match_device_ids(struct acpi_device *device,
271 const struct acpi_device_id *ids)
272{
273 const struct acpi_device_id *id;
274
275 if (device->flags.hardware_id) {
276 for (id = ids; id->id[0]; id++) {
277 if (!strcmp((char*)id->id, device->pnp.hardware_id))
278 return 0;
279 }
280 }
281
282 if (device->flags.compatible_ids) {
283 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
284 int i;
285
286 for (id = ids; id->id[0]; id++) {
287 /* compare multiple _CID entries against driver ids */
288 for (i = 0; i < cid_list->count; i++) {
289 if (!strcmp((char*)id->id,
290 cid_list->id[i].value))
291 return 0;
292 }
293 }
294 }
295
296 return -ENOENT;
297}
298EXPORT_SYMBOL(acpi_match_device_ids);
299
Patrick Mochel1890a972006-12-07 20:56:31 +0800300static void acpi_device_release(struct device *dev)
301{
302 struct acpi_device *acpi_dev = to_acpi_device(dev);
303
304 kfree(acpi_dev->pnp.cid_list);
305 kfree(acpi_dev);
306}
307
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800308static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800309{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800310 struct acpi_device *acpi_dev = to_acpi_device(dev);
311 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800312
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800313 if (acpi_drv && acpi_drv->ops.suspend)
314 return acpi_drv->ops.suspend(acpi_dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
316}
317
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800318static int acpi_device_resume(struct device *dev)
319{
320 struct acpi_device *acpi_dev = to_acpi_device(dev);
321 struct acpi_driver *acpi_drv = acpi_dev->driver;
322
323 if (acpi_drv && acpi_drv->ops.resume)
324 return acpi_drv->ops.resume(acpi_dev);
325 return 0;
326}
327
328static int acpi_bus_match(struct device *dev, struct device_driver *drv)
329{
330 struct acpi_device *acpi_dev = to_acpi_device(dev);
331 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
332
Thomas Renninger29b71a12007-07-23 14:43:51 +0200333 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800334}
335
Kay Sievers7eff2e72007-08-14 15:15:12 +0200336static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800337{
338 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200339 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800340
Kay Sievers7eff2e72007-08-14 15:15:12 +0200341 if (add_uevent_var(env, "MODALIAS="))
342 return -ENOMEM;
343 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
344 sizeof(env->buf) - env->buflen);
345 if (len >= (sizeof(env->buf) - env->buflen))
346 return -ENOMEM;
347 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return 0;
349}
350
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800351static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
352static int acpi_start_single_object(struct acpi_device *);
353static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800354{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800355 struct acpi_device *acpi_dev = to_acpi_device(dev);
356 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
357 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800359 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
360 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800361 if (acpi_dev->bus_ops.acpi_op_start)
362 acpi_start_single_object(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800363 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
364 "Found driver [%s] for device [%s]\n",
365 acpi_drv->name, acpi_dev->pnp.bus_id));
366 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800367 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800368 return ret;
369}
370
371static int acpi_device_remove(struct device * dev)
372{
373 struct acpi_device *acpi_dev = to_acpi_device(dev);
374 struct acpi_driver *acpi_drv = acpi_dev->driver;
375
376 if (acpi_drv) {
377 if (acpi_drv->ops.stop)
Li Shaohua96333572006-12-07 20:56:46 +0800378 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800379 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800380 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800381 }
382 acpi_dev->driver = NULL;
383 acpi_driver_data(dev) = NULL;
384
385 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800386 return 0;
387}
388
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800389static void acpi_device_shutdown(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800390{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800391 struct acpi_device *acpi_dev = to_acpi_device(dev);
392 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800393
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800394 if (acpi_drv && acpi_drv->ops.shutdown)
395 acpi_drv->ops.shutdown(acpi_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800397 return ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
David Brownell55955aa2007-05-08 00:28:35 -0700400struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800401 .name = "acpi",
402 .suspend = acpi_device_suspend,
403 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800404 .shutdown = acpi_device_shutdown,
405 .match = acpi_bus_match,
406 .probe = acpi_device_probe,
407 .remove = acpi_device_remove,
408 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409};
410
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800411static int acpi_device_register(struct acpi_device *device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 struct acpi_device *parent)
413{
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800414 int result;
415 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
416 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
418 * Linkage
419 * -------
420 * Link this device to its parent and siblings.
421 */
422 INIT_LIST_HEAD(&device->children);
423 INIT_LIST_HEAD(&device->node);
424 INIT_LIST_HEAD(&device->g_list);
425 INIT_LIST_HEAD(&device->wakeup_list);
426
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800427 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
428 if (!new_bus_id) {
429 printk(KERN_ERR PREFIX "Memory allocation error\n");
430 return -ENOMEM;
431 }
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spin_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800434 /*
435 * Find suitable bus_id and instance number in acpi_bus_id_list
436 * If failed, create one and link it into acpi_bus_id_list
437 */
438 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Zhang Ruibb095852007-01-04 15:03:18 +0800439 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800440 acpi_device_bus_id->instance_no ++;
441 found = 1;
442 kfree(new_bus_id);
443 break;
444 }
445 }
446 if(!found) {
447 acpi_device_bus_id = new_bus_id;
Zhang Ruibb095852007-01-04 15:03:18 +0800448 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800449 acpi_device_bus_id->instance_no = 0;
450 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
451 }
452 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (device->parent) {
455 list_add_tail(&device->node, &device->parent->children);
456 list_add_tail(&device->g_list, &device->parent->g_list);
457 } else
458 list_add_tail(&device->g_list, &acpi_device_list);
459 if (device->wakeup.flags.valid)
460 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
461 spin_unlock(&acpi_device_lock);
462
Patrick Mochel1890a972006-12-07 20:56:31 +0800463 if (device->parent)
464 device->dev.parent = &parent->dev;
465 device->dev.bus = &acpi_bus_type;
466 device_initialize(&device->dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800467 device->dev.release = &acpi_device_release;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800468 result = device_add(&device->dev);
469 if(result) {
Len Brown87ecd5c2008-01-01 14:00:00 -0500470 printk(KERN_ERR PREFIX "Error adding device %s", device->dev.bus_id);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800471 goto end;
472 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800473
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800474 result = acpi_device_setup_files(device);
475 if(result)
476 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
477
Li Shaohua96333572006-12-07 20:56:46 +0800478 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800479 return 0;
480 end:
481 spin_lock(&acpi_device_lock);
482 if (device->parent) {
483 list_del(&device->node);
484 list_del(&device->g_list);
485 } else
486 list_del(&device->g_list);
487 list_del(&device->wakeup_list);
488 spin_unlock(&acpi_device_lock);
489 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492static void acpi_device_unregister(struct acpi_device *device, int type)
493{
494 spin_lock(&acpi_device_lock);
495 if (device->parent) {
496 list_del(&device->node);
497 list_del(&device->g_list);
498 } else
499 list_del(&device->g_list);
500
501 list_del(&device->wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 spin_unlock(&acpi_device_lock);
503
504 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800505
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800506 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800507 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Zhang Rui9e89dde2006-12-07 20:56:16 +0800510/* --------------------------------------------------------------------------
511 Driver Management
512 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500514 * acpi_bus_driver_init - add a device to a driver
515 * @device: the device to add and initialize
516 * @driver: driver for the device
517 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800519 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
521static int
Len Brown4be44fc2005-08-05 00:44:28 -0400522acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Len Brown4be44fc2005-08-05 00:44:28 -0400524 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
527 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400528 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400531 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 result = driver->ops.add(device);
534 if (result) {
535 device->driver = NULL;
536 acpi_driver_data(device) = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400537 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
540 device->driver = driver;
541
542 /*
543 * TBD - Configuration Management: Assign resources to device based
544 * upon possible configuration and currently allocated resources.
545 */
546
Len Brown4be44fc2005-08-05 00:44:28 -0400547 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
548 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400549 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700550}
551
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400552static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700553{
554 int result = 0;
555 struct acpi_driver *driver;
556
Rajesh Shah3fb02732005-04-28 00:25:52 -0700557
558 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400559 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (driver->ops.start) {
562 result = driver->ops.start(device);
563 if (result && driver->ops.remove)
564 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Patrick Mocheld550d982006-06-27 00:41:40 -0400567 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500571 * acpi_bus_register_driver - register a driver with the ACPI bus
572 * @driver: driver being registered
573 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500575 * devices that match the driver's criteria and binds. Returns zero for
576 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Len Brown4be44fc2005-08-05 00:44:28 -0400578int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Patrick Mochel1890a972006-12-07 20:56:31 +0800580 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400583 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800584 driver->drv.name = driver->name;
585 driver->drv.bus = &acpi_bus_type;
586 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Patrick Mochel1890a972006-12-07 20:56:31 +0800588 ret = driver_register(&driver->drv);
589 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Len Brown4be44fc2005-08-05 00:44:28 -0400592EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500595 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
596 * @driver: driver to unregister
597 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 * Unregisters a driver with the ACPI bus. Searches the namespace for all
599 * devices that match the driver's criteria and unbinds.
600 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400601void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
Patrick Mochel1890a972006-12-07 20:56:31 +0800603 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
Len Brown4be44fc2005-08-05 00:44:28 -0400605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606EXPORT_SYMBOL(acpi_bus_unregister_driver);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/* --------------------------------------------------------------------------
609 Device Enumeration
610 -------------------------------------------------------------------------- */
Len Brownc8f7a622006-07-09 17:22:28 -0400611acpi_status
612acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
613{
614 acpi_status status;
615 acpi_handle tmp;
616 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
617 union acpi_object *obj;
618
619 status = acpi_get_handle(handle, "_EJD", &tmp);
620 if (ACPI_FAILURE(status))
621 return status;
622
623 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
624 if (ACPI_SUCCESS(status)) {
625 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100626 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
627 ejd);
Len Brownc8f7a622006-07-09 17:22:28 -0400628 kfree(buffer.pointer);
629 }
630 return status;
631}
632EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
635{
636
637 /* TBD */
638
639 return;
640}
641
Zhang Rui9e89dde2006-12-07 20:56:16 +0800642static int acpi_bus_get_perf_flags(struct acpi_device *device)
643{
644 device->performance.state = ACPI_STATE_UNKNOWN;
645 return 0;
646}
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648static acpi_status
649acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
650 union acpi_object *package)
651{
652 int i = 0;
653 union acpi_object *element = NULL;
654
655 if (!device || !package || (package->package.count < 2))
656 return AE_BAD_PARAMETER;
657
658 element = &(package->package.elements[0]);
659 if (!element)
660 return AE_BAD_PARAMETER;
661 if (element->type == ACPI_TYPE_PACKAGE) {
662 if ((element->package.count < 2) ||
663 (element->package.elements[0].type !=
664 ACPI_TYPE_LOCAL_REFERENCE)
665 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
666 return AE_BAD_DATA;
667 device->wakeup.gpe_device =
668 element->package.elements[0].reference.handle;
669 device->wakeup.gpe_number =
670 (u32) element->package.elements[1].integer.value;
671 } else if (element->type == ACPI_TYPE_INTEGER) {
672 device->wakeup.gpe_number = element->integer.value;
673 } else
674 return AE_BAD_DATA;
675
676 element = &(package->package.elements[1]);
677 if (element->type != ACPI_TYPE_INTEGER) {
678 return AE_BAD_DATA;
679 }
680 device->wakeup.sleep_state = element->integer.value;
681
682 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
683 return AE_NO_MEMORY;
684 }
685 device->wakeup.resources.count = package->package.count - 2;
686 for (i = 0; i < device->wakeup.resources.count; i++) {
687 element = &(package->package.elements[i + 2]);
Bob Moorecd0b2242008-04-10 19:06:43 +0400688 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return AE_BAD_DATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 device->wakeup.resources.handles[i] = element->reference.handle;
692 }
693
694 return AE_OK;
695}
696
697static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
698{
699 acpi_status status = 0;
700 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
701 union acpi_object *package = NULL;
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800702 union acpi_object in_arg[3];
703 struct acpi_object_list arg_list = { 3, in_arg };
704 acpi_status psw_status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Thomas Renninger29b71a12007-07-23 14:43:51 +0200706 struct acpi_device_id button_device_ids[] = {
707 {"PNP0C0D", 0},
708 {"PNP0C0C", 0},
709 {"PNP0C0E", 0},
710 {"", 0},
711 };
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /* _PRW */
714 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
715 if (ACPI_FAILURE(status)) {
716 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
717 goto end;
718 }
719
720 package = (union acpi_object *)buffer.pointer;
721 status = acpi_bus_extract_wakeup_device_power_package(device, package);
722 if (ACPI_FAILURE(status)) {
723 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
724 goto end;
725 }
726
727 kfree(buffer.pointer);
728
729 device->wakeup.flags.valid = 1;
Zhao Yakui729b2bd2008-03-19 13:26:54 +0800730 /* Call _PSW/_DSW object to disable its ability to wake the sleeping
731 * system for the ACPI device with the _PRW object.
732 * The _PSW object is depreciated in ACPI 3.0 and is replaced by _DSW.
733 * So it is necessary to call _DSW object first. Only when it is not
734 * present will the _PSW object used.
735 */
736 /*
737 * Three agruments are needed for the _DSW object.
738 * Argument 0: enable/disable the wake capabilities
739 * When _DSW object is called to disable the wake capabilities, maybe
740 * the first argument is filled. The value of the other two agruments
741 * is meaningless.
742 */
743 in_arg[0].type = ACPI_TYPE_INTEGER;
744 in_arg[0].integer.value = 0;
745 in_arg[1].type = ACPI_TYPE_INTEGER;
746 in_arg[1].integer.value = 0;
747 in_arg[2].type = ACPI_TYPE_INTEGER;
748 in_arg[2].integer.value = 0;
749 psw_status = acpi_evaluate_object(device->handle, "_DSW",
750 &arg_list, NULL);
751 if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND))
752 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in evaluate _DSW\n"));
753 /*
754 * When the _DSW object is not present, OSPM will call _PSW object.
755 */
756 if (psw_status == AE_NOT_FOUND) {
757 /*
758 * Only one agruments is required for the _PSW object.
759 * agrument 0: enable/disable the wake capabilities
760 */
761 arg_list.count = 1;
762 in_arg[0].integer.value = 0;
763 psw_status = acpi_evaluate_object(device->handle, "_PSW",
764 &arg_list, NULL);
765 if (ACPI_FAILURE(psw_status) && (psw_status != AE_NOT_FOUND))
766 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "error in "
767 "evaluate _PSW\n"));
768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 /* Power button, Lid switch always enable wakeup */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200770 if (!acpi_match_device_ids(device, button_device_ids))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 device->wakeup.flags.run_wake = 1;
772
773 end:
774 if (ACPI_FAILURE(status))
775 device->flags.wake_capable = 0;
776 return 0;
777}
778
Zhang Rui9e89dde2006-12-07 20:56:16 +0800779static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800781 acpi_status status = 0;
782 acpi_handle handle = NULL;
783 u32 i = 0;
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800787 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800789 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800791 device->power.flags.explicit_get = 1;
792 status = acpi_get_handle(device->handle, "_IRC", &handle);
793 if (ACPI_SUCCESS(status))
794 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800797 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800799 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
800 struct acpi_device_power_state *ps = &device->power.states[i];
801 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Zhang Rui9e89dde2006-12-07 20:56:16 +0800803 /* Evaluate "_PRx" to se if power resources are referenced */
804 acpi_evaluate_reference(device->handle, object_name, NULL,
805 &ps->resources);
806 if (ps->resources.count) {
807 device->power.flags.power_resources = 1;
808 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Zhang Rui9e89dde2006-12-07 20:56:16 +0800811 /* Evaluate "_PSx" to see if we can do explicit sets */
812 object_name[2] = 'S';
813 status = acpi_get_handle(device->handle, object_name, &handle);
814 if (ACPI_SUCCESS(status)) {
815 ps->flags.explicit_set = 1;
816 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800818
819 /* State is valid if we have some power control */
820 if (ps->resources.count || ps->flags.explicit_set)
821 ps->flags.valid = 1;
822
823 ps->power = -1; /* Unknown - driver assigned */
824 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Zhang Rui9e89dde2006-12-07 20:56:16 +0800827 /* Set defaults for D0 and D3 states (always valid) */
828 device->power.states[ACPI_STATE_D0].flags.valid = 1;
829 device->power.states[ACPI_STATE_D0].power = 100;
830 device->power.states[ACPI_STATE_D3].flags.valid = 1;
831 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Zhang Rui9e89dde2006-12-07 20:56:16 +0800833 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Zhang Rui9e89dde2006-12-07 20:56:16 +0800835 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 return 0;
838}
839
Len Brown4be44fc2005-08-05 00:44:28 -0400840static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Len Brown4be44fc2005-08-05 00:44:28 -0400842 acpi_status status = AE_OK;
843 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 /* Presence of _STA indicates 'dynamic_status' */
847 status = acpi_get_handle(device->handle, "_STA", &temp);
848 if (ACPI_SUCCESS(status))
849 device->flags.dynamic_status = 1;
850
851 /* Presence of _CID indicates 'compatible_ids' */
852 status = acpi_get_handle(device->handle, "_CID", &temp);
853 if (ACPI_SUCCESS(status))
854 device->flags.compatible_ids = 1;
855
856 /* Presence of _RMV indicates 'removable' */
857 status = acpi_get_handle(device->handle, "_RMV", &temp);
858 if (ACPI_SUCCESS(status))
859 device->flags.removable = 1;
860
861 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
862 status = acpi_get_handle(device->handle, "_EJD", &temp);
863 if (ACPI_SUCCESS(status))
864 device->flags.ejectable = 1;
865 else {
866 status = acpi_get_handle(device->handle, "_EJ0", &temp);
867 if (ACPI_SUCCESS(status))
868 device->flags.ejectable = 1;
869 }
870
871 /* Presence of _LCK indicates 'lockable' */
872 status = acpi_get_handle(device->handle, "_LCK", &temp);
873 if (ACPI_SUCCESS(status))
874 device->flags.lockable = 1;
875
876 /* Presence of _PS0|_PR0 indicates 'power manageable' */
877 status = acpi_get_handle(device->handle, "_PS0", &temp);
878 if (ACPI_FAILURE(status))
879 status = acpi_get_handle(device->handle, "_PR0", &temp);
880 if (ACPI_SUCCESS(status))
881 device->flags.power_manageable = 1;
882
883 /* Presence of _PRW indicates wake capable */
884 status = acpi_get_handle(device->handle, "_PRW", &temp);
885 if (ACPI_SUCCESS(status))
886 device->flags.wake_capable = 1;
887
Joe Perches3c5f9be42008-02-03 17:06:17 +0200888 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Patrick Mocheld550d982006-06-27 00:41:40 -0400890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891}
892
Len Brown4be44fc2005-08-05 00:44:28 -0400893static void acpi_device_get_busid(struct acpi_device *device,
894 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Len Brown4be44fc2005-08-05 00:44:28 -0400896 char bus_id[5] = { '?', 0 };
897 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
898 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 /*
901 * Bus ID
902 * ------
903 * The device's Bus ID is simply the object name.
904 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
905 */
906 switch (type) {
907 case ACPI_BUS_TYPE_SYSTEM:
908 strcpy(device->pnp.bus_id, "ACPI");
909 break;
910 case ACPI_BUS_TYPE_POWER_BUTTON:
911 strcpy(device->pnp.bus_id, "PWRF");
912 break;
913 case ACPI_BUS_TYPE_SLEEP_BUTTON:
914 strcpy(device->pnp.bus_id, "SLPF");
915 break;
916 default:
917 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
918 /* Clean up trailing underscores (if any) */
919 for (i = 3; i > 1; i--) {
920 if (bus_id[i] == '_')
921 bus_id[i] = '\0';
922 else
923 break;
924 }
925 strcpy(device->pnp.bus_id, bus_id);
926 break;
927 }
928}
929
Zhang Ruiae843332006-12-07 20:57:10 +0800930static int
931acpi_video_bus_match(struct acpi_device *device)
932{
Thomas Renninger66fb9d12008-04-16 20:52:02 +0200933 acpi_handle h_dummy;
Zhang Ruiae843332006-12-07 20:57:10 +0800934
935 if (!device)
936 return -EINVAL;
937
938 /* Since there is no HID, CID for ACPI Video drivers, we have
939 * to check well known required nodes for each feature we support.
940 */
941
942 /* Does this device able to support video switching ? */
Thomas Renninger66fb9d12008-04-16 20:52:02 +0200943 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy)) &&
944 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy)))
Zhang Ruiae843332006-12-07 20:57:10 +0800945 return 0;
946
947 /* Does this device able to retrieve a video ROM ? */
Thomas Renninger66fb9d12008-04-16 20:52:02 +0200948 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy)))
Zhang Ruiae843332006-12-07 20:57:10 +0800949 return 0;
950
951 /* Does this device able to configure which video head to be POSTed ? */
Thomas Renninger66fb9d12008-04-16 20:52:02 +0200952 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy)) &&
953 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy)) &&
954 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy)))
Zhang Ruiae843332006-12-07 20:57:10 +0800955 return 0;
956
957 return -ENODEV;
958}
959
Zhang Rui54735262007-01-11 02:09:09 -0500960/*
961 * acpi_bay_match - see if a device is an ejectable driver bay
962 *
963 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
964 * then we can safely call it an ejectable drive bay
965 */
966static int acpi_bay_match(struct acpi_device *device){
967 acpi_status status;
968 acpi_handle handle;
969 acpi_handle tmp;
970 acpi_handle phandle;
971
972 handle = device->handle;
973
974 status = acpi_get_handle(handle, "_EJ0", &tmp);
975 if (ACPI_FAILURE(status))
976 return -ENODEV;
977
978 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
979 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
980 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
981 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
982 return 0;
983
984 if (acpi_get_parent(handle, &phandle))
985 return -ENODEV;
986
987 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
988 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
989 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
990 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
991 return 0;
992
993 return -ENODEV;
994}
995
Frank Seidel3620f2f2007-12-07 13:20:34 +0100996/*
997 * acpi_dock_match - see if a device has a _DCK method
998 */
999static int acpi_dock_match(struct acpi_device *device)
1000{
1001 acpi_handle tmp;
1002 return acpi_get_handle(device->handle, "_DCK", &tmp);
1003}
1004
Len Brown4be44fc2005-08-05 00:44:28 -04001005static void acpi_device_set_id(struct acpi_device *device,
1006 struct acpi_device *parent, acpi_handle handle,
1007 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Len Brown4be44fc2005-08-05 00:44:28 -04001009 struct acpi_device_info *info;
1010 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1011 char *hid = NULL;
1012 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 struct acpi_compatible_id_list *cid_list = NULL;
Frank Seidel3620f2f2007-12-07 13:20:34 +01001014 const char *cid_add = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -04001015 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 switch (type) {
1018 case ACPI_BUS_TYPE_DEVICE:
1019 status = acpi_get_object_info(handle, &buffer);
1020 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -08001021 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return;
1023 }
1024
1025 info = buffer.pointer;
1026 if (info->valid & ACPI_VALID_HID)
1027 hid = info->hardware_id.value;
1028 if (info->valid & ACPI_VALID_UID)
1029 uid = info->unique_id.value;
1030 if (info->valid & ACPI_VALID_CID)
1031 cid_list = &info->compatibility_id;
1032 if (info->valid & ACPI_VALID_ADR) {
1033 device->pnp.bus_address = info->address;
1034 device->flags.bus_address = 1;
1035 }
Zhang Ruiae843332006-12-07 20:57:10 +08001036
Frank Seidel3620f2f2007-12-07 13:20:34 +01001037 /* If we have a video/bay/dock device, add our selfdefined
1038 HID to the CID list. Like that the video/bay/dock drivers
1039 will get autoloaded and the device might still match
1040 against another driver.
1041 */
1042 if (ACPI_SUCCESS(acpi_video_bus_match(device)))
1043 cid_add = ACPI_VIDEO_HID;
1044 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1045 cid_add = ACPI_BAY_HID;
1046 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1047 cid_add = ACPI_DOCK_HID;
Zhang Rui54735262007-01-11 02:09:09 -05001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 break;
1050 case ACPI_BUS_TYPE_POWER:
1051 hid = ACPI_POWER_HID;
1052 break;
1053 case ACPI_BUS_TYPE_PROCESSOR:
1054 hid = ACPI_PROCESSOR_HID;
1055 break;
1056 case ACPI_BUS_TYPE_SYSTEM:
1057 hid = ACPI_SYSTEM_HID;
1058 break;
1059 case ACPI_BUS_TYPE_THERMAL:
1060 hid = ACPI_THERMAL_HID;
1061 break;
1062 case ACPI_BUS_TYPE_POWER_BUTTON:
1063 hid = ACPI_BUTTON_HID_POWERF;
1064 break;
1065 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1066 hid = ACPI_BUTTON_HID_SLEEPF;
1067 break;
1068 }
1069
1070 /*
1071 * \_SB
1072 * ----
1073 * Fix for the system root bus device -- the only root-level device.
1074 */
Bob Moorec51a4de2005-11-17 13:07:00 -05001075 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 hid = ACPI_BUS_HID;
1077 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1078 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1079 }
1080
1081 if (hid) {
1082 strcpy(device->pnp.hardware_id, hid);
1083 device->flags.hardware_id = 1;
1084 }
1085 if (uid) {
1086 strcpy(device->pnp.unique_id, uid);
1087 device->flags.unique_id = 1;
1088 }
Frank Seidel3620f2f2007-12-07 13:20:34 +01001089 if (cid_list || cid_add) {
1090 struct acpi_compatible_id_list *list;
1091 int size = 0;
1092 int count = 0;
1093
1094 if (cid_list) {
1095 size = cid_list->size;
1096 } else if (cid_add) {
1097 size = sizeof(struct acpi_compatible_id_list);
1098 cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
1099 if (!cid_list) {
1100 printk(KERN_ERR "Memory allocation error\n");
1101 kfree(buffer.pointer);
1102 return;
1103 } else {
1104 cid_list->count = 0;
1105 cid_list->size = size;
1106 }
1107 }
1108 if (cid_add)
1109 size += sizeof(struct acpi_compatible_id);
1110 list = kmalloc(size, GFP_KERNEL);
1111
1112 if (list) {
1113 if (cid_list) {
1114 memcpy(list, cid_list, cid_list->size);
1115 count = cid_list->count;
1116 }
1117 if (cid_add) {
1118 strncpy(list->id[count].value, cid_add,
1119 ACPI_MAX_CID_LENGTH);
1120 count++;
1121 device->flags.compatible_ids = 1;
1122 }
1123 list->size = size;
1124 list->count = count;
1125 device->pnp.cid_list = list;
1126 } else
Len Brown87ecd5c2008-01-01 14:00:00 -05001127 printk(KERN_ERR PREFIX "Memory allocation error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
1129
Len Brown02438d82006-06-30 03:19:10 -04001130 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Len Brown4be44fc2005-08-05 00:44:28 -04001133static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
1135 acpi_status status = AE_OK;
1136 int result = 0;
1137 /*
1138 * Context
1139 * -------
1140 * Attach this 'struct acpi_device' to the ACPI object. This makes
1141 * resolutions from handle->device very efficient. Note that we need
1142 * to be careful with fixed-feature devices as they all attach to the
1143 * root object.
1144 */
Len Brown4be44fc2005-08-05 00:44:28 -04001145 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1147 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001148 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (ACPI_FAILURE(status)) {
Len Brown87ecd5c2008-01-01 14:00:00 -05001151 printk(KERN_ERR PREFIX "Error attaching device data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 result = -ENODEV;
1153 }
1154 }
1155 return result;
1156}
1157
Len Brown4be44fc2005-08-05 00:44:28 -04001158static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001161 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Li Shaohua96333572006-12-07 20:56:46 +08001163 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001164 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Rui Zhang2786f6e2006-12-21 02:21:13 -05001169 /*
1170 * unbind _ADR-Based Devices when hot removal
1171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (dev->flags.bus_address) {
1173 if ((dev->parent) && (dev->parent->ops.unbind))
1174 dev->parent->ops.unbind(dev);
1175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1177
Patrick Mocheld550d982006-06-27 00:41:40 -04001178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Rajesh Shah3fb02732005-04-28 00:25:52 -07001181static int
Frank Seidel3620f2f2007-12-07 13:20:34 +01001182acpi_is_child_device(struct acpi_device *device,
1183 int (*matcher)(struct acpi_device *))
1184{
1185 int result = -ENODEV;
1186
1187 do {
1188 if (ACPI_SUCCESS(matcher(device)))
1189 return AE_OK;
1190 } while ((device = device->parent));
1191
1192 return result;
1193}
1194
1195static int
Len Brown4be44fc2005-08-05 00:44:28 -04001196acpi_add_single_object(struct acpi_device **child,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001197 struct acpi_device *parent, acpi_handle handle, int type,
1198 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Len Brown4be44fc2005-08-05 00:44:28 -04001200 int result = 0;
1201 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 if (!child)
Patrick Mocheld550d982006-06-27 00:41:40 -04001205 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Burman Yan36bcbec2006-12-19 12:56:11 -08001207 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001209 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001210 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 device->handle = handle;
1214 device->parent = parent;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001215 device->bus_ops = *ops; /* workround for not call .start */
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Len Brown4be44fc2005-08-05 00:44:28 -04001218 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 /*
1221 * Flags
1222 * -----
1223 * Get prior to calling acpi_bus_get_status() so we know whether
1224 * or not _STA is present. Note that we only look for object
1225 * handles -- cannot evaluate objects until we know the device is
1226 * present and properly initialized.
1227 */
1228 result = acpi_bus_get_flags(device);
1229 if (result)
1230 goto end;
1231
1232 /*
1233 * Status
1234 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001235 * See if the device is present. We always assume that non-Device
1236 * and non-Processor objects (e.g. thermal zones, power resources,
1237 * etc.) are present, functioning, etc. (at least when parent object
1238 * is present). Note that _STA has a different meaning for some
1239 * objects (e.g. power resources) so we need to be careful how we use
1240 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 */
1242 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001243 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 case ACPI_BUS_TYPE_DEVICE:
1245 result = acpi_bus_get_status(device);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001246 if (ACPI_FAILURE(result)) {
1247 result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 goto end;
1249 }
Frank Seidel3620f2f2007-12-07 13:20:34 +01001250 if (!device->status.present) {
1251 /* Bay and dock should be handled even if absent */
1252 if (!ACPI_SUCCESS(
1253 acpi_is_child_device(device, acpi_bay_match)) &&
1254 !ACPI_SUCCESS(
1255 acpi_is_child_device(device, acpi_dock_match))) {
1256 result = -ENODEV;
1257 goto end;
1258 }
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261 default:
Bjorn Helgaas0c0e8922007-04-25 14:20:58 -04001262 STRUCT_TO_INT(device->status) =
1263 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1264 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 break;
1266 }
1267
1268 /*
1269 * Initialize Device
1270 * -----------------
1271 * TBD: Synch with Core's enumeration/initialization process.
1272 */
1273
1274 /*
1275 * Hardware ID, Unique ID, & Bus Address
1276 * -------------------------------------
1277 */
Len Brown4be44fc2005-08-05 00:44:28 -04001278 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /*
1281 * Power Management
1282 * ----------------
1283 */
1284 if (device->flags.power_manageable) {
1285 result = acpi_bus_get_power_flags(device);
1286 if (result)
1287 goto end;
1288 }
1289
Len Brown4be44fc2005-08-05 00:44:28 -04001290 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 * Wakeup device management
1292 *-----------------------
1293 */
1294 if (device->flags.wake_capable) {
1295 result = acpi_bus_get_wakeup_device_flags(device);
1296 if (result)
1297 goto end;
1298 }
1299
1300 /*
1301 * Performance Management
1302 * ----------------------
1303 */
1304 if (device->flags.performance_manageable) {
1305 result = acpi_bus_get_perf_flags(device);
1306 if (result)
1307 goto end;
1308 }
1309
Len Brown4be44fc2005-08-05 00:44:28 -04001310 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 goto end;
1312
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001313 result = acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001316 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 */
1318 if (device->flags.bus_address) {
1319 if (device->parent && device->parent->ops.bind)
1320 device->parent->ops.bind(device);
1321 }
1322
Len Brown4be44fc2005-08-05 00:44:28 -04001323 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (!result)
1325 *child = device;
1326 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001327 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 kfree(device);
1329 }
1330
Patrick Mocheld550d982006-06-27 00:41:40 -04001331 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Len Brown4be44fc2005-08-05 00:44:28 -04001334static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335{
Len Brown4be44fc2005-08-05 00:44:28 -04001336 acpi_status status = AE_OK;
1337 struct acpi_device *parent = NULL;
1338 struct acpi_device *child = NULL;
1339 acpi_handle phandle = NULL;
1340 acpi_handle chandle = NULL;
1341 acpi_object_type type = 0;
1342 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 if (!start)
Patrick Mocheld550d982006-06-27 00:41:40 -04001346 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348 parent = start;
1349 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 /*
1352 * Parse through the ACPI namespace, identify all 'devices', and
1353 * create a new 'struct acpi_device' for each.
1354 */
1355 while ((level > 0) && parent) {
1356
1357 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001358 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 /*
1361 * If this scope is exhausted then move our way back up.
1362 */
1363 if (ACPI_FAILURE(status)) {
1364 level--;
1365 chandle = phandle;
1366 acpi_get_parent(phandle, &phandle);
1367 if (parent->parent)
1368 parent = parent->parent;
1369 continue;
1370 }
1371
1372 status = acpi_get_type(chandle, &type);
1373 if (ACPI_FAILURE(status))
1374 continue;
1375
1376 /*
1377 * If this is a scope object then parse it (depth-first).
1378 */
1379 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1380 level++;
1381 phandle = chandle;
1382 chandle = NULL;
1383 continue;
1384 }
1385
1386 /*
1387 * We're only interested in objects that we consider 'devices'.
1388 */
1389 switch (type) {
1390 case ACPI_TYPE_DEVICE:
1391 type = ACPI_BUS_TYPE_DEVICE;
1392 break;
1393 case ACPI_TYPE_PROCESSOR:
1394 type = ACPI_BUS_TYPE_PROCESSOR;
1395 break;
1396 case ACPI_TYPE_THERMAL:
1397 type = ACPI_BUS_TYPE_THERMAL;
1398 break;
1399 case ACPI_TYPE_POWER:
1400 type = ACPI_BUS_TYPE_POWER;
1401 break;
1402 default:
1403 continue;
1404 }
1405
Rajesh Shah3fb02732005-04-28 00:25:52 -07001406 if (ops->acpi_op_add)
1407 status = acpi_add_single_object(&child, parent,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001408 chandle, type, ops);
Len Brown4be44fc2005-08-05 00:44:28 -04001409 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001410 status = acpi_bus_get_device(chandle, &child);
1411
Len Brown4be44fc2005-08-05 00:44:28 -04001412 if (ACPI_FAILURE(status))
1413 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001414
Li Shaohuac4168bf2006-12-07 20:56:41 +08001415 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001416 status = acpi_start_single_object(child);
1417 if (ACPI_FAILURE(status))
1418 continue;
1419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
1421 /*
1422 * If the device is present, enabled, and functioning then
1423 * parse its scope (depth-first). Note that we need to
1424 * represent absent devices to facilitate PnP notifications
1425 * -- but only the subtree head (not all of its children,
1426 * which will be enumerated when the parent is inserted).
1427 *
1428 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001429 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
1431 if (child->status.present) {
1432 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1433 NULL, NULL);
1434 if (ACPI_SUCCESS(status)) {
1435 level++;
1436 phandle = chandle;
1437 chandle = NULL;
1438 parent = child;
1439 }
1440 }
1441 }
1442
Patrick Mocheld550d982006-06-27 00:41:40 -04001443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Rajesh Shah3fb02732005-04-28 00:25:52 -07001446int
Len Brown4be44fc2005-08-05 00:44:28 -04001447acpi_bus_add(struct acpi_device **child,
1448 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001449{
1450 int result;
1451 struct acpi_bus_ops ops;
1452
Li Shaohuac4168bf2006-12-07 20:56:41 +08001453 memset(&ops, 0, sizeof(ops));
1454 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001455
Li Shaohuac4168bf2006-12-07 20:56:41 +08001456 result = acpi_add_single_object(child, parent, handle, type, &ops);
1457 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001458 result = acpi_bus_scan(*child, &ops);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001459
Patrick Mocheld550d982006-06-27 00:41:40 -04001460 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001461}
Len Brown4be44fc2005-08-05 00:44:28 -04001462
Rajesh Shah3fb02732005-04-28 00:25:52 -07001463EXPORT_SYMBOL(acpi_bus_add);
1464
Len Brown4be44fc2005-08-05 00:44:28 -04001465int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001466{
1467 int result;
1468 struct acpi_bus_ops ops;
1469
Rajesh Shah3fb02732005-04-28 00:25:52 -07001470
1471 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001472 return -EINVAL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001473
1474 result = acpi_start_single_object(device);
1475 if (!result) {
1476 memset(&ops, 0, sizeof(ops));
1477 ops.acpi_op_start = 1;
1478 result = acpi_bus_scan(device, &ops);
1479 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001480 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001481}
Len Brown4be44fc2005-08-05 00:44:28 -04001482
Rajesh Shah3fb02732005-04-28 00:25:52 -07001483EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Kristen Accardiceaba662006-02-23 17:56:01 -08001485int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Len Brown4be44fc2005-08-05 00:44:28 -04001487 acpi_status status;
1488 struct acpi_device *parent, *child;
1489 acpi_handle phandle, chandle;
1490 acpi_object_type type;
1491 u32 level = 1;
1492 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
Len Brown4be44fc2005-08-05 00:44:28 -04001494 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 phandle = start->handle;
1496 child = chandle = NULL;
1497
1498 while ((level > 0) && parent && (!err)) {
1499 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001500 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 /*
1503 * If this scope is exhausted then move our way back up.
1504 */
1505 if (ACPI_FAILURE(status)) {
1506 level--;
1507 chandle = phandle;
1508 acpi_get_parent(phandle, &phandle);
1509 child = parent;
1510 parent = parent->parent;
1511
1512 if (level == 0)
1513 err = acpi_bus_remove(child, rmdevice);
1514 else
1515 err = acpi_bus_remove(child, 1);
1516
1517 continue;
1518 }
1519
1520 status = acpi_get_type(chandle, &type);
1521 if (ACPI_FAILURE(status)) {
1522 continue;
1523 }
1524 /*
1525 * If there is a device corresponding to chandle then
1526 * parse it (depth-first).
1527 */
1528 if (acpi_bus_get_device(chandle, &child) == 0) {
1529 level++;
1530 phandle = chandle;
1531 chandle = NULL;
1532 parent = child;
1533 }
1534 continue;
1535 }
1536 return err;
1537}
Kristen Accardiceaba662006-02-23 17:56:01 -08001538EXPORT_SYMBOL_GPL(acpi_bus_trim);
1539
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Len Brown4be44fc2005-08-05 00:44:28 -04001541static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
Len Brown4be44fc2005-08-05 00:44:28 -04001543 int result = 0;
1544 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001545 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
1547 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -04001548 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Li Shaohuac4168bf2006-12-07 20:56:41 +08001550 memset(&ops, 0, sizeof(ops));
1551 ops.acpi_op_add = 1;
1552 ops.acpi_op_start = 1;
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /*
1555 * Enumerate all fixed-feature devices.
1556 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001557 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001558 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001559 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001560 ACPI_BUS_TYPE_POWER_BUTTON,
1561 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001564 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001565 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001566 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001567 ACPI_BUS_TYPE_SLEEP_BUTTON,
1568 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001569 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Patrick Mocheld550d982006-06-27 00:41:40 -04001571 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001574int __init acpi_boot_ec_enable(void);
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576static int __init acpi_scan_init(void)
1577{
1578 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001579 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001583 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Li Shaohuac4168bf2006-12-07 20:56:41 +08001585 memset(&ops, 0, sizeof(ops));
1586 ops.acpi_op_add = 1;
1587 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Patrick Mochel5b327262006-05-10 10:33:00 -04001589 result = bus_register(&acpi_bus_type);
1590 if (result) {
1591 /* We don't want to quit even if we failed to add suspend/resume */
1592 printk(KERN_ERR PREFIX "Could not register bus type\n");
1593 }
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 /*
1596 * Create the root device in the bus's device tree
1597 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001598 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001599 ACPI_BUS_TYPE_SYSTEM, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (result)
1601 goto Done;
1602
1603 /*
1604 * Enumerate devices in the ACPI namespace.
1605 */
1606 result = acpi_bus_scan_fixed(acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001607
1608 /* EC region might be needed at bus_scan, so enable it now */
1609 acpi_boot_ec_enable();
1610
Li Shaohuac4168bf2006-12-07 20:56:41 +08001611 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001612 result = acpi_bus_scan(acpi_root, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 if (result)
1615 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1616
Len Brown4be44fc2005-08-05 00:44:28 -04001617 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -04001618 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621subsys_initcall(acpi_scan_init);