mtdchar.c 27 KB

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