regmap: debugfs: Ensure a correct return value for empty caches
[firefly-linux-kernel-4.4.55.git] / drivers / base / regmap / regmap-debugfs.c
1 /*
2  * Register map access API - debugfs
3  *
4  * Copyright 2011 Wolfson Microelectronics plc
5  *
6  * Author: Mark Brown <broonie@opensource.wolfsonmicro.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 version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/slab.h>
14 #include <linux/mutex.h>
15 #include <linux/debugfs.h>
16 #include <linux/uaccess.h>
17 #include <linux/device.h>
18
19 #include "internal.h"
20
21 static struct dentry *regmap_debugfs_root;
22
23 /* Calculate the length of a fixed format  */
24 static size_t regmap_calc_reg_len(int max_val, char *buf, size_t buf_size)
25 {
26         snprintf(buf, buf_size, "%x", max_val);
27         return strlen(buf);
28 }
29
30 static ssize_t regmap_name_read_file(struct file *file,
31                                      char __user *user_buf, size_t count,
32                                      loff_t *ppos)
33 {
34         struct regmap *map = file->private_data;
35         int ret;
36         char *buf;
37
38         buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
39         if (!buf)
40                 return -ENOMEM;
41
42         ret = snprintf(buf, PAGE_SIZE, "%s\n", map->dev->driver->name);
43         if (ret < 0) {
44                 kfree(buf);
45                 return ret;
46         }
47
48         ret = simple_read_from_buffer(user_buf, count, ppos, buf, ret);
49         kfree(buf);
50         return ret;
51 }
52
53 static const struct file_operations regmap_name_fops = {
54         .open = simple_open,
55         .read = regmap_name_read_file,
56         .llseek = default_llseek,
57 };
58
59 static void regmap_debugfs_free_dump_cache(struct regmap *map)
60 {
61         struct regmap_debugfs_off_cache *c;
62
63         while (!list_empty(&map->debugfs_off_cache)) {
64                 c = list_first_entry(&map->debugfs_off_cache,
65                                      struct regmap_debugfs_off_cache,
66                                      list);
67                 list_del(&c->list);
68                 kfree(c);
69         }
70 }
71
72 /*
73  * Work out where the start offset maps into register numbers, bearing
74  * in mind that we suppress hidden registers.
75  */
76 static unsigned int regmap_debugfs_get_dump_start(struct regmap *map,
77                                                   unsigned int base,
78                                                   loff_t from,
79                                                   loff_t *pos)
80 {
81         struct regmap_debugfs_off_cache *c = NULL;
82         loff_t p = 0;
83         unsigned int i, ret;
84
85         /*
86          * If we don't have a cache build one so we don't have to do a
87          * linear scan each time.
88          */
89         if (list_empty(&map->debugfs_off_cache)) {
90                 for (i = base; i <= map->max_register; i += map->reg_stride) {
91                         /* Skip unprinted registers, closing off cache entry */
92                         if (!regmap_readable(map, i) ||
93                             regmap_precious(map, i)) {
94                                 if (c) {
95                                         c->max = p - 1;
96                                         list_add_tail(&c->list,
97                                                       &map->debugfs_off_cache);
98                                         c = NULL;
99                                 }
100
101                                 continue;
102                         }
103
104                         /* No cache entry?  Start a new one */
105                         if (!c) {
106                                 c = kzalloc(sizeof(*c), GFP_KERNEL);
107                                 if (!c) {
108                                         regmap_debugfs_free_dump_cache(map);
109                                         return base;
110                                 }
111                                 c->min = p;
112                                 c->base_reg = i;
113                         }
114
115                         p += map->debugfs_tot_len;
116                 }
117         }
118
119         /*
120          * This should never happen; we return above if we fail to
121          * allocate and we should never be in this code if there are
122          * no registers at all.
123          */
124         if (list_empty(&map->debugfs_off_cache)) {
125                 WARN_ON(list_empty(&map->debugfs_off_cache));
126                 return base;
127         }
128
129         /* Find the relevant block */
130         list_for_each_entry(c, &map->debugfs_off_cache, list) {
131                 if (from >= c->min && from <= c->max) {
132                         *pos = c->min;
133                         return c->base_reg;
134                 }
135
136                 *pos = c->min;
137                 ret = c->base_reg;
138         }
139
140         return ret;
141 }
142
143 static ssize_t regmap_read_debugfs(struct regmap *map, unsigned int from,
144                                    unsigned int to, char __user *user_buf,
145                                    size_t count, loff_t *ppos)
146 {
147         size_t buf_pos = 0;
148         loff_t p = *ppos;
149         ssize_t ret;
150         int i;
151         char *buf;
152         unsigned int val, start_reg;
153
154         if (*ppos < 0 || !count)
155                 return -EINVAL;
156
157         buf = kmalloc(count, GFP_KERNEL);
158         if (!buf)
159                 return -ENOMEM;
160
161         /* Calculate the length of a fixed format  */
162         if (!map->debugfs_tot_len) {
163                 map->debugfs_reg_len = regmap_calc_reg_len(map->max_register,
164                                                            buf, count);
165                 map->debugfs_val_len = 2 * map->format.val_bytes;
166                 map->debugfs_tot_len = map->debugfs_reg_len +
167                         map->debugfs_val_len + 3;      /* : \n */
168         }
169
170         /* Work out which register we're starting at */
171         start_reg = regmap_debugfs_get_dump_start(map, from, *ppos, &p);
172
173         for (i = start_reg; i <= to; i += map->reg_stride) {
174                 if (!regmap_readable(map, i))
175                         continue;
176
177                 if (regmap_precious(map, i))
178                         continue;
179
180                 /* If we're in the region the user is trying to read */
181                 if (p >= *ppos) {
182                         /* ...but not beyond it */
183                         if (buf_pos + 1 + map->debugfs_tot_len >= count)
184                                 break;
185
186                         /* Format the register */
187                         snprintf(buf + buf_pos, count - buf_pos, "%.*x: ",
188                                  map->debugfs_reg_len, i - from);
189                         buf_pos += map->debugfs_reg_len + 2;
190
191                         /* Format the value, write all X if we can't read */
192                         ret = regmap_read(map, i, &val);
193                         if (ret == 0)
194                                 snprintf(buf + buf_pos, count - buf_pos,
195                                          "%.*x", map->debugfs_val_len, val);
196                         else
197                                 memset(buf + buf_pos, 'X',
198                                        map->debugfs_val_len);
199                         buf_pos += 2 * map->format.val_bytes;
200
201                         buf[buf_pos++] = '\n';
202                 }
203                 p += map->debugfs_tot_len;
204         }
205
206         ret = buf_pos;
207
208         if (copy_to_user(user_buf, buf, buf_pos)) {
209                 ret = -EFAULT;
210                 goto out;
211         }
212
213         *ppos += buf_pos;
214
215 out:
216         kfree(buf);
217         return ret;
218 }
219
220 static ssize_t regmap_map_read_file(struct file *file, char __user *user_buf,
221                                     size_t count, loff_t *ppos)
222 {
223         struct regmap *map = file->private_data;
224
225         return regmap_read_debugfs(map, 0, map->max_register, user_buf,
226                                    count, ppos);
227 }
228
229 #undef REGMAP_ALLOW_WRITE_DEBUGFS
230 #ifdef REGMAP_ALLOW_WRITE_DEBUGFS
231 /*
232  * This can be dangerous especially when we have clients such as
233  * PMICs, therefore don't provide any real compile time configuration option
234  * for this feature, people who want to use this will need to modify
235  * the source code directly.
236  */
237 static ssize_t regmap_map_write_file(struct file *file,
238                                      const char __user *user_buf,
239                                      size_t count, loff_t *ppos)
240 {
241         char buf[32];
242         size_t buf_size;
243         char *start = buf;
244         unsigned long reg, value;
245         struct regmap *map = file->private_data;
246
247         buf_size = min(count, (sizeof(buf)-1));
248         if (copy_from_user(buf, user_buf, buf_size))
249                 return -EFAULT;
250         buf[buf_size] = 0;
251
252         while (*start == ' ')
253                 start++;
254         reg = simple_strtoul(start, &start, 16);
255         while (*start == ' ')
256                 start++;
257         if (strict_strtoul(start, 16, &value))
258                 return -EINVAL;
259
260         /* Userspace has been fiddling around behind the kernel's back */
261         add_taint(TAINT_USER);
262
263         regmap_write(map, reg, value);
264         return buf_size;
265 }
266 #else
267 #define regmap_map_write_file NULL
268 #endif
269
270 static const struct file_operations regmap_map_fops = {
271         .open = simple_open,
272         .read = regmap_map_read_file,
273         .write = regmap_map_write_file,
274         .llseek = default_llseek,
275 };
276
277 static ssize_t regmap_range_read_file(struct file *file, char __user *user_buf,
278                                       size_t count, loff_t *ppos)
279 {
280         struct regmap_range_node *range = file->private_data;
281         struct regmap *map = range->map;
282
283         return regmap_read_debugfs(map, range->range_min, range->range_max,
284                                    user_buf, count, ppos);
285 }
286
287 static const struct file_operations regmap_range_fops = {
288         .open = simple_open,
289         .read = regmap_range_read_file,
290         .llseek = default_llseek,
291 };
292
293 static ssize_t regmap_access_read_file(struct file *file,
294                                        char __user *user_buf, size_t count,
295                                        loff_t *ppos)
296 {
297         int reg_len, tot_len;
298         size_t buf_pos = 0;
299         loff_t p = 0;
300         ssize_t ret;
301         int i;
302         struct regmap *map = file->private_data;
303         char *buf;
304
305         if (*ppos < 0 || !count)
306                 return -EINVAL;
307
308         buf = kmalloc(count, GFP_KERNEL);
309         if (!buf)
310                 return -ENOMEM;
311
312         /* Calculate the length of a fixed format  */
313         reg_len = regmap_calc_reg_len(map->max_register, buf, count);
314         tot_len = reg_len + 10; /* ': R W V P\n' */
315
316         for (i = 0; i <= map->max_register; i += map->reg_stride) {
317                 /* Ignore registers which are neither readable nor writable */
318                 if (!regmap_readable(map, i) && !regmap_writeable(map, i))
319                         continue;
320
321                 /* If we're in the region the user is trying to read */
322                 if (p >= *ppos) {
323                         /* ...but not beyond it */
324                         if (buf_pos >= count - 1 - tot_len)
325                                 break;
326
327                         /* Format the register */
328                         snprintf(buf + buf_pos, count - buf_pos,
329                                  "%.*x: %c %c %c %c\n",
330                                  reg_len, i,
331                                  regmap_readable(map, i) ? 'y' : 'n',
332                                  regmap_writeable(map, i) ? 'y' : 'n',
333                                  regmap_volatile(map, i) ? 'y' : 'n',
334                                  regmap_precious(map, i) ? 'y' : 'n');
335
336                         buf_pos += tot_len;
337                 }
338                 p += tot_len;
339         }
340
341         ret = buf_pos;
342
343         if (copy_to_user(user_buf, buf, buf_pos)) {
344                 ret = -EFAULT;
345                 goto out;
346         }
347
348         *ppos += buf_pos;
349
350 out:
351         kfree(buf);
352         return ret;
353 }
354
355 static const struct file_operations regmap_access_fops = {
356         .open = simple_open,
357         .read = regmap_access_read_file,
358         .llseek = default_llseek,
359 };
360
361 void regmap_debugfs_init(struct regmap *map, const char *name)
362 {
363         struct rb_node *next;
364         struct regmap_range_node *range_node;
365
366         INIT_LIST_HEAD(&map->debugfs_off_cache);
367
368         if (name) {
369                 map->debugfs_name = kasprintf(GFP_KERNEL, "%s-%s",
370                                               dev_name(map->dev), name);
371                 name = map->debugfs_name;
372         } else {
373                 name = dev_name(map->dev);
374         }
375
376         map->debugfs = debugfs_create_dir(name, regmap_debugfs_root);
377         if (!map->debugfs) {
378                 dev_warn(map->dev, "Failed to create debugfs directory\n");
379                 return;
380         }
381
382         debugfs_create_file("name", 0400, map->debugfs,
383                             map, &regmap_name_fops);
384
385         if (map->max_register) {
386                 debugfs_create_file("registers", 0400, map->debugfs,
387                                     map, &regmap_map_fops);
388                 debugfs_create_file("access", 0400, map->debugfs,
389                                     map, &regmap_access_fops);
390         }
391
392         if (map->cache_type) {
393                 debugfs_create_bool("cache_only", 0400, map->debugfs,
394                                     &map->cache_only);
395                 debugfs_create_bool("cache_dirty", 0400, map->debugfs,
396                                     &map->cache_dirty);
397                 debugfs_create_bool("cache_bypass", 0400, map->debugfs,
398                                     &map->cache_bypass);
399         }
400
401         next = rb_first(&map->range_tree);
402         while (next) {
403                 range_node = rb_entry(next, struct regmap_range_node, node);
404
405                 if (range_node->name)
406                         debugfs_create_file(range_node->name, 0400,
407                                             map->debugfs, range_node,
408                                             &regmap_range_fops);
409
410                 next = rb_next(&range_node->node);
411         }
412 }
413
414 void regmap_debugfs_exit(struct regmap *map)
415 {
416         debugfs_remove_recursive(map->debugfs);
417         regmap_debugfs_free_dump_cache(map);
418         kfree(map->debugfs_name);
419 }
420
421 void regmap_debugfs_initcall(void)
422 {
423         regmap_debugfs_root = debugfs_create_dir("regmap", NULL);
424         if (!regmap_debugfs_root) {
425                 pr_warn("regmap: Failed to create debugfs root\n");
426                 return;
427         }
428 }