blob: 6b1999b1cd34046a6a86d7f2424890f0e8a44537 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
4
5#include <linux/module.h>
6#include <linux/init.h>
Randy Dunlap9b6d97b2006-07-12 02:08:00 -04007#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/acpi.h>
9
10#include <acpi/acpi_drivers.h>
11#include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#define _COMPONENT ACPI_BUS_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050014ACPI_MODULE_NAME("scan");
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#define STRUCT_TO_INT(s) (*((int*)&s))
Len Brown4be44fc2005-08-05 00:44:28 -040016extern struct acpi_device *acpi_root;
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#define ACPI_BUS_CLASS "system_bus"
Thomas Renninger29b71a12007-07-23 14:43:51 +020019#define ACPI_BUS_HID "LNXSYBUS"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#define ACPI_BUS_DEVICE_NAME "System Bus"
21
22static LIST_HEAD(acpi_device_list);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080023static LIST_HEAD(acpi_bus_id_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024DEFINE_SPINLOCK(acpi_device_lock);
25LIST_HEAD(acpi_wakeup_device_list);
26
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080027struct acpi_device_bus_id{
Zhang Ruibb095852007-01-04 15:03:18 +080028 char bus_id[15];
Zhang Ruie49bd2dd2006-12-08 17:23:43 +080029 unsigned int instance_no;
30 struct list_head node;
31};
Thomas Renninger29b71a12007-07-23 14:43:51 +020032
33/*
34 * Creates hid/cid(s) string needed for modalias and uevent
35 * e.g. on a device with hid:IBM0001 and cid:ACPI0001 you get:
36 * char *modalias: "acpi:IBM0001:ACPI0001"
37*/
Adrian Bunkb3e572d2007-08-14 23:22:35 +020038static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
39 int size)
40{
Thomas Renninger29b71a12007-07-23 14:43:51 +020041 int len;
Zhang Rui5c9fcb52008-03-20 16:40:32 +080042 int count;
Thomas Renninger29b71a12007-07-23 14:43:51 +020043
Zhang Rui5c9fcb52008-03-20 16:40:32 +080044 if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
Thomas Renninger29b71a12007-07-23 14:43:51 +020045 return -ENODEV;
46
Zhang Rui5c9fcb52008-03-20 16:40:32 +080047 len = snprintf(modalias, size, "acpi:");
Thomas Renninger29b71a12007-07-23 14:43:51 +020048 size -= len;
49
Zhang Rui5c9fcb52008-03-20 16:40:32 +080050 if (acpi_dev->flags.hardware_id) {
51 count = snprintf(&modalias[len], size, "%s:",
52 acpi_dev->pnp.hardware_id);
53 if (count < 0 || count >= size)
54 return -EINVAL;
55 len += count;
56 size -= count;
57 }
58
Thomas Renninger29b71a12007-07-23 14:43:51 +020059 if (acpi_dev->flags.compatible_ids) {
60 struct acpi_compatible_id_list *cid_list;
61 int i;
Thomas Renninger29b71a12007-07-23 14:43:51 +020062
63 cid_list = acpi_dev->pnp.cid_list;
64 for (i = 0; i < cid_list->count; i++) {
65 count = snprintf(&modalias[len], size, "%s:",
66 cid_list->id[i].value);
67 if (count < 0 || count >= size) {
Len Brown87ecd5c2008-01-01 14:00:00 -050068 printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
Thomas Renninger29b71a12007-07-23 14:43:51 +020069 acpi_dev->pnp.device_name, i);
70 break;
71 }
72 len += count;
73 size -= count;
74 }
75 }
76
77 modalias[len] = '\0';
78 return len;
79}
80
81static ssize_t
82acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
83 struct acpi_device *acpi_dev = to_acpi_device(dev);
84 int len;
85
86 /* Device has no HID and no CID or string is >1024 */
87 len = create_modalias(acpi_dev, buf, 1024);
88 if (len <= 0)
89 return 0;
90 buf[len++] = '\n';
91 return len;
92}
93static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
94
Len Brown4be44fc2005-08-05 00:44:28 -040095static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 struct acpi_object_list arg_list;
98 union acpi_object arg;
99 acpi_status status = AE_OK;
100
101 /*
102 * TBD: evaluate _PS3?
103 */
104
105 if (lockable) {
106 arg_list.count = 1;
107 arg_list.pointer = &arg;
108 arg.type = ACPI_TYPE_INTEGER;
109 arg.integer.value = 0;
110 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
111 }
112
113 arg_list.count = 1;
114 arg_list.pointer = &arg;
115 arg.type = ACPI_TYPE_INTEGER;
116 arg.integer.value = 1;
117
118 /*
119 * TBD: _EJD support.
120 */
121
122 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
123 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400124 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
126
Len Brown4be44fc2005-08-05 00:44:28 -0400127 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800131acpi_eject_store(struct device *d, struct device_attribute *attr,
132 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Len Brown4be44fc2005-08-05 00:44:28 -0400134 int result;
135 int ret = count;
136 int islockable;
137 acpi_status status;
138 acpi_handle handle;
139 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800140 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 if ((!count) || (buf[0] != '1')) {
143 return -EINVAL;
144 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800146 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 ret = -ENODEV;
148 goto err;
149 }
150#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800151 status = acpi_get_type(acpi_device->handle, &type);
152 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 ret = -ENODEV;
154 goto err;
155 }
156
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800157 islockable = acpi_device->flags.lockable;
158 handle = acpi_device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800160 result = acpi_bus_trim(acpi_device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 if (!result)
163 result = acpi_eject_operation(handle, islockable);
164
165 if (result) {
166 ret = -EBUSY;
167 }
Len Brown4be44fc2005-08-05 00:44:28 -0400168 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800172static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800174static ssize_t
175acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
176 struct acpi_device *acpi_dev = to_acpi_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800178 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
179}
180static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
181
182static ssize_t
183acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
184 struct acpi_device *acpi_dev = to_acpi_device(dev);
185 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
186 int result;
187
188 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
189 if(result)
190 goto end;
191
192 result = sprintf(buf, "%s\n", (char*)path.pointer);
193 kfree(path.pointer);
194 end:
195 return result;
196}
197static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
198
199static int acpi_device_setup_files(struct acpi_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800201 acpi_status status;
202 acpi_handle temp;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800203 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800205 /*
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800206 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800207 */
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800208 if(dev->handle) {
209 result = device_create_file(&dev->dev, &dev_attr_path);
210 if(result)
211 goto end;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800214 if(dev->flags.hardware_id) {
215 result = device_create_file(&dev->dev, &dev_attr_hid);
216 if(result)
217 goto end;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Thomas Renninger29b71a12007-07-23 14:43:51 +0200220 if (dev->flags.hardware_id || dev->flags.compatible_ids){
221 result = device_create_file(&dev->dev, &dev_attr_modalias);
222 if(result)
223 goto end;
224 }
225
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800226 /*
227 * If device has _EJ0, 'eject' file is created that is used to trigger
228 * hot-removal function from userland.
229 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800230 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
231 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800232 result = device_create_file(&dev->dev, &dev_attr_eject);
233 end:
234 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800235}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800237static void acpi_device_remove_files(struct acpi_device *dev)
238{
239 acpi_status status;
240 acpi_handle temp;
241
242 /*
243 * If device has _EJ0, 'eject' file is created that is used to trigger
244 * hot-removal function from userland.
245 */
246 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
247 if (ACPI_SUCCESS(status))
248 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800249
Thomas Renninger29b71a12007-07-23 14:43:51 +0200250 if (dev->flags.hardware_id || dev->flags.compatible_ids)
251 device_remove_file(&dev->dev, &dev_attr_modalias);
252
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800253 if(dev->flags.hardware_id)
254 device_remove_file(&dev->dev, &dev_attr_hid);
255 if(dev->handle)
256 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800257}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800259 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 -------------------------------------------------------------------------- */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200261
262int acpi_match_device_ids(struct acpi_device *device,
263 const struct acpi_device_id *ids)
264{
265 const struct acpi_device_id *id;
266
267 if (device->flags.hardware_id) {
268 for (id = ids; id->id[0]; id++) {
269 if (!strcmp((char*)id->id, device->pnp.hardware_id))
270 return 0;
271 }
272 }
273
274 if (device->flags.compatible_ids) {
275 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
276 int i;
277
278 for (id = ids; id->id[0]; id++) {
279 /* compare multiple _CID entries against driver ids */
280 for (i = 0; i < cid_list->count; i++) {
281 if (!strcmp((char*)id->id,
282 cid_list->id[i].value))
283 return 0;
284 }
285 }
286 }
287
288 return -ENOENT;
289}
290EXPORT_SYMBOL(acpi_match_device_ids);
291
Patrick Mochel1890a972006-12-07 20:56:31 +0800292static void acpi_device_release(struct device *dev)
293{
294 struct acpi_device *acpi_dev = to_acpi_device(dev);
295
296 kfree(acpi_dev->pnp.cid_list);
297 kfree(acpi_dev);
298}
299
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800300static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800301{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800302 struct acpi_device *acpi_dev = to_acpi_device(dev);
303 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800304
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800305 if (acpi_drv && acpi_drv->ops.suspend)
306 return acpi_drv->ops.suspend(acpi_dev, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return 0;
308}
309
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800310static int acpi_device_resume(struct device *dev)
311{
312 struct acpi_device *acpi_dev = to_acpi_device(dev);
313 struct acpi_driver *acpi_drv = acpi_dev->driver;
314
315 if (acpi_drv && acpi_drv->ops.resume)
316 return acpi_drv->ops.resume(acpi_dev);
317 return 0;
318}
319
320static int acpi_bus_match(struct device *dev, struct device_driver *drv)
321{
322 struct acpi_device *acpi_dev = to_acpi_device(dev);
323 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
324
Thomas Renninger29b71a12007-07-23 14:43:51 +0200325 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800326}
327
Kay Sievers7eff2e72007-08-14 15:15:12 +0200328static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800329{
330 struct acpi_device *acpi_dev = to_acpi_device(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200331 int len;
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800332
Kay Sievers7eff2e72007-08-14 15:15:12 +0200333 if (add_uevent_var(env, "MODALIAS="))
334 return -ENOMEM;
335 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
336 sizeof(env->buf) - env->buflen);
337 if (len >= (sizeof(env->buf) - env->buflen))
338 return -ENOMEM;
339 env->buflen += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return 0;
341}
342
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800343static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
344static int acpi_start_single_object(struct acpi_device *);
345static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800346{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800347 struct acpi_device *acpi_dev = to_acpi_device(dev);
348 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
349 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800351 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
352 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800353 if (acpi_dev->bus_ops.acpi_op_start)
354 acpi_start_single_object(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800355 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
356 "Found driver [%s] for device [%s]\n",
357 acpi_drv->name, acpi_dev->pnp.bus_id));
358 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800359 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800360 return ret;
361}
362
363static int acpi_device_remove(struct device * dev)
364{
365 struct acpi_device *acpi_dev = to_acpi_device(dev);
366 struct acpi_driver *acpi_drv = acpi_dev->driver;
367
368 if (acpi_drv) {
369 if (acpi_drv->ops.stop)
Li Shaohua96333572006-12-07 20:56:46 +0800370 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800371 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800372 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800373 }
374 acpi_dev->driver = NULL;
375 acpi_driver_data(dev) = NULL;
376
377 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800378 return 0;
379}
380
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800381static void acpi_device_shutdown(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800382{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800383 struct acpi_device *acpi_dev = to_acpi_device(dev);
384 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800385
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800386 if (acpi_drv && acpi_drv->ops.shutdown)
387 acpi_drv->ops.shutdown(acpi_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800389 return ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
David Brownell55955aa2007-05-08 00:28:35 -0700392struct bus_type acpi_bus_type = {
Zhang Rui9e89dde2006-12-07 20:56:16 +0800393 .name = "acpi",
394 .suspend = acpi_device_suspend,
395 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800396 .shutdown = acpi_device_shutdown,
397 .match = acpi_bus_match,
398 .probe = acpi_device_probe,
399 .remove = acpi_device_remove,
400 .uevent = acpi_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401};
402
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800403static int acpi_device_register(struct acpi_device *device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct acpi_device *parent)
405{
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800406 int result;
407 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
408 int found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Linkage
411 * -------
412 * Link this device to its parent and siblings.
413 */
414 INIT_LIST_HEAD(&device->children);
415 INIT_LIST_HEAD(&device->node);
416 INIT_LIST_HEAD(&device->g_list);
417 INIT_LIST_HEAD(&device->wakeup_list);
418
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800419 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
420 if (!new_bus_id) {
421 printk(KERN_ERR PREFIX "Memory allocation error\n");
422 return -ENOMEM;
423 }
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_lock(&acpi_device_lock);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800426 /*
427 * Find suitable bus_id and instance number in acpi_bus_id_list
428 * If failed, create one and link it into acpi_bus_id_list
429 */
430 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
Zhang Ruibb095852007-01-04 15:03:18 +0800431 if(!strcmp(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 ++;
433 found = 1;
434 kfree(new_bus_id);
435 break;
436 }
437 }
438 if(!found) {
439 acpi_device_bus_id = new_bus_id;
Zhang Ruibb095852007-01-04 15:03:18 +0800440 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800441 acpi_device_bus_id->instance_no = 0;
442 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
443 }
444 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (device->parent) {
447 list_add_tail(&device->node, &device->parent->children);
448 list_add_tail(&device->g_list, &device->parent->g_list);
449 } else
450 list_add_tail(&device->g_list, &acpi_device_list);
451 if (device->wakeup.flags.valid)
452 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
453 spin_unlock(&acpi_device_lock);
454
Patrick Mochel1890a972006-12-07 20:56:31 +0800455 if (device->parent)
456 device->dev.parent = &parent->dev;
457 device->dev.bus = &acpi_bus_type;
458 device_initialize(&device->dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800459 device->dev.release = &acpi_device_release;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800460 result = device_add(&device->dev);
461 if(result) {
Len Brown87ecd5c2008-01-01 14:00:00 -0500462 printk(KERN_ERR PREFIX "Error adding device %s", device->dev.bus_id);
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800463 goto end;
464 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800465
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800466 result = acpi_device_setup_files(device);
467 if(result)
468 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
469
Li Shaohua96333572006-12-07 20:56:46 +0800470 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2dd2006-12-08 17:23:43 +0800471 return 0;
472 end:
473 spin_lock(&acpi_device_lock);
474 if (device->parent) {
475 list_del(&device->node);
476 list_del(&device->g_list);
477 } else
478 list_del(&device->g_list);
479 list_del(&device->wakeup_list);
480 spin_unlock(&acpi_device_lock);
481 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484static void acpi_device_unregister(struct acpi_device *device, int type)
485{
486 spin_lock(&acpi_device_lock);
487 if (device->parent) {
488 list_del(&device->node);
489 list_del(&device->g_list);
490 } else
491 list_del(&device->g_list);
492
493 list_del(&device->wakeup_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 spin_unlock(&acpi_device_lock);
495
496 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800497
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800498 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800499 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Zhang Rui9e89dde2006-12-07 20:56:16 +0800502/* --------------------------------------------------------------------------
503 Driver Management
504 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500506 * acpi_bus_driver_init - add a device to a driver
507 * @device: the device to add and initialize
508 * @driver: driver for the device
509 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800511 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513static int
Len Brown4be44fc2005-08-05 00:44:28 -0400514acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Len Brown4be44fc2005-08-05 00:44:28 -0400516 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400520 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400523 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 result = driver->ops.add(device);
526 if (result) {
527 device->driver = NULL;
528 acpi_driver_data(device) = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400529 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
532 device->driver = driver;
533
534 /*
535 * TBD - Configuration Management: Assign resources to device based
536 * upon possible configuration and currently allocated resources.
537 */
538
Len Brown4be44fc2005-08-05 00:44:28 -0400539 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
540 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400541 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700542}
543
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400544static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700545{
546 int result = 0;
547 struct acpi_driver *driver;
548
Rajesh Shah3fb02732005-04-28 00:25:52 -0700549
550 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400551 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (driver->ops.start) {
554 result = driver->ops.start(device);
555 if (result && driver->ops.remove)
556 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
Patrick Mocheld550d982006-06-27 00:41:40 -0400559 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500563 * acpi_bus_register_driver - register a driver with the ACPI bus
564 * @driver: driver being registered
565 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500567 * devices that match the driver's criteria and binds. Returns zero for
568 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 */
Len Brown4be44fc2005-08-05 00:44:28 -0400570int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
Patrick Mochel1890a972006-12-07 20:56:31 +0800572 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400575 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800576 driver->drv.name = driver->name;
577 driver->drv.bus = &acpi_bus_type;
578 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Patrick Mochel1890a972006-12-07 20:56:31 +0800580 ret = driver_register(&driver->drv);
581 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Len Brown4be44fc2005-08-05 00:44:28 -0400584EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500587 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
588 * @driver: driver to unregister
589 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 * Unregisters a driver with the ACPI bus. Searches the namespace for all
591 * devices that match the driver's criteria and unbinds.
592 */
Bjorn Helgaas06ea8e082006-04-27 05:25:00 -0400593void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Patrick Mochel1890a972006-12-07 20:56:31 +0800595 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
Len Brown4be44fc2005-08-05 00:44:28 -0400597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598EXPORT_SYMBOL(acpi_bus_unregister_driver);
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/* --------------------------------------------------------------------------
601 Device Enumeration
602 -------------------------------------------------------------------------- */
Len Brownc8f7a62c2006-07-09 17:22:28 -0400603acpi_status
604acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
605{
606 acpi_status status;
607 acpi_handle tmp;
608 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
609 union acpi_object *obj;
610
611 status = acpi_get_handle(handle, "_EJD", &tmp);
612 if (ACPI_FAILURE(status))
613 return status;
614
615 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
616 if (ACPI_SUCCESS(status)) {
617 obj = buffer.pointer;
Holger Macht3b5fee52008-02-14 13:40:34 +0100618 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
619 ejd);
Len Brownc8f7a62c2006-07-09 17:22:28 -0400620 kfree(buffer.pointer);
621 }
622 return status;
623}
624EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
627{
628
629 /* TBD */
630
631 return;
632}
633
Zhang Rui9e89dde2006-12-07 20:56:16 +0800634static int acpi_bus_get_perf_flags(struct acpi_device *device)
635{
636 device->performance.state = ACPI_STATE_UNKNOWN;
637 return 0;
638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640static acpi_status
641acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
642 union acpi_object *package)
643{
644 int i = 0;
645 union acpi_object *element = NULL;
646
647 if (!device || !package || (package->package.count < 2))
648 return AE_BAD_PARAMETER;
649
650 element = &(package->package.elements[0]);
651 if (!element)
652 return AE_BAD_PARAMETER;
653 if (element->type == ACPI_TYPE_PACKAGE) {
654 if ((element->package.count < 2) ||
655 (element->package.elements[0].type !=
656 ACPI_TYPE_LOCAL_REFERENCE)
657 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
658 return AE_BAD_DATA;
659 device->wakeup.gpe_device =
660 element->package.elements[0].reference.handle;
661 device->wakeup.gpe_number =
662 (u32) element->package.elements[1].integer.value;
663 } else if (element->type == ACPI_TYPE_INTEGER) {
664 device->wakeup.gpe_number = element->integer.value;
665 } else
666 return AE_BAD_DATA;
667
668 element = &(package->package.elements[1]);
669 if (element->type != ACPI_TYPE_INTEGER) {
670 return AE_BAD_DATA;
671 }
672 device->wakeup.sleep_state = element->integer.value;
673
674 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
675 return AE_NO_MEMORY;
676 }
677 device->wakeup.resources.count = package->package.count - 2;
678 for (i = 0; i < device->wakeup.resources.count; i++) {
679 element = &(package->package.elements[i + 2]);
680 if (element->type != ACPI_TYPE_ANY) {
681 return AE_BAD_DATA;
682 }
683
684 device->wakeup.resources.handles[i] = element->reference.handle;
685 }
686
687 return AE_OK;
688}
689
690static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
691{
692 acpi_status status = 0;
693 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
694 union acpi_object *package = NULL;
695
Thomas Renninger29b71a12007-07-23 14:43:51 +0200696 struct acpi_device_id button_device_ids[] = {
697 {"PNP0C0D", 0},
698 {"PNP0C0C", 0},
699 {"PNP0C0E", 0},
700 {"", 0},
701 };
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /* _PRW */
705 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
706 if (ACPI_FAILURE(status)) {
707 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
708 goto end;
709 }
710
711 package = (union acpi_object *)buffer.pointer;
712 status = acpi_bus_extract_wakeup_device_power_package(device, package);
713 if (ACPI_FAILURE(status)) {
714 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
715 goto end;
716 }
717
718 kfree(buffer.pointer);
719
720 device->wakeup.flags.valid = 1;
721 /* Power button, Lid switch always enable wakeup */
Thomas Renninger29b71a12007-07-23 14:43:51 +0200722 if (!acpi_match_device_ids(device, button_device_ids))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 device->wakeup.flags.run_wake = 1;
724
725 end:
726 if (ACPI_FAILURE(status))
727 device->flags.wake_capable = 0;
728 return 0;
729}
730
Zhang Rui9e89dde2006-12-07 20:56:16 +0800731static int acpi_bus_get_power_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Zhang Rui9e89dde2006-12-07 20:56:16 +0800733 acpi_status status = 0;
734 acpi_handle handle = NULL;
735 u32 i = 0;
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800739 * Power Management Flags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800741 status = acpi_get_handle(device->handle, "_PSC", &handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (ACPI_SUCCESS(status))
Zhang Rui9e89dde2006-12-07 20:56:16 +0800743 device->power.flags.explicit_get = 1;
744 status = acpi_get_handle(device->handle, "_IRC", &handle);
745 if (ACPI_SUCCESS(status))
746 device->power.flags.inrush_current = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /*
Zhang Rui9e89dde2006-12-07 20:56:16 +0800749 * Enumerate supported power management states
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 */
Zhang Rui9e89dde2006-12-07 20:56:16 +0800751 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
752 struct acpi_device_power_state *ps = &device->power.states[i];
753 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Zhang Rui9e89dde2006-12-07 20:56:16 +0800755 /* Evaluate "_PRx" to se if power resources are referenced */
756 acpi_evaluate_reference(device->handle, object_name, NULL,
757 &ps->resources);
758 if (ps->resources.count) {
759 device->power.flags.power_resources = 1;
760 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Zhang Rui9e89dde2006-12-07 20:56:16 +0800763 /* Evaluate "_PSx" to see if we can do explicit sets */
764 object_name[2] = 'S';
765 status = acpi_get_handle(device->handle, object_name, &handle);
766 if (ACPI_SUCCESS(status)) {
767 ps->flags.explicit_set = 1;
768 ps->flags.valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Zhang Rui9e89dde2006-12-07 20:56:16 +0800770
771 /* State is valid if we have some power control */
772 if (ps->resources.count || ps->flags.explicit_set)
773 ps->flags.valid = 1;
774
775 ps->power = -1; /* Unknown - driver assigned */
776 ps->latency = -1; /* Unknown - driver assigned */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Zhang Rui9e89dde2006-12-07 20:56:16 +0800779 /* Set defaults for D0 and D3 states (always valid) */
780 device->power.states[ACPI_STATE_D0].flags.valid = 1;
781 device->power.states[ACPI_STATE_D0].power = 100;
782 device->power.states[ACPI_STATE_D3].flags.valid = 1;
783 device->power.states[ACPI_STATE_D3].power = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Zhang Rui9e89dde2006-12-07 20:56:16 +0800785 /* TBD: System wake support and resource requirements. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Zhang Rui9e89dde2006-12-07 20:56:16 +0800787 device->power.state = ACPI_STATE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 return 0;
790}
791
Len Brown4be44fc2005-08-05 00:44:28 -0400792static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Len Brown4be44fc2005-08-05 00:44:28 -0400794 acpi_status status = AE_OK;
795 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /* Presence of _STA indicates 'dynamic_status' */
799 status = acpi_get_handle(device->handle, "_STA", &temp);
800 if (ACPI_SUCCESS(status))
801 device->flags.dynamic_status = 1;
802
803 /* Presence of _CID indicates 'compatible_ids' */
804 status = acpi_get_handle(device->handle, "_CID", &temp);
805 if (ACPI_SUCCESS(status))
806 device->flags.compatible_ids = 1;
807
808 /* Presence of _RMV indicates 'removable' */
809 status = acpi_get_handle(device->handle, "_RMV", &temp);
810 if (ACPI_SUCCESS(status))
811 device->flags.removable = 1;
812
813 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
814 status = acpi_get_handle(device->handle, "_EJD", &temp);
815 if (ACPI_SUCCESS(status))
816 device->flags.ejectable = 1;
817 else {
818 status = acpi_get_handle(device->handle, "_EJ0", &temp);
819 if (ACPI_SUCCESS(status))
820 device->flags.ejectable = 1;
821 }
822
823 /* Presence of _LCK indicates 'lockable' */
824 status = acpi_get_handle(device->handle, "_LCK", &temp);
825 if (ACPI_SUCCESS(status))
826 device->flags.lockable = 1;
827
828 /* Presence of _PS0|_PR0 indicates 'power manageable' */
829 status = acpi_get_handle(device->handle, "_PS0", &temp);
830 if (ACPI_FAILURE(status))
831 status = acpi_get_handle(device->handle, "_PR0", &temp);
832 if (ACPI_SUCCESS(status))
833 device->flags.power_manageable = 1;
834
835 /* Presence of _PRW indicates wake capable */
836 status = acpi_get_handle(device->handle, "_PRW", &temp);
837 if (ACPI_SUCCESS(status))
838 device->flags.wake_capable = 1;
839
Joe Perches3c5f9be42008-02-03 17:06:17 +0200840 /* TBD: Performance management */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Patrick Mocheld550d982006-06-27 00:41:40 -0400842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Len Brown4be44fc2005-08-05 00:44:28 -0400845static void acpi_device_get_busid(struct acpi_device *device,
846 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Len Brown4be44fc2005-08-05 00:44:28 -0400848 char bus_id[5] = { '?', 0 };
849 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
850 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /*
853 * Bus ID
854 * ------
855 * The device's Bus ID is simply the object name.
856 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
857 */
858 switch (type) {
859 case ACPI_BUS_TYPE_SYSTEM:
860 strcpy(device->pnp.bus_id, "ACPI");
861 break;
862 case ACPI_BUS_TYPE_POWER_BUTTON:
863 strcpy(device->pnp.bus_id, "PWRF");
864 break;
865 case ACPI_BUS_TYPE_SLEEP_BUTTON:
866 strcpy(device->pnp.bus_id, "SLPF");
867 break;
868 default:
869 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
870 /* Clean up trailing underscores (if any) */
871 for (i = 3; i > 1; i--) {
872 if (bus_id[i] == '_')
873 bus_id[i] = '\0';
874 else
875 break;
876 }
877 strcpy(device->pnp.bus_id, bus_id);
878 break;
879 }
880}
881
Zhang Ruiae843332006-12-07 20:57:10 +0800882static int
883acpi_video_bus_match(struct acpi_device *device)
884{
Thomas Renninger66fb9d12008-04-16 20:52:02 +0200885 acpi_handle h_dummy;
Zhang Ruiae843332006-12-07 20:57:10 +0800886
887 if (!device)
888 return -EINVAL;
889
890 /* Since there is no HID, CID for ACPI Video drivers, we have
891 * to check well known required nodes for each feature we support.
892 */
893
894 /* Does this device able to support video switching ? */
Thomas Renninger66fb9d12008-04-16 20:52:02 +0200895 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy)) &&
896 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy)))
Zhang Ruiae843332006-12-07 20:57:10 +0800897 return 0;
898
899 /* Does this device able to retrieve a video ROM ? */
Thomas Renninger66fb9d12008-04-16 20:52:02 +0200900 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy)))
Zhang Ruiae843332006-12-07 20:57:10 +0800901 return 0;
902
903 /* Does this device able to configure which video head to be POSTed ? */
Thomas Renninger66fb9d12008-04-16 20:52:02 +0200904 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy)) &&
905 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy)) &&
906 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy)))
Zhang Ruiae843332006-12-07 20:57:10 +0800907 return 0;
908
909 return -ENODEV;
910}
911
Zhang Rui54735262007-01-11 02:09:09 -0500912/*
913 * acpi_bay_match - see if a device is an ejectable driver bay
914 *
915 * If an acpi object is ejectable and has one of the ACPI ATA methods defined,
916 * then we can safely call it an ejectable drive bay
917 */
918static int acpi_bay_match(struct acpi_device *device){
919 acpi_status status;
920 acpi_handle handle;
921 acpi_handle tmp;
922 acpi_handle phandle;
923
924 handle = device->handle;
925
926 status = acpi_get_handle(handle, "_EJ0", &tmp);
927 if (ACPI_FAILURE(status))
928 return -ENODEV;
929
930 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
931 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
932 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
933 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
934 return 0;
935
936 if (acpi_get_parent(handle, &phandle))
937 return -ENODEV;
938
939 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
940 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
941 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
942 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
943 return 0;
944
945 return -ENODEV;
946}
947
Frank Seidel3620f2f2007-12-07 13:20:34 +0100948/*
949 * acpi_dock_match - see if a device has a _DCK method
950 */
951static int acpi_dock_match(struct acpi_device *device)
952{
953 acpi_handle tmp;
954 return acpi_get_handle(device->handle, "_DCK", &tmp);
955}
956
Len Brown4be44fc2005-08-05 00:44:28 -0400957static void acpi_device_set_id(struct acpi_device *device,
958 struct acpi_device *parent, acpi_handle handle,
959 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Len Brown4be44fc2005-08-05 00:44:28 -0400961 struct acpi_device_info *info;
962 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
963 char *hid = NULL;
964 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 struct acpi_compatible_id_list *cid_list = NULL;
Frank Seidel3620f2f2007-12-07 13:20:34 +0100966 const char *cid_add = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400967 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 switch (type) {
970 case ACPI_BUS_TYPE_DEVICE:
971 status = acpi_get_object_info(handle, &buffer);
972 if (ACPI_FAILURE(status)) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -0800973 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return;
975 }
976
977 info = buffer.pointer;
978 if (info->valid & ACPI_VALID_HID)
979 hid = info->hardware_id.value;
980 if (info->valid & ACPI_VALID_UID)
981 uid = info->unique_id.value;
982 if (info->valid & ACPI_VALID_CID)
983 cid_list = &info->compatibility_id;
984 if (info->valid & ACPI_VALID_ADR) {
985 device->pnp.bus_address = info->address;
986 device->flags.bus_address = 1;
987 }
Zhang Ruiae843332006-12-07 20:57:10 +0800988
Frank Seidel3620f2f2007-12-07 13:20:34 +0100989 /* If we have a video/bay/dock device, add our selfdefined
990 HID to the CID list. Like that the video/bay/dock drivers
991 will get autoloaded and the device might still match
992 against another driver.
993 */
994 if (ACPI_SUCCESS(acpi_video_bus_match(device)))
995 cid_add = ACPI_VIDEO_HID;
996 else if (ACPI_SUCCESS(acpi_bay_match(device)))
997 cid_add = ACPI_BAY_HID;
998 else if (ACPI_SUCCESS(acpi_dock_match(device)))
999 cid_add = ACPI_DOCK_HID;
Zhang Rui54735262007-01-11 02:09:09 -05001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 break;
1002 case ACPI_BUS_TYPE_POWER:
1003 hid = ACPI_POWER_HID;
1004 break;
1005 case ACPI_BUS_TYPE_PROCESSOR:
1006 hid = ACPI_PROCESSOR_HID;
1007 break;
1008 case ACPI_BUS_TYPE_SYSTEM:
1009 hid = ACPI_SYSTEM_HID;
1010 break;
1011 case ACPI_BUS_TYPE_THERMAL:
1012 hid = ACPI_THERMAL_HID;
1013 break;
1014 case ACPI_BUS_TYPE_POWER_BUTTON:
1015 hid = ACPI_BUTTON_HID_POWERF;
1016 break;
1017 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1018 hid = ACPI_BUTTON_HID_SLEEPF;
1019 break;
1020 }
1021
1022 /*
1023 * \_SB
1024 * ----
1025 * Fix for the system root bus device -- the only root-level device.
1026 */
Bob Moorec51a4de2005-11-17 13:07:00 -05001027 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 hid = ACPI_BUS_HID;
1029 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1030 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1031 }
1032
1033 if (hid) {
1034 strcpy(device->pnp.hardware_id, hid);
1035 device->flags.hardware_id = 1;
1036 }
1037 if (uid) {
1038 strcpy(device->pnp.unique_id, uid);
1039 device->flags.unique_id = 1;
1040 }
Frank Seidel3620f2f2007-12-07 13:20:34 +01001041 if (cid_list || cid_add) {
1042 struct acpi_compatible_id_list *list;
1043 int size = 0;
1044 int count = 0;
1045
1046 if (cid_list) {
1047 size = cid_list->size;
1048 } else if (cid_add) {
1049 size = sizeof(struct acpi_compatible_id_list);
1050 cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
1051 if (!cid_list) {
1052 printk(KERN_ERR "Memory allocation error\n");
1053 kfree(buffer.pointer);
1054 return;
1055 } else {
1056 cid_list->count = 0;
1057 cid_list->size = size;
1058 }
1059 }
1060 if (cid_add)
1061 size += sizeof(struct acpi_compatible_id);
1062 list = kmalloc(size, GFP_KERNEL);
1063
1064 if (list) {
1065 if (cid_list) {
1066 memcpy(list, cid_list, cid_list->size);
1067 count = cid_list->count;
1068 }
1069 if (cid_add) {
1070 strncpy(list->id[count].value, cid_add,
1071 ACPI_MAX_CID_LENGTH);
1072 count++;
1073 device->flags.compatible_ids = 1;
1074 }
1075 list->size = size;
1076 list->count = count;
1077 device->pnp.cid_list = list;
1078 } else
Len Brown87ecd5c2008-01-01 14:00:00 -05001079 printk(KERN_ERR PREFIX "Memory allocation error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
Len Brown02438d82006-06-30 03:19:10 -04001082 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083}
1084
Len Brown4be44fc2005-08-05 00:44:28 -04001085static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
1087 acpi_status status = AE_OK;
1088 int result = 0;
1089 /*
1090 * Context
1091 * -------
1092 * Attach this 'struct acpi_device' to the ACPI object. This makes
1093 * resolutions from handle->device very efficient. Note that we need
1094 * to be careful with fixed-feature devices as they all attach to the
1095 * root object.
1096 */
Len Brown4be44fc2005-08-05 00:44:28 -04001097 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1099 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -04001100 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if (ACPI_FAILURE(status)) {
Len Brown87ecd5c2008-01-01 14:00:00 -05001103 printk(KERN_ERR PREFIX "Error attaching device data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 result = -ENODEV;
1105 }
1106 }
1107 return result;
1108}
1109
Len Brown4be44fc2005-08-05 00:44:28 -04001110static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001113 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Li Shaohua96333572006-12-07 20:56:46 +08001115 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001116 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
1118 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Rui Zhang2786f6e2006-12-21 02:21:13 -05001121 /*
1122 * unbind _ADR-Based Devices when hot removal
1123 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (dev->flags.bus_address) {
1125 if ((dev->parent) && (dev->parent->ops.unbind))
1126 dev->parent->ops.unbind(dev);
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1129
Patrick Mocheld550d982006-06-27 00:41:40 -04001130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Rajesh Shah3fb02732005-04-28 00:25:52 -07001133static int
Frank Seidel3620f2f2007-12-07 13:20:34 +01001134acpi_is_child_device(struct acpi_device *device,
1135 int (*matcher)(struct acpi_device *))
1136{
1137 int result = -ENODEV;
1138
1139 do {
1140 if (ACPI_SUCCESS(matcher(device)))
1141 return AE_OK;
1142 } while ((device = device->parent));
1143
1144 return result;
1145}
1146
1147static int
Len Brown4be44fc2005-08-05 00:44:28 -04001148acpi_add_single_object(struct acpi_device **child,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001149 struct acpi_device *parent, acpi_handle handle, int type,
1150 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151{
Len Brown4be44fc2005-08-05 00:44:28 -04001152 int result = 0;
1153 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (!child)
Patrick Mocheld550d982006-06-27 00:41:40 -04001157 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Burman Yan36bcbec2006-12-19 12:56:11 -08001159 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001161 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001162 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 device->handle = handle;
1166 device->parent = parent;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001167 device->bus_ops = *ops; /* workround for not call .start */
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Len Brown4be44fc2005-08-05 00:44:28 -04001170 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 /*
1173 * Flags
1174 * -----
1175 * Get prior to calling acpi_bus_get_status() so we know whether
1176 * or not _STA is present. Note that we only look for object
1177 * handles -- cannot evaluate objects until we know the device is
1178 * present and properly initialized.
1179 */
1180 result = acpi_bus_get_flags(device);
1181 if (result)
1182 goto end;
1183
1184 /*
1185 * Status
1186 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001187 * See if the device is present. We always assume that non-Device
1188 * and non-Processor objects (e.g. thermal zones, power resources,
1189 * etc.) are present, functioning, etc. (at least when parent object
1190 * is present). Note that _STA has a different meaning for some
1191 * objects (e.g. power resources) so we need to be careful how we use
1192 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 */
1194 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001195 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 case ACPI_BUS_TYPE_DEVICE:
1197 result = acpi_bus_get_status(device);
Frank Seidel3620f2f2007-12-07 13:20:34 +01001198 if (ACPI_FAILURE(result)) {
1199 result = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 goto end;
1201 }
Frank Seidel3620f2f2007-12-07 13:20:34 +01001202 if (!device->status.present) {
1203 /* Bay and dock should be handled even if absent */
1204 if (!ACPI_SUCCESS(
1205 acpi_is_child_device(device, acpi_bay_match)) &&
1206 !ACPI_SUCCESS(
1207 acpi_is_child_device(device, acpi_dock_match))) {
1208 result = -ENODEV;
1209 goto end;
1210 }
1211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 break;
1213 default:
Bjorn Helgaas0c0e8922007-04-25 14:20:58 -04001214 STRUCT_TO_INT(device->status) =
1215 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1216 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 break;
1218 }
1219
1220 /*
1221 * Initialize Device
1222 * -----------------
1223 * TBD: Synch with Core's enumeration/initialization process.
1224 */
1225
1226 /*
1227 * Hardware ID, Unique ID, & Bus Address
1228 * -------------------------------------
1229 */
Len Brown4be44fc2005-08-05 00:44:28 -04001230 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
1232 /*
1233 * Power Management
1234 * ----------------
1235 */
1236 if (device->flags.power_manageable) {
1237 result = acpi_bus_get_power_flags(device);
1238 if (result)
1239 goto end;
1240 }
1241
Len Brown4be44fc2005-08-05 00:44:28 -04001242 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 * Wakeup device management
1244 *-----------------------
1245 */
1246 if (device->flags.wake_capable) {
1247 result = acpi_bus_get_wakeup_device_flags(device);
1248 if (result)
1249 goto end;
1250 }
1251
1252 /*
1253 * Performance Management
1254 * ----------------------
1255 */
1256 if (device->flags.performance_manageable) {
1257 result = acpi_bus_get_perf_flags(device);
1258 if (result)
1259 goto end;
1260 }
1261
Len Brown4be44fc2005-08-05 00:44:28 -04001262 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 goto end;
1264
Zhang Ruie49bd2dd2006-12-08 17:23:43 +08001265 result = acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
Rui Zhang2786f6e2006-12-21 02:21:13 -05001268 * Bind _ADR-Based Devices when hot add
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 */
1270 if (device->flags.bus_address) {
1271 if (device->parent && device->parent->ops.bind)
1272 device->parent->ops.bind(device);
1273 }
1274
Len Brown4be44fc2005-08-05 00:44:28 -04001275 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (!result)
1277 *child = device;
1278 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001279 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 kfree(device);
1281 }
1282
Patrick Mocheld550d982006-06-27 00:41:40 -04001283 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Len Brown4be44fc2005-08-05 00:44:28 -04001286static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Len Brown4be44fc2005-08-05 00:44:28 -04001288 acpi_status status = AE_OK;
1289 struct acpi_device *parent = NULL;
1290 struct acpi_device *child = NULL;
1291 acpi_handle phandle = NULL;
1292 acpi_handle chandle = NULL;
1293 acpi_object_type type = 0;
1294 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297 if (!start)
Patrick Mocheld550d982006-06-27 00:41:40 -04001298 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 parent = start;
1301 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 /*
1304 * Parse through the ACPI namespace, identify all 'devices', and
1305 * create a new 'struct acpi_device' for each.
1306 */
1307 while ((level > 0) && parent) {
1308
1309 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001310 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 /*
1313 * If this scope is exhausted then move our way back up.
1314 */
1315 if (ACPI_FAILURE(status)) {
1316 level--;
1317 chandle = phandle;
1318 acpi_get_parent(phandle, &phandle);
1319 if (parent->parent)
1320 parent = parent->parent;
1321 continue;
1322 }
1323
1324 status = acpi_get_type(chandle, &type);
1325 if (ACPI_FAILURE(status))
1326 continue;
1327
1328 /*
1329 * If this is a scope object then parse it (depth-first).
1330 */
1331 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1332 level++;
1333 phandle = chandle;
1334 chandle = NULL;
1335 continue;
1336 }
1337
1338 /*
1339 * We're only interested in objects that we consider 'devices'.
1340 */
1341 switch (type) {
1342 case ACPI_TYPE_DEVICE:
1343 type = ACPI_BUS_TYPE_DEVICE;
1344 break;
1345 case ACPI_TYPE_PROCESSOR:
1346 type = ACPI_BUS_TYPE_PROCESSOR;
1347 break;
1348 case ACPI_TYPE_THERMAL:
1349 type = ACPI_BUS_TYPE_THERMAL;
1350 break;
1351 case ACPI_TYPE_POWER:
1352 type = ACPI_BUS_TYPE_POWER;
1353 break;
1354 default:
1355 continue;
1356 }
1357
Rajesh Shah3fb02732005-04-28 00:25:52 -07001358 if (ops->acpi_op_add)
1359 status = acpi_add_single_object(&child, parent,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001360 chandle, type, ops);
Len Brown4be44fc2005-08-05 00:44:28 -04001361 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001362 status = acpi_bus_get_device(chandle, &child);
1363
Len Brown4be44fc2005-08-05 00:44:28 -04001364 if (ACPI_FAILURE(status))
1365 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001366
Li Shaohuac4168bf2006-12-07 20:56:41 +08001367 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001368 status = acpi_start_single_object(child);
1369 if (ACPI_FAILURE(status))
1370 continue;
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /*
1374 * If the device is present, enabled, and functioning then
1375 * parse its scope (depth-first). Note that we need to
1376 * represent absent devices to facilitate PnP notifications
1377 * -- but only the subtree head (not all of its children,
1378 * which will be enumerated when the parent is inserted).
1379 *
1380 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001381 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
1383 if (child->status.present) {
1384 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1385 NULL, NULL);
1386 if (ACPI_SUCCESS(status)) {
1387 level++;
1388 phandle = chandle;
1389 chandle = NULL;
1390 parent = child;
1391 }
1392 }
1393 }
1394
Patrick Mocheld550d982006-06-27 00:41:40 -04001395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
Rajesh Shah3fb02732005-04-28 00:25:52 -07001398int
Len Brown4be44fc2005-08-05 00:44:28 -04001399acpi_bus_add(struct acpi_device **child,
1400 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001401{
1402 int result;
1403 struct acpi_bus_ops ops;
1404
Li Shaohuac4168bf2006-12-07 20:56:41 +08001405 memset(&ops, 0, sizeof(ops));
1406 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001407
Li Shaohuac4168bf2006-12-07 20:56:41 +08001408 result = acpi_add_single_object(child, parent, handle, type, &ops);
1409 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001410 result = acpi_bus_scan(*child, &ops);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001411
Patrick Mocheld550d982006-06-27 00:41:40 -04001412 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001413}
Len Brown4be44fc2005-08-05 00:44:28 -04001414
Rajesh Shah3fb02732005-04-28 00:25:52 -07001415EXPORT_SYMBOL(acpi_bus_add);
1416
Len Brown4be44fc2005-08-05 00:44:28 -04001417int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001418{
1419 int result;
1420 struct acpi_bus_ops ops;
1421
Rajesh Shah3fb02732005-04-28 00:25:52 -07001422
1423 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001424 return -EINVAL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001425
1426 result = acpi_start_single_object(device);
1427 if (!result) {
1428 memset(&ops, 0, sizeof(ops));
1429 ops.acpi_op_start = 1;
1430 result = acpi_bus_scan(device, &ops);
1431 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001432 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001433}
Len Brown4be44fc2005-08-05 00:44:28 -04001434
Rajesh Shah3fb02732005-04-28 00:25:52 -07001435EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Kristen Accardiceaba662006-02-23 17:56:01 -08001437int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Len Brown4be44fc2005-08-05 00:44:28 -04001439 acpi_status status;
1440 struct acpi_device *parent, *child;
1441 acpi_handle phandle, chandle;
1442 acpi_object_type type;
1443 u32 level = 1;
1444 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Len Brown4be44fc2005-08-05 00:44:28 -04001446 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 phandle = start->handle;
1448 child = chandle = NULL;
1449
1450 while ((level > 0) && parent && (!err)) {
1451 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001452 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 /*
1455 * If this scope is exhausted then move our way back up.
1456 */
1457 if (ACPI_FAILURE(status)) {
1458 level--;
1459 chandle = phandle;
1460 acpi_get_parent(phandle, &phandle);
1461 child = parent;
1462 parent = parent->parent;
1463
1464 if (level == 0)
1465 err = acpi_bus_remove(child, rmdevice);
1466 else
1467 err = acpi_bus_remove(child, 1);
1468
1469 continue;
1470 }
1471
1472 status = acpi_get_type(chandle, &type);
1473 if (ACPI_FAILURE(status)) {
1474 continue;
1475 }
1476 /*
1477 * If there is a device corresponding to chandle then
1478 * parse it (depth-first).
1479 */
1480 if (acpi_bus_get_device(chandle, &child) == 0) {
1481 level++;
1482 phandle = chandle;
1483 chandle = NULL;
1484 parent = child;
1485 }
1486 continue;
1487 }
1488 return err;
1489}
Kristen Accardiceaba662006-02-23 17:56:01 -08001490EXPORT_SYMBOL_GPL(acpi_bus_trim);
1491
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Len Brown4be44fc2005-08-05 00:44:28 -04001493static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494{
Len Brown4be44fc2005-08-05 00:44:28 -04001495 int result = 0;
1496 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001497 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -04001500 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Li Shaohuac4168bf2006-12-07 20:56:41 +08001502 memset(&ops, 0, sizeof(ops));
1503 ops.acpi_op_add = 1;
1504 ops.acpi_op_start = 1;
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 /*
1507 * Enumerate all fixed-feature devices.
1508 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001509 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001510 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001511 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001512 ACPI_BUS_TYPE_POWER_BUTTON,
1513 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001516 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001517 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001518 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001519 ACPI_BUS_TYPE_SLEEP_BUTTON,
1520 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Patrick Mocheld550d982006-06-27 00:41:40 -04001523 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524}
1525
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001526int __init acpi_boot_ec_enable(void);
1527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528static int __init acpi_scan_init(void)
1529{
1530 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001531 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001535 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Li Shaohuac4168bf2006-12-07 20:56:41 +08001537 memset(&ops, 0, sizeof(ops));
1538 ops.acpi_op_add = 1;
1539 ops.acpi_op_start = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Patrick Mochel5b327262006-05-10 10:33:00 -04001541 result = bus_register(&acpi_bus_type);
1542 if (result) {
1543 /* We don't want to quit even if we failed to add suspend/resume */
1544 printk(KERN_ERR PREFIX "Could not register bus type\n");
1545 }
1546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /*
1548 * Create the root device in the bus's device tree
1549 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001550 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001551 ACPI_BUS_TYPE_SYSTEM, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if (result)
1553 goto Done;
1554
1555 /*
1556 * Enumerate devices in the ACPI namespace.
1557 */
1558 result = acpi_bus_scan_fixed(acpi_root);
Alexey Starikovskiyc04209a2008-01-01 14:12:55 -05001559
1560 /* EC region might be needed at bus_scan, so enable it now */
1561 acpi_boot_ec_enable();
1562
Li Shaohuac4168bf2006-12-07 20:56:41 +08001563 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001564 result = acpi_bus_scan(acpi_root, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 if (result)
1567 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1568
Len Brown4be44fc2005-08-05 00:44:28 -04001569 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -04001570 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571}
1572
1573subsys_initcall(acpi_scan_init);