Commit 8671bbc1 authored by Rafael J. Wysocki's avatar Rafael J. Wysocki

PM / Sleep: Add more wakeup source initialization routines

The existing wakeup source initialization routines are not
particularly useful for wakeup sources that aren't created by
wakeup_source_create(), because their users have to open code
filling the objects with zeros and setting their names.  For this
reason, introduce routines that can be used for initializing, for
example, static wakeup source objects.
Requested-by: default avatarArve Hjønnevåg <arve@android.com>
Signed-off-by: default avatarRafael J. Wysocki <rjw@sisk.pl>
parent 05b4877f
...@@ -52,6 +52,23 @@ static void pm_wakeup_timer_fn(unsigned long data); ...@@ -52,6 +52,23 @@ static void pm_wakeup_timer_fn(unsigned long data);
static LIST_HEAD(wakeup_sources); static LIST_HEAD(wakeup_sources);
/**
* wakeup_source_prepare - Prepare a new wakeup source for initialization.
* @ws: Wakeup source to prepare.
* @name: Pointer to the name of the new wakeup source.
*
* Callers must ensure that the @name string won't be freed when @ws is still in
* use.
*/
void wakeup_source_prepare(struct wakeup_source *ws, const char *name)
{
if (ws) {
memset(ws, 0, sizeof(*ws));
ws->name = name;
}
}
EXPORT_SYMBOL_GPL(wakeup_source_prepare);
/** /**
* wakeup_source_create - Create a struct wakeup_source object. * wakeup_source_create - Create a struct wakeup_source object.
* @name: Name of the new wakeup source. * @name: Name of the new wakeup source.
...@@ -60,31 +77,44 @@ struct wakeup_source *wakeup_source_create(const char *name) ...@@ -60,31 +77,44 @@ struct wakeup_source *wakeup_source_create(const char *name)
{ {
struct wakeup_source *ws; struct wakeup_source *ws;
ws = kzalloc(sizeof(*ws), GFP_KERNEL); ws = kmalloc(sizeof(*ws), GFP_KERNEL);
if (!ws) if (!ws)
return NULL; return NULL;
if (name) wakeup_source_prepare(ws, name ? kstrdup(name, GFP_KERNEL) : NULL);
ws->name = kstrdup(name, GFP_KERNEL);
return ws; return ws;
} }
EXPORT_SYMBOL_GPL(wakeup_source_create); EXPORT_SYMBOL_GPL(wakeup_source_create);
/** /**
* wakeup_source_destroy - Destroy a struct wakeup_source object. * wakeup_source_drop - Prepare a struct wakeup_source object for destruction.
* @ws: Wakeup source to destroy. * @ws: Wakeup source to prepare for destruction.
* *
* Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never * Callers must ensure that __pm_stay_awake() or __pm_wakeup_event() will never
* be run in parallel with this function for the same wakeup source object. * be run in parallel with this function for the same wakeup source object.
*/ */
void wakeup_source_destroy(struct wakeup_source *ws) void wakeup_source_drop(struct wakeup_source *ws)
{ {
if (!ws) if (!ws)
return; return;
del_timer_sync(&ws->timer); del_timer_sync(&ws->timer);
__pm_relax(ws); __pm_relax(ws);
}
EXPORT_SYMBOL_GPL(wakeup_source_drop);
/**
* wakeup_source_destroy - Destroy a struct wakeup_source object.
* @ws: Wakeup source to destroy.
*
* Use only for wakeup source objects created with wakeup_source_create().
*/
void wakeup_source_destroy(struct wakeup_source *ws)
{
if (!ws)
return;
wakeup_source_drop(ws);
kfree(ws->name); kfree(ws->name);
kfree(ws); kfree(ws);
} }
...@@ -147,8 +177,10 @@ EXPORT_SYMBOL_GPL(wakeup_source_register); ...@@ -147,8 +177,10 @@ EXPORT_SYMBOL_GPL(wakeup_source_register);
*/ */
void wakeup_source_unregister(struct wakeup_source *ws) void wakeup_source_unregister(struct wakeup_source *ws)
{ {
wakeup_source_remove(ws); if (ws) {
wakeup_source_destroy(ws); wakeup_source_remove(ws);
wakeup_source_destroy(ws);
}
} }
EXPORT_SYMBOL_GPL(wakeup_source_unregister); EXPORT_SYMBOL_GPL(wakeup_source_unregister);
......
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
* @active: Status of the wakeup source. * @active: Status of the wakeup source.
*/ */
struct wakeup_source { struct wakeup_source {
char *name; const char *name;
struct list_head entry; struct list_head entry;
spinlock_t lock; spinlock_t lock;
struct timer_list timer; struct timer_list timer;
...@@ -73,7 +73,9 @@ static inline bool device_may_wakeup(struct device *dev) ...@@ -73,7 +73,9 @@ static inline bool device_may_wakeup(struct device *dev)
} }
/* drivers/base/power/wakeup.c */ /* drivers/base/power/wakeup.c */
extern void wakeup_source_prepare(struct wakeup_source *ws, const char *name);
extern struct wakeup_source *wakeup_source_create(const char *name); extern struct wakeup_source *wakeup_source_create(const char *name);
extern void wakeup_source_drop(struct wakeup_source *ws);
extern void wakeup_source_destroy(struct wakeup_source *ws); extern void wakeup_source_destroy(struct wakeup_source *ws);
extern void wakeup_source_add(struct wakeup_source *ws); extern void wakeup_source_add(struct wakeup_source *ws);
extern void wakeup_source_remove(struct wakeup_source *ws); extern void wakeup_source_remove(struct wakeup_source *ws);
...@@ -103,11 +105,16 @@ static inline bool device_can_wakeup(struct device *dev) ...@@ -103,11 +105,16 @@ static inline bool device_can_wakeup(struct device *dev)
return dev->power.can_wakeup; return dev->power.can_wakeup;
} }
static inline void wakeup_source_prepare(struct wakeup_source *ws,
const char *name) {}
static inline struct wakeup_source *wakeup_source_create(const char *name) static inline struct wakeup_source *wakeup_source_create(const char *name)
{ {
return NULL; return NULL;
} }
static inline void wakeup_source_drop(struct wakeup_source *ws) {}
static inline void wakeup_source_destroy(struct wakeup_source *ws) {} static inline void wakeup_source_destroy(struct wakeup_source *ws) {}
static inline void wakeup_source_add(struct wakeup_source *ws) {} static inline void wakeup_source_add(struct wakeup_source *ws) {}
...@@ -165,4 +172,17 @@ static inline void pm_wakeup_event(struct device *dev, unsigned int msec) {} ...@@ -165,4 +172,17 @@ static inline void pm_wakeup_event(struct device *dev, unsigned int msec) {}
#endif /* !CONFIG_PM_SLEEP */ #endif /* !CONFIG_PM_SLEEP */
static inline void wakeup_source_init(struct wakeup_source *ws,
const char *name)
{
wakeup_source_prepare(ws, name);
wakeup_source_add(ws);
}
static inline void wakeup_source_trash(struct wakeup_source *ws)
{
wakeup_source_remove(ws);
wakeup_source_drop(ws);
}
#endif /* _LINUX_PM_WAKEUP_H */ #endif /* _LINUX_PM_WAKEUP_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment