blob: 6749c2f963423a96f97db2311782e9cc5414cf12 [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>
Jonathan Corbet60712392008-05-15 10:10:37 -060015#include <linux/smp_lock.h>
David Howells402d3262009-02-12 10:40:00 +000016#include <linux/backing-dev.h>
Kevin Cernekee97718542009-04-08 22:53:13 -070017#include <linux/compat.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mtd/mtd.h>
20#include <linux/mtd/compatmac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Thomas Gleixner15fdc522005-11-07 00:14:42 +010022#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Nicolas Pitre045e9a52005-02-08 19:12:53 +000025/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020026 * Data structure to hold the pointer to the mtd device as well
27 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000028 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020029struct mtd_file_info {
30 struct mtd_info *mtd;
31 enum mtd_file_modes mode;
32};
Nicolas Pitre31f42332005-02-08 17:45:55 +000033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
35{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020036 struct mtd_file_info *mfi = file->private_data;
37 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040040 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040042 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010043 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040045 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010046 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 break;
48 default:
49 return -EINVAL;
50 }
51
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020052 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010053 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Todd Poynor8b491d72005-08-04 02:05:51 +010055 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
58
59
60static int mtd_open(struct inode *inode, struct file *file)
61{
62 int minor = iminor(inode);
63 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060064 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020066 struct mtd_file_info *mfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
69
70 if (devnum >= MAX_MTD_DEVICES)
71 return -ENODEV;
72
73 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040074 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 return -EACCES;
76
Jonathan Corbet60712392008-05-15 10:10:37 -060077 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000079
Jonathan Corbet60712392008-05-15 10:10:37 -060080 if (IS_ERR(mtd)) {
81 ret = PTR_ERR(mtd);
82 goto out;
83 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +000084
David Howells402d3262009-02-12 10:40:00 +000085 if (mtd->type == MTD_ABSENT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -060087 ret = -ENODEV;
88 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90
David Howells402d3262009-02-12 10:40:00 +000091 if (mtd->backing_dev_info)
92 file->f_mapping->backing_dev_info = mtd->backing_dev_info;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -040095 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -060097 ret = -EACCES;
98 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000100
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200101 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
102 if (!mfi) {
103 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600104 ret = -ENOMEM;
105 goto out;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200106 }
107 mfi->mtd = mtd;
108 file->private_data = mfi;
109
Jonathan Corbet60712392008-05-15 10:10:37 -0600110out:
111 unlock_kernel();
112 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113} /* mtd_open */
114
115/*====================================================================*/
116
117static int mtd_close(struct inode *inode, struct file *file)
118{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200119 struct mtd_file_info *mfi = file->private_data;
120 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
123
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200124 /* Only sync if opened RW */
Al Viroaeb5d722008-09-02 15:28:45 -0400125 if ((file->f_mode & FMODE_WRITE) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200129 file->private_data = NULL;
130 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 return 0;
133} /* mtd_close */
134
135/* FIXME: This _really_ needs to die. In 2.5, we should lock the
136 userspace buffer down and use it directly with readv/writev.
137*/
138#define MAX_KMALLOC_SIZE 0x20000
139
140static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
141{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200142 struct mtd_file_info *mfi = file->private_data;
143 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 size_t retlen=0;
145 size_t total_retlen=0;
146 int ret=0;
147 int len;
148 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
151
152 if (*ppos + count > mtd->size)
153 count = mtd->size - *ppos;
154
155 if (!count)
156 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
159 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100160
161 if (count > MAX_KMALLOC_SIZE)
162 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
163 else
164 kbuf=kmalloc(count, GFP_KERNEL);
165
166 if (!kbuf)
167 return -ENOMEM;
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100170
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000171 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 len = MAX_KMALLOC_SIZE;
173 else
174 len = count;
175
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200176 switch (mfi->mode) {
177 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000178 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
179 break;
180 case MTD_MODE_OTP_USER:
181 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
182 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200183 case MTD_MODE_RAW:
184 {
185 struct mtd_oob_ops ops;
186
187 ops.mode = MTD_OOB_RAW;
188 ops.datbuf = kbuf;
189 ops.oobbuf = NULL;
190 ops.len = len;
191
192 ret = mtd->read_oob(mtd, *ppos, &ops);
193 retlen = ops.retlen;
194 break;
195 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000196 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{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200235 struct mtd_file_info *mfi = file->private_data;
236 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 char *kbuf;
238 size_t retlen;
239 size_t total_retlen=0;
240 int ret=0;
241 int len;
242
243 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (*ppos == mtd->size)
246 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 if (*ppos + count > mtd->size)
249 count = mtd->size - *ppos;
250
251 if (!count)
252 return 0;
253
Thago Galesib802c072006-04-17 17:38:15 +0100254 if (count > MAX_KMALLOC_SIZE)
255 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
256 else
257 kbuf=kmalloc(count, GFP_KERNEL);
258
259 if (!kbuf)
260 return -ENOMEM;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100263
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000264 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 len = MAX_KMALLOC_SIZE;
266 else
267 len = count;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (copy_from_user(kbuf, buf, len)) {
270 kfree(kbuf);
271 return -EFAULT;
272 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000273
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200274 switch (mfi->mode) {
275 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000276 ret = -EROFS;
277 break;
278 case MTD_MODE_OTP_USER:
279 if (!mtd->write_user_prot_reg) {
280 ret = -EOPNOTSUPP;
281 break;
282 }
283 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
284 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200285
286 case MTD_MODE_RAW:
287 {
288 struct mtd_oob_ops ops;
289
290 ops.mode = MTD_OOB_RAW;
291 ops.datbuf = kbuf;
292 ops.oobbuf = NULL;
293 ops.len = len;
294
295 ret = mtd->write_oob(mtd, *ppos, &ops);
296 retlen = ops.retlen;
297 break;
298 }
299
Nicolas Pitre31f42332005-02-08 17:45:55 +0000300 default:
301 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 if (!ret) {
304 *ppos += retlen;
305 total_retlen += retlen;
306 count -= retlen;
307 buf += retlen;
308 }
309 else {
310 kfree(kbuf);
311 return ret;
312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 }
314
Thago Galesib802c072006-04-17 17:38:15 +0100315 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return total_retlen;
317} /* mtd_write */
318
319/*======================================================================
320
321 IOCTL calls for getting device parameters.
322
323======================================================================*/
324static void mtdchar_erase_callback (struct erase_info *instr)
325{
326 wake_up((wait_queue_head_t *)instr->priv);
327}
328
David Brownell34a82442008-07-30 12:35:05 -0700329#ifdef CONFIG_HAVE_MTD_OTP
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200330static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
331{
332 struct mtd_info *mtd = mfi->mtd;
333 int ret = 0;
334
335 switch (mode) {
336 case MTD_OTP_FACTORY:
337 if (!mtd->read_fact_prot_reg)
338 ret = -EOPNOTSUPP;
339 else
340 mfi->mode = MTD_MODE_OTP_FACTORY;
341 break;
342 case MTD_OTP_USER:
343 if (!mtd->read_fact_prot_reg)
344 ret = -EOPNOTSUPP;
345 else
346 mfi->mode = MTD_MODE_OTP_USER;
347 break;
348 default:
349 ret = -EINVAL;
350 case MTD_OTP_OFF:
351 break;
352 }
353 return ret;
354}
355#else
356# define otp_select_filemode(f,m) -EOPNOTSUPP
357#endif
358
Kevin Cernekee97718542009-04-08 22:53:13 -0700359static int mtd_do_writeoob(struct file *file, struct mtd_info *mtd,
360 uint64_t start, uint32_t length, void __user *ptr,
361 uint32_t __user *retp)
362{
363 struct mtd_oob_ops ops;
364 uint32_t retlen;
365 int ret = 0;
366
367 if (!(file->f_mode & FMODE_WRITE))
368 return -EPERM;
369
370 if (length > 4096)
371 return -EINVAL;
372
373 if (!mtd->write_oob)
374 ret = -EOPNOTSUPP;
375 else
376 ret = access_ok(VERIFY_READ, ptr, length) ? 0 : EFAULT;
377
378 if (ret)
379 return ret;
380
381 ops.ooblen = length;
382 ops.ooboffs = start & (mtd->oobsize - 1);
383 ops.datbuf = NULL;
384 ops.mode = MTD_OOB_PLACE;
385
386 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
387 return -EINVAL;
388
389 ops.oobbuf = kmalloc(length, GFP_KERNEL);
390 if (!ops.oobbuf)
391 return -ENOMEM;
392
393 if (copy_from_user(ops.oobbuf, ptr, length)) {
394 kfree(ops.oobbuf);
395 return -EFAULT;
396 }
397
398 start &= ~((uint64_t)mtd->oobsize - 1);
399 ret = mtd->write_oob(mtd, start, &ops);
400
401 if (ops.oobretlen > 0xFFFFFFFFU)
402 ret = -EOVERFLOW;
403 retlen = ops.oobretlen;
404 if (copy_to_user(retp, &retlen, sizeof(length)))
405 ret = -EFAULT;
406
407 kfree(ops.oobbuf);
408 return ret;
409}
410
411static int mtd_do_readoob(struct mtd_info *mtd, uint64_t start,
412 uint32_t length, void __user *ptr, uint32_t __user *retp)
413{
414 struct mtd_oob_ops ops;
415 int ret = 0;
416
417 if (length > 4096)
418 return -EINVAL;
419
420 if (!mtd->read_oob)
421 ret = -EOPNOTSUPP;
422 else
423 ret = access_ok(VERIFY_WRITE, ptr,
424 length) ? 0 : -EFAULT;
425 if (ret)
426 return ret;
427
428 ops.ooblen = length;
429 ops.ooboffs = start & (mtd->oobsize - 1);
430 ops.datbuf = NULL;
431 ops.mode = MTD_OOB_PLACE;
432
433 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
434 return -EINVAL;
435
436 ops.oobbuf = kmalloc(length, GFP_KERNEL);
437 if (!ops.oobbuf)
438 return -ENOMEM;
439
440 start &= ~((uint64_t)mtd->oobsize - 1);
441 ret = mtd->read_oob(mtd, start, &ops);
442
443 if (put_user(ops.oobretlen, retp))
444 ret = -EFAULT;
445 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
446 ops.oobretlen))
447 ret = -EFAULT;
448
449 kfree(ops.oobbuf);
450 return ret;
451}
452
Arnd Bergmann55929332010-04-27 00:24:05 +0200453static int mtd_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200455 struct mtd_file_info *mfi = file->private_data;
456 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 void __user *argp = (void __user *)arg;
458 int ret = 0;
459 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200460 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
463
464 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
465 if (cmd & IOC_IN) {
466 if (!access_ok(VERIFY_READ, argp, size))
467 return -EFAULT;
468 }
469 if (cmd & IOC_OUT) {
470 if (!access_ok(VERIFY_WRITE, argp, size))
471 return -EFAULT;
472 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 switch (cmd) {
475 case MEMGETREGIONCOUNT:
476 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
477 return -EFAULT;
478 break;
479
480 case MEMGETREGIONINFO:
481 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700482 uint32_t ur_idx;
483 struct mtd_erase_region_info *kr;
484 struct region_info_user *ur = (struct region_info_user *) argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Zev Weissb67c5f82008-09-01 05:02:12 -0700486 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -EFAULT;
488
Zev Weissb67c5f82008-09-01 05:02:12 -0700489 kr = &(mtd->eraseregions[ur_idx]);
490
491 if (put_user(kr->offset, &(ur->offset))
492 || put_user(kr->erasesize, &(ur->erasesize))
493 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 break;
497 }
498
499 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200500 info.type = mtd->type;
501 info.flags = mtd->flags;
502 info.size = mtd->size;
503 info.erasesize = mtd->erasesize;
504 info.writesize = mtd->writesize;
505 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200506 /* The below fields are obsolete */
507 info.ecctype = -1;
508 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200509 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return -EFAULT;
511 break;
512
513 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700514 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 {
516 struct erase_info *erase;
517
Al Viroaeb5d722008-09-02 15:28:45 -0400518 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return -EPERM;
520
Burman Yan95b93a02006-11-15 21:10:29 +0200521 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (!erase)
523 ret = -ENOMEM;
524 else {
525 wait_queue_head_t waitq;
526 DECLARE_WAITQUEUE(wait, current);
527
528 init_waitqueue_head(&waitq);
529
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700530 if (cmd == MEMERASE64) {
531 struct erase_info_user64 einfo64;
532
533 if (copy_from_user(&einfo64, argp,
534 sizeof(struct erase_info_user64))) {
535 kfree(erase);
536 return -EFAULT;
537 }
538 erase->addr = einfo64.start;
539 erase->len = einfo64.length;
540 } else {
541 struct erase_info_user einfo32;
542
543 if (copy_from_user(&einfo32, argp,
544 sizeof(struct erase_info_user))) {
545 kfree(erase);
546 return -EFAULT;
547 }
548 erase->addr = einfo32.start;
549 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551 erase->mtd = mtd;
552 erase->callback = mtdchar_erase_callback;
553 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
556 FIXME: Allow INTERRUPTIBLE. Which means
557 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 If the wq_head is on the stack, and we
560 leave because we got interrupted, then the
561 wq_head is no longer there when the
562 callback routine tries to wake us up.
563 */
564 ret = mtd->erase(mtd, erase);
565 if (!ret) {
566 set_current_state(TASK_UNINTERRUPTIBLE);
567 add_wait_queue(&waitq, &wait);
568 if (erase->state != MTD_ERASE_DONE &&
569 erase->state != MTD_ERASE_FAILED)
570 schedule();
571 remove_wait_queue(&waitq, &wait);
572 set_current_state(TASK_RUNNING);
573
574 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
575 }
576 kfree(erase);
577 }
578 break;
579 }
580
581 case MEMWRITEOOB:
582 {
583 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700584 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000585
Kevin Cernekee97718542009-04-08 22:53:13 -0700586 /* NOTE: writes return length to buf_user->length */
587 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700589 else
590 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
591 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
595 case MEMREADOOB:
596 {
597 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700598 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Kevin Cernekee97718542009-04-08 22:53:13 -0700600 /* NOTE: writes return length to buf_user->start */
601 if (copy_from_user(&buf, argp, sizeof(buf)))
602 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 else
Kevin Cernekee97718542009-04-08 22:53:13 -0700604 ret = mtd_do_readoob(mtd, buf.start, buf.length,
605 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 break;
607 }
608
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700609 case MEMWRITEOOB64:
610 {
611 struct mtd_oob_buf64 buf;
612 struct mtd_oob_buf64 __user *buf_user = argp;
613
614 if (copy_from_user(&buf, argp, sizeof(buf)))
615 ret = -EFAULT;
616 else
617 ret = mtd_do_writeoob(file, mtd, buf.start, buf.length,
618 (void __user *)(uintptr_t)buf.usr_ptr,
619 &buf_user->length);
620 break;
621 }
622
623 case MEMREADOOB64:
624 {
625 struct mtd_oob_buf64 buf;
626 struct mtd_oob_buf64 __user *buf_user = argp;
627
628 if (copy_from_user(&buf, argp, sizeof(buf)))
629 ret = -EFAULT;
630 else
631 ret = mtd_do_readoob(mtd, buf.start, buf.length,
632 (void __user *)(uintptr_t)buf.usr_ptr,
633 &buf_user->length);
634 break;
635 }
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 case MEMLOCK:
638 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700639 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Harvey Harrison175428b2008-07-03 23:40:14 -0700641 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return -EFAULT;
643
644 if (!mtd->lock)
645 ret = -EOPNOTSUPP;
646 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700647 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 break;
649 }
650
651 case MEMUNLOCK:
652 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700653 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Harvey Harrison175428b2008-07-03 23:40:14 -0700655 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 return -EFAULT;
657
658 if (!mtd->unlock)
659 ret = -EOPNOTSUPP;
660 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700661 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
663 }
664
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200665 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 case MEMGETOOBSEL:
667 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200668 struct nand_oobinfo oi;
669
670 if (!mtd->ecclayout)
671 return -EOPNOTSUPP;
672 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
673 return -EINVAL;
674
675 oi.useecc = MTD_NANDECC_AUTOPLACE;
676 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
677 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
678 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200679 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200680
681 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -EFAULT;
683 break;
684 }
685
686 case MEMGETBADBLOCK:
687 {
688 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (copy_from_user(&offs, argp, sizeof(loff_t)))
691 return -EFAULT;
692 if (!mtd->block_isbad)
693 ret = -EOPNOTSUPP;
694 else
695 return mtd->block_isbad(mtd, offs);
696 break;
697 }
698
699 case MEMSETBADBLOCK:
700 {
701 loff_t offs;
702
703 if (copy_from_user(&offs, argp, sizeof(loff_t)))
704 return -EFAULT;
705 if (!mtd->block_markbad)
706 ret = -EOPNOTSUPP;
707 else
708 return mtd->block_markbad(mtd, offs);
709 break;
710 }
711
David Brownell34a82442008-07-30 12:35:05 -0700712#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000713 case OTPSELECT:
714 {
715 int mode;
716 if (copy_from_user(&mode, argp, sizeof(int)))
717 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200718
719 mfi->mode = MTD_MODE_NORMAL;
720
721 ret = otp_select_filemode(mfi, mode);
722
Nicolas Pitre81dba482005-04-01 16:36:15 +0100723 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000724 break;
725 }
726
727 case OTPGETREGIONCOUNT:
728 case OTPGETREGIONINFO:
729 {
730 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
731 if (!buf)
732 return -ENOMEM;
733 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200734 switch (mfi->mode) {
735 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000736 if (mtd->get_fact_prot_info)
737 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
738 break;
739 case MTD_MODE_OTP_USER:
740 if (mtd->get_user_prot_info)
741 ret = mtd->get_user_prot_info(mtd, buf, 4096);
742 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200743 default:
744 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000745 }
746 if (ret >= 0) {
747 if (cmd == OTPGETREGIONCOUNT) {
748 int nbr = ret / sizeof(struct otp_info);
749 ret = copy_to_user(argp, &nbr, sizeof(int));
750 } else
751 ret = copy_to_user(argp, buf, ret);
752 if (ret)
753 ret = -EFAULT;
754 }
755 kfree(buf);
756 break;
757 }
758
759 case OTPLOCK:
760 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700761 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000762
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200763 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000764 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700765 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000766 return -EFAULT;
767 if (!mtd->lock_user_prot_reg)
768 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700769 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000770 break;
771 }
772#endif
773
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200774 case ECCGETLAYOUT:
775 {
776 if (!mtd->ecclayout)
777 return -EOPNOTSUPP;
778
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200779 if (copy_to_user(argp, mtd->ecclayout,
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200780 sizeof(struct nand_ecclayout)))
781 return -EFAULT;
782 break;
783 }
784
785 case ECCGETSTATS:
786 {
787 if (copy_to_user(argp, &mtd->ecc_stats,
788 sizeof(struct mtd_ecc_stats)))
789 return -EFAULT;
790 break;
791 }
792
793 case MTDFILEMODE:
794 {
795 mfi->mode = 0;
796
797 switch(arg) {
798 case MTD_MODE_OTP_FACTORY:
799 case MTD_MODE_OTP_USER:
800 ret = otp_select_filemode(mfi, arg);
801 break;
802
803 case MTD_MODE_RAW:
804 if (!mtd->read_oob || !mtd->write_oob)
805 return -EOPNOTSUPP;
806 mfi->mode = arg;
807
808 case MTD_MODE_NORMAL:
809 break;
810 default:
811 ret = -EINVAL;
812 }
813 file->f_pos = 0;
814 break;
815 }
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 default:
818 ret = -ENOTTY;
819 }
820
821 return ret;
822} /* memory_ioctl */
823
Arnd Bergmann55929332010-04-27 00:24:05 +0200824static long mtd_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
825{
826 int ret;
827
828 lock_kernel();
829 ret = mtd_ioctl(file, cmd, arg);
830 unlock_kernel();
831
832 return ret;
833}
834
Kevin Cernekee97718542009-04-08 22:53:13 -0700835#ifdef CONFIG_COMPAT
836
837struct mtd_oob_buf32 {
838 u_int32_t start;
839 u_int32_t length;
840 compat_caddr_t ptr; /* unsigned char* */
841};
842
843#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
844#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
845
846static long mtd_compat_ioctl(struct file *file, unsigned int cmd,
847 unsigned long arg)
848{
849 struct mtd_file_info *mfi = file->private_data;
850 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +0100851 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -0700852 int ret = 0;
853
854 lock_kernel();
855
856 switch (cmd) {
857 case MEMWRITEOOB32:
858 {
859 struct mtd_oob_buf32 buf;
860 struct mtd_oob_buf32 __user *buf_user = argp;
861
862 if (copy_from_user(&buf, argp, sizeof(buf)))
863 ret = -EFAULT;
864 else
865 ret = mtd_do_writeoob(file, mtd, buf.start,
866 buf.length, compat_ptr(buf.ptr),
867 &buf_user->length);
868 break;
869 }
870
871 case MEMREADOOB32:
872 {
873 struct mtd_oob_buf32 buf;
874 struct mtd_oob_buf32 __user *buf_user = argp;
875
876 /* NOTE: writes return length to buf->start */
877 if (copy_from_user(&buf, argp, sizeof(buf)))
878 ret = -EFAULT;
879 else
880 ret = mtd_do_readoob(mtd, buf.start,
881 buf.length, compat_ptr(buf.ptr),
882 &buf_user->start);
883 break;
884 }
885 default:
Arnd Bergmann55929332010-04-27 00:24:05 +0200886 ret = mtd_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -0700887 }
888
889 unlock_kernel();
890
891 return ret;
892}
893
894#endif /* CONFIG_COMPAT */
895
David Howells402d3262009-02-12 10:40:00 +0000896/*
897 * try to determine where a shared mapping can be made
898 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
899 * mappings)
900 */
901#ifndef CONFIG_MMU
902static unsigned long mtd_get_unmapped_area(struct file *file,
903 unsigned long addr,
904 unsigned long len,
905 unsigned long pgoff,
906 unsigned long flags)
907{
908 struct mtd_file_info *mfi = file->private_data;
909 struct mtd_info *mtd = mfi->mtd;
910
911 if (mtd->get_unmapped_area) {
912 unsigned long offset;
913
914 if (addr != 0)
915 return (unsigned long) -EINVAL;
916
917 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
918 return (unsigned long) -EINVAL;
919
920 offset = pgoff << PAGE_SHIFT;
921 if (offset > mtd->size - len)
922 return (unsigned long) -EINVAL;
923
924 return mtd->get_unmapped_area(mtd, len, offset, flags);
925 }
926
927 /* can't map directly */
928 return (unsigned long) -ENOSYS;
929}
930#endif
931
932/*
933 * set up a mapping for shared memory segments
934 */
935static int mtd_mmap(struct file *file, struct vm_area_struct *vma)
936{
937#ifdef CONFIG_MMU
938 struct mtd_file_info *mfi = file->private_data;
939 struct mtd_info *mtd = mfi->mtd;
940
941 if (mtd->type == MTD_RAM || mtd->type == MTD_ROM)
942 return 0;
943 return -ENOSYS;
944#else
945 return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
946#endif
947}
948
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800949static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 .owner = THIS_MODULE,
951 .llseek = mtd_lseek,
952 .read = mtd_read,
953 .write = mtd_write,
Arnd Bergmann55929332010-04-27 00:24:05 +0200954 .unlocked_ioctl = mtd_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -0700955#ifdef CONFIG_COMPAT
956 .compat_ioctl = mtd_compat_ioctl,
957#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 .open = mtd_open,
959 .release = mtd_close,
David Howells402d3262009-02-12 10:40:00 +0000960 .mmap = mtd_mmap,
961#ifndef CONFIG_MMU
962 .get_unmapped_area = mtd_get_unmapped_area,
963#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964};
965
966static int __init init_mtdchar(void)
967{
David Brownell1f24b5a2009-03-26 00:42:41 -0700968 int status;
969
970 status = register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops);
971 if (status < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
973 MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
975
David Brownell1f24b5a2009-03-26 00:42:41 -0700976 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979static void __exit cleanup_mtdchar(void)
980{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
982}
983
984module_init(init_mtdchar);
985module_exit(cleanup_mtdchar);
986
David Brownell1f24b5a2009-03-26 00:42:41 -0700987MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
989MODULE_LICENSE("GPL");
990MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
991MODULE_DESCRIPTION("Direct character-device access to MTD devices");
Scott James Remnant90160e12009-03-02 18:42:39 +0000992MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);