blob: 129d429cd2da5abc55ea1aa9c97a21041a5c85ff [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>
Jonathan Corbet60712392008-05-15 10:10:37 -060017#include <linux/smp_lock.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
24static struct class *mtd_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static void mtd_notify_add(struct mtd_info* mtd)
27{
28 if (!mtd)
29 return;
30
Tony Jonesa98894a2007-09-25 02:03:03 +020031 device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2), "mtd%d", mtd->index);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000032
Tony Jonesa98894a2007-09-25 02:03:03 +020033 device_create(mtd_class, NULL,
34 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1), "mtd%dro", mtd->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
37static void mtd_notify_remove(struct mtd_info* mtd)
38{
39 if (!mtd)
40 return;
Todd Poynor9bc7b382005-06-30 01:23:27 +010041
Tony Jonesa98894a2007-09-25 02:03:03 +020042 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
43 device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46static struct mtd_notifier notifier = {
47 .add = mtd_notify_add,
48 .remove = mtd_notify_remove,
49};
50
Nicolas Pitre045e9a52005-02-08 19:12:53 +000051/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020052 * Data structure to hold the pointer to the mtd device as well
53 * as mode information ofr various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000054 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020055struct mtd_file_info {
56 struct mtd_info *mtd;
57 enum mtd_file_modes mode;
58};
Nicolas Pitre31f42332005-02-08 17:45:55 +000059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
61{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020062 struct mtd_file_info *mfi = file->private_data;
63 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 switch (orig) {
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040066 case SEEK_SET:
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040068 case SEEK_CUR:
Todd Poynor8b491d72005-08-04 02:05:51 +010069 offset += file->f_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 break;
Josef 'Jeff' Sipekea598302006-09-16 21:09:29 -040071 case SEEK_END:
Todd Poynor8b491d72005-08-04 02:05:51 +010072 offset += mtd->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 break;
74 default:
75 return -EINVAL;
76 }
77
Herbert Valerio Riedel1887f512006-06-24 00:03:36 +020078 if (offset >= 0 && offset <= mtd->size)
Todd Poynor8b491d72005-08-04 02:05:51 +010079 return file->f_pos = offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Todd Poynor8b491d72005-08-04 02:05:51 +010081 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
84
85
86static int mtd_open(struct inode *inode, struct file *file)
87{
88 int minor = iminor(inode);
89 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060090 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020092 struct mtd_file_info *mfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
95
96 if (devnum >= MAX_MTD_DEVICES)
97 return -ENODEV;
98
99 /* You can't open the RO devices RW */
100 if ((file->f_mode & 2) && (minor & 1))
101 return -EACCES;
102
Jonathan Corbet60712392008-05-15 10:10:37 -0600103 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000105
Jonathan Corbet60712392008-05-15 10:10:37 -0600106 if (IS_ERR(mtd)) {
107 ret = PTR_ERR(mtd);
108 goto out;
109 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (MTD_ABSENT == mtd->type) {
112 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600113 ret = -ENODEV;
114 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* You can't open it RW if it's not a writeable device */
118 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
119 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600120 ret = -EACCES;
121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000123
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200124 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
125 if (!mfi) {
126 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -0600127 ret = -ENOMEM;
128 goto out;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200129 }
130 mfi->mtd = mtd;
131 file->private_data = mfi;
132
Jonathan Corbet60712392008-05-15 10:10:37 -0600133out:
134 unlock_kernel();
135 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136} /* mtd_open */
137
138/*====================================================================*/
139
140static int mtd_close(struct inode *inode, struct file *file)
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
145 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
146
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200147 /* Only sync if opened RW */
148 if ((file->f_mode & 2) && mtd->sync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200152 file->private_data = NULL;
153 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 return 0;
156} /* mtd_close */
157
158/* FIXME: This _really_ needs to die. In 2.5, we should lock the
159 userspace buffer down and use it directly with readv/writev.
160*/
161#define MAX_KMALLOC_SIZE 0x20000
162
163static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
164{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200165 struct mtd_file_info *mfi = file->private_data;
166 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 size_t retlen=0;
168 size_t total_retlen=0;
169 int ret=0;
170 int len;
171 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
174
175 if (*ppos + count > mtd->size)
176 count = mtd->size - *ppos;
177
178 if (!count)
179 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
182 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100183
184 if (count > MAX_KMALLOC_SIZE)
185 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
186 else
187 kbuf=kmalloc(count, GFP_KERNEL);
188
189 if (!kbuf)
190 return -ENOMEM;
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100193
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000194 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 len = MAX_KMALLOC_SIZE;
196 else
197 len = count;
198
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200199 switch (mfi->mode) {
200 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000201 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
202 break;
203 case MTD_MODE_OTP_USER:
204 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
205 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200206 case MTD_MODE_RAW:
207 {
208 struct mtd_oob_ops ops;
209
210 ops.mode = MTD_OOB_RAW;
211 ops.datbuf = kbuf;
212 ops.oobbuf = NULL;
213 ops.len = len;
214
215 ret = mtd->read_oob(mtd, *ppos, &ops);
216 retlen = ops.retlen;
217 break;
218 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000219 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200220 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /* Nand returns -EBADMSG on ecc errors, but it returns
223 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000224 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200225 * For kernel internal usage it also might return -EUCLEAN
226 * to signal the caller that a bitflip has occured and has
227 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 * Userspace software which accesses NAND this way
229 * must be aware of the fact that it deals with NAND
230 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200231 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *ppos += retlen;
233 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200234 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return -EFAULT;
236 }
237 else
238 total_retlen += retlen;
239
240 count -= retlen;
241 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000242 if (retlen == 0)
243 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245 else {
246 kfree(kbuf);
247 return ret;
248 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Thago Galesib802c072006-04-17 17:38:15 +0100252 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return total_retlen;
254} /* mtd_read */
255
256static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
257{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200258 struct mtd_file_info *mfi = file->private_data;
259 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 char *kbuf;
261 size_t retlen;
262 size_t total_retlen=0;
263 int ret=0;
264 int len;
265
266 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (*ppos == mtd->size)
269 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (*ppos + count > mtd->size)
272 count = mtd->size - *ppos;
273
274 if (!count)
275 return 0;
276
Thago Galesib802c072006-04-17 17:38:15 +0100277 if (count > MAX_KMALLOC_SIZE)
278 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
279 else
280 kbuf=kmalloc(count, GFP_KERNEL);
281
282 if (!kbuf)
283 return -ENOMEM;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100286
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000287 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 len = MAX_KMALLOC_SIZE;
289 else
290 len = count;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (copy_from_user(kbuf, buf, len)) {
293 kfree(kbuf);
294 return -EFAULT;
295 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000296
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200297 switch (mfi->mode) {
298 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000299 ret = -EROFS;
300 break;
301 case MTD_MODE_OTP_USER:
302 if (!mtd->write_user_prot_reg) {
303 ret = -EOPNOTSUPP;
304 break;
305 }
306 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
307 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200308
309 case MTD_MODE_RAW:
310 {
311 struct mtd_oob_ops ops;
312
313 ops.mode = MTD_OOB_RAW;
314 ops.datbuf = kbuf;
315 ops.oobbuf = NULL;
316 ops.len = len;
317
318 ret = mtd->write_oob(mtd, *ppos, &ops);
319 retlen = ops.retlen;
320 break;
321 }
322
Nicolas Pitre31f42332005-02-08 17:45:55 +0000323 default:
324 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!ret) {
327 *ppos += retlen;
328 total_retlen += retlen;
329 count -= retlen;
330 buf += retlen;
331 }
332 else {
333 kfree(kbuf);
334 return ret;
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
337
Thago Galesib802c072006-04-17 17:38:15 +0100338 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return total_retlen;
340} /* mtd_write */
341
342/*======================================================================
343
344 IOCTL calls for getting device parameters.
345
346======================================================================*/
347static void mtdchar_erase_callback (struct erase_info *instr)
348{
349 wake_up((wait_queue_head_t *)instr->priv);
350}
351
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200352#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
353static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
354{
355 struct mtd_info *mtd = mfi->mtd;
356 int ret = 0;
357
358 switch (mode) {
359 case MTD_OTP_FACTORY:
360 if (!mtd->read_fact_prot_reg)
361 ret = -EOPNOTSUPP;
362 else
363 mfi->mode = MTD_MODE_OTP_FACTORY;
364 break;
365 case MTD_OTP_USER:
366 if (!mtd->read_fact_prot_reg)
367 ret = -EOPNOTSUPP;
368 else
369 mfi->mode = MTD_MODE_OTP_USER;
370 break;
371 default:
372 ret = -EINVAL;
373 case MTD_OTP_OFF:
374 break;
375 }
376 return ret;
377}
378#else
379# define otp_select_filemode(f,m) -EOPNOTSUPP
380#endif
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382static int mtd_ioctl(struct inode *inode, struct file *file,
383 u_int cmd, u_long arg)
384{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200385 struct mtd_file_info *mfi = file->private_data;
386 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 void __user *argp = (void __user *)arg;
388 int ret = 0;
389 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200390 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
393
394 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
395 if (cmd & IOC_IN) {
396 if (!access_ok(VERIFY_READ, argp, size))
397 return -EFAULT;
398 }
399 if (cmd & IOC_OUT) {
400 if (!access_ok(VERIFY_WRITE, argp, size))
401 return -EFAULT;
402 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 switch (cmd) {
405 case MEMGETREGIONCOUNT:
406 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
407 return -EFAULT;
408 break;
409
410 case MEMGETREGIONINFO:
411 {
412 struct region_info_user ur;
413
414 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
415 return -EFAULT;
416
417 if (ur.regionindex >= mtd->numeraseregions)
418 return -EINVAL;
419 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
420 sizeof(struct mtd_erase_region_info)))
421 return -EFAULT;
422 break;
423 }
424
425 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200426 info.type = mtd->type;
427 info.flags = mtd->flags;
428 info.size = mtd->size;
429 info.erasesize = mtd->erasesize;
430 info.writesize = mtd->writesize;
431 info.oobsize = mtd->oobsize;
Artem Bityutskiy64f60712007-01-30 10:50:43 +0200432 /* The below fields are obsolete */
433 info.ecctype = -1;
434 info.eccsize = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200435 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -EFAULT;
437 break;
438
439 case MEMERASE:
440 {
441 struct erase_info *erase;
442
443 if(!(file->f_mode & 2))
444 return -EPERM;
445
Burman Yan95b93a02006-11-15 21:10:29 +0200446 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (!erase)
448 ret = -ENOMEM;
449 else {
450 wait_queue_head_t waitq;
451 DECLARE_WAITQUEUE(wait, current);
452
453 init_waitqueue_head(&waitq);
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (copy_from_user(&erase->addr, argp,
456 sizeof(struct erase_info_user))) {
457 kfree(erase);
458 return -EFAULT;
459 }
460 erase->mtd = mtd;
461 erase->callback = mtdchar_erase_callback;
462 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /*
465 FIXME: Allow INTERRUPTIBLE. Which means
466 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 If the wq_head is on the stack, and we
469 leave because we got interrupted, then the
470 wq_head is no longer there when the
471 callback routine tries to wake us up.
472 */
473 ret = mtd->erase(mtd, erase);
474 if (!ret) {
475 set_current_state(TASK_UNINTERRUPTIBLE);
476 add_wait_queue(&waitq, &wait);
477 if (erase->state != MTD_ERASE_DONE &&
478 erase->state != MTD_ERASE_FAILED)
479 schedule();
480 remove_wait_queue(&waitq, &wait);
481 set_current_state(TASK_RUNNING);
482
483 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
484 }
485 kfree(erase);
486 }
487 break;
488 }
489
490 case MEMWRITEOOB:
491 {
492 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200493 struct mtd_oob_ops ops;
David Scidmoree9d8d482007-12-11 17:44:30 -0600494 uint32_t retlen;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if(!(file->f_mode & 2))
497 return -EPERM;
498
499 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
500 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000501
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200502 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return -EINVAL;
504
505 if (!mtd->write_oob)
506 ret = -EOPNOTSUPP;
507 else
508 ret = access_ok(VERIFY_READ, buf.ptr,
509 buf.length) ? 0 : EFAULT;
510
511 if (ret)
512 return ret;
513
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200514 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200515 ops.ooboffs = buf.start & (mtd->oobsize - 1);
516 ops.datbuf = NULL;
517 ops.mode = MTD_OOB_PLACE;
518
Vitaly Wool70145682006-11-03 18:20:38 +0300519 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200520 return -EINVAL;
521
522 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
523 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000525
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200526 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
527 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -EFAULT;
529 }
530
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200531 buf.start &= ~(mtd->oobsize - 1);
532 ret = mtd->write_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
David Scidmoree9d8d482007-12-11 17:44:30 -0600534 if (ops.oobretlen > 0xFFFFFFFFU)
535 ret = -EOVERFLOW;
536 retlen = ops.oobretlen;
537 if (copy_to_user(&((struct mtd_oob_buf *)argp)->length,
538 &retlen, sizeof(buf.length)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 ret = -EFAULT;
540
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200541 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 break;
543
544 }
545
546 case MEMREADOOB:
547 {
548 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200549 struct mtd_oob_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
552 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000553
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200554 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return -EINVAL;
556
557 if (!mtd->read_oob)
558 ret = -EOPNOTSUPP;
559 else
560 ret = access_ok(VERIFY_WRITE, buf.ptr,
561 buf.length) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (ret)
563 return ret;
564
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200565 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200566 ops.ooboffs = buf.start & (mtd->oobsize - 1);
567 ops.datbuf = NULL;
568 ops.mode = MTD_OOB_PLACE;
569
Thomas Gleixner408b4832007-04-13 19:50:48 +0200570 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200571 return -EINVAL;
572
573 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
574 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000576
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200577 buf.start &= ~(mtd->oobsize - 1);
578 ret = mtd->read_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Vitaly Wool70145682006-11-03 18:20:38 +0300580 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 ret = -EFAULT;
Vitaly Wool70145682006-11-03 18:20:38 +0300582 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
583 ops.oobretlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 ret = -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000585
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200586 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 break;
588 }
589
590 case MEMLOCK:
591 {
592 struct erase_info_user info;
593
594 if (copy_from_user(&info, argp, sizeof(info)))
595 return -EFAULT;
596
597 if (!mtd->lock)
598 ret = -EOPNOTSUPP;
599 else
600 ret = mtd->lock(mtd, info.start, info.length);
601 break;
602 }
603
604 case MEMUNLOCK:
605 {
606 struct erase_info_user info;
607
608 if (copy_from_user(&info, argp, sizeof(info)))
609 return -EFAULT;
610
611 if (!mtd->unlock)
612 ret = -EOPNOTSUPP;
613 else
614 ret = mtd->unlock(mtd, info.start, info.length);
615 break;
616 }
617
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200618 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 case MEMGETOOBSEL:
620 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200621 struct nand_oobinfo oi;
622
623 if (!mtd->ecclayout)
624 return -EOPNOTSUPP;
625 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
626 return -EINVAL;
627
628 oi.useecc = MTD_NANDECC_AUTOPLACE;
629 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
630 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
631 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200632 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200633
634 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 return -EFAULT;
636 break;
637 }
638
639 case MEMGETBADBLOCK:
640 {
641 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (copy_from_user(&offs, argp, sizeof(loff_t)))
644 return -EFAULT;
645 if (!mtd->block_isbad)
646 ret = -EOPNOTSUPP;
647 else
648 return mtd->block_isbad(mtd, offs);
649 break;
650 }
651
652 case MEMSETBADBLOCK:
653 {
654 loff_t offs;
655
656 if (copy_from_user(&offs, argp, sizeof(loff_t)))
657 return -EFAULT;
658 if (!mtd->block_markbad)
659 ret = -EOPNOTSUPP;
660 else
661 return mtd->block_markbad(mtd, offs);
662 break;
663 }
664
Kyungmin Park493c6462006-05-12 17:03:07 +0300665#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000666 case OTPSELECT:
667 {
668 int mode;
669 if (copy_from_user(&mode, argp, sizeof(int)))
670 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200671
672 mfi->mode = MTD_MODE_NORMAL;
673
674 ret = otp_select_filemode(mfi, mode);
675
Nicolas Pitre81dba482005-04-01 16:36:15 +0100676 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000677 break;
678 }
679
680 case OTPGETREGIONCOUNT:
681 case OTPGETREGIONINFO:
682 {
683 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
684 if (!buf)
685 return -ENOMEM;
686 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200687 switch (mfi->mode) {
688 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000689 if (mtd->get_fact_prot_info)
690 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
691 break;
692 case MTD_MODE_OTP_USER:
693 if (mtd->get_user_prot_info)
694 ret = mtd->get_user_prot_info(mtd, buf, 4096);
695 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200696 default:
697 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000698 }
699 if (ret >= 0) {
700 if (cmd == OTPGETREGIONCOUNT) {
701 int nbr = ret / sizeof(struct otp_info);
702 ret = copy_to_user(argp, &nbr, sizeof(int));
703 } else
704 ret = copy_to_user(argp, buf, ret);
705 if (ret)
706 ret = -EFAULT;
707 }
708 kfree(buf);
709 break;
710 }
711
712 case OTPLOCK:
713 {
714 struct otp_info info;
715
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200716 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000717 return -EINVAL;
718 if (copy_from_user(&info, argp, sizeof(info)))
719 return -EFAULT;
720 if (!mtd->lock_user_prot_reg)
721 return -EOPNOTSUPP;
722 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
723 break;
724 }
725#endif
726
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200727 case ECCGETLAYOUT:
728 {
729 if (!mtd->ecclayout)
730 return -EOPNOTSUPP;
731
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200732 if (copy_to_user(argp, mtd->ecclayout,
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200733 sizeof(struct nand_ecclayout)))
734 return -EFAULT;
735 break;
736 }
737
738 case ECCGETSTATS:
739 {
740 if (copy_to_user(argp, &mtd->ecc_stats,
741 sizeof(struct mtd_ecc_stats)))
742 return -EFAULT;
743 break;
744 }
745
746 case MTDFILEMODE:
747 {
748 mfi->mode = 0;
749
750 switch(arg) {
751 case MTD_MODE_OTP_FACTORY:
752 case MTD_MODE_OTP_USER:
753 ret = otp_select_filemode(mfi, arg);
754 break;
755
756 case MTD_MODE_RAW:
757 if (!mtd->read_oob || !mtd->write_oob)
758 return -EOPNOTSUPP;
759 mfi->mode = arg;
760
761 case MTD_MODE_NORMAL:
762 break;
763 default:
764 ret = -EINVAL;
765 }
766 file->f_pos = 0;
767 break;
768 }
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 default:
771 ret = -ENOTTY;
772 }
773
774 return ret;
775} /* memory_ioctl */
776
Arjan van de Vend54b1fd2007-02-12 00:55:34 -0800777static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .owner = THIS_MODULE,
779 .llseek = mtd_lseek,
780 .read = mtd_read,
781 .write = mtd_write,
782 .ioctl = mtd_ioctl,
783 .open = mtd_open,
784 .release = mtd_close,
785};
786
787static int __init init_mtdchar(void)
788{
789 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
790 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
791 MTD_CHAR_MAJOR);
792 return -EAGAIN;
793 }
794
Todd Poynor9bc7b382005-06-30 01:23:27 +0100795 mtd_class = class_create(THIS_MODULE, "mtd");
796
797 if (IS_ERR(mtd_class)) {
798 printk(KERN_ERR "Error creating mtd class.\n");
799 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
Coywolf Qi Hunt3a7a8822005-07-04 12:15:28 -0500800 return PTR_ERR(mtd_class);
Todd Poynor9bc7b382005-06-30 01:23:27 +0100801 }
802
803 register_mtd_user(&notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 return 0;
805}
806
807static void __exit cleanup_mtdchar(void)
808{
Todd Poynor9bc7b382005-06-30 01:23:27 +0100809 unregister_mtd_user(&notifier);
810 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
812}
813
814module_init(init_mtdchar);
815module_exit(cleanup_mtdchar);
816
817
818MODULE_LICENSE("GPL");
819MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
820MODULE_DESCRIPTION("Direct character-device access to MTD devices");