blob: fdc535b22e39a9f3ed0c121d37fe3904babe790d [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/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020052 * Data structure to hold the pointer to the mtd device as well
53 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000054 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020055struct mtd_file_info {
56 struct mtd_info *mtd;
57 enum mtd_file_modes mode;
58};
Nicolas Pitre31f42332005-02-08 17:45:55 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
61{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020062 struct mtd_file_info *mfi = file->private_data;
63 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 switch (orig) {
66 case 0:
67 /* SEEK_SET */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 break;
69 case 1:
70 /* SEEK_CUR */
Todd Poynor8b491d72005-08-04 02:05:51 +010071 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 break;
73 case 2:
74 /* SEEK_END */
Todd Poynor8b491d72005-08-04 02:05:51 +010075 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 break;
77 default:
78 return -EINVAL;
79 }
80
Todd Poynor8b491d72005-08-04 02:05:51 +010081 if (offset >= 0 && offset < mtd->size)
82 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Todd Poynor8b491d72005-08-04 02:05:51 +010084 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87
88
89static int mtd_open(struct inode *inode, struct file *file)
90{
91 int minor = iminor(inode);
92 int devnum = minor >> 1;
93 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020094 struct mtd_file_info *mfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
97
98 if (devnum >= MAX_MTD_DEVICES)
99 return -ENODEV;
100
101 /* You can't open the RO devices RW */
102 if ((file->f_mode & 2) && (minor & 1))
103 return -EACCES;
104
105 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (!mtd)
108 return -ENODEV;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (MTD_ABSENT == mtd->type) {
111 put_mtd_device(mtd);
112 return -ENODEV;
113 }
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /* You can't open it RW if it's not a writeable device */
116 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
117 put_mtd_device(mtd);
118 return -EACCES;
119 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000120
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200121 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
122 if (!mfi) {
123 put_mtd_device(mtd);
124 return -ENOMEM;
125 }
126 mfi->mtd = mtd;
127 file->private_data = mfi;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return 0;
130} /* mtd_open */
131
132/*====================================================================*/
133
134static int mtd_close(struct inode *inode, struct file *file)
135{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200136 struct mtd_file_info *mfi = file->private_data;
137 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
140
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);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200145 file->private_data = NULL;
146 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 return 0;
149} /* mtd_close */
150
151/* FIXME: This _really_ needs to die. In 2.5, we should lock the
152 userspace buffer down and use it directly with readv/writev.
153*/
154#define MAX_KMALLOC_SIZE 0x20000
155
156static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
157{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200158 struct mtd_file_info *mfi = file->private_data;
159 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 size_t retlen=0;
161 size_t total_retlen=0;
162 int ret=0;
163 int len;
164 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
167
168 if (*ppos + count > mtd->size)
169 count = mtd->size - *ppos;
170
171 if (!count)
172 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
175 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100176
177 if (count > MAX_KMALLOC_SIZE)
178 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
179 else
180 kbuf=kmalloc(count, GFP_KERNEL);
181
182 if (!kbuf)
183 return -ENOMEM;
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100186
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000187 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 len = MAX_KMALLOC_SIZE;
189 else
190 len = count;
191
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200192 switch (mfi->mode) {
193 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000194 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
195 break;
196 case MTD_MODE_OTP_USER:
197 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
198 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200199 case MTD_MODE_RAW:
200 {
201 struct mtd_oob_ops ops;
202
203 ops.mode = MTD_OOB_RAW;
204 ops.datbuf = kbuf;
205 ops.oobbuf = NULL;
206 ops.len = len;
207
208 ret = mtd->read_oob(mtd, *ppos, &ops);
209 retlen = ops.retlen;
210 break;
211 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000212 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200213 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* Nand returns -EBADMSG on ecc errors, but it returns
216 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000217 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200218 * For kernel internal usage it also might return -EUCLEAN
219 * to signal the caller that a bitflip has occured and has
220 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Userspace software which accesses NAND this way
222 * must be aware of the fact that it deals with NAND
223 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200224 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 *ppos += retlen;
226 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200227 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EFAULT;
229 }
230 else
231 total_retlen += retlen;
232
233 count -= retlen;
234 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000235 if (retlen == 0)
236 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238 else {
239 kfree(kbuf);
240 return ret;
241 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Thago Galesib802c072006-04-17 17:38:15 +0100245 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return total_retlen;
247} /* mtd_read */
248
249static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
250{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200251 struct mtd_file_info *mfi = file->private_data;
252 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 char *kbuf;
254 size_t retlen;
255 size_t total_retlen=0;
256 int ret=0;
257 int len;
258
259 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (*ppos == mtd->size)
262 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (*ppos + count > mtd->size)
265 count = mtd->size - *ppos;
266
267 if (!count)
268 return 0;
269
Thago Galesib802c072006-04-17 17:38:15 +0100270 if (count > MAX_KMALLOC_SIZE)
271 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
272 else
273 kbuf=kmalloc(count, GFP_KERNEL);
274
275 if (!kbuf)
276 return -ENOMEM;
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100279
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000280 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 len = MAX_KMALLOC_SIZE;
282 else
283 len = count;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (copy_from_user(kbuf, buf, len)) {
286 kfree(kbuf);
287 return -EFAULT;
288 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000289
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200290 switch (mfi->mode) {
291 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000292 ret = -EROFS;
293 break;
294 case MTD_MODE_OTP_USER:
295 if (!mtd->write_user_prot_reg) {
296 ret = -EOPNOTSUPP;
297 break;
298 }
299 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
300 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200301
302 case MTD_MODE_RAW:
303 {
304 struct mtd_oob_ops ops;
305
306 ops.mode = MTD_OOB_RAW;
307 ops.datbuf = kbuf;
308 ops.oobbuf = NULL;
309 ops.len = len;
310
311 ret = mtd->write_oob(mtd, *ppos, &ops);
312 retlen = ops.retlen;
313 break;
314 }
315
Nicolas Pitre31f42332005-02-08 17:45:55 +0000316 default:
317 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (!ret) {
320 *ppos += retlen;
321 total_retlen += retlen;
322 count -= retlen;
323 buf += retlen;
324 }
325 else {
326 kfree(kbuf);
327 return ret;
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 }
330
Thago Galesib802c072006-04-17 17:38:15 +0100331 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return total_retlen;
333} /* mtd_write */
334
335/*======================================================================
336
337 IOCTL calls for getting device parameters.
338
339======================================================================*/
340static void mtdchar_erase_callback (struct erase_info *instr)
341{
342 wake_up((wait_queue_head_t *)instr->priv);
343}
344
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200345#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
346static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
347{
348 struct mtd_info *mtd = mfi->mtd;
349 int ret = 0;
350
351 switch (mode) {
352 case MTD_OTP_FACTORY:
353 if (!mtd->read_fact_prot_reg)
354 ret = -EOPNOTSUPP;
355 else
356 mfi->mode = MTD_MODE_OTP_FACTORY;
357 break;
358 case MTD_OTP_USER:
359 if (!mtd->read_fact_prot_reg)
360 ret = -EOPNOTSUPP;
361 else
362 mfi->mode = MTD_MODE_OTP_USER;
363 break;
364 default:
365 ret = -EINVAL;
366 case MTD_OTP_OFF:
367 break;
368 }
369 return ret;
370}
371#else
372# define otp_select_filemode(f,m) -EOPNOTSUPP
373#endif
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static int mtd_ioctl(struct inode *inode, struct file *file,
376 u_int cmd, u_long arg)
377{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200378 struct mtd_file_info *mfi = file->private_data;
379 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 void __user *argp = (void __user *)arg;
381 int ret = 0;
382 u_long size;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
385
386 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
387 if (cmd & IOC_IN) {
388 if (!access_ok(VERIFY_READ, argp, size))
389 return -EFAULT;
390 }
391 if (cmd & IOC_OUT) {
392 if (!access_ok(VERIFY_WRITE, argp, size))
393 return -EFAULT;
394 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 switch (cmd) {
397 case MEMGETREGIONCOUNT:
398 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
399 return -EFAULT;
400 break;
401
402 case MEMGETREGIONINFO:
403 {
404 struct region_info_user ur;
405
406 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
407 return -EFAULT;
408
409 if (ur.regionindex >= mtd->numeraseregions)
410 return -EINVAL;
411 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
412 sizeof(struct mtd_erase_region_info)))
413 return -EFAULT;
414 break;
415 }
416
417 case MEMGETINFO:
418 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
419 return -EFAULT;
420 break;
421
422 case MEMERASE:
423 {
424 struct erase_info *erase;
425
426 if(!(file->f_mode & 2))
427 return -EPERM;
428
429 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
430 if (!erase)
431 ret = -ENOMEM;
432 else {
433 wait_queue_head_t waitq;
434 DECLARE_WAITQUEUE(wait, current);
435
436 init_waitqueue_head(&waitq);
437
438 memset (erase,0,sizeof(struct erase_info));
439 if (copy_from_user(&erase->addr, argp,
440 sizeof(struct erase_info_user))) {
441 kfree(erase);
442 return -EFAULT;
443 }
444 erase->mtd = mtd;
445 erase->callback = mtdchar_erase_callback;
446 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /*
449 FIXME: Allow INTERRUPTIBLE. Which means
450 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 If the wq_head is on the stack, and we
453 leave because we got interrupted, then the
454 wq_head is no longer there when the
455 callback routine tries to wake us up.
456 */
457 ret = mtd->erase(mtd, erase);
458 if (!ret) {
459 set_current_state(TASK_UNINTERRUPTIBLE);
460 add_wait_queue(&waitq, &wait);
461 if (erase->state != MTD_ERASE_DONE &&
462 erase->state != MTD_ERASE_FAILED)
463 schedule();
464 remove_wait_queue(&waitq, &wait);
465 set_current_state(TASK_RUNNING);
466
467 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
468 }
469 kfree(erase);
470 }
471 break;
472 }
473
474 case MEMWRITEOOB:
475 {
476 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200477 struct mtd_oob_ops ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if(!(file->f_mode & 2))
480 return -EPERM;
481
482 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
483 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000484
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200485 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return -EINVAL;
487
488 if (!mtd->write_oob)
489 ret = -EOPNOTSUPP;
490 else
491 ret = access_ok(VERIFY_READ, buf.ptr,
492 buf.length) ? 0 : EFAULT;
493
494 if (ret)
495 return ret;
496
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200497 ops.len = buf.length;
498 ops.ooblen = mtd->oobsize;
499 ops.ooboffs = buf.start & (mtd->oobsize - 1);
500 ops.datbuf = NULL;
501 ops.mode = MTD_OOB_PLACE;
502
503 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
504 return -EINVAL;
505
506 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
507 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000509
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200510 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
511 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EFAULT;
513 }
514
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200515 buf.start &= ~(mtd->oobsize - 1);
516 ret = mtd->write_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200518 if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen,
519 sizeof(uint32_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 ret = -EFAULT;
521
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200522 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
524
525 }
526
527 case MEMREADOOB:
528 {
529 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200530 struct mtd_oob_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
533 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000534
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200535 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return -EINVAL;
537
538 if (!mtd->read_oob)
539 ret = -EOPNOTSUPP;
540 else
541 ret = access_ok(VERIFY_WRITE, buf.ptr,
542 buf.length) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (ret)
544 return ret;
545
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200546 ops.len = buf.length;
547 ops.ooblen = mtd->oobsize;
548 ops.ooboffs = buf.start & (mtd->oobsize - 1);
549 ops.datbuf = NULL;
550 ops.mode = MTD_OOB_PLACE;
551
552 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
553 return -EINVAL;
554
555 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
556 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000558
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200559 buf.start &= ~(mtd->oobsize - 1);
560 ret = mtd->read_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200562 if (put_user(ops.retlen, (uint32_t __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 ret = -EFAULT;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200564 else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf,
565 ops.retlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 ret = -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000567
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200568 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 break;
570 }
571
572 case MEMLOCK:
573 {
574 struct erase_info_user info;
575
576 if (copy_from_user(&info, argp, sizeof(info)))
577 return -EFAULT;
578
579 if (!mtd->lock)
580 ret = -EOPNOTSUPP;
581 else
582 ret = mtd->lock(mtd, info.start, info.length);
583 break;
584 }
585
586 case MEMUNLOCK:
587 {
588 struct erase_info_user info;
589
590 if (copy_from_user(&info, argp, sizeof(info)))
591 return -EFAULT;
592
593 if (!mtd->unlock)
594 ret = -EOPNOTSUPP;
595 else
596 ret = mtd->unlock(mtd, info.start, info.length);
597 break;
598 }
599
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200600 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 case MEMGETOOBSEL:
602 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200603 struct nand_oobinfo oi;
604
605 if (!mtd->ecclayout)
606 return -EOPNOTSUPP;
607 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
608 return -EINVAL;
609
610 oi.useecc = MTD_NANDECC_AUTOPLACE;
611 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
612 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
613 sizeof(oi.oobfree));
614
615 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 return -EFAULT;
617 break;
618 }
619
620 case MEMGETBADBLOCK:
621 {
622 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (copy_from_user(&offs, argp, sizeof(loff_t)))
625 return -EFAULT;
626 if (!mtd->block_isbad)
627 ret = -EOPNOTSUPP;
628 else
629 return mtd->block_isbad(mtd, offs);
630 break;
631 }
632
633 case MEMSETBADBLOCK:
634 {
635 loff_t offs;
636
637 if (copy_from_user(&offs, argp, sizeof(loff_t)))
638 return -EFAULT;
639 if (!mtd->block_markbad)
640 ret = -EOPNOTSUPP;
641 else
642 return mtd->block_markbad(mtd, offs);
643 break;
644 }
645
Kyungmin Park493c6462006-05-12 17:03:07 +0300646#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000647 case OTPSELECT:
648 {
649 int mode;
650 if (copy_from_user(&mode, argp, sizeof(int)))
651 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200652
653 mfi->mode = MTD_MODE_NORMAL;
654
655 ret = otp_select_filemode(mfi, mode);
656
Nicolas Pitre81dba482005-04-01 16:36:15 +0100657 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000658 break;
659 }
660
661 case OTPGETREGIONCOUNT:
662 case OTPGETREGIONINFO:
663 {
664 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
665 if (!buf)
666 return -ENOMEM;
667 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200668 switch (mfi->mode) {
669 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000670 if (mtd->get_fact_prot_info)
671 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
672 break;
673 case MTD_MODE_OTP_USER:
674 if (mtd->get_user_prot_info)
675 ret = mtd->get_user_prot_info(mtd, buf, 4096);
676 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200677 default:
678 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000679 }
680 if (ret >= 0) {
681 if (cmd == OTPGETREGIONCOUNT) {
682 int nbr = ret / sizeof(struct otp_info);
683 ret = copy_to_user(argp, &nbr, sizeof(int));
684 } else
685 ret = copy_to_user(argp, buf, ret);
686 if (ret)
687 ret = -EFAULT;
688 }
689 kfree(buf);
690 break;
691 }
692
693 case OTPLOCK:
694 {
695 struct otp_info info;
696
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200697 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000698 return -EINVAL;
699 if (copy_from_user(&info, argp, sizeof(info)))
700 return -EFAULT;
701 if (!mtd->lock_user_prot_reg)
702 return -EOPNOTSUPP;
703 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
704 break;
705 }
706#endif
707
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200708 case ECCGETLAYOUT:
709 {
710 if (!mtd->ecclayout)
711 return -EOPNOTSUPP;
712
713 if (copy_to_user(argp, &mtd->ecclayout,
714 sizeof(struct nand_ecclayout)))
715 return -EFAULT;
716 break;
717 }
718
719 case ECCGETSTATS:
720 {
721 if (copy_to_user(argp, &mtd->ecc_stats,
722 sizeof(struct mtd_ecc_stats)))
723 return -EFAULT;
724 break;
725 }
726
727 case MTDFILEMODE:
728 {
729 mfi->mode = 0;
730
731 switch(arg) {
732 case MTD_MODE_OTP_FACTORY:
733 case MTD_MODE_OTP_USER:
734 ret = otp_select_filemode(mfi, arg);
735 break;
736
737 case MTD_MODE_RAW:
738 if (!mtd->read_oob || !mtd->write_oob)
739 return -EOPNOTSUPP;
740 mfi->mode = arg;
741
742 case MTD_MODE_NORMAL:
743 break;
744 default:
745 ret = -EINVAL;
746 }
747 file->f_pos = 0;
748 break;
749 }
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 default:
752 ret = -ENOTTY;
753 }
754
755 return ret;
756} /* memory_ioctl */
757
758static struct file_operations mtd_fops = {
759 .owner = THIS_MODULE,
760 .llseek = mtd_lseek,
761 .read = mtd_read,
762 .write = mtd_write,
763 .ioctl = mtd_ioctl,
764 .open = mtd_open,
765 .release = mtd_close,
766};
767
768static int __init init_mtdchar(void)
769{
770 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
771 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
772 MTD_CHAR_MAJOR);
773 return -EAGAIN;
774 }
775
Todd Poynor9bc7b382005-06-30 01:23:27 +0100776 mtd_class = class_create(THIS_MODULE, "mtd");
777
778 if (IS_ERR(mtd_class)) {
779 printk(KERN_ERR "Error creating mtd class.\n");
780 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
Coywolf Qi Hunt3a7a8822005-07-04 12:15:28 -0500781 return PTR_ERR(mtd_class);
Todd Poynor9bc7b382005-06-30 01:23:27 +0100782 }
783
784 register_mtd_user(&notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786}
787
788static void __exit cleanup_mtdchar(void)
789{
Todd Poynor9bc7b382005-06-30 01:23:27 +0100790 unregister_mtd_user(&notifier);
791 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
793}
794
795module_init(init_mtdchar);
796module_exit(cleanup_mtdchar);
797
798
799MODULE_LICENSE("GPL");
800MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
801MODULE_DESCRIPTION("Direct character-device access to MTD devices");