Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * scan.c - support for transforming the ACPI namespace into individual objects | |
3 | */ | |
4 | ||
5 | #include <linux/module.h> | |
6 | #include <linux/init.h> | |
9b6d97b6 | 7 | #include <linux/kernel.h> |
1da177e4 LT |
8 | #include <linux/acpi.h> |
9 | ||
10 | #include <acpi/acpi_drivers.h> | |
11 | #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */ | |
12 | ||
1da177e4 | 13 | #define _COMPONENT ACPI_BUS_COMPONENT |
4be44fcd | 14 | ACPI_MODULE_NAME("scan") |
1da177e4 | 15 | #define STRUCT_TO_INT(s) (*((int*)&s)) |
4be44fcd | 16 | extern struct acpi_device *acpi_root; |
1da177e4 LT |
17 | |
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 | ||
23 | static LIST_HEAD(acpi_device_list); | |
24 | DEFINE_SPINLOCK(acpi_device_lock); | |
25 | LIST_HEAD(acpi_wakeup_device_list); | |
26 | ||
1da177e4 | 27 | |
1890a97a | 28 | static void acpi_device_release_legacy(struct kobject *kobj) |
1da177e4 | 29 | { |
4be44fcd | 30 | struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj); |
6044ec88 | 31 | kfree(dev->pnp.cid_list); |
1da177e4 LT |
32 | kfree(dev); |
33 | } | |
34 | ||
35 | struct acpi_device_attribute { | |
36 | struct attribute attr; | |
4be44fcd LB |
37 | ssize_t(*show) (struct acpi_device *, char *); |
38 | ssize_t(*store) (struct acpi_device *, const char *, size_t); | |
1da177e4 LT |
39 | }; |
40 | ||
41 | typedef void acpi_device_sysfs_files(struct kobject *, | |
4be44fcd | 42 | const struct attribute *); |
1da177e4 LT |
43 | |
44 | static void setup_sys_fs_device_files(struct acpi_device *dev, | |
4be44fcd | 45 | acpi_device_sysfs_files * func); |
1da177e4 LT |
46 | |
47 | #define create_sysfs_device_files(dev) \ | |
48 | setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file) | |
49 | #define remove_sysfs_device_files(dev) \ | |
50 | setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file) | |
51 | ||
1d268b0a | 52 | #define to_acpi_dev(n) container_of(n, struct acpi_device, kobj) |
1da177e4 LT |
53 | #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr); |
54 | ||
55 | static ssize_t acpi_device_attr_show(struct kobject *kobj, | |
4be44fcd | 56 | struct attribute *attr, char *buf) |
1da177e4 | 57 | { |
1d268b0a | 58 | struct acpi_device *device = to_acpi_dev(kobj); |
1da177e4 | 59 | struct acpi_device_attribute *attribute = to_handle_attr(attr); |
70f2817a | 60 | return attribute->show ? attribute->show(device, buf) : -EIO; |
1da177e4 LT |
61 | } |
62 | static ssize_t acpi_device_attr_store(struct kobject *kobj, | |
4be44fcd LB |
63 | struct attribute *attr, const char *buf, |
64 | size_t len) | |
1da177e4 | 65 | { |
1d268b0a | 66 | struct acpi_device *device = to_acpi_dev(kobj); |
1da177e4 | 67 | struct acpi_device_attribute *attribute = to_handle_attr(attr); |
70f2817a | 68 | return attribute->store ? attribute->store(device, buf, len) : -EIO; |
1da177e4 LT |
69 | } |
70 | ||
71 | static struct sysfs_ops acpi_device_sysfs_ops = { | |
4be44fcd LB |
72 | .show = acpi_device_attr_show, |
73 | .store = acpi_device_attr_store, | |
1da177e4 LT |
74 | }; |
75 | ||
76 | static struct kobj_type ktype_acpi_ns = { | |
4be44fcd | 77 | .sysfs_ops = &acpi_device_sysfs_ops, |
1890a97a | 78 | .release = acpi_device_release_legacy, |
1da177e4 LT |
79 | }; |
80 | ||
312c004d | 81 | static int namespace_uevent(struct kset *kset, struct kobject *kobj, |
1da177e4 LT |
82 | char **envp, int num_envp, char *buffer, |
83 | int buffer_size) | |
84 | { | |
1d268b0a | 85 | struct acpi_device *dev = to_acpi_dev(kobj); |
1da177e4 LT |
86 | int i = 0; |
87 | int len = 0; | |
88 | ||
89 | if (!dev->driver) | |
90 | return 0; | |
91 | ||
312c004d KS |
92 | if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len, |
93 | "PHYSDEVDRIVER=%s", dev->driver->name)) | |
1da177e4 LT |
94 | return -ENOMEM; |
95 | ||
96 | envp[i] = NULL; | |
97 | ||
98 | return 0; | |
99 | } | |
100 | ||
312c004d KS |
101 | static struct kset_uevent_ops namespace_uevent_ops = { |
102 | .uevent = &namespace_uevent, | |
1da177e4 LT |
103 | }; |
104 | ||
105 | static struct kset acpi_namespace_kset = { | |
4be44fcd LB |
106 | .kobj = { |
107 | .name = "namespace", | |
108 | }, | |
1da177e4 | 109 | .subsys = &acpi_subsys, |
4be44fcd | 110 | .ktype = &ktype_acpi_ns, |
312c004d | 111 | .uevent_ops = &namespace_uevent_ops, |
1da177e4 LT |
112 | }; |
113 | ||
1da177e4 | 114 | /* -------------------------------------------------------------------------- |
312c004d | 115 | ACPI sysfs device file support |
1da177e4 | 116 | -------------------------------------------------------------------------- */ |
4be44fcd LB |
117 | static ssize_t acpi_eject_store(struct acpi_device *device, |
118 | const char *buf, size_t count); | |
1da177e4 LT |
119 | |
120 | #define ACPI_DEVICE_ATTR(_name,_mode,_show,_store) \ | |
121 | static struct acpi_device_attribute acpi_device_attr_##_name = \ | |
122 | __ATTR(_name, _mode, _show, _store) | |
123 | ||
124 | ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store); | |
125 | ||
126 | /** | |
127 | * setup_sys_fs_device_files - sets up the device files under device namespace | |
67be2dd1 MW |
128 | * @dev: acpi_device object |
129 | * @func: function pointer to create or destroy the device file | |
1da177e4 LT |
130 | */ |
131 | static void | |
4be44fcd LB |
132 | setup_sys_fs_device_files(struct acpi_device *dev, |
133 | acpi_device_sysfs_files * func) | |
1da177e4 | 134 | { |
4be44fcd LB |
135 | acpi_status status; |
136 | acpi_handle temp = NULL; | |
1da177e4 LT |
137 | |
138 | /* | |
139 | * If device has _EJ0, 'eject' file is created that is used to trigger | |
140 | * hot-removal function from userland. | |
141 | */ | |
142 | status = acpi_get_handle(dev->handle, "_EJ0", &temp); | |
143 | if (ACPI_SUCCESS(status)) | |
4be44fcd | 144 | (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr); |
1da177e4 LT |
145 | } |
146 | ||
4be44fcd | 147 | static int acpi_eject_operation(acpi_handle handle, int lockable) |
1da177e4 LT |
148 | { |
149 | struct acpi_object_list arg_list; | |
150 | union acpi_object arg; | |
151 | acpi_status status = AE_OK; | |
152 | ||
153 | /* | |
154 | * TBD: evaluate _PS3? | |
155 | */ | |
156 | ||
157 | if (lockable) { | |
158 | arg_list.count = 1; | |
159 | arg_list.pointer = &arg; | |
160 | arg.type = ACPI_TYPE_INTEGER; | |
161 | arg.integer.value = 0; | |
162 | acpi_evaluate_object(handle, "_LCK", &arg_list, NULL); | |
163 | } | |
164 | ||
165 | arg_list.count = 1; | |
166 | arg_list.pointer = &arg; | |
167 | arg.type = ACPI_TYPE_INTEGER; | |
168 | arg.integer.value = 1; | |
169 | ||
170 | /* | |
171 | * TBD: _EJD support. | |
172 | */ | |
173 | ||
174 | status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL); | |
175 | if (ACPI_FAILURE(status)) { | |
4be44fcd | 176 | return (-ENODEV); |
1da177e4 LT |
177 | } |
178 | ||
4be44fcd | 179 | return (0); |
1da177e4 LT |
180 | } |
181 | ||
1da177e4 LT |
182 | static ssize_t |
183 | acpi_eject_store(struct acpi_device *device, const char *buf, size_t count) | |
184 | { | |
4be44fcd LB |
185 | int result; |
186 | int ret = count; | |
187 | int islockable; | |
188 | acpi_status status; | |
189 | acpi_handle handle; | |
190 | acpi_object_type type = 0; | |
1da177e4 LT |
191 | |
192 | if ((!count) || (buf[0] != '1')) { | |
193 | return -EINVAL; | |
194 | } | |
1da177e4 LT |
195 | #ifndef FORCE_EJECT |
196 | if (device->driver == NULL) { | |
197 | ret = -ENODEV; | |
198 | goto err; | |
199 | } | |
200 | #endif | |
201 | status = acpi_get_type(device->handle, &type); | |
4be44fcd | 202 | if (ACPI_FAILURE(status) || (!device->flags.ejectable)) { |
1da177e4 LT |
203 | ret = -ENODEV; |
204 | goto err; | |
205 | } | |
206 | ||
207 | islockable = device->flags.lockable; | |
208 | handle = device->handle; | |
209 | ||
eefa27a9 | 210 | result = acpi_bus_trim(device, 1); |
1da177e4 LT |
211 | |
212 | if (!result) | |
213 | result = acpi_eject_operation(handle, islockable); | |
214 | ||
215 | if (result) { | |
216 | ret = -EBUSY; | |
217 | } | |
4be44fcd | 218 | err: |
1da177e4 LT |
219 | return ret; |
220 | } | |
221 | ||
1da177e4 | 222 | /* -------------------------------------------------------------------------- |
9e89dde2 | 223 | ACPI Bus operations |
1da177e4 | 224 | -------------------------------------------------------------------------- */ |
1890a97a PM |
225 | static void acpi_device_release(struct device *dev) |
226 | { | |
227 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
228 | ||
229 | kfree(acpi_dev->pnp.cid_list); | |
230 | kfree(acpi_dev); | |
231 | } | |
232 | ||
5d9464a4 | 233 | static int acpi_device_suspend(struct device *dev, pm_message_t state) |
1da177e4 | 234 | { |
5d9464a4 PM |
235 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
236 | struct acpi_driver *acpi_drv = acpi_dev->driver; | |
9e89dde2 | 237 | |
5d9464a4 PM |
238 | if (acpi_drv && acpi_drv->ops.suspend) |
239 | return acpi_drv->ops.suspend(acpi_dev, state); | |
1da177e4 LT |
240 | return 0; |
241 | } | |
242 | ||
5d9464a4 | 243 | static int acpi_device_resume(struct device *dev) |
9e89dde2 | 244 | { |
5d9464a4 PM |
245 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
246 | struct acpi_driver *acpi_drv = acpi_dev->driver; | |
1da177e4 | 247 | |
5d9464a4 PM |
248 | if (acpi_drv && acpi_drv->ops.resume) |
249 | return acpi_drv->ops.resume(acpi_dev); | |
9e89dde2 ZR |
250 | return 0; |
251 | } | |
252 | ||
5d9464a4 | 253 | static int acpi_bus_match(struct device *dev, struct device_driver *drv) |
9e89dde2 | 254 | { |
5d9464a4 PM |
255 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
256 | struct acpi_driver *acpi_drv = to_acpi_driver(drv); | |
9e89dde2 | 257 | |
5d9464a4 PM |
258 | if (acpi_drv->ops.match) |
259 | return !acpi_drv->ops.match(acpi_dev, acpi_drv); | |
260 | return !acpi_match_ids(acpi_dev, acpi_drv->ids); | |
261 | } | |
262 | ||
263 | static int acpi_device_uevent(struct device *dev, char **envp, int num_envp, | |
264 | char *buffer, int buffer_size) | |
265 | { | |
266 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
267 | int i = 0, length = 0, ret = 0; | |
268 | ||
269 | if (acpi_dev->flags.hardware_id) | |
270 | ret = add_uevent_var(envp, num_envp, &i, | |
271 | buffer, buffer_size, &length, | |
272 | "HWID=%s", acpi_dev->pnp.hardware_id); | |
273 | if (ret) | |
274 | return -ENOMEM; | |
275 | if (acpi_dev->flags.compatible_ids) { | |
276 | int j; | |
277 | struct acpi_compatible_id_list *cid_list; | |
278 | ||
279 | cid_list = acpi_dev->pnp.cid_list; | |
280 | ||
281 | for (j = 0; j < cid_list->count; j++) { | |
282 | ret = add_uevent_var(envp, num_envp, &i, buffer, | |
283 | buffer_size, &length, "COMPTID=%s", | |
284 | cid_list->id[j].value); | |
285 | if (ret) | |
286 | return -ENOMEM; | |
9e89dde2 ZR |
287 | } |
288 | } | |
5d9464a4 PM |
289 | |
290 | envp[i] = NULL; | |
9e89dde2 ZR |
291 | return 0; |
292 | } | |
293 | ||
5d9464a4 PM |
294 | static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *); |
295 | static int acpi_start_single_object(struct acpi_device *); | |
296 | static int acpi_device_probe(struct device * dev) | |
9e89dde2 | 297 | { |
5d9464a4 PM |
298 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
299 | struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver); | |
300 | int ret; | |
301 | ||
302 | ret = acpi_bus_driver_init(acpi_dev, acpi_drv); | |
303 | if (!ret) { | |
304 | acpi_start_single_object(acpi_dev); | |
305 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, | |
306 | "Found driver [%s] for device [%s]\n", | |
307 | acpi_drv->name, acpi_dev->pnp.bus_id)); | |
308 | get_device(dev); | |
309 | } | |
310 | return ret; | |
311 | } | |
9e89dde2 | 312 | |
5d9464a4 PM |
313 | static int acpi_device_remove(struct device * dev) |
314 | { | |
315 | struct acpi_device *acpi_dev = to_acpi_device(dev); | |
316 | struct acpi_driver *acpi_drv = acpi_dev->driver; | |
317 | ||
318 | if (acpi_drv) { | |
319 | if (acpi_drv->ops.stop) | |
320 | acpi_drv->ops.stop(acpi_dev, ACPI_BUS_REMOVAL_NORMAL); | |
321 | if (acpi_drv->ops.remove) | |
322 | acpi_drv->ops.remove(acpi_dev, ACPI_BUS_REMOVAL_NORMAL); | |
323 | } | |
324 | acpi_dev->driver = NULL; | |
325 | acpi_driver_data(dev) = NULL; | |
326 | ||
327 | put_device(dev); | |
9e89dde2 ZR |
328 | return 0; |
329 | } | |
1da177e4 | 330 | |
5d9464a4 | 331 | static void acpi_device_shutdown(struct device *dev) |
1da177e4 | 332 | { |
5d9464a4 PM |
333 | struct acpi_device *acpi_dev = to_acpi_device(dev); |
334 | struct acpi_driver *acpi_drv = acpi_dev->driver; | |
335 | ||
336 | if (acpi_drv && acpi_drv->ops.shutdown) | |
337 | acpi_drv->ops.shutdown(acpi_dev); | |
338 | ||
339 | return ; | |
1da177e4 LT |
340 | } |
341 | ||
9e89dde2 ZR |
342 | static struct bus_type acpi_bus_type = { |
343 | .name = "acpi", | |
344 | .suspend = acpi_device_suspend, | |
345 | .resume = acpi_device_resume, | |
5d9464a4 PM |
346 | .shutdown = acpi_device_shutdown, |
347 | .match = acpi_bus_match, | |
348 | .probe = acpi_device_probe, | |
349 | .remove = acpi_device_remove, | |
350 | .uevent = acpi_device_uevent, | |
9e89dde2 ZR |
351 | }; |
352 | ||
353 | static void acpi_device_register(struct acpi_device *device, | |
354 | struct acpi_device *parent) | |
355 | { | |
356 | int err; | |
357 | ||
358 | /* | |
359 | * Linkage | |
360 | * ------- | |
361 | * Link this device to its parent and siblings. | |
362 | */ | |
363 | INIT_LIST_HEAD(&device->children); | |
364 | INIT_LIST_HEAD(&device->node); | |
365 | INIT_LIST_HEAD(&device->g_list); | |
366 | INIT_LIST_HEAD(&device->wakeup_list); | |
367 | ||
368 | spin_lock(&acpi_device_lock); | |
369 | if (device->parent) { | |
370 | list_add_tail(&device->node, &device->parent->children); | |
371 | list_add_tail(&device->g_list, &device->parent->g_list); | |
372 | } else | |
373 | list_add_tail(&device->g_list, &acpi_device_list); | |
374 | if (device->wakeup.flags.valid) | |
375 | list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list); | |
376 | spin_unlock(&acpi_device_lock); | |
377 | ||
378 | strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN); | |
379 | if (parent) | |
380 | device->kobj.parent = &parent->kobj; | |
381 | device->kobj.ktype = &ktype_acpi_ns; | |
382 | device->kobj.kset = &acpi_namespace_kset; | |
383 | err = kobject_register(&device->kobj); | |
384 | if (err < 0) | |
385 | printk(KERN_WARNING "%s: kobject_register error: %d\n", | |
386 | __FUNCTION__, err); | |
387 | create_sysfs_device_files(device); | |
1890a97a PM |
388 | |
389 | if (device->parent) | |
390 | device->dev.parent = &parent->dev; | |
391 | device->dev.bus = &acpi_bus_type; | |
392 | device_initialize(&device->dev); | |
393 | sprintf(device->dev.bus_id, "%s", device->pnp.bus_id); | |
394 | device->dev.release = &acpi_device_release; | |
395 | device_add(&device->dev); | |
9e89dde2 ZR |
396 | } |
397 | ||
398 | static 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); | |
408 | ||
409 | spin_unlock(&acpi_device_lock); | |
410 | ||
411 | acpi_detach_data(device->handle, acpi_bus_data_handler); | |
412 | remove_sysfs_device_files(device); | |
413 | kobject_unregister(&device->kobj); | |
1890a97a PM |
414 | |
415 | device_unregister(&device->dev); | |
9e89dde2 ZR |
416 | } |
417 | ||
418 | /* -------------------------------------------------------------------------- | |
419 | Driver Management | |
420 | -------------------------------------------------------------------------- */ | |
1da177e4 | 421 | /** |
d758a8fa RD |
422 | * acpi_bus_driver_init - add a device to a driver |
423 | * @device: the device to add and initialize | |
424 | * @driver: driver for the device | |
425 | * | |
1da177e4 | 426 | * Used to initialize a device via its device driver. Called whenever a |
1890a97a | 427 | * driver is bound to a device. Invokes the driver's add() ops. |
1da177e4 LT |
428 | */ |
429 | static int | |
4be44fcd | 430 | acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver) |
1da177e4 | 431 | { |
4be44fcd | 432 | int result = 0; |
1da177e4 | 433 | |
1da177e4 LT |
434 | |
435 | if (!device || !driver) | |
d550d98d | 436 | return -EINVAL; |
1da177e4 LT |
437 | |
438 | if (!driver->ops.add) | |
d550d98d | 439 | return -ENOSYS; |
1da177e4 LT |
440 | |
441 | result = driver->ops.add(device); | |
442 | if (result) { | |
443 | device->driver = NULL; | |
444 | acpi_driver_data(device) = NULL; | |
d550d98d | 445 | return result; |
1da177e4 LT |
446 | } |
447 | ||
448 | device->driver = driver; | |
449 | ||
450 | /* | |
451 | * TBD - Configuration Management: Assign resources to device based | |
452 | * upon possible configuration and currently allocated resources. | |
453 | */ | |
454 | ||
4be44fcd LB |
455 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
456 | "Driver successfully bound to device\n")); | |
d550d98d | 457 | return 0; |
3fb02738 RS |
458 | } |
459 | ||
8713cbef | 460 | static int acpi_start_single_object(struct acpi_device *device) |
3fb02738 RS |
461 | { |
462 | int result = 0; | |
463 | struct acpi_driver *driver; | |
464 | ||
3fb02738 RS |
465 | |
466 | if (!(driver = device->driver)) | |
d550d98d | 467 | return 0; |
3fb02738 | 468 | |
1da177e4 LT |
469 | if (driver->ops.start) { |
470 | result = driver->ops.start(device); | |
471 | if (result && driver->ops.remove) | |
472 | driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL); | |
1da177e4 LT |
473 | } |
474 | ||
d550d98d | 475 | return result; |
1da177e4 LT |
476 | } |
477 | ||
9e89dde2 ZR |
478 | /** |
479 | * acpi_bus_register_driver - register a driver with the ACPI bus | |
480 | * @driver: driver being registered | |
481 | * | |
482 | * Registers a driver with the ACPI bus. Searches the namespace for all | |
483 | * devices that match the driver's criteria and binds. Returns zero for | |
484 | * success or a negative error status for failure. | |
485 | */ | |
486 | int acpi_bus_register_driver(struct acpi_driver *driver) | |
487 | { | |
1890a97a | 488 | int ret; |
9e89dde2 ZR |
489 | |
490 | if (acpi_disabled) | |
491 | return -ENODEV; | |
1890a97a PM |
492 | driver->drv.name = driver->name; |
493 | driver->drv.bus = &acpi_bus_type; | |
494 | driver->drv.owner = driver->owner; | |
9e89dde2 | 495 | |
1890a97a PM |
496 | ret = driver_register(&driver->drv); |
497 | return ret; | |
9e89dde2 ZR |
498 | } |
499 | ||
500 | EXPORT_SYMBOL(acpi_bus_register_driver); | |
501 | ||
502 | /** | |
503 | * acpi_bus_unregister_driver - unregisters a driver with the APIC bus | |
504 | * @driver: driver to unregister | |
505 | * | |
506 | * Unregisters a driver with the ACPI bus. Searches the namespace for all | |
507 | * devices that match the driver's criteria and unbinds. | |
508 | */ | |
509 | void acpi_bus_unregister_driver(struct acpi_driver *driver) | |
510 | { | |
1890a97a | 511 | driver_unregister(&driver->drv); |
9e89dde2 ZR |
512 | } |
513 | ||
514 | EXPORT_SYMBOL(acpi_bus_unregister_driver); | |
515 | ||
9e89dde2 ZR |
516 | /* -------------------------------------------------------------------------- |
517 | Device Enumeration | |
518 | -------------------------------------------------------------------------- */ | |
519 | acpi_status | |
520 | acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd) | |
521 | { | |
522 | acpi_status status; | |
523 | acpi_handle tmp; | |
524 | struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL}; | |
525 | union acpi_object *obj; | |
526 | ||
527 | status = acpi_get_handle(handle, "_EJD", &tmp); | |
528 | if (ACPI_FAILURE(status)) | |
529 | return status; | |
530 | ||
531 | status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer); | |
532 | if (ACPI_SUCCESS(status)) { | |
533 | obj = buffer.pointer; | |
534 | status = acpi_get_handle(NULL, obj->string.pointer, ejd); | |
535 | kfree(buffer.pointer); | |
536 | } | |
537 | return status; | |
538 | } | |
539 | EXPORT_SYMBOL_GPL(acpi_bus_get_ejd); | |
540 | ||
541 | void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context) | |
542 | { | |
543 | ||
544 | /* TBD */ | |
545 | ||
546 | return; | |
547 | } | |
548 | ||
549 | int acpi_match_ids(struct acpi_device *device, char *ids) | |
550 | { | |
551 | if (device->flags.hardware_id) | |
552 | if (strstr(ids, device->pnp.hardware_id)) | |
553 | return 0; | |
554 | ||
555 | if (device->flags.compatible_ids) { | |
556 | struct acpi_compatible_id_list *cid_list = device->pnp.cid_list; | |
557 | int i; | |
558 | ||
559 | /* compare multiple _CID entries against driver ids */ | |
560 | for (i = 0; i < cid_list->count; i++) { | |
561 | if (strstr(ids, cid_list->id[i].value)) | |
562 | return 0; | |
563 | } | |
564 | } | |
565 | return -ENOENT; | |
566 | } | |
567 | ||
568 | static int acpi_bus_get_perf_flags(struct acpi_device *device) | |
569 | { | |
570 | device->performance.state = ACPI_STATE_UNKNOWN; | |
571 | return 0; | |
572 | } | |
573 | ||
574 | static acpi_status | |
575 | acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device, | |
576 | union acpi_object *package) | |
577 | { | |
578 | int i = 0; | |
579 | union acpi_object *element = NULL; | |
580 | ||
581 | if (!device || !package || (package->package.count < 2)) | |
582 | return AE_BAD_PARAMETER; | |
583 | ||
584 | element = &(package->package.elements[0]); | |
585 | if (!element) | |
586 | return AE_BAD_PARAMETER; | |
587 | if (element->type == ACPI_TYPE_PACKAGE) { | |
588 | if ((element->package.count < 2) || | |
589 | (element->package.elements[0].type != | |
590 | ACPI_TYPE_LOCAL_REFERENCE) | |
591 | || (element->package.elements[1].type != ACPI_TYPE_INTEGER)) | |
592 | return AE_BAD_DATA; | |
593 | device->wakeup.gpe_device = | |
594 | element->package.elements[0].reference.handle; | |
595 | device->wakeup.gpe_number = | |
596 | (u32) element->package.elements[1].integer.value; | |
597 | } else if (element->type == ACPI_TYPE_INTEGER) { | |
598 | device->wakeup.gpe_number = element->integer.value; | |
599 | } else | |
600 | return AE_BAD_DATA; | |
601 | ||
602 | element = &(package->package.elements[1]); | |
603 | if (element->type != ACPI_TYPE_INTEGER) { | |
604 | return AE_BAD_DATA; | |
605 | } | |
606 | device->wakeup.sleep_state = element->integer.value; | |
607 | ||
608 | if ((package->package.count - 2) > ACPI_MAX_HANDLES) { | |
609 | return AE_NO_MEMORY; | |
610 | } | |
611 | device->wakeup.resources.count = package->package.count - 2; | |
612 | for (i = 0; i < device->wakeup.resources.count; i++) { | |
613 | element = &(package->package.elements[i + 2]); | |
614 | if (element->type != ACPI_TYPE_ANY) { | |
615 | return AE_BAD_DATA; | |
616 | } | |
617 | ||
618 | device->wakeup.resources.handles[i] = element->reference.handle; | |
619 | } | |
620 | ||
621 | return AE_OK; | |
1da177e4 LT |
622 | } |
623 | ||
9e89dde2 | 624 | static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device) |
1da177e4 | 625 | { |
9e89dde2 ZR |
626 | acpi_status status = 0; |
627 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
628 | union acpi_object *package = NULL; | |
1da177e4 | 629 | |
1da177e4 | 630 | |
9e89dde2 ZR |
631 | /* _PRW */ |
632 | status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer); | |
633 | if (ACPI_FAILURE(status)) { | |
634 | ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW")); | |
635 | goto end; | |
636 | } | |
1da177e4 | 637 | |
9e89dde2 ZR |
638 | package = (union acpi_object *)buffer.pointer; |
639 | status = acpi_bus_extract_wakeup_device_power_package(device, package); | |
640 | if (ACPI_FAILURE(status)) { | |
641 | ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package")); | |
642 | goto end; | |
643 | } | |
1da177e4 | 644 | |
9e89dde2 | 645 | kfree(buffer.pointer); |
1da177e4 | 646 | |
9e89dde2 ZR |
647 | device->wakeup.flags.valid = 1; |
648 | /* Power button, Lid switch always enable wakeup */ | |
649 | if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E")) | |
650 | device->wakeup.flags.run_wake = 1; | |
1a365616 | 651 | |
9e89dde2 ZR |
652 | end: |
653 | if (ACPI_FAILURE(status)) | |
654 | device->flags.wake_capable = 0; | |
655 | return 0; | |
1da177e4 | 656 | } |
4be44fcd | 657 | |
9e89dde2 | 658 | static int acpi_bus_get_power_flags(struct acpi_device *device) |
1da177e4 | 659 | { |
9e89dde2 ZR |
660 | acpi_status status = 0; |
661 | acpi_handle handle = NULL; | |
662 | u32 i = 0; | |
1da177e4 | 663 | |
1da177e4 | 664 | |
9e89dde2 ZR |
665 | /* |
666 | * Power Management Flags | |
667 | */ | |
668 | status = acpi_get_handle(device->handle, "_PSC", &handle); | |
669 | if (ACPI_SUCCESS(status)) | |
670 | device->power.flags.explicit_get = 1; | |
671 | status = acpi_get_handle(device->handle, "_IRC", &handle); | |
672 | if (ACPI_SUCCESS(status)) | |
673 | device->power.flags.inrush_current = 1; | |
1da177e4 | 674 | |
9e89dde2 ZR |
675 | /* |
676 | * Enumerate supported power management states | |
677 | */ | |
678 | for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) { | |
679 | struct acpi_device_power_state *ps = &device->power.states[i]; | |
680 | char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' }; | |
681 | ||
682 | /* Evaluate "_PRx" to se if power resources are referenced */ | |
683 | acpi_evaluate_reference(device->handle, object_name, NULL, | |
684 | &ps->resources); | |
685 | if (ps->resources.count) { | |
686 | device->power.flags.power_resources = 1; | |
687 | ps->flags.valid = 1; | |
1da177e4 | 688 | } |
1da177e4 | 689 | |
9e89dde2 ZR |
690 | /* Evaluate "_PSx" to see if we can do explicit sets */ |
691 | object_name[2] = 'S'; | |
692 | status = acpi_get_handle(device->handle, object_name, &handle); | |
693 | if (ACPI_SUCCESS(status)) { | |
694 | ps->flags.explicit_set = 1; | |
695 | ps->flags.valid = 1; | |
696 | } | |
1da177e4 | 697 | |
9e89dde2 ZR |
698 | /* State is valid if we have some power control */ |
699 | if (ps->resources.count || ps->flags.explicit_set) | |
700 | ps->flags.valid = 1; | |
1da177e4 | 701 | |
9e89dde2 ZR |
702 | ps->power = -1; /* Unknown - driver assigned */ |
703 | ps->latency = -1; /* Unknown - driver assigned */ | |
704 | } | |
c8f7a62c | 705 | |
9e89dde2 ZR |
706 | /* Set defaults for D0 and D3 states (always valid) */ |
707 | device->power.states[ACPI_STATE_D0].flags.valid = 1; | |
708 | device->power.states[ACPI_STATE_D0].power = 100; | |
709 | device->power.states[ACPI_STATE_D3].flags.valid = 1; | |
710 | device->power.states[ACPI_STATE_D3].power = 0; | |
c8f7a62c | 711 | |
9e89dde2 ZR |
712 | /* TBD: System wake support and resource requirements. */ |
713 | ||
714 | device->power.state = ACPI_STATE_UNKNOWN; | |
c8f7a62c | 715 | |
9e89dde2 ZR |
716 | return 0; |
717 | } | |
c8f7a62c | 718 | |
4be44fcd | 719 | static int acpi_bus_get_flags(struct acpi_device *device) |
1da177e4 | 720 | { |
4be44fcd LB |
721 | acpi_status status = AE_OK; |
722 | acpi_handle temp = NULL; | |
1da177e4 | 723 | |
1da177e4 LT |
724 | |
725 | /* Presence of _STA indicates 'dynamic_status' */ | |
726 | status = acpi_get_handle(device->handle, "_STA", &temp); | |
727 | if (ACPI_SUCCESS(status)) | |
728 | device->flags.dynamic_status = 1; | |
729 | ||
730 | /* Presence of _CID indicates 'compatible_ids' */ | |
731 | status = acpi_get_handle(device->handle, "_CID", &temp); | |
732 | if (ACPI_SUCCESS(status)) | |
733 | device->flags.compatible_ids = 1; | |
734 | ||
735 | /* Presence of _RMV indicates 'removable' */ | |
736 | status = acpi_get_handle(device->handle, "_RMV", &temp); | |
737 | if (ACPI_SUCCESS(status)) | |
738 | device->flags.removable = 1; | |
739 | ||
740 | /* Presence of _EJD|_EJ0 indicates 'ejectable' */ | |
741 | status = acpi_get_handle(device->handle, "_EJD", &temp); | |
742 | if (ACPI_SUCCESS(status)) | |
743 | device->flags.ejectable = 1; | |
744 | else { | |
745 | status = acpi_get_handle(device->handle, "_EJ0", &temp); | |
746 | if (ACPI_SUCCESS(status)) | |
747 | device->flags.ejectable = 1; | |
748 | } | |
749 | ||
750 | /* Presence of _LCK indicates 'lockable' */ | |
751 | status = acpi_get_handle(device->handle, "_LCK", &temp); | |
752 | if (ACPI_SUCCESS(status)) | |
753 | device->flags.lockable = 1; | |
754 | ||
755 | /* Presence of _PS0|_PR0 indicates 'power manageable' */ | |
756 | status = acpi_get_handle(device->handle, "_PS0", &temp); | |
757 | if (ACPI_FAILURE(status)) | |
758 | status = acpi_get_handle(device->handle, "_PR0", &temp); | |
759 | if (ACPI_SUCCESS(status)) | |
760 | device->flags.power_manageable = 1; | |
761 | ||
762 | /* Presence of _PRW indicates wake capable */ | |
763 | status = acpi_get_handle(device->handle, "_PRW", &temp); | |
764 | if (ACPI_SUCCESS(status)) | |
765 | device->flags.wake_capable = 1; | |
766 | ||
767 | /* TBD: Peformance management */ | |
768 | ||
d550d98d | 769 | return 0; |
1da177e4 LT |
770 | } |
771 | ||
4be44fcd LB |
772 | static void acpi_device_get_busid(struct acpi_device *device, |
773 | acpi_handle handle, int type) | |
1da177e4 | 774 | { |
4be44fcd LB |
775 | char bus_id[5] = { '?', 0 }; |
776 | struct acpi_buffer buffer = { sizeof(bus_id), bus_id }; | |
777 | int i = 0; | |
1da177e4 LT |
778 | |
779 | /* | |
780 | * Bus ID | |
781 | * ------ | |
782 | * The device's Bus ID is simply the object name. | |
783 | * TBD: Shouldn't this value be unique (within the ACPI namespace)? | |
784 | */ | |
785 | switch (type) { | |
786 | case ACPI_BUS_TYPE_SYSTEM: | |
787 | strcpy(device->pnp.bus_id, "ACPI"); | |
788 | break; | |
789 | case ACPI_BUS_TYPE_POWER_BUTTON: | |
790 | strcpy(device->pnp.bus_id, "PWRF"); | |
791 | break; | |
792 | case ACPI_BUS_TYPE_SLEEP_BUTTON: | |
793 | strcpy(device->pnp.bus_id, "SLPF"); | |
794 | break; | |
795 | default: | |
796 | acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer); | |
797 | /* Clean up trailing underscores (if any) */ | |
798 | for (i = 3; i > 1; i--) { | |
799 | if (bus_id[i] == '_') | |
800 | bus_id[i] = '\0'; | |
801 | else | |
802 | break; | |
803 | } | |
804 | strcpy(device->pnp.bus_id, bus_id); | |
805 | break; | |
806 | } | |
807 | } | |
808 | ||
4be44fcd LB |
809 | static void acpi_device_set_id(struct acpi_device *device, |
810 | struct acpi_device *parent, acpi_handle handle, | |
811 | int type) | |
1da177e4 | 812 | { |
4be44fcd LB |
813 | struct acpi_device_info *info; |
814 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; | |
815 | char *hid = NULL; | |
816 | char *uid = NULL; | |
1da177e4 | 817 | struct acpi_compatible_id_list *cid_list = NULL; |
4be44fcd | 818 | acpi_status status; |
1da177e4 LT |
819 | |
820 | switch (type) { | |
821 | case ACPI_BUS_TYPE_DEVICE: | |
822 | status = acpi_get_object_info(handle, &buffer); | |
823 | if (ACPI_FAILURE(status)) { | |
4be44fcd | 824 | printk("%s: Error reading device info\n", __FUNCTION__); |
1da177e4 LT |
825 | return; |
826 | } | |
827 | ||
828 | info = buffer.pointer; | |
829 | if (info->valid & ACPI_VALID_HID) | |
830 | hid = info->hardware_id.value; | |
831 | if (info->valid & ACPI_VALID_UID) | |
832 | uid = info->unique_id.value; | |
833 | if (info->valid & ACPI_VALID_CID) | |
834 | cid_list = &info->compatibility_id; | |
835 | if (info->valid & ACPI_VALID_ADR) { | |
836 | device->pnp.bus_address = info->address; | |
837 | device->flags.bus_address = 1; | |
838 | } | |
839 | break; | |
840 | case ACPI_BUS_TYPE_POWER: | |
841 | hid = ACPI_POWER_HID; | |
842 | break; | |
843 | case ACPI_BUS_TYPE_PROCESSOR: | |
844 | hid = ACPI_PROCESSOR_HID; | |
845 | break; | |
846 | case ACPI_BUS_TYPE_SYSTEM: | |
847 | hid = ACPI_SYSTEM_HID; | |
848 | break; | |
849 | case ACPI_BUS_TYPE_THERMAL: | |
850 | hid = ACPI_THERMAL_HID; | |
851 | break; | |
852 | case ACPI_BUS_TYPE_POWER_BUTTON: | |
853 | hid = ACPI_BUTTON_HID_POWERF; | |
854 | break; | |
855 | case ACPI_BUS_TYPE_SLEEP_BUTTON: | |
856 | hid = ACPI_BUTTON_HID_SLEEPF; | |
857 | break; | |
858 | } | |
859 | ||
860 | /* | |
861 | * \_SB | |
862 | * ---- | |
863 | * Fix for the system root bus device -- the only root-level device. | |
864 | */ | |
c51a4de8 | 865 | if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) { |
1da177e4 LT |
866 | hid = ACPI_BUS_HID; |
867 | strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME); | |
868 | strcpy(device->pnp.device_class, ACPI_BUS_CLASS); | |
869 | } | |
870 | ||
871 | if (hid) { | |
872 | strcpy(device->pnp.hardware_id, hid); | |
873 | device->flags.hardware_id = 1; | |
874 | } | |
875 | if (uid) { | |
876 | strcpy(device->pnp.unique_id, uid); | |
877 | device->flags.unique_id = 1; | |
878 | } | |
879 | if (cid_list) { | |
880 | device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL); | |
881 | if (device->pnp.cid_list) | |
882 | memcpy(device->pnp.cid_list, cid_list, cid_list->size); | |
883 | else | |
884 | printk(KERN_ERR "Memory allocation error\n"); | |
885 | } | |
886 | ||
02438d87 | 887 | kfree(buffer.pointer); |
1da177e4 LT |
888 | } |
889 | ||
4be44fcd | 890 | static int acpi_device_set_context(struct acpi_device *device, int type) |
1da177e4 LT |
891 | { |
892 | acpi_status status = AE_OK; | |
893 | int result = 0; | |
894 | /* | |
895 | * Context | |
896 | * ------- | |
897 | * Attach this 'struct acpi_device' to the ACPI object. This makes | |
898 | * resolutions from handle->device very efficient. Note that we need | |
899 | * to be careful with fixed-feature devices as they all attach to the | |
900 | * root object. | |
901 | */ | |
4be44fcd | 902 | if (type != ACPI_BUS_TYPE_POWER_BUTTON && |
1da177e4 LT |
903 | type != ACPI_BUS_TYPE_SLEEP_BUTTON) { |
904 | status = acpi_attach_data(device->handle, | |
4be44fcd | 905 | acpi_bus_data_handler, device); |
1da177e4 LT |
906 | |
907 | if (ACPI_FAILURE(status)) { | |
908 | printk("Error attaching device data\n"); | |
909 | result = -ENODEV; | |
910 | } | |
911 | } | |
912 | return result; | |
913 | } | |
914 | ||
4be44fcd LB |
915 | static void acpi_device_get_debug_info(struct acpi_device *device, |
916 | acpi_handle handle, int type) | |
1da177e4 LT |
917 | { |
918 | #ifdef CONFIG_ACPI_DEBUG_OUTPUT | |
4be44fcd LB |
919 | char *type_string = NULL; |
920 | char name[80] = { '?', '\0' }; | |
921 | struct acpi_buffer buffer = { sizeof(name), name }; | |
1da177e4 LT |
922 | |
923 | switch (type) { | |
924 | case ACPI_BUS_TYPE_DEVICE: | |
925 | type_string = "Device"; | |
926 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | |
927 | break; | |
928 | case ACPI_BUS_TYPE_POWER: | |
929 | type_string = "Power Resource"; | |
930 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | |
931 | break; | |
932 | case ACPI_BUS_TYPE_PROCESSOR: | |
933 | type_string = "Processor"; | |
934 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | |
935 | break; | |
936 | case ACPI_BUS_TYPE_SYSTEM: | |
937 | type_string = "System"; | |
938 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | |
939 | break; | |
940 | case ACPI_BUS_TYPE_THERMAL: | |
941 | type_string = "Thermal Zone"; | |
942 | acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer); | |
943 | break; | |
944 | case ACPI_BUS_TYPE_POWER_BUTTON: | |
945 | type_string = "Power Button"; | |
946 | sprintf(name, "PWRB"); | |
947 | break; | |
948 | case ACPI_BUS_TYPE_SLEEP_BUTTON: | |
949 | type_string = "Sleep Button"; | |
950 | sprintf(name, "SLPB"); | |
951 | break; | |
952 | } | |
953 | ||
954 | printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle); | |
4be44fcd | 955 | #endif /*CONFIG_ACPI_DEBUG_OUTPUT */ |
1da177e4 LT |
956 | } |
957 | ||
4be44fcd | 958 | static int acpi_bus_remove(struct acpi_device *dev, int rmdevice) |
1da177e4 | 959 | { |
1da177e4 | 960 | if (!dev) |
d550d98d | 961 | return -EINVAL; |
1da177e4 | 962 | |
1890a97a | 963 | device_release_driver(&dev->dev); |
1da177e4 LT |
964 | |
965 | if (!rmdevice) | |
d550d98d | 966 | return 0; |
1da177e4 LT |
967 | |
968 | if (dev->flags.bus_address) { | |
969 | if ((dev->parent) && (dev->parent->ops.unbind)) | |
970 | dev->parent->ops.unbind(dev); | |
971 | } | |
4be44fcd | 972 | |
1da177e4 LT |
973 | acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT); |
974 | ||
d550d98d | 975 | return 0; |
1da177e4 LT |
976 | } |
977 | ||
3fb02738 | 978 | static int |
4be44fcd LB |
979 | acpi_add_single_object(struct acpi_device **child, |
980 | struct acpi_device *parent, acpi_handle handle, int type) | |
1da177e4 | 981 | { |
4be44fcd LB |
982 | int result = 0; |
983 | struct acpi_device *device = NULL; | |
1da177e4 | 984 | |
1da177e4 LT |
985 | |
986 | if (!child) | |
d550d98d | 987 | return -EINVAL; |
1da177e4 LT |
988 | |
989 | device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL); | |
990 | if (!device) { | |
6468463a | 991 | printk(KERN_ERR PREFIX "Memory allocation error\n"); |
d550d98d | 992 | return -ENOMEM; |
1da177e4 LT |
993 | } |
994 | memset(device, 0, sizeof(struct acpi_device)); | |
995 | ||
996 | device->handle = handle; | |
997 | device->parent = parent; | |
998 | ||
4be44fcd | 999 | acpi_device_get_busid(device, handle, type); |
1da177e4 LT |
1000 | |
1001 | /* | |
1002 | * Flags | |
1003 | * ----- | |
1004 | * Get prior to calling acpi_bus_get_status() so we know whether | |
1005 | * or not _STA is present. Note that we only look for object | |
1006 | * handles -- cannot evaluate objects until we know the device is | |
1007 | * present and properly initialized. | |
1008 | */ | |
1009 | result = acpi_bus_get_flags(device); | |
1010 | if (result) | |
1011 | goto end; | |
1012 | ||
1013 | /* | |
1014 | * Status | |
1015 | * ------ | |
6940faba KT |
1016 | * See if the device is present. We always assume that non-Device |
1017 | * and non-Processor objects (e.g. thermal zones, power resources, | |
1018 | * etc.) are present, functioning, etc. (at least when parent object | |
1019 | * is present). Note that _STA has a different meaning for some | |
1020 | * objects (e.g. power resources) so we need to be careful how we use | |
1021 | * it. | |
1da177e4 LT |
1022 | */ |
1023 | switch (type) { | |
6940faba | 1024 | case ACPI_BUS_TYPE_PROCESSOR: |
1da177e4 LT |
1025 | case ACPI_BUS_TYPE_DEVICE: |
1026 | result = acpi_bus_get_status(device); | |
1027 | if (ACPI_FAILURE(result) || !device->status.present) { | |
1028 | result = -ENOENT; | |
1029 | goto end; | |
1030 | } | |
1031 | break; | |
1032 | default: | |
1033 | STRUCT_TO_INT(device->status) = 0x0F; | |
1034 | break; | |
1035 | } | |
1036 | ||
1037 | /* | |
1038 | * Initialize Device | |
1039 | * ----------------- | |
1040 | * TBD: Synch with Core's enumeration/initialization process. | |
1041 | */ | |
1042 | ||
1043 | /* | |
1044 | * Hardware ID, Unique ID, & Bus Address | |
1045 | * ------------------------------------- | |
1046 | */ | |
4be44fcd | 1047 | acpi_device_set_id(device, parent, handle, type); |
1da177e4 LT |
1048 | |
1049 | /* | |
1050 | * Power Management | |
1051 | * ---------------- | |
1052 | */ | |
1053 | if (device->flags.power_manageable) { | |
1054 | result = acpi_bus_get_power_flags(device); | |
1055 | if (result) | |
1056 | goto end; | |
1057 | } | |
1058 | ||
4be44fcd | 1059 | /* |
1da177e4 LT |
1060 | * Wakeup device management |
1061 | *----------------------- | |
1062 | */ | |
1063 | if (device->flags.wake_capable) { | |
1064 | result = acpi_bus_get_wakeup_device_flags(device); | |
1065 | if (result) | |
1066 | goto end; | |
1067 | } | |
1068 | ||
1069 | /* | |
1070 | * Performance Management | |
1071 | * ---------------------- | |
1072 | */ | |
1073 | if (device->flags.performance_manageable) { | |
1074 | result = acpi_bus_get_perf_flags(device); | |
1075 | if (result) | |
1076 | goto end; | |
1077 | } | |
1078 | ||
4be44fcd | 1079 | if ((result = acpi_device_set_context(device, type))) |
1da177e4 LT |
1080 | goto end; |
1081 | ||
4be44fcd | 1082 | acpi_device_get_debug_info(device, handle, type); |
1da177e4 | 1083 | |
4be44fcd | 1084 | acpi_device_register(device, parent); |
1da177e4 LT |
1085 | |
1086 | /* | |
1087 | * Bind _ADR-Based Devices | |
1088 | * ----------------------- | |
1089 | * If there's a a bus address (_ADR) then we utilize the parent's | |
1090 | * 'bind' function (if exists) to bind the ACPI- and natively- | |
1091 | * enumerated device representations. | |
1092 | */ | |
1093 | if (device->flags.bus_address) { | |
1094 | if (device->parent && device->parent->ops.bind) | |
1095 | device->parent->ops.bind(device); | |
1096 | } | |
1097 | ||
4be44fcd | 1098 | end: |
1da177e4 LT |
1099 | if (!result) |
1100 | *child = device; | |
1101 | else { | |
6044ec88 | 1102 | kfree(device->pnp.cid_list); |
1da177e4 LT |
1103 | kfree(device); |
1104 | } | |
1105 | ||
d550d98d | 1106 | return result; |
1da177e4 | 1107 | } |
1da177e4 | 1108 | |
4be44fcd | 1109 | static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops) |
1da177e4 | 1110 | { |
4be44fcd LB |
1111 | acpi_status status = AE_OK; |
1112 | struct acpi_device *parent = NULL; | |
1113 | struct acpi_device *child = NULL; | |
1114 | acpi_handle phandle = NULL; | |
1115 | acpi_handle chandle = NULL; | |
1116 | acpi_object_type type = 0; | |
1117 | u32 level = 1; | |
1da177e4 | 1118 | |
1da177e4 LT |
1119 | |
1120 | if (!start) | |
d550d98d | 1121 | return -EINVAL; |
1da177e4 LT |
1122 | |
1123 | parent = start; | |
1124 | phandle = start->handle; | |
4be44fcd | 1125 | |
1da177e4 LT |
1126 | /* |
1127 | * Parse through the ACPI namespace, identify all 'devices', and | |
1128 | * create a new 'struct acpi_device' for each. | |
1129 | */ | |
1130 | while ((level > 0) && parent) { | |
1131 | ||
1132 | status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, | |
4be44fcd | 1133 | chandle, &chandle); |
1da177e4 LT |
1134 | |
1135 | /* | |
1136 | * If this scope is exhausted then move our way back up. | |
1137 | */ | |
1138 | if (ACPI_FAILURE(status)) { | |
1139 | level--; | |
1140 | chandle = phandle; | |
1141 | acpi_get_parent(phandle, &phandle); | |
1142 | if (parent->parent) | |
1143 | parent = parent->parent; | |
1144 | continue; | |
1145 | } | |
1146 | ||
1147 | status = acpi_get_type(chandle, &type); | |
1148 | if (ACPI_FAILURE(status)) | |
1149 | continue; | |
1150 | ||
1151 | /* | |
1152 | * If this is a scope object then parse it (depth-first). | |
1153 | */ | |
1154 | if (type == ACPI_TYPE_LOCAL_SCOPE) { | |
1155 | level++; | |
1156 | phandle = chandle; | |
1157 | chandle = NULL; | |
1158 | continue; | |
1159 | } | |
1160 | ||
1161 | /* | |
1162 | * We're only interested in objects that we consider 'devices'. | |
1163 | */ | |
1164 | switch (type) { | |
1165 | case ACPI_TYPE_DEVICE: | |
1166 | type = ACPI_BUS_TYPE_DEVICE; | |
1167 | break; | |
1168 | case ACPI_TYPE_PROCESSOR: | |
1169 | type = ACPI_BUS_TYPE_PROCESSOR; | |
1170 | break; | |
1171 | case ACPI_TYPE_THERMAL: | |
1172 | type = ACPI_BUS_TYPE_THERMAL; | |
1173 | break; | |
1174 | case ACPI_TYPE_POWER: | |
1175 | type = ACPI_BUS_TYPE_POWER; | |
1176 | break; | |
1177 | default: | |
1178 | continue; | |
1179 | } | |
1180 | ||
3fb02738 RS |
1181 | if (ops->acpi_op_add) |
1182 | status = acpi_add_single_object(&child, parent, | |
4be44fcd LB |
1183 | chandle, type); |
1184 | else | |
3fb02738 RS |
1185 | status = acpi_bus_get_device(chandle, &child); |
1186 | ||
4be44fcd LB |
1187 | if (ACPI_FAILURE(status)) |
1188 | continue; | |
3fb02738 RS |
1189 | |
1190 | if (ops->acpi_op_start) { | |
1191 | status = acpi_start_single_object(child); | |
1192 | if (ACPI_FAILURE(status)) | |
1193 | continue; | |
1194 | } | |
1da177e4 LT |
1195 | |
1196 | /* | |
1197 | * If the device is present, enabled, and functioning then | |
1198 | * parse its scope (depth-first). Note that we need to | |
1199 | * represent absent devices to facilitate PnP notifications | |
1200 | * -- but only the subtree head (not all of its children, | |
1201 | * which will be enumerated when the parent is inserted). | |
1202 | * | |
1203 | * TBD: Need notifications and other detection mechanisms | |
4be44fcd | 1204 | * in place before we can fully implement this. |
1da177e4 LT |
1205 | */ |
1206 | if (child->status.present) { | |
1207 | status = acpi_get_next_object(ACPI_TYPE_ANY, chandle, | |
1208 | NULL, NULL); | |
1209 | if (ACPI_SUCCESS(status)) { | |
1210 | level++; | |
1211 | phandle = chandle; | |
1212 | chandle = NULL; | |
1213 | parent = child; | |
1214 | } | |
1215 | } | |
1216 | } | |
1217 | ||
d550d98d | 1218 | return 0; |
1da177e4 | 1219 | } |
1da177e4 | 1220 | |
3fb02738 | 1221 | int |
4be44fcd LB |
1222 | acpi_bus_add(struct acpi_device **child, |
1223 | struct acpi_device *parent, acpi_handle handle, int type) | |
3fb02738 RS |
1224 | { |
1225 | int result; | |
1226 | struct acpi_bus_ops ops; | |
1227 | ||
3fb02738 RS |
1228 | |
1229 | result = acpi_add_single_object(child, parent, handle, type); | |
1230 | if (!result) { | |
1231 | memset(&ops, 0, sizeof(ops)); | |
1232 | ops.acpi_op_add = 1; | |
1233 | result = acpi_bus_scan(*child, &ops); | |
1234 | } | |
d550d98d | 1235 | return result; |
3fb02738 | 1236 | } |
4be44fcd | 1237 | |
3fb02738 RS |
1238 | EXPORT_SYMBOL(acpi_bus_add); |
1239 | ||
4be44fcd | 1240 | int acpi_bus_start(struct acpi_device *device) |
3fb02738 RS |
1241 | { |
1242 | int result; | |
1243 | struct acpi_bus_ops ops; | |
1244 | ||
3fb02738 RS |
1245 | |
1246 | if (!device) | |
d550d98d | 1247 | return -EINVAL; |
3fb02738 RS |
1248 | |
1249 | result = acpi_start_single_object(device); | |
1250 | if (!result) { | |
1251 | memset(&ops, 0, sizeof(ops)); | |
1252 | ops.acpi_op_start = 1; | |
1253 | result = acpi_bus_scan(device, &ops); | |
1254 | } | |
d550d98d | 1255 | return result; |
3fb02738 | 1256 | } |
4be44fcd | 1257 | |
3fb02738 | 1258 | EXPORT_SYMBOL(acpi_bus_start); |
1da177e4 | 1259 | |
ceaba663 | 1260 | int acpi_bus_trim(struct acpi_device *start, int rmdevice) |
1da177e4 | 1261 | { |
4be44fcd LB |
1262 | acpi_status status; |
1263 | struct acpi_device *parent, *child; | |
1264 | acpi_handle phandle, chandle; | |
1265 | acpi_object_type type; | |
1266 | u32 level = 1; | |
1267 | int err = 0; | |
1268 | ||
1269 | parent = start; | |
1da177e4 LT |
1270 | phandle = start->handle; |
1271 | child = chandle = NULL; | |
1272 | ||
1273 | while ((level > 0) && parent && (!err)) { | |
1274 | status = acpi_get_next_object(ACPI_TYPE_ANY, phandle, | |
4be44fcd | 1275 | chandle, &chandle); |
1da177e4 LT |
1276 | |
1277 | /* | |
1278 | * If this scope is exhausted then move our way back up. | |
1279 | */ | |
1280 | if (ACPI_FAILURE(status)) { | |
1281 | level--; | |
1282 | chandle = phandle; | |
1283 | acpi_get_parent(phandle, &phandle); | |
1284 | child = parent; | |
1285 | parent = parent->parent; | |
1286 | ||
1287 | if (level == 0) | |
1288 | err = acpi_bus_remove(child, rmdevice); | |
1289 | else | |
1290 | err = acpi_bus_remove(child, 1); | |
1291 | ||
1292 | continue; | |
1293 | } | |
1294 | ||
1295 | status = acpi_get_type(chandle, &type); | |
1296 | if (ACPI_FAILURE(status)) { | |
1297 | continue; | |
1298 | } | |
1299 | /* | |
1300 | * If there is a device corresponding to chandle then | |
1301 | * parse it (depth-first). | |
1302 | */ | |
1303 | if (acpi_bus_get_device(chandle, &child) == 0) { | |
1304 | level++; | |
1305 | phandle = chandle; | |
1306 | chandle = NULL; | |
1307 | parent = child; | |
1308 | } | |
1309 | continue; | |
1310 | } | |
1311 | return err; | |
1312 | } | |
ceaba663 KA |
1313 | EXPORT_SYMBOL_GPL(acpi_bus_trim); |
1314 | ||
1da177e4 | 1315 | |
4be44fcd | 1316 | static int acpi_bus_scan_fixed(struct acpi_device *root) |
1da177e4 | 1317 | { |
4be44fcd LB |
1318 | int result = 0; |
1319 | struct acpi_device *device = NULL; | |
1da177e4 | 1320 | |
1da177e4 LT |
1321 | |
1322 | if (!root) | |
d550d98d | 1323 | return -ENODEV; |
1da177e4 LT |
1324 | |
1325 | /* | |
1326 | * Enumerate all fixed-feature devices. | |
1327 | */ | |
3fb02738 RS |
1328 | if (acpi_fadt.pwr_button == 0) { |
1329 | result = acpi_add_single_object(&device, acpi_root, | |
4be44fcd LB |
1330 | NULL, |
1331 | ACPI_BUS_TYPE_POWER_BUTTON); | |
3fb02738 RS |
1332 | if (!result) |
1333 | result = acpi_start_single_object(device); | |
1334 | } | |
1da177e4 | 1335 | |
3fb02738 RS |
1336 | if (acpi_fadt.sleep_button == 0) { |
1337 | result = acpi_add_single_object(&device, acpi_root, | |
4be44fcd LB |
1338 | NULL, |
1339 | ACPI_BUS_TYPE_SLEEP_BUTTON); | |
3fb02738 RS |
1340 | if (!result) |
1341 | result = acpi_start_single_object(device); | |
1342 | } | |
1da177e4 | 1343 | |
d550d98d | 1344 | return result; |
1da177e4 LT |
1345 | } |
1346 | ||
1da177e4 LT |
1347 | static int __init acpi_scan_init(void) |
1348 | { | |
1349 | int result; | |
3fb02738 | 1350 | struct acpi_bus_ops ops; |
1da177e4 | 1351 | |
1da177e4 LT |
1352 | |
1353 | if (acpi_disabled) | |
d550d98d | 1354 | return 0; |
1da177e4 | 1355 | |
9b6d97b6 RD |
1356 | result = kset_register(&acpi_namespace_kset); |
1357 | if (result < 0) | |
1358 | printk(KERN_ERR PREFIX "kset_register error: %d\n", result); | |
1da177e4 | 1359 | |
5b327265 PM |
1360 | result = bus_register(&acpi_bus_type); |
1361 | if (result) { | |
1362 | /* We don't want to quit even if we failed to add suspend/resume */ | |
1363 | printk(KERN_ERR PREFIX "Could not register bus type\n"); | |
1364 | } | |
1365 | ||
1da177e4 LT |
1366 | /* |
1367 | * Create the root device in the bus's device tree | |
1368 | */ | |
3fb02738 | 1369 | result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT, |
4be44fcd | 1370 | ACPI_BUS_TYPE_SYSTEM); |
1da177e4 LT |
1371 | if (result) |
1372 | goto Done; | |
1373 | ||
3fb02738 | 1374 | result = acpi_start_single_object(acpi_root); |
5b327265 PM |
1375 | if (result) |
1376 | goto Done; | |
1377 | ||
1da177e4 LT |
1378 | /* |
1379 | * Enumerate devices in the ACPI namespace. | |
1380 | */ | |
1381 | result = acpi_bus_scan_fixed(acpi_root); | |
3fb02738 RS |
1382 | if (!result) { |
1383 | memset(&ops, 0, sizeof(ops)); | |
1384 | ops.acpi_op_add = 1; | |
1385 | ops.acpi_op_start = 1; | |
1386 | result = acpi_bus_scan(acpi_root, &ops); | |
1387 | } | |
1da177e4 LT |
1388 | |
1389 | if (result) | |
1390 | acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL); | |
1391 | ||
4be44fcd | 1392 | Done: |
d550d98d | 1393 | return result; |
1da177e4 LT |
1394 | } |
1395 | ||
1396 | subsys_initcall(acpi_scan_init); |