mtdchar.c 14 KB

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