Skip to content

Commit 3f2e265

Browse files
KProfiles: Upstream to v5.0.0
* 'main' of https://github.com/dakkshesh07/kprofiles: kp: version: Bump to 5.0.0 kp: kp_set_mode: Avoid unnecessary checks for arguments kp: kp_set_mode(): Handle invalid mode arguments kp: reformat with clang-format kp: Make (kp_mode > 3) statement as unlikely kp: Make kp_mode an unsigned int type variable README: Document about Mi DRM notifier support kp: Add auto kprofiles support for MI DRM Notifier kp: Simplify notifier callback for MSM_DRM and FB notifiers Kprofiles: Cleanup and consolidate kp: version: bump to 4.0.2 kp: make auto_kprofiles __read_mostly README: Add a missing slash kp: make default mode number configurable via defconfig kp: Kconfig: Re-indent and rephrase some bits README: small typo fix-up
2 parents 4784c39 + 0671422 commit 3f2e265

4 files changed

Lines changed: 128 additions & 78 deletions

File tree

drivers/misc/kprofiles/Kconfig

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,29 @@ config KPROFILES
66
tristate "Kprofiles Kernel Module"
77
default y
88
help
9-
This allows you to regulate in-kernel activities in a profile-oriented manner.
10-
it creates a sysfs node which can be used to change profiles via userspace,
11-
It provides 4 profile modes, (Disabled, Battery, Balanced, Performance)
9+
This allows you to regulate in-kernel activities in a profile-oriented manner.
10+
It creates a sysfs node which can be used to change profiles via userspace.
11+
It provides 4 profile modes: Disabled, Battery, Balanced and Performance.
1212

13-
The former can be used to disable/enable or tune kernel activities
14-
according to a profile mode. Check the README.md for more info.
13+
The former can be used to disable/enable or tune kernel activities according
14+
to a profile mode. Check the README.md for more information.
1515

16-
It's safe to say Y here, as the driver isn't very demanding
17-
and is only used when developer uses the exported API functions
18-
in an another driver.
16+
It's safe to say Y here, as the driver isn't very demanding and is only
17+
used when developer uses the exported API functions in an another driver.
1918

2019
if KPROFILES
2120

21+
config DEFAULT_KP_MODE
22+
int "Default mode number for kp_mode"
23+
range 0 3
24+
default 0
25+
help
26+
Kprofiles will activate this mode during boot-up.
27+
2228
config AUTO_KPROFILES
2329
bool
2430
help
25-
Selected if either of the Auto Kprofiles option is enabled.
31+
This option is selected if one of the Auto Kprofiles options is enabled.
2632

2733
choice
2834
prompt "Auto Kprofiles support"
@@ -33,25 +39,35 @@ config AUTO_KPROFILES_MSM_DRM
3339
depends on DRM_MSM
3440
select AUTO_KPROFILES
3541
help
36-
Select this to enable Kprofile's automatic mode changer via msm_drm_notifier.
37-
When this option is enabled Kprofiles will automatically switch to
38-
battery mode when device screen turns off and switch back
39-
to previously active mode when the device wakes up.
42+
Select this to enable Kprofile's automatic mode changer via msm_drm_notifier.
43+
When this option is enabled, Kprofiles will automatically switch to battery
44+
mode when device screen turns off and will switch back to previously active
45+
mode when the device wakes up.
46+
47+
config AUTO_KPROFILES_MI_DRM
48+
bool "Auto Kprofiles using mi_drm_notifier"
49+
depends on !DRM_MSM
50+
select AUTO_KPROFILES
51+
help
52+
Select this to enable Kprofile's automatic mode changer via mi_drm_notifier.
53+
When this option is enabled, Kprofiles will automatically switch to battery
54+
mode when device screen turns off and will switch back to previously active
55+
mode when the device wakes up.
4056

4157
config AUTO_KPROFILES_FB
4258
bool "Auto Kprofiles using fb_notifier"
4359
depends on FB
4460
select AUTO_KPROFILES
4561
help
46-
Select this to enable Kprofile's automatic mode changer via fb_notifier.
47-
When this option is enabled Kprofiles will automatically switch to
48-
battery mode when device screen turns off and switch back
49-
to previously active mode when the device wakes up.
62+
Select this to enable Kprofile's automatic mode changer via fb_notifier.
63+
When this option is enabled, Kprofiles will automatically switch to
64+
battery mode when device screen turns off and will switch back to
65+
previously active mode when the device wakes up.
5066

5167
config AUTO_KPROFILES_NONE
5268
bool "None"
5369
help
54-
Select this to build Kprofiles without auto Kprofiles.
70+
Select this to build Kprofiles without auto Kprofiles.
5571

5672
endchoice
5773

drivers/misc/kprofiles/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Kprofiles is a simple Linux kernel module that can be used to regulate in-kernel
2121
| Balanced | 2 |
2222
| Performance | 3 |
2323

24-
Furthermore, Kprofiles provides automatic profile changer (auto Kprofiles), which uses `FB notifier` or `MSM DRM notifier` to enforce battery profile mode when the device's screen goes off and switches back to previously active mode when the device wakes up. Users can disable or enable this feature at runtime without recompiling the kernel by changing the bool value in `/sys/module/kprofiles/parameters/auto_kprofiles`
24+
Furthermore, Kprofiles provides automatic profile changer (auto Kprofiles), which uses `FB notifier`, `MSM DRM notifier` or `MI DRM notifier` to enforce battery profile mode when the device's screen goes off and switches back to previously active mode when the device wakes up. Users can disable or enable this feature at runtime without recompiling the kernel by changing the bool value in `/sys/module/kprofiles/parameters/auto_kprofiles`
2525

2626
Kprofiles additionally has API functions for switching profiles in response to any in-kernel event. For further information, please see the table of [Available APIs](#available-apis)
2727

@@ -46,11 +46,11 @@ source "drivers/misc/echo/Kconfig"
4646
endmenu
4747
```
4848

49-
3. Modify =drivers/misc/Makefile=
49+
3. Modify `drivers/misc/Makefile`
5050

5151
```diff
5252
obj-$(CONFIG_GENWQE) += genwqe/
53-
+obj-$(CONFIG_KPROFILES) += kprofiles
53+
+obj-$(CONFIG_KPROFILES) += kprofiles/
5454
obj-$(CONFIG_ECHO) += echo/
5555
```
5656

drivers/misc/kprofiles/main.c

Lines changed: 90 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,39 @@
88
#include <linux/moduleparam.h>
99
#ifdef CONFIG_AUTO_KPROFILES_MSM_DRM
1010
#include <linux/msm_drm_notify.h>
11+
#elif defined(CONFIG_AUTO_KPROFILES_MI_DRM)
12+
#include <drm/drm_notifier_mi.h>
1113
#elif defined(CONFIG_AUTO_KPROFILES_FB)
1214
#include <linux/fb.h>
1315
#endif
1416
#include "version.h"
1517

16-
static int kp_mode = 1;
17-
module_param(kp_mode, int, 0664);
18+
#ifdef CONFIG_AUTO_KPROFILES_MSM_DRM
19+
#define KP_EVENT_BLANK MSM_DRM_EVENT_BLANK
20+
#define KP_BLANK_POWERDOWN MSM_DRM_BLANK_POWERDOWN
21+
#define KP_BLANK_UNBLANK MSM_DRM_BLANK_UNBLANK
22+
#elif defined(CONFIG_AUTO_KPROFILES_MI_DRM)
23+
#define KP_EVENT_BLANK MI_DRM_EVENT_BLANK
24+
#define KP_BLANK_POWERDOWN MI_DRM_BLANK_POWERDOWN
25+
#define KP_BLANK_UNBLANK MI_DRM_BLANK_UNBLANK
26+
#elif defined(CONFIG_AUTO_KPROFILES_FB)
27+
#define KP_EVENT_BLANK FB_EVENT_BLANK
28+
#define KP_BLANK_POWERDOWN FB_BLANK_POWERDOWN
29+
#define KP_BLANK_UNBLANK FB_BLANK_UNBLANK
30+
#endif
1831

1932
static unsigned int kp_override_mode;
2033
static bool kp_override = false;
21-
22-
static bool auto_kprofiles = true;
23-
module_param(auto_kprofiles, bool, 0664);
24-
2534
#ifdef CONFIG_AUTO_KPROFILES
2635
static bool screen_on = true;
2736
#endif
2837

38+
static bool auto_kprofiles __read_mostly = true;
39+
module_param(auto_kprofiles, bool, 0664);
40+
41+
static unsigned int kp_mode = CONFIG_DEFAULT_KP_MODE;
42+
module_param(kp_mode, int, 0664);
43+
2944
DEFINE_MUTEX(kplock);
3045

3146
/*
@@ -42,13 +57,20 @@ void kp_set_mode_rollback(unsigned int level, unsigned int duration_ms)
4257
return;
4358
#endif
4459

45-
mutex_lock(&kplock);
46-
if (level && duration_ms && auto_kprofiles) {
47-
kp_override_mode = level;
48-
kp_override = true;
49-
msleep(duration_ms);
50-
kp_override = false;
60+
if (!auto_kprofiles)
61+
return;
62+
63+
if (unlikely(level > 3)) {
64+
pr_err("%s: Invalid mode requested, Skipping mode change",
65+
__func__);
66+
return;
5167
}
68+
69+
mutex_lock(&kplock);
70+
kp_override_mode = level;
71+
kp_override = true;
72+
msleep(duration_ms);
73+
kp_override = false;
5274
mutex_unlock(&kplock);
5375
}
5476

@@ -67,7 +89,13 @@ void kp_set_mode(unsigned int level)
6789
return;
6890
#endif
6991

70-
if (level && auto_kprofiles)
92+
if (unlikely(level > 3)) {
93+
pr_err("%s: Invalid mode requested, Skipping mode change",
94+
__func__);
95+
return;
96+
}
97+
98+
if (auto_kprofiles)
7199
kp_mode = level;
72100
}
73101

@@ -101,7 +129,7 @@ int kp_active_mode(void)
101129
if (kp_override)
102130
return kp_override_mode;
103131

104-
if (kp_mode > 3) {
132+
if (unlikely(kp_mode > 3)) {
105133
kp_mode = 0;
106134
pr_info("Invalid value passed, falling back to level 0\n");
107135
}
@@ -117,93 +145,99 @@ static inline int kp_notifier_callback(struct notifier_block *self,
117145
{
118146
#ifdef CONFIG_AUTO_KPROFILES_MSM_DRM
119147
struct msm_drm_notifier *evdata = data;
120-
int *blank;
121-
122-
if (event != MSM_DRM_EVENT_BLANK)
123-
goto out;
124-
125-
if (!evdata || !evdata->data || evdata->id != MSM_DRM_PRIMARY_DISPLAY)
126-
goto out;
127-
128-
blank = evdata->data;
129-
switch (*blank) {
130-
case MSM_DRM_BLANK_POWERDOWN:
131-
if (!screen_on)
132-
break;
133-
screen_on = false;
134-
break;
135-
case MSM_DRM_BLANK_UNBLANK:
136-
if (screen_on)
137-
break;
138-
screen_on = true;
139-
break;
140-
}
148+
#elif defined(CONFIG_AUTO_KPROFILES_MI_DRM)
149+
struct mi_drm_notifier *evdata = data;
141150
#elif defined(CONFIG_AUTO_KPROFILES_FB)
142151
struct fb_event *evdata = data;
152+
#endif
143153
int *blank;
144154

145-
if (event != FB_EVENT_BLANK)
146-
goto out;
155+
if (event != KP_EVENT_BLANK
156+
#ifdef CONFIG_AUTO_KPROFILES_MSM_DRM
157+
|| !evdata || !evdata->data || evdata->id != MSM_DRM_PRIMARY_DISPLAY
158+
#endif
159+
)
160+
return NOTIFY_OK;
147161

148162
blank = evdata->data;
149163
switch (*blank) {
150-
case FB_BLANK_POWERDOWN:
164+
case KP_BLANK_POWERDOWN:
151165
if (!screen_on)
152166
break;
153167
screen_on = false;
154168
break;
155-
case FB_BLANK_UNBLANK:
169+
case KP_BLANK_UNBLANK:
156170
if (screen_on)
157171
break;
158172
screen_on = true;
159173
break;
160174
}
161-
#endif
162175

163-
out:
164176
return NOTIFY_OK;
165177
}
166178

167179
static struct notifier_block kp_notifier_block = {
168180
.notifier_call = kp_notifier_callback,
169181
};
170182

171-
#endif
172-
173-
static int __init kp_init(void)
183+
static int kprofiles_register_notifier(void)
174184
{
175185
int ret = 0;
176186

177187
#ifdef CONFIG_AUTO_KPROFILES_MSM_DRM
178188
ret = msm_drm_register_client(&kp_notifier_block);
179-
if (ret) {
180-
pr_err("Failed to register msm_drm notifier, err: %d\n", ret);
181-
msm_drm_unregister_client(&kp_notifier_block);
182-
}
189+
#elif defined(CONFIG_AUTO_KPROFILES_MI_DRM)
190+
ret = mi_drm_register_client(&kp_notifier_block);
183191
#elif defined(CONFIG_AUTO_KPROFILES_FB)
184192
ret = fb_register_client(&kp_notifier_block);
185-
if (ret) {
186-
pr_err("Failed to register fb notifier, err: %d\n", ret);
187-
fb_unregister_client(&kp_notifier_block);
188-
}
189193
#endif
190-
pr_info("Kprofiles " KPROFILES_VERSION
191-
" loaded. Visit https://github.com/dakkshesh07/Kprofiles/blob/main/README.md for information.\n");
192-
pr_info("Copyright (C) 2021-2022 Dakkshesh <dakkshesh5@gmail.com>.\n");
194+
193195
return ret;
194196
}
195197

196-
static void __exit kp_exit(void)
198+
static void kprofiles_unregister_notifier(void)
197199
{
198200
#ifdef CONFIG_AUTO_KPROFILES_MSM_DRM
199201
msm_drm_unregister_client(&kp_notifier_block);
202+
#elif defined(CONFIG_AUTO_KPROFILES_MI_DRM)
203+
mi_drm_unregister_client(&kp_notifier_block);
200204
#elif defined(CONFIG_AUTO_KPROFILES_FB)
201205
fb_unregister_client(&kp_notifier_block);
202206
#endif
203207
}
204208

209+
#else
210+
static inline int kprofiles_register_notifier(void)
211+
{
212+
return 0;
213+
}
214+
static inline void kprofiles_unregister_notifier(void)
215+
{
216+
}
217+
#endif
218+
219+
static int __init kp_init(void)
220+
{
221+
int ret = 0;
222+
223+
ret = kprofiles_register_notifier();
224+
if (ret)
225+
pr_err("Failed to register notifier, err: %d\n", ret);
226+
227+
pr_info("Kprofiles " KPROFILES_VERSION
228+
" loaded. Visit https://github.com/dakkshesh07/Kprofiles/blob/main/README.md for information.\n");
229+
pr_info("Copyright (C) 2021-2022 Dakkshesh <dakkshesh5@gmail.com>.\n");
230+
231+
return ret;
232+
}
205233
module_init(kp_init);
234+
235+
static void __exit kp_exit(void)
236+
{
237+
kprofiles_unregister_notifier();
238+
}
206239
module_exit(kp_exit);
240+
207241
MODULE_LICENSE("GPL v2");
208242
MODULE_DESCRIPTION("KernelSpace Profiles");
209243
MODULE_AUTHOR("Dakkshesh <dakkshesh5@gmail.com>");

drivers/misc/kprofiles/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
*/
55

66
#ifndef KPROFILES_VERSION
7-
#define KPROFILES_VERSION "4.0.1"
7+
#define KPROFILES_VERSION "5.0.0"
88
#endif

0 commit comments

Comments
 (0)