2 * Copyright 2010-2011 Christian Lamparter <chunkeey@googlemail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation version 2 of the License.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <sys/types.h>
31 static int get_val(char *str, unsigned int *val)
35 err = sscanf(str, "%8x", val);
42 static int get_addr(char *str, unsigned int *val)
46 err = get_val(str, val);
48 fprintf(stderr, "Address 0x%.8x is not a multiple of 4.\n",
58 new_fix_entry(struct carlfw *fw, struct carl9170fw_fix_entry *fix_entry)
60 struct carl9170fw_fix_desc *fix;
63 len = sizeof(*fix) + sizeof(*fix_entry);
68 carl9170fw_fill_desc(&fix->head, (uint8_t *) FIX_MAGIC,
70 CARL9170FW_FIX_DESC_MIN_VER,
71 CARL9170FW_FIX_DESC_CUR_VER);
73 memcpy(&fix->data[0], fix_entry, sizeof(*fix_entry));
75 return carlfw_desc_add_tail(fw, &fix->head);
78 static struct carl9170fw_fix_entry *
79 scan_for_similar_fix(struct carl9170fw_fix_desc *fix, __le32 address)
81 unsigned int i, entries;
83 entries = (le16_to_cpu(fix->head.length) - sizeof(*fix)) /
84 sizeof(struct carl9170fw_fix_entry);
86 for (i = 0; i < entries; i++) {
87 if (address == fix->data[i].address)
95 add_another_fix_entry(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
96 struct carl9170fw_fix_entry *fix_entry)
100 fix = carlfw_desc_mod_len(fw, &fix->head, sizeof(*fix_entry));
101 if (IS_ERR_OR_NULL(fix))
102 return (int) PTR_ERR(fix);
104 entry = (le16_to_cpu(fix->head.length) - sizeof(*fix)) /
105 sizeof(*fix_entry) - 1;
107 memcpy(&fix->data[entry], fix_entry, sizeof(*fix_entry));
112 update_entry(char option, struct carl9170fw_fix_entry *entry,
113 struct carl9170fw_fix_entry *fix)
117 entry->mask = fix->mask;
118 entry->value = fix->value;
122 entry->mask |= fix->mask;
123 entry->value |= fix->value;
127 entry->mask &= fix->mask;
128 entry->value &= fix->value;
132 fprintf(stderr, "Unknown option: '%c'\n", option);
139 static void user_education(void)
141 fprintf(stderr, "Usage:\n");
142 fprintf(stderr, "\teeprom_fix FW-FILE SWITCH [ADDRESS [VALUE MASK]]\n");
144 fprintf(stderr, "\nDescription:\n");
145 fprintf(stderr, "\tThis utility manage a set of overrides which "
146 "commands the driver\n\tto load customized EEPROM' "
147 "data for all specified addresses.\n");
149 fprintf(stderr, "\nParameters:\n");
150 fprintf(stderr, "\t'FW-FILE' = firmware file [basename]\n");
151 fprintf(stderr, "\t'SWITCH' = [=|d|D]\n");
152 fprintf(stderr, "\t | '=' => add/set value for address\n");
153 fprintf(stderr, "\t | 'D' => removes all EEPROM overrides\n");
154 fprintf(stderr, "\t * 'd' => removed override for 'address'\n");
155 fprintf(stderr, "\n\t'ADDRESS' = location of the EEPROM override\n");
156 fprintf(stderr, "\t\t NB: must be a multiple of 4.\n");
157 fprintf(stderr, "\t\t an address map can be found in eeprom.h.\n");
158 fprintf(stderr, "\n\t'VALUE' = replacement value\n");
159 fprintf(stderr, "\t'MASK' = mask for the value placement.\n\n");
165 set_fix(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
166 char __unused option, int __unused argc, char *args[])
168 struct carl9170fw_fix_entry fix_entry, *entry = NULL;
169 unsigned int address, value, mask;
172 err = get_addr(args[3], &address);
176 err = get_val(args[4], &value);
180 err = get_val(args[5], &mask);
184 fix_entry.address = cpu_to_le32(address);
185 fix_entry.value = cpu_to_le32(value);
186 fix_entry.mask = cpu_to_le32(mask);
189 err = new_fix_entry(fw, &fix_entry);
191 entry = scan_for_similar_fix(fix, fix_entry.address);
193 err = update_entry(option, entry, &fix_entry);
195 fprintf(stdout, "Overwrite old entry.\n");
197 err = add_another_fix_entry(fw, fix, &fix_entry);
205 del_fix(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
206 char __unused option, int __unused argc, char *args[])
208 struct carl9170fw_fix_entry *entry = NULL;
209 unsigned int address;
211 unsigned int rem_len;
214 err = get_addr(args[3], &address);
219 entry = scan_for_similar_fix(fix, cpu_to_le32(address));
222 fprintf(stderr, "Entry for 0x%.8x not found\n", address);
226 off = (unsigned long) entry - (unsigned long) fix->data;
227 rem_len = le16_to_cpu(fix->head.length) - off;
231 cont = (unsigned long) entry + sizeof(*entry);
232 memmove(entry, (void *)cont, rem_len);
235 fix = carlfw_desc_mod_len(fw, &fix->head, -sizeof(*entry));
236 err = IS_ERR_OR_NULL(fix);
240 static int del_all(struct carlfw *fw, struct carl9170fw_fix_desc *fix,
241 char __unused option, int __unused argc, char __unused *args[])
246 carlfw_desc_del(fw, &fix->head);
250 static const struct {
253 int (*func)(struct carlfw *, struct carl9170fw_fix_desc *,
255 } programm_function[] = {
263 int main(int argc, char *args[])
265 struct carl9170fw_fix_desc *fix;
266 struct carlfw *fw = NULL;
271 if (argc < 3 || argc > 6) {
276 fw = carlfw_load(args[1]);
277 if (IS_ERR_OR_NULL(fw)) {
279 fprintf(stderr, "Failed to open file \"%s\" (%d).\n",
284 fix = carlfw_find_desc(fw, (uint8_t *)FIX_MAGIC, sizeof(*fix),
285 CARL9170FW_FIX_DESC_CUR_VER);
288 for (i = 0; i < ARRAY_SIZE(programm_function); i++) {
289 if (programm_function[i].option != option)
292 if (argc != programm_function[i].argc) {
297 err = programm_function[i].func(fw, fix, option, argc, args);
303 if (i == ARRAY_SIZE(programm_function)) {
304 fprintf(stderr, "Unknown option: '%c'\n",
309 err = carlfw_store(fw);
311 fprintf(stderr, "Failed to apply changes (%d).\n", err);
322 fprintf(stderr, "%s\n", strerror(err));
325 return err ? EXIT_FAILURE : EXIT_SUCCESS;