staging: android: ram_console: fix crash in ram_console_late_init
[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 ram_console_platform_data *pdata = pdev->dev.platform_data;
56         struct persistent_ram_zone *prz;
57
58         prz = persistent_ram_init_ringbuffer(&pdev->dev, true);
59         if (IS_ERR(prz))
60                 return PTR_ERR(prz);
61
62
63         if (pdata) {
64                 bootinfo = kstrdup(pdata->bootinfo, GFP_KERNEL);
65                 if (bootinfo)
66                         bootinfo_size = strlen(bootinfo);
67         }
68
69         ram_console_zone = prz;
70         ram_console.data = prz;
71
72         register_console(&ram_console);
73
74         return 0;
75 }
76
77 static struct platform_driver ram_console_driver = {
78         .probe = ram_console_driver_probe,
79         .driver         = {
80                 .name   = "ram_console",
81         },
82 };
83
84 static int __init ram_console_module_init(void)
85 {
86         int err;
87         err = platform_driver_register(&ram_console_driver);
88         return err;
89 }
90
91 static ssize_t ram_console_read_old(struct file *file, char __user *buf,
92                                     size_t len, loff_t *offset)
93 {
94         loff_t pos = *offset;
95         ssize_t count;
96         struct persistent_ram_zone *prz = ram_console_zone;
97         size_t old_log_size = persistent_ram_old_size(prz);
98         const char *old_log = persistent_ram_old(prz);
99         char *str;
100         int ret;
101
102         /* Main last_kmsg log */
103         if (pos < old_log_size) {
104                 count = min(len, (size_t)(old_log_size - pos));
105                 if (copy_to_user(buf, old_log + pos, count))
106                         return -EFAULT;
107                 goto out;
108         }
109
110         /* ECC correction notice */
111         pos -= old_log_size;
112         count = persistent_ram_ecc_string(prz, NULL, 0);
113         if (pos < count) {
114                 str = kmalloc(count, GFP_KERNEL);
115                 if (!str)
116                         return -ENOMEM;
117                 persistent_ram_ecc_string(prz, str, count + 1);
118                 count = min(len, (size_t)(count - pos));
119                 ret = copy_to_user(buf, str + pos, count);
120                 kfree(str);
121                 if (ret)
122                         return -EFAULT;
123                 goto out;
124         }
125
126         /* Boot info passed through pdata */
127         pos -= count;
128         if (pos < bootinfo_size) {
129                 count = min(len, (size_t)(bootinfo_size - pos));
130                 if (copy_to_user(buf, bootinfo + pos, count))
131                         return -EFAULT;
132                 goto out;
133         }
134
135         /* EOF */
136         return 0;
137
138 out:
139         *offset += count;
140         return count;
141 }
142
143 static const struct file_operations ram_console_file_ops = {
144         .owner = THIS_MODULE,
145         .read = ram_console_read_old,
146 };
147
148 static int __init ram_console_late_init(void)
149 {
150         struct proc_dir_entry *entry;
151         struct persistent_ram_zone *prz = ram_console_zone;
152
153         if (!prz)
154                 return 0;
155
156         if (persistent_ram_old_size(prz) == 0)
157                 return 0;
158
159         entry = create_proc_entry("last_kmsg", S_IFREG | S_IRUGO, NULL);
160         if (!entry) {
161                 printk(KERN_ERR "ram_console: failed to create proc entry\n");
162                 persistent_ram_free_old(prz);
163                 return 0;
164         }
165
166         entry->proc_fops = &ram_console_file_ops;
167         entry->size = persistent_ram_old_size(prz) +
168                 persistent_ram_ecc_string(prz, NULL, 0) +
169                 bootinfo_size;
170
171         return 0;
172 }
173
174 late_initcall(ram_console_late_init);
175 postcore_initcall(ram_console_module_init);