2 * Copyright (C) 2010 Michael Buesch <mb@bu3sch.de>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
21 #include <sys/ioctl.h>
27 #include "user_retu_tahvo.h"
30 static const char * chip2str(int chip)
41 static int str2uint(const char *str, unsigned int *value)
45 *value = strtoul(str, &tail, 0);
52 static int open_dev(int chip)
68 fd = open(path, O_RDWR);
70 fprintf(stderr, "Failed to open %d: %s\n",
71 path, strerror(errno));
77 #define MASKREG(mask, reg) ((mask) | (((reg) & 0x3F) << 16))
79 static unsigned int encrapify_value(unsigned int value, unsigned int mask)
91 static unsigned int decrapify_value(unsigned int value, unsigned int mask)
103 static int do_read(int chip, int fd, unsigned int mask, unsigned int reg, unsigned int *value)
105 unsigned int command;
110 command = RETU_IOCH_READ;
113 command = TAHVO_IOCH_READ;
118 res = ioctl(fd, command, MASKREG(mask, reg));
121 *value = decrapify_value(res, mask);
126 static int task_read(int chip, unsigned int reg, unsigned int *value)
133 err = do_read(chip, fd, 0xFFFF, reg, value);
139 static int do_write(int chip, int fd, unsigned int mask, unsigned int reg, unsigned int value)
141 struct retu_tahvo_write_parms p;
142 unsigned int command;
147 command = RETU_IOCX_WRITE;
150 command = TAHVO_IOCX_WRITE;
156 memset(&p, 0, sizeof(p));
157 p.field = MASKREG(mask, reg);
158 p.value = encrapify_value(value, mask);
160 err = ioctl(fd, command, &p);
162 fprintf(stderr, "Write ioctl failed\n");
166 fprintf(stderr, "Failed to write\n");
173 static int task_maskset(int chip, unsigned int reg, unsigned int mask, unsigned int set)
184 err = do_write(chip, fd, mask, reg, set);
190 static int task_write(int chip, unsigned int reg, unsigned int value)
192 return task_maskset(chip, reg, 0xFFFF, value);
195 static void usage(FILE *fd, int argc, char **argv)
197 fprintf(fd, "Usage: %s CHIP TASK REG [VALUE...]\n", argv[0]);
198 fprintf(fd, " CHIP is one of RETU or TAHVO\n");
199 fprintf(fd, " TASK is one of READ, WRITE or MASKSET\n");
200 fprintf(fd, " VALUE are values, depending on TASK\n");
203 int main(int argc, char **argv)
205 const char *chip_str, *task, *reg_str;
207 unsigned int reg, mask, set, value;
210 if (argc != 4 && argc != 5 && argc != 6)
216 if (strcasecmp(chip_str, "retu") == 0)
218 else if (strcasecmp(chip_str, "tahvo") == 0)
223 err = str2uint(reg_str, ®);
227 if (strcasecmp(task, "read") == 0) {
230 err = task_read(chip, reg, &value);
232 fprintf(stderr, "Failed to read %s register 0x%02X\n",
233 chip2str(chip), reg);
236 printf("0x%04X\n", value);
237 } else if (strcasecmp(task, "write") == 0) {
240 err = str2uint(argv[4], &value);
243 err = task_write(chip, reg, value);
245 fprintf(stderr, "Failed to write %s register 0x%02X\n",
246 chip2str(chip), reg);
249 } else if (strcasecmp(task, "maskset") == 0) {
252 err = str2uint(argv[4], &mask);
253 err |= str2uint(argv[5], &set);
256 err = task_maskset(chip, reg, mask, set);
258 fprintf(stderr, "Failed to maskset %s register 0x%02X\n",
259 chip2str(chip), reg);
268 usage(stderr, argc, argv);