mtdchar.c 27 KB

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