blob: 942c88ec5b6a71ded4a4e55507e48774263308cc [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
Thomas Gleixner15fdc522005-11-07 00:14:42 +01008#include <linux/device.h>
9#include <linux/fs.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +030010#include <linux/err.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010011#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) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040066 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040068 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010069 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040071 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010072 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 break;
74 default:
75 return -EINVAL;
76 }
77
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020078 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010079 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Todd Poynor8b491d72005-08-04 02:05:51 +010081 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84
85
86static int mtd_open(struct inode *inode, struct file *file)
87{
88 int minor = iminor(inode);
89 int devnum = minor >> 1;
90 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020091 struct mtd_file_info *mfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
94
95 if (devnum >= MAX_MTD_DEVICES)
96 return -ENODEV;
97
98 /* You can't open the RO devices RW */
99 if ((file->f_mode & 2) && (minor & 1))
100 return -EACCES;
101
102 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000103
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300104 if (IS_ERR(mtd))
105 return PTR_ERR(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (MTD_ABSENT == mtd->type) {
108 put_mtd_device(mtd);
109 return -ENODEV;
110 }
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* You can't open it RW if it's not a writeable device */
113 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
114 put_mtd_device(mtd);
115 return -EACCES;
116 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000117
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200118 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
119 if (!mfi) {
120 put_mtd_device(mtd);
121 return -ENOMEM;
122 }
123 mfi->mtd = mtd;
124 file->private_data = mfi;
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 0;
127} /* mtd_open */
128
129/*====================================================================*/
130
131static int mtd_close(struct inode *inode, struct file *file)
132{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200133 struct mtd_file_info *mfi = file->private_data;
134 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
137
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200138 /* Only sync if opened RW */
139 if ((file->f_mode & 2) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200143 file->private_data = NULL;
144 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
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{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200156 struct mtd_file_info *mfi = file->private_data;
157 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 size_t retlen=0;
159 size_t total_retlen=0;
160 int ret=0;
161 int len;
162 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
165
166 if (*ppos + count > mtd->size)
167 count = mtd->size - *ppos;
168
169 if (!count)
170 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
173 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100174
175 if (count > MAX_KMALLOC_SIZE)
176 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
177 else
178 kbuf=kmalloc(count, GFP_KERNEL);
179
180 if (!kbuf)
181 return -ENOMEM;
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100184
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000185 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 len = MAX_KMALLOC_SIZE;
187 else
188 len = count;
189
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200190 switch (mfi->mode) {
191 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000192 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
193 break;
194 case MTD_MODE_OTP_USER:
195 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
196 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200197 case MTD_MODE_RAW:
198 {
199 struct mtd_oob_ops ops;
200
201 ops.mode = MTD_OOB_RAW;
202 ops.datbuf = kbuf;
203 ops.oobbuf = NULL;
204 ops.len = len;
205
206 ret = mtd->read_oob(mtd, *ppos, &ops);
207 retlen = ops.retlen;
208 break;
209 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000210 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200211 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Nand returns -EBADMSG on ecc errors, but it returns
214 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000215 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200216 * For kernel internal usage it also might return -EUCLEAN
217 * to signal the caller that a bitflip has occured and has
218 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 * Userspace software which accesses NAND this way
220 * must be aware of the fact that it deals with NAND
221 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200222 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 *ppos += retlen;
224 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200225 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return -EFAULT;
227 }
228 else
229 total_retlen += retlen;
230
231 count -= retlen;
232 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000233 if (retlen == 0)
234 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 else {
237 kfree(kbuf);
238 return ret;
239 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
Thago Galesib802c072006-04-17 17:38:15 +0100243 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return total_retlen;
245} /* mtd_read */
246
247static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
248{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200249 struct mtd_file_info *mfi = file->private_data;
250 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 char *kbuf;
252 size_t retlen;
253 size_t total_retlen=0;
254 int ret=0;
255 int len;
256
257 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (*ppos == mtd->size)
260 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (*ppos + count > mtd->size)
263 count = mtd->size - *ppos;
264
265 if (!count)
266 return 0;
267
Thago Galesib802c072006-04-17 17:38:15 +0100268 if (count > MAX_KMALLOC_SIZE)
269 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
270 else
271 kbuf=kmalloc(count, GFP_KERNEL);
272
273 if (!kbuf)
274 return -ENOMEM;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100277
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000278 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 len = MAX_KMALLOC_SIZE;
280 else
281 len = count;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (copy_from_user(kbuf, buf, len)) {
284 kfree(kbuf);
285 return -EFAULT;
286 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000287
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200288 switch (mfi->mode) {
289 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000290 ret = -EROFS;
291 break;
292 case MTD_MODE_OTP_USER:
293 if (!mtd->write_user_prot_reg) {
294 ret = -EOPNOTSUPP;
295 break;
296 }
297 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
298 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200299
300 case MTD_MODE_RAW:
301 {
302 struct mtd_oob_ops ops;
303
304 ops.mode = MTD_OOB_RAW;
305 ops.datbuf = kbuf;
306 ops.oobbuf = NULL;
307 ops.len = len;
308
309 ret = mtd->write_oob(mtd, *ppos, &ops);
310 retlen = ops.retlen;
311 break;
312 }
313
Nicolas Pitre31f42332005-02-08 17:45:55 +0000314 default:
315 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!ret) {
318 *ppos += retlen;
319 total_retlen += retlen;
320 count -= retlen;
321 buf += retlen;
322 }
323 else {
324 kfree(kbuf);
325 return ret;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
Thago Galesib802c072006-04-17 17:38:15 +0100329 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return total_retlen;
331} /* mtd_write */
332
333/*======================================================================
334
335 IOCTL calls for getting device parameters.
336
337======================================================================*/
338static void mtdchar_erase_callback (struct erase_info *instr)
339{
340 wake_up((wait_queue_head_t *)instr->priv);
341}
342
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200343#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
344static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
345{
346 struct mtd_info *mtd = mfi->mtd;
347 int ret = 0;
348
349 switch (mode) {
350 case MTD_OTP_FACTORY:
351 if (!mtd->read_fact_prot_reg)
352 ret = -EOPNOTSUPP;
353 else
354 mfi->mode = MTD_MODE_OTP_FACTORY;
355 break;
356 case MTD_OTP_USER:
357 if (!mtd->read_fact_prot_reg)
358 ret = -EOPNOTSUPP;
359 else
360 mfi->mode = MTD_MODE_OTP_USER;
361 break;
362 default:
363 ret = -EINVAL;
364 case MTD_OTP_OFF:
365 break;
366 }
367 return ret;
368}
369#else
370# define otp_select_filemode(f,m) -EOPNOTSUPP
371#endif
372
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373static int mtd_ioctl(struct inode *inode, struct file *file,
374 u_int cmd, u_long arg)
375{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200376 struct mtd_file_info *mfi = file->private_data;
377 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 void __user *argp = (void __user *)arg;
379 int ret = 0;
380 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200381 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
384
385 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
386 if (cmd & IOC_IN) {
387 if (!access_ok(VERIFY_READ, argp, size))
388 return -EFAULT;
389 }
390 if (cmd & IOC_OUT) {
391 if (!access_ok(VERIFY_WRITE, argp, size))
392 return -EFAULT;
393 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 switch (cmd) {
396 case MEMGETREGIONCOUNT:
397 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
398 return -EFAULT;
399 break;
400
401 case MEMGETREGIONINFO:
402 {
403 struct region_info_user ur;
404
405 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
406 return -EFAULT;
407
408 if (ur.regionindex >= mtd->numeraseregions)
409 return -EINVAL;
410 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
411 sizeof(struct mtd_erase_region_info)))
412 return -EFAULT;
413 break;
414 }
415
416 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200417 info.type = mtd->type;
418 info.flags = mtd->flags;
419 info.size = mtd->size;
420 info.erasesize = mtd->erasesize;
421 info.writesize = mtd->writesize;
422 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200423 /* The below fields are obsolete */
424 info.ecctype = -1;
425 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200426 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 return -EFAULT;
428 break;
429
430 case MEMERASE:
431 {
432 struct erase_info *erase;
433
434 if(!(file->f_mode & 2))
435 return -EPERM;
436
Burman Yan95b93a02006-11-15 21:10:29 +0200437 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!erase)
439 ret = -ENOMEM;
440 else {
441 wait_queue_head_t waitq;
442 DECLARE_WAITQUEUE(wait, current);
443
444 init_waitqueue_head(&waitq);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (copy_from_user(&erase->addr, argp,
447 sizeof(struct erase_info_user))) {
448 kfree(erase);
449 return -EFAULT;
450 }
451 erase->mtd = mtd;
452 erase->callback = mtdchar_erase_callback;
453 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 /*
456 FIXME: Allow INTERRUPTIBLE. Which means
457 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 If the wq_head is on the stack, and we
460 leave because we got interrupted, then the
461 wq_head is no longer there when the
462 callback routine tries to wake us up.
463 */
464 ret = mtd->erase(mtd, erase);
465 if (!ret) {
466 set_current_state(TASK_UNINTERRUPTIBLE);
467 add_wait_queue(&waitq, &wait);
468 if (erase->state != MTD_ERASE_DONE &&
469 erase->state != MTD_ERASE_FAILED)
470 schedule();
471 remove_wait_queue(&waitq, &wait);
472 set_current_state(TASK_RUNNING);
473
474 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
475 }
476 kfree(erase);
477 }
478 break;
479 }
480
481 case MEMWRITEOOB:
482 {
483 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200484 struct mtd_oob_ops ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if(!(file->f_mode & 2))
487 return -EPERM;
488
489 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
490 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000491
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200492 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return -EINVAL;
494
495 if (!mtd->write_oob)
496 ret = -EOPNOTSUPP;
497 else
498 ret = access_ok(VERIFY_READ, buf.ptr,
499 buf.length) ? 0 : EFAULT;
500
501 if (ret)
502 return ret;
503
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200504 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200505 ops.ooboffs = buf.start & (mtd->oobsize - 1);
506 ops.datbuf = NULL;
507 ops.mode = MTD_OOB_PLACE;
508
Vitaly Wool70145682006-11-03 18:20:38 +0300509 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200510 return -EINVAL;
511
512 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
513 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000515
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200516 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
517 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -EFAULT;
519 }
520
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200521 buf.start &= ~(mtd->oobsize - 1);
522 ret = mtd->write_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Vitaly Wool70145682006-11-03 18:20:38 +0300524 if (copy_to_user(argp + sizeof(uint32_t), &ops.oobretlen,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200525 sizeof(uint32_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 ret = -EFAULT;
527
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200528 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 break;
530
531 }
532
533 case MEMREADOOB:
534 {
535 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200536 struct mtd_oob_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
539 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000540
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200541 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return -EINVAL;
543
544 if (!mtd->read_oob)
545 ret = -EOPNOTSUPP;
546 else
547 ret = access_ok(VERIFY_WRITE, buf.ptr,
548 buf.length) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 if (ret)
550 return ret;
551
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200552 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200553 ops.ooboffs = buf.start & (mtd->oobsize - 1);
554 ops.datbuf = NULL;
555 ops.mode = MTD_OOB_PLACE;
556
Thomas Gleixner408b4832007-04-13 19:50:48 +0200557 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200558 return -EINVAL;
559
560 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
561 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000563
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200564 buf.start &= ~(mtd->oobsize - 1);
565 ret = mtd->read_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Vitaly Wool70145682006-11-03 18:20:38 +0300567 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 ret = -EFAULT;
Vitaly Wool70145682006-11-03 18:20:38 +0300569 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
570 ops.oobretlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ret = -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000572
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200573 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
575 }
576
577 case MEMLOCK:
578 {
579 struct erase_info_user info;
580
581 if (copy_from_user(&info, argp, sizeof(info)))
582 return -EFAULT;
583
584 if (!mtd->lock)
585 ret = -EOPNOTSUPP;
586 else
587 ret = mtd->lock(mtd, info.start, info.length);
588 break;
589 }
590
591 case MEMUNLOCK:
592 {
593 struct erase_info_user info;
594
595 if (copy_from_user(&info, argp, sizeof(info)))
596 return -EFAULT;
597
598 if (!mtd->unlock)
599 ret = -EOPNOTSUPP;
600 else
601 ret = mtd->unlock(mtd, info.start, info.length);
602 break;
603 }
604
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200605 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 case MEMGETOOBSEL:
607 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200608 struct nand_oobinfo oi;
609
610 if (!mtd->ecclayout)
611 return -EOPNOTSUPP;
612 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
613 return -EINVAL;
614
615 oi.useecc = MTD_NANDECC_AUTOPLACE;
616 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
617 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
618 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200619 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200620
621 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 return -EFAULT;
623 break;
624 }
625
626 case MEMGETBADBLOCK:
627 {
628 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (copy_from_user(&offs, argp, sizeof(loff_t)))
631 return -EFAULT;
632 if (!mtd->block_isbad)
633 ret = -EOPNOTSUPP;
634 else
635 return mtd->block_isbad(mtd, offs);
636 break;
637 }
638
639 case MEMSETBADBLOCK:
640 {
641 loff_t offs;
642
643 if (copy_from_user(&offs, argp, sizeof(loff_t)))
644 return -EFAULT;
645 if (!mtd->block_markbad)
646 ret = -EOPNOTSUPP;
647 else
648 return mtd->block_markbad(mtd, offs);
649 break;
650 }
651
Kyungmin Park493c6462006-05-12 17:03:07 +0300652#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000653 case OTPSELECT:
654 {
655 int mode;
656 if (copy_from_user(&mode, argp, sizeof(int)))
657 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200658
659 mfi->mode = MTD_MODE_NORMAL;
660
661 ret = otp_select_filemode(mfi, mode);
662
Nicolas Pitre81dba482005-04-01 16:36:15 +0100663 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000664 break;
665 }
666
667 case OTPGETREGIONCOUNT:
668 case OTPGETREGIONINFO:
669 {
670 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
671 if (!buf)
672 return -ENOMEM;
673 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200674 switch (mfi->mode) {
675 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000676 if (mtd->get_fact_prot_info)
677 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
678 break;
679 case MTD_MODE_OTP_USER:
680 if (mtd->get_user_prot_info)
681 ret = mtd->get_user_prot_info(mtd, buf, 4096);
682 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200683 default:
684 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000685 }
686 if (ret >= 0) {
687 if (cmd == OTPGETREGIONCOUNT) {
688 int nbr = ret / sizeof(struct otp_info);
689 ret = copy_to_user(argp, &nbr, sizeof(int));
690 } else
691 ret = copy_to_user(argp, buf, ret);
692 if (ret)
693 ret = -EFAULT;
694 }
695 kfree(buf);
696 break;
697 }
698
699 case OTPLOCK:
700 {
701 struct otp_info info;
702
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200703 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000704 return -EINVAL;
705 if (copy_from_user(&info, argp, sizeof(info)))
706 return -EFAULT;
707 if (!mtd->lock_user_prot_reg)
708 return -EOPNOTSUPP;
709 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
710 break;
711 }
712#endif
713
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200714 case ECCGETLAYOUT:
715 {
716 if (!mtd->ecclayout)
717 return -EOPNOTSUPP;
718
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200719 if (copy_to_user(argp, mtd->ecclayout,
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200720 sizeof(struct nand_ecclayout)))
721 return -EFAULT;
722 break;
723 }
724
725 case ECCGETSTATS:
726 {
727 if (copy_to_user(argp, &mtd->ecc_stats,
728 sizeof(struct mtd_ecc_stats)))
729 return -EFAULT;
730 break;
731 }
732
733 case MTDFILEMODE:
734 {
735 mfi->mode = 0;
736
737 switch(arg) {
738 case MTD_MODE_OTP_FACTORY:
739 case MTD_MODE_OTP_USER:
740 ret = otp_select_filemode(mfi, arg);
741 break;
742
743 case MTD_MODE_RAW:
744 if (!mtd->read_oob || !mtd->write_oob)
745 return -EOPNOTSUPP;
746 mfi->mode = arg;
747
748 case MTD_MODE_NORMAL:
749 break;
750 default:
751 ret = -EINVAL;
752 }
753 file->f_pos = 0;
754 break;
755 }
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 default:
758 ret = -ENOTTY;
759 }
760
761 return ret;
762} /* memory_ioctl */
763
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800764static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 .owner = THIS_MODULE,
766 .llseek = mtd_lseek,
767 .read = mtd_read,
768 .write = mtd_write,
769 .ioctl = mtd_ioctl,
770 .open = mtd_open,
771 .release = mtd_close,
772};
773
774static int __init init_mtdchar(void)
775{
776 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
777 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
778 MTD_CHAR_MAJOR);
779 return -EAGAIN;
780 }
781
Todd Poynor9bc7b382005-06-30 01:23:27 +0100782 mtd_class = class_create(THIS_MODULE, "mtd");
783
784 if (IS_ERR(mtd_class)) {
785 printk(KERN_ERR "Error creating mtd class.\n");
786 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
Coywolf Qi Hunt3a7a8822005-07-04 12:15:28 -0500787 return PTR_ERR(mtd_class);
Todd Poynor9bc7b382005-06-30 01:23:27 +0100788 }
789
790 register_mtd_user(&notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 return 0;
792}
793
794static void __exit cleanup_mtdchar(void)
795{
Todd Poynor9bc7b382005-06-30 01:23:27 +0100796 unregister_mtd_user(&notifier);
797 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
799}
800
801module_init(init_mtdchar);
802module_exit(cleanup_mtdchar);
803
804
805MODULE_LICENSE("GPL");
806MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
807MODULE_DESCRIPTION("Direct character-device access to MTD devices");