2 * linux/drivers/cpufreq/proc_intf.c
4 * Copyright (C) 2002 - 2003 Dominik Brodowski
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/cpufreq.h>
11 #include <linux/ctype.h>
12 #include <linux/proc_fs.h>
13 #include <asm/uaccess.h>
16 #define CPUFREQ_ALL_CPUS ((NR_CPUS))
19 * cpufreq_parse_policy - parse a policy string
20 * @input_string: the string to parse.
21 * @policy: the policy written inside input_string
23 * This function parses a "policy string" - something the user echo'es into
24 * /proc/cpufreq or gives as boot parameter - into a struct cpufreq_policy.
25 * If there are invalid/missing entries, they are replaced with current
28 static int cpufreq_parse_policy(char input_string[42], struct cpufreq_policy *policy)
33 char str_governor[16];
34 struct cpufreq_policy current_policy;
35 unsigned int result = -EFAULT;
43 policy->cpu = CPUFREQ_ALL_CPUS;
45 if (sscanf(input_string, "%d:%d:%d:%15s", &cpu, &min, &max, str_governor) == 4)
53 if (sscanf(input_string, "%d%%%d%%%d%%%15s", &cpu, &min, &max, str_governor) == 4)
55 if (!cpufreq_get_policy(¤t_policy, cpu)) {
56 policy->min = (min * current_policy.cpuinfo.max_freq) / 100;
57 policy->max = (max * current_policy.cpuinfo.max_freq) / 100;
64 if (sscanf(input_string, "%d:%d:%15s", &min, &max, str_governor) == 3)
72 if (sscanf(input_string, "%d%%%d%%%15s", &min, &max, str_governor) == 3)
74 if (!cpufreq_get_policy(¤t_policy, cpu)) {
75 policy->min = (min * current_policy.cpuinfo.max_freq) / 100;
76 policy->max = (max * current_policy.cpuinfo.max_freq) / 100;
85 result = cpufreq_parse_governor(str_governor, &policy->policy, &policy->governor);
91 * cpufreq_proc_read - read /proc/cpufreq
93 * This function prints out the current cpufreq policy.
95 static int cpufreq_proc_read (
105 struct cpufreq_policy policy;
106 unsigned int min_pctg = 0;
107 unsigned int max_pctg = 0;
113 p += sprintf(p, " minimum CPU frequency - maximum CPU frequency - policy\n");
114 for (i=0;i<NR_CPUS;i++) {
118 if (cpufreq_get_policy(&policy, i))
121 if (!policy.cpuinfo.max_freq)
124 min_pctg = (policy.min * 100) / policy.cpuinfo.max_freq;
125 max_pctg = (policy.max * 100) / policy.cpuinfo.max_freq;
127 p += sprintf(p, "CPU%3d %9d kHz (%3d %%) - %9d kHz (%3d %%) - ",
128 i , policy.min, min_pctg, policy.max, max_pctg);
130 switch (policy.policy) {
131 case CPUFREQ_POLICY_POWERSAVE:
132 p += sprintf(p, "powersave\n");
134 case CPUFREQ_POLICY_PERFORMANCE:
135 p += sprintf(p, "performance\n");
138 p += sprintf(p, "INVALID\n");
142 p += scnprintf(p, CPUFREQ_NAME_LEN, "%s\n", policy.governor->name);
146 if (len <= off+count)
160 * cpufreq_proc_write - handles writing into /proc/cpufreq
162 * This function calls the parsing script and then sets the policy
165 static int cpufreq_proc_write (
167 const char __user *buffer,
172 char proc_string[42] = {'\0'};
173 struct cpufreq_policy policy;
177 if ((count > sizeof(proc_string) - 1))
180 if (copy_from_user(proc_string, buffer, count))
183 proc_string[count] = '\0';
185 result = cpufreq_parse_policy(proc_string, &policy);
189 if (policy.cpu == CPUFREQ_ALL_CPUS)
191 for (i=0; i<NR_CPUS; i++)
195 cpufreq_set_policy(&policy);
199 cpufreq_set_policy(&policy);
206 * cpufreq_proc_init - add "cpufreq" to the /proc root directory
208 * This function adds "cpufreq" to the /proc root directory.
210 static int __init cpufreq_proc_init (void)
212 struct proc_dir_entry *entry = NULL;
214 /* are these acceptable values? */
215 entry = create_proc_entry("cpufreq", S_IFREG|S_IRUGO|S_IWUSR,
219 printk(KERN_ERR "unable to create /proc/cpufreq entry\n");
222 entry->read_proc = cpufreq_proc_read;
223 entry->write_proc = cpufreq_proc_write;
231 * cpufreq_proc_exit - removes "cpufreq" from the /proc root directory.
233 * This function removes "cpufreq" from the /proc root directory.
235 static void __exit cpufreq_proc_exit (void)
237 remove_proc_entry("cpufreq", &proc_root);
241 MODULE_AUTHOR ("Dominik Brodowski <linux@brodo.de>");
242 MODULE_DESCRIPTION ("CPUfreq /proc/cpufreq interface");
243 MODULE_LICENSE ("GPL");
245 module_init(cpufreq_proc_init);
246 module_exit(cpufreq_proc_exit);