blob: 769e54bc9226050574bfd7c6ce8526133ad0e491 [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 Brown4be44fc2005-08-05 00:44:28 -040014ACPI_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"
19#define ACPI_BUS_HID "ACPI_BUS"
20#define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
21#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{
29 char bus_id[9];
30 unsigned int instance_no;
31 struct list_head node;
32};
Len Brown4be44fc2005-08-05 00:44:28 -040033static int acpi_eject_operation(acpi_handle handle, int lockable)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 struct acpi_object_list arg_list;
36 union acpi_object arg;
37 acpi_status status = AE_OK;
38
39 /*
40 * TBD: evaluate _PS3?
41 */
42
43 if (lockable) {
44 arg_list.count = 1;
45 arg_list.pointer = &arg;
46 arg.type = ACPI_TYPE_INTEGER;
47 arg.integer.value = 0;
48 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
49 }
50
51 arg_list.count = 1;
52 arg_list.pointer = &arg;
53 arg.type = ACPI_TYPE_INTEGER;
54 arg.integer.value = 1;
55
56 /*
57 * TBD: _EJD support.
58 */
59
60 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
61 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -040062 return (-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
64
Len Brown4be44fc2005-08-05 00:44:28 -040065 return (0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static ssize_t
Patrick Mochelf883d9d2006-12-07 20:56:38 +080069acpi_eject_store(struct device *d, struct device_attribute *attr,
70 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Len Brown4be44fc2005-08-05 00:44:28 -040072 int result;
73 int ret = count;
74 int islockable;
75 acpi_status status;
76 acpi_handle handle;
77 acpi_object_type type = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +080078 struct acpi_device *acpi_device = to_acpi_device(d);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 if ((!count) || (buf[0] != '1')) {
81 return -EINVAL;
82 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083#ifndef FORCE_EJECT
Patrick Mochelf883d9d2006-12-07 20:56:38 +080084 if (acpi_device->driver == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 ret = -ENODEV;
86 goto err;
87 }
88#endif
Patrick Mochelf883d9d2006-12-07 20:56:38 +080089 status = acpi_get_type(acpi_device->handle, &type);
90 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 ret = -ENODEV;
92 goto err;
93 }
94
Patrick Mochelf883d9d2006-12-07 20:56:38 +080095 islockable = acpi_device->flags.lockable;
96 handle = acpi_device->handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Patrick Mochelf883d9d2006-12-07 20:56:38 +080098 result = acpi_bus_trim(acpi_device, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 if (!result)
101 result = acpi_eject_operation(handle, islockable);
102
103 if (result) {
104 ret = -EBUSY;
105 }
Len Brown4be44fc2005-08-05 00:44:28 -0400106 err:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return ret;
108}
109
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800110static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
111
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800112static ssize_t
113acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
114 struct acpi_device *acpi_dev = to_acpi_device(dev);
115
116 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
117}
118static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
119
120static ssize_t
121acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
122 struct acpi_device *acpi_dev = to_acpi_device(dev);
123 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
124 int result;
125
126 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
127 if(result)
128 goto end;
129
130 result = sprintf(buf, "%s\n", (char*)path.pointer);
131 kfree(path.pointer);
132 end:
133 return result;
134}
135static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
136
137static int acpi_device_setup_files(struct acpi_device *dev)
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800138{
139 acpi_status status;
140 acpi_handle temp;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800141 int result = 0;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800142
143 /*
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800144 * Devices gotten from FADT don't have a "path" attribute
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800145 */
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800146 if(dev->handle) {
147 result = device_create_file(&dev->dev, &dev_attr_path);
148 if(result)
149 goto end;
150 }
151
152 if(dev->flags.hardware_id) {
153 result = device_create_file(&dev->dev, &dev_attr_hid);
154 if(result)
155 goto end;
156 }
157
158 /*
159 * If device has _EJ0, 'eject' file is created that is used to trigger
160 * hot-removal function from userland.
161 */
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800162 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
163 if (ACPI_SUCCESS(status))
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800164 result = device_create_file(&dev->dev, &dev_attr_eject);
165 end:
166 return result;
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800167}
168
169static void acpi_device_remove_files(struct acpi_device *dev)
170{
171 acpi_status status;
172 acpi_handle temp;
173
174 /*
175 * If device has _EJ0, 'eject' file is created that is used to trigger
176 * hot-removal function from userland.
177 */
178 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
179 if (ACPI_SUCCESS(status))
180 device_remove_file(&dev->dev, &dev_attr_eject);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800181
182 if(dev->flags.hardware_id)
183 device_remove_file(&dev->dev, &dev_attr_hid);
184 if(dev->handle)
185 device_remove_file(&dev->dev, &dev_attr_path);
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800186}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/* --------------------------------------------------------------------------
Zhang Rui9e89dde2006-12-07 20:56:16 +0800188 ACPI Bus operations
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 -------------------------------------------------------------------------- */
Patrick Mochel1890a972006-12-07 20:56:31 +0800190static void acpi_device_release(struct device *dev)
191{
192 struct acpi_device *acpi_dev = to_acpi_device(dev);
193
194 kfree(acpi_dev->pnp.cid_list);
195 kfree(acpi_dev);
196}
197
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800198static int acpi_device_suspend(struct device *dev, pm_message_t state)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800199{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800200 struct acpi_device *acpi_dev = to_acpi_device(dev);
201 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800202
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800203 if (acpi_drv && acpi_drv->ops.suspend)
204 return acpi_drv->ops.suspend(acpi_dev, state);
205 return 0;
206}
207
208static int acpi_device_resume(struct device *dev)
209{
210 struct acpi_device *acpi_dev = to_acpi_device(dev);
211 struct acpi_driver *acpi_drv = acpi_dev->driver;
212
213 if (acpi_drv && acpi_drv->ops.resume)
214 return acpi_drv->ops.resume(acpi_dev);
215 return 0;
216}
217
218static int acpi_bus_match(struct device *dev, struct device_driver *drv)
219{
220 struct acpi_device *acpi_dev = to_acpi_device(dev);
221 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
222
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800223 return !acpi_match_ids(acpi_dev, acpi_drv->ids);
224}
225
226static int acpi_device_uevent(struct device *dev, char **envp, int num_envp,
227 char *buffer, int buffer_size)
228{
229 struct acpi_device *acpi_dev = to_acpi_device(dev);
230 int i = 0, length = 0, ret = 0;
231
232 if (acpi_dev->flags.hardware_id)
233 ret = add_uevent_var(envp, num_envp, &i,
234 buffer, buffer_size, &length,
235 "HWID=%s", acpi_dev->pnp.hardware_id);
236 if (ret)
237 return -ENOMEM;
238 if (acpi_dev->flags.compatible_ids) {
239 int j;
240 struct acpi_compatible_id_list *cid_list;
241
242 cid_list = acpi_dev->pnp.cid_list;
243
244 for (j = 0; j < cid_list->count; j++) {
245 ret = add_uevent_var(envp, num_envp, &i, buffer,
246 buffer_size, &length, "COMPTID=%s",
247 cid_list->id[j].value);
248 if (ret)
249 return -ENOMEM;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800250 }
251 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800252
253 envp[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return 0;
255}
256
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800257static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
258static int acpi_start_single_object(struct acpi_device *);
259static int acpi_device_probe(struct device * dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800260{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800261 struct acpi_device *acpi_dev = to_acpi_device(dev);
262 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
263 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800265 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
266 if (!ret) {
Li Shaohuac4168bf2006-12-07 20:56:41 +0800267 if (acpi_dev->bus_ops.acpi_op_start)
268 acpi_start_single_object(acpi_dev);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800269 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
270 "Found driver [%s] for device [%s]\n",
271 acpi_drv->name, acpi_dev->pnp.bus_id));
272 get_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800273 }
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800274 return ret;
275}
276
277static int acpi_device_remove(struct device * dev)
278{
279 struct acpi_device *acpi_dev = to_acpi_device(dev);
280 struct acpi_driver *acpi_drv = acpi_dev->driver;
281
282 if (acpi_drv) {
283 if (acpi_drv->ops.stop)
Li Shaohua96333572006-12-07 20:56:46 +0800284 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800285 if (acpi_drv->ops.remove)
Li Shaohua96333572006-12-07 20:56:46 +0800286 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800287 }
288 acpi_dev->driver = NULL;
289 acpi_driver_data(dev) = NULL;
290
291 put_device(dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800292 return 0;
293}
294
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800295static void acpi_device_shutdown(struct device *dev)
Zhang Rui9e89dde2006-12-07 20:56:16 +0800296{
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800297 struct acpi_device *acpi_dev = to_acpi_device(dev);
298 struct acpi_driver *acpi_drv = acpi_dev->driver;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800299
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800300 if (acpi_drv && acpi_drv->ops.shutdown)
301 acpi_drv->ops.shutdown(acpi_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800303 return ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Zhang Rui9e89dde2006-12-07 20:56:16 +0800306static struct bus_type acpi_bus_type = {
307 .name = "acpi",
308 .suspend = acpi_device_suspend,
309 .resume = acpi_device_resume,
Patrick Mochel5d9464a2006-12-07 20:56:27 +0800310 .shutdown = acpi_device_shutdown,
311 .match = acpi_bus_match,
312 .probe = acpi_device_probe,
313 .remove = acpi_device_remove,
314 .uevent = acpi_device_uevent,
Zhang Rui9e89dde2006-12-07 20:56:16 +0800315};
316
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800317static int acpi_device_register(struct acpi_device *device,
Zhang Rui9e89dde2006-12-07 20:56:16 +0800318 struct acpi_device *parent)
319{
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800320 int result;
321 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
322 int found = 0;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800323 /*
324 * Linkage
325 * -------
326 * Link this device to its parent and siblings.
327 */
328 INIT_LIST_HEAD(&device->children);
329 INIT_LIST_HEAD(&device->node);
330 INIT_LIST_HEAD(&device->g_list);
331 INIT_LIST_HEAD(&device->wakeup_list);
332
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800333 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
334 if (!new_bus_id) {
335 printk(KERN_ERR PREFIX "Memory allocation error\n");
336 return -ENOMEM;
337 }
338
Zhang Rui9e89dde2006-12-07 20:56:16 +0800339 spin_lock(&acpi_device_lock);
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800340 /*
341 * Find suitable bus_id and instance number in acpi_bus_id_list
342 * If failed, create one and link it into acpi_bus_id_list
343 */
344 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
345 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "PNPIDNON")) {
346 acpi_device_bus_id->instance_no ++;
347 found = 1;
348 kfree(new_bus_id);
349 break;
350 }
351 }
352 if(!found) {
353 acpi_device_bus_id = new_bus_id;
354 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "PNPIDNON");
355 acpi_device_bus_id->instance_no = 0;
356 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
357 }
358 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
359
Zhang Rui9e89dde2006-12-07 20:56:16 +0800360 if (device->parent) {
361 list_add_tail(&device->node, &device->parent->children);
362 list_add_tail(&device->g_list, &device->parent->g_list);
363 } else
364 list_add_tail(&device->g_list, &acpi_device_list);
365 if (device->wakeup.flags.valid)
366 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
367 spin_unlock(&acpi_device_lock);
368
Patrick Mochel1890a972006-12-07 20:56:31 +0800369 if (device->parent)
370 device->dev.parent = &parent->dev;
371 device->dev.bus = &acpi_bus_type;
372 device_initialize(&device->dev);
Patrick Mochel1890a972006-12-07 20:56:31 +0800373 device->dev.release = &acpi_device_release;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800374 result = device_add(&device->dev);
375 if(result) {
376 printk("Error adding device %s", device->dev.bus_id);
377 goto end;
378 }
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800379
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800380 result = acpi_device_setup_files(device);
381 if(result)
382 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
383
Li Shaohua96333572006-12-07 20:56:46 +0800384 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
Zhang Ruie49bd2d2006-12-08 17:23:43 +0800385 return 0;
386 end:
387 spin_lock(&acpi_device_lock);
388 if (device->parent) {
389 list_del(&device->node);
390 list_del(&device->g_list);
391 } else
392 list_del(&device->g_list);
393 list_del(&device->wakeup_list);
394 spin_unlock(&acpi_device_lock);
395 return result;
Zhang Rui9e89dde2006-12-07 20:56:16 +0800396}
397
398static void acpi_device_unregister(struct acpi_device *device, int type)
399{
400 spin_lock(&acpi_device_lock);
401 if (device->parent) {
402 list_del(&device->node);
403 list_del(&device->g_list);
404 } else
405 list_del(&device->g_list);
406
407 list_del(&device->wakeup_list);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800408 spin_unlock(&acpi_device_lock);
409
410 acpi_detach_data(device->handle, acpi_bus_data_handler);
Patrick Mochel1890a972006-12-07 20:56:31 +0800411
Patrick Mochelf883d9d2006-12-07 20:56:38 +0800412 acpi_device_remove_files(device);
Patrick Mochel1890a972006-12-07 20:56:31 +0800413 device_unregister(&device->dev);
Zhang Rui9e89dde2006-12-07 20:56:16 +0800414}
415
416/* --------------------------------------------------------------------------
417 Driver Management
418 -------------------------------------------------------------------------- */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500420 * acpi_bus_driver_init - add a device to a driver
421 * @device: the device to add and initialize
422 * @driver: driver for the device
423 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 * Used to initialize a device via its device driver. Called whenever a
Patrick Mochel1890a972006-12-07 20:56:31 +0800425 * driver is bound to a device. Invokes the driver's add() ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
427static int
Len Brown4be44fc2005-08-05 00:44:28 -0400428acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Len Brown4be44fc2005-08-05 00:44:28 -0400430 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (!device || !driver)
Patrick Mocheld550d982006-06-27 00:41:40 -0400434 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (!driver->ops.add)
Patrick Mocheld550d982006-06-27 00:41:40 -0400437 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 result = driver->ops.add(device);
440 if (result) {
441 device->driver = NULL;
442 acpi_driver_data(device) = NULL;
Patrick Mocheld550d982006-06-27 00:41:40 -0400443 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 device->driver = driver;
447
448 /*
449 * TBD - Configuration Management: Assign resources to device based
450 * upon possible configuration and currently allocated resources.
451 */
452
Len Brown4be44fc2005-08-05 00:44:28 -0400453 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
454 "Driver successfully bound to device\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400455 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700456}
457
Adrian Bunk8713cbe2005-09-02 17:16:48 -0400458static int acpi_start_single_object(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -0700459{
460 int result = 0;
461 struct acpi_driver *driver;
462
Rajesh Shah3fb02732005-04-28 00:25:52 -0700463
464 if (!(driver = device->driver))
Patrick Mocheld550d982006-06-27 00:41:40 -0400465 return 0;
Rajesh Shah3fb02732005-04-28 00:25:52 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (driver->ops.start) {
468 result = driver->ops.start(device);
469 if (result && driver->ops.remove)
470 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
Patrick Mocheld550d982006-06-27 00:41:40 -0400473 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500477 * acpi_bus_register_driver - register a driver with the ACPI bus
478 * @driver: driver being registered
479 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 * Registers a driver with the ACPI bus. Searches the namespace for all
Bjorn Helgaas9d9f7492006-03-28 17:04:00 -0500481 * devices that match the driver's criteria and binds. Returns zero for
482 * success or a negative error status for failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
Len Brown4be44fc2005-08-05 00:44:28 -0400484int acpi_bus_register_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Patrick Mochel1890a972006-12-07 20:56:31 +0800486 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400489 return -ENODEV;
Patrick Mochel1890a972006-12-07 20:56:31 +0800490 driver->drv.name = driver->name;
491 driver->drv.bus = &acpi_bus_type;
492 driver->drv.owner = driver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Patrick Mochel1890a972006-12-07 20:56:31 +0800494 ret = driver_register(&driver->drv);
495 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Len Brown4be44fc2005-08-05 00:44:28 -0400498EXPORT_SYMBOL(acpi_bus_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500/**
Randy Dunlapd758a8f2006-01-06 01:31:00 -0500501 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
502 * @driver: driver to unregister
503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * Unregisters a driver with the ACPI bus. Searches the namespace for all
505 * devices that match the driver's criteria and unbinds.
506 */
Bjorn Helgaas06ea8e02006-04-27 05:25:00 -0400507void acpi_bus_unregister_driver(struct acpi_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Patrick Mochel1890a972006-12-07 20:56:31 +0800509 driver_unregister(&driver->drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
Len Brown4be44fc2005-08-05 00:44:28 -0400511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512EXPORT_SYMBOL(acpi_bus_unregister_driver);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514/* --------------------------------------------------------------------------
515 Device Enumeration
516 -------------------------------------------------------------------------- */
Len Brownc8f7a622006-07-09 17:22:28 -0400517acpi_status
518acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
519{
520 acpi_status status;
521 acpi_handle tmp;
522 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
523 union acpi_object *obj;
524
525 status = acpi_get_handle(handle, "_EJD", &tmp);
526 if (ACPI_FAILURE(status))
527 return status;
528
529 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
530 if (ACPI_SUCCESS(status)) {
531 obj = buffer.pointer;
532 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
533 kfree(buffer.pointer);
534 }
535 return status;
536}
537EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
538
Zhang Rui9e89dde2006-12-07 20:56:16 +0800539void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
540{
541
542 /* TBD */
543
544 return;
545}
546
547int acpi_match_ids(struct acpi_device *device, char *ids)
548{
549 if (device->flags.hardware_id)
550 if (strstr(ids, device->pnp.hardware_id))
551 return 0;
552
553 if (device->flags.compatible_ids) {
554 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
555 int i;
556
557 /* compare multiple _CID entries against driver ids */
558 for (i = 0; i < cid_list->count; i++) {
559 if (strstr(ids, cid_list->id[i].value))
560 return 0;
561 }
562 }
563 return -ENOENT;
564}
565
566static int acpi_bus_get_perf_flags(struct acpi_device *device)
567{
568 device->performance.state = ACPI_STATE_UNKNOWN;
569 return 0;
570}
571
572static acpi_status
573acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
574 union acpi_object *package)
575{
576 int i = 0;
577 union acpi_object *element = NULL;
578
579 if (!device || !package || (package->package.count < 2))
580 return AE_BAD_PARAMETER;
581
582 element = &(package->package.elements[0]);
583 if (!element)
584 return AE_BAD_PARAMETER;
585 if (element->type == ACPI_TYPE_PACKAGE) {
586 if ((element->package.count < 2) ||
587 (element->package.elements[0].type !=
588 ACPI_TYPE_LOCAL_REFERENCE)
589 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
590 return AE_BAD_DATA;
591 device->wakeup.gpe_device =
592 element->package.elements[0].reference.handle;
593 device->wakeup.gpe_number =
594 (u32) element->package.elements[1].integer.value;
595 } else if (element->type == ACPI_TYPE_INTEGER) {
596 device->wakeup.gpe_number = element->integer.value;
597 } else
598 return AE_BAD_DATA;
599
600 element = &(package->package.elements[1]);
601 if (element->type != ACPI_TYPE_INTEGER) {
602 return AE_BAD_DATA;
603 }
604 device->wakeup.sleep_state = element->integer.value;
605
606 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
607 return AE_NO_MEMORY;
608 }
609 device->wakeup.resources.count = package->package.count - 2;
610 for (i = 0; i < device->wakeup.resources.count; i++) {
611 element = &(package->package.elements[i + 2]);
612 if (element->type != ACPI_TYPE_ANY) {
613 return AE_BAD_DATA;
614 }
615
616 device->wakeup.resources.handles[i] = element->reference.handle;
617 }
618
619 return AE_OK;
620}
621
622static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
623{
624 acpi_status status = 0;
625 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
626 union acpi_object *package = NULL;
627
628
629 /* _PRW */
630 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
631 if (ACPI_FAILURE(status)) {
632 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
633 goto end;
634 }
635
636 package = (union acpi_object *)buffer.pointer;
637 status = acpi_bus_extract_wakeup_device_power_package(device, package);
638 if (ACPI_FAILURE(status)) {
639 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
640 goto end;
641 }
642
643 kfree(buffer.pointer);
644
645 device->wakeup.flags.valid = 1;
646 /* Power button, Lid switch always enable wakeup */
647 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
648 device->wakeup.flags.run_wake = 1;
649
650 end:
651 if (ACPI_FAILURE(status))
652 device->flags.wake_capable = 0;
653 return 0;
654}
655
656static int acpi_bus_get_power_flags(struct acpi_device *device)
657{
658 acpi_status status = 0;
659 acpi_handle handle = NULL;
660 u32 i = 0;
661
662
663 /*
664 * Power Management Flags
665 */
666 status = acpi_get_handle(device->handle, "_PSC", &handle);
667 if (ACPI_SUCCESS(status))
668 device->power.flags.explicit_get = 1;
669 status = acpi_get_handle(device->handle, "_IRC", &handle);
670 if (ACPI_SUCCESS(status))
671 device->power.flags.inrush_current = 1;
672
673 /*
674 * Enumerate supported power management states
675 */
676 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
677 struct acpi_device_power_state *ps = &device->power.states[i];
678 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
679
680 /* Evaluate "_PRx" to se if power resources are referenced */
681 acpi_evaluate_reference(device->handle, object_name, NULL,
682 &ps->resources);
683 if (ps->resources.count) {
684 device->power.flags.power_resources = 1;
685 ps->flags.valid = 1;
686 }
687
688 /* Evaluate "_PSx" to see if we can do explicit sets */
689 object_name[2] = 'S';
690 status = acpi_get_handle(device->handle, object_name, &handle);
691 if (ACPI_SUCCESS(status)) {
692 ps->flags.explicit_set = 1;
693 ps->flags.valid = 1;
694 }
695
696 /* State is valid if we have some power control */
697 if (ps->resources.count || ps->flags.explicit_set)
698 ps->flags.valid = 1;
699
700 ps->power = -1; /* Unknown - driver assigned */
701 ps->latency = -1; /* Unknown - driver assigned */
702 }
703
704 /* Set defaults for D0 and D3 states (always valid) */
705 device->power.states[ACPI_STATE_D0].flags.valid = 1;
706 device->power.states[ACPI_STATE_D0].power = 100;
707 device->power.states[ACPI_STATE_D3].flags.valid = 1;
708 device->power.states[ACPI_STATE_D3].power = 0;
709
710 /* TBD: System wake support and resource requirements. */
711
712 device->power.state = ACPI_STATE_UNKNOWN;
713
714 return 0;
715}
Len Brownc8f7a622006-07-09 17:22:28 -0400716
Len Brown4be44fc2005-08-05 00:44:28 -0400717static int acpi_bus_get_flags(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Len Brown4be44fc2005-08-05 00:44:28 -0400719 acpi_status status = AE_OK;
720 acpi_handle temp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /* Presence of _STA indicates 'dynamic_status' */
724 status = acpi_get_handle(device->handle, "_STA", &temp);
725 if (ACPI_SUCCESS(status))
726 device->flags.dynamic_status = 1;
727
728 /* Presence of _CID indicates 'compatible_ids' */
729 status = acpi_get_handle(device->handle, "_CID", &temp);
730 if (ACPI_SUCCESS(status))
731 device->flags.compatible_ids = 1;
732
733 /* Presence of _RMV indicates 'removable' */
734 status = acpi_get_handle(device->handle, "_RMV", &temp);
735 if (ACPI_SUCCESS(status))
736 device->flags.removable = 1;
737
738 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
739 status = acpi_get_handle(device->handle, "_EJD", &temp);
740 if (ACPI_SUCCESS(status))
741 device->flags.ejectable = 1;
742 else {
743 status = acpi_get_handle(device->handle, "_EJ0", &temp);
744 if (ACPI_SUCCESS(status))
745 device->flags.ejectable = 1;
746 }
747
748 /* Presence of _LCK indicates 'lockable' */
749 status = acpi_get_handle(device->handle, "_LCK", &temp);
750 if (ACPI_SUCCESS(status))
751 device->flags.lockable = 1;
752
753 /* Presence of _PS0|_PR0 indicates 'power manageable' */
754 status = acpi_get_handle(device->handle, "_PS0", &temp);
755 if (ACPI_FAILURE(status))
756 status = acpi_get_handle(device->handle, "_PR0", &temp);
757 if (ACPI_SUCCESS(status))
758 device->flags.power_manageable = 1;
759
760 /* Presence of _PRW indicates wake capable */
761 status = acpi_get_handle(device->handle, "_PRW", &temp);
762 if (ACPI_SUCCESS(status))
763 device->flags.wake_capable = 1;
764
765 /* TBD: Peformance management */
766
Patrick Mocheld550d982006-06-27 00:41:40 -0400767 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
Len Brown4be44fc2005-08-05 00:44:28 -0400770static void acpi_device_get_busid(struct acpi_device *device,
771 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Len Brown4be44fc2005-08-05 00:44:28 -0400773 char bus_id[5] = { '?', 0 };
774 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
775 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 /*
778 * Bus ID
779 * ------
780 * The device's Bus ID is simply the object name.
781 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
782 */
783 switch (type) {
784 case ACPI_BUS_TYPE_SYSTEM:
785 strcpy(device->pnp.bus_id, "ACPI");
786 break;
787 case ACPI_BUS_TYPE_POWER_BUTTON:
788 strcpy(device->pnp.bus_id, "PWRF");
789 break;
790 case ACPI_BUS_TYPE_SLEEP_BUTTON:
791 strcpy(device->pnp.bus_id, "SLPF");
792 break;
793 default:
794 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
795 /* Clean up trailing underscores (if any) */
796 for (i = 3; i > 1; i--) {
797 if (bus_id[i] == '_')
798 bus_id[i] = '\0';
799 else
800 break;
801 }
802 strcpy(device->pnp.bus_id, bus_id);
803 break;
804 }
805}
806
Zhang Ruiae843332006-12-07 20:57:10 +0800807static int
808acpi_video_bus_match(struct acpi_device *device)
809{
810 acpi_handle h_dummy1;
811 acpi_handle h_dummy2;
812 acpi_handle h_dummy3;
813
814
815 if (!device)
816 return -EINVAL;
817
818 /* Since there is no HID, CID for ACPI Video drivers, we have
819 * to check well known required nodes for each feature we support.
820 */
821
822 /* Does this device able to support video switching ? */
823 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy1)) &&
824 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy2)))
825 return 0;
826
827 /* Does this device able to retrieve a video ROM ? */
828 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy1)))
829 return 0;
830
831 /* Does this device able to configure which video head to be POSTed ? */
832 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy1)) &&
833 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy2)) &&
834 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy3)))
835 return 0;
836
837 return -ENODEV;
838}
839
840static int acpi_pci_bridge_match(struct acpi_device *device)
841{
842 acpi_status status;
843 acpi_handle handle;
844
845 /* pci bridge has _PRT but isn't PNP0A03 */
846 status = acpi_get_handle(device->handle, METHOD_NAME__PRT, &handle);
847 if (ACPI_FAILURE(status))
848 return -ENODEV;
849 if (!acpi_match_ids(device, "PNP0A03"))
850 return -ENODEV;
851 return 0;
852}
853
Len Brown4be44fc2005-08-05 00:44:28 -0400854static void acpi_device_set_id(struct acpi_device *device,
855 struct acpi_device *parent, acpi_handle handle,
856 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Len Brown4be44fc2005-08-05 00:44:28 -0400858 struct acpi_device_info *info;
859 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
860 char *hid = NULL;
861 char *uid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 struct acpi_compatible_id_list *cid_list = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400863 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 switch (type) {
866 case ACPI_BUS_TYPE_DEVICE:
867 status = acpi_get_object_info(handle, &buffer);
868 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400869 printk("%s: Error reading device info\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return;
871 }
872
873 info = buffer.pointer;
874 if (info->valid & ACPI_VALID_HID)
875 hid = info->hardware_id.value;
876 if (info->valid & ACPI_VALID_UID)
877 uid = info->unique_id.value;
878 if (info->valid & ACPI_VALID_CID)
879 cid_list = &info->compatibility_id;
880 if (info->valid & ACPI_VALID_ADR) {
881 device->pnp.bus_address = info->address;
882 device->flags.bus_address = 1;
883 }
Zhang Ruiae843332006-12-07 20:57:10 +0800884
885 if(!(info->valid & (ACPI_VALID_HID | ACPI_VALID_CID))){
886 status = acpi_video_bus_match(device);
887 if(ACPI_SUCCESS(status))
888 hid = ACPI_VIDEO_HID;
889
890 status = acpi_pci_bridge_match(device);
891 if(ACPI_SUCCESS(status))
892 hid = ACPI_PCI_BRIDGE_HID;
893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 break;
895 case ACPI_BUS_TYPE_POWER:
896 hid = ACPI_POWER_HID;
897 break;
898 case ACPI_BUS_TYPE_PROCESSOR:
899 hid = ACPI_PROCESSOR_HID;
900 break;
901 case ACPI_BUS_TYPE_SYSTEM:
902 hid = ACPI_SYSTEM_HID;
903 break;
904 case ACPI_BUS_TYPE_THERMAL:
905 hid = ACPI_THERMAL_HID;
906 break;
907 case ACPI_BUS_TYPE_POWER_BUTTON:
908 hid = ACPI_BUTTON_HID_POWERF;
909 break;
910 case ACPI_BUS_TYPE_SLEEP_BUTTON:
911 hid = ACPI_BUTTON_HID_SLEEPF;
912 break;
913 }
914
915 /*
916 * \_SB
917 * ----
918 * Fix for the system root bus device -- the only root-level device.
919 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500920 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 hid = ACPI_BUS_HID;
922 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
923 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
924 }
925
926 if (hid) {
927 strcpy(device->pnp.hardware_id, hid);
928 device->flags.hardware_id = 1;
929 }
930 if (uid) {
931 strcpy(device->pnp.unique_id, uid);
932 device->flags.unique_id = 1;
933 }
934 if (cid_list) {
935 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
936 if (device->pnp.cid_list)
937 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
938 else
939 printk(KERN_ERR "Memory allocation error\n");
940 }
941
Len Brown02438d82006-06-30 03:19:10 -0400942 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Len Brown4be44fc2005-08-05 00:44:28 -0400945static int acpi_device_set_context(struct acpi_device *device, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
947 acpi_status status = AE_OK;
948 int result = 0;
949 /*
950 * Context
951 * -------
952 * Attach this 'struct acpi_device' to the ACPI object. This makes
953 * resolutions from handle->device very efficient. Note that we need
954 * to be careful with fixed-feature devices as they all attach to the
955 * root object.
956 */
Len Brown4be44fc2005-08-05 00:44:28 -0400957 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
959 status = acpi_attach_data(device->handle,
Len Brown4be44fc2005-08-05 00:44:28 -0400960 acpi_bus_data_handler, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
962 if (ACPI_FAILURE(status)) {
963 printk("Error attaching device data\n");
964 result = -ENODEV;
965 }
966 }
967 return result;
968}
969
Len Brown4be44fc2005-08-05 00:44:28 -0400970static void acpi_device_get_debug_info(struct acpi_device *device,
971 acpi_handle handle, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973#ifdef CONFIG_ACPI_DEBUG_OUTPUT
Len Brown4be44fc2005-08-05 00:44:28 -0400974 char *type_string = NULL;
975 char name[80] = { '?', '\0' };
976 struct acpi_buffer buffer = { sizeof(name), name };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 switch (type) {
979 case ACPI_BUS_TYPE_DEVICE:
980 type_string = "Device";
981 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
982 break;
983 case ACPI_BUS_TYPE_POWER:
984 type_string = "Power Resource";
985 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
986 break;
987 case ACPI_BUS_TYPE_PROCESSOR:
988 type_string = "Processor";
989 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
990 break;
991 case ACPI_BUS_TYPE_SYSTEM:
992 type_string = "System";
993 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
994 break;
995 case ACPI_BUS_TYPE_THERMAL:
996 type_string = "Thermal Zone";
997 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
998 break;
999 case ACPI_BUS_TYPE_POWER_BUTTON:
1000 type_string = "Power Button";
1001 sprintf(name, "PWRB");
1002 break;
1003 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1004 type_string = "Sleep Button";
1005 sprintf(name, "SLPB");
1006 break;
1007 }
1008
1009 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
Len Brown4be44fc2005-08-05 00:44:28 -04001010#endif /*CONFIG_ACPI_DEBUG_OUTPUT */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Len Brown4be44fc2005-08-05 00:44:28 -04001013static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!dev)
Patrick Mocheld550d982006-06-27 00:41:40 -04001016 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Li Shaohua96333572006-12-07 20:56:46 +08001018 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
Patrick Mochel1890a972006-12-07 20:56:31 +08001019 device_release_driver(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 if (!rmdevice)
Patrick Mocheld550d982006-06-27 00:41:40 -04001022 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1025
Patrick Mocheld550d982006-06-27 00:41:40 -04001026 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Rajesh Shah3fb02732005-04-28 00:25:52 -07001029static int
Len Brown4be44fc2005-08-05 00:44:28 -04001030acpi_add_single_object(struct acpi_device **child,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001031 struct acpi_device *parent, acpi_handle handle, int type,
1032 struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Len Brown4be44fc2005-08-05 00:44:28 -04001034 int result = 0;
1035 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 if (!child)
Patrick Mocheld550d982006-06-27 00:41:40 -04001039 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
1042 if (!device) {
Len Brown64684632006-06-26 23:41:38 -04001043 printk(KERN_ERR PREFIX "Memory allocation error\n");
Patrick Mocheld550d982006-06-27 00:41:40 -04001044 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046 memset(device, 0, sizeof(struct acpi_device));
1047
1048 device->handle = handle;
1049 device->parent = parent;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001050 device->bus_ops = *ops; /* workround for not call .start */
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Len Brown4be44fc2005-08-05 00:44:28 -04001053 acpi_device_get_busid(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 /*
1056 * Flags
1057 * -----
1058 * Get prior to calling acpi_bus_get_status() so we know whether
1059 * or not _STA is present. Note that we only look for object
1060 * handles -- cannot evaluate objects until we know the device is
1061 * present and properly initialized.
1062 */
1063 result = acpi_bus_get_flags(device);
1064 if (result)
1065 goto end;
1066
1067 /*
1068 * Status
1069 * ------
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001070 * See if the device is present. We always assume that non-Device
1071 * and non-Processor objects (e.g. thermal zones, power resources,
1072 * etc.) are present, functioning, etc. (at least when parent object
1073 * is present). Note that _STA has a different meaning for some
1074 * objects (e.g. power resources) so we need to be careful how we use
1075 * it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
1077 switch (type) {
Keiichiro Tokunaga6940fab2005-03-30 23:15:47 -05001078 case ACPI_BUS_TYPE_PROCESSOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 case ACPI_BUS_TYPE_DEVICE:
1080 result = acpi_bus_get_status(device);
1081 if (ACPI_FAILURE(result) || !device->status.present) {
1082 result = -ENOENT;
1083 goto end;
1084 }
1085 break;
1086 default:
1087 STRUCT_TO_INT(device->status) = 0x0F;
1088 break;
1089 }
1090
1091 /*
1092 * Initialize Device
1093 * -----------------
1094 * TBD: Synch with Core's enumeration/initialization process.
1095 */
1096
1097 /*
1098 * Hardware ID, Unique ID, & Bus Address
1099 * -------------------------------------
1100 */
Len Brown4be44fc2005-08-05 00:44:28 -04001101 acpi_device_set_id(device, parent, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /*
1104 * Power Management
1105 * ----------------
1106 */
1107 if (device->flags.power_manageable) {
1108 result = acpi_bus_get_power_flags(device);
1109 if (result)
1110 goto end;
1111 }
1112
Len Brown4be44fc2005-08-05 00:44:28 -04001113 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 * Wakeup device management
1115 *-----------------------
1116 */
1117 if (device->flags.wake_capable) {
1118 result = acpi_bus_get_wakeup_device_flags(device);
1119 if (result)
1120 goto end;
1121 }
1122
1123 /*
1124 * Performance Management
1125 * ----------------------
1126 */
1127 if (device->flags.performance_manageable) {
1128 result = acpi_bus_get_perf_flags(device);
1129 if (result)
1130 goto end;
1131 }
1132
Len Brown4be44fc2005-08-05 00:44:28 -04001133 if ((result = acpi_device_set_context(device, type)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 goto end;
1135
Len Brown4be44fc2005-08-05 00:44:28 -04001136 acpi_device_get_debug_info(device, handle, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Zhang Ruie49bd2d2006-12-08 17:23:43 +08001138 result = acpi_device_register(device, parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Len Brown4be44fc2005-08-05 00:44:28 -04001140 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!result)
1142 *child = device;
1143 else {
Jesper Juhl6044ec82005-11-07 01:01:32 -08001144 kfree(device->pnp.cid_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 kfree(device);
1146 }
1147
Patrick Mocheld550d982006-06-27 00:41:40 -04001148 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Len Brown4be44fc2005-08-05 00:44:28 -04001151static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
Len Brown4be44fc2005-08-05 00:44:28 -04001153 acpi_status status = AE_OK;
1154 struct acpi_device *parent = NULL;
1155 struct acpi_device *child = NULL;
1156 acpi_handle phandle = NULL;
1157 acpi_handle chandle = NULL;
1158 acpi_object_type type = 0;
1159 u32 level = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 if (!start)
Patrick Mocheld550d982006-06-27 00:41:40 -04001163 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
1165 parent = start;
1166 phandle = start->handle;
Len Brown4be44fc2005-08-05 00:44:28 -04001167
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
1169 * Parse through the ACPI namespace, identify all 'devices', and
1170 * create a new 'struct acpi_device' for each.
1171 */
1172 while ((level > 0) && parent) {
1173
1174 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001175 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 /*
1178 * If this scope is exhausted then move our way back up.
1179 */
1180 if (ACPI_FAILURE(status)) {
1181 level--;
1182 chandle = phandle;
1183 acpi_get_parent(phandle, &phandle);
1184 if (parent->parent)
1185 parent = parent->parent;
1186 continue;
1187 }
1188
1189 status = acpi_get_type(chandle, &type);
1190 if (ACPI_FAILURE(status))
1191 continue;
1192
1193 /*
1194 * If this is a scope object then parse it (depth-first).
1195 */
1196 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1197 level++;
1198 phandle = chandle;
1199 chandle = NULL;
1200 continue;
1201 }
1202
1203 /*
1204 * We're only interested in objects that we consider 'devices'.
1205 */
1206 switch (type) {
1207 case ACPI_TYPE_DEVICE:
1208 type = ACPI_BUS_TYPE_DEVICE;
1209 break;
1210 case ACPI_TYPE_PROCESSOR:
1211 type = ACPI_BUS_TYPE_PROCESSOR;
1212 break;
1213 case ACPI_TYPE_THERMAL:
1214 type = ACPI_BUS_TYPE_THERMAL;
1215 break;
1216 case ACPI_TYPE_POWER:
1217 type = ACPI_BUS_TYPE_POWER;
1218 break;
1219 default:
1220 continue;
1221 }
1222
Rajesh Shah3fb02732005-04-28 00:25:52 -07001223 if (ops->acpi_op_add)
1224 status = acpi_add_single_object(&child, parent,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001225 chandle, type, ops);
Len Brown4be44fc2005-08-05 00:44:28 -04001226 else
Rajesh Shah3fb02732005-04-28 00:25:52 -07001227 status = acpi_bus_get_device(chandle, &child);
1228
Len Brown4be44fc2005-08-05 00:44:28 -04001229 if (ACPI_FAILURE(status))
1230 continue;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001231
Li Shaohuac4168bf2006-12-07 20:56:41 +08001232 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
Rajesh Shah3fb02732005-04-28 00:25:52 -07001233 status = acpi_start_single_object(child);
1234 if (ACPI_FAILURE(status))
1235 continue;
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /*
1239 * If the device is present, enabled, and functioning then
1240 * parse its scope (depth-first). Note that we need to
1241 * represent absent devices to facilitate PnP notifications
1242 * -- but only the subtree head (not all of its children,
1243 * which will be enumerated when the parent is inserted).
1244 *
1245 * TBD: Need notifications and other detection mechanisms
Len Brown4be44fc2005-08-05 00:44:28 -04001246 * in place before we can fully implement this.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
1248 if (child->status.present) {
1249 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1250 NULL, NULL);
1251 if (ACPI_SUCCESS(status)) {
1252 level++;
1253 phandle = chandle;
1254 chandle = NULL;
1255 parent = child;
1256 }
1257 }
1258 }
1259
Patrick Mocheld550d982006-06-27 00:41:40 -04001260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Rajesh Shah3fb02732005-04-28 00:25:52 -07001263int
Len Brown4be44fc2005-08-05 00:44:28 -04001264acpi_bus_add(struct acpi_device **child,
1265 struct acpi_device *parent, acpi_handle handle, int type)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001266{
1267 int result;
1268 struct acpi_bus_ops ops;
1269
Li Shaohuac4168bf2006-12-07 20:56:41 +08001270 memset(&ops, 0, sizeof(ops));
1271 ops.acpi_op_add = 1;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001272
Li Shaohuac4168bf2006-12-07 20:56:41 +08001273 result = acpi_add_single_object(child, parent, handle, type, &ops);
1274 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001275 result = acpi_bus_scan(*child, &ops);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001276
Patrick Mocheld550d982006-06-27 00:41:40 -04001277 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001278}
Len Brown4be44fc2005-08-05 00:44:28 -04001279
Rajesh Shah3fb02732005-04-28 00:25:52 -07001280EXPORT_SYMBOL(acpi_bus_add);
1281
Len Brown4be44fc2005-08-05 00:44:28 -04001282int acpi_bus_start(struct acpi_device *device)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001283{
1284 int result;
1285 struct acpi_bus_ops ops;
1286
Rajesh Shah3fb02732005-04-28 00:25:52 -07001287
1288 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -04001289 return -EINVAL;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001290
1291 result = acpi_start_single_object(device);
1292 if (!result) {
1293 memset(&ops, 0, sizeof(ops));
1294 ops.acpi_op_start = 1;
1295 result = acpi_bus_scan(device, &ops);
1296 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001297 return result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001298}
Len Brown4be44fc2005-08-05 00:44:28 -04001299
Rajesh Shah3fb02732005-04-28 00:25:52 -07001300EXPORT_SYMBOL(acpi_bus_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Kristen Accardiceaba662006-02-23 17:56:01 -08001302int acpi_bus_trim(struct acpi_device *start, int rmdevice)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Len Brown4be44fc2005-08-05 00:44:28 -04001304 acpi_status status;
1305 struct acpi_device *parent, *child;
1306 acpi_handle phandle, chandle;
1307 acpi_object_type type;
1308 u32 level = 1;
1309 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Len Brown4be44fc2005-08-05 00:44:28 -04001311 parent = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 phandle = start->handle;
1313 child = chandle = NULL;
1314
1315 while ((level > 0) && parent && (!err)) {
1316 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
Len Brown4be44fc2005-08-05 00:44:28 -04001317 chandle, &chandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319 /*
1320 * If this scope is exhausted then move our way back up.
1321 */
1322 if (ACPI_FAILURE(status)) {
1323 level--;
1324 chandle = phandle;
1325 acpi_get_parent(phandle, &phandle);
1326 child = parent;
1327 parent = parent->parent;
1328
1329 if (level == 0)
1330 err = acpi_bus_remove(child, rmdevice);
1331 else
1332 err = acpi_bus_remove(child, 1);
1333
1334 continue;
1335 }
1336
1337 status = acpi_get_type(chandle, &type);
1338 if (ACPI_FAILURE(status)) {
1339 continue;
1340 }
1341 /*
1342 * If there is a device corresponding to chandle then
1343 * parse it (depth-first).
1344 */
1345 if (acpi_bus_get_device(chandle, &child) == 0) {
1346 level++;
1347 phandle = chandle;
1348 chandle = NULL;
1349 parent = child;
1350 }
1351 continue;
1352 }
1353 return err;
1354}
Kristen Accardiceaba662006-02-23 17:56:01 -08001355EXPORT_SYMBOL_GPL(acpi_bus_trim);
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Len Brown4be44fc2005-08-05 00:44:28 -04001358static int acpi_bus_scan_fixed(struct acpi_device *root)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Len Brown4be44fc2005-08-05 00:44:28 -04001360 int result = 0;
1361 struct acpi_device *device = NULL;
Li Shaohuac4168bf2006-12-07 20:56:41 +08001362 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 if (!root)
Patrick Mocheld550d982006-06-27 00:41:40 -04001365 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366
Li Shaohuac4168bf2006-12-07 20:56:41 +08001367 memset(&ops, 0, sizeof(ops));
1368 ops.acpi_op_add = 1;
1369 ops.acpi_op_start = 1;
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 /*
1372 * Enumerate all fixed-feature devices.
1373 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001374 if (acpi_fadt.pwr_button == 0) {
1375 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001376 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001377 ACPI_BUS_TYPE_POWER_BUTTON,
1378 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Rajesh Shah3fb02732005-04-28 00:25:52 -07001381 if (acpi_fadt.sleep_button == 0) {
1382 result = acpi_add_single_object(&device, acpi_root,
Len Brown4be44fc2005-08-05 00:44:28 -04001383 NULL,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001384 ACPI_BUS_TYPE_SLEEP_BUTTON,
1385 &ops);
Rajesh Shah3fb02732005-04-28 00:25:52 -07001386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Patrick Mocheld550d982006-06-27 00:41:40 -04001388 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391static int __init acpi_scan_init(void)
1392{
1393 int result;
Rajesh Shah3fb02732005-04-28 00:25:52 -07001394 struct acpi_bus_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
1397 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -04001398 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Li Shaohuac4168bf2006-12-07 20:56:41 +08001400 memset(&ops, 0, sizeof(ops));
1401 ops.acpi_op_add = 1;
1402 ops.acpi_op_start = 1;
1403
Patrick Mochel5b327262006-05-10 10:33:00 -04001404 result = bus_register(&acpi_bus_type);
1405 if (result) {
1406 /* We don't want to quit even if we failed to add suspend/resume */
1407 printk(KERN_ERR PREFIX "Could not register bus type\n");
1408 }
1409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 /*
1411 * Create the root device in the bus's device tree
1412 */
Rajesh Shah3fb02732005-04-28 00:25:52 -07001413 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
Li Shaohuac4168bf2006-12-07 20:56:41 +08001414 ACPI_BUS_TYPE_SYSTEM, &ops);
Patrick Mochel5b327262006-05-10 10:33:00 -04001415 if (result)
1416 goto Done;
1417
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 /*
1419 * Enumerate devices in the ACPI namespace.
1420 */
1421 result = acpi_bus_scan_fixed(acpi_root);
Li Shaohuac4168bf2006-12-07 20:56:41 +08001422 if (!result)
Rajesh Shah3fb02732005-04-28 00:25:52 -07001423 result = acpi_bus_scan(acpi_root, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (result)
1426 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1427
Len Brown4be44fc2005-08-05 00:44:28 -04001428 Done:
Patrick Mocheld550d982006-06-27 00:41:40 -04001429 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
1432subsys_initcall(acpi_scan_init);