blob: f5061fe72e4cb197a2d2fcc92e6da205de0b01ea [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Character-device access to raw MTD devices.
3 *
4 */
5
Thomas Gleixner15fdc522005-11-07 00:14:42 +01006#include <linux/device.h>
7#include <linux/fs.h>
Andrew Morton0c1eafd2007-08-10 13:01:06 -07008#include <linux/mm.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +03009#include <linux/err.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010010#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/module.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010013#include <linux/slab.h>
14#include <linux/sched.h>
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mtd/mtd.h>
17#include <linux/mtd/compatmac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Thomas Gleixner15fdc522005-11-07 00:14:42 +010019#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010020
21static struct class *mtd_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23static void mtd_notify_add(struct mtd_info* mtd)
24{
25 if (!mtd)
26 return;
27
Tony Jonesa98894a2007-09-25 02:03:03 +020028 device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2), "mtd%d", mtd->index);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000029
Tony Jonesa98894a2007-09-25 02:03:03 +020030 device_create(mtd_class, NULL,
31 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1), "mtd%dro", mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
34static void mtd_notify_remove(struct mtd_info* mtd)
35{
36 if (!mtd)
37 return;
Todd Poynor9bc7b382005-06-30 01:23:27 +010038
Tony Jonesa98894a2007-09-25 02:03:03 +020039 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
40 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
43static struct mtd_notifier notifier = {
44 .add = mtd_notify_add,
45 .remove = mtd_notify_remove,
46};
47
Nicolas Pitre045e9a52005-02-08 19:12:53 +000048/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020049 * Data structure to hold the pointer to the mtd device as well
50 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000051 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020052struct mtd_file_info {
53 struct mtd_info *mtd;
54 enum mtd_file_modes mode;
55};
Nicolas Pitre31f42332005-02-08 17:45:55 +000056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
58{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020059 struct mtd_file_info *mfi = file->private_data;
60 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040063 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040065 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010066 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040068 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010069 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 break;
71 default:
72 return -EINVAL;
73 }
74
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020075 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010076 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Todd Poynor8b491d72005-08-04 02:05:51 +010078 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81
82
83static int mtd_open(struct inode *inode, struct file *file)
84{
85 int minor = iminor(inode);
86 int devnum = minor >> 1;
87 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020088 struct mtd_file_info *mfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
91
92 if (devnum >= MAX_MTD_DEVICES)
93 return -ENODEV;
94
95 /* You can't open the RO devices RW */
96 if ((file->f_mode & 2) && (minor & 1))
97 return -EACCES;
98
99 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000100
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300101 if (IS_ERR(mtd))
102 return PTR_ERR(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 if (MTD_ABSENT == mtd->type) {
105 put_mtd_device(mtd);
106 return -ENODEV;
107 }
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /* You can't open it RW if it's not a writeable device */
110 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
111 put_mtd_device(mtd);
112 return -EACCES;
113 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000114
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200115 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
116 if (!mfi) {
117 put_mtd_device(mtd);
118 return -ENOMEM;
119 }
120 mfi->mtd = mtd;
121 file->private_data = mfi;
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return 0;
124} /* mtd_open */
125
126/*====================================================================*/
127
128static int mtd_close(struct inode *inode, struct file *file)
129{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200130 struct mtd_file_info *mfi = file->private_data;
131 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
134
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200135 /* Only sync if opened RW */
136 if ((file->f_mode & 2) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200140 file->private_data = NULL;
141 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 return 0;
144} /* mtd_close */
145
146/* FIXME: This _really_ needs to die. In 2.5, we should lock the
147 userspace buffer down and use it directly with readv/writev.
148*/
149#define MAX_KMALLOC_SIZE 0x20000
150
151static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
152{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200153 struct mtd_file_info *mfi = file->private_data;
154 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 size_t retlen=0;
156 size_t total_retlen=0;
157 int ret=0;
158 int len;
159 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
162
163 if (*ppos + count > mtd->size)
164 count = mtd->size - *ppos;
165
166 if (!count)
167 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
170 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100171
172 if (count > MAX_KMALLOC_SIZE)
173 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
174 else
175 kbuf=kmalloc(count, GFP_KERNEL);
176
177 if (!kbuf)
178 return -ENOMEM;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100181
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000182 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 len = MAX_KMALLOC_SIZE;
184 else
185 len = count;
186
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200187 switch (mfi->mode) {
188 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000189 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
190 break;
191 case MTD_MODE_OTP_USER:
192 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
193 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200194 case MTD_MODE_RAW:
195 {
196 struct mtd_oob_ops ops;
197
198 ops.mode = MTD_OOB_RAW;
199 ops.datbuf = kbuf;
200 ops.oobbuf = NULL;
201 ops.len = len;
202
203 ret = mtd->read_oob(mtd, *ppos, &ops);
204 retlen = ops.retlen;
205 break;
206 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000207 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200208 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 /* Nand returns -EBADMSG on ecc errors, but it returns
211 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000212 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200213 * For kernel internal usage it also might return -EUCLEAN
214 * to signal the caller that a bitflip has occured and has
215 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 * Userspace software which accesses NAND this way
217 * must be aware of the fact that it deals with NAND
218 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200219 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 *ppos += retlen;
221 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200222 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return -EFAULT;
224 }
225 else
226 total_retlen += retlen;
227
228 count -= retlen;
229 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000230 if (retlen == 0)
231 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233 else {
234 kfree(kbuf);
235 return ret;
236 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
Thago Galesib802c072006-04-17 17:38:15 +0100240 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return total_retlen;
242} /* mtd_read */
243
244static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
245{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200246 struct mtd_file_info *mfi = file->private_data;
247 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 char *kbuf;
249 size_t retlen;
250 size_t total_retlen=0;
251 int ret=0;
252 int len;
253
254 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (*ppos == mtd->size)
257 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (*ppos + count > mtd->size)
260 count = mtd->size - *ppos;
261
262 if (!count)
263 return 0;
264
Thago Galesib802c072006-04-17 17:38:15 +0100265 if (count > MAX_KMALLOC_SIZE)
266 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
267 else
268 kbuf=kmalloc(count, GFP_KERNEL);
269
270 if (!kbuf)
271 return -ENOMEM;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100274
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000275 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 len = MAX_KMALLOC_SIZE;
277 else
278 len = count;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (copy_from_user(kbuf, buf, len)) {
281 kfree(kbuf);
282 return -EFAULT;
283 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000284
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200285 switch (mfi->mode) {
286 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000287 ret = -EROFS;
288 break;
289 case MTD_MODE_OTP_USER:
290 if (!mtd->write_user_prot_reg) {
291 ret = -EOPNOTSUPP;
292 break;
293 }
294 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
295 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200296
297 case MTD_MODE_RAW:
298 {
299 struct mtd_oob_ops ops;
300
301 ops.mode = MTD_OOB_RAW;
302 ops.datbuf = kbuf;
303 ops.oobbuf = NULL;
304 ops.len = len;
305
306 ret = mtd->write_oob(mtd, *ppos, &ops);
307 retlen = ops.retlen;
308 break;
309 }
310
Nicolas Pitre31f42332005-02-08 17:45:55 +0000311 default:
312 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (!ret) {
315 *ppos += retlen;
316 total_retlen += retlen;
317 count -= retlen;
318 buf += retlen;
319 }
320 else {
321 kfree(kbuf);
322 return ret;
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
Thago Galesib802c072006-04-17 17:38:15 +0100326 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return total_retlen;
328} /* mtd_write */
329
330/*======================================================================
331
332 IOCTL calls for getting device parameters.
333
334======================================================================*/
335static void mtdchar_erase_callback (struct erase_info *instr)
336{
337 wake_up((wait_queue_head_t *)instr->priv);
338}
339
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200340#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
341static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
342{
343 struct mtd_info *mtd = mfi->mtd;
344 int ret = 0;
345
346 switch (mode) {
347 case MTD_OTP_FACTORY:
348 if (!mtd->read_fact_prot_reg)
349 ret = -EOPNOTSUPP;
350 else
351 mfi->mode = MTD_MODE_OTP_FACTORY;
352 break;
353 case MTD_OTP_USER:
354 if (!mtd->read_fact_prot_reg)
355 ret = -EOPNOTSUPP;
356 else
357 mfi->mode = MTD_MODE_OTP_USER;
358 break;
359 default:
360 ret = -EINVAL;
361 case MTD_OTP_OFF:
362 break;
363 }
364 return ret;
365}
366#else
367# define otp_select_filemode(f,m) -EOPNOTSUPP
368#endif
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370static int mtd_ioctl(struct inode *inode, struct file *file,
371 u_int cmd, u_long arg)
372{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200373 struct mtd_file_info *mfi = file->private_data;
374 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 void __user *argp = (void __user *)arg;
376 int ret = 0;
377 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200378 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
381
382 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
383 if (cmd & IOC_IN) {
384 if (!access_ok(VERIFY_READ, argp, size))
385 return -EFAULT;
386 }
387 if (cmd & IOC_OUT) {
388 if (!access_ok(VERIFY_WRITE, argp, size))
389 return -EFAULT;
390 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 switch (cmd) {
393 case MEMGETREGIONCOUNT:
394 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
395 return -EFAULT;
396 break;
397
398 case MEMGETREGIONINFO:
399 {
400 struct region_info_user ur;
401
402 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
403 return -EFAULT;
404
405 if (ur.regionindex >= mtd->numeraseregions)
406 return -EINVAL;
407 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
408 sizeof(struct mtd_erase_region_info)))
409 return -EFAULT;
410 break;
411 }
412
413 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200414 info.type = mtd->type;
415 info.flags = mtd->flags;
416 info.size = mtd->size;
417 info.erasesize = mtd->erasesize;
418 info.writesize = mtd->writesize;
419 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200420 /* The below fields are obsolete */
421 info.ecctype = -1;
422 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200423 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -EFAULT;
425 break;
426
427 case MEMERASE:
428 {
429 struct erase_info *erase;
430
431 if(!(file->f_mode & 2))
432 return -EPERM;
433
Burman Yan95b93a02006-11-15 21:10:29 +0200434 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!erase)
436 ret = -ENOMEM;
437 else {
438 wait_queue_head_t waitq;
439 DECLARE_WAITQUEUE(wait, current);
440
441 init_waitqueue_head(&waitq);
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (copy_from_user(&erase->addr, argp,
444 sizeof(struct erase_info_user))) {
445 kfree(erase);
446 return -EFAULT;
447 }
448 erase->mtd = mtd;
449 erase->callback = mtdchar_erase_callback;
450 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /*
453 FIXME: Allow INTERRUPTIBLE. Which means
454 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 If the wq_head is on the stack, and we
457 leave because we got interrupted, then the
458 wq_head is no longer there when the
459 callback routine tries to wake us up.
460 */
461 ret = mtd->erase(mtd, erase);
462 if (!ret) {
463 set_current_state(TASK_UNINTERRUPTIBLE);
464 add_wait_queue(&waitq, &wait);
465 if (erase->state != MTD_ERASE_DONE &&
466 erase->state != MTD_ERASE_FAILED)
467 schedule();
468 remove_wait_queue(&waitq, &wait);
469 set_current_state(TASK_RUNNING);
470
471 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
472 }
473 kfree(erase);
474 }
475 break;
476 }
477
478 case MEMWRITEOOB:
479 {
480 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200481 struct mtd_oob_ops ops;
Harvey Harrison5f692832008-07-03 23:40:13 -0700482 struct mtd_oob_buf __user *user_buf = argp;
David Scidmoree9d8d482007-12-11 17:44:30 -0600483 uint32_t retlen;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if(!(file->f_mode & 2))
486 return -EPERM;
487
488 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
489 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000490
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200491 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return -EINVAL;
493
494 if (!mtd->write_oob)
495 ret = -EOPNOTSUPP;
496 else
497 ret = access_ok(VERIFY_READ, buf.ptr,
498 buf.length) ? 0 : EFAULT;
499
500 if (ret)
501 return ret;
502
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200503 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200504 ops.ooboffs = buf.start & (mtd->oobsize - 1);
505 ops.datbuf = NULL;
506 ops.mode = MTD_OOB_PLACE;
507
Vitaly Wool70145682006-11-03 18:20:38 +0300508 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200509 return -EINVAL;
510
511 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
512 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000514
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200515 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
516 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 return -EFAULT;
518 }
519
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200520 buf.start &= ~(mtd->oobsize - 1);
521 ret = mtd->write_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
David Scidmoree9d8d482007-12-11 17:44:30 -0600523 if (ops.oobretlen > 0xFFFFFFFFU)
524 ret = -EOVERFLOW;
525 retlen = ops.oobretlen;
Harvey Harrison5f692832008-07-03 23:40:13 -0700526 if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ret = -EFAULT;
528
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200529 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 break;
531
532 }
533
534 case MEMREADOOB:
535 {
536 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200537 struct mtd_oob_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
539 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
540 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000541
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200542 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -EINVAL;
544
545 if (!mtd->read_oob)
546 ret = -EOPNOTSUPP;
547 else
548 ret = access_ok(VERIFY_WRITE, buf.ptr,
549 buf.length) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (ret)
551 return ret;
552
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200553 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200554 ops.ooboffs = buf.start & (mtd->oobsize - 1);
555 ops.datbuf = NULL;
556 ops.mode = MTD_OOB_PLACE;
557
Thomas Gleixner408b4832007-04-13 19:50:48 +0200558 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200559 return -EINVAL;
560
561 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
562 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000564
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200565 buf.start &= ~(mtd->oobsize - 1);
566 ret = mtd->read_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Vitaly Wool70145682006-11-03 18:20:38 +0300568 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 ret = -EFAULT;
Vitaly Wool70145682006-11-03 18:20:38 +0300570 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
571 ops.oobretlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 ret = -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000573
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200574 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 break;
576 }
577
578 case MEMLOCK:
579 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700580 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Harvey Harrison175428b2008-07-03 23:40:14 -0700582 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EFAULT;
584
585 if (!mtd->lock)
586 ret = -EOPNOTSUPP;
587 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700588 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 break;
590 }
591
592 case MEMUNLOCK:
593 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700594 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Harvey Harrison175428b2008-07-03 23:40:14 -0700596 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -EFAULT;
598
599 if (!mtd->unlock)
600 ret = -EOPNOTSUPP;
601 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700602 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 break;
604 }
605
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200606 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 case MEMGETOOBSEL:
608 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200609 struct nand_oobinfo oi;
610
611 if (!mtd->ecclayout)
612 return -EOPNOTSUPP;
613 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
614 return -EINVAL;
615
616 oi.useecc = MTD_NANDECC_AUTOPLACE;
617 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
618 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
619 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200620 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200621
622 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 return -EFAULT;
624 break;
625 }
626
627 case MEMGETBADBLOCK:
628 {
629 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (copy_from_user(&offs, argp, sizeof(loff_t)))
632 return -EFAULT;
633 if (!mtd->block_isbad)
634 ret = -EOPNOTSUPP;
635 else
636 return mtd->block_isbad(mtd, offs);
637 break;
638 }
639
640 case MEMSETBADBLOCK:
641 {
642 loff_t offs;
643
644 if (copy_from_user(&offs, argp, sizeof(loff_t)))
645 return -EFAULT;
646 if (!mtd->block_markbad)
647 ret = -EOPNOTSUPP;
648 else
649 return mtd->block_markbad(mtd, offs);
650 break;
651 }
652
Kyungmin Park493c6462006-05-12 17:03:07 +0300653#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000654 case OTPSELECT:
655 {
656 int mode;
657 if (copy_from_user(&mode, argp, sizeof(int)))
658 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200659
660 mfi->mode = MTD_MODE_NORMAL;
661
662 ret = otp_select_filemode(mfi, mode);
663
Nicolas Pitre81dba482005-04-01 16:36:15 +0100664 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000665 break;
666 }
667
668 case OTPGETREGIONCOUNT:
669 case OTPGETREGIONINFO:
670 {
671 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
672 if (!buf)
673 return -ENOMEM;
674 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200675 switch (mfi->mode) {
676 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000677 if (mtd->get_fact_prot_info)
678 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
679 break;
680 case MTD_MODE_OTP_USER:
681 if (mtd->get_user_prot_info)
682 ret = mtd->get_user_prot_info(mtd, buf, 4096);
683 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200684 default:
685 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000686 }
687 if (ret >= 0) {
688 if (cmd == OTPGETREGIONCOUNT) {
689 int nbr = ret / sizeof(struct otp_info);
690 ret = copy_to_user(argp, &nbr, sizeof(int));
691 } else
692 ret = copy_to_user(argp, buf, ret);
693 if (ret)
694 ret = -EFAULT;
695 }
696 kfree(buf);
697 break;
698 }
699
700 case OTPLOCK:
701 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700702 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000703
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200704 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000705 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700706 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000707 return -EFAULT;
708 if (!mtd->lock_user_prot_reg)
709 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700710 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000711 break;
712 }
713#endif
714
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200715 case ECCGETLAYOUT:
716 {
717 if (!mtd->ecclayout)
718 return -EOPNOTSUPP;
719
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200720 if (copy_to_user(argp, mtd->ecclayout,
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200721 sizeof(struct nand_ecclayout)))
722 return -EFAULT;
723 break;
724 }
725
726 case ECCGETSTATS:
727 {
728 if (copy_to_user(argp, &mtd->ecc_stats,
729 sizeof(struct mtd_ecc_stats)))
730 return -EFAULT;
731 break;
732 }
733
734 case MTDFILEMODE:
735 {
736 mfi->mode = 0;
737
738 switch(arg) {
739 case MTD_MODE_OTP_FACTORY:
740 case MTD_MODE_OTP_USER:
741 ret = otp_select_filemode(mfi, arg);
742 break;
743
744 case MTD_MODE_RAW:
745 if (!mtd->read_oob || !mtd->write_oob)
746 return -EOPNOTSUPP;
747 mfi->mode = arg;
748
749 case MTD_MODE_NORMAL:
750 break;
751 default:
752 ret = -EINVAL;
753 }
754 file->f_pos = 0;
755 break;
756 }
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 default:
759 ret = -ENOTTY;
760 }
761
762 return ret;
763} /* memory_ioctl */
764
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800765static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 .owner = THIS_MODULE,
767 .llseek = mtd_lseek,
768 .read = mtd_read,
769 .write = mtd_write,
770 .ioctl = mtd_ioctl,
771 .open = mtd_open,
772 .release = mtd_close,
773};
774
775static int __init init_mtdchar(void)
776{
777 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
778 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
779 MTD_CHAR_MAJOR);
780 return -EAGAIN;
781 }
782
Todd Poynor9bc7b382005-06-30 01:23:27 +0100783 mtd_class = class_create(THIS_MODULE, "mtd");
784
785 if (IS_ERR(mtd_class)) {
786 printk(KERN_ERR "Error creating mtd class.\n");
787 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
Coywolf Qi Hunt3a7a8822005-07-04 12:15:28 -0500788 return PTR_ERR(mtd_class);
Todd Poynor9bc7b382005-06-30 01:23:27 +0100789 }
790
791 register_mtd_user(&notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return 0;
793}
794
795static void __exit cleanup_mtdchar(void)
796{
Todd Poynor9bc7b382005-06-30 01:23:27 +0100797 unregister_mtd_user(&notifier);
798 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
800}
801
802module_init(init_mtdchar);
803module_exit(cleanup_mtdchar);
804
805
806MODULE_LICENSE("GPL");
807MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
808MODULE_DESCRIPTION("Direct character-device access to MTD devices");