mtdchar.c 13 KB

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