staging: android: persistent_ram: refactor ecc support
[pandora-kernel.git] / drivers / staging / android / ram_console.c
1 /* drivers/android/ram_console.c
2  *
3  * Copyright (C) 2007-2008 Google, Inc.
4  *
5  * This software is licensed under the terms of the GNU General Public
6  * License version 2, as published by the Free Software Foundation, and
7  * may be copied, distributed, and modified under those terms.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  */
15
16 #include <linux/console.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/proc_fs.h>
21 #include <linux/string.h>
22 #include <linux/uaccess.h>
23 #include <linux/io.h>
24 #include "persistent_ram.h"
25 #include "ram_console.h"
26
27 static struct persistent_ram_zone ram_console_zone;
28 static const char *bootinfo;
29 static size_t bootinfo_size;
30
31 static void
32 ram_console_write(struct console *console, const char *s, unsigned int count)
33 {
34         struct persistent_ram_zone *prz = console->data;
35         persistent_ram_write(prz, s, count);
36 }
37
38 static struct console ram_console = {
39         .name   = "ram",
40         .write  = ram_console_write,
41         .flags  = CON_PRINTBUFFER | CON_ENABLED | CON_ANYTIME,
42         .index  = -1,
43 };
44
45 void ram_console_enable_console(int enabled)
46 {
47         if (enabled)
48                 ram_console.flags |= CON_ENABLED;
49         else
50                 ram_console.flags &= ~CON_ENABLED;
51 }
52
53 static int ram_console_driver_probe(struct platform_device *pdev)
54 {
55         struct resource *res = pdev->resource;
56         size_t start;
57         size_t buffer_size;
58         void *buffer;
59         struct ram_console_platform_data *pdata = pdev->dev.platform_data;
60         int ret;
61
62         if (res == NULL || pdev->num_resources != 1 ||
63             !(res->flags & IORESOURCE_MEM)) {
64                 printk(KERN_ERR "ram_console: invalid resource, %p %d flags "
65                        "%lx\n", res, pdev->num_resources, res ? res->flags : 0);
66                 return -ENXIO;
67         }
68         buffer_size = resource_size(res);
69         start = res->start;
70         printk(KERN_INFO "ram_console: got buffer at %zx, size %zx\n",
71                start, buffer_size);
72         buffer = ioremap(res->start, buffer_size);
73         if (buffer == NULL) {
74                 printk(KERN_ERR "ram_console: failed to map memory\n");
75                 return -ENOMEM;
76         }
77
78         ret = persistent_ram_init_ringbuffer(&ram_console_zone, buffer,
79                                              buffer_size, true);
80         if (ret)
81                 goto err;
82
83         if (pdata) {
84                 bootinfo = kstrdup(pdata->bootinfo, GFP_KERNEL);
85                 if (bootinfo)
86                         bootinfo_size = strlen(bootinfo);
87         }
88
89         ram_console.data = &ram_console_zone;
90
91         register_console(&ram_console);
92         return 0;
93
94 err:
95         iounmap(buffer);
96         return ret;
97 }
98
99 static struct platform_driver ram_console_driver = {
100         .probe = ram_console_driver_probe,
101         .driver         = {
102                 .name   = "ram_console",
103         },
104 };
105
106 static int __init ram_console_module_init(void)
107 {
108         int err;
109         err = platform_driver_register(&ram_console_driver);
110         return err;
111 }
112
113 static ssize_t ram_console_read_old(struct file *file, char __user *buf,
114                                     size_t len, loff_t *offset)
115 {
116         loff_t pos = *offset;
117         ssize_t count;
118         struct persistent_ram_zone *prz = &ram_console_zone;
119         size_t old_log_size = persistent_ram_old_size(prz);
120         const char *old_log = persistent_ram_old(prz);
121         char *str;
122         int ret;
123
124         /* Main last_kmsg log */
125         if (pos < old_log_size) {
126                 count = min(len, (size_t)(old_log_size - pos));
127                 if (copy_to_user(buf, old_log + pos, count))
128                         return -EFAULT;
129                 goto out;
130         }
131
132         /* ECC correction notice */
133         pos -= old_log_size;
134         count = persistent_ram_ecc_string(prz, NULL, 0);
135         if (pos < count) {
136                 str = kmalloc(count, GFP_KERNEL);
137                 if (!str)
138                         return -ENOMEM;
139                 persistent_ram_ecc_string(prz, str, count + 1);
140                 count = min(len, (size_t)(count - pos));
141                 ret = copy_to_user(buf, str + pos, count);
142                 kfree(str);
143                 if (ret)
144                         return -EFAULT;
145                 goto out;
146         }
147
148         /* Boot info passed through pdata */
149         pos -= count;
150         if (pos < bootinfo_size) {
151                 count = min(len, (size_t)(bootinfo_size - pos));
152                 if (copy_to_user(buf, bootinfo + pos, count))
153                         return -EFAULT;
154                 goto out;
155         }
156
157         /* EOF */
158         return 0;
159
160 out:
161         *offset += count;
162         return count;
163 }
164
165 static const struct file_operations ram_console_file_ops = {
166         .owner = THIS_MODULE,
167         .read = ram_console_read_old,
168 };
169
170 static int __init ram_console_late_init(void)
171 {
172         struct proc_dir_entry *entry;
173         struct persistent_ram_zone *prz = &ram_console_zone;
174
175         if (persistent_ram_old_size(prz) == 0)
176                 return 0;
177
178         entry = create_proc_entry("last_kmsg", S_IFREG | S_IRUGO, NULL);
179         if (!entry) {
180                 printk(KERN_ERR "ram_console: failed to create proc entry\n");
181                 persistent_ram_free_old(prz);
182                 return 0;
183         }
184
185         entry->proc_fops = &ram_console_file_ops;
186         entry->size = persistent_ram_old_size(prz) +
187                 persistent_ram_ecc_string(prz, NULL, 0) +
188                 bootinfo_size;
189
190         return 0;
191 }
192
193 late_initcall(ram_console_late_init);
194 postcore_initcall(ram_console_module_init);