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