1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38#include <linux/kernel.h>
39#include <linux/module.h>
40#include <linux/init.h>
41#include <linux/types.h>
42#include <linux/proc_fs.h>
43#include <linux/seq_file.h>
44#include <acpi/acpi_bus.h>
45#include <acpi/acpi_drivers.h>
46#include "sleep.h"
47
48#define PREFIX "ACPI: "
49
50#define _COMPONENT ACPI_POWER_COMPONENT
51ACPI_MODULE_NAME("power");
52#define ACPI_POWER_CLASS "power_resource"
53#define ACPI_POWER_DEVICE_NAME "Power Resource"
54#define ACPI_POWER_FILE_INFO "info"
55#define ACPI_POWER_FILE_STATUS "state"
56#define ACPI_POWER_RESOURCE_STATE_OFF 0x00
57#define ACPI_POWER_RESOURCE_STATE_ON 0x01
58#define ACPI_POWER_RESOURCE_STATE_UNKNOWN 0xFF
59
60int acpi_power_nocheck;
61module_param_named(power_nocheck, acpi_power_nocheck, bool, 000);
62
63static int acpi_power_add(struct acpi_device *device);
64static int acpi_power_remove(struct acpi_device *device, int type);
65static int acpi_power_resume(struct acpi_device *device);
66static int acpi_power_open_fs(struct inode *inode, struct file *file);
67
68static struct acpi_device_id power_device_ids[] = {
69 {ACPI_POWER_HID, 0},
70 {"", 0},
71};
72MODULE_DEVICE_TABLE(acpi, power_device_ids);
73
74static struct acpi_driver acpi_power_driver = {
75 .name = "power",
76 .class = ACPI_POWER_CLASS,
77 .ids = power_device_ids,
78 .ops = {
79 .add = acpi_power_add,
80 .remove = acpi_power_remove,
81 .resume = acpi_power_resume,
82 },
83};
84
85struct acpi_power_reference {
86 struct list_head node;
87 struct acpi_device *device;
88};
89
90struct acpi_power_resource {
91 struct acpi_device * device;
92 acpi_bus_id name;
93 u32 system_level;
94 u32 order;
95 struct mutex resource_lock;
96 struct list_head reference;
97};
98
99static struct list_head acpi_power_resource_list;
100
101static const struct file_operations acpi_power_fops = {
102 .owner = THIS_MODULE,
103 .open = acpi_power_open_fs,
104 .read = seq_read,
105 .llseek = seq_lseek,
106 .release = single_release,
107};
108
109
110
111
112
113static int
114acpi_power_get_context(acpi_handle handle,
115 struct acpi_power_resource **resource)
116{
117 int result = 0;
118 struct acpi_device *device = NULL;
119
120
121 if (!resource)
122 return -ENODEV;
123
124 result = acpi_bus_get_device(handle, &device);
125 if (result) {
126 printk(KERN_WARNING PREFIX "Getting context [%p]\n", handle);
127 return result;
128 }
129
130 *resource = acpi_driver_data(device);
131 if (!*resource)
132 return -ENODEV;
133
134 return 0;
135}
136
137static int acpi_power_get_state(acpi_handle handle, int *state)
138{
139 acpi_status status = AE_OK;
140 unsigned long long sta = 0;
141 char node_name[5];
142 struct acpi_buffer buffer = { sizeof(node_name), node_name };
143
144
145 if (!handle || !state)
146 return -EINVAL;
147
148 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
149 if (ACPI_FAILURE(status))
150 return -ENODEV;
151
152 *state = (sta & 0x01)?ACPI_POWER_RESOURCE_STATE_ON:
153 ACPI_POWER_RESOURCE_STATE_OFF;
154
155 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
156
157 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] is %s\n",
158 node_name,
159 *state ? "on" : "off"));
160
161 return 0;
162}
163
164static int acpi_power_get_list_state(struct acpi_handle_list *list, int *state)
165{
166 int result = 0, state1;
167 u32 i = 0;
168
169
170 if (!list || !state)
171 return -EINVAL;
172
173
174
175
176 for (i = 0; i < list->count; i++) {
177
178
179
180
181
182 result = acpi_power_get_state(list->handles[i], &state1);
183 if (result)
184 return result;
185
186 *state = state1;
187
188 if (*state != ACPI_POWER_RESOURCE_STATE_ON)
189 break;
190 }
191
192 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource list is %s\n",
193 *state ? "on" : "off"));
194
195 return result;
196}
197
198static int acpi_power_on(acpi_handle handle, struct acpi_device *dev)
199{
200 int result = 0;
201 int found = 0;
202 acpi_status status = AE_OK;
203 struct acpi_power_resource *resource = NULL;
204 struct list_head *node, *next;
205 struct acpi_power_reference *ref;
206
207
208 result = acpi_power_get_context(handle, &resource);
209 if (result)
210 return result;
211
212 mutex_lock(&resource->resource_lock);
213 list_for_each_safe(node, next, &resource->reference) {
214 ref = container_of(node, struct acpi_power_reference, node);
215 if (dev->handle == ref->device->handle) {
216 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] already referenced by resource [%s]\n",
217 dev->pnp.bus_id, resource->name));
218 found = 1;
219 break;
220 }
221 }
222
223 if (!found) {
224 ref = kmalloc(sizeof (struct acpi_power_reference),
225 irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL);
226 if (!ref) {
227 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "kmalloc() failed\n"));
228 mutex_unlock(&resource->resource_lock);
229 return -ENOMEM;
230 }
231 list_add_tail(&ref->node, &resource->reference);
232 ref->device = dev;
233 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] added to resource [%s] references\n",
234 dev->pnp.bus_id, resource->name));
235 }
236 mutex_unlock(&resource->resource_lock);
237
238 status = acpi_evaluate_object(resource->device->handle, "_ON", NULL, NULL);
239 if (ACPI_FAILURE(status))
240 return -ENODEV;
241
242
243 resource->device->power.state = ACPI_STATE_D0;
244
245 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned on\n",
246 resource->name));
247 return 0;
248}
249
250static int acpi_power_off_device(acpi_handle handle, struct acpi_device *dev)
251{
252 int result = 0;
253 acpi_status status = AE_OK;
254 struct acpi_power_resource *resource = NULL;
255 struct list_head *node, *next;
256 struct acpi_power_reference *ref;
257
258
259 result = acpi_power_get_context(handle, &resource);
260 if (result)
261 return result;
262
263 mutex_lock(&resource->resource_lock);
264 list_for_each_safe(node, next, &resource->reference) {
265 ref = container_of(node, struct acpi_power_reference, node);
266 if (dev->handle == ref->device->handle) {
267 list_del(&ref->node);
268 kfree(ref);
269 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] removed from resource [%s] references\n",
270 dev->pnp.bus_id, resource->name));
271 break;
272 }
273 }
274
275 if (!list_empty(&resource->reference)) {
276 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Cannot turn resource [%s] off - resource is in use\n",
277 resource->name));
278 mutex_unlock(&resource->resource_lock);
279 return 0;
280 }
281 mutex_unlock(&resource->resource_lock);
282
283 status = acpi_evaluate_object(resource->device->handle, "_OFF", NULL, NULL);
284 if (ACPI_FAILURE(status))
285 return -ENODEV;
286
287
288 resource->device->power.state = ACPI_STATE_D3;
289
290 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Resource [%s] turned off\n",
291 resource->name));
292
293 return 0;
294}
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313int acpi_device_sleep_wake(struct acpi_device *dev,
314 int enable, int sleep_state, int dev_state)
315{
316 union acpi_object in_arg[3];
317 struct acpi_object_list arg_list = { 3, in_arg };
318 acpi_status status = AE_OK;
319
320
321
322
323
324
325
326
327
328
329
330
331 in_arg[0].type = ACPI_TYPE_INTEGER;
332 in_arg[0].integer.value = enable;
333 in_arg[1].type = ACPI_TYPE_INTEGER;
334 in_arg[1].integer.value = sleep_state;
335 in_arg[2].type = ACPI_TYPE_INTEGER;
336 in_arg[2].integer.value = dev_state;
337 status = acpi_evaluate_object(dev->handle, "_DSW", &arg_list, NULL);
338 if (ACPI_SUCCESS(status)) {
339 return 0;
340 } else if (status != AE_NOT_FOUND) {
341 printk(KERN_ERR PREFIX "_DSW execution failed\n");
342 dev->wakeup.flags.valid = 0;
343 return -ENODEV;
344 }
345
346
347 arg_list.count = 1;
348 in_arg[0].integer.value = enable;
349 status = acpi_evaluate_object(dev->handle, "_PSW", &arg_list, NULL);
350 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
351 printk(KERN_ERR PREFIX "_PSW execution failed\n");
352 dev->wakeup.flags.valid = 0;
353 return -ENODEV;
354 }
355
356 return 0;
357}
358
359
360
361
362
363
364
365int acpi_enable_wakeup_device_power(struct acpi_device *dev, int sleep_state)
366{
367 int i, err = 0;
368
369 if (!dev || !dev->wakeup.flags.valid)
370 return -EINVAL;
371
372 mutex_lock(&acpi_device_lock);
373
374 if (dev->wakeup.prepare_count++)
375 goto out;
376
377
378 for (i = 0; i < dev->wakeup.resources.count; i++) {
379 int ret = acpi_power_on(dev->wakeup.resources.handles[i], dev);
380 if (ret) {
381 printk(KERN_ERR PREFIX "Transition power state\n");
382 dev->wakeup.flags.valid = 0;
383 err = -ENODEV;
384 goto err_out;
385 }
386 }
387
388
389
390
391
392 err = acpi_device_sleep_wake(dev, 1, sleep_state, 3);
393
394 err_out:
395 if (err)
396 dev->wakeup.prepare_count = 0;
397
398 out:
399 mutex_unlock(&acpi_device_lock);
400 return err;
401}
402
403
404
405
406
407
408
409int acpi_disable_wakeup_device_power(struct acpi_device *dev)
410{
411 int i, err = 0;
412
413 if (!dev || !dev->wakeup.flags.valid)
414 return -EINVAL;
415
416 mutex_lock(&acpi_device_lock);
417
418 if (--dev->wakeup.prepare_count > 0)
419 goto out;
420
421
422
423
424
425 if (dev->wakeup.prepare_count < 0)
426 dev->wakeup.prepare_count = 0;
427
428 err = acpi_device_sleep_wake(dev, 0, 0, 0);
429 if (err)
430 goto out;
431
432
433 for (i = 0; i < dev->wakeup.resources.count; i++) {
434 int ret = acpi_power_off_device(
435 dev->wakeup.resources.handles[i], dev);
436 if (ret) {
437 printk(KERN_ERR PREFIX "Transition power state\n");
438 dev->wakeup.flags.valid = 0;
439 err = -ENODEV;
440 goto out;
441 }
442 }
443
444 out:
445 mutex_unlock(&acpi_device_lock);
446 return err;
447}
448
449
450
451
452
453int acpi_power_get_inferred_state(struct acpi_device *device)
454{
455 int result = 0;
456 struct acpi_handle_list *list = NULL;
457 int list_state = 0;
458 int i = 0;
459
460
461 if (!device)
462 return -EINVAL;
463
464 device->power.state = ACPI_STATE_UNKNOWN;
465
466
467
468
469
470 for (i = ACPI_STATE_D0; i < ACPI_STATE_D3; i++) {
471 list = &device->power.states[i].resources;
472 if (list->count < 1)
473 continue;
474
475 result = acpi_power_get_list_state(list, &list_state);
476 if (result)
477 return result;
478
479 if (list_state == ACPI_POWER_RESOURCE_STATE_ON) {
480 device->power.state = i;
481 return 0;
482 }
483 }
484
485 device->power.state = ACPI_STATE_D3;
486
487 return 0;
488}
489
490int acpi_power_transition(struct acpi_device *device, int state)
491{
492 int result = 0;
493 struct acpi_handle_list *cl = NULL;
494 struct acpi_handle_list *tl = NULL;
495 int i = 0;
496
497
498 if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
499 return -EINVAL;
500
501 if ((device->power.state < ACPI_STATE_D0)
502 || (device->power.state > ACPI_STATE_D3))
503 return -ENODEV;
504
505 cl = &device->power.states[device->power.state].resources;
506 tl = &device->power.states[state].resources;
507
508
509
510
511
512
513
514 for (i = 0; i < tl->count; i++) {
515 result = acpi_power_on(tl->handles[i], device);
516 if (result)
517 goto end;
518 }
519
520 if (device->power.state == state) {
521 goto end;
522 }
523
524
525
526
527 for (i = 0; i < cl->count; i++) {
528 result = acpi_power_off_device(cl->handles[i], device);
529 if (result)
530 goto end;
531 }
532
533 end:
534 if (result)
535 device->power.state = ACPI_STATE_UNKNOWN;
536 else {
537
538 device->power.state = state;
539 }
540
541 return result;
542}
543
544
545
546
547
548static struct proc_dir_entry *acpi_power_dir;
549
550static int acpi_power_seq_show(struct seq_file *seq, void *offset)
551{
552 int count = 0;
553 int result = 0, state;
554 struct acpi_power_resource *resource = NULL;
555 struct list_head *node, *next;
556 struct acpi_power_reference *ref;
557
558
559 resource = seq->private;
560
561 if (!resource)
562 goto end;
563
564 result = acpi_power_get_state(resource->device->handle, &state);
565 if (result)
566 goto end;
567
568 seq_puts(seq, "state: ");
569 switch (state) {
570 case ACPI_POWER_RESOURCE_STATE_ON:
571 seq_puts(seq, "on\n");
572 break;
573 case ACPI_POWER_RESOURCE_STATE_OFF:
574 seq_puts(seq, "off\n");
575 break;
576 default:
577 seq_puts(seq, "unknown\n");
578 break;
579 }
580
581 mutex_lock(&resource->resource_lock);
582 list_for_each_safe(node, next, &resource->reference) {
583 ref = container_of(node, struct acpi_power_reference, node);
584 count++;
585 }
586 mutex_unlock(&resource->resource_lock);
587
588 seq_printf(seq, "system level: S%d\n"
589 "order: %d\n"
590 "reference count: %d\n",
591 resource->system_level,
592 resource->order, count);
593
594 end:
595 return 0;
596}
597
598static int acpi_power_open_fs(struct inode *inode, struct file *file)
599{
600 return single_open(file, acpi_power_seq_show, PDE(inode)->data);
601}
602
603static int acpi_power_add_fs(struct acpi_device *device)
604{
605 struct proc_dir_entry *entry = NULL;
606
607
608 if (!device)
609 return -EINVAL;
610
611 if (!acpi_device_dir(device)) {
612 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
613 acpi_power_dir);
614 if (!acpi_device_dir(device))
615 return -ENODEV;
616 }
617
618
619 entry = proc_create_data(ACPI_POWER_FILE_STATUS,
620 S_IRUGO, acpi_device_dir(device),
621 &acpi_power_fops, acpi_driver_data(device));
622 if (!entry)
623 return -EIO;
624 return 0;
625}
626
627static int acpi_power_remove_fs(struct acpi_device *device)
628{
629
630 if (acpi_device_dir(device)) {
631 remove_proc_entry(ACPI_POWER_FILE_STATUS,
632 acpi_device_dir(device));
633 remove_proc_entry(acpi_device_bid(device), acpi_power_dir);
634 acpi_device_dir(device) = NULL;
635 }
636
637 return 0;
638}
639
640
641
642
643
644static int acpi_power_add(struct acpi_device *device)
645{
646 int result = 0, state;
647 acpi_status status = AE_OK;
648 struct acpi_power_resource *resource = NULL;
649 union acpi_object acpi_object;
650 struct acpi_buffer buffer = { sizeof(acpi_object), &acpi_object };
651
652
653 if (!device)
654 return -EINVAL;
655
656 resource = kzalloc(sizeof(struct acpi_power_resource), GFP_KERNEL);
657 if (!resource)
658 return -ENOMEM;
659
660 resource->device = device;
661 mutex_init(&resource->resource_lock);
662 INIT_LIST_HEAD(&resource->reference);
663 strcpy(resource->name, device->pnp.bus_id);
664 strcpy(acpi_device_name(device), ACPI_POWER_DEVICE_NAME);
665 strcpy(acpi_device_class(device), ACPI_POWER_CLASS);
666 device->driver_data = resource;
667
668
669 status = acpi_evaluate_object(device->handle, NULL, NULL, &buffer);
670 if (ACPI_FAILURE(status)) {
671 result = -ENODEV;
672 goto end;
673 }
674 resource->system_level = acpi_object.power_resource.system_level;
675 resource->order = acpi_object.power_resource.resource_order;
676
677 result = acpi_power_get_state(device->handle, &state);
678 if (result)
679 goto end;
680
681 switch (state) {
682 case ACPI_POWER_RESOURCE_STATE_ON:
683 device->power.state = ACPI_STATE_D0;
684 break;
685 case ACPI_POWER_RESOURCE_STATE_OFF:
686 device->power.state = ACPI_STATE_D3;
687 break;
688 default:
689 device->power.state = ACPI_STATE_UNKNOWN;
690 break;
691 }
692
693 result = acpi_power_add_fs(device);
694 if (result)
695 goto end;
696
697 printk(KERN_INFO PREFIX "%s [%s] (%s)\n", acpi_device_name(device),
698 acpi_device_bid(device), state ? "on" : "off");
699
700 end:
701 if (result)
702 kfree(resource);
703
704 return result;
705}
706
707static int acpi_power_remove(struct acpi_device *device, int type)
708{
709 struct acpi_power_resource *resource = NULL;
710 struct list_head *node, *next;
711
712
713 if (!device || !acpi_driver_data(device))
714 return -EINVAL;
715
716 resource = acpi_driver_data(device);
717
718 acpi_power_remove_fs(device);
719
720 mutex_lock(&resource->resource_lock);
721 list_for_each_safe(node, next, &resource->reference) {
722 struct acpi_power_reference *ref = container_of(node, struct acpi_power_reference, node);
723 list_del(&ref->node);
724 kfree(ref);
725 }
726 mutex_unlock(&resource->resource_lock);
727
728 kfree(resource);
729
730 return 0;
731}
732
733static int acpi_power_resume(struct acpi_device *device)
734{
735 int result = 0, state;
736 struct acpi_power_resource *resource = NULL;
737 struct acpi_power_reference *ref;
738
739 if (!device || !acpi_driver_data(device))
740 return -EINVAL;
741
742 resource = acpi_driver_data(device);
743
744 result = acpi_power_get_state(device->handle, &state);
745 if (result)
746 return result;
747
748 mutex_lock(&resource->resource_lock);
749 if (state == ACPI_POWER_RESOURCE_STATE_OFF &&
750 !list_empty(&resource->reference)) {
751 ref = container_of(resource->reference.next, struct acpi_power_reference, node);
752 mutex_unlock(&resource->resource_lock);
753 result = acpi_power_on(device->handle, ref->device);
754 return result;
755 }
756
757 mutex_unlock(&resource->resource_lock);
758 return 0;
759}
760
761int __init acpi_power_init(void)
762{
763 int result = 0;
764
765 INIT_LIST_HEAD(&acpi_power_resource_list);
766
767 acpi_power_dir = proc_mkdir(ACPI_POWER_CLASS, acpi_root_dir);
768 if (!acpi_power_dir)
769 return -ENODEV;
770
771 result = acpi_bus_register_driver(&acpi_power_driver);
772 if (result < 0) {
773 remove_proc_entry(ACPI_POWER_CLASS, acpi_root_dir);
774 return -ENODEV;
775 }
776
777 return 0;
778}
779