blob: 5d3ac512ce169f1f4efc8de2cde7ae6ca21c6148 [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>
Andrew Morton0c1eafd2007-08-10 13:01:06 -070010#include <linux/mm.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +030011#include <linux/err.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010012#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/module.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010015#include <linux/slab.h>
16#include <linux/sched.h>
17
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mtd/mtd.h>
19#include <linux/mtd/compatmac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Thomas Gleixner15fdc522005-11-07 00:14:42 +010021#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010022
23static struct class *mtd_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static void mtd_notify_add(struct mtd_info* mtd)
26{
27 if (!mtd)
28 return;
29
Tony Jonesa98894a2007-09-25 02:03:03 +020030 device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2), "mtd%d", mtd->index);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000031
Tony Jonesa98894a2007-09-25 02:03:03 +020032 device_create(mtd_class, NULL,
33 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1), "mtd%dro", mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034}
35
36static void mtd_notify_remove(struct mtd_info* mtd)
37{
38 if (!mtd)
39 return;
Todd Poynor9bc7b382005-06-30 01:23:27 +010040
Tony Jonesa98894a2007-09-25 02:03:03 +020041 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
42 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45static struct mtd_notifier notifier = {
46 .add = mtd_notify_add,
47 .remove = mtd_notify_remove,
48};
49
Nicolas Pitre045e9a52005-02-08 19:12:53 +000050/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020051 * Data structure to hold the pointer to the mtd device as well
52 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000053 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020054struct mtd_file_info {
55 struct mtd_info *mtd;
56 enum mtd_file_modes mode;
57};
Nicolas Pitre31f42332005-02-08 17:45:55 +000058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
60{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020061 struct mtd_file_info *mfi = file->private_data;
62 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040065 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040067 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010068 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040070 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010071 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 break;
73 default:
74 return -EINVAL;
75 }
76
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020077 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010078 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Todd Poynor8b491d72005-08-04 02:05:51 +010080 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
83
84
85static int mtd_open(struct inode *inode, struct file *file)
86{
87 int minor = iminor(inode);
88 int devnum = minor >> 1;
89 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020090 struct mtd_file_info *mfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
93
94 if (devnum >= MAX_MTD_DEVICES)
95 return -ENODEV;
96
97 /* You can't open the RO devices RW */
98 if ((file->f_mode & 2) && (minor & 1))
99 return -EACCES;
100
101 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000102
Artem Bityutskiy9c740342006-10-11 14:52:47 +0300103 if (IS_ERR(mtd))
104 return PTR_ERR(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (MTD_ABSENT == mtd->type) {
107 put_mtd_device(mtd);
108 return -ENODEV;
109 }
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* You can't open it RW if it's not a writeable device */
112 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
113 put_mtd_device(mtd);
114 return -EACCES;
115 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000116
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200117 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
118 if (!mfi) {
119 put_mtd_device(mtd);
120 return -ENOMEM;
121 }
122 mfi->mtd = mtd;
123 file->private_data = mfi;
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126} /* mtd_open */
127
128/*====================================================================*/
129
130static int mtd_close(struct inode *inode, struct file *file)
131{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200132 struct mtd_file_info *mfi = file->private_data;
133 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
136
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200137 /* Only sync if opened RW */
138 if ((file->f_mode & 2) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200142 file->private_data = NULL;
143 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 return 0;
146} /* mtd_close */
147
148/* FIXME: This _really_ needs to die. In 2.5, we should lock the
149 userspace buffer down and use it directly with readv/writev.
150*/
151#define MAX_KMALLOC_SIZE 0x20000
152
153static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
154{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200155 struct mtd_file_info *mfi = file->private_data;
156 struct mtd_info *mtd = mfi->mtd;
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
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200189 switch (mfi->mode) {
190 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000191 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;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200196 case MTD_MODE_RAW:
197 {
198 struct mtd_oob_ops ops;
199
200 ops.mode = MTD_OOB_RAW;
201 ops.datbuf = kbuf;
202 ops.oobbuf = NULL;
203 ops.len = len;
204
205 ret = mtd->read_oob(mtd, *ppos, &ops);
206 retlen = ops.retlen;
207 break;
208 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000209 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200210 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 /* Nand returns -EBADMSG on ecc errors, but it returns
213 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000214 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200215 * For kernel internal usage it also might return -EUCLEAN
216 * to signal the caller that a bitflip has occured and has
217 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 * Userspace software which accesses NAND this way
219 * must be aware of the fact that it deals with NAND
220 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200221 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 *ppos += retlen;
223 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200224 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 return -EFAULT;
226 }
227 else
228 total_retlen += retlen;
229
230 count -= retlen;
231 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000232 if (retlen == 0)
233 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 else {
236 kfree(kbuf);
237 return ret;
238 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
Thago Galesib802c072006-04-17 17:38:15 +0100242 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return total_retlen;
244} /* mtd_read */
245
246static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
247{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200248 struct mtd_file_info *mfi = file->private_data;
249 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 char *kbuf;
251 size_t retlen;
252 size_t total_retlen=0;
253 int ret=0;
254 int len;
255
256 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (*ppos == mtd->size)
259 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (*ppos + count > mtd->size)
262 count = mtd->size - *ppos;
263
264 if (!count)
265 return 0;
266
Thago Galesib802c072006-04-17 17:38:15 +0100267 if (count > MAX_KMALLOC_SIZE)
268 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
269 else
270 kbuf=kmalloc(count, GFP_KERNEL);
271
272 if (!kbuf)
273 return -ENOMEM;
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100276
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000277 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 len = MAX_KMALLOC_SIZE;
279 else
280 len = count;
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (copy_from_user(kbuf, buf, len)) {
283 kfree(kbuf);
284 return -EFAULT;
285 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000286
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200287 switch (mfi->mode) {
288 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000289 ret = -EROFS;
290 break;
291 case MTD_MODE_OTP_USER:
292 if (!mtd->write_user_prot_reg) {
293 ret = -EOPNOTSUPP;
294 break;
295 }
296 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
297 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200298
299 case MTD_MODE_RAW:
300 {
301 struct mtd_oob_ops ops;
302
303 ops.mode = MTD_OOB_RAW;
304 ops.datbuf = kbuf;
305 ops.oobbuf = NULL;
306 ops.len = len;
307
308 ret = mtd->write_oob(mtd, *ppos, &ops);
309 retlen = ops.retlen;
310 break;
311 }
312
Nicolas Pitre31f42332005-02-08 17:45:55 +0000313 default:
314 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!ret) {
317 *ppos += retlen;
318 total_retlen += retlen;
319 count -= retlen;
320 buf += retlen;
321 }
322 else {
323 kfree(kbuf);
324 return ret;
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
Thago Galesib802c072006-04-17 17:38:15 +0100328 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return total_retlen;
330} /* mtd_write */
331
332/*======================================================================
333
334 IOCTL calls for getting device parameters.
335
336======================================================================*/
337static void mtdchar_erase_callback (struct erase_info *instr)
338{
339 wake_up((wait_queue_head_t *)instr->priv);
340}
341
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200342#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
343static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
344{
345 struct mtd_info *mtd = mfi->mtd;
346 int ret = 0;
347
348 switch (mode) {
349 case MTD_OTP_FACTORY:
350 if (!mtd->read_fact_prot_reg)
351 ret = -EOPNOTSUPP;
352 else
353 mfi->mode = MTD_MODE_OTP_FACTORY;
354 break;
355 case MTD_OTP_USER:
356 if (!mtd->read_fact_prot_reg)
357 ret = -EOPNOTSUPP;
358 else
359 mfi->mode = MTD_MODE_OTP_USER;
360 break;
361 default:
362 ret = -EINVAL;
363 case MTD_OTP_OFF:
364 break;
365 }
366 return ret;
367}
368#else
369# define otp_select_filemode(f,m) -EOPNOTSUPP
370#endif
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static int mtd_ioctl(struct inode *inode, struct file *file,
373 u_int cmd, u_long arg)
374{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200375 struct mtd_file_info *mfi = file->private_data;
376 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 void __user *argp = (void __user *)arg;
378 int ret = 0;
379 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200380 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
383
384 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
385 if (cmd & IOC_IN) {
386 if (!access_ok(VERIFY_READ, argp, size))
387 return -EFAULT;
388 }
389 if (cmd & IOC_OUT) {
390 if (!access_ok(VERIFY_WRITE, argp, size))
391 return -EFAULT;
392 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 switch (cmd) {
395 case MEMGETREGIONCOUNT:
396 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
397 return -EFAULT;
398 break;
399
400 case MEMGETREGIONINFO:
401 {
402 struct region_info_user ur;
403
404 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
405 return -EFAULT;
406
407 if (ur.regionindex >= mtd->numeraseregions)
408 return -EINVAL;
409 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
410 sizeof(struct mtd_erase_region_info)))
411 return -EFAULT;
412 break;
413 }
414
415 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200416 info.type = mtd->type;
417 info.flags = mtd->flags;
418 info.size = mtd->size;
419 info.erasesize = mtd->erasesize;
420 info.writesize = mtd->writesize;
421 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200422 /* The below fields are obsolete */
423 info.ecctype = -1;
424 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200425 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return -EFAULT;
427 break;
428
429 case MEMERASE:
430 {
431 struct erase_info *erase;
432
433 if(!(file->f_mode & 2))
434 return -EPERM;
435
Burman Yan95b93a02006-11-15 21:10:29 +0200436 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (!erase)
438 ret = -ENOMEM;
439 else {
440 wait_queue_head_t waitq;
441 DECLARE_WAITQUEUE(wait, current);
442
443 init_waitqueue_head(&waitq);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (copy_from_user(&erase->addr, argp,
446 sizeof(struct erase_info_user))) {
447 kfree(erase);
448 return -EFAULT;
449 }
450 erase->mtd = mtd;
451 erase->callback = mtdchar_erase_callback;
452 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 /*
455 FIXME: Allow INTERRUPTIBLE. Which means
456 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 If the wq_head is on the stack, and we
459 leave because we got interrupted, then the
460 wq_head is no longer there when the
461 callback routine tries to wake us up.
462 */
463 ret = mtd->erase(mtd, erase);
464 if (!ret) {
465 set_current_state(TASK_UNINTERRUPTIBLE);
466 add_wait_queue(&waitq, &wait);
467 if (erase->state != MTD_ERASE_DONE &&
468 erase->state != MTD_ERASE_FAILED)
469 schedule();
470 remove_wait_queue(&waitq, &wait);
471 set_current_state(TASK_RUNNING);
472
473 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
474 }
475 kfree(erase);
476 }
477 break;
478 }
479
480 case MEMWRITEOOB:
481 {
482 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200483 struct mtd_oob_ops ops;
David Scidmoree9d8d482007-12-11 17:44:30 -0600484 uint32_t retlen;
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
David Scidmoree9d8d482007-12-11 17:44:30 -0600524 if (ops.oobretlen > 0xFFFFFFFFU)
525 ret = -EOVERFLOW;
526 retlen = ops.oobretlen;
527 if (copy_to_user(&((struct mtd_oob_buf *)argp)->length,
528 &retlen, sizeof(buf.length)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 ret = -EFAULT;
530
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200531 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
533
534 }
535
536 case MEMREADOOB:
537 {
538 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200539 struct mtd_oob_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
542 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000543
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200544 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return -EINVAL;
546
547 if (!mtd->read_oob)
548 ret = -EOPNOTSUPP;
549 else
550 ret = access_ok(VERIFY_WRITE, buf.ptr,
551 buf.length) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (ret)
553 return ret;
554
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200555 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200556 ops.ooboffs = buf.start & (mtd->oobsize - 1);
557 ops.datbuf = NULL;
558 ops.mode = MTD_OOB_PLACE;
559
Thomas Gleixner408b4832007-04-13 19:50:48 +0200560 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200561 return -EINVAL;
562
563 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
564 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000566
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200567 buf.start &= ~(mtd->oobsize - 1);
568 ret = mtd->read_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Vitaly Wool70145682006-11-03 18:20:38 +0300570 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 ret = -EFAULT;
Vitaly Wool70145682006-11-03 18:20:38 +0300572 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
573 ops.oobretlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 ret = -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000575
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200576 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 break;
578 }
579
580 case MEMLOCK:
581 {
582 struct erase_info_user info;
583
584 if (copy_from_user(&info, argp, sizeof(info)))
585 return -EFAULT;
586
587 if (!mtd->lock)
588 ret = -EOPNOTSUPP;
589 else
590 ret = mtd->lock(mtd, info.start, info.length);
591 break;
592 }
593
594 case MEMUNLOCK:
595 {
596 struct erase_info_user info;
597
598 if (copy_from_user(&info, argp, sizeof(info)))
599 return -EFAULT;
600
601 if (!mtd->unlock)
602 ret = -EOPNOTSUPP;
603 else
604 ret = mtd->unlock(mtd, info.start, info.length);
605 break;
606 }
607
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200608 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 case MEMGETOOBSEL:
610 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200611 struct nand_oobinfo oi;
612
613 if (!mtd->ecclayout)
614 return -EOPNOTSUPP;
615 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
616 return -EINVAL;
617
618 oi.useecc = MTD_NANDECC_AUTOPLACE;
619 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
620 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
621 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200622 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200623
624 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return -EFAULT;
626 break;
627 }
628
629 case MEMGETBADBLOCK:
630 {
631 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 if (copy_from_user(&offs, argp, sizeof(loff_t)))
634 return -EFAULT;
635 if (!mtd->block_isbad)
636 ret = -EOPNOTSUPP;
637 else
638 return mtd->block_isbad(mtd, offs);
639 break;
640 }
641
642 case MEMSETBADBLOCK:
643 {
644 loff_t offs;
645
646 if (copy_from_user(&offs, argp, sizeof(loff_t)))
647 return -EFAULT;
648 if (!mtd->block_markbad)
649 ret = -EOPNOTSUPP;
650 else
651 return mtd->block_markbad(mtd, offs);
652 break;
653 }
654
Kyungmin Park493c6462006-05-12 17:03:07 +0300655#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000656 case OTPSELECT:
657 {
658 int mode;
659 if (copy_from_user(&mode, argp, sizeof(int)))
660 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200661
662 mfi->mode = MTD_MODE_NORMAL;
663
664 ret = otp_select_filemode(mfi, mode);
665
Nicolas Pitre81dba482005-04-01 16:36:15 +0100666 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000667 break;
668 }
669
670 case OTPGETREGIONCOUNT:
671 case OTPGETREGIONINFO:
672 {
673 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
674 if (!buf)
675 return -ENOMEM;
676 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200677 switch (mfi->mode) {
678 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000679 if (mtd->get_fact_prot_info)
680 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
681 break;
682 case MTD_MODE_OTP_USER:
683 if (mtd->get_user_prot_info)
684 ret = mtd->get_user_prot_info(mtd, buf, 4096);
685 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200686 default:
687 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000688 }
689 if (ret >= 0) {
690 if (cmd == OTPGETREGIONCOUNT) {
691 int nbr = ret / sizeof(struct otp_info);
692 ret = copy_to_user(argp, &nbr, sizeof(int));
693 } else
694 ret = copy_to_user(argp, buf, ret);
695 if (ret)
696 ret = -EFAULT;
697 }
698 kfree(buf);
699 break;
700 }
701
702 case OTPLOCK:
703 {
704 struct otp_info info;
705
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200706 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000707 return -EINVAL;
708 if (copy_from_user(&info, argp, sizeof(info)))
709 return -EFAULT;
710 if (!mtd->lock_user_prot_reg)
711 return -EOPNOTSUPP;
712 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
713 break;
714 }
715#endif
716
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200717 case ECCGETLAYOUT:
718 {
719 if (!mtd->ecclayout)
720 return -EOPNOTSUPP;
721
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200722 if (copy_to_user(argp, mtd->ecclayout,
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200723 sizeof(struct nand_ecclayout)))
724 return -EFAULT;
725 break;
726 }
727
728 case ECCGETSTATS:
729 {
730 if (copy_to_user(argp, &mtd->ecc_stats,
731 sizeof(struct mtd_ecc_stats)))
732 return -EFAULT;
733 break;
734 }
735
736 case MTDFILEMODE:
737 {
738 mfi->mode = 0;
739
740 switch(arg) {
741 case MTD_MODE_OTP_FACTORY:
742 case MTD_MODE_OTP_USER:
743 ret = otp_select_filemode(mfi, arg);
744 break;
745
746 case MTD_MODE_RAW:
747 if (!mtd->read_oob || !mtd->write_oob)
748 return -EOPNOTSUPP;
749 mfi->mode = arg;
750
751 case MTD_MODE_NORMAL:
752 break;
753 default:
754 ret = -EINVAL;
755 }
756 file->f_pos = 0;
757 break;
758 }
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 default:
761 ret = -ENOTTY;
762 }
763
764 return ret;
765} /* memory_ioctl */
766
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800767static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 .owner = THIS_MODULE,
769 .llseek = mtd_lseek,
770 .read = mtd_read,
771 .write = mtd_write,
772 .ioctl = mtd_ioctl,
773 .open = mtd_open,
774 .release = mtd_close,
775};
776
777static int __init init_mtdchar(void)
778{
779 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
780 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
781 MTD_CHAR_MAJOR);
782 return -EAGAIN;
783 }
784
Todd Poynor9bc7b382005-06-30 01:23:27 +0100785 mtd_class = class_create(THIS_MODULE, "mtd");
786
787 if (IS_ERR(mtd_class)) {
788 printk(KERN_ERR "Error creating mtd class.\n");
789 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
Coywolf Qi Hunt3a7a8822005-07-04 12:15:28 -0500790 return PTR_ERR(mtd_class);
Todd Poynor9bc7b382005-06-30 01:23:27 +0100791 }
792
793 register_mtd_user(&notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 return 0;
795}
796
797static void __exit cleanup_mtdchar(void)
798{
Todd Poynor9bc7b382005-06-30 01:23:27 +0100799 unregister_mtd_user(&notifier);
800 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
802}
803
804module_init(init_mtdchar);
805module_exit(cleanup_mtdchar);
806
807
808MODULE_LICENSE("GPL");
809MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
810MODULE_DESCRIPTION("Direct character-device access to MTD devices");