blob: 13cc67ad272a682ee11313326fdcbc07acbd5c7b [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>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010016
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-Hartmandaea34b2008-05-21 12:52:33 -070029 device_create_drvdata(mtd_class, NULL,
30 MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
31 NULL, "mtd%d", mtd->index);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000032
Greg Kroah-Hartmandaea34b2008-05-21 12:52:33 -070033 device_create_drvdata(mtd_class, NULL,
34 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
35 NULL, "mtd%dro", mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38static void mtd_notify_remove(struct mtd_info* mtd)
39{
40 if (!mtd)
41 return;
Todd Poynor9bc7b382005-06-30 01:23:27 +010042
Tony Jonesa98894a2007-09-25 02:03:03 +020043 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
44 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47static struct mtd_notifier notifier = {
48 .add = mtd_notify_add,
49 .remove = mtd_notify_remove,
50};
51
Nicolas Pitre045e9a52005-02-08 19:12:53 +000052/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020053 * Data structure to hold the pointer to the mtd device as well
54 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000055 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020056struct mtd_file_info {
57 struct mtd_info *mtd;
58 enum mtd_file_modes mode;
59};
Nicolas Pitre31f42332005-02-08 17:45:55 +000060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
62{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020063 struct mtd_file_info *mfi = file->private_data;
64 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040067 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040069 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010070 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040072 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010073 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 break;
75 default:
76 return -EINVAL;
77 }
78
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020079 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010080 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Todd Poynor8b491d72005-08-04 02:05:51 +010082 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85
86
87static int mtd_open(struct inode *inode, struct file *file)
88{
89 int minor = iminor(inode);
90 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060091 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020093 struct mtd_file_info *mfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
96
97 if (devnum >= MAX_MTD_DEVICES)
98 return -ENODEV;
99
100 /* You can't open the RO devices RW */
101 if ((file->f_mode & 2) && (minor & 1))
102 return -EACCES;
103
Jonathan Corbet60712392008-05-15 10:10:37 -0600104 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000106
Jonathan Corbet60712392008-05-15 10:10:37 -0600107 if (IS_ERR(mtd)) {
108 ret = PTR_ERR(mtd);
109 goto out;
110 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (MTD_ABSENT == mtd->type) {
113 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600114 ret = -ENODEV;
115 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 /* You can't open it RW if it's not a writeable device */
119 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
120 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600121 ret = -EACCES;
122 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000124
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200125 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
126 if (!mfi) {
127 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600128 ret = -ENOMEM;
129 goto out;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200130 }
131 mfi->mtd = mtd;
132 file->private_data = mfi;
133
Jonathan Corbet60712392008-05-15 10:10:37 -0600134out:
135 unlock_kernel();
136 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137} /* mtd_open */
138
139/*====================================================================*/
140
141static int mtd_close(struct inode *inode, struct file *file)
142{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200143 struct mtd_file_info *mfi = file->private_data;
144 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
147
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200148 /* Only sync if opened RW */
149 if ((file->f_mode & 2) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200153 file->private_data = NULL;
154 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 return 0;
157} /* mtd_close */
158
159/* FIXME: This _really_ needs to die. In 2.5, we should lock the
160 userspace buffer down and use it directly with readv/writev.
161*/
162#define MAX_KMALLOC_SIZE 0x20000
163
164static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
165{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200166 struct mtd_file_info *mfi = file->private_data;
167 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 size_t retlen=0;
169 size_t total_retlen=0;
170 int ret=0;
171 int len;
172 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
175
176 if (*ppos + count > mtd->size)
177 count = mtd->size - *ppos;
178
179 if (!count)
180 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
183 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100184
185 if (count > MAX_KMALLOC_SIZE)
186 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
187 else
188 kbuf=kmalloc(count, GFP_KERNEL);
189
190 if (!kbuf)
191 return -ENOMEM;
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100194
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000195 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 len = MAX_KMALLOC_SIZE;
197 else
198 len = count;
199
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200200 switch (mfi->mode) {
201 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000202 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
203 break;
204 case MTD_MODE_OTP_USER:
205 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
206 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200207 case MTD_MODE_RAW:
208 {
209 struct mtd_oob_ops ops;
210
211 ops.mode = MTD_OOB_RAW;
212 ops.datbuf = kbuf;
213 ops.oobbuf = NULL;
214 ops.len = len;
215
216 ret = mtd->read_oob(mtd, *ppos, &ops);
217 retlen = ops.retlen;
218 break;
219 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000220 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200221 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 /* Nand returns -EBADMSG on ecc errors, but it returns
224 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000225 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200226 * For kernel internal usage it also might return -EUCLEAN
227 * to signal the caller that a bitflip has occured and has
228 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 * Userspace software which accesses NAND this way
230 * must be aware of the fact that it deals with NAND
231 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200232 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 *ppos += retlen;
234 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200235 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return -EFAULT;
237 }
238 else
239 total_retlen += retlen;
240
241 count -= retlen;
242 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000243 if (retlen == 0)
244 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 else {
247 kfree(kbuf);
248 return ret;
249 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Thago Galesib802c072006-04-17 17:38:15 +0100253 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return total_retlen;
255} /* mtd_read */
256
257static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
258{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200259 struct mtd_file_info *mfi = file->private_data;
260 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 char *kbuf;
262 size_t retlen;
263 size_t total_retlen=0;
264 int ret=0;
265 int len;
266
267 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (*ppos == mtd->size)
270 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (*ppos + count > mtd->size)
273 count = mtd->size - *ppos;
274
275 if (!count)
276 return 0;
277
Thago Galesib802c072006-04-17 17:38:15 +0100278 if (count > MAX_KMALLOC_SIZE)
279 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
280 else
281 kbuf=kmalloc(count, GFP_KERNEL);
282
283 if (!kbuf)
284 return -ENOMEM;
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100287
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000288 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 len = MAX_KMALLOC_SIZE;
290 else
291 len = count;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 if (copy_from_user(kbuf, buf, len)) {
294 kfree(kbuf);
295 return -EFAULT;
296 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000297
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200298 switch (mfi->mode) {
299 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000300 ret = -EROFS;
301 break;
302 case MTD_MODE_OTP_USER:
303 if (!mtd->write_user_prot_reg) {
304 ret = -EOPNOTSUPP;
305 break;
306 }
307 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
308 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200309
310 case MTD_MODE_RAW:
311 {
312 struct mtd_oob_ops ops;
313
314 ops.mode = MTD_OOB_RAW;
315 ops.datbuf = kbuf;
316 ops.oobbuf = NULL;
317 ops.len = len;
318
319 ret = mtd->write_oob(mtd, *ppos, &ops);
320 retlen = ops.retlen;
321 break;
322 }
323
Nicolas Pitre31f42332005-02-08 17:45:55 +0000324 default:
325 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!ret) {
328 *ppos += retlen;
329 total_retlen += retlen;
330 count -= retlen;
331 buf += retlen;
332 }
333 else {
334 kfree(kbuf);
335 return ret;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 }
338
Thago Galesib802c072006-04-17 17:38:15 +0100339 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return total_retlen;
341} /* mtd_write */
342
343/*======================================================================
344
345 IOCTL calls for getting device parameters.
346
347======================================================================*/
348static void mtdchar_erase_callback (struct erase_info *instr)
349{
350 wake_up((wait_queue_head_t *)instr->priv);
351}
352
David Brownell34a82442008-07-30 12:35:05 -0700353#ifdef CONFIG_HAVE_MTD_OTP
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200354static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
355{
356 struct mtd_info *mtd = mfi->mtd;
357 int ret = 0;
358
359 switch (mode) {
360 case MTD_OTP_FACTORY:
361 if (!mtd->read_fact_prot_reg)
362 ret = -EOPNOTSUPP;
363 else
364 mfi->mode = MTD_MODE_OTP_FACTORY;
365 break;
366 case MTD_OTP_USER:
367 if (!mtd->read_fact_prot_reg)
368 ret = -EOPNOTSUPP;
369 else
370 mfi->mode = MTD_MODE_OTP_USER;
371 break;
372 default:
373 ret = -EINVAL;
374 case MTD_OTP_OFF:
375 break;
376 }
377 return ret;
378}
379#else
380# define otp_select_filemode(f,m) -EOPNOTSUPP
381#endif
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383static int mtd_ioctl(struct inode *inode, struct file *file,
384 u_int cmd, u_long arg)
385{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200386 struct mtd_file_info *mfi = file->private_data;
387 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 void __user *argp = (void __user *)arg;
389 int ret = 0;
390 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200391 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
394
395 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
396 if (cmd & IOC_IN) {
397 if (!access_ok(VERIFY_READ, argp, size))
398 return -EFAULT;
399 }
400 if (cmd & IOC_OUT) {
401 if (!access_ok(VERIFY_WRITE, argp, size))
402 return -EFAULT;
403 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 switch (cmd) {
406 case MEMGETREGIONCOUNT:
407 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
408 return -EFAULT;
409 break;
410
411 case MEMGETREGIONINFO:
412 {
413 struct region_info_user ur;
414
415 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
416 return -EFAULT;
417
418 if (ur.regionindex >= mtd->numeraseregions)
419 return -EINVAL;
420 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
421 sizeof(struct mtd_erase_region_info)))
422 return -EFAULT;
423 break;
424 }
425
426 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200427 info.type = mtd->type;
428 info.flags = mtd->flags;
429 info.size = mtd->size;
430 info.erasesize = mtd->erasesize;
431 info.writesize = mtd->writesize;
432 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200433 /* The below fields are obsolete */
434 info.ecctype = -1;
435 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200436 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return -EFAULT;
438 break;
439
440 case MEMERASE:
441 {
442 struct erase_info *erase;
443
444 if(!(file->f_mode & 2))
445 return -EPERM;
446
Burman Yan95b93a02006-11-15 21:10:29 +0200447 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (!erase)
449 ret = -ENOMEM;
450 else {
451 wait_queue_head_t waitq;
452 DECLARE_WAITQUEUE(wait, current);
453
454 init_waitqueue_head(&waitq);
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (copy_from_user(&erase->addr, argp,
457 sizeof(struct erase_info_user))) {
458 kfree(erase);
459 return -EFAULT;
460 }
461 erase->mtd = mtd;
462 erase->callback = mtdchar_erase_callback;
463 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
466 FIXME: Allow INTERRUPTIBLE. Which means
467 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 If the wq_head is on the stack, and we
470 leave because we got interrupted, then the
471 wq_head is no longer there when the
472 callback routine tries to wake us up.
473 */
474 ret = mtd->erase(mtd, erase);
475 if (!ret) {
476 set_current_state(TASK_UNINTERRUPTIBLE);
477 add_wait_queue(&waitq, &wait);
478 if (erase->state != MTD_ERASE_DONE &&
479 erase->state != MTD_ERASE_FAILED)
480 schedule();
481 remove_wait_queue(&waitq, &wait);
482 set_current_state(TASK_RUNNING);
483
484 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
485 }
486 kfree(erase);
487 }
488 break;
489 }
490
491 case MEMWRITEOOB:
492 {
493 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200494 struct mtd_oob_ops ops;
Harvey Harrison5f692832008-07-03 23:40:13 -0700495 struct mtd_oob_buf __user *user_buf = argp;
David Scidmoree9d8d482007-12-11 17:44:30 -0600496 uint32_t retlen;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if(!(file->f_mode & 2))
499 return -EPERM;
500
501 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
502 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000503
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200504 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return -EINVAL;
506
507 if (!mtd->write_oob)
508 ret = -EOPNOTSUPP;
509 else
510 ret = access_ok(VERIFY_READ, buf.ptr,
511 buf.length) ? 0 : EFAULT;
512
513 if (ret)
514 return ret;
515
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200516 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200517 ops.ooboffs = buf.start & (mtd->oobsize - 1);
518 ops.datbuf = NULL;
519 ops.mode = MTD_OOB_PLACE;
520
Vitaly Wool70145682006-11-03 18:20:38 +0300521 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200522 return -EINVAL;
523
524 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
525 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000527
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200528 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
529 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return -EFAULT;
531 }
532
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200533 buf.start &= ~(mtd->oobsize - 1);
534 ret = mtd->write_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
David Scidmoree9d8d482007-12-11 17:44:30 -0600536 if (ops.oobretlen > 0xFFFFFFFFU)
537 ret = -EOVERFLOW;
538 retlen = ops.oobretlen;
Harvey Harrison5f692832008-07-03 23:40:13 -0700539 if (copy_to_user(&user_buf->length, &retlen, sizeof(buf.length)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ret = -EFAULT;
541
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200542 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544
545 }
546
547 case MEMREADOOB:
548 {
549 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200550 struct mtd_oob_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
553 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000554
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200555 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -EINVAL;
557
558 if (!mtd->read_oob)
559 ret = -EOPNOTSUPP;
560 else
561 ret = access_ok(VERIFY_WRITE, buf.ptr,
562 buf.length) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 if (ret)
564 return ret;
565
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200566 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200567 ops.ooboffs = buf.start & (mtd->oobsize - 1);
568 ops.datbuf = NULL;
569 ops.mode = MTD_OOB_PLACE;
570
Thomas Gleixner408b4832007-04-13 19:50:48 +0200571 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200572 return -EINVAL;
573
574 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
575 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000577
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200578 buf.start &= ~(mtd->oobsize - 1);
579 ret = mtd->read_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Vitaly Wool70145682006-11-03 18:20:38 +0300581 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ret = -EFAULT;
Vitaly Wool70145682006-11-03 18:20:38 +0300583 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
584 ops.oobretlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 ret = -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000586
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200587 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 break;
589 }
590
591 case MEMLOCK:
592 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700593 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Harvey Harrison175428b2008-07-03 23:40:14 -0700595 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return -EFAULT;
597
598 if (!mtd->lock)
599 ret = -EOPNOTSUPP;
600 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700601 ret = mtd->lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
603 }
604
605 case MEMUNLOCK:
606 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700607 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Harvey Harrison175428b2008-07-03 23:40:14 -0700609 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 return -EFAULT;
611
612 if (!mtd->unlock)
613 ret = -EOPNOTSUPP;
614 else
Harvey Harrison175428b2008-07-03 23:40:14 -0700615 ret = mtd->unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617 }
618
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200619 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 case MEMGETOOBSEL:
621 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200622 struct nand_oobinfo oi;
623
624 if (!mtd->ecclayout)
625 return -EOPNOTSUPP;
626 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
627 return -EINVAL;
628
629 oi.useecc = MTD_NANDECC_AUTOPLACE;
630 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
631 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
632 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200633 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200634
635 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return -EFAULT;
637 break;
638 }
639
640 case MEMGETBADBLOCK:
641 {
642 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (copy_from_user(&offs, argp, sizeof(loff_t)))
645 return -EFAULT;
646 if (!mtd->block_isbad)
647 ret = -EOPNOTSUPP;
648 else
649 return mtd->block_isbad(mtd, offs);
650 break;
651 }
652
653 case MEMSETBADBLOCK:
654 {
655 loff_t offs;
656
657 if (copy_from_user(&offs, argp, sizeof(loff_t)))
658 return -EFAULT;
659 if (!mtd->block_markbad)
660 ret = -EOPNOTSUPP;
661 else
662 return mtd->block_markbad(mtd, offs);
663 break;
664 }
665
David Brownell34a82442008-07-30 12:35:05 -0700666#ifdef CONFIG_HAVE_MTD_OTP
Nicolas Pitre31f42332005-02-08 17:45:55 +0000667 case OTPSELECT:
668 {
669 int mode;
670 if (copy_from_user(&mode, argp, sizeof(int)))
671 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200672
673 mfi->mode = MTD_MODE_NORMAL;
674
675 ret = otp_select_filemode(mfi, mode);
676
Nicolas Pitre81dba482005-04-01 16:36:15 +0100677 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000678 break;
679 }
680
681 case OTPGETREGIONCOUNT:
682 case OTPGETREGIONINFO:
683 {
684 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
685 if (!buf)
686 return -ENOMEM;
687 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200688 switch (mfi->mode) {
689 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000690 if (mtd->get_fact_prot_info)
691 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
692 break;
693 case MTD_MODE_OTP_USER:
694 if (mtd->get_user_prot_info)
695 ret = mtd->get_user_prot_info(mtd, buf, 4096);
696 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200697 default:
698 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000699 }
700 if (ret >= 0) {
701 if (cmd == OTPGETREGIONCOUNT) {
702 int nbr = ret / sizeof(struct otp_info);
703 ret = copy_to_user(argp, &nbr, sizeof(int));
704 } else
705 ret = copy_to_user(argp, buf, ret);
706 if (ret)
707 ret = -EFAULT;
708 }
709 kfree(buf);
710 break;
711 }
712
713 case OTPLOCK:
714 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700715 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000716
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200717 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000718 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700719 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000720 return -EFAULT;
721 if (!mtd->lock_user_prot_reg)
722 return -EOPNOTSUPP;
Harvey Harrison175428b2008-07-03 23:40:14 -0700723 ret = mtd->lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000724 break;
725 }
726#endif
727
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200728 case ECCGETLAYOUT:
729 {
730 if (!mtd->ecclayout)
731 return -EOPNOTSUPP;
732
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200733 if (copy_to_user(argp, mtd->ecclayout,
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200734 sizeof(struct nand_ecclayout)))
735 return -EFAULT;
736 break;
737 }
738
739 case ECCGETSTATS:
740 {
741 if (copy_to_user(argp, &mtd->ecc_stats,
742 sizeof(struct mtd_ecc_stats)))
743 return -EFAULT;
744 break;
745 }
746
747 case MTDFILEMODE:
748 {
749 mfi->mode = 0;
750
751 switch(arg) {
752 case MTD_MODE_OTP_FACTORY:
753 case MTD_MODE_OTP_USER:
754 ret = otp_select_filemode(mfi, arg);
755 break;
756
757 case MTD_MODE_RAW:
758 if (!mtd->read_oob || !mtd->write_oob)
759 return -EOPNOTSUPP;
760 mfi->mode = arg;
761
762 case MTD_MODE_NORMAL:
763 break;
764 default:
765 ret = -EINVAL;
766 }
767 file->f_pos = 0;
768 break;
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 default:
772 ret = -ENOTTY;
773 }
774
775 return ret;
776} /* memory_ioctl */
777
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800778static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 .owner = THIS_MODULE,
780 .llseek = mtd_lseek,
781 .read = mtd_read,
782 .write = mtd_write,
783 .ioctl = mtd_ioctl,
784 .open = mtd_open,
785 .release = mtd_close,
786};
787
788static int __init init_mtdchar(void)
789{
790 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
791 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
792 MTD_CHAR_MAJOR);
793 return -EAGAIN;
794 }
795
Todd Poynor9bc7b382005-06-30 01:23:27 +0100796 mtd_class = class_create(THIS_MODULE, "mtd");
797
798 if (IS_ERR(mtd_class)) {
799 printk(KERN_ERR "Error creating mtd class.\n");
800 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
Coywolf Qi Hunt3a7a8822005-07-04 12:15:28 -0500801 return PTR_ERR(mtd_class);
Todd Poynor9bc7b382005-06-30 01:23:27 +0100802 }
803
804 register_mtd_user(&notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return 0;
806}
807
808static void __exit cleanup_mtdchar(void)
809{
Todd Poynor9bc7b382005-06-30 01:23:27 +0100810 unregister_mtd_user(&notifier);
811 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
813}
814
815module_init(init_mtdchar);
816module_exit(cleanup_mtdchar);
817
818
819MODULE_LICENSE("GPL");
820MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
821MODULE_DESCRIPTION("Direct character-device access to MTD devices");