blob: a48210d58b9286628743308316555827fafb5327 [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 !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200202 * For kernel internal usage it also might return -EUCLEAN
203 * to signal the caller that a bitflip has occured and has
204 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * Userspace software which accesses NAND this way
206 * must be aware of the fact that it deals with NAND
207 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200208 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 *ppos += retlen;
210 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200211 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 return -EFAULT;
213 }
214 else
215 total_retlen += retlen;
216
217 count -= retlen;
218 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000219 if (retlen == 0)
220 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222 else {
223 kfree(kbuf);
224 return ret;
225 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
Thago Galesib802c072006-04-17 17:38:15 +0100229 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return total_retlen;
231} /* mtd_read */
232
233static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
234{
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000235 struct mtd_info *mtd = TO_MTD(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 char *kbuf;
237 size_t retlen;
238 size_t total_retlen=0;
239 int ret=0;
240 int len;
241
242 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (*ppos == mtd->size)
245 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (*ppos + count > mtd->size)
248 count = mtd->size - *ppos;
249
250 if (!count)
251 return 0;
252
Thago Galesib802c072006-04-17 17:38:15 +0100253 if (count > MAX_KMALLOC_SIZE)
254 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
255 else
256 kbuf=kmalloc(count, GFP_KERNEL);
257
258 if (!kbuf)
259 return -ENOMEM;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100262
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000263 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 len = MAX_KMALLOC_SIZE;
265 else
266 len = count;
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (copy_from_user(kbuf, buf, len)) {
269 kfree(kbuf);
270 return -EFAULT;
271 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000272
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000273 switch (MTD_MODE(file)) {
Nicolas Pitre31f42332005-02-08 17:45:55 +0000274 case MTD_MODE_OTP_FACT:
275 ret = -EROFS;
276 break;
277 case MTD_MODE_OTP_USER:
278 if (!mtd->write_user_prot_reg) {
279 ret = -EOPNOTSUPP;
280 break;
281 }
282 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
283 break;
284 default:
285 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!ret) {
288 *ppos += retlen;
289 total_retlen += retlen;
290 count -= retlen;
291 buf += retlen;
292 }
293 else {
294 kfree(kbuf);
295 return ret;
296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
Thago Galesib802c072006-04-17 17:38:15 +0100299 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return total_retlen;
301} /* mtd_write */
302
303/*======================================================================
304
305 IOCTL calls for getting device parameters.
306
307======================================================================*/
308static void mtdchar_erase_callback (struct erase_info *instr)
309{
310 wake_up((wait_queue_head_t *)instr->priv);
311}
312
313static int mtd_ioctl(struct inode *inode, struct file *file,
314 u_int cmd, u_long arg)
315{
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000316 struct mtd_info *mtd = TO_MTD(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 void __user *argp = (void __user *)arg;
318 int ret = 0;
319 u_long size;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
322
323 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
324 if (cmd & IOC_IN) {
325 if (!access_ok(VERIFY_READ, argp, size))
326 return -EFAULT;
327 }
328 if (cmd & IOC_OUT) {
329 if (!access_ok(VERIFY_WRITE, argp, size))
330 return -EFAULT;
331 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 switch (cmd) {
334 case MEMGETREGIONCOUNT:
335 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
336 return -EFAULT;
337 break;
338
339 case MEMGETREGIONINFO:
340 {
341 struct region_info_user ur;
342
343 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
344 return -EFAULT;
345
346 if (ur.regionindex >= mtd->numeraseregions)
347 return -EINVAL;
348 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
349 sizeof(struct mtd_erase_region_info)))
350 return -EFAULT;
351 break;
352 }
353
354 case MEMGETINFO:
355 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
356 return -EFAULT;
357 break;
358
359 case MEMERASE:
360 {
361 struct erase_info *erase;
362
363 if(!(file->f_mode & 2))
364 return -EPERM;
365
366 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
367 if (!erase)
368 ret = -ENOMEM;
369 else {
370 wait_queue_head_t waitq;
371 DECLARE_WAITQUEUE(wait, current);
372
373 init_waitqueue_head(&waitq);
374
375 memset (erase,0,sizeof(struct erase_info));
376 if (copy_from_user(&erase->addr, argp,
377 sizeof(struct erase_info_user))) {
378 kfree(erase);
379 return -EFAULT;
380 }
381 erase->mtd = mtd;
382 erase->callback = mtdchar_erase_callback;
383 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 /*
386 FIXME: Allow INTERRUPTIBLE. Which means
387 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 If the wq_head is on the stack, and we
390 leave because we got interrupted, then the
391 wq_head is no longer there when the
392 callback routine tries to wake us up.
393 */
394 ret = mtd->erase(mtd, erase);
395 if (!ret) {
396 set_current_state(TASK_UNINTERRUPTIBLE);
397 add_wait_queue(&waitq, &wait);
398 if (erase->state != MTD_ERASE_DONE &&
399 erase->state != MTD_ERASE_FAILED)
400 schedule();
401 remove_wait_queue(&waitq, &wait);
402 set_current_state(TASK_RUNNING);
403
404 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
405 }
406 kfree(erase);
407 }
408 break;
409 }
410
411 case MEMWRITEOOB:
412 {
413 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200414 struct mtd_oob_ops ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if(!(file->f_mode & 2))
417 return -EPERM;
418
419 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
420 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000421
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200422 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return -EINVAL;
424
425 if (!mtd->write_oob)
426 ret = -EOPNOTSUPP;
427 else
428 ret = access_ok(VERIFY_READ, buf.ptr,
429 buf.length) ? 0 : EFAULT;
430
431 if (ret)
432 return ret;
433
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200434 ops.len = buf.length;
435 ops.ooblen = mtd->oobsize;
436 ops.ooboffs = buf.start & (mtd->oobsize - 1);
437 ops.datbuf = NULL;
438 ops.mode = MTD_OOB_PLACE;
439
440 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
441 return -EINVAL;
442
443 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
444 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000446
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200447 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
448 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -EFAULT;
450 }
451
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200452 buf.start &= ~(mtd->oobsize - 1);
453 ret = mtd->write_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200455 if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen,
456 sizeof(uint32_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 ret = -EFAULT;
458
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200459 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461
462 }
463
464 case MEMREADOOB:
465 {
466 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200467 struct mtd_oob_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
470 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000471
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200472 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return -EINVAL;
474
475 if (!mtd->read_oob)
476 ret = -EOPNOTSUPP;
477 else
478 ret = access_ok(VERIFY_WRITE, buf.ptr,
479 buf.length) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 if (ret)
481 return ret;
482
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200483 ops.len = buf.length;
484 ops.ooblen = mtd->oobsize;
485 ops.ooboffs = buf.start & (mtd->oobsize - 1);
486 ops.datbuf = NULL;
487 ops.mode = MTD_OOB_PLACE;
488
489 if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
490 return -EINVAL;
491
492 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
493 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000495
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200496 buf.start &= ~(mtd->oobsize - 1);
497 ret = mtd->read_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200499 if (put_user(ops.retlen, (uint32_t __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 ret = -EFAULT;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200501 else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf,
502 ops.retlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 ret = -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000504
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200505 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507 }
508
509 case MEMLOCK:
510 {
511 struct erase_info_user info;
512
513 if (copy_from_user(&info, argp, sizeof(info)))
514 return -EFAULT;
515
516 if (!mtd->lock)
517 ret = -EOPNOTSUPP;
518 else
519 ret = mtd->lock(mtd, info.start, info.length);
520 break;
521 }
522
523 case MEMUNLOCK:
524 {
525 struct erase_info_user info;
526
527 if (copy_from_user(&info, argp, sizeof(info)))
528 return -EFAULT;
529
530 if (!mtd->unlock)
531 ret = -EOPNOTSUPP;
532 else
533 ret = mtd->unlock(mtd, info.start, info.length);
534 break;
535 }
536
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200537 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 case MEMGETOOBSEL:
539 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200540 struct nand_oobinfo oi;
541
542 if (!mtd->ecclayout)
543 return -EOPNOTSUPP;
544 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
545 return -EINVAL;
546
547 oi.useecc = MTD_NANDECC_AUTOPLACE;
548 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
549 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
550 sizeof(oi.oobfree));
551
552 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -EFAULT;
554 break;
555 }
556
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200557 case ECCGETLAYOUT:
558
559 if (!mtd->ecclayout)
560 return -EOPNOTSUPP;
561
562 if (copy_to_user(argp, &mtd->ecclayout,
563 sizeof(struct nand_ecclayout)))
564 return -EFAULT;
565 break;
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 case MEMGETBADBLOCK:
568 {
569 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (copy_from_user(&offs, argp, sizeof(loff_t)))
572 return -EFAULT;
573 if (!mtd->block_isbad)
574 ret = -EOPNOTSUPP;
575 else
576 return mtd->block_isbad(mtd, offs);
577 break;
578 }
579
580 case MEMSETBADBLOCK:
581 {
582 loff_t offs;
583
584 if (copy_from_user(&offs, argp, sizeof(loff_t)))
585 return -EFAULT;
586 if (!mtd->block_markbad)
587 ret = -EOPNOTSUPP;
588 else
589 return mtd->block_markbad(mtd, offs);
590 break;
591 }
592
Kyungmin Park493c6462006-05-12 17:03:07 +0300593#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000594 case OTPSELECT:
595 {
596 int mode;
597 if (copy_from_user(&mode, argp, sizeof(int)))
598 return -EFAULT;
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000599 SET_MTD_MODE(file, 0);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000600 switch (mode) {
601 case MTD_OTP_FACTORY:
602 if (!mtd->read_fact_prot_reg)
603 ret = -EOPNOTSUPP;
604 else
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000605 SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000606 break;
607 case MTD_OTP_USER:
608 if (!mtd->read_fact_prot_reg)
609 ret = -EOPNOTSUPP;
610 else
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000611 SET_MTD_MODE(file, MTD_MODE_OTP_USER);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000612 break;
613 default:
614 ret = -EINVAL;
615 case MTD_OTP_OFF:
616 break;
617 }
Nicolas Pitre81dba482005-04-01 16:36:15 +0100618 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000619 break;
620 }
621
622 case OTPGETREGIONCOUNT:
623 case OTPGETREGIONINFO:
624 {
625 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
626 if (!buf)
627 return -ENOMEM;
628 ret = -EOPNOTSUPP;
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000629 switch (MTD_MODE(file)) {
Nicolas Pitre31f42332005-02-08 17:45:55 +0000630 case MTD_MODE_OTP_FACT:
631 if (mtd->get_fact_prot_info)
632 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
633 break;
634 case MTD_MODE_OTP_USER:
635 if (mtd->get_user_prot_info)
636 ret = mtd->get_user_prot_info(mtd, buf, 4096);
637 break;
638 }
639 if (ret >= 0) {
640 if (cmd == OTPGETREGIONCOUNT) {
641 int nbr = ret / sizeof(struct otp_info);
642 ret = copy_to_user(argp, &nbr, sizeof(int));
643 } else
644 ret = copy_to_user(argp, buf, ret);
645 if (ret)
646 ret = -EFAULT;
647 }
648 kfree(buf);
649 break;
650 }
651
652 case OTPLOCK:
653 {
654 struct otp_info info;
655
Nicolas Pitre045e9a52005-02-08 19:12:53 +0000656 if (MTD_MODE(file) != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000657 return -EINVAL;
658 if (copy_from_user(&info, argp, sizeof(info)))
659 return -EFAULT;
660 if (!mtd->lock_user_prot_reg)
661 return -EOPNOTSUPP;
662 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
663 break;
664 }
665#endif
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 default:
668 ret = -ENOTTY;
669 }
670
671 return ret;
672} /* memory_ioctl */
673
674static struct file_operations mtd_fops = {
675 .owner = THIS_MODULE,
676 .llseek = mtd_lseek,
677 .read = mtd_read,
678 .write = mtd_write,
679 .ioctl = mtd_ioctl,
680 .open = mtd_open,
681 .release = mtd_close,
682};
683
684static int __init init_mtdchar(void)
685{
686 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
687 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
688 MTD_CHAR_MAJOR);
689 return -EAGAIN;
690 }
691
Todd Poynor9bc7b382005-06-30 01:23:27 +0100692 mtd_class = class_create(THIS_MODULE, "mtd");
693
694 if (IS_ERR(mtd_class)) {
695 printk(KERN_ERR "Error creating mtd class.\n");
696 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
Coywolf Qi Hunt3a7a8822005-07-04 12:15:28 -0500697 return PTR_ERR(mtd_class);
Todd Poynor9bc7b382005-06-30 01:23:27 +0100698 }
699
700 register_mtd_user(&notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return 0;
702}
703
704static void __exit cleanup_mtdchar(void)
705{
Todd Poynor9bc7b382005-06-30 01:23:27 +0100706 unregister_mtd_user(&notifier);
707 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
709}
710
711module_init(init_mtdchar);
712module_exit(cleanup_mtdchar);
713
714
715MODULE_LICENSE("GPL");
716MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
717MODULE_DESCRIPTION("Direct character-device access to MTD devices");