mtdchar.c 11 KB

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