blob: 7c4adc6413281a855cf4a9888eca798d49f463bc [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>
10#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/module.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010013#include <linux/slab.h>
14#include <linux/sched.h>
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/mtd/mtd.h>
17#include <linux/mtd/compatmac.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Thomas Gleixner15fdc522005-11-07 00:14:42 +010019#include <asm/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010020
21static struct class *mtd_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23static void mtd_notify_add(struct mtd_info* mtd)
24{
25 if (!mtd)
26 return;
27
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -070028 class_device_create(mtd_class, NULL, MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
Todd Poynor9bc7b382005-06-30 01:23:27 +010029 NULL, "mtd%d", mtd->index);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000030
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -070031 class_device_create(mtd_class, NULL,
Todd Poynor9bc7b382005-06-30 01:23:27 +010032 MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
33 NULL, "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
41 class_device_destroy(mtd_class, MKDEV(MTD_CHAR_MAJOR, mtd->index*2));
42 class_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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (!mtd)
104 return -ENODEV;
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 if (mtd->sync)
138 mtd->sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200141 file->private_data = NULL;
142 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return 0;
145} /* mtd_close */
146
147/* FIXME: This _really_ needs to die. In 2.5, we should lock the
148 userspace buffer down and use it directly with readv/writev.
149*/
150#define MAX_KMALLOC_SIZE 0x20000
151
152static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
153{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200154 struct mtd_file_info *mfi = file->private_data;
155 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 size_t retlen=0;
157 size_t total_retlen=0;
158 int ret=0;
159 int len;
160 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
163
164 if (*ppos + count > mtd->size)
165 count = mtd->size - *ppos;
166
167 if (!count)
168 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
171 and pass them directly to the MTD functions */
Thago Galesib802c072006-04-17 17:38:15 +0100172
173 if (count > MAX_KMALLOC_SIZE)
174 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
175 else
176 kbuf=kmalloc(count, GFP_KERNEL);
177
178 if (!kbuf)
179 return -ENOMEM;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100182
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000183 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 len = MAX_KMALLOC_SIZE;
185 else
186 len = count;
187
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200188 switch (mfi->mode) {
189 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000190 ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
191 break;
192 case MTD_MODE_OTP_USER:
193 ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
194 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200195 case MTD_MODE_RAW:
196 {
197 struct mtd_oob_ops ops;
198
199 ops.mode = MTD_OOB_RAW;
200 ops.datbuf = kbuf;
201 ops.oobbuf = NULL;
202 ops.len = len;
203
204 ret = mtd->read_oob(mtd, *ppos, &ops);
205 retlen = ops.retlen;
206 break;
207 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000208 default:
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200209 ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 /* Nand returns -EBADMSG on ecc errors, but it returns
212 * the data. For our userspace tools it is important
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000213 * to dump areas with ecc errors !
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200214 * For kernel internal usage it also might return -EUCLEAN
215 * to signal the caller that a bitflip has occured and has
216 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 * Userspace software which accesses NAND this way
218 * must be aware of the fact that it deals with NAND
219 */
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200220 if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *ppos += retlen;
222 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200223 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EFAULT;
225 }
226 else
227 total_retlen += retlen;
228
229 count -= retlen;
230 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000231 if (retlen == 0)
232 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 else {
235 kfree(kbuf);
236 return ret;
237 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Thago Galesib802c072006-04-17 17:38:15 +0100241 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return total_retlen;
243} /* mtd_read */
244
245static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
246{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200247 struct mtd_file_info *mfi = file->private_data;
248 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 char *kbuf;
250 size_t retlen;
251 size_t total_retlen=0;
252 int ret=0;
253 int len;
254
255 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (*ppos == mtd->size)
258 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (*ppos + count > mtd->size)
261 count = mtd->size - *ppos;
262
263 if (!count)
264 return 0;
265
Thago Galesib802c072006-04-17 17:38:15 +0100266 if (count > MAX_KMALLOC_SIZE)
267 kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
268 else
269 kbuf=kmalloc(count, GFP_KERNEL);
270
271 if (!kbuf)
272 return -ENOMEM;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 while (count) {
Thago Galesib802c072006-04-17 17:38:15 +0100275
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000276 if (count > MAX_KMALLOC_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 len = MAX_KMALLOC_SIZE;
278 else
279 len = count;
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (copy_from_user(kbuf, buf, len)) {
282 kfree(kbuf);
283 return -EFAULT;
284 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000285
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200286 switch (mfi->mode) {
287 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000288 ret = -EROFS;
289 break;
290 case MTD_MODE_OTP_USER:
291 if (!mtd->write_user_prot_reg) {
292 ret = -EOPNOTSUPP;
293 break;
294 }
295 ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
296 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200297
298 case MTD_MODE_RAW:
299 {
300 struct mtd_oob_ops ops;
301
302 ops.mode = MTD_OOB_RAW;
303 ops.datbuf = kbuf;
304 ops.oobbuf = NULL;
305 ops.len = len;
306
307 ret = mtd->write_oob(mtd, *ppos, &ops);
308 retlen = ops.retlen;
309 break;
310 }
311
Nicolas Pitre31f42332005-02-08 17:45:55 +0000312 default:
313 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
314 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!ret) {
316 *ppos += retlen;
317 total_retlen += retlen;
318 count -= retlen;
319 buf += retlen;
320 }
321 else {
322 kfree(kbuf);
323 return ret;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
326
Thago Galesib802c072006-04-17 17:38:15 +0100327 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return total_retlen;
329} /* mtd_write */
330
331/*======================================================================
332
333 IOCTL calls for getting device parameters.
334
335======================================================================*/
336static void mtdchar_erase_callback (struct erase_info *instr)
337{
338 wake_up((wait_queue_head_t *)instr->priv);
339}
340
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200341#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
342static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
343{
344 struct mtd_info *mtd = mfi->mtd;
345 int ret = 0;
346
347 switch (mode) {
348 case MTD_OTP_FACTORY:
349 if (!mtd->read_fact_prot_reg)
350 ret = -EOPNOTSUPP;
351 else
352 mfi->mode = MTD_MODE_OTP_FACTORY;
353 break;
354 case MTD_OTP_USER:
355 if (!mtd->read_fact_prot_reg)
356 ret = -EOPNOTSUPP;
357 else
358 mfi->mode = MTD_MODE_OTP_USER;
359 break;
360 default:
361 ret = -EINVAL;
362 case MTD_OTP_OFF:
363 break;
364 }
365 return ret;
366}
367#else
368# define otp_select_filemode(f,m) -EOPNOTSUPP
369#endif
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371static int mtd_ioctl(struct inode *inode, struct file *file,
372 u_int cmd, u_long arg)
373{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200374 struct mtd_file_info *mfi = file->private_data;
375 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 void __user *argp = (void __user *)arg;
377 int ret = 0;
378 u_long size;
Joern Engel73c619e2006-05-30 14:25:35 +0200379 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
382
383 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
384 if (cmd & IOC_IN) {
385 if (!access_ok(VERIFY_READ, argp, size))
386 return -EFAULT;
387 }
388 if (cmd & IOC_OUT) {
389 if (!access_ok(VERIFY_WRITE, argp, size))
390 return -EFAULT;
391 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 switch (cmd) {
394 case MEMGETREGIONCOUNT:
395 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
396 return -EFAULT;
397 break;
398
399 case MEMGETREGIONINFO:
400 {
401 struct region_info_user ur;
402
403 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
404 return -EFAULT;
405
406 if (ur.regionindex >= mtd->numeraseregions)
407 return -EINVAL;
408 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
409 sizeof(struct mtd_erase_region_info)))
410 return -EFAULT;
411 break;
412 }
413
414 case MEMGETINFO:
Joern Engel73c619e2006-05-30 14:25:35 +0200415 info.type = mtd->type;
416 info.flags = mtd->flags;
417 info.size = mtd->size;
418 info.erasesize = mtd->erasesize;
419 info.writesize = mtd->writesize;
420 info.oobsize = mtd->oobsize;
421 info.ecctype = mtd->ecctype;
422 info.eccsize = mtd->eccsize;
423 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return -EFAULT;
425 break;
426
427 case MEMERASE:
428 {
429 struct erase_info *erase;
430
431 if(!(file->f_mode & 2))
432 return -EPERM;
433
Burman Yan95b93a02006-11-15 21:10:29 +0200434 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 if (!erase)
436 ret = -ENOMEM;
437 else {
438 wait_queue_head_t waitq;
439 DECLARE_WAITQUEUE(wait, current);
440
441 init_waitqueue_head(&waitq);
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (copy_from_user(&erase->addr, argp,
444 sizeof(struct erase_info_user))) {
445 kfree(erase);
446 return -EFAULT;
447 }
448 erase->mtd = mtd;
449 erase->callback = mtdchar_erase_callback;
450 erase->priv = (unsigned long)&waitq;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 /*
453 FIXME: Allow INTERRUPTIBLE. Which means
454 not having the wait_queue head on the stack.
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 If the wq_head is on the stack, and we
457 leave because we got interrupted, then the
458 wq_head is no longer there when the
459 callback routine tries to wake us up.
460 */
461 ret = mtd->erase(mtd, erase);
462 if (!ret) {
463 set_current_state(TASK_UNINTERRUPTIBLE);
464 add_wait_queue(&waitq, &wait);
465 if (erase->state != MTD_ERASE_DONE &&
466 erase->state != MTD_ERASE_FAILED)
467 schedule();
468 remove_wait_queue(&waitq, &wait);
469 set_current_state(TASK_RUNNING);
470
471 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
472 }
473 kfree(erase);
474 }
475 break;
476 }
477
478 case MEMWRITEOOB:
479 {
480 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200481 struct mtd_oob_ops ops;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if(!(file->f_mode & 2))
484 return -EPERM;
485
486 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
487 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000488
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200489 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return -EINVAL;
491
492 if (!mtd->write_oob)
493 ret = -EOPNOTSUPP;
494 else
495 ret = access_ok(VERIFY_READ, buf.ptr,
496 buf.length) ? 0 : EFAULT;
497
498 if (ret)
499 return ret;
500
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200501 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200502 ops.ooboffs = buf.start & (mtd->oobsize - 1);
503 ops.datbuf = NULL;
504 ops.mode = MTD_OOB_PLACE;
505
Vitaly Wool70145682006-11-03 18:20:38 +0300506 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200507 return -EINVAL;
508
509 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
510 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000512
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200513 if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
514 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return -EFAULT;
516 }
517
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200518 buf.start &= ~(mtd->oobsize - 1);
519 ret = mtd->write_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Vitaly Wool70145682006-11-03 18:20:38 +0300521 if (copy_to_user(argp + sizeof(uint32_t), &ops.oobretlen,
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200522 sizeof(uint32_t)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 ret = -EFAULT;
524
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200525 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 break;
527
528 }
529
530 case MEMREADOOB:
531 {
532 struct mtd_oob_buf buf;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200533 struct mtd_oob_ops ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
536 return -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000537
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200538 if (buf.length > 4096)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return -EINVAL;
540
541 if (!mtd->read_oob)
542 ret = -EOPNOTSUPP;
543 else
544 ret = access_ok(VERIFY_WRITE, buf.ptr,
545 buf.length) ? 0 : -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (ret)
547 return ret;
548
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200549 ops.ooblen = buf.length;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200550 ops.ooboffs = buf.start & (mtd->oobsize - 1);
551 ops.datbuf = NULL;
552 ops.mode = MTD_OOB_PLACE;
553
Thomas Gleixner7bc33122006-06-20 20:05:05 +0200554 if (ops.ooboffs && ops.len > (mtd->oobsize - ops.ooboffs))
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200555 return -EINVAL;
556
557 ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
558 if (!ops.oobbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -ENOMEM;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000560
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200561 buf.start &= ~(mtd->oobsize - 1);
562 ret = mtd->read_oob(mtd, buf.start, &ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Vitaly Wool70145682006-11-03 18:20:38 +0300564 if (put_user(ops.oobretlen, (uint32_t __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 ret = -EFAULT;
Vitaly Wool70145682006-11-03 18:20:38 +0300566 else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
567 ops.oobretlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 ret = -EFAULT;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000569
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200570 kfree(ops.oobbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
572 }
573
574 case MEMLOCK:
575 {
576 struct erase_info_user info;
577
578 if (copy_from_user(&info, argp, sizeof(info)))
579 return -EFAULT;
580
581 if (!mtd->lock)
582 ret = -EOPNOTSUPP;
583 else
584 ret = mtd->lock(mtd, info.start, info.length);
585 break;
586 }
587
588 case MEMUNLOCK:
589 {
590 struct erase_info_user info;
591
592 if (copy_from_user(&info, argp, sizeof(info)))
593 return -EFAULT;
594
595 if (!mtd->unlock)
596 ret = -EOPNOTSUPP;
597 else
598 ret = mtd->unlock(mtd, info.start, info.length);
599 break;
600 }
601
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200602 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 case MEMGETOOBSEL:
604 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200605 struct nand_oobinfo oi;
606
607 if (!mtd->ecclayout)
608 return -EOPNOTSUPP;
609 if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
610 return -EINVAL;
611
612 oi.useecc = MTD_NANDECC_AUTOPLACE;
613 memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
614 memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
615 sizeof(oi.oobfree));
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200616 oi.eccbytes = mtd->ecclayout->eccbytes;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200617
618 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return -EFAULT;
620 break;
621 }
622
623 case MEMGETBADBLOCK:
624 {
625 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (copy_from_user(&offs, argp, sizeof(loff_t)))
628 return -EFAULT;
629 if (!mtd->block_isbad)
630 ret = -EOPNOTSUPP;
631 else
632 return mtd->block_isbad(mtd, offs);
633 break;
634 }
635
636 case MEMSETBADBLOCK:
637 {
638 loff_t offs;
639
640 if (copy_from_user(&offs, argp, sizeof(loff_t)))
641 return -EFAULT;
642 if (!mtd->block_markbad)
643 ret = -EOPNOTSUPP;
644 else
645 return mtd->block_markbad(mtd, offs);
646 break;
647 }
648
Kyungmin Park493c6462006-05-12 17:03:07 +0300649#if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000650 case OTPSELECT:
651 {
652 int mode;
653 if (copy_from_user(&mode, argp, sizeof(int)))
654 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200655
656 mfi->mode = MTD_MODE_NORMAL;
657
658 ret = otp_select_filemode(mfi, mode);
659
Nicolas Pitre81dba482005-04-01 16:36:15 +0100660 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000661 break;
662 }
663
664 case OTPGETREGIONCOUNT:
665 case OTPGETREGIONINFO:
666 {
667 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
668 if (!buf)
669 return -ENOMEM;
670 ret = -EOPNOTSUPP;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200671 switch (mfi->mode) {
672 case MTD_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000673 if (mtd->get_fact_prot_info)
674 ret = mtd->get_fact_prot_info(mtd, buf, 4096);
675 break;
676 case MTD_MODE_OTP_USER:
677 if (mtd->get_user_prot_info)
678 ret = mtd->get_user_prot_info(mtd, buf, 4096);
679 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200680 default:
681 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000682 }
683 if (ret >= 0) {
684 if (cmd == OTPGETREGIONCOUNT) {
685 int nbr = ret / sizeof(struct otp_info);
686 ret = copy_to_user(argp, &nbr, sizeof(int));
687 } else
688 ret = copy_to_user(argp, buf, ret);
689 if (ret)
690 ret = -EFAULT;
691 }
692 kfree(buf);
693 break;
694 }
695
696 case OTPLOCK:
697 {
698 struct otp_info info;
699
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200700 if (mfi->mode != MTD_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000701 return -EINVAL;
702 if (copy_from_user(&info, argp, sizeof(info)))
703 return -EFAULT;
704 if (!mtd->lock_user_prot_reg)
705 return -EOPNOTSUPP;
706 ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
707 break;
708 }
709#endif
710
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200711 case ECCGETLAYOUT:
712 {
713 if (!mtd->ecclayout)
714 return -EOPNOTSUPP;
715
Ricard Wanderlöfd25ade72006-10-17 17:27:11 +0200716 if (copy_to_user(argp, mtd->ecclayout,
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200717 sizeof(struct nand_ecclayout)))
718 return -EFAULT;
719 break;
720 }
721
722 case ECCGETSTATS:
723 {
724 if (copy_to_user(argp, &mtd->ecc_stats,
725 sizeof(struct mtd_ecc_stats)))
726 return -EFAULT;
727 break;
728 }
729
730 case MTDFILEMODE:
731 {
732 mfi->mode = 0;
733
734 switch(arg) {
735 case MTD_MODE_OTP_FACTORY:
736 case MTD_MODE_OTP_USER:
737 ret = otp_select_filemode(mfi, arg);
738 break;
739
740 case MTD_MODE_RAW:
741 if (!mtd->read_oob || !mtd->write_oob)
742 return -EOPNOTSUPP;
743 mfi->mode = arg;
744
745 case MTD_MODE_NORMAL:
746 break;
747 default:
748 ret = -EINVAL;
749 }
750 file->f_pos = 0;
751 break;
752 }
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 default:
755 ret = -ENOTTY;
756 }
757
758 return ret;
759} /* memory_ioctl */
760
761static struct file_operations mtd_fops = {
762 .owner = THIS_MODULE,
763 .llseek = mtd_lseek,
764 .read = mtd_read,
765 .write = mtd_write,
766 .ioctl = mtd_ioctl,
767 .open = mtd_open,
768 .release = mtd_close,
769};
770
771static int __init init_mtdchar(void)
772{
773 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
774 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
775 MTD_CHAR_MAJOR);
776 return -EAGAIN;
777 }
778
Todd Poynor9bc7b382005-06-30 01:23:27 +0100779 mtd_class = class_create(THIS_MODULE, "mtd");
780
781 if (IS_ERR(mtd_class)) {
782 printk(KERN_ERR "Error creating mtd class.\n");
783 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
Coywolf Qi Hunt3a7a8822005-07-04 12:15:28 -0500784 return PTR_ERR(mtd_class);
Todd Poynor9bc7b382005-06-30 01:23:27 +0100785 }
786
787 register_mtd_user(&notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return 0;
789}
790
791static void __exit cleanup_mtdchar(void)
792{
Todd Poynor9bc7b382005-06-30 01:23:27 +0100793 unregister_mtd_user(&notifier);
794 class_destroy(mtd_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
796}
797
798module_init(init_mtdchar);
799module_exit(cleanup_mtdchar);
800
801
802MODULE_LICENSE("GPL");
803MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
804MODULE_DESCRIPTION("Direct character-device access to MTD devices");