summaryrefslogtreecommitdiffstats
path: root/ppgpio.c
blob: ebf4fe5150011a40be185a7d1127a652291ecd7e (plain)
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
400
401
402
403
404
405
406
407
/*
 * Parport GPIO control
 *
 * Copyright (c) 2009 Michael Buesch <m@bues.ch>
 *
 * Licensed under the GNU/GPL version 2 or later.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/ppdev.h>
#include <linux/parport.h>


#define ARRAY_SIZE(a)	(sizeof(a) / sizeof((a)[0]))

enum gpio_type {
	GPIO_PPDATA,
	GPIO_PPCONTROL,
	GPIO_PPSTATUS,
};

enum gpio_direction {
	GPIO_OUT,
	GPIO_IN,
};

struct gpio {
	enum gpio_type type;
	enum gpio_direction dir;
	unsigned int bit;
	int default_state;
};

/* Mask of hardware inverters. */
static const unsigned char ppcontrol_hwinvert_mask = 0x0B;
static const unsigned char ppstatus_hwinvert_mask = 0x80;

static const struct gpio gpio_table[] = {
/* Data outputs */
	{ /* GPIO 0 = Pin 2 */
		.type		= GPIO_PPDATA,
		.dir		= GPIO_OUT,
		.bit		= 0,
	},
	{ /* GPIO 1 = Pin 3 */
		.type		= GPIO_PPDATA,
		.dir		= GPIO_OUT,
		.bit		= 1,
	},
	{ /* GPIO 2 = Pin 4 */
		.type		= GPIO_PPDATA,
		.dir		= GPIO_OUT,
		.bit		= 2,
	},
	{ /* GPIO 3 = Pin 5 */
		.type		= GPIO_PPDATA,
		.dir		= GPIO_OUT,
		.bit		= 3,
	},
	{ /* GPIO 4 = Pin 6 */
		.type		= GPIO_PPDATA,
		.dir		= GPIO_OUT,
		.bit		= 4,
	},
	{ /* GPIO 5 = Pin 7 */
		.type		= GPIO_PPDATA,
		.dir		= GPIO_OUT,
		.bit		= 5,
	},
	{ /* GPIO 6 = Pin 8 */
		.type		= GPIO_PPDATA,
		.dir		= GPIO_OUT,
		.bit		= 6,
	},
	{ /* GPIO 7 = Pin 9 */
		.type		= GPIO_PPDATA,
		.dir		= GPIO_OUT,
		.bit		= 7,
	},
/* Control outputs */
	{ /* GPIO 8 = Pin 1 */
		.type		= GPIO_PPCONTROL,
		.dir		= GPIO_OUT,
		.bit		= 0,
	},
	{ /* GPIO 9 = Pin 14 */
		.type		= GPIO_PPCONTROL,
		.dir		= GPIO_OUT,
		.bit		= 1,
	},
	{ /* GPIO 10 = Pin 16 */
		.type		= GPIO_PPCONTROL,
		.dir		= GPIO_OUT,
		.bit		= 2,
	},
	{ /* GPIO 11 = Pin 17 */
		.type		= GPIO_PPCONTROL,
		.dir		= GPIO_OUT,
		.bit		= 3,
	},
/* Inputs */
	{ /* GPIO 12 = Pin 15 */
		.type		= GPIO_PPSTATUS,
		.dir		= GPIO_IN,
		.bit		= 3,
	},
	{ /* GPIO 13 = Pin 13 */
		.type		= GPIO_PPSTATUS,
		.dir		= GPIO_IN,
		.bit		= 4,
	},
	{ /* GPIO 14 = Pin 12 */
		.type		= GPIO_PPSTATUS,
		.dir		= GPIO_IN,
		.bit		= 5,
	},
	{ /* GPIO 15 = Pin 10 */
		.type		= GPIO_PPSTATUS,
		.dir		= GPIO_IN,
		.bit		= 6,
	},
	{ /* GPIO 16 = Pin 11 */
		.type		= GPIO_PPSTATUS,
		.dir		= GPIO_IN,
		.bit		= 7,
	},
};

static int gpio_set(int fd, const struct gpio *gpio, int state)
{
	unsigned char data;
	int err;
	struct ppdev_frob_struct control_frob;

	if (gpio->dir != GPIO_OUT) {
		fprintf(stderr, "GPIO is not an output pin\n");
		return -1;
	}

	switch (gpio->type) {
	case GPIO_PPDATA:
		err = ioctl(fd, PPRDATA, &data);
		if (err) {
			fprintf(stderr, "Failed to read parport DATA: %s\n",
				strerror(errno));
			return -1;
		}

		data &= ~(1 << gpio->bit);
		if (state)
			data |= (1 << gpio->bit);

		err = ioctl(fd, PPWDATA, &data);
		if (err) {
			fprintf(stderr, "Failed to write parport DATA: %s\n",
				strerror(errno));
			return -1;
		}
		break;
	case GPIO_PPCONTROL:
		control_frob.mask = (1 << gpio->bit);
		control_frob.val = 0;
		if (state)
			control_frob.val = (1 << gpio->bit);

		control_frob.val ^= ppcontrol_hwinvert_mask;
		control_frob.val &= control_frob.mask;

		err = ioctl(fd, PPFCONTROL, &control_frob);
		if (err) {
			fprintf(stderr, "Failed to frob parport CONTROL: %s\n",
				strerror(errno));
			return -1;
		}
		break;
	case GPIO_PPSTATUS:
		fprintf(stderr, "gpio_set: Cannot write status port.\n");
		return -1;
	}

	return 0;
}

static int gpio_get(int fd, const struct gpio *gpio)
{
	int err;
	unsigned char d;

	switch (gpio->type) {
	case GPIO_PPDATA:
		err = ioctl(fd, PPRDATA, &d);
		if (err) {
			fprintf(stderr, "Failed to read parport DATA: %s\n",
				strerror(errno));
			return -1;
		}
		break;
	case GPIO_PPCONTROL:
		err = ioctl(fd, PPRCONTROL, &d);
		if (err) {
			fprintf(stderr, "Failed to read parport CONTROL: %s\n",
				strerror(errno));
			return -1;
		}
		d ^= ppcontrol_hwinvert_mask;
		break;
	case GPIO_PPSTATUS:
		err = ioctl(fd, PPRSTATUS, &d);
		if (err) {
			fprintf(stderr, "Failed to read parport STATUS: %s\n",
				strerror(errno));
			return -1;
		}
		d ^= ppstatus_hwinvert_mask;
		break;
	}

	return !!(d & (1 << gpio->bit));
}

static int do_setup(int fd)
{
	const struct gpio *gpio;
	int dir, mode, err;
	unsigned int i;

	err = ioctl(fd, PPCLAIM);
	if (err) {
		fprintf(stderr, "Failed to claim parport device: %s\n",
			strerror(errno));
		return -1;
	}

	mode = IEEE1284_MODE_COMPAT;
	err = ioctl(fd, PPSETMODE, &mode);
	if (err) {
		fprintf(stderr, "Failed to set parport mode: %s\n",
			strerror(errno));
		goto out;
	}

	dir = 0;
	err = ioctl(fd, PPDATADIR, &dir);
	if (err) {
		fprintf(stderr, "Failed to turn on DATA output: %s\n",
			strerror(errno));
		goto out;
	}

	err = 0;
	for (i = 0; i < ARRAY_SIZE(gpio_table); i++) {
		gpio = &(gpio_table[i]);
		if (gpio->dir == GPIO_OUT) {
			err = gpio_set(fd, gpio, gpio->default_state);
			if (err)
				break;
		}
	}
out:
	ioctl(fd, PPRELEASE);

	return err;
}

static int do_change_gpio_state(int fd, unsigned int gpionr, int newstate)
{
	const struct gpio *gpio;
	int err;

	if (gpionr >= ARRAY_SIZE(gpio_table)) {
		fprintf(stderr, "GPIO number is too big\n");
		return -1;
	}
	gpio = &(gpio_table[gpionr]);

	err = ioctl(fd, PPCLAIM);
	if (err) {
		fprintf(stderr, "Failed to claim parport device: %s\n",
			strerror(errno));
		return -1;
	}
	err = gpio_set(fd, gpio, newstate);
	ioctl(fd, PPRELEASE);

	return err;
}

static int do_get_gpio_state(int fd, unsigned int gpionr)
{
	const struct gpio *gpio;
	int err, res;

	if (gpionr >= ARRAY_SIZE(gpio_table)) {
		fprintf(stderr, "GPIO number is too big\n");
		return -1;
	}
	gpio = &(gpio_table[gpionr]);

	err = ioctl(fd, PPCLAIM);
	if (err) {
		fprintf(stderr, "Failed to claim parport device: %s\n",
			strerror(errno));
		return -1;
	}
	res = gpio_get(fd, gpio);
	ioctl(fd, PPRELEASE);

	return res;
}

static int str2bool(const char *str)
{
	int res = -1;

	if ((strcasecmp(str, "1") == 0) ||
	    (strcasecmp(str, "on") == 0) ||
	    (strcasecmp(str, "yes") == 0) ||
	    (strcasecmp(str, "true") == 0))
		res = 1;
	else if ((strcasecmp(str, "0") == 0) ||
		 (strcasecmp(str, "off") == 0) ||
		 (strcasecmp(str, "no") == 0) ||
		 (strcasecmp(str, "false") == 0))
		res = 0;

	return res;
}

static void usage(int argc, char **argv)
{
	printf("Usage: %s /dev/parport0 GPIONR on/off\n", argv[0]);
	printf("       %s /dev/parport0 GPIONR get\n", argv[0]);
	printf("       %s /dev/parport0 --setup\n", argv[0]);
}

#define ERRCODE_FAIL	0xFF

int main(int argc, char **argv)
{
	const char *ppdev, *gpiostr, *statestr;
	unsigned int gpionr;
	int err, res, state = 0, fd;
	int setup = 0, get = 0;

	if ((argc != 4) && (argc != 3)) {
		usage(argc, argv);
		return ERRCODE_FAIL;
	}
	if (argc == 4) {
		ppdev = argv[1];
		gpiostr = argv[2];
		statestr = argv[3];

		if (sscanf(gpiostr, "%u", &gpionr) != 1) {
			fprintf(stderr, "Invalid GPIO number.\n");
			return ERRCODE_FAIL;
		}
		if (strcasecmp(statestr, "get") == 0) {
			get = 1;
		} else {
			state = str2bool(statestr);
			if (state == -1) {
				fprintf(stderr, "Invalid boolean state value.\n");
				return ERRCODE_FAIL;
			}
		}
	} else {
		ppdev = argv[1];
		if (strcasecmp(argv[2], "--setup") != 0) {
			usage(argc, argv);
			return ERRCODE_FAIL;
		}
		setup = 1;
	}

	fd = open(ppdev, O_RDWR);
	if (fd == -1) {
		fprintf(stderr, "Failed to open %s: %s\n",
			ppdev, strerror(errno));
		return ERRCODE_FAIL;
	}

	res = 0;
	err = 0;
	if (setup) {
		err = do_setup(fd);
	} else {
		if (get) {
			res = do_get_gpio_state(fd, gpionr);
			if (res < 0)
				err = -1;
			else
				printf("%d\n", res);
		} else
			err = do_change_gpio_state(fd, gpionr, state);
	}
	close(fd);

	return err ? ERRCODE_FAIL : res;
}
bues.ch cgit interface