ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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 *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 *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         int ret = 0;
242         u_long size;
243         
244         DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
245
246         size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
247         if (cmd & IOC_IN) {
248                 ret = verify_area(VERIFY_READ, (char *)arg, size);
249                 if (ret) return ret;
250         }
251         if (cmd & IOC_OUT) {
252                 ret = verify_area(VERIFY_WRITE, (char *)arg, size);
253                 if (ret) return ret;
254         }
255         
256         switch (cmd) {
257         case MEMGETREGIONCOUNT:
258                 if (copy_to_user((int *) arg, &(mtd->numeraseregions), sizeof(int)))
259                         return -EFAULT;
260                 break;
261
262         case MEMGETREGIONINFO:
263         {
264                 struct region_info_user ur;
265
266                 if (copy_from_user(     &ur, 
267                                         (struct region_info_user *)arg, 
268                                         sizeof(struct region_info_user))) {
269                         return -EFAULT;
270                 }
271
272                 if (ur.regionindex >= mtd->numeraseregions)
273                         return -EINVAL;
274                 if (copy_to_user((struct mtd_erase_region_info *) arg, 
275                                 &(mtd->eraseregions[ur.regionindex]),
276                                 sizeof(struct mtd_erase_region_info)))
277                         return -EFAULT;
278                 break;
279         }
280
281         case MEMGETINFO:
282                 if (copy_to_user((struct mtd_info *)arg, mtd,
283                                  sizeof(struct mtd_info_user)))
284                         return -EFAULT;
285                 break;
286
287         case MEMERASE:
288         {
289                 struct erase_info *erase;
290
291                 if(!(file->f_mode & 2))
292                         return -EPERM;
293
294                 erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
295                 if (!erase)
296                         ret = -ENOMEM;
297                 else {
298                         wait_queue_head_t waitq;
299                         DECLARE_WAITQUEUE(wait, current);
300
301                         init_waitqueue_head(&waitq);
302
303                         memset (erase,0,sizeof(struct erase_info));
304                         if (copy_from_user(&erase->addr, (u_long *)arg,
305                                            2 * sizeof(u_long))) {
306                                 kfree(erase);
307                                 return -EFAULT;
308                         }
309                         erase->mtd = mtd;
310                         erase->callback = mtd_erase_callback;
311                         erase->priv = (unsigned long)&waitq;
312                         
313                         /*
314                           FIXME: Allow INTERRUPTIBLE. Which means
315                           not having the wait_queue head on the stack.
316                           
317                           If the wq_head is on the stack, and we
318                           leave because we got interrupted, then the
319                           wq_head is no longer there when the
320                           callback routine tries to wake us up.
321                         */
322                         ret = mtd->erase(mtd, erase);
323                         if (!ret) {
324                                 set_current_state(TASK_UNINTERRUPTIBLE);
325                                 add_wait_queue(&waitq, &wait);
326                                 if (erase->state != MTD_ERASE_DONE &&
327                                     erase->state != MTD_ERASE_FAILED)
328                                         schedule();
329                                 remove_wait_queue(&waitq, &wait);
330                                 set_current_state(TASK_RUNNING);
331
332                                 ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
333                         }
334                         kfree(erase);
335                 }
336                 break;
337         }
338
339         case MEMWRITEOOB:
340         {
341                 struct mtd_oob_buf buf;
342                 void *databuf;
343                 ssize_t retlen;
344                 
345                 if(!(file->f_mode & 2))
346                         return -EPERM;
347
348                 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
349                         return -EFAULT;
350                 
351                 if (buf.length > 0x4096)
352                         return -EINVAL;
353
354                 if (!mtd->write_oob)
355                         ret = -EOPNOTSUPP;
356                 else
357                         ret = verify_area(VERIFY_READ, (char *)buf.ptr, buf.length);
358
359                 if (ret)
360                         return ret;
361
362                 databuf = kmalloc(buf.length, GFP_KERNEL);
363                 if (!databuf)
364                         return -ENOMEM;
365                 
366                 if (copy_from_user(databuf, buf.ptr, buf.length)) {
367                         kfree(databuf);
368                         return -EFAULT;
369                 }
370
371                 ret = (mtd->write_oob)(mtd, buf.start, buf.length, &retlen, databuf);
372
373                 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
374                         ret = -EFAULT;
375
376                 kfree(databuf);
377                 break;
378
379         }
380
381         case MEMREADOOB:
382         {
383                 struct mtd_oob_buf buf;
384                 void *databuf;
385                 ssize_t retlen;
386
387                 if (copy_from_user(&buf, (struct mtd_oob_buf *)arg, sizeof(struct mtd_oob_buf)))
388                         return -EFAULT;
389                 
390                 if (buf.length > 0x4096)
391                         return -EINVAL;
392
393                 if (!mtd->read_oob)
394                         ret = -EOPNOTSUPP;
395                 else
396                         ret = verify_area(VERIFY_WRITE, (char *)buf.ptr, buf.length);
397
398                 if (ret)
399                         return ret;
400
401                 databuf = kmalloc(buf.length, GFP_KERNEL);
402                 if (!databuf)
403                         return -ENOMEM;
404                 
405                 ret = (mtd->read_oob)(mtd, buf.start, buf.length, &retlen, databuf);
406
407                 if (copy_to_user((void *)arg + sizeof(u_int32_t), &retlen, sizeof(u_int32_t)))
408                         ret = -EFAULT;
409                 else if (retlen && copy_to_user(buf.ptr, databuf, retlen))
410                         ret = -EFAULT;
411                 
412                 kfree(databuf);
413                 break;
414         }
415
416         case MEMLOCK:
417         {
418                 unsigned long adrs[2];
419
420                 if (copy_from_user(adrs ,(void *)arg, 2* sizeof(unsigned long)))
421                         return -EFAULT;
422
423                 if (!mtd->lock)
424                         ret = -EOPNOTSUPP;
425                 else
426                         ret = mtd->lock(mtd, adrs[0], adrs[1]);
427                 break;
428         }
429
430         case MEMUNLOCK:
431         {
432                 unsigned long adrs[2];
433
434                 if (copy_from_user(adrs, (void *)arg, 2* sizeof(unsigned long)))
435                         return -EFAULT;
436
437                 if (!mtd->unlock)
438                         ret = -EOPNOTSUPP;
439                 else
440                         ret = mtd->unlock(mtd, adrs[0], adrs[1]);
441                 break;
442         }
443
444         case MEMSETOOBSEL:
445         {
446                 if (copy_from_user(&mtd->oobinfo ,(void *)arg, sizeof(struct nand_oobinfo)))
447                         return -EFAULT;
448                 break;
449         }
450                 
451         default:
452                 DEBUG(MTD_DEBUG_LEVEL0, "Invalid ioctl %x (MEMGETINFO = %x)\n", cmd, MEMGETINFO);
453                 ret = -ENOTTY;
454         }
455
456         return ret;
457 } /* memory_ioctl */
458
459 static struct file_operations mtd_fops = {
460         .owner          = THIS_MODULE,
461         .llseek         = mtd_lseek,
462         .read           = mtd_read,
463         .write          = mtd_write,
464         .ioctl          = mtd_ioctl,
465         .open           = mtd_open,
466         .release        = mtd_close,
467 };
468
469
470 #ifdef CONFIG_DEVFS_FS
471 /* Notification that a new device has been added. Create the devfs entry for
472  * it. */
473
474 static void mtd_notify_add(struct mtd_info* mtd)
475 {
476         if (!mtd)
477                 return;
478         devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2),
479                         S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%d", mtd->index);
480         devfs_mk_cdev(MKDEV(MTD_CHAR_MAJOR, mtd->index*2+1),
481                         S_IFCHR | S_IRUGO | S_IWUGO, "mtd/%dro", mtd->index);
482 }
483
484 static void mtd_notify_remove(struct mtd_info* mtd)
485 {
486         if (!mtd)
487                 return;
488         devfs_remove("mtd/%d", mtd->index);
489         devfs_remove("mtd/%dro", mtd->index);
490 }
491 #endif
492
493 static int __init init_mtdchar(void)
494 {
495         if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
496                 printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
497                        MTD_CHAR_MAJOR);
498                 return -EAGAIN;
499         }
500
501 #ifdef CONFIG_DEVFS_FS
502         devfs_mk_dir("mtd");
503
504         register_mtd_user(&notifier);
505 #endif
506         return 0;
507 }
508
509 static void __exit cleanup_mtdchar(void)
510 {
511 #ifdef CONFIG_DEVFS_FS
512         unregister_mtd_user(&notifier);
513         devfs_remove("mtd");
514 #endif
515         unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
516 }
517
518 module_init(init_mtdchar);
519 module_exit(cleanup_mtdchar);
520
521
522 MODULE_LICENSE("GPL");
523 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
524 MODULE_DESCRIPTION("Direct character-device access to MTD devices");