mtdchar.c 26 KB

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