mtdchar.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  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. if (count > MAX_KMALLOC_SIZE)
  131. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  132. else
  133. kbuf=kmalloc(count, GFP_KERNEL);
  134. if (!kbuf)
  135. return -ENOMEM;
  136. while (count) {
  137. if (count > MAX_KMALLOC_SIZE)
  138. len = MAX_KMALLOC_SIZE;
  139. else
  140. len = count;
  141. switch (MTD_MODE(file)) {
  142. case MTD_MODE_OTP_FACT:
  143. ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  144. break;
  145. case MTD_MODE_OTP_USER:
  146. ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  147. break;
  148. default:
  149. ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
  150. }
  151. /* Nand returns -EBADMSG on ecc errors, but it returns
  152. * the data. For our userspace tools it is important
  153. * to dump areas with ecc errors !
  154. * Userspace software which accesses NAND this way
  155. * must be aware of the fact that it deals with NAND
  156. */
  157. if (!ret || (ret == -EBADMSG)) {
  158. *ppos += retlen;
  159. if (copy_to_user(buf, kbuf, retlen)) {
  160. kfree(kbuf);
  161. return -EFAULT;
  162. }
  163. else
  164. total_retlen += retlen;
  165. count -= retlen;
  166. buf += retlen;
  167. if (retlen == 0)
  168. count = 0;
  169. }
  170. else {
  171. kfree(kbuf);
  172. return ret;
  173. }
  174. }
  175. kfree(kbuf);
  176. return total_retlen;
  177. } /* mtd_read */
  178. static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
  179. {
  180. struct mtd_info *mtd = TO_MTD(file);
  181. char *kbuf;
  182. size_t retlen;
  183. size_t total_retlen=0;
  184. int ret=0;
  185. int len;
  186. DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
  187. if (*ppos == mtd->size)
  188. return -ENOSPC;
  189. if (*ppos + count > mtd->size)
  190. count = mtd->size - *ppos;
  191. if (!count)
  192. return 0;
  193. if (count > MAX_KMALLOC_SIZE)
  194. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  195. else
  196. kbuf=kmalloc(count, GFP_KERNEL);
  197. if (!kbuf)
  198. return -ENOMEM;
  199. while (count) {
  200. if (count > MAX_KMALLOC_SIZE)
  201. len = MAX_KMALLOC_SIZE;
  202. else
  203. len = count;
  204. if (copy_from_user(kbuf, buf, len)) {
  205. kfree(kbuf);
  206. return -EFAULT;
  207. }
  208. switch (MTD_MODE(file)) {
  209. case MTD_MODE_OTP_FACT:
  210. ret = -EROFS;
  211. break;
  212. case MTD_MODE_OTP_USER:
  213. if (!mtd->write_user_prot_reg) {
  214. ret = -EOPNOTSUPP;
  215. break;
  216. }
  217. ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  218. break;
  219. default:
  220. ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
  221. }
  222. if (!ret) {
  223. *ppos += retlen;
  224. total_retlen += retlen;
  225. count -= retlen;
  226. buf += retlen;
  227. }
  228. else {
  229. kfree(kbuf);
  230. return ret;
  231. }
  232. }
  233. kfree(kbuf);
  234. return total_retlen;
  235. } /* mtd_write */
  236. /*======================================================================
  237. IOCTL calls for getting device parameters.
  238. ======================================================================*/
  239. static void mtdchar_erase_callback (struct erase_info *instr)
  240. {
  241. wake_up((wait_queue_head_t *)instr->priv);
  242. }
  243. static int mtd_ioctl(struct inode *inode, struct file *file,
  244. u_int cmd, u_long arg)
  245. {
  246. struct mtd_info *mtd = TO_MTD(file);
  247. void __user *argp = (void __user *)arg;
  248. int ret = 0;
  249. u_long size;
  250. DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
  251. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  252. if (cmd & IOC_IN) {
  253. if (!access_ok(VERIFY_READ, argp, size))
  254. return -EFAULT;
  255. }
  256. if (cmd & IOC_OUT) {
  257. if (!access_ok(VERIFY_WRITE, argp, size))
  258. return -EFAULT;
  259. }
  260. switch (cmd) {
  261. case MEMGETREGIONCOUNT:
  262. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  263. return -EFAULT;
  264. break;
  265. case MEMGETREGIONINFO:
  266. {
  267. struct region_info_user ur;
  268. if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
  269. return -EFAULT;
  270. if (ur.regionindex >= mtd->numeraseregions)
  271. return -EINVAL;
  272. if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
  273. sizeof(struct mtd_erase_region_info)))
  274. return -EFAULT;
  275. break;
  276. }
  277. case MEMGETINFO:
  278. if (copy_to_user(argp, mtd, sizeof(struct mtd_info_user)))
  279. return -EFAULT;
  280. break;
  281. case MEMERASE:
  282. {
  283. struct erase_info *erase;
  284. if(!(file->f_mode & 2))
  285. return -EPERM;
  286. erase=kmalloc(sizeof(struct erase_info),GFP_KERNEL);
  287. if (!erase)
  288. ret = -ENOMEM;
  289. else {
  290. wait_queue_head_t waitq;
  291. DECLARE_WAITQUEUE(wait, current);
  292. init_waitqueue_head(&waitq);
  293. memset (erase,0,sizeof(struct erase_info));
  294. if (copy_from_user(&erase->addr, argp,
  295. sizeof(struct erase_info_user))) {
  296. kfree(erase);
  297. return -EFAULT;
  298. }
  299. erase->mtd = mtd;
  300. erase->callback = mtdchar_erase_callback;
  301. erase->priv = (unsigned long)&waitq;
  302. /*
  303. FIXME: Allow INTERRUPTIBLE. Which means
  304. not having the wait_queue head on the stack.
  305. If the wq_head is on the stack, and we
  306. leave because we got interrupted, then the
  307. wq_head is no longer there when the
  308. callback routine tries to wake us up.
  309. */
  310. ret = mtd->erase(mtd, erase);
  311. if (!ret) {
  312. set_current_state(TASK_UNINTERRUPTIBLE);
  313. add_wait_queue(&waitq, &wait);
  314. if (erase->state != MTD_ERASE_DONE &&
  315. erase->state != MTD_ERASE_FAILED)
  316. schedule();
  317. remove_wait_queue(&waitq, &wait);
  318. set_current_state(TASK_RUNNING);
  319. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  320. }
  321. kfree(erase);
  322. }
  323. break;
  324. }
  325. case MEMWRITEOOB:
  326. {
  327. struct mtd_oob_buf buf;
  328. struct mtd_oob_ops ops;
  329. if(!(file->f_mode & 2))
  330. return -EPERM;
  331. if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  332. return -EFAULT;
  333. if (buf.length > 4096)
  334. return -EINVAL;
  335. if (!mtd->write_oob)
  336. ret = -EOPNOTSUPP;
  337. else
  338. ret = access_ok(VERIFY_READ, buf.ptr,
  339. buf.length) ? 0 : EFAULT;
  340. if (ret)
  341. return ret;
  342. ops.len = buf.length;
  343. ops.ooblen = mtd->oobsize;
  344. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  345. ops.datbuf = NULL;
  346. ops.mode = MTD_OOB_PLACE;
  347. if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
  348. return -EINVAL;
  349. ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
  350. if (!ops.oobbuf)
  351. return -ENOMEM;
  352. if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
  353. kfree(ops.oobbuf);
  354. return -EFAULT;
  355. }
  356. buf.start &= ~(mtd->oobsize - 1);
  357. ret = mtd->write_oob(mtd, buf.start, &ops);
  358. if (copy_to_user(argp + sizeof(uint32_t), &ops.retlen,
  359. sizeof(uint32_t)))
  360. ret = -EFAULT;
  361. kfree(ops.oobbuf);
  362. break;
  363. }
  364. case MEMREADOOB:
  365. {
  366. struct mtd_oob_buf buf;
  367. struct mtd_oob_ops ops;
  368. if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  369. return -EFAULT;
  370. if (buf.length > 4096)
  371. return -EINVAL;
  372. if (!mtd->read_oob)
  373. ret = -EOPNOTSUPP;
  374. else
  375. ret = access_ok(VERIFY_WRITE, buf.ptr,
  376. buf.length) ? 0 : -EFAULT;
  377. if (ret)
  378. return ret;
  379. ops.len = buf.length;
  380. ops.ooblen = mtd->oobsize;
  381. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  382. ops.datbuf = NULL;
  383. ops.mode = MTD_OOB_PLACE;
  384. if (ops.ooboffs && ops.len > (ops.ooblen - ops.ooboffs))
  385. return -EINVAL;
  386. ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
  387. if (!ops.oobbuf)
  388. return -ENOMEM;
  389. buf.start &= ~(mtd->oobsize - 1);
  390. ret = mtd->read_oob(mtd, buf.start, &ops);
  391. if (put_user(ops.retlen, (uint32_t __user *)argp))
  392. ret = -EFAULT;
  393. else if (ops.retlen && copy_to_user(buf.ptr, ops.oobbuf,
  394. ops.retlen))
  395. ret = -EFAULT;
  396. kfree(ops.oobbuf);
  397. break;
  398. }
  399. case MEMLOCK:
  400. {
  401. struct erase_info_user info;
  402. if (copy_from_user(&info, argp, sizeof(info)))
  403. return -EFAULT;
  404. if (!mtd->lock)
  405. ret = -EOPNOTSUPP;
  406. else
  407. ret = mtd->lock(mtd, info.start, info.length);
  408. break;
  409. }
  410. case MEMUNLOCK:
  411. {
  412. struct erase_info_user info;
  413. if (copy_from_user(&info, argp, sizeof(info)))
  414. return -EFAULT;
  415. if (!mtd->unlock)
  416. ret = -EOPNOTSUPP;
  417. else
  418. ret = mtd->unlock(mtd, info.start, info.length);
  419. break;
  420. }
  421. /* Legacy interface */
  422. case MEMGETOOBSEL:
  423. {
  424. struct nand_oobinfo oi;
  425. if (!mtd->ecclayout)
  426. return -EOPNOTSUPP;
  427. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  428. return -EINVAL;
  429. oi.useecc = MTD_NANDECC_AUTOPLACE;
  430. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  431. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  432. sizeof(oi.oobfree));
  433. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  434. return -EFAULT;
  435. break;
  436. }
  437. case ECCGETLAYOUT:
  438. if (!mtd->ecclayout)
  439. return -EOPNOTSUPP;
  440. if (copy_to_user(argp, &mtd->ecclayout,
  441. sizeof(struct nand_ecclayout)))
  442. return -EFAULT;
  443. break;
  444. case MEMGETBADBLOCK:
  445. {
  446. loff_t offs;
  447. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  448. return -EFAULT;
  449. if (!mtd->block_isbad)
  450. ret = -EOPNOTSUPP;
  451. else
  452. return mtd->block_isbad(mtd, offs);
  453. break;
  454. }
  455. case MEMSETBADBLOCK:
  456. {
  457. loff_t offs;
  458. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  459. return -EFAULT;
  460. if (!mtd->block_markbad)
  461. ret = -EOPNOTSUPP;
  462. else
  463. return mtd->block_markbad(mtd, offs);
  464. break;
  465. }
  466. #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
  467. case OTPSELECT:
  468. {
  469. int mode;
  470. if (copy_from_user(&mode, argp, sizeof(int)))
  471. return -EFAULT;
  472. SET_MTD_MODE(file, 0);
  473. switch (mode) {
  474. case MTD_OTP_FACTORY:
  475. if (!mtd->read_fact_prot_reg)
  476. ret = -EOPNOTSUPP;
  477. else
  478. SET_MTD_MODE(file, MTD_MODE_OTP_FACT);
  479. break;
  480. case MTD_OTP_USER:
  481. if (!mtd->read_fact_prot_reg)
  482. ret = -EOPNOTSUPP;
  483. else
  484. SET_MTD_MODE(file, MTD_MODE_OTP_USER);
  485. break;
  486. default:
  487. ret = -EINVAL;
  488. case MTD_OTP_OFF:
  489. break;
  490. }
  491. file->f_pos = 0;
  492. break;
  493. }
  494. case OTPGETREGIONCOUNT:
  495. case OTPGETREGIONINFO:
  496. {
  497. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  498. if (!buf)
  499. return -ENOMEM;
  500. ret = -EOPNOTSUPP;
  501. switch (MTD_MODE(file)) {
  502. case MTD_MODE_OTP_FACT:
  503. if (mtd->get_fact_prot_info)
  504. ret = mtd->get_fact_prot_info(mtd, buf, 4096);
  505. break;
  506. case MTD_MODE_OTP_USER:
  507. if (mtd->get_user_prot_info)
  508. ret = mtd->get_user_prot_info(mtd, buf, 4096);
  509. break;
  510. }
  511. if (ret >= 0) {
  512. if (cmd == OTPGETREGIONCOUNT) {
  513. int nbr = ret / sizeof(struct otp_info);
  514. ret = copy_to_user(argp, &nbr, sizeof(int));
  515. } else
  516. ret = copy_to_user(argp, buf, ret);
  517. if (ret)
  518. ret = -EFAULT;
  519. }
  520. kfree(buf);
  521. break;
  522. }
  523. case OTPLOCK:
  524. {
  525. struct otp_info info;
  526. if (MTD_MODE(file) != MTD_MODE_OTP_USER)
  527. return -EINVAL;
  528. if (copy_from_user(&info, argp, sizeof(info)))
  529. return -EFAULT;
  530. if (!mtd->lock_user_prot_reg)
  531. return -EOPNOTSUPP;
  532. ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
  533. break;
  534. }
  535. #endif
  536. default:
  537. ret = -ENOTTY;
  538. }
  539. return ret;
  540. } /* memory_ioctl */
  541. static struct file_operations mtd_fops = {
  542. .owner = THIS_MODULE,
  543. .llseek = mtd_lseek,
  544. .read = mtd_read,
  545. .write = mtd_write,
  546. .ioctl = mtd_ioctl,
  547. .open = mtd_open,
  548. .release = mtd_close,
  549. };
  550. static int __init init_mtdchar(void)
  551. {
  552. if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
  553. printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
  554. MTD_CHAR_MAJOR);
  555. return -EAGAIN;
  556. }
  557. mtd_class = class_create(THIS_MODULE, "mtd");
  558. if (IS_ERR(mtd_class)) {
  559. printk(KERN_ERR "Error creating mtd class.\n");
  560. unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
  561. return PTR_ERR(mtd_class);
  562. }
  563. register_mtd_user(&notifier);
  564. return 0;
  565. }
  566. static void __exit cleanup_mtdchar(void)
  567. {
  568. unregister_mtd_user(&notifier);
  569. class_destroy(mtd_class);
  570. unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
  571. }
  572. module_init(init_mtdchar);
  573. module_exit(cleanup_mtdchar);
  574. MODULE_LICENSE("GPL");
  575. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  576. MODULE_DESCRIPTION("Direct character-device access to MTD devices");