VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / mtd / mtdchar.c
1 /*
2  * $Id: mtdchar.c,v 1.64 2004/08/09 13:59:46 dwmw2 Exp $
3  *
4  * Character-device access to raw MTD devices.
5  *
6  */
7
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>
17
18 #ifdef CONFIG_DEVFS_FS
19 #include <linux/devfs_fs_kernel.h>
20
21 static void mtd_notify_add(struct mtd_info* mtd)
22 {
23         if (!mtd)
24                 return;
25
26         devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
27                       S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%d", mtd->index);
28                 
29         devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
30                       S_IFCHR | S_IRUGO, "mtd/%dro", mtd->index);
31 }
32
33 static void mtd_notify_remove(struct mtd_info* mtd)
34 {
35         if (!mtd)
36                 return;
37         devfs_remove("mtd/%d", mtd->index);
38         devfs_remove("mtd/%dro", mtd->index);
39 }
40
41 static struct mtd_notifier notifier = {
42         .add    = mtd_notify_add,
43         .remove = mtd_notify_remove,
44 };
45
46 static inline void mtdchar_devfs_init(void)
47 {
48         devfs_mk_dir("mtd");
49         register_mtd_user(&notifier);
50 }
51
52 static inline void mtdchar_devfs_exit(void)
53 {
54         unregister_mtd_user(&notifier);
55         devfs_remove("mtd");
56 }
57 #else /* !DEVFS */
58 #define mtdchar_devfs_init() do { } while(0)
59 #define mtdchar_devfs_exit() do { } while(0)
60 #endif
61
62 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
63 {
64         struct mtd_info *mtd=(struct mtd_info *)file->private_data;
65
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;
81         }
82
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;
87
88         return file->f_pos;
89 }
90
91
92
93 static int mtd_open(struct inode *inode, struct file *file)
94 {
95         int minor = iminor(inode);
96         int devnum = minor >> 1;
97         struct mtd_info *mtd;
98
99         DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
100
101         if (devnum >= MAX_MTD_DEVICES)
102                 return -ENODEV;
103
104         /* You can't open the RO devices RW */
105         if ((file->f_mode & 2) && (minor & 1))
106                 return -EACCES;
107
108         mtd = get_mtd_device(NULL, devnum);
109         
110         if (!mtd)
111                 return -ENODEV;
112         
113         if (MTD_ABSENT == mtd->type) {
114                 put_mtd_device(mtd);
115                 return -ENODEV;
116         }
117
118         file->private_data = mtd;
119                 
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;
124         }
125                 
126         return 0;
127 } /* mtd_open */
128
129 /*====================================================================*/
130
131 static int mtd_close(struct inode *inode, struct file *file)
132 {
133         struct mtd_info *mtd;
134
135         DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
136
137         mtd = (struct mtd_info *)file->private_data;
138         
139         if (mtd->sync)
140                 mtd->sync(mtd);
141         
142         put_mtd_device(mtd);
143
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
152 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
153 {
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;
160         
161         DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
162
163         if (*ppos + count > mtd->size)
164                 count = mtd->size - *ppos;
165
166         if (!count)
167                 return 0;
168         
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;
176
177                 kbuf=kmalloc(len,GFP_KERNEL);
178                 if (!kbuf)
179                         return -ENOMEM;
180                 
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;
187                         }
188                         else
189                                 total_retlen += retlen;
190
191                         count -= retlen;
192                         buf += retlen;
193                 }
194                 else {
195                         kfree(kbuf);
196                         return ret;
197                 }
198                 
199                 kfree(kbuf);
200         }
201         
202         return total_retlen;
203 } /* mtd_read */
204
205 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
206 {
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;
213
214         DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
215         
216         if (*ppos == mtd->size)
217                 return -ENOSPC;
218         
219         if (*ppos + count > mtd->size)
220                 count = mtd->size - *ppos;
221
222         if (!count)
223                 return 0;
224
225         while (count) {
226                 if (count > MAX_KMALLOC_SIZE) 
227                         len = MAX_KMALLOC_SIZE;
228                 else
229                         len = count;
230
231                 kbuf=kmalloc(len,GFP_KERNEL);
232                 if (!kbuf) {
233                         printk("kmalloc is null\n");
234                         return -ENOMEM;
235                 }
236
237                 if (copy_from_user(kbuf, buf, len)) {
238                         kfree(kbuf);
239                         return -EFAULT;
240                 }
241                 
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;
248                 }
249                 else {
250                         kfree(kbuf);
251                         return ret;
252                 }
253                 
254                 kfree(kbuf);
255         }
256
257         return total_retlen;
258 } /* mtd_write */
259
260 /*======================================================================
261
262     IOCTL calls for getting device parameters.
263
264 ======================================================================*/
265 static void mtdchar_erase_callback (struct erase_info *instr)
266 {
267         wake_up((wait_queue_head_t *)instr->priv);
268 }
269
270 static int mtd_ioctl(struct inode *inode, struct file *file,
271                      u_int cmd, u_long arg)
272 {
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;
277         
278         DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
279
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;
284         }
285         if (cmd & IOC_OUT) {
286                 ret = verify_area(VERIFY_WRITE, argp, size);
287                 if (ret) return ret;
288         }
289         
290         switch (cmd) {
291         case MEMGETREGIONCOUNT:
292                 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
293                         return -EFAULT;
294                 break;
295
296         case MEMGETREGIONINFO:
297         {
298                 struct region_info_user ur;
299
300                 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
301                         return -EFAULT;
302
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;
309         }
310
311         case MEMGETINFO:
312                 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
313                         return -EFAULT;
314                 break;
315
316         case MEMERASE:
317         {
318                 struct erase_info *erase;
319
320                 if(!(file->f_mode & 2))
321                         return -EPERM;
322
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);
329
330                         init_waitqueue_head(&waitq);
331
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;
337                         }
338                         erase->mtd = mtd;
339                         erase->callback = mtdchar_erase_callback;
340                         erase->priv = (unsigned long)&waitq;
341                         
342                         /*
343                           FIXME: Allow INTERRUPTIBLE. Which means
344                           not having the wait_queue head on the stack.
345                           
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.
350                         */
351                         ret = mtd->erase(mtd, erase);
352                         if (!ret) {
353                                 set_current_state(TASK_UNINTERRUPTIBLE);
354                                 add_wait_queue(&waitq, &wait);
355                                 if (erase->state != MTD_ERASE_DONE &&
356                                     erase->state != MTD_ERASE_FAILED)
357                                         schedule();
358                                 remove_wait_queue(&waitq, &wait);
359                                 set_current_state(TASK_RUNNING);
360
361                                 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
362                         }
363                         kfree(erase);
364                 }
365                 break;
366         }
367
368         case MEMWRITEOOB:
369         {
370                 struct mtd_oob_buf buf;
371                 void *databuf;
372                 ssize_t retlen;
373                 
374                 if(!(file->f_mode & 2))
375                         return -EPERM;
376
377                 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
378                         return -EFAULT;
379                 
380                 if (buf.length > 0x4096)
381                         return -EINVAL;
382
383                 if (!mtd->write_oob)
384                         ret = -EOPNOTSUPP;
385                 else
386                         ret = verify_area(VERIFY_READ, buf.ptr, buf.length);
387
388                 if (ret)
389                         return ret;
390
391                 databuf = kmalloc(buf.length, GFP_KERNEL);
392                 if (!databuf)
393                         return -ENOMEM;
394                 
395                 if (copy_from_user(databuf, buf.ptr, buf.length)) {
396                         kfree(databuf);
397                         return -EFAULT;
398                 }
399
400                 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
401
402                 if (copy_to_user(argp + sizeof(uint32_t), &retlen, sizeof(uint32_t)))
403                         ret = -EFAULT;
404
405                 kfree(databuf);
406                 break;
407
408         }
409
410         case MEMREADOOB:
411         {
412                 struct mtd_oob_buf buf;
413                 void *databuf;
414                 ssize_t retlen;
415
416                 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
417                         return -EFAULT;
418                 
419                 if (buf.length > 0x4096)
420                         return -EINVAL;
421
422                 if (!mtd->read_oob)
423                         ret = -EOPNOTSUPP;
424                 else
425                         ret = verify_area(VERIFY_WRITE, buf.ptr, buf.length);
426
427                 if (ret)
428                         return ret;
429
430                 databuf = kmalloc(buf.length, GFP_KERNEL);
431                 if (!databuf)
432                         return -ENOMEM;
433                 
434                 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
435
436                 if (put_user(retlen, (uint32_t __user *)argp))
437                         ret = -EFAULT;
438                 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
439                         ret = -EFAULT;
440                 
441                 kfree(databuf);
442                 break;
443         }
444
445         case MEMLOCK:
446         {
447                 struct erase_info_user info;
448
449                 if (copy_from_user(&info, argp, sizeof(info)))
450                         return -EFAULT;
451
452                 if (!mtd->lock)
453                         ret = -EOPNOTSUPP;
454                 else
455                         ret = mtd->lock(mtd, info.start, info.length);
456                 break;
457         }
458
459         case MEMUNLOCK:
460         {
461                 struct erase_info_user info;
462
463                 if (copy_from_user(&info, argp, sizeof(info)))
464                         return -EFAULT;
465
466                 if (!mtd->unlock)
467                         ret = -EOPNOTSUPP;
468                 else
469                         ret = mtd->unlock(mtd, info.start, info.length);
470                 break;
471         }
472
473         case MEMSETOOBSEL:
474         {
475                 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
476                         return -EFAULT;
477                 break;
478         }
479
480         case MEMGETOOBSEL:
481         {
482                 if (copy_to_user(argp, &(mtd->oobinfo), sizeof(struct nand_oobinfo)))
483                         return -EFAULT;
484                 break;
485         }
486
487         case MEMGETBADBLOCK:
488         {
489                 loff_t offs;
490                 
491                 if (copy_from_user(&offs, argp, sizeof(loff_t)))
492                         return -EFAULT;
493                 if (!mtd->block_isbad)
494                         ret = -EOPNOTSUPP;
495                 else
496                         return mtd->block_isbad(mtd, offs);
497                 break;
498         }
499
500         case MEMSETBADBLOCK:
501         {
502                 loff_t offs;
503
504                 if (copy_from_user(&offs, argp, sizeof(loff_t)))
505                         return -EFAULT;
506                 if (!mtd->block_markbad)
507                         ret = -EOPNOTSUPP;
508                 else
509                         return mtd->block_markbad(mtd, offs);
510                 break;
511         }
512
513         default:
514                 ret = -ENOTTY;
515         }
516
517         return ret;
518 } /* memory_ioctl */
519
520 static struct file_operations mtd_fops = {
521         .owner          = THIS_MODULE,
522         .llseek         = mtd_lseek,
523         .read           = mtd_read,
524         .write          = mtd_write,
525         .ioctl          = mtd_ioctl,
526         .open           = mtd_open,
527         .release        = mtd_close,
528 };
529
530 static int __init init_mtdchar(void)
531 {
532         if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
533                 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
534                        MTD_CHAR_MAJOR);
535                 return -EAGAIN;
536         }
537
538         mtdchar_devfs_init();
539         return 0;
540 }
541
542 static void __exit cleanup_mtdchar(void)
543 {
544         mtdchar_devfs_exit();
545         unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
546 }
547
548 module_init(init_mtdchar);
549 module_exit(cleanup_mtdchar);
550
551
552 MODULE_LICENSE("GPL");
553 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
554 MODULE_DESCRIPTION("Direct character-device access to MTD devices");