mtdchar.c 13 KB

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