Merge to Fedora kernel-2.6.7-1.492
[linux-2.6.git] / drivers / mtd / mtdchar.c
1 /*
2  * $Id: mtdchar.c,v 1.54 2003/05/21 10:50:43 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/slab.h>
13 #include <linux/init.h>
14 #include <linux/fs.h>
15 #include <asm/uaccess.h>
16
17 #ifdef CONFIG_DEVFS_FS
18 #include <linux/devfs_fs_kernel.h>
19 static void mtd_notify_add(struct mtd_info* mtd);
20 static void mtd_notify_remove(struct mtd_info* mtd);
21
22 static struct mtd_notifier notifier = {
23         .add    = mtd_notify_add,
24         .remove = mtd_notify_remove,
25 };
26
27 #endif
28
29 static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
30 {
31         struct mtd_info *mtd=(struct mtd_info *)file->private_data;
32
33         switch (orig) {
34         case 0:
35                 /* SEEK_SET */
36                 file->f_pos = offset;
37                 break;
38         case 1:
39                 /* SEEK_CUR */
40                 file->f_pos += offset;
41                 break;
42         case 2:
43                 /* SEEK_END */
44                 file->f_pos =mtd->size + offset;
45                 break;
46         default:
47                 return -EINVAL;
48         }
49
50         if (file->f_pos < 0)
51                 file->f_pos = 0;
52         else if (file->f_pos >= mtd->size)
53                 file->f_pos = mtd->size - 1;
54
55         return file->f_pos;
56 }
57
58
59
60 static int mtd_open(struct inode *inode, struct file *file)
61 {
62         int minor = iminor(inode);
63         int devnum = minor >> 1;
64         struct mtd_info *mtd;
65
66         DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
67
68         if (devnum >= MAX_MTD_DEVICES)
69                 return -ENODEV;
70
71         /* You can't open the RO devices RW */
72         if ((file->f_mode & 2) && (minor & 1))
73                 return -EACCES;
74
75         mtd = get_mtd_device(NULL, devnum);
76         
77         if (!mtd)
78                 return -ENODEV;
79         
80         if (MTD_ABSENT == mtd->type) {
81                 put_mtd_device(mtd);
82                 return -ENODEV;
83         }
84
85         file->private_data = mtd;
86                 
87         /* You can't open it RW if it's not a writeable device */
88         if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
89                 put_mtd_device(mtd);
90                 return -EACCES;
91         }
92                 
93         return 0;
94 } /* mtd_open */
95
96 /*====================================================================*/
97
98 static int mtd_close(struct inode *inode, struct file *file)
99 {
100         struct mtd_info *mtd;
101
102         DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
103
104         mtd = (struct mtd_info *)file->private_data;
105         
106         if (mtd->sync)
107                 mtd->sync(mtd);
108         
109         put_mtd_device(mtd);
110
111         return 0;
112 } /* mtd_close */
113
114 /* FIXME: This _really_ needs to die. In 2.5, we should lock the
115    userspace buffer down and use it directly with readv/writev.
116 */
117 #define MAX_KMALLOC_SIZE 0x20000
118
119 static ssize_t mtd_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
120 {
121         struct mtd_info *mtd = (struct mtd_info *)file->private_data;
122         size_t retlen=0;
123         size_t total_retlen=0;
124         int ret=0;
125         int len;
126         char *kbuf;
127         
128         DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
129
130         if (*ppos + count > mtd->size)
131                 count = mtd->size - *ppos;
132
133         if (!count)
134                 return 0;
135         
136         /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
137            and pass them directly to the MTD functions */
138         while (count) {
139                 if (count > MAX_KMALLOC_SIZE) 
140                         len = MAX_KMALLOC_SIZE;
141                 else
142                         len = count;
143
144                 kbuf=kmalloc(len,GFP_KERNEL);
145                 if (!kbuf)
146                         return -ENOMEM;
147                 
148                 ret = MTD_READ(mtd, *ppos, len, &retlen, kbuf);
149                 if (!ret) {
150                         *ppos += retlen;
151                         if (copy_to_user(buf, kbuf, retlen)) {
152                                 kfree(kbuf);
153                                 return -EFAULT;
154                         }
155                         else
156                                 total_retlen += retlen;
157
158                         count -= retlen;
159                         buf += retlen;
160                 }
161                 else {
162                         kfree(kbuf);
163                         return ret;
164                 }
165                 
166                 kfree(kbuf);
167         }
168         
169         return total_retlen;
170 } /* mtd_read */
171
172 static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
173 {
174         struct mtd_info *mtd = (struct mtd_info *)file->private_data;
175         char *kbuf;
176         size_t retlen;
177         size_t total_retlen=0;
178         int ret=0;
179         int len;
180
181         DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
182         
183         if (*ppos == mtd->size)
184                 return -ENOSPC;
185         
186         if (*ppos + count > mtd->size)
187                 count = mtd->size - *ppos;
188
189         if (!count)
190                 return 0;
191
192         while (count) {
193                 if (count > MAX_KMALLOC_SIZE) 
194                         len = MAX_KMALLOC_SIZE;
195                 else
196                         len = count;
197
198                 kbuf=kmalloc(len,GFP_KERNEL);
199                 if (!kbuf) {
200                         printk("kmalloc is null\n");
201                         return -ENOMEM;
202                 }
203
204                 if (copy_from_user(kbuf, buf, len)) {
205                         kfree(kbuf);
206                         return -EFAULT;
207                 }
208                 
209                 ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
210                 if (!ret) {
211                         *ppos += retlen;
212                         total_retlen += retlen;
213                         count -= retlen;
214                         buf += retlen;
215                 }
216                 else {
217                         kfree(kbuf);
218                         return ret;
219                 }
220                 
221                 kfree(kbuf);
222         }
223
224         return total_retlen;
225 } /* mtd_write */
226
227 /*======================================================================
228
229     IOCTL calls for getting device parameters.
230
231 ======================================================================*/
232 static void mtd_erase_callback (struct erase_info *instr)
233 {
234         wake_up((wait_queue_head_t *)instr->priv);
235 }
236
237 static int mtd_ioctl(struct inode *inode, struct file *file,
238                      u_int cmd, u_long arg)
239 {
240         struct mtd_info *mtd = (struct mtd_info *)file->private_data;
241         void __user *argp = (void __user *)arg;
242         int ret = 0;
243         u_long size;
244         
245         DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
246
247         size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
248         if (cmd & IOC_IN) {
249                 ret = verify_area(VERIFY_READ, argp, size);
250                 if (ret) return ret;
251         }
252         if (cmd & IOC_OUT) {
253                 ret = verify_area(VERIFY_WRITE, argp, size);
254                 if (ret) return ret;
255         }
256         
257         switch (cmd) {
258         case MEMGETREGIONCOUNT:
259                 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
260                         return -EFAULT;
261                 break;
262
263         case MEMGETREGIONINFO:
264         {
265                 struct region_info_user ur;
266
267                 if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
268                         return -EFAULT;
269
270                 if (ur.regionindex >= mtd->numeraseregions)
271                         return -EINVAL;
272                 if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
273                                 sizeof(struct mtd_erase_region_info)))
274                         return -EFAULT;
275                 break;
276         }
277
278         case MEMGETINFO:
279                 if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
280                         return -EFAULT;
281                 break;
282
283         case MEMERASE:
284         {
285                 struct erase_info *erase;
286
287                 if(!(file->f_mode & 2))
288                         return -EPERM;
289
290                 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
291                 if (!erase)
292                         ret = -ENOMEM;
293                 else {
294                         wait_queue_head_t waitq;
295                         DECLARE_WAITQUEUE(wait, current);
296
297                         init_waitqueue_head(&waitq);
298
299                         memset (erase,0,sizeof(struct erase_info));
300                         if (copy_from_user(&erase->addr, argp,
301                                            2 * sizeof(u_long))) {
302                                 kfree(erase);
303                                 return -EFAULT;
304                         }
305                         erase->mtd = mtd;
306                         erase->callback = mtd_erase_callback;
307                         erase->priv = (unsigned long)&waitq;
308                         
309                         /*
310                           FIXME: Allow INTERRUPTIBLE. Which means
311                           not having the wait_queue head on the stack.
312                           
313                           If the wq_head is on the stack, and we
314                           leave because we got interrupted, then the
315                           wq_head is no longer there when the
316                           callback routine tries to wake us up.
317                         */
318                         ret = mtd->erase(mtd, erase);
319                         if (!ret) {
320                                 set_current_state(TASK_UNINTERRUPTIBLE);
321                                 add_wait_queue(&waitq, &wait);
322                                 if (erase->state != MTD_ERASE_DONE &&
323                                     erase->state != MTD_ERASE_FAILED)
324                                         schedule();
325                                 remove_wait_queue(&waitq, &wait);
326                                 set_current_state(TASK_RUNNING);
327
328                                 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
329                         }
330                         kfree(erase);
331                 }
332                 break;
333         }
334
335         case MEMWRITEOOB:
336         {
337                 struct mtd_oob_buf buf;
338                 void *databuf;
339                 ssize_t retlen;
340                 
341                 if(!(file->f_mode & 2))
342                         return -EPERM;
343
344                 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
345                         return -EFAULT;
346                 
347                 if (buf.length > 0x4096)
348                         return -EINVAL;
349
350                 if (!mtd->write_oob)
351                         ret = -EOPNOTSUPP;
352                 else
353                         ret = verify_area(VERIFY_READ, buf.ptr, buf.length);
354
355                 if (ret)
356                         return ret;
357
358                 databuf = kmalloc(buf.length, GFP_KERNEL);
359                 if (!databuf)
360                         return -ENOMEM;
361                 
362                 if (copy_from_user(databuf, buf.ptr, buf.length)) {
363                         kfree(databuf);
364                         return -EFAULT;
365                 }
366
367                 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
368
369                 if (copy_to_user(argp + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
370                         ret = -EFAULT;
371
372                 kfree(databuf);
373                 break;
374
375         }
376
377         case MEMREADOOB:
378         {
379                 struct mtd_oob_buf buf;
380                 void *databuf;
381                 ssize_t retlen;
382
383                 if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
384                         return -EFAULT;
385                 
386                 if (buf.length > 0x4096)
387                         return -EINVAL;
388
389                 if (!mtd->read_oob)
390                         ret = -EOPNOTSUPP;
391                 else
392                         ret = verify_area(VERIFY_WRITE, buf.ptr, buf.length);
393
394                 if (ret)
395                         return ret;
396
397                 databuf = kmalloc(buf.length, GFP_KERNEL);
398                 if (!databuf)
399                         return -ENOMEM;
400                 
401                 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
402
403                 if (copy_to_user(argp + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
404                         ret = -EFAULT;
405                 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
406                         ret = -EFAULT;
407                 
408                 kfree(databuf);
409                 break;
410         }
411
412         case MEMLOCK:
413         {
414                 unsigned long adrs[2];
415
416                 if (copy_from_user(adrs, argp, 2* sizeof(unsigned long)))
417                         return -EFAULT;
418
419                 if (!mtd->lock)
420                         ret = -EOPNOTSUPP;
421                 else
422                         ret = mtd->lock(mtd, adrs[0], adrs[1]);
423                 break;
424         }
425
426         case MEMUNLOCK:
427         {
428                 unsigned long adrs[2];
429
430                 if (copy_from_user(adrs, argp, 2* sizeof(unsigned long)))
431                         return -EFAULT;
432
433                 if (!mtd->unlock)
434                         ret = -EOPNOTSUPP;
435                 else
436                         ret = mtd->unlock(mtd, adrs[0], adrs[1]);
437                 break;
438         }
439
440         case MEMSETOOBSEL:
441         {
442                 if (copy_from_user(&mtd->oobinfo, argp, sizeof(struct nand_oobinfo)))
443                         return -EFAULT;
444                 break;
445         }
446                 
447         default:
448                 DEBUG(MTD_DEBUG_LEVEL0, "Invalid ioctl %x (MEMGETINFO = %x)\n", cmd, MEMGETINFO);
449                 ret = -ENOTTY;
450         }
451
452         return ret;
453 } /* memory_ioctl */
454
455 static struct file_operations mtd_fops = {
456         .owner          = THIS_MODULE,
457         .llseek         = mtd_lseek,
458         .read           = mtd_read,
459         .write          = mtd_write,
460         .ioctl          = mtd_ioctl,
461         .open           = mtd_open,
462         .release        = mtd_close,
463 };
464
465
466 #ifdef CONFIG_DEVFS_FS
467 /* Notification that a new device has been added. Create the devfs entry for
468  * it. */
469
470 static void mtd_notify_add(struct mtd_info* mtd)
471 {
472         if (!mtd)
473                 return;
474         devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
475                         S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%d", mtd->index);
476         devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
477                         S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%dro", mtd->index);
478 }
479
480 static void mtd_notify_remove(struct mtd_info* mtd)
481 {
482         if (!mtd)
483                 return;
484         devfs_remove("mtd/%d", mtd->index);
485         devfs_remove("mtd/%dro", mtd->index);
486 }
487 #endif
488
489 static int __init init_mtdchar(void)
490 {
491         if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
492                 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
493                        MTD_CHAR_MAJOR);
494                 return -EAGAIN;
495         }
496
497 #ifdef CONFIG_DEVFS_FS
498         devfs_mk_dir("mtd");
499
500         register_mtd_user(&notifier);
501 #endif
502         return 0;
503 }
504
505 static void __exit cleanup_mtdchar(void)
506 {
507 #ifdef CONFIG_DEVFS_FS
508         unregister_mtd_user(&notifier);
509         devfs_remove("mtd");
510 #endif
511         unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
512 }
513
514 module_init(init_mtdchar);
515 module_exit(cleanup_mtdchar);
516
517
518 MODULE_LICENSE("GPL");
519 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
520 MODULE_DESCRIPTION("Direct character-device access to MTD devices");