1 #include <unistd.h>
2 #include <stdio.h>
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <limits.h>
6 #include <string.h>
7 #include <ctype.h>
8 
9 #include <getopt.h>
10 
11 #include "cpufreq.h"
12 #include "helpers/helpers.h"
13 #include "helpers/sysfs.h"
14 
15 static struct option info_opts[] = {
16 	{ .name = "disable",	.has_arg = required_argument,	.flag = NULL,	.val = 'd'},
17 	{ .name = "enable",	.has_arg = required_argument,	.flag = NULL,	.val = 'e'},
18 	{ },
19 };
20 
21 
22 int cmd_idle_set(int argc, char **argv)
23 {
24 	extern char *optarg;
25 	extern int optind, opterr, optopt;
26 	int ret = 0, cont = 1, param = 0, idlestate = 0;
27 	unsigned int cpu = 0;
28 
29 	do {
30 		ret = getopt_long(argc, argv, "d:e:", info_opts, NULL);
31 		if (ret == -1)
32 			break;
33 		switch (ret) {
34 		case '?':
35 			param = '?';
36 			cont = 0;
37 			break;
38 		case 'd':
39 			if (param) {
40 				param = -1;
41 				cont = 0;
42 				break;
43 			}
44 			param = ret;
45 			idlestate = atoi(optarg);
46 			break;
47 		case 'e':
48 			if (param) {
49 				param = -1;
50 				cont = 0;
51 				break;
52 			}
53 			param = ret;
54 			idlestate = atoi(optarg);
55 			break;
56 		case -1:
57 			cont = 0;
58 			break;
59 		}
60 	} while (cont);
61 
62 	switch (param) {
63 	case -1:
64 		printf(_("You can't specify more than one "
65 			 "output-specific argument\n"));
66 		exit(EXIT_FAILURE);
67 	case '?':
68 		printf(_("invalid or unknown argument\n"));
69 		exit(EXIT_FAILURE);
70 	}
71 
72 	/* Default is: set all CPUs */
73 	if (bitmask_isallclear(cpus_chosen))
74 		bitmask_setall(cpus_chosen);
75 
76 	for (cpu = bitmask_first(cpus_chosen);
77 	     cpu <= bitmask_last(cpus_chosen); cpu++) {
78 
79 		if (!bitmask_isbitset(cpus_chosen, cpu))
80 			continue;
81 
82 		switch (param) {
83 
84 		case 'd':
85 			ret = sysfs_idlestate_disable(cpu, idlestate, 1);
86 			if (ret == 0)
87 		printf(_("Idlestate %u disabled on CPU %u\n"),  idlestate, cpu);
88 			else if (ret == -1)
89 		printf(_("Idlestate %u not available on CPU %u\n"),
90 		       idlestate, cpu);
91 			else if (ret == -2)
92 		printf(_("Idlestate disabling not supported by kernel\n"));
93 			else
94 		printf(_("Idlestate %u not disabled on CPU %u\n"),
95 		       idlestate, cpu);
96 			break;
97 		case 'e':
98 			ret = sysfs_idlestate_disable(cpu, idlestate, 0);
99 			if (ret == 0)
100 		printf(_("Idlestate %u enabled on CPU %u\n"),  idlestate, cpu);
101 			else if (ret == -1)
102 		printf(_("Idlestate %u not available on CPU %u\n"),
103 		       idlestate, cpu);
104 			else if (ret == -2)
105 		printf(_("Idlestate enabling not supported by kernel\n"));
106 			else
107 		printf(_("Idlestate %u not enabled on CPU %u\n"),
108 		       idlestate, cpu);
109 			break;
110 		default:
111 			/* Not reachable with proper args checking */
112 			printf(_("Invalid or unknown argument\n"));
113 			exit(EXIT_FAILURE);
114 			break;
115 		}
116 	}
117 	return EXIT_SUCCESS;
118 }
119