Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * resume.c - Functions for waking devices up. |
| 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Open Source Development Labs |
| 6 | * |
| 7 | * This file is released under the GPLv2 |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/device.h> |
Linus Torvalds | d02f40e | 2006-06-24 14:32:18 -0700 | [diff] [blame] | 12 | #include <linux/resume-trace.h> |
Adrian Bunk | f67d115 | 2006-01-19 17:30:17 +0100 | [diff] [blame] | 13 | #include "../base.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "power.h" |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * resume_device - Restore state for one device. |
| 19 | * @dev: Device. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | int resume_device(struct device * dev) |
| 24 | { |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 25 | int error = 0; |
| 26 | |
Linus Torvalds | d02f40e | 2006-06-24 14:32:18 -0700 | [diff] [blame] | 27 | TRACE_DEVICE(dev); |
| 28 | TRACE_RESUME(0); |
Dmitry Torokhov | f89cbc3 | 2007-04-03 01:08:40 -0400 | [diff] [blame] | 29 | |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 30 | down(&dev->sem); |
Dmitry Torokhov | f89cbc3 | 2007-04-03 01:08:40 -0400 | [diff] [blame] | 31 | |
Rafael J. Wysocki | 9cddad7 | 2007-06-13 15:53:34 +0200 | [diff] [blame^] | 32 | if (dev->parent && dev->parent->power.power_state.event) { |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 33 | dev_err(dev, "PM: resume from %d, parent %s still %d\n", |
Pavel Machek | ca078ba | 2005-09-03 15:56:57 -0700 | [diff] [blame] | 34 | dev->power.power_state.event, |
Rafael J. Wysocki | 9cddad7 | 2007-06-13 15:53:34 +0200 | [diff] [blame^] | 35 | dev->parent->bus_id, |
| 36 | dev->parent->power.power_state.event); |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 37 | } |
Dmitry Torokhov | f89cbc3 | 2007-04-03 01:08:40 -0400 | [diff] [blame] | 38 | |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 39 | if (dev->bus && dev->bus->resume) { |
| 40 | dev_dbg(dev,"resuming\n"); |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 41 | error = dev->bus->resume(dev); |
David Brownell | 82428b6 | 2005-05-09 08:07:00 -0700 | [diff] [blame] | 42 | } |
Dmitry Torokhov | f89cbc3 | 2007-04-03 01:08:40 -0400 | [diff] [blame] | 43 | |
| 44 | if (!error && dev->type && dev->type->resume) { |
| 45 | dev_dbg(dev,"resuming\n"); |
| 46 | error = dev->type->resume(dev); |
| 47 | } |
| 48 | |
| 49 | if (!error && dev->class && dev->class->resume) { |
Linus Torvalds | 7c8265f | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 50 | dev_dbg(dev,"class resume\n"); |
| 51 | error = dev->class->resume(dev); |
| 52 | } |
Dmitry Torokhov | f89cbc3 | 2007-04-03 01:08:40 -0400 | [diff] [blame] | 53 | |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 54 | up(&dev->sem); |
Dmitry Torokhov | f89cbc3 | 2007-04-03 01:08:40 -0400 | [diff] [blame] | 55 | |
Linus Torvalds | d02f40e | 2006-06-24 14:32:18 -0700 | [diff] [blame] | 56 | TRACE_RESUME(error); |
mochel@digitalimplant.org | af70316 | 2005-03-21 10:41:04 -0800 | [diff] [blame] | 57 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | |
Linus Torvalds | 7c8265f | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 61 | static int resume_device_early(struct device * dev) |
| 62 | { |
| 63 | int error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 7c8265f | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 65 | TRACE_DEVICE(dev); |
| 66 | TRACE_RESUME(0); |
| 67 | if (dev->bus && dev->bus->resume_early) { |
| 68 | dev_dbg(dev,"EARLY resume\n"); |
| 69 | error = dev->bus->resume_early(dev); |
| 70 | } |
| 71 | TRACE_RESUME(error); |
| 72 | return error; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Resume the devices that have either not gone through |
| 77 | * the late suspend, or that did go through it but also |
| 78 | * went through the early resume |
| 79 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | void dpm_resume(void) |
| 81 | { |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 82 | mutex_lock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | while(!list_empty(&dpm_off)) { |
| 84 | struct list_head * entry = dpm_off.next; |
| 85 | struct device * dev = to_device(entry); |
| 86 | |
| 87 | get_device(dev); |
Akinobu Mita | 1bfba4e | 2006-06-26 00:24:40 -0700 | [diff] [blame] | 88 | list_move_tail(entry, &dpm_active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 90 | mutex_unlock(&dpm_list_mtx); |
Pavel Machek | ca078ba | 2005-09-03 15:56:57 -0700 | [diff] [blame] | 91 | if (!dev->power.prev_state.event) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | resume_device(dev); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 93 | mutex_lock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | put_device(dev); |
| 95 | } |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 96 | mutex_unlock(&dpm_list_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * device_resume - Restore state of each device in system. |
| 102 | * |
| 103 | * Walk the dpm_off list, remove each entry, resume the device, |
| 104 | * then add it to the dpm_active list. |
| 105 | */ |
| 106 | |
| 107 | void device_resume(void) |
| 108 | { |
Pavel Machek | bb84c89 | 2006-08-31 22:02:11 -0700 | [diff] [blame] | 109 | might_sleep(); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 110 | mutex_lock(&dpm_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | dpm_resume(); |
Matthias Kaehlcke | 11048dc | 2007-05-23 14:19:41 -0700 | [diff] [blame] | 112 | mutex_unlock(&dpm_mtx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | EXPORT_SYMBOL_GPL(device_resume); |
| 116 | |
| 117 | |
| 118 | /** |
Dmitry Torokhov | 9de72ee | 2006-07-15 00:31:54 -0400 | [diff] [blame] | 119 | * dpm_power_up - Power on some devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | * |
| 121 | * Walk the dpm_off_irq list and power each device up. This |
| 122 | * is used for devices that required they be powered down with |
Dmitry Torokhov | 9de72ee | 2006-07-15 00:31:54 -0400 | [diff] [blame] | 123 | * interrupts disabled. As devices are powered on, they are moved |
| 124 | * to the dpm_active list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * |
| 126 | * Interrupts must be disabled when calling this. |
| 127 | */ |
| 128 | |
| 129 | void dpm_power_up(void) |
| 130 | { |
| 131 | while(!list_empty(&dpm_off_irq)) { |
| 132 | struct list_head * entry = dpm_off_irq.next; |
| 133 | struct device * dev = to_device(entry); |
| 134 | |
Linus Torvalds | 7c8265f | 2006-06-24 14:50:29 -0700 | [diff] [blame] | 135 | list_move_tail(entry, &dpm_off); |
| 136 | resume_device_early(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /** |
Dmitry Torokhov | 9de72ee | 2006-07-15 00:31:54 -0400 | [diff] [blame] | 142 | * device_power_up - Turn on all devices that need special attention. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | * |
| 144 | * Power on system devices then devices that required we shut them down |
| 145 | * with interrupts disabled. |
| 146 | * Called with interrupts disabled. |
| 147 | */ |
| 148 | |
| 149 | void device_power_up(void) |
| 150 | { |
| 151 | sysdev_resume(); |
| 152 | dpm_power_up(); |
| 153 | } |
| 154 | |
| 155 | EXPORT_SYMBOL_GPL(device_power_up); |
| 156 | |
| 157 | |