blob: 7522fc3a282748d6ebbf9d54e13b9d15641b2c50 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Thomas Gleixner97894cd2005-11-07 11:15:26 +00002 * $Id: mtdchar.c,v 1.76 2005/11/07 11:14:20 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Character-device access to raw MTD devices.
5 *
6 */
7
8#include <linux/config.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +01009#include <linux/device.h>
10#include <linux/fs.h>
11#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
13#include <linux/module.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010014#include <linux/slab.h>
15#include <linux/sched.h>
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mtd/mtd.h>
18#include <linux/mtd/compatmac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Thomas Gleixner15fdc522005-11-07 00:14:42 +010020#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010021
22static struct class *mtd_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24static void mtd_notify_add(struct mtd_info* mtd)
25{
26 if (!mtd)
27 return;
28
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -070029 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
Todd Poynor9bc7b382005-06-30 01:23:27 +010030 NULL, "mtd%d", mtd->index);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000031
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -070032 class_device_create(mtd_class, NULL,
Todd Poynor9bc7b382005-06-30 01:23:27 +010033 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
34 NULL, "mtd%dro", mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
37static void mtd_notify_remove(struct mtd_info* mtd)
38{
39 if (!mtd)
40 return;
Todd Poynor9bc7b382005-06-30 01:23:27 +010041
42 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
43 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46static struct mtd_notifier notifier = {
47 .add = mtd_notify_add,
48 .remove = mtd_notify_remove,
49};
50
Nicolas Pitre045e9a52005-02-08 19:12:53 +000051/*
52 * We use file->private_data to store a pointer to the MTDdevice.
53 * Since alighment is at least 32 bits, we have 2 bits free for OTP
54 * modes as well.
55 */
Nicolas Pitre31f42332005-02-08 17:45:55 +000056
Nicolas Pitre045e9a52005-02-08 19:12:53 +000057#define TO_MTD(file) (struct mtd_info *)((long)((file)->private_data) & ~3L)
58
59#define MTD_MODE_OTP_FACT 1
60#define MTD_MODE_OTP_USER 2
61#define MTD_MODE(file) ((long)((file)->private_data) & 3)
62
63#define SET_MTD_MODE(file, mode) \
64 do { long __p = (long)((file)->private_data); \
65 (file)->private_data = (void *)((__p & ~3L) | mode); } while (0)
Nicolas Pitre31f42332005-02-08 17:45:55 +000066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
68{
Nicolas Pitre045e9a52005-02-08 19:12:53 +000069 struct mtd_info *mtd = TO_MTD(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 switch (orig) {
72 case 0:
73 /* SEEK_SET */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 case 1:
76 /* SEEK_CUR */
Todd Poynor8b491d72005-08-04 02:05:51 +010077 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
79 case 2:
80 /* SEEK_END */
Todd Poynor8b491d72005-08-04 02:05:51 +010081 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
83 default:
84 return -EINVAL;
85 }
86
Todd Poynor8b491d72005-08-04 02:05:51 +010087 if (offset >= 0 && offset < mtd->size)
88 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Todd Poynor8b491d72005-08-04 02:05:51 +010090 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93
94
95static int mtd_open(struct inode *inode, struct file *file)
96{
97 int minor = iminor(inode);
98 int devnum = minor >> 1;
99 struct mtd_info *mtd;
100
101 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
102
103 if (devnum >= MAX_MTD_DEVICES)
104 return -ENODEV;
105
106 /* You can't open the RO devices RW */
107 if ((file->f_mode & 2) && (minor & 1))
108 return -EACCES;
109
110 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (!mtd)
113 return -ENODEV;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (MTD_ABSENT == mtd->type) {
116 put_mtd_device(mtd);
117 return -ENODEV;
118 }
119
120 file->private_data = mtd;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 /* You can't open it RW if it's not a writeable device */
123 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
124 put_mtd_device(mtd);
125 return -EACCES;
126 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return 0;
129} /* mtd_open */
130
131/*====================================================================*/
132
133static int mtd_close(struct inode *inode, struct file *file)
134{
135 struct mtd_info *mtd;
136
137 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
138
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000139 mtd = TO_MTD(file);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (mtd->sync)
142 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 put_mtd_device(mtd);
145
146 return 0;
147} /* mtd_close */
148
149/* FIXME: This _really_ needs to die. In 2.5, we should lock the
150 userspace buffer down and use it directly with readv/writev.
151*/
152#define MAX_KMALLOC_SIZE 0x20000
153
154static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
155{
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000156 struct mtd_info *mtd = TO_MTD(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 size_t retlen=0;
158 size_t total_retlen=0;
159 int ret=0;
160 int len;
161 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
164
165 if (*ppos + count > mtd->size)
166 count = mtd->size - *ppos;
167
168 if (!count)
169 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
172 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100173
174 if (count > MAX_KMALLOC_SIZE)
175 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
176 else
177 kbuf=kmalloc(count, GFP_KERNEL);
178
179 if (!kbuf)
180 return -ENOMEM;
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100183
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000184 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 len = MAX_KMALLOC_SIZE;
186 else
187 len = count;
188
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000189 switch (MTD_MODE(file)) {
Nicolas Pitre31f42332005-02-08 17:45:55 +0000190 case MTD_MODE_OTP_FACT:
191 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
192 break;
193 case MTD_MODE_OTP_USER:
194 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
195 break;
196 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200197 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /* Nand returns -EBADMSG on ecc errors, but it returns
200 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000201 * to dump areas with ecc errors !
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * Userspace software which accesses NAND this way
203 * must be aware of the fact that it deals with NAND
204 */
205 if (!ret || (ret == -EBADMSG)) {
206 *ppos += retlen;
207 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200208 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return -EFAULT;
210 }
211 else
212 total_retlen += retlen;
213
214 count -= retlen;
215 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000216 if (retlen == 0)
217 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
219 else {
220 kfree(kbuf);
221 return ret;
222 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Thago Galesib802c072006-04-17 17:38:15 +0100226 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return total_retlen;
228} /* mtd_read */
229
230static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
231{
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000232 struct mtd_info *mtd = TO_MTD(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 char *kbuf;
234 size_t retlen;
235 size_t total_retlen=0;
236 int ret=0;
237 int len;
238
239 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (*ppos == mtd->size)
242 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (*ppos + count > mtd->size)
245 count = mtd->size - *ppos;
246
247 if (!count)
248 return 0;
249
Thago Galesib802c072006-04-17 17:38:15 +0100250 if (count > MAX_KMALLOC_SIZE)
251 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
252 else
253 kbuf=kmalloc(count, GFP_KERNEL);
254
255 if (!kbuf)
256 return -ENOMEM;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100259
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000260 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 len = MAX_KMALLOC_SIZE;
262 else
263 len = count;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (copy_from_user(kbuf, buf, len)) {
266 kfree(kbuf);
267 return -EFAULT;
268 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000269
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000270 switch (MTD_MODE(file)) {
Nicolas Pitre31f42332005-02-08 17:45:55 +0000271 case MTD_MODE_OTP_FACT:
272 ret = -EROFS;
273 break;
274 case MTD_MODE_OTP_USER:
275 if (!mtd->write_user_prot_reg) {
276 ret = -EOPNOTSUPP;
277 break;
278 }
279 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
280 break;
281 default:
282 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (!ret) {
285 *ppos += retlen;
286 total_retlen += retlen;
287 count -= retlen;
288 buf += retlen;
289 }
290 else {
291 kfree(kbuf);
292 return ret;
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
Thago Galesib802c072006-04-17 17:38:15 +0100296 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return total_retlen;
298} /* mtd_write */
299
300/*======================================================================
301
302 IOCTL calls for getting device parameters.
303
304======================================================================*/
305static void mtdchar_erase_callback (struct erase_info *instr)
306{
307 wake_up((wait_queue_head_t *)instr->priv);
308}
309
310static int mtd_ioctl(struct inode *inode, struct file *file,
311 u_int cmd, u_long arg)
312{
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000313 struct mtd_info *mtd = TO_MTD(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 void __user *argp = (void __user *)arg;
315 int ret = 0;
316 u_long size;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
319
320 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
321 if (cmd & IOC_IN) {
322 if (!access_ok(VERIFY_READ, argp, size))
323 return -EFAULT;
324 }
325 if (cmd & IOC_OUT) {
326 if (!access_ok(VERIFY_WRITE, argp, size))
327 return -EFAULT;
328 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 switch (cmd) {
331 case MEMGETREGIONCOUNT:
332 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
333 return -EFAULT;
334 break;
335
336 case MEMGETREGIONINFO:
337 {
338 struct region_info_user ur;
339
340 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
341 return -EFAULT;
342
343 if (ur.regionindex >= mtd->numeraseregions)
344 return -EINVAL;
345 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
346 sizeof(struct mtd_erase_region_info)))
347 return -EFAULT;
348 break;
349 }
350
351 case MEMGETINFO:
352 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
353 return -EFAULT;
354 break;
355
356 case MEMERASE:
357 {
358 struct erase_info *erase;
359
360 if(!(file->f_mode & 2))
361 return -EPERM;
362
363 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
364 if (!erase)
365 ret = -ENOMEM;
366 else {
367 wait_queue_head_t waitq;
368 DECLARE_WAITQUEUE(wait, current);
369
370 init_waitqueue_head(&waitq);
371
372 memset (erase,0,sizeof(struct erase_info));
373 if (copy_from_user(&erase->addr, argp,
374 sizeof(struct erase_info_user))) {
375 kfree(erase);
376 return -EFAULT;
377 }
378 erase->mtd = mtd;
379 erase->callback = mtdchar_erase_callback;
380 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
383 FIXME: Allow INTERRUPTIBLE. Which means
384 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 If the wq_head is on the stack, and we
387 leave because we got interrupted, then the
388 wq_head is no longer there when the
389 callback routine tries to wake us up.
390 */
391 ret = mtd->erase(mtd, erase);
392 if (!ret) {
393 set_current_state(TASK_UNINTERRUPTIBLE);
394 add_wait_queue(&waitq, &wait);
395 if (erase->state != MTD_ERASE_DONE &&
396 erase->state != MTD_ERASE_FAILED)
397 schedule();
398 remove_wait_queue(&waitq, &wait);
399 set_current_state(TASK_RUNNING);
400
401 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
402 }
403 kfree(erase);
404 }
405 break;
406 }
407
408 case MEMWRITEOOB:
409 {
410 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200411 struct mtd_oob_ops ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if(!(file->f_mode & 2))
414 return -EPERM;
415
416 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
417 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000418
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200419 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return -EINVAL;
421
422 if (!mtd->write_oob)
423 ret = -EOPNOTSUPP;
424 else
425 ret = access_ok(VERIFY_READ, buf.ptr,
426 buf.length) ? 0 : EFAULT;
427
428 if (ret)
429 return ret;
430
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200431 ops.len = buf.length;
432 ops.ooblen = mtd->oobsize;
433 ops.ooboffs = buf.start & (mtd->oobsize - 1);
434 ops.datbuf = NULL;
435 ops.mode = MTD_OOB_PLACE;
436
437 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
438 return -EINVAL;
439
440 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
441 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000443
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200444 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
445 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return -EFAULT;
447 }
448
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200449 buf.start &= ~(mtd->oobsize - 1);
450 ret = mtd->write_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200452 if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen,
453 sizeof(uint32_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 ret = -EFAULT;
455
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200456 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458
459 }
460
461 case MEMREADOOB:
462 {
463 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200464 struct mtd_oob_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
467 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000468
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200469 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return -EINVAL;
471
472 if (!mtd->read_oob)
473 ret = -EOPNOTSUPP;
474 else
475 ret = access_ok(VERIFY_WRITE, buf.ptr,
476 buf.length) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (ret)
478 return ret;
479
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200480 ops.len = buf.length;
481 ops.ooblen = mtd->oobsize;
482 ops.ooboffs = buf.start & (mtd->oobsize - 1);
483 ops.datbuf = NULL;
484 ops.mode = MTD_OOB_PLACE;
485
486 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
487 return -EINVAL;
488
489 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
490 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000492
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200493 buf.start &= ~(mtd->oobsize - 1);
494 ret = mtd->read_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200496 if (put_user(ops.retlen, (uint32_t __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 ret = -EFAULT;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200498 else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf,
499 ops.retlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ret = -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000501
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200502 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 break;
504 }
505
506 case MEMLOCK:
507 {
508 struct erase_info_user info;
509
510 if (copy_from_user(&info, argp, sizeof(info)))
511 return -EFAULT;
512
513 if (!mtd->lock)
514 ret = -EOPNOTSUPP;
515 else
516 ret = mtd->lock(mtd, info.start, info.length);
517 break;
518 }
519
520 case MEMUNLOCK:
521 {
522 struct erase_info_user info;
523
524 if (copy_from_user(&info, argp, sizeof(info)))
525 return -EFAULT;
526
527 if (!mtd->unlock)
528 ret = -EOPNOTSUPP;
529 else
530 ret = mtd->unlock(mtd, info.start, info.length);
531 break;
532 }
533
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200534 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 case MEMGETOOBSEL:
536 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200537 struct nand_oobinfo oi;
538
539 if (!mtd->ecclayout)
540 return -EOPNOTSUPP;
541 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
542 return -EINVAL;
543
544 oi.useecc = MTD_NANDECC_AUTOPLACE;
545 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
546 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
547 sizeof(oi.oobfree));
548
549 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return -EFAULT;
551 break;
552 }
553
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200554 case ECCGETLAYOUT:
555
556 if (!mtd->ecclayout)
557 return -EOPNOTSUPP;
558
559 if (copy_to_user(argp, &mtd->ecclayout,
560 sizeof(struct nand_ecclayout)))
561 return -EFAULT;
562 break;
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 case MEMGETBADBLOCK:
565 {
566 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 if (copy_from_user(&offs, argp, sizeof(loff_t)))
569 return -EFAULT;
570 if (!mtd->block_isbad)
571 ret = -EOPNOTSUPP;
572 else
573 return mtd->block_isbad(mtd, offs);
574 break;
575 }
576
577 case MEMSETBADBLOCK:
578 {
579 loff_t offs;
580
581 if (copy_from_user(&offs, argp, sizeof(loff_t)))
582 return -EFAULT;
583 if (!mtd->block_markbad)
584 ret = -EOPNOTSUPP;
585 else
586 return mtd->block_markbad(mtd, offs);
587 break;
588 }
589
Kyungmin Park493c6462006-05-12 17:03:07 +0300590#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000591 case OTPSELECT:
592 {
593 int mode;
594 if (copy_from_user(&mode, argp, sizeof(int)))
595 return -EFAULT;
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000596 SET_MTD_MODE(file, 0);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000597 switch (mode) {
598 case MTD_OTP_FACTORY:
599 if (!mtd->read_fact_prot_reg)
600 ret = -EOPNOTSUPP;
601 else
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000602 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000603 break;
604 case MTD_OTP_USER:
605 if (!mtd->read_fact_prot_reg)
606 ret = -EOPNOTSUPP;
607 else
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000608 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000609 break;
610 default:
611 ret = -EINVAL;
612 case MTD_OTP_OFF:
613 break;
614 }
Nicolas Pitre81dba482005-04-01 16:36:15 +0100615 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000616 break;
617 }
618
619 case OTPGETREGIONCOUNT:
620 case OTPGETREGIONINFO:
621 {
622 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
623 if (!buf)
624 return -ENOMEM;
625 ret = -EOPNOTSUPP;
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000626 switch (MTD_MODE(file)) {
Nicolas Pitre31f42332005-02-08 17:45:55 +0000627 case MTD_MODE_OTP_FACT:
628 if (mtd->get_fact_prot_info)
629 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
630 break;
631 case MTD_MODE_OTP_USER:
632 if (mtd->get_user_prot_info)
633 ret = mtd->get_user_prot_info(mtd, buf, 4096);
634 break;
635 }
636 if (ret >= 0) {
637 if (cmd == OTPGETREGIONCOUNT) {
638 int nbr = ret / sizeof(struct otp_info);
639 ret = copy_to_user(argp, &nbr, sizeof(int));
640 } else
641 ret = copy_to_user(argp, buf, ret);
642 if (ret)
643 ret = -EFAULT;
644 }
645 kfree(buf);
646 break;
647 }
648
649 case OTPLOCK:
650 {
651 struct otp_info info;
652
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000653 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000654 return -EINVAL;
655 if (copy_from_user(&info, argp, sizeof(info)))
656 return -EFAULT;
657 if (!mtd->lock_user_prot_reg)
658 return -EOPNOTSUPP;
659 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
660 break;
661 }
662#endif
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 default:
665 ret = -ENOTTY;
666 }
667
668 return ret;
669} /* memory_ioctl */
670
671static struct file_operations mtd_fops = {
672 .owner = THIS_MODULE,
673 .llseek = mtd_lseek,
674 .read = mtd_read,
675 .write = mtd_write,
676 .ioctl = mtd_ioctl,
677 .open = mtd_open,
678 .release = mtd_close,
679};
680
681static int __init init_mtdchar(void)
682{
683 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
684 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
685 MTD_CHAR_MAJOR);
686 return -EAGAIN;
687 }
688
Todd Poynor9bc7b382005-06-30 01:23:27 +0100689 mtd_class = class_create(THIS_MODULE, "mtd");
690
691 if (IS_ERR(mtd_class)) {
692 printk(KERN_ERR "Error creating mtd class.\n");
693 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
Coywolf Qi Hunt3a7a8822005-07-04 12:15:28 -0500694 return PTR_ERR(mtd_class);
Todd Poynor9bc7b382005-06-30 01:23:27 +0100695 }
696
697 register_mtd_user(&notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return 0;
699}
700
701static void __exit cleanup_mtdchar(void)
702{
Todd Poynor9bc7b382005-06-30 01:23:27 +0100703 unregister_mtd_user(&notifier);
704 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
706}
707
708module_init(init_mtdchar);
709module_exit(cleanup_mtdchar);
710
711
712MODULE_LICENSE("GPL");
713MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
714MODULE_DESCRIPTION("Direct character-device access to MTD devices");