mtdchar.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. /*
  2. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. */
  19. #include <linux/device.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #include <linux/mutex.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/compat.h>
  31. #include <linux/mount.h>
  32. #include <linux/blkpg.h>
  33. #include <linux/mtd/mtd.h>
  34. #include <linux/mtd/partitions.h>
  35. #include <linux/mtd/map.h>
  36. #include <asm/uaccess.h>
  37. #define MTD_INODE_FS_MAGIC 0x11307854
  38. static DEFINE_MUTEX(mtd_mutex);
  39. static struct vfsmount *mtd_inode_mnt __read_mostly;
  40. /*
  41. * Data structure to hold the pointer to the mtd device as well
  42. * as mode information of various use cases.
  43. */
  44. struct mtd_file_info {
  45. struct mtd_info *mtd;
  46. struct inode *ino;
  47. enum mtd_file_modes mode;
  48. };
  49. static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
  50. {
  51. struct mtd_file_info *mfi = file->private_data;
  52. struct mtd_info *mtd = mfi->mtd;
  53. switch (orig) {
  54. case SEEK_SET:
  55. break;
  56. case SEEK_CUR:
  57. offset += file->f_pos;
  58. break;
  59. case SEEK_END:
  60. offset += mtd->size;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. if (offset >= 0 && offset <= mtd->size)
  66. return file->f_pos = offset;
  67. return -EINVAL;
  68. }
  69. static int mtdchar_open(struct inode *inode, struct file *file)
  70. {
  71. int minor = iminor(inode);
  72. int devnum = minor >> 1;
  73. int ret = 0;
  74. struct mtd_info *mtd;
  75. struct mtd_file_info *mfi;
  76. struct inode *mtd_ino;
  77. pr_debug("MTD_open\n");
  78. /* You can't open the RO devices RW */
  79. if ((file->f_mode & FMODE_WRITE) && (minor & 1))
  80. return -EACCES;
  81. mutex_lock(&mtd_mutex);
  82. mtd = get_mtd_device(NULL, devnum);
  83. if (IS_ERR(mtd)) {
  84. ret = PTR_ERR(mtd);
  85. goto out;
  86. }
  87. if (mtd->type == MTD_ABSENT) {
  88. put_mtd_device(mtd);
  89. ret = -ENODEV;
  90. goto out;
  91. }
  92. mtd_ino = iget_locked(mtd_inode_mnt->mnt_sb, devnum);
  93. if (!mtd_ino) {
  94. put_mtd_device(mtd);
  95. ret = -ENOMEM;
  96. goto out;
  97. }
  98. if (mtd_ino->i_state & I_NEW) {
  99. mtd_ino->i_private = mtd;
  100. mtd_ino->i_mode = S_IFCHR;
  101. mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
  102. unlock_new_inode(mtd_ino);
  103. }
  104. file->f_mapping = mtd_ino->i_mapping;
  105. /* You can't open it RW if it's not a writeable device */
  106. if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
  107. iput(mtd_ino);
  108. put_mtd_device(mtd);
  109. ret = -EACCES;
  110. goto out;
  111. }
  112. mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
  113. if (!mfi) {
  114. iput(mtd_ino);
  115. put_mtd_device(mtd);
  116. ret = -ENOMEM;
  117. goto out;
  118. }
  119. mfi->ino = mtd_ino;
  120. mfi->mtd = mtd;
  121. file->private_data = mfi;
  122. out:
  123. mutex_unlock(&mtd_mutex);
  124. return ret;
  125. } /* mtdchar_open */
  126. /*====================================================================*/
  127. static int mtdchar_close(struct inode *inode, struct file *file)
  128. {
  129. struct mtd_file_info *mfi = file->private_data;
  130. struct mtd_info *mtd = mfi->mtd;
  131. pr_debug("MTD_close\n");
  132. /* Only sync if opened RW */
  133. if ((file->f_mode & FMODE_WRITE) && mtd->sync)
  134. mtd_sync(mtd);
  135. iput(mfi->ino);
  136. put_mtd_device(mtd);
  137. file->private_data = NULL;
  138. kfree(mfi);
  139. return 0;
  140. } /* mtdchar_close */
  141. /* Back in June 2001, dwmw2 wrote:
  142. *
  143. * FIXME: This _really_ needs to die. In 2.5, we should lock the
  144. * userspace buffer down and use it directly with readv/writev.
  145. *
  146. * The implementation below, using mtd_kmalloc_up_to, mitigates
  147. * allocation failures when the system is under low-memory situations
  148. * or if memory is highly fragmented at the cost of reducing the
  149. * performance of the requested transfer due to a smaller buffer size.
  150. *
  151. * A more complex but more memory-efficient implementation based on
  152. * get_user_pages and iovecs to cover extents of those pages is a
  153. * longer-term goal, as intimated by dwmw2 above. However, for the
  154. * write case, this requires yet more complex head and tail transfer
  155. * handling when those head and tail offsets and sizes are such that
  156. * alignment requirements are not met in the NAND subdriver.
  157. */
  158. static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
  159. loff_t *ppos)
  160. {
  161. struct mtd_file_info *mfi = file->private_data;
  162. struct mtd_info *mtd = mfi->mtd;
  163. size_t retlen;
  164. size_t total_retlen=0;
  165. int ret=0;
  166. int len;
  167. size_t size = count;
  168. char *kbuf;
  169. pr_debug("MTD_read\n");
  170. if (*ppos + count > mtd->size)
  171. count = mtd->size - *ppos;
  172. if (!count)
  173. return 0;
  174. kbuf = mtd_kmalloc_up_to(mtd, &size);
  175. if (!kbuf)
  176. return -ENOMEM;
  177. while (count) {
  178. len = min_t(size_t, count, size);
  179. switch (mfi->mode) {
  180. case MTD_FILE_MODE_OTP_FACTORY:
  181. ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
  182. &retlen, kbuf);
  183. break;
  184. case MTD_FILE_MODE_OTP_USER:
  185. ret = mtd_read_user_prot_reg(mtd, *ppos, len,
  186. &retlen, kbuf);
  187. break;
  188. case MTD_FILE_MODE_RAW:
  189. {
  190. struct mtd_oob_ops ops;
  191. ops.mode = MTD_OPS_RAW;
  192. ops.datbuf = kbuf;
  193. ops.oobbuf = NULL;
  194. ops.len = len;
  195. ret = mtd_read_oob(mtd, *ppos, &ops);
  196. retlen = ops.retlen;
  197. break;
  198. }
  199. default:
  200. ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
  201. }
  202. /* Nand returns -EBADMSG on ECC errors, but it returns
  203. * the data. For our userspace tools it is important
  204. * to dump areas with ECC errors!
  205. * For kernel internal usage it also might return -EUCLEAN
  206. * to signal the caller that a bitflip has occurred and has
  207. * been corrected by the ECC algorithm.
  208. * Userspace software which accesses NAND this way
  209. * must be aware of the fact that it deals with NAND
  210. */
  211. if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
  212. *ppos += retlen;
  213. if (copy_to_user(buf, kbuf, retlen)) {
  214. kfree(kbuf);
  215. return -EFAULT;
  216. }
  217. else
  218. total_retlen += retlen;
  219. count -= retlen;
  220. buf += retlen;
  221. if (retlen == 0)
  222. count = 0;
  223. }
  224. else {
  225. kfree(kbuf);
  226. return ret;
  227. }
  228. }
  229. kfree(kbuf);
  230. return total_retlen;
  231. } /* mtdchar_read */
  232. static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
  233. loff_t *ppos)
  234. {
  235. struct mtd_file_info *mfi = file->private_data;
  236. struct mtd_info *mtd = mfi->mtd;
  237. size_t size = count;
  238. char *kbuf;
  239. size_t retlen;
  240. size_t total_retlen=0;
  241. int ret=0;
  242. int len;
  243. pr_debug("MTD_write\n");
  244. if (*ppos == mtd->size)
  245. return -ENOSPC;
  246. if (*ppos + count > mtd->size)
  247. count = mtd->size - *ppos;
  248. if (!count)
  249. return 0;
  250. kbuf = mtd_kmalloc_up_to(mtd, &size);
  251. if (!kbuf)
  252. return -ENOMEM;
  253. while (count) {
  254. len = min_t(size_t, count, size);
  255. if (copy_from_user(kbuf, buf, len)) {
  256. kfree(kbuf);
  257. return -EFAULT;
  258. }
  259. switch (mfi->mode) {
  260. case MTD_FILE_MODE_OTP_FACTORY:
  261. ret = -EROFS;
  262. break;
  263. case MTD_FILE_MODE_OTP_USER:
  264. ret = mtd_write_user_prot_reg(mtd, *ppos, len,
  265. &retlen, kbuf);
  266. break;
  267. case MTD_FILE_MODE_RAW:
  268. {
  269. struct mtd_oob_ops ops;
  270. ops.mode = MTD_OPS_RAW;
  271. ops.datbuf = kbuf;
  272. ops.oobbuf = NULL;
  273. ops.ooboffs = 0;
  274. ops.len = len;
  275. ret = mtd_write_oob(mtd, *ppos, &ops);
  276. retlen = ops.retlen;
  277. break;
  278. }
  279. default:
  280. ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
  281. }
  282. if (!ret) {
  283. *ppos += retlen;
  284. total_retlen += retlen;
  285. count -= retlen;
  286. buf += retlen;
  287. }
  288. else {
  289. kfree(kbuf);
  290. return ret;
  291. }
  292. }
  293. kfree(kbuf);
  294. return total_retlen;
  295. } /* mtdchar_write */
  296. /*======================================================================
  297. IOCTL calls for getting device parameters.
  298. ======================================================================*/
  299. static void mtdchar_erase_callback (struct erase_info *instr)
  300. {
  301. wake_up((wait_queue_head_t *)instr->priv);
  302. }
  303. #ifdef CONFIG_HAVE_MTD_OTP
  304. static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
  305. {
  306. struct mtd_info *mtd = mfi->mtd;
  307. size_t retlen;
  308. int ret = 0;
  309. /*
  310. * Make a fake call to mtd_read_fact_prot_reg() to check if OTP
  311. * operations are supported.
  312. */
  313. if (mtd_read_fact_prot_reg(mtd, -1, -1, &retlen, NULL) == -EOPNOTSUPP)
  314. return -EOPNOTSUPP;
  315. switch (mode) {
  316. case MTD_OTP_FACTORY:
  317. mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
  318. break;
  319. case MTD_OTP_USER:
  320. mfi->mode = MTD_FILE_MODE_OTP_USER;
  321. break;
  322. default:
  323. ret = -EINVAL;
  324. case MTD_OTP_OFF:
  325. break;
  326. }
  327. return ret;
  328. }
  329. #else
  330. # define otp_select_filemode(f,m) -EOPNOTSUPP
  331. #endif
  332. static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
  333. uint64_t start, uint32_t length, void __user *ptr,
  334. uint32_t __user *retp)
  335. {
  336. struct mtd_file_info *mfi = file->private_data;
  337. struct mtd_oob_ops ops;
  338. uint32_t retlen;
  339. int ret = 0;
  340. if (!(file->f_mode & FMODE_WRITE))
  341. return -EPERM;
  342. if (length > 4096)
  343. return -EINVAL;
  344. if (!mtd->write_oob)
  345. ret = -EOPNOTSUPP;
  346. else
  347. ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
  348. if (ret)
  349. return ret;
  350. ops.ooblen = length;
  351. ops.ooboffs = start & (mtd->writesize - 1);
  352. ops.datbuf = NULL;
  353. ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
  354. MTD_OPS_PLACE_OOB;
  355. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  356. return -EINVAL;
  357. ops.oobbuf = memdup_user(ptr, length);
  358. if (IS_ERR(ops.oobbuf))
  359. return PTR_ERR(ops.oobbuf);
  360. start &= ~((uint64_t)mtd->writesize - 1);
  361. ret = mtd_write_oob(mtd, start, &ops);
  362. if (ops.oobretlen > 0xFFFFFFFFU)
  363. ret = -EOVERFLOW;
  364. retlen = ops.oobretlen;
  365. if (copy_to_user(retp, &retlen, sizeof(length)))
  366. ret = -EFAULT;
  367. kfree(ops.oobbuf);
  368. return ret;
  369. }
  370. static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
  371. uint64_t start, uint32_t length, void __user *ptr,
  372. uint32_t __user *retp)
  373. {
  374. struct mtd_file_info *mfi = file->private_data;
  375. struct mtd_oob_ops ops;
  376. int ret = 0;
  377. if (length > 4096)
  378. return -EINVAL;
  379. if (!access_ok(VERIFY_WRITE, ptr, length))
  380. return -EFAULT;
  381. ops.ooblen = length;
  382. ops.ooboffs = start & (mtd->writesize - 1);
  383. ops.datbuf = NULL;
  384. ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
  385. MTD_OPS_PLACE_OOB;
  386. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  387. return -EINVAL;
  388. ops.oobbuf = kmalloc(length, GFP_KERNEL);
  389. if (!ops.oobbuf)
  390. return -ENOMEM;
  391. start &= ~((uint64_t)mtd->writesize - 1);
  392. ret = mtd_read_oob(mtd, start, &ops);
  393. if (put_user(ops.oobretlen, retp))
  394. ret = -EFAULT;
  395. else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
  396. ops.oobretlen))
  397. ret = -EFAULT;
  398. kfree(ops.oobbuf);
  399. /*
  400. * NAND returns -EBADMSG on ECC errors, but it returns the OOB
  401. * data. For our userspace tools it is important to dump areas
  402. * with ECC errors!
  403. * For kernel internal usage it also might return -EUCLEAN
  404. * to signal the caller that a bitflip has occured and has
  405. * been corrected by the ECC algorithm.
  406. *
  407. * Note: currently the standard NAND function, nand_read_oob_std,
  408. * does not calculate ECC for the OOB area, so do not rely on
  409. * this behavior unless you have replaced it with your own.
  410. */
  411. if (mtd_is_bitflip_or_eccerr(ret))
  412. return 0;
  413. return ret;
  414. }
  415. /*
  416. * Copies (and truncates, if necessary) data from the larger struct,
  417. * nand_ecclayout, to the smaller, deprecated layout struct,
  418. * nand_ecclayout_user. This is necessary only to support the deprecated
  419. * API ioctl ECCGETLAYOUT while allowing all new functionality to use
  420. * nand_ecclayout flexibly (i.e. the struct may change size in new
  421. * releases without requiring major rewrites).
  422. */
  423. static int shrink_ecclayout(const struct nand_ecclayout *from,
  424. struct nand_ecclayout_user *to)
  425. {
  426. int i;
  427. if (!from || !to)
  428. return -EINVAL;
  429. memset(to, 0, sizeof(*to));
  430. to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
  431. for (i = 0; i < to->eccbytes; i++)
  432. to->eccpos[i] = from->eccpos[i];
  433. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
  434. if (from->oobfree[i].length == 0 &&
  435. from->oobfree[i].offset == 0)
  436. break;
  437. to->oobavail += from->oobfree[i].length;
  438. to->oobfree[i] = from->oobfree[i];
  439. }
  440. return 0;
  441. }
  442. static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
  443. struct blkpg_ioctl_arg __user *arg)
  444. {
  445. struct blkpg_ioctl_arg a;
  446. struct blkpg_partition p;
  447. if (!capable(CAP_SYS_ADMIN))
  448. return -EPERM;
  449. if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
  450. return -EFAULT;
  451. if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
  452. return -EFAULT;
  453. switch (a.op) {
  454. case BLKPG_ADD_PARTITION:
  455. /* Only master mtd device must be used to add partitions */
  456. if (mtd_is_partition(mtd))
  457. return -EINVAL;
  458. return mtd_add_partition(mtd, p.devname, p.start, p.length);
  459. case BLKPG_DEL_PARTITION:
  460. if (p.pno < 0)
  461. return -EINVAL;
  462. return mtd_del_partition(mtd, p.pno);
  463. default:
  464. return -EINVAL;
  465. }
  466. }
  467. static int mtdchar_write_ioctl(struct mtd_info *mtd,
  468. struct mtd_write_req __user *argp)
  469. {
  470. struct mtd_write_req req;
  471. struct mtd_oob_ops ops;
  472. void __user *usr_data, *usr_oob;
  473. int ret;
  474. if (copy_from_user(&req, argp, sizeof(req)) ||
  475. !access_ok(VERIFY_READ, req.usr_data, req.len) ||
  476. !access_ok(VERIFY_READ, req.usr_oob, req.ooblen))
  477. return -EFAULT;
  478. if (!mtd->write_oob)
  479. return -EOPNOTSUPP;
  480. ops.mode = req.mode;
  481. ops.len = (size_t)req.len;
  482. ops.ooblen = (size_t)req.ooblen;
  483. ops.ooboffs = 0;
  484. usr_data = (void __user *)(uintptr_t)req.usr_data;
  485. usr_oob = (void __user *)(uintptr_t)req.usr_oob;
  486. if (req.usr_data) {
  487. ops.datbuf = memdup_user(usr_data, ops.len);
  488. if (IS_ERR(ops.datbuf))
  489. return PTR_ERR(ops.datbuf);
  490. } else {
  491. ops.datbuf = NULL;
  492. }
  493. if (req.usr_oob) {
  494. ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
  495. if (IS_ERR(ops.oobbuf)) {
  496. kfree(ops.datbuf);
  497. return PTR_ERR(ops.oobbuf);
  498. }
  499. } else {
  500. ops.oobbuf = NULL;
  501. }
  502. ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
  503. kfree(ops.datbuf);
  504. kfree(ops.oobbuf);
  505. return ret;
  506. }
  507. static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
  508. {
  509. struct mtd_file_info *mfi = file->private_data;
  510. struct mtd_info *mtd = mfi->mtd;
  511. void __user *argp = (void __user *)arg;
  512. int ret = 0;
  513. u_long size;
  514. struct mtd_info_user info;
  515. pr_debug("MTD_ioctl\n");
  516. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  517. if (cmd & IOC_IN) {
  518. if (!access_ok(VERIFY_READ, argp, size))
  519. return -EFAULT;
  520. }
  521. if (cmd & IOC_OUT) {
  522. if (!access_ok(VERIFY_WRITE, argp, size))
  523. return -EFAULT;
  524. }
  525. switch (cmd) {
  526. case MEMGETREGIONCOUNT:
  527. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  528. return -EFAULT;
  529. break;
  530. case MEMGETREGIONINFO:
  531. {
  532. uint32_t ur_idx;
  533. struct mtd_erase_region_info *kr;
  534. struct region_info_user __user *ur = argp;
  535. if (get_user(ur_idx, &(ur->regionindex)))
  536. return -EFAULT;
  537. if (ur_idx >= mtd->numeraseregions)
  538. return -EINVAL;
  539. kr = &(mtd->eraseregions[ur_idx]);
  540. if (put_user(kr->offset, &(ur->offset))
  541. || put_user(kr->erasesize, &(ur->erasesize))
  542. || put_user(kr->numblocks, &(ur->numblocks)))
  543. return -EFAULT;
  544. break;
  545. }
  546. case MEMGETINFO:
  547. memset(&info, 0, sizeof(info));
  548. info.type = mtd->type;
  549. info.flags = mtd->flags;
  550. info.size = mtd->size;
  551. info.erasesize = mtd->erasesize;
  552. info.writesize = mtd->writesize;
  553. info.oobsize = mtd->oobsize;
  554. /* The below field is obsolete */
  555. info.padding = 0;
  556. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  557. return -EFAULT;
  558. break;
  559. case MEMERASE:
  560. case MEMERASE64:
  561. {
  562. struct erase_info *erase;
  563. if(!(file->f_mode & FMODE_WRITE))
  564. return -EPERM;
  565. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  566. if (!erase)
  567. ret = -ENOMEM;
  568. else {
  569. wait_queue_head_t waitq;
  570. DECLARE_WAITQUEUE(wait, current);
  571. init_waitqueue_head(&waitq);
  572. if (cmd == MEMERASE64) {
  573. struct erase_info_user64 einfo64;
  574. if (copy_from_user(&einfo64, argp,
  575. sizeof(struct erase_info_user64))) {
  576. kfree(erase);
  577. return -EFAULT;
  578. }
  579. erase->addr = einfo64.start;
  580. erase->len = einfo64.length;
  581. } else {
  582. struct erase_info_user einfo32;
  583. if (copy_from_user(&einfo32, argp,
  584. sizeof(struct erase_info_user))) {
  585. kfree(erase);
  586. return -EFAULT;
  587. }
  588. erase->addr = einfo32.start;
  589. erase->len = einfo32.length;
  590. }
  591. erase->mtd = mtd;
  592. erase->callback = mtdchar_erase_callback;
  593. erase->priv = (unsigned long)&waitq;
  594. /*
  595. FIXME: Allow INTERRUPTIBLE. Which means
  596. not having the wait_queue head on the stack.
  597. If the wq_head is on the stack, and we
  598. leave because we got interrupted, then the
  599. wq_head is no longer there when the
  600. callback routine tries to wake us up.
  601. */
  602. ret = mtd_erase(mtd, erase);
  603. if (!ret) {
  604. set_current_state(TASK_UNINTERRUPTIBLE);
  605. add_wait_queue(&waitq, &wait);
  606. if (erase->state != MTD_ERASE_DONE &&
  607. erase->state != MTD_ERASE_FAILED)
  608. schedule();
  609. remove_wait_queue(&waitq, &wait);
  610. set_current_state(TASK_RUNNING);
  611. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  612. }
  613. kfree(erase);
  614. }
  615. break;
  616. }
  617. case MEMWRITEOOB:
  618. {
  619. struct mtd_oob_buf buf;
  620. struct mtd_oob_buf __user *buf_user = argp;
  621. /* NOTE: writes return length to buf_user->length */
  622. if (copy_from_user(&buf, argp, sizeof(buf)))
  623. ret = -EFAULT;
  624. else
  625. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  626. buf.ptr, &buf_user->length);
  627. break;
  628. }
  629. case MEMREADOOB:
  630. {
  631. struct mtd_oob_buf buf;
  632. struct mtd_oob_buf __user *buf_user = argp;
  633. /* NOTE: writes return length to buf_user->start */
  634. if (copy_from_user(&buf, argp, sizeof(buf)))
  635. ret = -EFAULT;
  636. else
  637. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  638. buf.ptr, &buf_user->start);
  639. break;
  640. }
  641. case MEMWRITEOOB64:
  642. {
  643. struct mtd_oob_buf64 buf;
  644. struct mtd_oob_buf64 __user *buf_user = argp;
  645. if (copy_from_user(&buf, argp, sizeof(buf)))
  646. ret = -EFAULT;
  647. else
  648. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  649. (void __user *)(uintptr_t)buf.usr_ptr,
  650. &buf_user->length);
  651. break;
  652. }
  653. case MEMREADOOB64:
  654. {
  655. struct mtd_oob_buf64 buf;
  656. struct mtd_oob_buf64 __user *buf_user = argp;
  657. if (copy_from_user(&buf, argp, sizeof(buf)))
  658. ret = -EFAULT;
  659. else
  660. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  661. (void __user *)(uintptr_t)buf.usr_ptr,
  662. &buf_user->length);
  663. break;
  664. }
  665. case MEMWRITE:
  666. {
  667. ret = mtdchar_write_ioctl(mtd,
  668. (struct mtd_write_req __user *)arg);
  669. break;
  670. }
  671. case MEMLOCK:
  672. {
  673. struct erase_info_user einfo;
  674. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  675. return -EFAULT;
  676. if (!mtd->lock)
  677. ret = -EOPNOTSUPP;
  678. else
  679. ret = mtd_lock(mtd, einfo.start, einfo.length);
  680. break;
  681. }
  682. case MEMUNLOCK:
  683. {
  684. struct erase_info_user einfo;
  685. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  686. return -EFAULT;
  687. if (!mtd->unlock)
  688. ret = -EOPNOTSUPP;
  689. else
  690. ret = mtd_unlock(mtd, einfo.start, einfo.length);
  691. break;
  692. }
  693. case MEMISLOCKED:
  694. {
  695. struct erase_info_user einfo;
  696. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  697. return -EFAULT;
  698. if (!mtd->is_locked)
  699. ret = -EOPNOTSUPP;
  700. else
  701. ret = mtd_is_locked(mtd, einfo.start, einfo.length);
  702. break;
  703. }
  704. /* Legacy interface */
  705. case MEMGETOOBSEL:
  706. {
  707. struct nand_oobinfo oi;
  708. if (!mtd->ecclayout)
  709. return -EOPNOTSUPP;
  710. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  711. return -EINVAL;
  712. oi.useecc = MTD_NANDECC_AUTOPLACE;
  713. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  714. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  715. sizeof(oi.oobfree));
  716. oi.eccbytes = mtd->ecclayout->eccbytes;
  717. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  718. return -EFAULT;
  719. break;
  720. }
  721. case MEMGETBADBLOCK:
  722. {
  723. loff_t offs;
  724. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  725. return -EFAULT;
  726. if (!mtd->block_isbad)
  727. ret = -EOPNOTSUPP;
  728. else
  729. return mtd_block_isbad(mtd, offs);
  730. break;
  731. }
  732. case MEMSETBADBLOCK:
  733. {
  734. loff_t offs;
  735. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  736. return -EFAULT;
  737. if (!mtd->block_markbad)
  738. ret = -EOPNOTSUPP;
  739. else
  740. return mtd_block_markbad(mtd, offs);
  741. break;
  742. }
  743. #ifdef CONFIG_HAVE_MTD_OTP
  744. case OTPSELECT:
  745. {
  746. int mode;
  747. if (copy_from_user(&mode, argp, sizeof(int)))
  748. return -EFAULT;
  749. mfi->mode = MTD_FILE_MODE_NORMAL;
  750. ret = otp_select_filemode(mfi, mode);
  751. file->f_pos = 0;
  752. break;
  753. }
  754. case OTPGETREGIONCOUNT:
  755. case OTPGETREGIONINFO:
  756. {
  757. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  758. if (!buf)
  759. return -ENOMEM;
  760. switch (mfi->mode) {
  761. case MTD_FILE_MODE_OTP_FACTORY:
  762. ret = mtd_get_fact_prot_info(mtd, buf, 4096);
  763. break;
  764. case MTD_FILE_MODE_OTP_USER:
  765. ret = mtd_get_user_prot_info(mtd, buf, 4096);
  766. break;
  767. default:
  768. ret = -EINVAL;
  769. break;
  770. }
  771. if (ret >= 0) {
  772. if (cmd == OTPGETREGIONCOUNT) {
  773. int nbr = ret / sizeof(struct otp_info);
  774. ret = copy_to_user(argp, &nbr, sizeof(int));
  775. } else
  776. ret = copy_to_user(argp, buf, ret);
  777. if (ret)
  778. ret = -EFAULT;
  779. }
  780. kfree(buf);
  781. break;
  782. }
  783. case OTPLOCK:
  784. {
  785. struct otp_info oinfo;
  786. if (mfi->mode != MTD_FILE_MODE_OTP_USER)
  787. return -EINVAL;
  788. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  789. return -EFAULT;
  790. if (!mtd->lock_user_prot_reg)
  791. return -EOPNOTSUPP;
  792. ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  793. break;
  794. }
  795. #endif
  796. /* This ioctl is being deprecated - it truncates the ECC layout */
  797. case ECCGETLAYOUT:
  798. {
  799. struct nand_ecclayout_user *usrlay;
  800. if (!mtd->ecclayout)
  801. return -EOPNOTSUPP;
  802. usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
  803. if (!usrlay)
  804. return -ENOMEM;
  805. shrink_ecclayout(mtd->ecclayout, usrlay);
  806. if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
  807. ret = -EFAULT;
  808. kfree(usrlay);
  809. break;
  810. }
  811. case ECCGETSTATS:
  812. {
  813. if (copy_to_user(argp, &mtd->ecc_stats,
  814. sizeof(struct mtd_ecc_stats)))
  815. return -EFAULT;
  816. break;
  817. }
  818. case MTDFILEMODE:
  819. {
  820. mfi->mode = 0;
  821. switch(arg) {
  822. case MTD_FILE_MODE_OTP_FACTORY:
  823. case MTD_FILE_MODE_OTP_USER:
  824. ret = otp_select_filemode(mfi, arg);
  825. break;
  826. case MTD_FILE_MODE_RAW:
  827. if (!mtd_has_oob(mtd))
  828. return -EOPNOTSUPP;
  829. mfi->mode = arg;
  830. case MTD_FILE_MODE_NORMAL:
  831. break;
  832. default:
  833. ret = -EINVAL;
  834. }
  835. file->f_pos = 0;
  836. break;
  837. }
  838. case BLKPG:
  839. {
  840. ret = mtdchar_blkpg_ioctl(mtd,
  841. (struct blkpg_ioctl_arg __user *)arg);
  842. break;
  843. }
  844. case BLKRRPART:
  845. {
  846. /* No reread partition feature. Just return ok */
  847. ret = 0;
  848. break;
  849. }
  850. default:
  851. ret = -ENOTTY;
  852. }
  853. return ret;
  854. } /* memory_ioctl */
  855. static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  856. {
  857. int ret;
  858. mutex_lock(&mtd_mutex);
  859. ret = mtdchar_ioctl(file, cmd, arg);
  860. mutex_unlock(&mtd_mutex);
  861. return ret;
  862. }
  863. #ifdef CONFIG_COMPAT
  864. struct mtd_oob_buf32 {
  865. u_int32_t start;
  866. u_int32_t length;
  867. compat_caddr_t ptr; /* unsigned char* */
  868. };
  869. #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
  870. #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
  871. static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
  872. unsigned long arg)
  873. {
  874. struct mtd_file_info *mfi = file->private_data;
  875. struct mtd_info *mtd = mfi->mtd;
  876. void __user *argp = compat_ptr(arg);
  877. int ret = 0;
  878. mutex_lock(&mtd_mutex);
  879. switch (cmd) {
  880. case MEMWRITEOOB32:
  881. {
  882. struct mtd_oob_buf32 buf;
  883. struct mtd_oob_buf32 __user *buf_user = argp;
  884. if (copy_from_user(&buf, argp, sizeof(buf)))
  885. ret = -EFAULT;
  886. else
  887. ret = mtdchar_writeoob(file, mtd, buf.start,
  888. buf.length, compat_ptr(buf.ptr),
  889. &buf_user->length);
  890. break;
  891. }
  892. case MEMREADOOB32:
  893. {
  894. struct mtd_oob_buf32 buf;
  895. struct mtd_oob_buf32 __user *buf_user = argp;
  896. /* NOTE: writes return length to buf->start */
  897. if (copy_from_user(&buf, argp, sizeof(buf)))
  898. ret = -EFAULT;
  899. else
  900. ret = mtdchar_readoob(file, mtd, buf.start,
  901. buf.length, compat_ptr(buf.ptr),
  902. &buf_user->start);
  903. break;
  904. }
  905. default:
  906. ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
  907. }
  908. mutex_unlock(&mtd_mutex);
  909. return ret;
  910. }
  911. #endif /* CONFIG_COMPAT */
  912. /*
  913. * try to determine where a shared mapping can be made
  914. * - only supported for NOMMU at the moment (MMU can't doesn't copy private
  915. * mappings)
  916. */
  917. #ifndef CONFIG_MMU
  918. static unsigned long mtdchar_get_unmapped_area(struct file *file,
  919. unsigned long addr,
  920. unsigned long len,
  921. unsigned long pgoff,
  922. unsigned long flags)
  923. {
  924. struct mtd_file_info *mfi = file->private_data;
  925. struct mtd_info *mtd = mfi->mtd;
  926. unsigned long offset;
  927. int ret;
  928. if (addr != 0)
  929. return (unsigned long) -EINVAL;
  930. if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
  931. return (unsigned long) -EINVAL;
  932. offset = pgoff << PAGE_SHIFT;
  933. if (offset > mtd->size - len)
  934. return (unsigned long) -EINVAL;
  935. ret = mtd_get_unmapped_area(mtd, len, offset, flags);
  936. return ret == -EOPNOTSUPP ? -ENOSYS : ret;
  937. }
  938. #endif
  939. /*
  940. * set up a mapping for shared memory segments
  941. */
  942. static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
  943. {
  944. #ifdef CONFIG_MMU
  945. struct mtd_file_info *mfi = file->private_data;
  946. struct mtd_info *mtd = mfi->mtd;
  947. struct map_info *map = mtd->priv;
  948. unsigned long start;
  949. unsigned long off;
  950. u32 len;
  951. if (mtd->type == MTD_RAM || mtd->type == MTD_ROM) {
  952. off = vma->vm_pgoff << PAGE_SHIFT;
  953. start = map->phys;
  954. len = PAGE_ALIGN((start & ~PAGE_MASK) + map->size);
  955. start &= PAGE_MASK;
  956. if ((vma->vm_end - vma->vm_start + off) > len)
  957. return -EINVAL;
  958. off += start;
  959. vma->vm_pgoff = off >> PAGE_SHIFT;
  960. vma->vm_flags |= VM_IO | VM_RESERVED;
  961. #ifdef pgprot_noncached
  962. if (file->f_flags & O_DSYNC || off >= __pa(high_memory))
  963. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  964. #endif
  965. if (io_remap_pfn_range(vma, vma->vm_start, off >> PAGE_SHIFT,
  966. vma->vm_end - vma->vm_start,
  967. vma->vm_page_prot))
  968. return -EAGAIN;
  969. return 0;
  970. }
  971. return -ENOSYS;
  972. #else
  973. return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
  974. #endif
  975. }
  976. static const struct file_operations mtd_fops = {
  977. .owner = THIS_MODULE,
  978. .llseek = mtdchar_lseek,
  979. .read = mtdchar_read,
  980. .write = mtdchar_write,
  981. .unlocked_ioctl = mtdchar_unlocked_ioctl,
  982. #ifdef CONFIG_COMPAT
  983. .compat_ioctl = mtdchar_compat_ioctl,
  984. #endif
  985. .open = mtdchar_open,
  986. .release = mtdchar_close,
  987. .mmap = mtdchar_mmap,
  988. #ifndef CONFIG_MMU
  989. .get_unmapped_area = mtdchar_get_unmapped_area,
  990. #endif
  991. };
  992. static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
  993. int flags, const char *dev_name, void *data)
  994. {
  995. return mount_pseudo(fs_type, "mtd_inode:", NULL, NULL, MTD_INODE_FS_MAGIC);
  996. }
  997. static struct file_system_type mtd_inodefs_type = {
  998. .name = "mtd_inodefs",
  999. .mount = mtd_inodefs_mount,
  1000. .kill_sb = kill_anon_super,
  1001. };
  1002. static void mtdchar_notify_add(struct mtd_info *mtd)
  1003. {
  1004. }
  1005. static void mtdchar_notify_remove(struct mtd_info *mtd)
  1006. {
  1007. struct inode *mtd_ino = ilookup(mtd_inode_mnt->mnt_sb, mtd->index);
  1008. if (mtd_ino) {
  1009. /* Destroy the inode if it exists */
  1010. clear_nlink(mtd_ino);
  1011. iput(mtd_ino);
  1012. }
  1013. }
  1014. static struct mtd_notifier mtdchar_notifier = {
  1015. .add = mtdchar_notify_add,
  1016. .remove = mtdchar_notify_remove,
  1017. };
  1018. static int __init init_mtdchar(void)
  1019. {
  1020. int ret;
  1021. ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
  1022. "mtd", &mtd_fops);
  1023. if (ret < 0) {
  1024. pr_notice("Can't allocate major number %d for "
  1025. "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
  1026. return ret;
  1027. }
  1028. ret = register_filesystem(&mtd_inodefs_type);
  1029. if (ret) {
  1030. pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
  1031. goto err_unregister_chdev;
  1032. }
  1033. mtd_inode_mnt = kern_mount(&mtd_inodefs_type);
  1034. if (IS_ERR(mtd_inode_mnt)) {
  1035. ret = PTR_ERR(mtd_inode_mnt);
  1036. pr_notice("Error mounting mtd_inodefs filesystem: %d\n", ret);
  1037. goto err_unregister_filesystem;
  1038. }
  1039. register_mtd_user(&mtdchar_notifier);
  1040. return ret;
  1041. err_unregister_filesystem:
  1042. unregister_filesystem(&mtd_inodefs_type);
  1043. err_unregister_chdev:
  1044. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1045. return ret;
  1046. }
  1047. static void __exit cleanup_mtdchar(void)
  1048. {
  1049. unregister_mtd_user(&mtdchar_notifier);
  1050. kern_unmount(mtd_inode_mnt);
  1051. unregister_filesystem(&mtd_inodefs_type);
  1052. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1053. }
  1054. module_init(init_mtdchar);
  1055. module_exit(cleanup_mtdchar);
  1056. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
  1057. MODULE_LICENSE("GPL");
  1058. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1059. MODULE_DESCRIPTION("Direct character-device access to MTD devices");
  1060. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);