Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 1 | /* |
| 2 | * MTD Oops/Panic logger |
| 3 | * |
| 4 | * Copyright (C) 2007 Nokia Corporation. All rights reserved. |
| 5 | * |
| 6 | * Author: Richard Purdie <rpurdie@openedhand.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/console.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | #include <linux/workqueue.h> |
| 29 | #include <linux/sched.h> |
| 30 | #include <linux/wait.h> |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 31 | #include <linux/delay.h> |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 32 | #include <linux/spinlock.h> |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 33 | #include <linux/mtd/mtd.h> |
| 34 | |
| 35 | #define OOPS_PAGE_SIZE 4096 |
| 36 | |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 37 | struct mtdoops_context { |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 38 | int mtd_index; |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 39 | struct work_struct work_erase; |
| 40 | struct work_struct work_write; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 41 | struct mtd_info *mtd; |
| 42 | int oops_pages; |
| 43 | int nextpage; |
| 44 | int nextcount; |
| 45 | |
| 46 | void *oops_buf; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 47 | |
| 48 | /* writecount and disabling ready are spin lock protected */ |
| 49 | spinlock_t writecount_lock; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 50 | int ready; |
| 51 | int writecount; |
| 52 | } oops_cxt; |
| 53 | |
| 54 | static void mtdoops_erase_callback(struct erase_info *done) |
| 55 | { |
| 56 | wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv; |
| 57 | wake_up(wait_q); |
| 58 | } |
| 59 | |
| 60 | static int mtdoops_erase_block(struct mtd_info *mtd, int offset) |
| 61 | { |
| 62 | struct erase_info erase; |
| 63 | DECLARE_WAITQUEUE(wait, current); |
| 64 | wait_queue_head_t wait_q; |
| 65 | int ret; |
| 66 | |
| 67 | init_waitqueue_head(&wait_q); |
| 68 | erase.mtd = mtd; |
| 69 | erase.callback = mtdoops_erase_callback; |
| 70 | erase.addr = offset; |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 71 | erase.len = mtd->erasesize; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 72 | erase.priv = (u_long)&wait_q; |
| 73 | |
| 74 | set_current_state(TASK_INTERRUPTIBLE); |
| 75 | add_wait_queue(&wait_q, &wait); |
| 76 | |
| 77 | ret = mtd->erase(mtd, &erase); |
| 78 | if (ret) { |
| 79 | set_current_state(TASK_RUNNING); |
| 80 | remove_wait_queue(&wait_q, &wait); |
| 81 | printk (KERN_WARNING "mtdoops: erase of region [0x%x, 0x%x] " |
| 82 | "on \"%s\" failed\n", |
| 83 | erase.addr, erase.len, mtd->name); |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | schedule(); /* Wait for erase to finish. */ |
| 88 | remove_wait_queue(&wait_q, &wait); |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 93 | static void mtdoops_inc_counter(struct mtdoops_context *cxt) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 94 | { |
| 95 | struct mtd_info *mtd = cxt->mtd; |
| 96 | size_t retlen; |
| 97 | u32 count; |
| 98 | int ret; |
| 99 | |
| 100 | cxt->nextpage++; |
| 101 | if (cxt->nextpage > cxt->oops_pages) |
| 102 | cxt->nextpage = 0; |
| 103 | cxt->nextcount++; |
| 104 | if (cxt->nextcount == 0xffffffff) |
| 105 | cxt->nextcount = 0; |
| 106 | |
| 107 | ret = mtd->read(mtd, cxt->nextpage * OOPS_PAGE_SIZE, 4, |
| 108 | &retlen, (u_char *) &count); |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 109 | if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) { |
Andrew Morton | 68d09b1 | 2007-08-10 14:01:31 -0700 | [diff] [blame] | 110 | printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)" |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 111 | ", err %d.\n", cxt->nextpage * OOPS_PAGE_SIZE, |
| 112 | retlen, ret); |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 113 | schedule_work(&cxt->work_erase); |
| 114 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* See if we need to erase the next block */ |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 118 | if (count != 0xffffffff) { |
| 119 | schedule_work(&cxt->work_erase); |
| 120 | return; |
| 121 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 122 | |
| 123 | printk(KERN_DEBUG "mtdoops: Ready %d, %d (no erase)\n", |
| 124 | cxt->nextpage, cxt->nextcount); |
| 125 | cxt->ready = 1; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 128 | /* Scheduled work - when we can't proceed without erasing a block */ |
| 129 | static void mtdoops_workfunc_erase(struct work_struct *work) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 130 | { |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 131 | struct mtdoops_context *cxt = |
| 132 | container_of(work, struct mtdoops_context, work_erase); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 133 | struct mtd_info *mtd = cxt->mtd; |
| 134 | int i = 0, j, ret, mod; |
| 135 | |
| 136 | /* We were unregistered */ |
| 137 | if (!mtd) |
| 138 | return; |
| 139 | |
| 140 | mod = (cxt->nextpage * OOPS_PAGE_SIZE) % mtd->erasesize; |
| 141 | if (mod != 0) { |
| 142 | cxt->nextpage = cxt->nextpage + ((mtd->erasesize - mod) / OOPS_PAGE_SIZE); |
| 143 | if (cxt->nextpage > cxt->oops_pages) |
| 144 | cxt->nextpage = 0; |
| 145 | } |
| 146 | |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 147 | while (mtd->block_isbad) { |
| 148 | ret = mtd->block_isbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE); |
| 149 | if (!ret) |
| 150 | break; |
| 151 | if (ret < 0) { |
| 152 | printk(KERN_ERR "mtdoops: block_isbad failed, aborting.\n"); |
| 153 | return; |
| 154 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 155 | badblock: |
| 156 | printk(KERN_WARNING "mtdoops: Bad block at %08x\n", |
| 157 | cxt->nextpage * OOPS_PAGE_SIZE); |
| 158 | i++; |
| 159 | cxt->nextpage = cxt->nextpage + (mtd->erasesize / OOPS_PAGE_SIZE); |
| 160 | if (cxt->nextpage > cxt->oops_pages) |
| 161 | cxt->nextpage = 0; |
| 162 | if (i == (cxt->oops_pages / (mtd->erasesize / OOPS_PAGE_SIZE))) { |
| 163 | printk(KERN_ERR "mtdoops: All blocks bad!\n"); |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | for (j = 0, ret = -1; (j < 3) && (ret < 0); j++) |
| 169 | ret = mtdoops_erase_block(mtd, cxt->nextpage * OOPS_PAGE_SIZE); |
| 170 | |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 171 | if (ret >= 0) { |
| 172 | printk(KERN_DEBUG "mtdoops: Ready %d, %d \n", cxt->nextpage, cxt->nextcount); |
| 173 | cxt->ready = 1; |
| 174 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 175 | } |
| 176 | |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 177 | if (mtd->block_markbad && (ret == -EIO)) { |
| 178 | ret = mtd->block_markbad(mtd, cxt->nextpage * OOPS_PAGE_SIZE); |
| 179 | if (ret < 0) { |
| 180 | printk(KERN_ERR "mtdoops: block_markbad failed, aborting.\n"); |
| 181 | return; |
| 182 | } |
| 183 | } |
| 184 | goto badblock; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 187 | static void mtdoops_write(struct mtdoops_context *cxt, int panic) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 188 | { |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 189 | struct mtd_info *mtd = cxt->mtd; |
| 190 | size_t retlen; |
| 191 | int ret; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 192 | |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 193 | if (cxt->writecount < OOPS_PAGE_SIZE) |
| 194 | memset(cxt->oops_buf + cxt->writecount, 0xff, |
| 195 | OOPS_PAGE_SIZE - cxt->writecount); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 196 | |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 197 | if (panic) |
| 198 | ret = mtd->panic_write(mtd, cxt->nextpage * OOPS_PAGE_SIZE, |
| 199 | OOPS_PAGE_SIZE, &retlen, cxt->oops_buf); |
| 200 | else |
| 201 | ret = mtd->write(mtd, cxt->nextpage * OOPS_PAGE_SIZE, |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 202 | OOPS_PAGE_SIZE, &retlen, cxt->oops_buf); |
| 203 | |
| 204 | cxt->writecount = 0; |
| 205 | |
| 206 | if ((retlen != OOPS_PAGE_SIZE) || (ret < 0)) |
| 207 | printk(KERN_ERR "mtdoops: Write failure at %d (%td of %d written), err %d.\n", |
| 208 | cxt->nextpage * OOPS_PAGE_SIZE, retlen, OOPS_PAGE_SIZE, ret); |
| 209 | |
| 210 | mtdoops_inc_counter(cxt); |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | |
| 214 | static void mtdoops_workfunc_write(struct work_struct *work) |
| 215 | { |
| 216 | struct mtdoops_context *cxt = |
| 217 | container_of(work, struct mtdoops_context, work_write); |
| 218 | |
| 219 | mtdoops_write(cxt, 0); |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void find_next_position(struct mtdoops_context *cxt) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 223 | { |
| 224 | struct mtd_info *mtd = cxt->mtd; |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 225 | int ret, page, maxpos = 0; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 226 | u32 count, maxcount = 0xffffffff; |
| 227 | size_t retlen; |
| 228 | |
| 229 | for (page = 0; page < cxt->oops_pages; page++) { |
Richard Purdie | 2986bd2 | 2008-01-29 11:27:09 +0000 | [diff] [blame] | 230 | ret = mtd->read(mtd, page * OOPS_PAGE_SIZE, 4, &retlen, (u_char *) &count); |
| 231 | if ((retlen != 4) || ((ret < 0) && (ret != -EUCLEAN))) { |
| 232 | printk(KERN_ERR "mtdoops: Read failure at %d (%td of 4 read)" |
| 233 | ", err %d.\n", page * OOPS_PAGE_SIZE, retlen, ret); |
| 234 | continue; |
| 235 | } |
| 236 | |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 237 | if (count == 0xffffffff) |
| 238 | continue; |
| 239 | if (maxcount == 0xffffffff) { |
| 240 | maxcount = count; |
| 241 | maxpos = page; |
| 242 | } else if ((count < 0x40000000) && (maxcount > 0xc0000000)) { |
| 243 | maxcount = count; |
| 244 | maxpos = page; |
| 245 | } else if ((count > maxcount) && (count < 0xc0000000)) { |
| 246 | maxcount = count; |
| 247 | maxpos = page; |
| 248 | } else if ((count > maxcount) && (count > 0xc0000000) |
| 249 | && (maxcount > 0x80000000)) { |
| 250 | maxcount = count; |
| 251 | maxpos = page; |
| 252 | } |
| 253 | } |
| 254 | if (maxcount == 0xffffffff) { |
| 255 | cxt->nextpage = 0; |
| 256 | cxt->nextcount = 1; |
| 257 | cxt->ready = 1; |
| 258 | printk(KERN_DEBUG "mtdoops: Ready %d, %d (first init)\n", |
| 259 | cxt->nextpage, cxt->nextcount); |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 260 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | cxt->nextpage = maxpos; |
| 264 | cxt->nextcount = maxcount; |
| 265 | |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 266 | mtdoops_inc_counter(cxt); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | |
| 270 | static void mtdoops_notify_add(struct mtd_info *mtd) |
| 271 | { |
| 272 | struct mtdoops_context *cxt = &oops_cxt; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 273 | |
| 274 | if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0) |
| 275 | return; |
| 276 | |
| 277 | if (mtd->size < (mtd->erasesize * 2)) { |
| 278 | printk(KERN_ERR "MTD partition %d not big enough for mtdoops\n", |
| 279 | mtd->index); |
| 280 | return; |
| 281 | } |
| 282 | |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 283 | if (mtd->erasesize < OOPS_PAGE_SIZE) { |
| 284 | printk(KERN_ERR "Eraseblock size of MTD partition %d too small\n", |
| 285 | mtd->index); |
| 286 | return; |
| 287 | } |
| 288 | |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 289 | cxt->mtd = mtd; |
| 290 | cxt->oops_pages = mtd->size / OOPS_PAGE_SIZE; |
| 291 | |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 292 | find_next_position(cxt); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 293 | |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 294 | printk(KERN_INFO "mtdoops: Attached to MTD device %d\n", mtd->index); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void mtdoops_notify_remove(struct mtd_info *mtd) |
| 298 | { |
| 299 | struct mtdoops_context *cxt = &oops_cxt; |
| 300 | |
| 301 | if ((mtd->index != cxt->mtd_index) || cxt->mtd_index < 0) |
| 302 | return; |
| 303 | |
| 304 | cxt->mtd = NULL; |
| 305 | flush_scheduled_work(); |
| 306 | } |
| 307 | |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 308 | static void mtdoops_console_sync(void) |
| 309 | { |
| 310 | struct mtdoops_context *cxt = &oops_cxt; |
| 311 | struct mtd_info *mtd = cxt->mtd; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 312 | unsigned long flags; |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 313 | |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 314 | if (!cxt->ready || !mtd || cxt->writecount == 0) |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 315 | return; |
| 316 | |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 317 | /* |
| 318 | * Once ready is 0 and we've held the lock no further writes to the |
| 319 | * buffer will happen |
| 320 | */ |
| 321 | spin_lock_irqsave(&cxt->writecount_lock, flags); |
| 322 | if (!cxt->ready) { |
| 323 | spin_unlock_irqrestore(&cxt->writecount_lock, flags); |
| 324 | return; |
| 325 | } |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 326 | cxt->ready = 0; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 327 | spin_unlock_irqrestore(&cxt->writecount_lock, flags); |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 328 | |
Richard Purdie | 621e4f8 | 2008-02-06 10:17:50 +0000 | [diff] [blame] | 329 | if (mtd->panic_write && in_interrupt()) |
| 330 | /* Interrupt context, we're going to panic so try and log */ |
| 331 | mtdoops_write(cxt, 1); |
| 332 | else |
| 333 | schedule_work(&cxt->work_write); |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 334 | } |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 335 | |
| 336 | static void |
| 337 | mtdoops_console_write(struct console *co, const char *s, unsigned int count) |
| 338 | { |
| 339 | struct mtdoops_context *cxt = co->data; |
| 340 | struct mtd_info *mtd = cxt->mtd; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 341 | unsigned long flags; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 342 | |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 343 | if (!oops_in_progress) { |
| 344 | mtdoops_console_sync(); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 345 | return; |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 346 | } |
| 347 | |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 348 | if (!cxt->ready || !mtd) |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 349 | return; |
| 350 | |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 351 | /* Locking on writecount ensures sequential writes to the buffer */ |
| 352 | spin_lock_irqsave(&cxt->writecount_lock, flags); |
| 353 | |
| 354 | /* Check ready status didn't change whilst waiting for the lock */ |
| 355 | if (!cxt->ready) |
| 356 | return; |
| 357 | |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 358 | if (cxt->writecount == 0) { |
| 359 | u32 *stamp = cxt->oops_buf; |
| 360 | *stamp = cxt->nextcount; |
| 361 | cxt->writecount = 4; |
| 362 | } |
| 363 | |
| 364 | if ((count + cxt->writecount) > OOPS_PAGE_SIZE) |
| 365 | count = OOPS_PAGE_SIZE - cxt->writecount; |
| 366 | |
Peter Korsgaard | 235d620 | 2007-11-06 11:56:02 +0100 | [diff] [blame] | 367 | memcpy(cxt->oops_buf + cxt->writecount, s, count); |
| 368 | cxt->writecount += count; |
Richard Purdie | 47c152b | 2008-01-29 10:21:56 +0000 | [diff] [blame] | 369 | |
| 370 | spin_unlock_irqrestore(&cxt->writecount_lock, flags); |
| 371 | |
| 372 | if (cxt->writecount == OOPS_PAGE_SIZE) |
| 373 | mtdoops_console_sync(); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static int __init mtdoops_console_setup(struct console *co, char *options) |
| 377 | { |
| 378 | struct mtdoops_context *cxt = co->data; |
| 379 | |
| 380 | if (cxt->mtd_index != -1) |
| 381 | return -EBUSY; |
| 382 | if (co->index == -1) |
| 383 | return -EINVAL; |
| 384 | |
| 385 | cxt->mtd_index = co->index; |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static struct mtd_notifier mtdoops_notifier = { |
| 390 | .add = mtdoops_notify_add, |
| 391 | .remove = mtdoops_notify_remove, |
| 392 | }; |
| 393 | |
| 394 | static struct console mtdoops_console = { |
| 395 | .name = "ttyMTD", |
| 396 | .write = mtdoops_console_write, |
| 397 | .setup = mtdoops_console_setup, |
Richard Purdie | 8691a72 | 2007-07-10 20:33:54 +0100 | [diff] [blame] | 398 | .unblank = mtdoops_console_sync, |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 399 | .index = -1, |
| 400 | .data = &oops_cxt, |
| 401 | }; |
| 402 | |
| 403 | static int __init mtdoops_console_init(void) |
| 404 | { |
| 405 | struct mtdoops_context *cxt = &oops_cxt; |
| 406 | |
| 407 | cxt->mtd_index = -1; |
| 408 | cxt->oops_buf = vmalloc(OOPS_PAGE_SIZE); |
| 409 | |
| 410 | if (!cxt->oops_buf) { |
Richard Purdie | 79dcd8e | 2008-01-29 10:25:55 +0000 | [diff] [blame] | 411 | printk(KERN_ERR "Failed to allocate mtdoops buffer workspace\n"); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 412 | return -ENOMEM; |
| 413 | } |
| 414 | |
Richard Purdie | 6ce0a856 | 2008-01-29 11:27:11 +0000 | [diff] [blame] | 415 | INIT_WORK(&cxt->work_erase, mtdoops_workfunc_erase); |
| 416 | INIT_WORK(&cxt->work_write, mtdoops_workfunc_write); |
Richard Purdie | 4b23aff | 2007-05-29 13:31:42 +0100 | [diff] [blame] | 417 | |
| 418 | register_console(&mtdoops_console); |
| 419 | register_mtd_user(&mtdoops_notifier); |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | static void __exit mtdoops_console_exit(void) |
| 424 | { |
| 425 | struct mtdoops_context *cxt = &oops_cxt; |
| 426 | |
| 427 | unregister_mtd_user(&mtdoops_notifier); |
| 428 | unregister_console(&mtdoops_console); |
| 429 | vfree(cxt->oops_buf); |
| 430 | } |
| 431 | |
| 432 | |
| 433 | subsys_initcall(mtdoops_console_init); |
| 434 | module_exit(mtdoops_console_exit); |
| 435 | |
| 436 | MODULE_LICENSE("GPL"); |
| 437 | MODULE_AUTHOR("Richard Purdie <rpurdie@openedhand.com>"); |
| 438 | MODULE_DESCRIPTION("MTD Oops/Panic console logger/driver"); |