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