d83c107bdae9353f8395ef975d0e1bcfbea91dfb
[carl9170fw.git] / tools / carlu / src / cmd.c
1 /*
2  * carl9170user - userspace testing utility for ar9170 devices
3  *
4  * Abstraction Layer for FW/HW command interface
5  *
6  * Copyright 2009, 2010 Christian Lamparter <chunkeey@googlemail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include "libusb.h"
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <unistd.h>
37
38 #include "carlu.h"
39 #include "usb.h"
40 #include "debug.h"
41 #include "fwcmd.h"
42 #include "eeprom.h"
43 #include "cmd.h"
44
45 int carlu_cmd_echo(struct carlu *ar, const uint32_t message)
46 {
47         uint32_t _message;
48         int ret;
49
50         ret = carlusb_cmd(ar, CARL9170_CMD_ECHO,
51                              (uint8_t *)&message, sizeof(message),
52                              (uint8_t *)&_message, sizeof(_message));
53
54         if (ret == 0)
55                 ret = (message == _message) ? 0 : -EIO;
56
57         return ret;
58 }
59
60 struct carl9170_cmd *carlu_cmd_buf(struct carlu *ar,
61         const enum carl9170_cmd_oids cmd, const unsigned int len)
62 {
63         struct carl9170_cmd *tmp;
64
65         if (len % 4 || (sizeof(struct carl9170_cmd_head) + len > 64))
66                 return ERR_PTR(-EINVAL);
67
68         tmp = malloc(sizeof(struct carl9170_cmd_head) + len);
69         if (tmp) {
70                 tmp->hdr.cmd = cmd;
71                 tmp->hdr.len = len;
72         }
73         return tmp;
74 }
75
76 int carlu_cmd_reboot(struct carlu *ar)
77 {
78         struct carl9170_cmd *reboot;
79         int err;
80
81         /* sure, we could put the struct on the stack too. */
82         reboot = carlu_cmd_buf(ar, CARL9170_CMD_REBOOT_ASYNC, 0);
83         if (IS_ERR_OR_NULL(reboot))
84                 return reboot ? PTR_ERR(reboot) : -ENOMEM;
85
86         err = carlusb_cmd_async(ar, reboot, true);
87         return err;
88 }
89
90 int carlu_cmd_mem_dump(struct carlu *ar, const uint32_t start,
91                         const unsigned int len, void *_buf)
92 {
93 #define RW      8       /* number of words to read at once */
94 #define RB      (sizeof(uint32_t) * RW)
95         uint8_t *buf = _buf;
96         unsigned int i, j, block;
97         int err;
98         __le32 offsets[RW];
99
100         for (i = 0; i < (len + RB - 1) / RB; i++) {
101                 block = min_t(unsigned int, (len - RB * i) / sizeof(uint32_t), RW);
102                 for (j = 0; j < block; j++)
103                         offsets[j] = cpu_to_le32(start + RB * i + 4 * j);
104
105                 err = carlusb_cmd(ar, CARL9170_CMD_RREG,
106                                     (void *) &offsets, block * sizeof(uint32_t),
107                                     (void *) buf + RB * i, RB);
108
109                 if (err)
110                         return err;
111         }
112
113 #undef RW
114 #undef RB
115
116         return 0;
117 }
118
119 int carlu_cmd_write_mem(struct carlu *ar, const uint32_t addr,
120                         const uint32_t val)
121 {
122         int err;
123         __le32 msg, block[2] = { addr, val };
124
125         err = carlusb_cmd(ar, CARL9170_CMD_WREG,
126                           (void *) &block, sizeof(block),
127                           (void *) &msg, sizeof(msg));
128         return err;
129 }
130
131 int carlu_cmd_read_eeprom(struct carlu *ar)
132 {
133
134         int err;
135
136         err = carlu_cmd_mem_dump(ar, AR9170_EEPROM_START, sizeof(ar->eeprom),
137                                   &ar->eeprom);
138
139 #ifndef __CHECKER__
140         /* don't want to handle trailing remains */
141         BUILD_BUG_ON(sizeof(ar->eeprom) % 8);
142 #endif
143
144         if (ar->eeprom.length == cpu_to_le16(0xffff))
145                 return -ENODATA;
146
147         return 0;
148 }