2 * carlu - userspace testing utility for ar9170 devices
4 * Abstraction Layer for FW/HW command interface
6 * Copyright 2009-2011 Christian Lamparter <chunkeey@googlemail.com>
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.
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.
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.
34 #include <sys/types.h>
45 int carlu_cmd_echo(struct carlu *ar, const uint32_t message)
50 ret = carlusb_cmd(ar, CARL9170_CMD_ECHO,
51 (uint8_t *)&message, sizeof(message),
52 (uint8_t *)&_message, sizeof(_message));
55 ret = (message == _message) ? 0 : -EIO;
60 struct carl9170_cmd *carlu_cmd_buf(struct carlu *ar,
61 const enum carl9170_cmd_oids cmd, const unsigned int len)
63 struct carl9170_cmd *tmp;
65 if (len % 4 || (sizeof(struct carl9170_cmd_head) + len > 64))
66 return ERR_PTR(-EINVAL);
68 tmp = malloc(sizeof(struct carl9170_cmd_head) + len);
76 int carlu_cmd_reboot(struct carlu *ar)
78 struct carl9170_cmd *reboot;
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;
86 err = carlusb_cmd_async(ar, reboot, true);
90 int carlu_cmd_mem_dump(struct carlu *ar, const uint32_t start,
91 const unsigned int len, void *_buf)
93 #define RW 8 /* number of words to read at once */
94 #define RB (sizeof(uint32_t) * RW)
96 unsigned int i, j, block;
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);
105 err = carlusb_cmd(ar, CARL9170_CMD_RREG,
106 (void *) &offsets, block * sizeof(uint32_t),
107 (void *) buf + RB * i, RB);
119 int carlu_cmd_mem_watch(struct carlu *ar, const uint32_t mem,
120 const unsigned int len, void *_buf)
122 #define RW 8 /* number of words to read at once */
123 #define RB (sizeof(uint32_t) * RW)
125 unsigned int i, j, block;
129 for (i = 0; i < (len + RB - 1) / RB; i++) {
130 block = min_t(unsigned int, (len - RB * i) / sizeof(uint32_t), RW);
131 for (j = 0; j < block; j++)
132 offsets[j] = cpu_to_le32(mem);
134 err = carlusb_cmd(ar, CARL9170_CMD_RREG,
135 (void *) &offsets, block * sizeof(uint32_t),
136 (void *) buf + RB * i, RB);
148 int carlu_cmd_write_mem(struct carlu *ar, const uint32_t addr,
152 __le32 msg, block[2] = { cpu_to_le32(addr), cpu_to_le32(val) };
154 err = carlusb_cmd(ar, CARL9170_CMD_WREG,
155 (void *) &block, sizeof(block),
156 (void *) &msg, sizeof(msg));
160 int carlu_cmd_read_mem(struct carlu *ar, const uint32_t _addr,
164 __le32 msg, addr = { cpu_to_le32(_addr) };
165 err = carlusb_cmd(ar, CARL9170_CMD_RREG, (void *) &addr, sizeof(addr),
166 (void *) &msg, sizeof(msg));
168 *val = le32_to_cpu(msg);
172 int carlu_cmd_read_eeprom(struct carlu *ar)
177 err = carlu_cmd_mem_dump(ar, AR9170_EEPROM_START, sizeof(ar->eeprom),
181 /* don't want to handle trailing remains */
182 BUILD_BUG_ON(sizeof(ar->eeprom) % 8);
185 if (ar->eeprom.length == cpu_to_le16(0xffff))