1
2
3
4
5#include <linux/module.h>
6#include <linux/init.h>
7#include <linux/kernel.h>
8#include <linux/acpi.h>
9#include <linux/signal.h>
10#include <linux/kthread.h>
11
12#include <acpi/acpi_drivers.h>
13#include <acpi/acinterp.h>
14
15#define _COMPONENT ACPI_BUS_COMPONENT
16ACPI_MODULE_NAME("scan");
17#define STRUCT_TO_INT(s) (*((int*)&s))
18extern struct acpi_device *acpi_root;
19
20#define ACPI_BUS_CLASS "system_bus"
21#define ACPI_BUS_HID "LNXSYBUS"
22#define ACPI_BUS_DEVICE_NAME "System Bus"
23
24static LIST_HEAD(acpi_device_list);
25static LIST_HEAD(acpi_bus_id_list);
26DEFINE_SPINLOCK(acpi_device_lock);
27LIST_HEAD(acpi_wakeup_device_list);
28
29struct acpi_device_bus_id{
30 char bus_id[15];
31 unsigned int instance_no;
32 struct list_head node;
33};
34
35
36
37
38
39
40static int create_modalias(struct acpi_device *acpi_dev, char *modalias,
41 int size)
42{
43 int len;
44 int count;
45
46 if (!acpi_dev->flags.hardware_id && !acpi_dev->flags.compatible_ids)
47 return -ENODEV;
48
49 len = snprintf(modalias, size, "acpi:");
50 size -= len;
51
52 if (acpi_dev->flags.hardware_id) {
53 count = snprintf(&modalias[len], size, "%s:",
54 acpi_dev->pnp.hardware_id);
55 if (count < 0 || count >= size)
56 return -EINVAL;
57 len += count;
58 size -= count;
59 }
60
61 if (acpi_dev->flags.compatible_ids) {
62 struct acpi_compatible_id_list *cid_list;
63 int i;
64
65 cid_list = acpi_dev->pnp.cid_list;
66 for (i = 0; i < cid_list->count; i++) {
67 count = snprintf(&modalias[len], size, "%s:",
68 cid_list->id[i].value);
69 if (count < 0 || count >= size) {
70 printk(KERN_ERR PREFIX "%s cid[%i] exceeds event buffer size",
71 acpi_dev->pnp.device_name, i);
72 break;
73 }
74 len += count;
75 size -= count;
76 }
77 }
78
79 modalias[len] = '\0';
80 return len;
81}
82
83static ssize_t
84acpi_device_modalias_show(struct device *dev, struct device_attribute *attr, char *buf) {
85 struct acpi_device *acpi_dev = to_acpi_device(dev);
86 int len;
87
88
89 len = create_modalias(acpi_dev, buf, 1024);
90 if (len <= 0)
91 return 0;
92 buf[len++] = '\n';
93 return len;
94}
95static DEVICE_ATTR(modalias, 0444, acpi_device_modalias_show, NULL);
96
97static int acpi_bus_hot_remove_device(void *context)
98{
99 struct acpi_device *device;
100 acpi_handle handle = context;
101 struct acpi_object_list arg_list;
102 union acpi_object arg;
103 acpi_status status = AE_OK;
104
105 if (acpi_bus_get_device(handle, &device))
106 return 0;
107
108 if (!device)
109 return 0;
110
111 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
112 "Hot-removing device %s...\n", device->dev.bus_id));
113
114
115 if (acpi_bus_trim(device, 1)) {
116 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
117 "Removing device failed\n"));
118 return -1;
119 }
120
121
122 status = acpi_evaluate_object(handle, "_PS3", NULL, NULL);
123 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
124 ACPI_DEBUG_PRINT((ACPI_DB_WARN,
125 "Power-off device failed\n"));
126
127 if (device->flags.lockable) {
128 arg_list.count = 1;
129 arg_list.pointer = &arg;
130 arg.type = ACPI_TYPE_INTEGER;
131 arg.integer.value = 0;
132 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
133 }
134
135 arg_list.count = 1;
136 arg_list.pointer = &arg;
137 arg.type = ACPI_TYPE_INTEGER;
138 arg.integer.value = 1;
139
140
141
142
143 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
144 if (ACPI_FAILURE(status))
145 return -ENODEV;
146
147 return 0;
148}
149
150static ssize_t
151acpi_eject_store(struct device *d, struct device_attribute *attr,
152 const char *buf, size_t count)
153{
154 int ret = count;
155 acpi_status status;
156 acpi_object_type type = 0;
157 struct acpi_device *acpi_device = to_acpi_device(d);
158 struct task_struct *task;
159
160 if ((!count) || (buf[0] != '1')) {
161 return -EINVAL;
162 }
163#ifndef FORCE_EJECT
164 if (acpi_device->driver == NULL) {
165 ret = -ENODEV;
166 goto err;
167 }
168#endif
169 status = acpi_get_type(acpi_device->handle, &type);
170 if (ACPI_FAILURE(status) || (!acpi_device->flags.ejectable)) {
171 ret = -ENODEV;
172 goto err;
173 }
174
175
176 task = kthread_run(acpi_bus_hot_remove_device,
177 acpi_device->handle, "acpi_hot_remove_device");
178 if (IS_ERR(task))
179 ret = PTR_ERR(task);
180err:
181 return ret;
182}
183
184static DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
185
186static ssize_t
187acpi_device_hid_show(struct device *dev, struct device_attribute *attr, char *buf) {
188 struct acpi_device *acpi_dev = to_acpi_device(dev);
189
190 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
191}
192static DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
193
194static ssize_t
195acpi_device_path_show(struct device *dev, struct device_attribute *attr, char *buf) {
196 struct acpi_device *acpi_dev = to_acpi_device(dev);
197 struct acpi_buffer path = {ACPI_ALLOCATE_BUFFER, NULL};
198 int result;
199
200 result = acpi_get_name(acpi_dev->handle, ACPI_FULL_PATHNAME, &path);
201 if(result)
202 goto end;
203
204 result = sprintf(buf, "%s\n", (char*)path.pointer);
205 kfree(path.pointer);
206 end:
207 return result;
208}
209static DEVICE_ATTR(path, 0444, acpi_device_path_show, NULL);
210
211static int acpi_device_setup_files(struct acpi_device *dev)
212{
213 acpi_status status;
214 acpi_handle temp;
215 int result = 0;
216
217
218
219
220 if(dev->handle) {
221 result = device_create_file(&dev->dev, &dev_attr_path);
222 if(result)
223 goto end;
224 }
225
226 if(dev->flags.hardware_id) {
227 result = device_create_file(&dev->dev, &dev_attr_hid);
228 if(result)
229 goto end;
230 }
231
232 if (dev->flags.hardware_id || dev->flags.compatible_ids){
233 result = device_create_file(&dev->dev, &dev_attr_modalias);
234 if(result)
235 goto end;
236 }
237
238
239
240
241
242 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
243 if (ACPI_SUCCESS(status))
244 result = device_create_file(&dev->dev, &dev_attr_eject);
245 end:
246 return result;
247}
248
249static void acpi_device_remove_files(struct acpi_device *dev)
250{
251 acpi_status status;
252 acpi_handle temp;
253
254
255
256
257
258 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
259 if (ACPI_SUCCESS(status))
260 device_remove_file(&dev->dev, &dev_attr_eject);
261
262 if (dev->flags.hardware_id || dev->flags.compatible_ids)
263 device_remove_file(&dev->dev, &dev_attr_modalias);
264
265 if(dev->flags.hardware_id)
266 device_remove_file(&dev->dev, &dev_attr_hid);
267 if(dev->handle)
268 device_remove_file(&dev->dev, &dev_attr_path);
269}
270
271
272
273
274int acpi_match_device_ids(struct acpi_device *device,
275 const struct acpi_device_id *ids)
276{
277 const struct acpi_device_id *id;
278
279
280
281
282
283 if (!device->status.present)
284 return -ENODEV;
285
286 if (device->flags.hardware_id) {
287 for (id = ids; id->id[0]; id++) {
288 if (!strcmp((char*)id->id, device->pnp.hardware_id))
289 return 0;
290 }
291 }
292
293 if (device->flags.compatible_ids) {
294 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
295 int i;
296
297 for (id = ids; id->id[0]; id++) {
298
299 for (i = 0; i < cid_list->count; i++) {
300 if (!strcmp((char*)id->id,
301 cid_list->id[i].value))
302 return 0;
303 }
304 }
305 }
306
307 return -ENOENT;
308}
309EXPORT_SYMBOL(acpi_match_device_ids);
310
311static void acpi_device_release(struct device *dev)
312{
313 struct acpi_device *acpi_dev = to_acpi_device(dev);
314
315 kfree(acpi_dev->pnp.cid_list);
316 kfree(acpi_dev);
317}
318
319static int acpi_device_suspend(struct device *dev, pm_message_t state)
320{
321 struct acpi_device *acpi_dev = to_acpi_device(dev);
322 struct acpi_driver *acpi_drv = acpi_dev->driver;
323
324 if (acpi_drv && acpi_drv->ops.suspend)
325 return acpi_drv->ops.suspend(acpi_dev, state);
326 return 0;
327}
328
329static int acpi_device_resume(struct device *dev)
330{
331 struct acpi_device *acpi_dev = to_acpi_device(dev);
332 struct acpi_driver *acpi_drv = acpi_dev->driver;
333
334 if (acpi_drv && acpi_drv->ops.resume)
335 return acpi_drv->ops.resume(acpi_dev);
336 return 0;
337}
338
339static int acpi_bus_match(struct device *dev, struct device_driver *drv)
340{
341 struct acpi_device *acpi_dev = to_acpi_device(dev);
342 struct acpi_driver *acpi_drv = to_acpi_driver(drv);
343
344 return !acpi_match_device_ids(acpi_dev, acpi_drv->ids);
345}
346
347static int acpi_device_uevent(struct device *dev, struct kobj_uevent_env *env)
348{
349 struct acpi_device *acpi_dev = to_acpi_device(dev);
350 int len;
351
352 if (add_uevent_var(env, "MODALIAS="))
353 return -ENOMEM;
354 len = create_modalias(acpi_dev, &env->buf[env->buflen - 1],
355 sizeof(env->buf) - env->buflen);
356 if (len >= (sizeof(env->buf) - env->buflen))
357 return -ENOMEM;
358 env->buflen += len;
359 return 0;
360}
361
362static int acpi_bus_driver_init(struct acpi_device *, struct acpi_driver *);
363static int acpi_start_single_object(struct acpi_device *);
364static int acpi_device_probe(struct device * dev)
365{
366 struct acpi_device *acpi_dev = to_acpi_device(dev);
367 struct acpi_driver *acpi_drv = to_acpi_driver(dev->driver);
368 int ret;
369
370 ret = acpi_bus_driver_init(acpi_dev, acpi_drv);
371 if (!ret) {
372 if (acpi_dev->bus_ops.acpi_op_start)
373 acpi_start_single_object(acpi_dev);
374 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
375 "Found driver [%s] for device [%s]\n",
376 acpi_drv->name, acpi_dev->pnp.bus_id));
377 get_device(dev);
378 }
379 return ret;
380}
381
382static int acpi_device_remove(struct device * dev)
383{
384 struct acpi_device *acpi_dev = to_acpi_device(dev);
385 struct acpi_driver *acpi_drv = acpi_dev->driver;
386
387 if (acpi_drv) {
388 if (acpi_drv->ops.stop)
389 acpi_drv->ops.stop(acpi_dev, acpi_dev->removal_type);
390 if (acpi_drv->ops.remove)
391 acpi_drv->ops.remove(acpi_dev, acpi_dev->removal_type);
392 }
393 acpi_dev->driver = NULL;
394 acpi_driver_data(dev) = NULL;
395
396 put_device(dev);
397 return 0;
398}
399
400static void acpi_device_shutdown(struct device *dev)
401{
402 struct acpi_device *acpi_dev = to_acpi_device(dev);
403 struct acpi_driver *acpi_drv = acpi_dev->driver;
404
405 if (acpi_drv && acpi_drv->ops.shutdown)
406 acpi_drv->ops.shutdown(acpi_dev);
407
408 return ;
409}
410
411struct bus_type acpi_bus_type = {
412 .name = "acpi",
413 .suspend = acpi_device_suspend,
414 .resume = acpi_device_resume,
415 .shutdown = acpi_device_shutdown,
416 .match = acpi_bus_match,
417 .probe = acpi_device_probe,
418 .remove = acpi_device_remove,
419 .uevent = acpi_device_uevent,
420};
421
422static int acpi_device_register(struct acpi_device *device,
423 struct acpi_device *parent)
424{
425 int result;
426 struct acpi_device_bus_id *acpi_device_bus_id, *new_bus_id;
427 int found = 0;
428
429
430
431
432
433 INIT_LIST_HEAD(&device->children);
434 INIT_LIST_HEAD(&device->node);
435 INIT_LIST_HEAD(&device->g_list);
436 INIT_LIST_HEAD(&device->wakeup_list);
437
438 new_bus_id = kzalloc(sizeof(struct acpi_device_bus_id), GFP_KERNEL);
439 if (!new_bus_id) {
440 printk(KERN_ERR PREFIX "Memory allocation error\n");
441 return -ENOMEM;
442 }
443
444 spin_lock(&acpi_device_lock);
445
446
447
448
449 list_for_each_entry(acpi_device_bus_id, &acpi_bus_id_list, node) {
450 if(!strcmp(acpi_device_bus_id->bus_id, device->flags.hardware_id? device->pnp.hardware_id : "device")) {
451 acpi_device_bus_id->instance_no ++;
452 found = 1;
453 kfree(new_bus_id);
454 break;
455 }
456 }
457 if(!found) {
458 acpi_device_bus_id = new_bus_id;
459 strcpy(acpi_device_bus_id->bus_id, device->flags.hardware_id ? device->pnp.hardware_id : "device");
460 acpi_device_bus_id->instance_no = 0;
461 list_add_tail(&acpi_device_bus_id->node, &acpi_bus_id_list);
462 }
463 sprintf(device->dev.bus_id, "%s:%02x", acpi_device_bus_id->bus_id, acpi_device_bus_id->instance_no);
464
465 if (device->parent) {
466 list_add_tail(&device->node, &device->parent->children);
467 list_add_tail(&device->g_list, &device->parent->g_list);
468 } else
469 list_add_tail(&device->g_list, &acpi_device_list);
470 if (device->wakeup.flags.valid)
471 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
472 spin_unlock(&acpi_device_lock);
473
474 if (device->parent)
475 device->dev.parent = &parent->dev;
476 device->dev.bus = &acpi_bus_type;
477 device_initialize(&device->dev);
478 device->dev.release = &acpi_device_release;
479 result = device_add(&device->dev);
480 if(result) {
481 dev_err(&device->dev, "Error adding device\n");
482 goto end;
483 }
484
485 result = acpi_device_setup_files(device);
486 if(result)
487 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error creating sysfs interface for device %s\n", device->dev.bus_id));
488
489 device->removal_type = ACPI_BUS_REMOVAL_NORMAL;
490 return 0;
491 end:
492 spin_lock(&acpi_device_lock);
493 if (device->parent) {
494 list_del(&device->node);
495 list_del(&device->g_list);
496 } else
497 list_del(&device->g_list);
498 list_del(&device->wakeup_list);
499 spin_unlock(&acpi_device_lock);
500 return result;
501}
502
503static void acpi_device_unregister(struct acpi_device *device, int type)
504{
505 spin_lock(&acpi_device_lock);
506 if (device->parent) {
507 list_del(&device->node);
508 list_del(&device->g_list);
509 } else
510 list_del(&device->g_list);
511
512 list_del(&device->wakeup_list);
513 spin_unlock(&acpi_device_lock);
514
515 acpi_detach_data(device->handle, acpi_bus_data_handler);
516
517 acpi_device_remove_files(device);
518 device_unregister(&device->dev);
519}
520
521
522
523
524
525
526
527
528
529
530
531
532static int
533acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
534{
535 int result = 0;
536
537
538 if (!device || !driver)
539 return -EINVAL;
540
541 if (!driver->ops.add)
542 return -ENOSYS;
543
544 result = driver->ops.add(device);
545 if (result) {
546 device->driver = NULL;
547 acpi_driver_data(device) = NULL;
548 return result;
549 }
550
551 device->driver = driver;
552
553
554
555
556
557
558 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
559 "Driver successfully bound to device\n"));
560 return 0;
561}
562
563static int acpi_start_single_object(struct acpi_device *device)
564{
565 int result = 0;
566 struct acpi_driver *driver;
567
568
569 if (!(driver = device->driver))
570 return 0;
571
572 if (driver->ops.start) {
573 result = driver->ops.start(device);
574 if (result && driver->ops.remove)
575 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
576 }
577
578 return result;
579}
580
581
582
583
584
585
586
587
588
589int acpi_bus_register_driver(struct acpi_driver *driver)
590{
591 int ret;
592
593 if (acpi_disabled)
594 return -ENODEV;
595 driver->drv.name = driver->name;
596 driver->drv.bus = &acpi_bus_type;
597 driver->drv.owner = driver->owner;
598
599 ret = driver_register(&driver->drv);
600 return ret;
601}
602
603EXPORT_SYMBOL(acpi_bus_register_driver);
604
605
606
607
608
609
610
611
612void acpi_bus_unregister_driver(struct acpi_driver *driver)
613{
614 driver_unregister(&driver->drv);
615}
616
617EXPORT_SYMBOL(acpi_bus_unregister_driver);
618
619
620
621
622acpi_status
623acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
624{
625 acpi_status status;
626 acpi_handle tmp;
627 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
628 union acpi_object *obj;
629
630 status = acpi_get_handle(handle, "_EJD", &tmp);
631 if (ACPI_FAILURE(status))
632 return status;
633
634 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
635 if (ACPI_SUCCESS(status)) {
636 obj = buffer.pointer;
637 status = acpi_get_handle(ACPI_ROOT_OBJECT, obj->string.pointer,
638 ejd);
639 kfree(buffer.pointer);
640 }
641 return status;
642}
643EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
644
645void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
646{
647
648
649
650 return;
651}
652
653static int acpi_bus_get_perf_flags(struct acpi_device *device)
654{
655 device->performance.state = ACPI_STATE_UNKNOWN;
656 return 0;
657}
658
659static acpi_status
660acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
661 union acpi_object *package)
662{
663 int i = 0;
664 union acpi_object *element = NULL;
665
666 if (!device || !package || (package->package.count < 2))
667 return AE_BAD_PARAMETER;
668
669 element = &(package->package.elements[0]);
670 if (!element)
671 return AE_BAD_PARAMETER;
672 if (element->type == ACPI_TYPE_PACKAGE) {
673 if ((element->package.count < 2) ||
674 (element->package.elements[0].type !=
675 ACPI_TYPE_LOCAL_REFERENCE)
676 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
677 return AE_BAD_DATA;
678 device->wakeup.gpe_device =
679 element->package.elements[0].reference.handle;
680 device->wakeup.gpe_number =
681 (u32) element->package.elements[1].integer.value;
682 } else if (element->type == ACPI_TYPE_INTEGER) {
683 device->wakeup.gpe_number = element->integer.value;
684 } else
685 return AE_BAD_DATA;
686
687 element = &(package->package.elements[1]);
688 if (element->type != ACPI_TYPE_INTEGER) {
689 return AE_BAD_DATA;
690 }
691 device->wakeup.sleep_state = element->integer.value;
692
693 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
694 return AE_NO_MEMORY;
695 }
696 device->wakeup.resources.count = package->package.count - 2;
697 for (i = 0; i < device->wakeup.resources.count; i++) {
698 element = &(package->package.elements[i + 2]);
699 if (element->type != ACPI_TYPE_LOCAL_REFERENCE)
700 return AE_BAD_DATA;
701
702 device->wakeup.resources.handles[i] = element->reference.handle;
703 }
704
705 return AE_OK;
706}
707
708static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
709{
710 acpi_status status = 0;
711 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
712 union acpi_object *package = NULL;
713 int psw_error;
714
715 struct acpi_device_id button_device_ids[] = {
716 {"PNP0C0D", 0},
717 {"PNP0C0C", 0},
718 {"PNP0C0E", 0},
719 {"", 0},
720 };
721
722
723 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
724 if (ACPI_FAILURE(status)) {
725 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
726 goto end;
727 }
728
729 package = (union acpi_object *)buffer.pointer;
730 status = acpi_bus_extract_wakeup_device_power_package(device, package);
731 if (ACPI_FAILURE(status)) {
732 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
733 goto end;
734 }
735
736 kfree(buffer.pointer);
737
738 device->wakeup.flags.valid = 1;
739
740
741
742
743
744
745 psw_error = acpi_device_sleep_wake(device, 0, 0, 0);
746 if (psw_error)
747 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
748 "error in _DSW or _PSW evaluation\n"));
749
750
751 if (!acpi_match_device_ids(device, button_device_ids))
752 device->wakeup.flags.run_wake = 1;
753
754 end:
755 if (ACPI_FAILURE(status))
756 device->flags.wake_capable = 0;
757 return 0;
758}
759
760static int acpi_bus_get_power_flags(struct acpi_device *device)
761{
762 acpi_status status = 0;
763 acpi_handle handle = NULL;
764 u32 i = 0;
765
766
767
768
769
770 status = acpi_get_handle(device->handle, "_PSC", &handle);
771 if (ACPI_SUCCESS(status))
772 device->power.flags.explicit_get = 1;
773 status = acpi_get_handle(device->handle, "_IRC", &handle);
774 if (ACPI_SUCCESS(status))
775 device->power.flags.inrush_current = 1;
776
777
778
779
780 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
781 struct acpi_device_power_state *ps = &device->power.states[i];
782 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
783
784
785 acpi_evaluate_reference(device->handle, object_name, NULL,
786 &ps->resources);
787 if (ps->resources.count) {
788 device->power.flags.power_resources = 1;
789 ps->flags.valid = 1;
790 }
791
792
793 object_name[2] = 'S';
794 status = acpi_get_handle(device->handle, object_name, &handle);
795 if (ACPI_SUCCESS(status)) {
796 ps->flags.explicit_set = 1;
797 ps->flags.valid = 1;
798 }
799
800
801 if (ps->resources.count || ps->flags.explicit_set)
802 ps->flags.valid = 1;
803
804 ps->power = -1;
805 ps->latency = -1;
806 }
807
808
809 device->power.states[ACPI_STATE_D0].flags.valid = 1;
810 device->power.states[ACPI_STATE_D0].power = 100;
811 device->power.states[ACPI_STATE_D3].flags.valid = 1;
812 device->power.states[ACPI_STATE_D3].power = 0;
813
814
815
816 device->power.state = ACPI_STATE_UNKNOWN;
817
818 return 0;
819}
820
821static int acpi_bus_get_flags(struct acpi_device *device)
822{
823 acpi_status status = AE_OK;
824 acpi_handle temp = NULL;
825
826
827
828 status = acpi_get_handle(device->handle, "_STA", &temp);
829 if (ACPI_SUCCESS(status))
830 device->flags.dynamic_status = 1;
831
832
833 status = acpi_get_handle(device->handle, "_CID", &temp);
834 if (ACPI_SUCCESS(status))
835 device->flags.compatible_ids = 1;
836
837
838 status = acpi_get_handle(device->handle, "_RMV", &temp);
839 if (ACPI_SUCCESS(status))
840 device->flags.removable = 1;
841
842
843 status = acpi_get_handle(device->handle, "_EJD", &temp);
844 if (ACPI_SUCCESS(status))
845 device->flags.ejectable = 1;
846 else {
847 status = acpi_get_handle(device->handle, "_EJ0", &temp);
848 if (ACPI_SUCCESS(status))
849 device->flags.ejectable = 1;
850 }
851
852
853 status = acpi_get_handle(device->handle, "_LCK", &temp);
854 if (ACPI_SUCCESS(status))
855 device->flags.lockable = 1;
856
857
858 status = acpi_get_handle(device->handle, "_PS0", &temp);
859 if (ACPI_FAILURE(status))
860 status = acpi_get_handle(device->handle, "_PR0", &temp);
861 if (ACPI_SUCCESS(status))
862 device->flags.power_manageable = 1;
863
864
865 status = acpi_get_handle(device->handle, "_PRW", &temp);
866 if (ACPI_SUCCESS(status))
867 device->flags.wake_capable = 1;
868
869
870
871 return 0;
872}
873
874static void acpi_device_get_busid(struct acpi_device *device,
875 acpi_handle handle, int type)
876{
877 char bus_id[5] = { '?', 0 };
878 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
879 int i = 0;
880
881
882
883
884
885
886
887 switch (type) {
888 case ACPI_BUS_TYPE_SYSTEM:
889 strcpy(device->pnp.bus_id, "ACPI");
890 break;
891 case ACPI_BUS_TYPE_POWER_BUTTON:
892 strcpy(device->pnp.bus_id, "PWRF");
893 break;
894 case ACPI_BUS_TYPE_SLEEP_BUTTON:
895 strcpy(device->pnp.bus_id, "SLPF");
896 break;
897 default:
898 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
899
900 for (i = 3; i > 1; i--) {
901 if (bus_id[i] == '_')
902 bus_id[i] = '\0';
903 else
904 break;
905 }
906 strcpy(device->pnp.bus_id, bus_id);
907 break;
908 }
909}
910
911static int
912acpi_video_bus_match(struct acpi_device *device)
913{
914 acpi_handle h_dummy;
915
916 if (!device)
917 return -EINVAL;
918
919
920
921
922
923
924 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOD", &h_dummy)) &&
925 ACPI_SUCCESS(acpi_get_handle(device->handle, "_DOS", &h_dummy)))
926 return 0;
927
928
929 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_ROM", &h_dummy)))
930 return 0;
931
932
933 if (ACPI_SUCCESS(acpi_get_handle(device->handle, "_VPO", &h_dummy)) &&
934 ACPI_SUCCESS(acpi_get_handle(device->handle, "_GPD", &h_dummy)) &&
935 ACPI_SUCCESS(acpi_get_handle(device->handle, "_SPD", &h_dummy)))
936 return 0;
937
938 return -ENODEV;
939}
940
941
942
943
944
945
946
947static int acpi_bay_match(struct acpi_device *device){
948 acpi_status status;
949 acpi_handle handle;
950 acpi_handle tmp;
951 acpi_handle phandle;
952
953 handle = device->handle;
954
955 status = acpi_get_handle(handle, "_EJ0", &tmp);
956 if (ACPI_FAILURE(status))
957 return -ENODEV;
958
959 if ((ACPI_SUCCESS(acpi_get_handle(handle, "_GTF", &tmp))) ||
960 (ACPI_SUCCESS(acpi_get_handle(handle, "_GTM", &tmp))) ||
961 (ACPI_SUCCESS(acpi_get_handle(handle, "_STM", &tmp))) ||
962 (ACPI_SUCCESS(acpi_get_handle(handle, "_SDD", &tmp))))
963 return 0;
964
965 if (acpi_get_parent(handle, &phandle))
966 return -ENODEV;
967
968 if ((ACPI_SUCCESS(acpi_get_handle(phandle, "_GTF", &tmp))) ||
969 (ACPI_SUCCESS(acpi_get_handle(phandle, "_GTM", &tmp))) ||
970 (ACPI_SUCCESS(acpi_get_handle(phandle, "_STM", &tmp))) ||
971 (ACPI_SUCCESS(acpi_get_handle(phandle, "_SDD", &tmp))))
972 return 0;
973
974 return -ENODEV;
975}
976
977
978
979
980static int acpi_dock_match(struct acpi_device *device)
981{
982 acpi_handle tmp;
983 return acpi_get_handle(device->handle, "_DCK", &tmp);
984}
985
986static void acpi_device_set_id(struct acpi_device *device,
987 struct acpi_device *parent, acpi_handle handle,
988 int type)
989{
990 struct acpi_device_info *info;
991 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
992 char *hid = NULL;
993 char *uid = NULL;
994 struct acpi_compatible_id_list *cid_list = NULL;
995 const char *cid_add = NULL;
996 acpi_status status;
997
998 switch (type) {
999 case ACPI_BUS_TYPE_DEVICE:
1000 status = acpi_get_object_info(handle, &buffer);
1001 if (ACPI_FAILURE(status)) {
1002 printk(KERN_ERR PREFIX "%s: Error reading device info\n", __func__);
1003 return;
1004 }
1005
1006 info = buffer.pointer;
1007 if (info->valid & ACPI_VALID_HID)
1008 hid = info->hardware_id.value;
1009 if (info->valid & ACPI_VALID_UID)
1010 uid = info->unique_id.value;
1011 if (info->valid & ACPI_VALID_CID)
1012 cid_list = &info->compatibility_id;
1013 if (info->valid & ACPI_VALID_ADR) {
1014 device->pnp.bus_address = info->address;
1015 device->flags.bus_address = 1;
1016 }
1017
1018
1019
1020
1021
1022
1023 if (ACPI_SUCCESS(acpi_video_bus_match(device)))
1024 cid_add = ACPI_VIDEO_HID;
1025 else if (ACPI_SUCCESS(acpi_bay_match(device)))
1026 cid_add = ACPI_BAY_HID;
1027 else if (ACPI_SUCCESS(acpi_dock_match(device)))
1028 cid_add = ACPI_DOCK_HID;
1029
1030 break;
1031 case ACPI_BUS_TYPE_POWER:
1032 hid = ACPI_POWER_HID;
1033 break;
1034 case ACPI_BUS_TYPE_PROCESSOR:
1035 hid = ACPI_PROCESSOR_HID;
1036 break;
1037 case ACPI_BUS_TYPE_SYSTEM:
1038 hid = ACPI_SYSTEM_HID;
1039 break;
1040 case ACPI_BUS_TYPE_THERMAL:
1041 hid = ACPI_THERMAL_HID;
1042 break;
1043 case ACPI_BUS_TYPE_POWER_BUTTON:
1044 hid = ACPI_BUTTON_HID_POWERF;
1045 break;
1046 case ACPI_BUS_TYPE_SLEEP_BUTTON:
1047 hid = ACPI_BUTTON_HID_SLEEPF;
1048 break;
1049 }
1050
1051
1052
1053
1054
1055
1056 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
1057 hid = ACPI_BUS_HID;
1058 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
1059 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
1060 }
1061
1062 if (hid) {
1063 strcpy(device->pnp.hardware_id, hid);
1064 device->flags.hardware_id = 1;
1065 }
1066 if (uid) {
1067 strcpy(device->pnp.unique_id, uid);
1068 device->flags.unique_id = 1;
1069 }
1070 if (cid_list || cid_add) {
1071 struct acpi_compatible_id_list *list;
1072 int size = 0;
1073 int count = 0;
1074
1075 if (cid_list) {
1076 size = cid_list->size;
1077 } else if (cid_add) {
1078 size = sizeof(struct acpi_compatible_id_list);
1079 cid_list = ACPI_ALLOCATE_ZEROED((acpi_size) size);
1080 if (!cid_list) {
1081 printk(KERN_ERR "Memory allocation error\n");
1082 kfree(buffer.pointer);
1083 return;
1084 } else {
1085 cid_list->count = 0;
1086 cid_list->size = size;
1087 }
1088 }
1089 if (cid_add)
1090 size += sizeof(struct acpi_compatible_id);
1091 list = kmalloc(size, GFP_KERNEL);
1092
1093 if (list) {
1094 if (cid_list) {
1095 memcpy(list, cid_list, cid_list->size);
1096 count = cid_list->count;
1097 }
1098 if (cid_add) {
1099 strncpy(list->id[count].value, cid_add,
1100 ACPI_MAX_CID_LENGTH);
1101 count++;
1102 device->flags.compatible_ids = 1;
1103 }
1104 list->size = size;
1105 list->count = count;
1106 device->pnp.cid_list = list;
1107 } else
1108 printk(KERN_ERR PREFIX "Memory allocation error\n");
1109 }
1110
1111 kfree(buffer.pointer);
1112}
1113
1114static int acpi_device_set_context(struct acpi_device *device, int type)
1115{
1116 acpi_status status = AE_OK;
1117 int result = 0;
1118
1119
1120
1121
1122
1123
1124
1125
1126 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
1127 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
1128 status = acpi_attach_data(device->handle,
1129 acpi_bus_data_handler, device);
1130
1131 if (ACPI_FAILURE(status)) {
1132 printk(KERN_ERR PREFIX "Error attaching device data\n");
1133 result = -ENODEV;
1134 }
1135 }
1136 return result;
1137}
1138
1139static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
1140{
1141 if (!dev)
1142 return -EINVAL;
1143
1144 dev->removal_type = ACPI_BUS_REMOVAL_EJECT;
1145 device_release_driver(&dev->dev);
1146
1147 if (!rmdevice)
1148 return 0;
1149
1150
1151
1152
1153 if (dev->flags.bus_address) {
1154 if ((dev->parent) && (dev->parent->ops.unbind))
1155 dev->parent->ops.unbind(dev);
1156 }
1157 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1158
1159 return 0;
1160}
1161
1162static int
1163acpi_is_child_device(struct acpi_device *device,
1164 int (*matcher)(struct acpi_device *))
1165{
1166 int result = -ENODEV;
1167
1168 do {
1169 if (ACPI_SUCCESS(matcher(device)))
1170 return AE_OK;
1171 } while ((device = device->parent));
1172
1173 return result;
1174}
1175
1176static int
1177acpi_add_single_object(struct acpi_device **child,
1178 struct acpi_device *parent, acpi_handle handle, int type,
1179 struct acpi_bus_ops *ops)
1180{
1181 int result = 0;
1182 struct acpi_device *device = NULL;
1183
1184
1185 if (!child)
1186 return -EINVAL;
1187
1188 device = kzalloc(sizeof(struct acpi_device), GFP_KERNEL);
1189 if (!device) {
1190 printk(KERN_ERR PREFIX "Memory allocation error\n");
1191 return -ENOMEM;
1192 }
1193
1194 device->handle = handle;
1195 device->parent = parent;
1196 device->bus_ops = *ops;
1197
1198
1199 acpi_device_get_busid(device, handle, type);
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209 result = acpi_bus_get_flags(device);
1210 if (result)
1211 goto end;
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223 switch (type) {
1224 case ACPI_BUS_TYPE_PROCESSOR:
1225 case ACPI_BUS_TYPE_DEVICE:
1226 result = acpi_bus_get_status(device);
1227 if (ACPI_FAILURE(result)) {
1228 result = -ENODEV;
1229 goto end;
1230 }
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240 if (!device->status.present && !device->status.functional) {
1241 result = -ENODEV;
1242 goto end;
1243 }
1244 break;
1245 default:
1246 STRUCT_TO_INT(device->status) =
1247 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
1248 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
1249 break;
1250 }
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262 acpi_device_set_id(device, parent, handle, type);
1263
1264
1265
1266
1267
1268 if (device->flags.power_manageable) {
1269 result = acpi_bus_get_power_flags(device);
1270 if (result)
1271 goto end;
1272 }
1273
1274
1275
1276
1277
1278 if (device->flags.wake_capable) {
1279 result = acpi_bus_get_wakeup_device_flags(device);
1280 if (result)
1281 goto end;
1282 }
1283
1284
1285
1286
1287
1288 if (device->flags.performance_manageable) {
1289 result = acpi_bus_get_perf_flags(device);
1290 if (result)
1291 goto end;
1292 }
1293
1294 if ((result = acpi_device_set_context(device, type)))
1295 goto end;
1296
1297 result = acpi_device_register(device, parent);
1298
1299
1300
1301
1302 if (device->flags.bus_address) {
1303 if (device->parent && device->parent->ops.bind)
1304 device->parent->ops.bind(device);
1305 }
1306
1307 end:
1308 if (!result)
1309 *child = device;
1310 else {
1311 kfree(device->pnp.cid_list);
1312 kfree(device);
1313 }
1314
1315 return result;
1316}
1317
1318static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1319{
1320 acpi_status status = AE_OK;
1321 struct acpi_device *parent = NULL;
1322 struct acpi_device *child = NULL;
1323 acpi_handle phandle = NULL;
1324 acpi_handle chandle = NULL;
1325 acpi_object_type type = 0;
1326 u32 level = 1;
1327
1328
1329 if (!start)
1330 return -EINVAL;
1331
1332 parent = start;
1333 phandle = start->handle;
1334
1335
1336
1337
1338
1339 while ((level > 0) && parent) {
1340
1341 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1342 chandle, &chandle);
1343
1344
1345
1346
1347 if (ACPI_FAILURE(status)) {
1348 level--;
1349 chandle = phandle;
1350 acpi_get_parent(phandle, &phandle);
1351 if (parent->parent)
1352 parent = parent->parent;
1353 continue;
1354 }
1355
1356 status = acpi_get_type(chandle, &type);
1357 if (ACPI_FAILURE(status))
1358 continue;
1359
1360
1361
1362
1363 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1364 level++;
1365 phandle = chandle;
1366 chandle = NULL;
1367 continue;
1368 }
1369
1370
1371
1372
1373 switch (type) {
1374 case ACPI_TYPE_DEVICE:
1375 type = ACPI_BUS_TYPE_DEVICE;
1376 break;
1377 case ACPI_TYPE_PROCESSOR:
1378 type = ACPI_BUS_TYPE_PROCESSOR;
1379 break;
1380 case ACPI_TYPE_THERMAL:
1381 type = ACPI_BUS_TYPE_THERMAL;
1382 break;
1383 case ACPI_TYPE_POWER:
1384 type = ACPI_BUS_TYPE_POWER;
1385 break;
1386 default:
1387 continue;
1388 }
1389
1390 if (ops->acpi_op_add)
1391 status = acpi_add_single_object(&child, parent,
1392 chandle, type, ops);
1393 else
1394 status = acpi_bus_get_device(chandle, &child);
1395
1396 if (ACPI_FAILURE(status))
1397 continue;
1398
1399 if (ops->acpi_op_start && !(ops->acpi_op_add)) {
1400 status = acpi_start_single_object(child);
1401 if (ACPI_FAILURE(status))
1402 continue;
1403 }
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419 if (child->status.present || (!child->status.present &&
1420 child->status.functional)) {
1421 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1422 NULL, NULL);
1423 if (ACPI_SUCCESS(status)) {
1424 level++;
1425 phandle = chandle;
1426 chandle = NULL;
1427 parent = child;
1428 }
1429 }
1430 }
1431
1432 return 0;
1433}
1434
1435int
1436acpi_bus_add(struct acpi_device **child,
1437 struct acpi_device *parent, acpi_handle handle, int type)
1438{
1439 int result;
1440 struct acpi_bus_ops ops;
1441
1442 memset(&ops, 0, sizeof(ops));
1443 ops.acpi_op_add = 1;
1444
1445 result = acpi_add_single_object(child, parent, handle, type, &ops);
1446 if (!result)
1447 result = acpi_bus_scan(*child, &ops);
1448
1449 return result;
1450}
1451
1452EXPORT_SYMBOL(acpi_bus_add);
1453
1454int acpi_bus_start(struct acpi_device *device)
1455{
1456 int result;
1457 struct acpi_bus_ops ops;
1458
1459
1460 if (!device)
1461 return -EINVAL;
1462
1463 result = acpi_start_single_object(device);
1464 if (!result) {
1465 memset(&ops, 0, sizeof(ops));
1466 ops.acpi_op_start = 1;
1467 result = acpi_bus_scan(device, &ops);
1468 }
1469 return result;
1470}
1471
1472EXPORT_SYMBOL(acpi_bus_start);
1473
1474int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1475{
1476 acpi_status status;
1477 struct acpi_device *parent, *child;
1478 acpi_handle phandle, chandle;
1479 acpi_object_type type;
1480 u32 level = 1;
1481 int err = 0;
1482
1483 parent = start;
1484 phandle = start->handle;
1485 child = chandle = NULL;
1486
1487 while ((level > 0) && parent && (!err)) {
1488 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1489 chandle, &chandle);
1490
1491
1492
1493
1494 if (ACPI_FAILURE(status)) {
1495 level--;
1496 chandle = phandle;
1497 acpi_get_parent(phandle, &phandle);
1498 child = parent;
1499 parent = parent->parent;
1500
1501 if (level == 0)
1502 err = acpi_bus_remove(child, rmdevice);
1503 else
1504 err = acpi_bus_remove(child, 1);
1505
1506 continue;
1507 }
1508
1509 status = acpi_get_type(chandle, &type);
1510 if (ACPI_FAILURE(status)) {
1511 continue;
1512 }
1513
1514
1515
1516
1517 if (acpi_bus_get_device(chandle, &child) == 0) {
1518 level++;
1519 phandle = chandle;
1520 chandle = NULL;
1521 parent = child;
1522 }
1523 continue;
1524 }
1525 return err;
1526}
1527EXPORT_SYMBOL_GPL(acpi_bus_trim);
1528
1529
1530static int acpi_bus_scan_fixed(struct acpi_device *root)
1531{
1532 int result = 0;
1533 struct acpi_device *device = NULL;
1534 struct acpi_bus_ops ops;
1535
1536 if (!root)
1537 return -ENODEV;
1538
1539 memset(&ops, 0, sizeof(ops));
1540 ops.acpi_op_add = 1;
1541 ops.acpi_op_start = 1;
1542
1543
1544
1545
1546 if ((acpi_gbl_FADT.flags & ACPI_FADT_POWER_BUTTON) == 0) {
1547 result = acpi_add_single_object(&device, acpi_root,
1548 NULL,
1549 ACPI_BUS_TYPE_POWER_BUTTON,
1550 &ops);
1551 }
1552
1553 if ((acpi_gbl_FADT.flags & ACPI_FADT_SLEEP_BUTTON) == 0) {
1554 result = acpi_add_single_object(&device, acpi_root,
1555 NULL,
1556 ACPI_BUS_TYPE_SLEEP_BUTTON,
1557 &ops);
1558 }
1559
1560 return result;
1561}
1562
1563int __init acpi_boot_ec_enable(void);
1564
1565static int __init acpi_scan_init(void)
1566{
1567 int result;
1568 struct acpi_bus_ops ops;
1569
1570
1571 if (acpi_disabled)
1572 return 0;
1573
1574 memset(&ops, 0, sizeof(ops));
1575 ops.acpi_op_add = 1;
1576 ops.acpi_op_start = 1;
1577
1578 result = bus_register(&acpi_bus_type);
1579 if (result) {
1580
1581 printk(KERN_ERR PREFIX "Could not register bus type\n");
1582 }
1583
1584
1585
1586
1587 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1588 ACPI_BUS_TYPE_SYSTEM, &ops);
1589 if (result)
1590 goto Done;
1591
1592
1593
1594
1595 result = acpi_bus_scan_fixed(acpi_root);
1596
1597
1598 acpi_boot_ec_enable();
1599
1600 if (!result)
1601 result = acpi_bus_scan(acpi_root, &ops);
1602
1603 if (result)
1604 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1605
1606 Done:
1607 return result;
1608}
1609
1610subsys_initcall(acpi_scan_init);
1611