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