File: sysfs.c

package info (click to toggle)
libpwm 1.0~rc2-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,596 kB
  • sloc: sh: 4,443; ansic: 859; makefile: 58
file content (399 lines) | stat: -rw-r--r-- 8,918 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* SPDX-License-Identifier: LGPL-2.1-only */
/* SPDX-FileCopyrightText: 2023-2025 Uwe Kleine-König <u.kleine-koenig@baylibre.com> */

#include "config.h"

#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <pwm.h>

#include "pwm-internal.h"

struct pwm_sysfs {
	struct pwm pwm;
	int dirfd;

	/* cached settings */
	bool enabled;
	uint64_t period;
	uint64_t duty_cycle;
	bool inverted_polarity;

	bool enabled_cache_valid;
	bool period_cache_valid;
	bool duty_cycle_cache_valid;
	bool polarity_cache_valid;
};

struct pwm_chip_sysfs {
	struct pwm_chip chip;
	int dirfd;
	struct pwm_sysfs *pwms[];
};

static void pwm_chip_sysfs_close(struct pwm_chip *chip)
{
	unsigned int i;
	struct pwm_chip_sysfs *chip_sysfs = container_of(chip, struct pwm_chip_sysfs, chip);

	for (i = 0; i < chip->npwm; ++i) {
		if (chip_sysfs->pwms[i]) {
			int fd = openat(chip_sysfs->dirfd, "unexport", O_WRONLY | O_CLOEXEC);

			if (fd >= 0) {
				dprintf(fd, "%u\n", i);
				close(fd);
			}

			close(chip_sysfs->pwms[i]->dirfd);
			free(chip_sysfs->pwms[i]);
		}
	}
	close(chip_sysfs->dirfd);
	free(chip_sysfs);
}

static struct pwm *pwm_chip_sysfs_get_pwm(struct pwm_chip *chip,
					  unsigned int offset)
{
	struct pwm_chip_sysfs *chip_sysfs = container_of(chip, struct pwm_chip_sysfs, chip);
	struct pwm_sysfs *pwm_sysfs;
	struct pwm *pwm;
#if UINT_MAX <= 4294967295U
	/* force a compiler error if buffer gets too small */
	char pwmX[14];
#endif
	int fd;

	if (chip_sysfs->pwms[offset])
		return &chip_sysfs->pwms[offset]->pwm;

	pwm_sysfs = calloc(1, sizeof(*pwm_sysfs));
	if (!pwm_sysfs)
		return NULL;
	pwm = &pwm_sysfs->pwm;

	pwm->chip = chip;

	sprintf(pwmX, "pwm%u", offset);

	pwm_sysfs->dirfd = openat(chip_sysfs->dirfd, pwmX,
				  O_PATH | O_CLOEXEC);
	if (pwm_sysfs->dirfd < 0 && errno == ENOENT) {
		int ret;

		fd = openat(chip_sysfs->dirfd, "export", O_WRONLY | O_CLOEXEC);
		if (fd < 0) {
			free(pwm_sysfs);
			return NULL;
		}

		ret = dprintf(fd, "%d\n", offset);
		if (ret < 0) {
			close(fd);
			free(pwm_sysfs);
			return NULL;
		}

		ret = close(fd);
		if (ret < 0) {
			free(pwm_sysfs);
			return NULL;
		}

		pwm_sysfs->dirfd = openat(chip_sysfs->dirfd, pwmX,
					  O_PATH | O_CLOEXEC);
	}

	if (pwm_sysfs->dirfd < 0) {
		free(pwm_sysfs);
		return NULL;
	}

	chip_sysfs->pwms[offset] = pwm_sysfs;

	pwm_sysfs->enabled_cache_valid = false;
	pwm_sysfs->period_cache_valid = false;
	pwm_sysfs->duty_cycle_cache_valid = false;
	pwm_sysfs->polarity_cache_valid = false;

	return pwm;
}

static ssize_t pwm_chip_sysfs_read_prop(const struct pwm_sysfs *pwm_sysfs,
					char *propname,
					char *buf, size_t count)
{
	int fd;
	va_list ap;
	int ret;
	size_t cntread = 0;

	fd = openat(pwm_sysfs->dirfd, propname, O_RDONLY | O_CLOEXEC);
	if (fd < 0)
		return -1;

	while (cntread < count) {
		ret = read(fd, buf + cntread, count - cntread);
		if (ret < 0) {
			int saved_errno = errno;

			close(fd);

			errno = saved_errno;

			return ret;
		} else if (ret == 0) {
			break;
		}

		cntread += ret;
	}

	ret = close(fd);
	if (ret)
		return ret;

	return cntread;
}

static int pwm_chip_sysfs_write_prop(const struct pwm_sysfs *pwm_sysfs,
					 char *propname,
					 const char *restrict format, ...)
{
	int fd;
	va_list ap;
	int ret;

	fd = openat(pwm_sysfs->dirfd, propname, O_WRONLY | O_CLOEXEC);
	if (fd < 0)
		return -1;

	va_start(ap, format);

	ret = vdprintf(fd, format, ap);

	va_end(ap);

	if (ret < 0) {
		int saved_errno = errno;

		close(fd);

		errno = saved_errno;

		return ret;
	}
	return close(fd);
}

static int pwm_chip_sysfs_set_waveform(struct pwm *pwm,
				       const struct pwm_waveform *wf)
{
	struct pwm_sysfs *pwm_sysfs = container_of(pwm, struct pwm_sysfs, pwm);
	int ret;
	uint64_t duty_cycle;

	/* period_length_ns = 0 is interpreted as disabled */
	if (wf->period_length_ns == 0) {
		if (!pwm_sysfs->enabled) {
			ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "enable", "0\n");
			if (ret)
				return ret;
			pwm_sysfs->enabled = false;
		}

		return 0;
	}

	if (!pwm_sysfs->polarity_cache_valid ||
	    (wf->duty_offset_ns >= wf->period_length_ns - wf->duty_length_ns) != pwm_sysfs->inverted_polarity) {
		if (wf->duty_length_ns == wf->period_length_ns || wf->duty_length_ns == 0) {
			/*
			 * Waveforms with constant inactive output have two
			 * possible representations in sysfs. Either with normal
			 * polarity and duty_cycle = 0, or with inverted
			 * polarity and duty_cycle = period. The analogous
			 * statement is true for constant active waveforms. As
			 * many PWMs only support a single polarity, and also to
			 * minimize sysfs access, keep the current polarity in
			 * this case.
			 */
			if (!pwm_sysfs->polarity_cache_valid) {
				char buf[20];

				ret = pwm_chip_sysfs_read_prop(pwm_sysfs, "polarity", buf, sizeof(buf));
				if (ret < 0)
					return ret;
				pwm_sysfs->inverted_polarity = buf[0] == 'i';
			}
		} else if (wf->duty_offset_ns < wf->period_length_ns - wf->duty_length_ns) {
			ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "polarity", "normal\n");
			if (ret)
				return ret;

			pwm_sysfs->inverted_polarity = false;
		} else {
			ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "polarity", "inversed\n");
			if (ret)
				return ret;

			pwm_sysfs->inverted_polarity = true;
		}

	}
	pwm_sysfs->polarity_cache_valid = true;

	if (pwm_sysfs->inverted_polarity)
		duty_cycle = wf->period_length_ns - wf->duty_length_ns;
	else
		duty_cycle = wf->duty_length_ns;

	/*
	 * Ensure that we never hit duty_cycle > period. As updating period and
	 * duty_cycle cannot be done in a single step write period first if
	 * period increases and write duty_cycle first if period decreases.
	 */
	if (!pwm_sysfs->period_cache_valid || !pwm_sysfs->duty_cycle_cache_valid ||
	    pwm_sysfs->period <= wf->period_length_ns) {
		if (!pwm_sysfs->period_cache_valid ||
		    pwm_sysfs->period != wf->period_length_ns) {
			ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "period",
							"%" PRIu64 "\n", wf->period_length_ns);
			if (ret)
				return ret;
			pwm_sysfs->period = wf->period_length_ns;
		}
		pwm_sysfs->period_cache_valid = true;

		if (!pwm_sysfs->duty_cycle_cache_valid ||
		    pwm_sysfs->duty_cycle != wf->duty_length_ns) {
			ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "duty_cycle",
							"%" PRIu64 "\n", duty_cycle);
			if (ret)
				return ret;
			pwm_sysfs->duty_cycle = duty_cycle;
		}
		pwm_sysfs->duty_cycle_cache_valid = true;
	} else {
		if (pwm_sysfs->duty_cycle != wf->duty_length_ns) {
			ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "duty_cycle",
							"%" PRIu64 "\n", duty_cycle);
			if (ret)
				return ret;
			pwm_sysfs->duty_cycle = duty_cycle;
		}
		pwm_sysfs->duty_cycle_cache_valid = true;

		/*
		 * It's already known that
		 * pwm_sysfs->wf.period_length_ns > wf->period_length_ns, so
		 * no need to check for pwm_sysfs->wf.period_length_ns != wf->perio_lengthd
		 */
		ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "period",
						"%" PRIu64 "\n", wf->period_length_ns);
		if (ret)
			return ret;
		pwm_sysfs->period = wf->period_length_ns;
		pwm_sysfs->period_cache_valid = true;
	}

	if (!pwm_sysfs->enabled_cache_valid || !pwm_sysfs->enabled) {
		ret = pwm_chip_sysfs_write_prop(pwm_sysfs, "enable", "1\n");
		if (ret)
			return ret;
		pwm_sysfs->enabled = true;
	}
	pwm_sysfs->enabled_cache_valid = true;

	return 0;
}

static int pwm_chip_sysfs_get_waveform(struct pwm *pwm, struct pwm_waveform *wf)
{
	(void)pwm; (void)wf;
	errno = EIO;
	return -1;
}

struct pwm_chip *pwm_chip_sysfs_open_by_number(unsigned int num)
{
	struct pwm_chip_sysfs *chip_sysfs;
	struct pwm_chip *chip;
	long npwm;
	int dirfd, fd;
	ssize_t ret;
	char buf[128];

	ret = snprintf(buf, sizeof(buf), "/sys/class/pwm/pwmchip%d", num);
	if (ret < 0)
		return NULL;
	if ((size_t)ret >= sizeof(buf)) {
		/* huh */
		errno = EINVAL;
		return NULL;
	}

	dirfd = open(buf, O_PATH | O_CLOEXEC);
	if (dirfd < 0)
		return NULL;

	fd = openat(dirfd, "npwm", O_RDONLY | O_CLOEXEC);
	if (fd < 0) {
		close(dirfd);
		return NULL;
	}

	ret = read(fd, buf, sizeof(buf));
	close(fd);
	if (ret < 0) {
		close(dirfd);
		return NULL;
	}

	if (ret == 0 || buf[ret - 1] != '\n') {
		close(dirfd);
		errno = EINVAL;
		return NULL;
	}

	errno = 0;
	npwm = strtol(buf, NULL, 10);

	if (errno) {
		close(dirfd);
		return NULL;
	}

	if (npwm < 0 || npwm > 128) {
		close(dirfd);
		errno = EINVAL;
		return NULL;
	}

	chip_sysfs = calloc(1, sizeof(*chip_sysfs) + npwm * sizeof(chip_sysfs->pwms[0]));
	if (!chip_sysfs) {
		close(dirfd);
		return NULL;
	}

	chip = &chip_sysfs->chip;
	chip->close = pwm_chip_sysfs_close;
	chip->get_pwm = pwm_chip_sysfs_get_pwm;
	chip->set_waveform = pwm_chip_sysfs_set_waveform;
	chip->get_waveform = pwm_chip_sysfs_get_waveform;
	chip->npwm = npwm;

	chip_sysfs->dirfd = dirfd;

	return chip;
}