xref: /freebsd/usr.sbin/pwm/pwm.c (revision 6419bb52)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  */
29 
30 #include <sys/types.h>
31 #include <sys/ioctl.h>
32 #include <stdbool.h>
33 #include <sys/capsicum.h>
34 #include <dev/pwm/pwmc.h>
35 
36 #include <err.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <capsicum_helpers.h>
45 
46 #define	PWM_ENABLE	0x0001
47 #define	PWM_DISABLE	0x0002
48 #define	PWM_SHOW_CONFIG	0x0004
49 #define	PWM_PERIOD	0x0008
50 #define	PWM_DUTY	0x0010
51 
52 static char device_name[PATH_MAX] = "/dev/pwm/pwmc0.0";
53 
54 static void
55 set_device_name(const char *name)
56 {
57 
58 	if (name[0] == '/')
59 		strlcpy(device_name, name, sizeof(device_name));
60 	else
61 		snprintf(device_name, sizeof(device_name), "/dev/pwm/%s", name);
62 }
63 
64 static void
65 usage(void)
66 {
67 	fprintf(stderr, "Usage:\n");
68 	fprintf(stderr, "\tpwm [-f dev] -C\n");
69 	fprintf(stderr, "\tpwm [-f dev] [-D | -E] [-p period] [-d duty[%%]]\n");
70 	exit(1);
71 }
72 
73 int
74 main(int argc, char *argv[])
75 {
76 	struct pwm_state state;
77 	int fd;
78 	int period, duty;
79 	int action, ch;
80 	cap_rights_t right_ioctl;
81 	const unsigned long pwm_ioctls[] = {PWMGETSTATE, PWMSETSTATE};
82 	char *percent;
83 	bool setname;
84 
85 	action = 0;
86 	setname = false;
87 	fd = -1;
88 	period = duty = -1;
89 
90 	while ((ch = getopt(argc, argv, "f:EDCp:d:")) != -1) {
91 		switch (ch) {
92 		case 'E':
93 			if (action & (PWM_DISABLE | PWM_SHOW_CONFIG))
94 				usage();
95 			action |= PWM_ENABLE;
96 			break;
97 		case 'D':
98 			if (action & (PWM_ENABLE | PWM_SHOW_CONFIG))
99 				usage();
100 			action |= PWM_DISABLE;
101 			break;
102 		case 'C':
103 			if (action)
104 				usage();
105 			action = PWM_SHOW_CONFIG;
106 			break;
107 		case 'p':
108 			if (action & PWM_SHOW_CONFIG)
109 				usage();
110 			action |= PWM_PERIOD;
111 			period = strtol(optarg, NULL, 10);
112 			break;
113 		case 'd':
114 			if (action & PWM_SHOW_CONFIG)
115 				usage();
116 			action |= PWM_DUTY;
117 			duty = strtol(optarg, &percent, 10);
118 			if (*percent == '%') {
119 				if (duty < 0 || duty > 100) {
120 					fprintf(stderr,
121 					    "Invalid duty percentage\n");
122 					usage();
123 				}
124 			} else if (*percent != '\0')
125 				usage();
126 			break;
127 		case 'f':
128 			setname = true;
129 			set_device_name(optarg);
130 			break;
131 		case '?':
132 			usage();
133 			break;
134 		}
135 	}
136 
137 	if (action == 0)
138 		usage();
139 
140 	if ((fd = open(device_name, O_RDWR)) == -1) {
141 		fprintf(stderr, "pwm: cannot open %s: %s\n",
142 		    device_name, strerror(errno));
143 		if (setname)
144 			exit(1);
145 		else
146 			usage();
147 	}
148 
149 	if (caph_limit_stdio() < 0) {
150 		fprintf(stderr, "can't limit stdio rights");
151 		goto fail;
152 	}
153 	caph_cache_catpages();
154 	cap_rights_init(&right_ioctl, CAP_IOCTL);
155 	if (caph_rights_limit(fd, &right_ioctl) < 0) {
156 		fprintf(stderr, "cap_right_limit() failed\n");
157 		goto fail;
158 	}
159 	if (caph_ioctls_limit(fd, pwm_ioctls, nitems(pwm_ioctls)) < 0) {
160 		fprintf(stderr, "caph_ioctls_limit() failed\n");
161 		goto fail;
162 	}
163 	if (caph_enter() < 0) {
164 		fprintf(stderr, "failed to enter capability mode\n");
165 		goto fail;
166 	}
167 
168 	/* Fill the common args */
169 	if (ioctl(fd, PWMGETSTATE, &state) == -1) {
170 		fprintf(stderr, "Cannot get current state of the pwm controller\n");
171 		goto fail;
172 	}
173 
174 	if (action == PWM_SHOW_CONFIG) {
175 		printf("period: %u\nduty: %u\nenabled:%d\n",
176 		    state.period,
177 		    state.duty,
178 		    state.enable);
179 		goto fail;
180 	} else {
181 		if (action & PWM_ENABLE)
182 			state.enable = true;
183 		if (action & PWM_DISABLE)
184 			state.enable = false;
185 		if (action & PWM_PERIOD)
186 			state.period = period;
187 		if (action & PWM_DUTY) {
188 			if (*percent != '\0')
189 				state.duty = state.period * duty / 100;
190 			else
191 				state.duty = duty;
192 		}
193 
194 		if (ioctl(fd, PWMSETSTATE, &state) == -1) {
195 			fprintf(stderr,
196 			  "Cannot configure the pwm controller\n");
197 			goto fail;
198 		}
199 	}
200 
201 	close(fd);
202 	return (0);
203 
204 fail:
205 	close(fd);
206 	return (1);
207 }
208