mtdchar.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*
  2. * $Id: mtdchar.c,v 1.1.1.1 2007/06/12 07:27:09 eyryu Exp $
  3. *
  4. * Character-device access to raw MTD devices.
  5. *
  6. */
  7. #include <linux/device.h>
  8. #include <linux/fs.h>
  9. #include <linux/err.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. * Data structure to hold the pointer to the mtd device as well
  42. * as mode information ofr various use cases.
  43. */
  44. struct mtd_file_info {
  45. struct mtd_info *mtd;
  46. enum mtd_file_modes mode;
  47. };
  48. static loff_t mtd_lseek (struct file *file, loff_t offset, int orig)
  49. {
  50. struct mtd_file_info *mfi = file->private_data;
  51. struct mtd_info *mtd = mfi->mtd;
  52. switch (orig) {
  53. case SEEK_SET:
  54. break;
  55. case SEEK_CUR:
  56. offset += file->f_pos;
  57. break;
  58. case SEEK_END:
  59. offset += mtd->size;
  60. break;
  61. default:
  62. return -EINVAL;
  63. }
  64. if (offset >= 0 && offset <= mtd->size)
  65. return file->f_pos = offset;
  66. return -EINVAL;
  67. }
  68. static int mtd_open(struct inode *inode, struct file *file)
  69. {
  70. int minor = iminor(inode);
  71. int devnum = minor >> 1;
  72. struct mtd_info *mtd;
  73. struct mtd_file_info *mfi;
  74. DEBUG(MTD_DEBUG_LEVEL0, "MTD_open\n");
  75. if (devnum >= MAX_MTD_DEVICES)
  76. return -ENODEV;
  77. /* You can't open the RO devices RW */
  78. if ((file->f_mode & 2) && (minor & 1))
  79. return -EACCES;
  80. mtd = get_mtd_device(NULL, devnum);
  81. if (IS_ERR(mtd))
  82. return PTR_ERR(mtd);
  83. if (MTD_ABSENT == mtd->type) {
  84. put_mtd_device(mtd);
  85. return -ENODEV;
  86. }
  87. /* You can't open it RW if it's not a writeable device */
  88. if ((file->f_mode & 2) && !(mtd->flags & MTD_WRITEABLE)) {
  89. put_mtd_device(mtd);
  90. return -EACCES;
  91. }
  92. mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
  93. if (!mfi) {
  94. put_mtd_device(mtd);
  95. return -ENOMEM;
  96. }
  97. mfi->mtd = mtd;
  98. file->private_data = mfi;
  99. return 0;
  100. } /* mtd_open */
  101. /*====================================================================*/
  102. static int mtd_close(struct inode *inode, struct file *file)
  103. {
  104. struct mtd_file_info *mfi = file->private_data;
  105. struct mtd_info *mtd = mfi->mtd;
  106. DEBUG(MTD_DEBUG_LEVEL0, "MTD_close\n");
  107. if (mtd->sync)
  108. mtd->sync(mtd);
  109. put_mtd_device(mtd);
  110. file->private_data = NULL;
  111. kfree(mfi);
  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_file_info *mfi = file->private_data;
  121. struct mtd_info *mtd = mfi->mtd;
  122. size_t retlen=0;
  123. size_t total_retlen=0;
  124. int ret=0;
  125. int len;
  126. char *kbuf;
  127. DEBUG(MTD_DEBUG_LEVEL0,"MTD_read\n");
  128. if (*ppos + count > mtd->size)
  129. count = mtd->size - *ppos;
  130. if (!count)
  131. return 0;
  132. /* FIXME: Use kiovec in 2.5 to lock down the user's buffers
  133. and pass them directly to the MTD functions */
  134. if (count > MAX_KMALLOC_SIZE)
  135. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  136. else
  137. kbuf=kmalloc(count, GFP_KERNEL);
  138. if (!kbuf)
  139. return -ENOMEM;
  140. while (count) {
  141. if (count > MAX_KMALLOC_SIZE)
  142. len = MAX_KMALLOC_SIZE;
  143. else
  144. len = count;
  145. switch (mfi->mode) {
  146. case MTD_MODE_OTP_FACTORY:
  147. ret = mtd->read_fact_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  148. break;
  149. case MTD_MODE_OTP_USER:
  150. ret = mtd->read_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  151. break;
  152. case MTD_MODE_RAW:
  153. {
  154. struct mtd_oob_ops ops;
  155. ops.mode = MTD_OOB_RAW;
  156. ops.datbuf = kbuf;
  157. ops.oobbuf = NULL;
  158. ops.len = len;
  159. ret = mtd->read_oob(mtd, *ppos, &ops);
  160. retlen = ops.retlen;
  161. break;
  162. }
  163. default:
  164. ret = mtd->read(mtd, *ppos, len, &retlen, kbuf);
  165. }
  166. /* Nand returns -EBADMSG on ecc errors, but it returns
  167. * the data. For our userspace tools it is important
  168. * to dump areas with ecc errors !
  169. * For kernel internal usage it also might return -EUCLEAN
  170. * to signal the caller that a bitflip has occured and has
  171. * been corrected by the ECC algorithm.
  172. * Userspace software which accesses NAND this way
  173. * must be aware of the fact that it deals with NAND
  174. */
  175. if (!ret || (ret == -EUCLEAN) || (ret == -EBADMSG)) {
  176. *ppos += retlen;
  177. if (copy_to_user(buf, kbuf, retlen)) {
  178. kfree(kbuf);
  179. return -EFAULT;
  180. }
  181. else
  182. total_retlen += retlen;
  183. count -= retlen;
  184. buf += retlen;
  185. if (retlen == 0)
  186. count = 0;
  187. }
  188. else {
  189. kfree(kbuf);
  190. return ret;
  191. }
  192. }
  193. kfree(kbuf);
  194. return total_retlen;
  195. } /* mtd_read */
  196. static ssize_t mtd_write(struct file *file, const char __user *buf, size_t count,loff_t *ppos)
  197. {
  198. struct mtd_file_info *mfi = file->private_data;
  199. struct mtd_info *mtd = mfi->mtd;
  200. char *kbuf;
  201. size_t retlen;
  202. size_t total_retlen=0;
  203. int ret=0;
  204. int len;
  205. DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
  206. if (*ppos == mtd->size)
  207. return -ENOSPC;
  208. if (*ppos + count > mtd->size)
  209. count = mtd->size - *ppos;
  210. if (!count)
  211. return 0;
  212. if (count > MAX_KMALLOC_SIZE)
  213. kbuf=kmalloc(MAX_KMALLOC_SIZE, GFP_KERNEL);
  214. else
  215. kbuf=kmalloc(count, GFP_KERNEL);
  216. if (!kbuf)
  217. return -ENOMEM;
  218. while (count) {
  219. if (count > MAX_KMALLOC_SIZE)
  220. len = MAX_KMALLOC_SIZE;
  221. else
  222. len = count;
  223. if (copy_from_user(kbuf, buf, len)) {
  224. kfree(kbuf);
  225. return -EFAULT;
  226. }
  227. switch (mfi->mode) {
  228. case MTD_MODE_OTP_FACTORY:
  229. ret = -EROFS;
  230. break;
  231. case MTD_MODE_OTP_USER:
  232. if (!mtd->write_user_prot_reg) {
  233. ret = -EOPNOTSUPP;
  234. break;
  235. }
  236. ret = mtd->write_user_prot_reg(mtd, *ppos, len, &retlen, kbuf);
  237. break;
  238. case MTD_MODE_RAW:
  239. {
  240. struct mtd_oob_ops ops;
  241. ops.mode = MTD_OOB_RAW;
  242. ops.datbuf = kbuf;
  243. ops.oobbuf = NULL;
  244. ops.len = len;
  245. ret = mtd->write_oob(mtd, *ppos, &ops);
  246. retlen = ops.retlen;
  247. break;
  248. }
  249. default:
  250. ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
  251. }
  252. if (!ret) {
  253. *ppos += retlen;
  254. total_retlen += retlen;
  255. count -= retlen;
  256. buf += retlen;
  257. }
  258. else {
  259. kfree(kbuf);
  260. return ret;
  261. }
  262. }
  263. kfree(kbuf);
  264. return total_retlen;
  265. } /* mtd_write */
  266. /*======================================================================
  267. IOCTL calls for getting device parameters.
  268. ======================================================================*/
  269. static void mtdchar_erase_callback (struct erase_info *instr)
  270. {
  271. wake_up((wait_queue_head_t *)instr->priv);
  272. }
  273. #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
  274. static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
  275. {
  276. struct mtd_info *mtd = mfi->mtd;
  277. int ret = 0;
  278. switch (mode) {
  279. case MTD_OTP_FACTORY:
  280. if (!mtd->read_fact_prot_reg)
  281. ret = -EOPNOTSUPP;
  282. else
  283. mfi->mode = MTD_MODE_OTP_FACTORY;
  284. break;
  285. case MTD_OTP_USER:
  286. if (!mtd->read_fact_prot_reg)
  287. ret = -EOPNOTSUPP;
  288. else
  289. mfi->mode = MTD_MODE_OTP_USER;
  290. break;
  291. default:
  292. ret = -EINVAL;
  293. case MTD_OTP_OFF:
  294. break;
  295. }
  296. return ret;
  297. }
  298. #else
  299. # define otp_select_filemode(f,m) -EOPNOTSUPP
  300. #endif
  301. static int mtd_ioctl(struct inode *inode, struct file *file,
  302. u_int cmd, u_long arg)
  303. {
  304. struct mtd_file_info *mfi = file->private_data;
  305. struct mtd_info *mtd = mfi->mtd;
  306. void __user *argp = (void __user *)arg;
  307. int ret = 0;
  308. u_long size;
  309. struct mtd_info_user info;
  310. DEBUG(MTD_DEBUG_LEVEL0, "MTD_ioctl\n");
  311. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  312. if (cmd & IOC_IN) {
  313. if (!access_ok(VERIFY_READ, argp, size))
  314. return -EFAULT;
  315. }
  316. if (cmd & IOC_OUT) {
  317. if (!access_ok(VERIFY_WRITE, argp, size))
  318. return -EFAULT;
  319. }
  320. switch (cmd) {
  321. case MEMGETREGIONCOUNT:
  322. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  323. return -EFAULT;
  324. break;
  325. case MEMGETREGIONINFO:
  326. {
  327. struct region_info_user ur;
  328. if (copy_from_user(&ur, argp, sizeof(struct region_info_user)))
  329. return -EFAULT;
  330. if (ur.regionindex >= mtd->numeraseregions)
  331. return -EINVAL;
  332. if (copy_to_user(argp, &(mtd->eraseregions[ur.regionindex]),
  333. sizeof(struct mtd_erase_region_info)))
  334. return -EFAULT;
  335. break;
  336. }
  337. case MEMGETINFO:
  338. info.type = mtd->type;
  339. info.flags = mtd->flags;
  340. info.size = mtd->size;
  341. info.erasesize = mtd->erasesize;
  342. info.writesize = mtd->writesize;
  343. info.oobsize = mtd->oobsize;
  344. /* The below fields are obsolete */
  345. info.ecctype = -1;
  346. info.eccsize = 0;
  347. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  348. return -EFAULT;
  349. break;
  350. case MEMERASE:
  351. {
  352. struct erase_info *erase;
  353. if(!(file->f_mode & 2))
  354. return -EPERM;
  355. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  356. if (!erase)
  357. ret = -ENOMEM;
  358. else {
  359. wait_queue_head_t waitq;
  360. DECLARE_WAITQUEUE(wait, current);
  361. init_waitqueue_head(&waitq);
  362. if (copy_from_user(&erase->addr, argp,
  363. sizeof(struct erase_info_user))) {
  364. kfree(erase);
  365. return -EFAULT;
  366. }
  367. erase->mtd = mtd;
  368. erase->callback = mtdchar_erase_callback;
  369. erase->priv = (unsigned long)&waitq;
  370. /*
  371. FIXME: Allow INTERRUPTIBLE. Which means
  372. not having the wait_queue head on the stack.
  373. If the wq_head is on the stack, and we
  374. leave because we got interrupted, then the
  375. wq_head is no longer there when the
  376. callback routine tries to wake us up.
  377. */
  378. ret = mtd->erase(mtd, erase);
  379. if (!ret) {
  380. set_current_state(TASK_UNINTERRUPTIBLE);
  381. add_wait_queue(&waitq, &wait);
  382. if (erase->state != MTD_ERASE_DONE &&
  383. erase->state != MTD_ERASE_FAILED)
  384. schedule();
  385. remove_wait_queue(&waitq, &wait);
  386. set_current_state(TASK_RUNNING);
  387. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  388. }
  389. kfree(erase);
  390. }
  391. break;
  392. }
  393. case MEMWRITEOOB:
  394. {
  395. struct mtd_oob_buf buf;
  396. struct mtd_oob_ops ops;
  397. if(!(file->f_mode & 2))
  398. return -EPERM;
  399. if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  400. return -EFAULT;
  401. if (buf.length > 4096)
  402. return -EINVAL;
  403. if (!mtd->write_oob)
  404. ret = -EOPNOTSUPP;
  405. else
  406. ret = access_ok(VERIFY_READ, buf.ptr,
  407. buf.length) ? 0 : EFAULT;
  408. if (ret)
  409. return ret;
  410. ops.ooblen = buf.length;
  411. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  412. ops.datbuf = NULL;
  413. ops.mode = MTD_OOB_PLACE;
  414. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  415. return -EINVAL;
  416. ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
  417. if (!ops.oobbuf)
  418. return -ENOMEM;
  419. if (copy_from_user(ops.oobbuf, buf.ptr, buf.length)) {
  420. kfree(ops.oobbuf);
  421. return -EFAULT;
  422. }
  423. buf.start &= ~(mtd->oobsize - 1);
  424. ret = mtd->write_oob(mtd, buf.start, &ops);
  425. if (copy_to_user(argp + sizeof(uint32_t), &ops.oobretlen,
  426. sizeof(uint32_t)))
  427. ret = -EFAULT;
  428. kfree(ops.oobbuf);
  429. break;
  430. }
  431. case MEMREADOOB:
  432. {
  433. struct mtd_oob_buf buf;
  434. struct mtd_oob_ops ops;
  435. if (copy_from_user(&buf, argp, sizeof(struct mtd_oob_buf)))
  436. return -EFAULT;
  437. if (buf.length > 4096)
  438. return -EINVAL;
  439. if (!mtd->read_oob)
  440. ret = -EOPNOTSUPP;
  441. else
  442. ret = access_ok(VERIFY_WRITE, buf.ptr,
  443. buf.length) ? 0 : -EFAULT;
  444. if (ret)
  445. return ret;
  446. ops.ooblen = buf.length;
  447. ops.ooboffs = buf.start & (mtd->oobsize - 1);
  448. ops.datbuf = NULL;
  449. ops.mode = MTD_OOB_PLACE;
  450. if (ops.ooboffs && ops.len > (mtd->oobsize - ops.ooboffs))
  451. return -EINVAL;
  452. ops.oobbuf = kmalloc(buf.length, GFP_KERNEL);
  453. if (!ops.oobbuf)
  454. return -ENOMEM;
  455. buf.start &= ~(mtd->oobsize - 1);
  456. ret = mtd->read_oob(mtd, buf.start, &ops);
  457. if (put_user(ops.oobretlen, (uint32_t __user *)argp))
  458. ret = -EFAULT;
  459. else if (ops.oobretlen && copy_to_user(buf.ptr, ops.oobbuf,
  460. ops.oobretlen))
  461. ret = -EFAULT;
  462. kfree(ops.oobbuf);
  463. break;
  464. }
  465. case MEMLOCK:
  466. {
  467. struct erase_info_user info;
  468. if (copy_from_user(&info, argp, sizeof(info)))
  469. return -EFAULT;
  470. if (!mtd->lock)
  471. ret = -EOPNOTSUPP;
  472. else
  473. ret = mtd->lock(mtd, info.start, info.length);
  474. break;
  475. }
  476. case MEMUNLOCK:
  477. {
  478. struct erase_info_user info;
  479. if (copy_from_user(&info, argp, sizeof(info)))
  480. return -EFAULT;
  481. if (!mtd->unlock)
  482. ret = -EOPNOTSUPP;
  483. else
  484. ret = mtd->unlock(mtd, info.start, info.length);
  485. break;
  486. }
  487. /* Legacy interface */
  488. case MEMGETOOBSEL:
  489. {
  490. struct nand_oobinfo oi;
  491. if (!mtd->ecclayout)
  492. return -EOPNOTSUPP;
  493. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  494. return -EINVAL;
  495. oi.useecc = MTD_NANDECC_AUTOPLACE;
  496. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  497. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  498. sizeof(oi.oobfree));
  499. oi.eccbytes = mtd->ecclayout->eccbytes;
  500. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  501. return -EFAULT;
  502. break;
  503. }
  504. case MEMGETBADBLOCK:
  505. {
  506. loff_t offs;
  507. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  508. return -EFAULT;
  509. if (!mtd->block_isbad)
  510. ret = -EOPNOTSUPP;
  511. else
  512. return mtd->block_isbad(mtd, offs);
  513. break;
  514. }
  515. case MEMSETBADBLOCK:
  516. {
  517. loff_t offs;
  518. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  519. return -EFAULT;
  520. if (!mtd->block_markbad)
  521. ret = -EOPNOTSUPP;
  522. else
  523. return mtd->block_markbad(mtd, offs);
  524. break;
  525. }
  526. #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP)
  527. case OTPSELECT:
  528. {
  529. int mode;
  530. if (copy_from_user(&mode, argp, sizeof(int)))
  531. return -EFAULT;
  532. mfi->mode = MTD_MODE_NORMAL;
  533. ret = otp_select_filemode(mfi, mode);
  534. file->f_pos = 0;
  535. break;
  536. }
  537. case OTPGETREGIONCOUNT:
  538. case OTPGETREGIONINFO:
  539. {
  540. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  541. if (!buf)
  542. return -ENOMEM;
  543. ret = -EOPNOTSUPP;
  544. switch (mfi->mode) {
  545. case MTD_MODE_OTP_FACTORY:
  546. if (mtd->get_fact_prot_info)
  547. ret = mtd->get_fact_prot_info(mtd, buf, 4096);
  548. break;
  549. case MTD_MODE_OTP_USER:
  550. if (mtd->get_user_prot_info)
  551. ret = mtd->get_user_prot_info(mtd, buf, 4096);
  552. break;
  553. default:
  554. break;
  555. }
  556. if (ret >= 0) {
  557. if (cmd == OTPGETREGIONCOUNT) {
  558. int nbr = ret / sizeof(struct otp_info);
  559. ret = copy_to_user(argp, &nbr, sizeof(int));
  560. } else
  561. ret = copy_to_user(argp, buf, ret);
  562. if (ret)
  563. ret = -EFAULT;
  564. }
  565. kfree(buf);
  566. break;
  567. }
  568. case OTPLOCK:
  569. {
  570. struct otp_info info;
  571. if (mfi->mode != MTD_MODE_OTP_USER)
  572. return -EINVAL;
  573. if (copy_from_user(&info, argp, sizeof(info)))
  574. return -EFAULT;
  575. if (!mtd->lock_user_prot_reg)
  576. return -EOPNOTSUPP;
  577. ret = mtd->lock_user_prot_reg(mtd, info.start, info.length);
  578. break;
  579. }
  580. #endif
  581. case ECCGETLAYOUT:
  582. {
  583. if (!mtd->ecclayout)
  584. return -EOPNOTSUPP;
  585. if (copy_to_user(argp, mtd->ecclayout,
  586. sizeof(struct nand_ecclayout)))
  587. return -EFAULT;
  588. break;
  589. }
  590. case ECCGETSTATS:
  591. {
  592. if (copy_to_user(argp, &mtd->ecc_stats,
  593. sizeof(struct mtd_ecc_stats)))
  594. return -EFAULT;
  595. break;
  596. }
  597. case MTDFILEMODE:
  598. {
  599. mfi->mode = 0;
  600. switch(arg) {
  601. case MTD_MODE_OTP_FACTORY:
  602. case MTD_MODE_OTP_USER:
  603. ret = otp_select_filemode(mfi, arg);
  604. break;
  605. case MTD_MODE_RAW:
  606. if (!mtd->read_oob || !mtd->write_oob)
  607. return -EOPNOTSUPP;
  608. mfi->mode = arg;
  609. case MTD_MODE_NORMAL:
  610. break;
  611. default:
  612. ret = -EINVAL;
  613. }
  614. file->f_pos = 0;
  615. break;
  616. }
  617. /* qisda tim.huang 090722 SD download Flag { */
  618. case MTDDOWNLOADFLAG:
  619. {
  620. struct erase_info *erase;
  621. unsigned int flag_org;
  622. /* qisda tim.huang 090826 change flag address { */
  623. if(mtd->index != 3) /*make sure it's MTD 3 */
  624. return -EINVAL;
  625. /* qisda tim.huang 090826 change flag address } */
  626. /* Erase Flag */
  627. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  628. if (!erase)
  629. ret = -ENOMEM;
  630. else
  631. {
  632. wait_queue_head_t waitq;
  633. DECLARE_WAITQUEUE(wait, current);
  634. init_waitqueue_head(&waitq);
  635. #if 0
  636. printk("Erase Flag from NAND \n");
  637. {
  638. printk("MTD mtd->size =%x \n", mtd->size);
  639. printk("MTD mtd->erasesize =%d \n", mtd->erasesize);
  640. printk("MTD mtd->writesize =%d \n", mtd->writesize);
  641. printk("MTD mtd->oobsize =%d \n", mtd->oobsize);
  642. printk("MTD region count =%d \n", mtd->numeraseregions);
  643. }
  644. #endif
  645. erase->addr= mtd->size - mtd->erasesize; // last block
  646. erase->len = mtd->erasesize; // one block
  647. erase->mtd = mtd;
  648. erase->callback = mtdchar_erase_callback;
  649. erase->priv = (unsigned long)&waitq;
  650. flag_org = mtd->flags;
  651. mtd->flags = flag_org | MTD_WRITEABLE;
  652. ret = mtd->erase(mtd, erase);
  653. if (!ret) {
  654. set_current_state(TASK_UNINTERRUPTIBLE);
  655. add_wait_queue(&waitq, &wait);
  656. if (erase->state != MTD_ERASE_DONE &&
  657. erase->state != MTD_ERASE_FAILED)
  658. schedule();
  659. remove_wait_queue(&waitq, &wait);
  660. set_current_state(TASK_RUNNING);
  661. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  662. }
  663. if (erase->state != MTD_ERASE_DONE)
  664. {
  665. kfree(erase);
  666. mtd->flags = flag_org;
  667. return ret;
  668. }
  669. kfree(erase);
  670. /* write Flag to NAND */
  671. {
  672. struct dl_conf *pdl;
  673. long retlen=0;
  674. loff_t to=0;
  675. long lBlkSize, lPageSize,lOffset;
  676. pdl = kzalloc(mtd->writesize,GFP_KERNEL);
  677. if (!pdl)
  678. ret = -ENOMEM;
  679. else
  680. {
  681. if (copy_from_user(pdl, argp, sizeof(dl_conf)))
  682. {
  683. kfree(pdl);
  684. return -EFAULT;
  685. }
  686. if(pdl->magic1 != DL_MAGIC_1 || pdl->magic2 != DL_MAGIC_2 )
  687. {
  688. kfree(pdl);
  689. mtd->flags = flag_org;
  690. return -EFAULT;
  691. }
  692. lBlkSize = mtd->erasesize;
  693. lPageSize = mtd->writesize;
  694. lOffset = (mtd->size - mtd->erasesize);
  695. to = lOffset;
  696. // printk("to = %x,%x \n", ((long*)&to)[0], ((long*)&to)[1]);
  697. ret = (*(mtd->write))(mtd, to, lPageSize, &retlen, pdl);
  698. printk("Set Download Flag = %x \n", ret);
  699. mtd->flags = flag_org;
  700. kfree(pdl);
  701. }
  702. }
  703. }
  704. break;
  705. }
  706. /* qisda tim.huang 090722 SD download Flag } */
  707. default:
  708. ret = -ENOTTY;
  709. }
  710. return ret;
  711. } /* memory_ioctl */
  712. static const struct file_operations mtd_fops = {
  713. .owner = THIS_MODULE,
  714. .llseek = mtd_lseek,
  715. .read = mtd_read,
  716. .write = mtd_write,
  717. .ioctl = mtd_ioctl,
  718. .open = mtd_open,
  719. .release = mtd_close,
  720. };
  721. static int __init init_mtdchar(void)
  722. {
  723. if (register_chrdev(MTD_CHAR_MAJOR, "mtd", &mtd_fops)) {
  724. printk(KERN_NOTICE "Can't allocate major number %d for Memory Technology Devices.\n",
  725. MTD_CHAR_MAJOR);
  726. return -EAGAIN;
  727. }
  728. mtd_class = class_create(THIS_MODULE, "mtd");
  729. if (IS_ERR(mtd_class)) {
  730. printk(KERN_ERR "Error creating mtd class.\n");
  731. unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
  732. return PTR_ERR(mtd_class);
  733. }
  734. register_mtd_user(&notifier);
  735. return 0;
  736. }
  737. static void __exit cleanup_mtdchar(void)
  738. {
  739. unregister_mtd_user(&notifier);
  740. class_destroy(mtd_class);
  741. unregister_chrdev(MTD_CHAR_MAJOR, "mtd");
  742. }
  743. module_init(init_mtdchar);
  744. module_exit(cleanup_mtdchar);
  745. MODULE_LICENSE("GPL");
  746. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  747. MODULE_DESCRIPTION("Direct character-device access to MTD devices");