MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mtd / mtdchar.c
blob18de2268f394a30fd7cd78278201402bd012f200
1 /*
2 * $Id: mtdchar.c,v 1.64 2004/08/09 13:59:46 dwmw2 Exp $
4 * Character-device access to raw MTD devices.
6 */
8 #include <linux/config.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/mtd/mtd.h>
12 #include <linux/mtd/compatmac.h>
13 #include <linux/slab.h>
14 #include <linux/init.h>
15 #include <linux/fs.h>
16 #include <asm/uaccess.h>
18 #ifdef CONFIG_DEVFS_FS
19 #include <linux/devfs_fs_kernel.h>
21 static void mtd_notify_add(struct mtd_info* mtd)
23 if (!mtd)
24 return;
26 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
27 S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%d", mtd->index);
29 devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
30 S_IFCHR | S_IRUGO, "mtd/%dro", mtd->index);
33 static void mtd_notify_remove(struct mtd_info* mtd)
35 if (!mtd)
36 return;
37 devfs_remove("mtd/%d", mtd->index);
38 devfs_remove("mtd/%dro", mtd->index);
41 static struct mtd_notifier notifier = {
42 .add = mtd_notify_add,
43 .remove = mtd_notify_remove,
46 static inline void mtdchar_devfs_init(void)
48 devfs_mk_dir("mtd");
49 register_mtd_user(&notifier);
52 static inline void mtdchar_devfs_exit(void)
54 unregister_mtd_user(&notifier);
55 devfs_remove("mtd");
57 #else /* !DEVFS */
58 #define mtdchar_devfs_init() do { } while(0)
59 #define mtdchar_devfs_exit() do { } while(0)
60 #endif
62 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
64 struct mtd_info *mtd=(struct mtd_info *)file->private_data;
66 switch (orig) {
67 case 0:
68 /* SEEK_SET */
69 file->f_pos = offset;
70 break;
71 case 1:
72 /* SEEK_CUR */
73 file->f_pos += offset;
74 break;
75 case 2:
76 /* SEEK_END */
77 file->f_pos =mtd->size + offset;
78 break;
79 default:
80 return -EINVAL;
83 if (file->f_pos < 0)
84 file->f_pos = 0;
85 else if (file->f_pos >= mtd->size)
86 file->f_pos = mtd->size - 1;
88 return file->f_pos;
93 static int mtd_open(struct inode *inode, struct file *file)
95 int minor = iminor(inode);
96 int devnum = minor >> 1;
97 struct mtd_info *mtd;
99 DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
101 if (devnum >= MAX_MTD_DEVICES)
102 return -ENODEV;
104 /* You can't open the RO devices RW */
105 if ((file->f_mode & 2) && (minor & 1))
106 return -EACCES;
108 mtd = get_mtd_device(NULL, devnum);
110 if (!mtd)
111 return -ENODEV;
113 if (MTD_ABSENT == mtd->type) {
114 put_mtd_device(mtd);
115 return -ENODEV;
118 file->private_data = mtd;
120 /* You can't open it RW if it's not a writeable device */
121 if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
122 put_mtd_device(mtd);
123 return -EACCES;
126 return 0;
127 } /* mtd_open */
129 /*====================================================================*/
131 static int mtd_close(struct inode *inode, struct file *file)
133 struct mtd_info *mtd;
135 DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
137 mtd = (struct mtd_info *)file->private_data;
139 if (mtd->sync)
140 mtd->sync(mtd);
142 put_mtd_device(mtd);
144 return 0;
145 } /* mtd_close */
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.
150 #define MAX_KMALLOC_SIZE 0x20000
152 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
154 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
155 size_t retlen=0;
156 size_t total_retlen=0;
157 int ret=0;
158 int len;
159 char *kbuf;
161 DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
163 if (*ppos + count > mtd->size)
164 count = mtd->size - *ppos;
166 if (!count)
167 return 0;
169 /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
170 and pass them directly to the MTD functions */
171 while (count) {
172 if (count > MAX_KMALLOC_SIZE)
173 len = MAX_KMALLOC_SIZE;
174 else
175 len = count;
177 kbuf=kmalloc(len,GFP_KERNEL);
178 if (!kbuf)
179 return -ENOMEM;
181 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
182 if (!ret) {
183 *ppos += retlen;
184 if (copy_to_user(buf, kbuf, retlen)) {
185 kfree(kbuf);
186 return -EFAULT;
188 else
189 total_retlen += retlen;
191 count -= retlen;
192 buf += retlen;
194 else {
195 kfree(kbuf);
196 return ret;
199 kfree(kbuf);
202 return total_retlen;
203 } /* mtd_read */
205 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
207 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
208 char *kbuf;
209 size_t retlen;
210 size_t total_retlen=0;
211 int ret=0;
212 int len;
214 DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
216 if (*ppos == mtd->size)
217 return -ENOSPC;
219 if (*ppos + count > mtd->size)
220 count = mtd->size - *ppos;
222 if (!count)
223 return 0;
225 while (count) {
226 if (count > MAX_KMALLOC_SIZE)
227 len = MAX_KMALLOC_SIZE;
228 else
229 len = count;
231 kbuf=kmalloc(len,GFP_KERNEL);
232 if (!kbuf) {
233 printk("kmalloc is null\n");
234 return -ENOMEM;
237 if (copy_from_user(kbuf, buf, len)) {
238 kfree(kbuf);
239 return -EFAULT;
242 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
243 if (!ret) {
244 *ppos += retlen;
245 total_retlen += retlen;
246 count -= retlen;
247 buf += retlen;
249 else {
250 kfree(kbuf);
251 return ret;
254 kfree(kbuf);
257 return total_retlen;
258 } /* mtd_write */
260 /*======================================================================
262 IOCTL calls for getting device parameters.
264 ======================================================================*/
265 static void mtdchar_erase_callback (struct erase_info *instr)
267 wake_up((wait_queue_head_t *)instr->priv);
270 static int mtd_ioctl(struct inode *inode, struct file *file,
271 u_int cmd, u_long arg)
273 struct mtd_info *mtd = (struct mtd_info *)file->private_data;
274 void __user *argp = (void __user *)arg;
275 int ret = 0;
276 u_long size;
278 DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
280 size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
281 if (cmd & IOC_IN) {
282 ret = verify_area(VERIFY_READ, argp, size);
283 if (ret) return ret;
285 if (cmd & IOC_OUT) {
286 ret = verify_area(VERIFY_WRITE, argp, size);
287 if (ret) return ret;
290 switch (cmd) {
291 case MEMGETREGIONCOUNT:
292 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
293 return -EFAULT;
294 break;
296 case MEMGETREGIONINFO:
298 struct region_info_user ur;
300 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
301 return -EFAULT;
303 if (ur.regionindex >= mtd->numeraseregions)
304 return -EINVAL;
305 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
306 sizeof(struct mtd_erase_region_info)))
307 return -EFAULT;
308 break;
311 case MEMGETINFO:
312 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
313 return -EFAULT;
314 break;
316 case MEMERASE:
318 struct erase_info *erase;
320 if(!(file->f_mode & 2))
321 return -EPERM;
323 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
324 if (!erase)
325 ret = -ENOMEM;
326 else {
327 wait_queue_head_t waitq;
328 DECLARE_WAITQUEUE(wait, current);
330 init_waitqueue_head(&waitq);
332 memset (erase,0,sizeof(struct erase_info));
333 if (copy_from_user(&erase->addr, argp,
334 sizeof(struct erase_info_user))) {
335 kfree(erase);
336 return -EFAULT;
338 erase->mtd = mtd;
339 erase->callback = mtdchar_erase_callback;
340 erase->priv = (unsigned long)&waitq;
343 FIXME: Allow INTERRUPTIBLE. Which means
344 not having the wait_queue head on the stack.
346 If the wq_head is on the stack, and we
347 leave because we got interrupted, then the
348 wq_head is no longer there when the
349 callback routine tries to wake us up.
351 ret = mtd->erase(mtd, erase);
352 if (!ret) {
353 #if 1 // mask by Victor Yu. 05-14-2007
354 set_current_state(TASK_UNINTERRUPTIBLE);
355 #endif
356 add_wait_queue(&waitq, &wait);
357 if (erase->state != MTD_ERASE_DONE &&
358 erase->state != MTD_ERASE_FAILED)
359 schedule();
360 remove_wait_queue(&waitq, &wait);
361 #if 1 // mask by Victor Yu. 05-14-2007
362 set_current_state(TASK_RUNNING);
363 #endif
365 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
367 kfree(erase);
369 break;
372 case MEMWRITEOOB:
374 struct mtd_oob_buf buf;
375 void *databuf;
376 ssize_t retlen;
378 if(!(file->f_mode & 2))
379 return -EPERM;
381 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
382 return -EFAULT;
384 if (buf.length > 0x4096)
385 return -EINVAL;
387 if (!mtd->write_oob)
388 ret = -EOPNOTSUPP;
389 else
390 ret = verify_area(VERIFY_READ, buf.ptr, buf.length);
392 if (ret)
393 return ret;
395 databuf = kmalloc(buf.length, GFP_KERNEL);
396 if (!databuf)
397 return -ENOMEM;
399 if (copy_from_user(databuf, buf.ptr, buf.length)) {
400 kfree(databuf);
401 return -EFAULT;
404 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
406 if (copy_to_user(argp + sizeof(uint32_t), &retlen, sizeof(uint32_t)))
407 ret = -EFAULT;
409 kfree(databuf);
410 break;
414 case MEMREADOOB:
416 struct mtd_oob_buf buf;
417 void *databuf;
418 ssize_t retlen;
420 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
421 return -EFAULT;
423 if (buf.length > 0x4096)
424 return -EINVAL;
426 if (!mtd->read_oob)
427 ret = -EOPNOTSUPP;
428 else
429 ret = verify_area(VERIFY_WRITE, buf.ptr, buf.length);
431 if (ret)
432 return ret;
434 databuf = kmalloc(buf.length, GFP_KERNEL);
435 if (!databuf)
436 return -ENOMEM;
438 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
440 if (put_user(retlen, (uint32_t __user *)argp))
441 ret = -EFAULT;
442 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
443 ret = -EFAULT;
445 kfree(databuf);
446 break;
449 case MEMLOCK:
451 struct erase_info_user info;
453 if (copy_from_user(&info, argp, sizeof(info)))
454 return -EFAULT;
456 if (!mtd->lock)
457 ret = -EOPNOTSUPP;
458 else
459 ret = mtd->lock(mtd, info.start, info.length);
460 break;
463 case MEMUNLOCK:
465 struct erase_info_user info;
467 if (copy_from_user(&info, argp, sizeof(info)))
468 return -EFAULT;
470 if (!mtd->unlock)
471 ret = -EOPNOTSUPP;
472 else
473 ret = mtd->unlock(mtd, info.start, info.length);
474 break;
477 case MEMSETOOBSEL:
479 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
480 return -EFAULT;
481 break;
484 case MEMGETOOBSEL:
486 if (copy_to_user(argp, &(mtd->oobinfo), sizeof(struct nand_oobinfo)))
487 return -EFAULT;
488 break;
491 case MEMGETBADBLOCK:
493 loff_t offs;
495 if (copy_from_user(&offs, argp, sizeof(loff_t)))
496 return -EFAULT;
497 if (!mtd->block_isbad)
498 ret = -EOPNOTSUPP;
499 else
500 return mtd->block_isbad(mtd, offs);
501 break;
504 case MEMSETBADBLOCK:
506 loff_t offs;
508 if (copy_from_user(&offs, argp, sizeof(loff_t)))
509 return -EFAULT;
510 if (!mtd->block_markbad)
511 ret = -EOPNOTSUPP;
512 else
513 return mtd->block_markbad(mtd, offs);
514 break;
517 default:
518 ret = -ENOTTY;
521 return ret;
522 } /* memory_ioctl */
524 static struct file_operations mtd_fops = {
525 .owner = THIS_MODULE,
526 .llseek = mtd_lseek,
527 .read = mtd_read,
528 .write = mtd_write,
529 .ioctl = mtd_ioctl,
530 .open = mtd_open,
531 .release = mtd_close,
534 static int __init init_mtdchar(void)
536 if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
537 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
538 MTD_CHAR_MAJOR);
539 return -EAGAIN;
542 mtdchar_devfs_init();
543 return 0;
546 static void __exit cleanup_mtdchar(void)
548 mtdchar_devfs_exit();
549 unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
552 module_init(init_mtdchar);
553 module_exit(cleanup_mtdchar);
556 MODULE_LICENSE("GPL");
557 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
558 MODULE_DESCRIPTION("Direct character-device access to MTD devices");