mtdcore.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238
  1. /*
  2. * Core registration and callback routines for MTD
  3. * drivers and users.
  4. *
  5. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
  6. * Copyright © 2006 Red Hat UK Limited
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/ptrace.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/string.h>
  28. #include <linux/timer.h>
  29. #include <linux/major.h>
  30. #include <linux/fs.h>
  31. #include <linux/err.h>
  32. #include <linux/ioctl.h>
  33. #include <linux/init.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/idr.h>
  36. #include <linux/backing-dev.h>
  37. #include <linux/gfp.h>
  38. #include <linux/slab.h>
  39. #include <linux/mtd/mtd.h>
  40. #include <linux/mtd/partitions.h>
  41. #include "mtdcore.h"
  42. /*
  43. * backing device capabilities for non-mappable devices (such as NAND flash)
  44. * - permits private mappings, copies are taken of the data
  45. */
  46. static struct backing_dev_info mtd_bdi_unmappable = {
  47. .capabilities = BDI_CAP_MAP_COPY,
  48. };
  49. /*
  50. * backing device capabilities for R/O mappable devices (such as ROM)
  51. * - permits private mappings, copies are taken of the data
  52. * - permits non-writable shared mappings
  53. */
  54. static struct backing_dev_info mtd_bdi_ro_mappable = {
  55. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  56. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP),
  57. };
  58. /*
  59. * backing device capabilities for writable mappable devices (such as RAM)
  60. * - permits private mappings, copies are taken of the data
  61. * - permits non-writable shared mappings
  62. */
  63. static struct backing_dev_info mtd_bdi_rw_mappable = {
  64. .capabilities = (BDI_CAP_MAP_COPY | BDI_CAP_MAP_DIRECT |
  65. BDI_CAP_EXEC_MAP | BDI_CAP_READ_MAP |
  66. BDI_CAP_WRITE_MAP),
  67. };
  68. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  69. static int mtd_cls_resume(struct device *dev);
  70. static struct class mtd_class = {
  71. .name = "mtd",
  72. .owner = THIS_MODULE,
  73. .suspend = mtd_cls_suspend,
  74. .resume = mtd_cls_resume,
  75. };
  76. static DEFINE_IDR(mtd_idr);
  77. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  78. should not use them for _anything_ else */
  79. DEFINE_MUTEX(mtd_table_mutex);
  80. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  81. struct mtd_info *__mtd_next_device(int i)
  82. {
  83. return idr_get_next(&mtd_idr, &i);
  84. }
  85. EXPORT_SYMBOL_GPL(__mtd_next_device);
  86. static LIST_HEAD(mtd_notifiers);
  87. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  88. /* REVISIT once MTD uses the driver model better, whoever allocates
  89. * the mtd_info will probably want to use the release() hook...
  90. */
  91. static void mtd_release(struct device *dev)
  92. {
  93. struct mtd_info __maybe_unused *mtd = dev_get_drvdata(dev);
  94. dev_t index = MTD_DEVT(mtd->index);
  95. /* remove /dev/mtdXro node if needed */
  96. if (index)
  97. device_destroy(&mtd_class, index + 1);
  98. }
  99. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  100. {
  101. struct mtd_info *mtd = dev_get_drvdata(dev);
  102. return mtd ? mtd_suspend(mtd) : 0;
  103. }
  104. static int mtd_cls_resume(struct device *dev)
  105. {
  106. struct mtd_info *mtd = dev_get_drvdata(dev);
  107. if (mtd)
  108. mtd_resume(mtd);
  109. return 0;
  110. }
  111. static ssize_t mtd_type_show(struct device *dev,
  112. struct device_attribute *attr, char *buf)
  113. {
  114. struct mtd_info *mtd = dev_get_drvdata(dev);
  115. char *type;
  116. switch (mtd->type) {
  117. case MTD_ABSENT:
  118. type = "absent";
  119. break;
  120. case MTD_RAM:
  121. type = "ram";
  122. break;
  123. case MTD_ROM:
  124. type = "rom";
  125. break;
  126. case MTD_NORFLASH:
  127. type = "nor";
  128. break;
  129. case MTD_NANDFLASH:
  130. type = "nand";
  131. break;
  132. case MTD_DATAFLASH:
  133. type = "dataflash";
  134. break;
  135. case MTD_UBIVOLUME:
  136. type = "ubi";
  137. break;
  138. case MTD_MLCNANDFLASH:
  139. type = "mlc-nand";
  140. break;
  141. default:
  142. type = "unknown";
  143. }
  144. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  145. }
  146. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  147. static ssize_t mtd_flags_show(struct device *dev,
  148. struct device_attribute *attr, char *buf)
  149. {
  150. struct mtd_info *mtd = dev_get_drvdata(dev);
  151. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  152. }
  153. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  154. static ssize_t mtd_size_show(struct device *dev,
  155. struct device_attribute *attr, char *buf)
  156. {
  157. struct mtd_info *mtd = dev_get_drvdata(dev);
  158. return snprintf(buf, PAGE_SIZE, "%llu\n",
  159. (unsigned long long)mtd->size);
  160. }
  161. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  162. static ssize_t mtd_erasesize_show(struct device *dev,
  163. struct device_attribute *attr, char *buf)
  164. {
  165. struct mtd_info *mtd = dev_get_drvdata(dev);
  166. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  167. }
  168. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  169. static ssize_t mtd_writesize_show(struct device *dev,
  170. struct device_attribute *attr, char *buf)
  171. {
  172. struct mtd_info *mtd = dev_get_drvdata(dev);
  173. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  174. }
  175. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  176. static ssize_t mtd_subpagesize_show(struct device *dev,
  177. struct device_attribute *attr, char *buf)
  178. {
  179. struct mtd_info *mtd = dev_get_drvdata(dev);
  180. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  181. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  182. }
  183. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  184. static ssize_t mtd_oobsize_show(struct device *dev,
  185. struct device_attribute *attr, char *buf)
  186. {
  187. struct mtd_info *mtd = dev_get_drvdata(dev);
  188. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  189. }
  190. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  191. static ssize_t mtd_numeraseregions_show(struct device *dev,
  192. struct device_attribute *attr, char *buf)
  193. {
  194. struct mtd_info *mtd = dev_get_drvdata(dev);
  195. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  196. }
  197. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  198. NULL);
  199. static ssize_t mtd_name_show(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct mtd_info *mtd = dev_get_drvdata(dev);
  203. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  204. }
  205. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  206. static ssize_t mtd_ecc_strength_show(struct device *dev,
  207. struct device_attribute *attr, char *buf)
  208. {
  209. struct mtd_info *mtd = dev_get_drvdata(dev);
  210. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_strength);
  211. }
  212. static DEVICE_ATTR(ecc_strength, S_IRUGO, mtd_ecc_strength_show, NULL);
  213. static ssize_t mtd_bitflip_threshold_show(struct device *dev,
  214. struct device_attribute *attr,
  215. char *buf)
  216. {
  217. struct mtd_info *mtd = dev_get_drvdata(dev);
  218. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->bitflip_threshold);
  219. }
  220. static ssize_t mtd_bitflip_threshold_store(struct device *dev,
  221. struct device_attribute *attr,
  222. const char *buf, size_t count)
  223. {
  224. struct mtd_info *mtd = dev_get_drvdata(dev);
  225. unsigned int bitflip_threshold;
  226. int retval;
  227. retval = kstrtouint(buf, 0, &bitflip_threshold);
  228. if (retval)
  229. return retval;
  230. mtd->bitflip_threshold = bitflip_threshold;
  231. return count;
  232. }
  233. static DEVICE_ATTR(bitflip_threshold, S_IRUGO | S_IWUSR,
  234. mtd_bitflip_threshold_show,
  235. mtd_bitflip_threshold_store);
  236. static ssize_t mtd_ecc_step_size_show(struct device *dev,
  237. struct device_attribute *attr, char *buf)
  238. {
  239. struct mtd_info *mtd = dev_get_drvdata(dev);
  240. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->ecc_step_size);
  241. }
  242. static DEVICE_ATTR(ecc_step_size, S_IRUGO, mtd_ecc_step_size_show, NULL);
  243. static struct attribute *mtd_attrs[] = {
  244. &dev_attr_type.attr,
  245. &dev_attr_flags.attr,
  246. &dev_attr_size.attr,
  247. &dev_attr_erasesize.attr,
  248. &dev_attr_writesize.attr,
  249. &dev_attr_subpagesize.attr,
  250. &dev_attr_oobsize.attr,
  251. &dev_attr_numeraseregions.attr,
  252. &dev_attr_name.attr,
  253. &dev_attr_ecc_strength.attr,
  254. &dev_attr_ecc_step_size.attr,
  255. &dev_attr_bitflip_threshold.attr,
  256. NULL,
  257. };
  258. ATTRIBUTE_GROUPS(mtd);
  259. static struct device_type mtd_devtype = {
  260. .name = "mtd",
  261. .groups = mtd_groups,
  262. .release = mtd_release,
  263. };
  264. /**
  265. * add_mtd_device - register an MTD device
  266. * @mtd: pointer to new MTD device info structure
  267. *
  268. * Add a device to the list of MTD devices present in the system, and
  269. * notify each currently active MTD 'user' of its arrival. Returns
  270. * zero on success or 1 on failure, which currently will only happen
  271. * if there is insufficient memory or a sysfs error.
  272. */
  273. int add_mtd_device(struct mtd_info *mtd)
  274. {
  275. struct mtd_notifier *not;
  276. int i, error;
  277. if (!mtd->backing_dev_info) {
  278. switch (mtd->type) {
  279. case MTD_RAM:
  280. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  281. break;
  282. case MTD_ROM:
  283. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  284. break;
  285. default:
  286. mtd->backing_dev_info = &mtd_bdi_unmappable;
  287. break;
  288. }
  289. }
  290. BUG_ON(mtd->writesize == 0);
  291. mutex_lock(&mtd_table_mutex);
  292. i = idr_alloc(&mtd_idr, mtd, 0, 0, GFP_KERNEL);
  293. if (i < 0)
  294. goto fail_locked;
  295. mtd->index = i;
  296. mtd->usecount = 0;
  297. /* default value if not set by driver */
  298. if (mtd->bitflip_threshold == 0)
  299. mtd->bitflip_threshold = mtd->ecc_strength;
  300. if (is_power_of_2(mtd->erasesize))
  301. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  302. else
  303. mtd->erasesize_shift = 0;
  304. if (is_power_of_2(mtd->writesize))
  305. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  306. else
  307. mtd->writesize_shift = 0;
  308. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  309. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  310. /* Some chips always power up locked. Unlock them now */
  311. if ((mtd->flags & MTD_WRITEABLE) && (mtd->flags & MTD_POWERUP_LOCK)) {
  312. error = mtd_unlock(mtd, 0, mtd->size);
  313. if (error && error != -EOPNOTSUPP)
  314. printk(KERN_WARNING
  315. "%s: unlock failed, writes may not work\n",
  316. mtd->name);
  317. }
  318. /* Caller should have set dev.parent to match the
  319. * physical device.
  320. */
  321. mtd->dev.type = &mtd_devtype;
  322. mtd->dev.class = &mtd_class;
  323. mtd->dev.devt = MTD_DEVT(i);
  324. dev_set_name(&mtd->dev, "mtd%d", i);
  325. dev_set_drvdata(&mtd->dev, mtd);
  326. if (device_register(&mtd->dev) != 0)
  327. goto fail_added;
  328. if (MTD_DEVT(i))
  329. device_create(&mtd_class, mtd->dev.parent,
  330. MTD_DEVT(i) + 1,
  331. NULL, "mtd%dro", i);
  332. pr_debug("mtd: Giving out device %d to %s\n", i, mtd->name);
  333. /* No need to get a refcount on the module containing
  334. the notifier, since we hold the mtd_table_mutex */
  335. list_for_each_entry(not, &mtd_notifiers, list)
  336. not->add(mtd);
  337. mutex_unlock(&mtd_table_mutex);
  338. /* We _know_ we aren't being removed, because
  339. our caller is still holding us here. So none
  340. of this try_ nonsense, and no bitching about it
  341. either. :) */
  342. __module_get(THIS_MODULE);
  343. return 0;
  344. fail_added:
  345. idr_remove(&mtd_idr, i);
  346. fail_locked:
  347. mutex_unlock(&mtd_table_mutex);
  348. return 1;
  349. }
  350. /**
  351. * del_mtd_device - unregister an MTD device
  352. * @mtd: pointer to MTD device info structure
  353. *
  354. * Remove a device from the list of MTD devices present in the system,
  355. * and notify each currently active MTD 'user' of its departure.
  356. * Returns zero on success or 1 on failure, which currently will happen
  357. * if the requested device does not appear to be present in the list.
  358. */
  359. int del_mtd_device(struct mtd_info *mtd)
  360. {
  361. int ret;
  362. struct mtd_notifier *not;
  363. mutex_lock(&mtd_table_mutex);
  364. if (idr_find(&mtd_idr, mtd->index) != mtd) {
  365. ret = -ENODEV;
  366. goto out_error;
  367. }
  368. /* No need to get a refcount on the module containing
  369. the notifier, since we hold the mtd_table_mutex */
  370. list_for_each_entry(not, &mtd_notifiers, list)
  371. not->remove(mtd);
  372. if (mtd->usecount) {
  373. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  374. mtd->index, mtd->name, mtd->usecount);
  375. ret = -EBUSY;
  376. } else {
  377. device_unregister(&mtd->dev);
  378. idr_remove(&mtd_idr, mtd->index);
  379. module_put(THIS_MODULE);
  380. ret = 0;
  381. }
  382. out_error:
  383. mutex_unlock(&mtd_table_mutex);
  384. return ret;
  385. }
  386. /**
  387. * mtd_device_parse_register - parse partitions and register an MTD device.
  388. *
  389. * @mtd: the MTD device to register
  390. * @types: the list of MTD partition probes to try, see
  391. * 'parse_mtd_partitions()' for more information
  392. * @parser_data: MTD partition parser-specific data
  393. * @parts: fallback partition information to register, if parsing fails;
  394. * only valid if %nr_parts > %0
  395. * @nr_parts: the number of partitions in parts, if zero then the full
  396. * MTD device is registered if no partition info is found
  397. *
  398. * This function aggregates MTD partitions parsing (done by
  399. * 'parse_mtd_partitions()') and MTD device and partitions registering. It
  400. * basically follows the most common pattern found in many MTD drivers:
  401. *
  402. * * It first tries to probe partitions on MTD device @mtd using parsers
  403. * specified in @types (if @types is %NULL, then the default list of parsers
  404. * is used, see 'parse_mtd_partitions()' for more information). If none are
  405. * found this functions tries to fallback to information specified in
  406. * @parts/@nr_parts.
  407. * * If any partitioning info was found, this function registers the found
  408. * partitions.
  409. * * If no partitions were found this function just registers the MTD device
  410. * @mtd and exits.
  411. *
  412. * Returns zero in case of success and a negative error code in case of failure.
  413. */
  414. int mtd_device_parse_register(struct mtd_info *mtd, const char * const *types,
  415. struct mtd_part_parser_data *parser_data,
  416. const struct mtd_partition *parts,
  417. int nr_parts)
  418. {
  419. int err;
  420. struct mtd_partition *real_parts;
  421. err = parse_mtd_partitions(mtd, types, &real_parts, parser_data);
  422. if (err <= 0 && nr_parts && parts) {
  423. real_parts = kmemdup(parts, sizeof(*parts) * nr_parts,
  424. GFP_KERNEL);
  425. if (!real_parts)
  426. err = -ENOMEM;
  427. else
  428. err = nr_parts;
  429. }
  430. if (err > 0) {
  431. err = add_mtd_partitions(mtd, real_parts, err);
  432. kfree(real_parts);
  433. } else if (err == 0) {
  434. err = add_mtd_device(mtd);
  435. if (err == 1)
  436. err = -ENODEV;
  437. }
  438. return err;
  439. }
  440. EXPORT_SYMBOL_GPL(mtd_device_parse_register);
  441. /**
  442. * mtd_device_unregister - unregister an existing MTD device.
  443. *
  444. * @master: the MTD device to unregister. This will unregister both the master
  445. * and any partitions if registered.
  446. */
  447. int mtd_device_unregister(struct mtd_info *master)
  448. {
  449. int err;
  450. err = del_mtd_partitions(master);
  451. if (err)
  452. return err;
  453. if (!device_is_registered(&master->dev))
  454. return 0;
  455. return del_mtd_device(master);
  456. }
  457. EXPORT_SYMBOL_GPL(mtd_device_unregister);
  458. /**
  459. * register_mtd_user - register a 'user' of MTD devices.
  460. * @new: pointer to notifier info structure
  461. *
  462. * Registers a pair of callbacks function to be called upon addition
  463. * or removal of MTD devices. Causes the 'add' callback to be immediately
  464. * invoked for each MTD device currently present in the system.
  465. */
  466. void register_mtd_user (struct mtd_notifier *new)
  467. {
  468. struct mtd_info *mtd;
  469. mutex_lock(&mtd_table_mutex);
  470. list_add(&new->list, &mtd_notifiers);
  471. __module_get(THIS_MODULE);
  472. mtd_for_each_device(mtd)
  473. new->add(mtd);
  474. mutex_unlock(&mtd_table_mutex);
  475. }
  476. EXPORT_SYMBOL_GPL(register_mtd_user);
  477. /**
  478. * unregister_mtd_user - unregister a 'user' of MTD devices.
  479. * @old: pointer to notifier info structure
  480. *
  481. * Removes a callback function pair from the list of 'users' to be
  482. * notified upon addition or removal of MTD devices. Causes the
  483. * 'remove' callback to be immediately invoked for each MTD device
  484. * currently present in the system.
  485. */
  486. int unregister_mtd_user (struct mtd_notifier *old)
  487. {
  488. struct mtd_info *mtd;
  489. mutex_lock(&mtd_table_mutex);
  490. module_put(THIS_MODULE);
  491. mtd_for_each_device(mtd)
  492. old->remove(mtd);
  493. list_del(&old->list);
  494. mutex_unlock(&mtd_table_mutex);
  495. return 0;
  496. }
  497. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  498. /**
  499. * get_mtd_device - obtain a validated handle for an MTD device
  500. * @mtd: last known address of the required MTD device
  501. * @num: internal device number of the required MTD device
  502. *
  503. * Given a number and NULL address, return the num'th entry in the device
  504. * table, if any. Given an address and num == -1, search the device table
  505. * for a device with that address and return if it's still present. Given
  506. * both, return the num'th driver only if its address matches. Return
  507. * error code if not.
  508. */
  509. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  510. {
  511. struct mtd_info *ret = NULL, *other;
  512. int err = -ENODEV;
  513. mutex_lock(&mtd_table_mutex);
  514. if (num == -1) {
  515. mtd_for_each_device(other) {
  516. if (other == mtd) {
  517. ret = mtd;
  518. break;
  519. }
  520. }
  521. } else if (num >= 0) {
  522. ret = idr_find(&mtd_idr, num);
  523. if (mtd && mtd != ret)
  524. ret = NULL;
  525. }
  526. if (!ret) {
  527. ret = ERR_PTR(err);
  528. goto out;
  529. }
  530. err = __get_mtd_device(ret);
  531. if (err)
  532. ret = ERR_PTR(err);
  533. out:
  534. mutex_unlock(&mtd_table_mutex);
  535. return ret;
  536. }
  537. EXPORT_SYMBOL_GPL(get_mtd_device);
  538. int __get_mtd_device(struct mtd_info *mtd)
  539. {
  540. int err;
  541. if (!try_module_get(mtd->owner))
  542. return -ENODEV;
  543. if (mtd->_get_device) {
  544. err = mtd->_get_device(mtd);
  545. if (err) {
  546. module_put(mtd->owner);
  547. return err;
  548. }
  549. }
  550. mtd->usecount++;
  551. return 0;
  552. }
  553. EXPORT_SYMBOL_GPL(__get_mtd_device);
  554. /**
  555. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  556. * device name
  557. * @name: MTD device name to open
  558. *
  559. * This function returns MTD device description structure in case of
  560. * success and an error code in case of failure.
  561. */
  562. struct mtd_info *get_mtd_device_nm(const char *name)
  563. {
  564. int err = -ENODEV;
  565. struct mtd_info *mtd = NULL, *other;
  566. mutex_lock(&mtd_table_mutex);
  567. mtd_for_each_device(other) {
  568. if (!strcmp(name, other->name)) {
  569. mtd = other;
  570. break;
  571. }
  572. }
  573. if (!mtd)
  574. goto out_unlock;
  575. err = __get_mtd_device(mtd);
  576. if (err)
  577. goto out_unlock;
  578. mutex_unlock(&mtd_table_mutex);
  579. return mtd;
  580. out_unlock:
  581. mutex_unlock(&mtd_table_mutex);
  582. return ERR_PTR(err);
  583. }
  584. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  585. void put_mtd_device(struct mtd_info *mtd)
  586. {
  587. mutex_lock(&mtd_table_mutex);
  588. __put_mtd_device(mtd);
  589. mutex_unlock(&mtd_table_mutex);
  590. }
  591. EXPORT_SYMBOL_GPL(put_mtd_device);
  592. void __put_mtd_device(struct mtd_info *mtd)
  593. {
  594. --mtd->usecount;
  595. BUG_ON(mtd->usecount < 0);
  596. if (mtd->_put_device)
  597. mtd->_put_device(mtd);
  598. module_put(mtd->owner);
  599. }
  600. EXPORT_SYMBOL_GPL(__put_mtd_device);
  601. /*
  602. * Erase is an asynchronous operation. Device drivers are supposed
  603. * to call instr->callback() whenever the operation completes, even
  604. * if it completes with a failure.
  605. * Callers are supposed to pass a callback function and wait for it
  606. * to be called before writing to the block.
  607. */
  608. int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  609. {
  610. if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr)
  611. return -EINVAL;
  612. if (!(mtd->flags & MTD_WRITEABLE))
  613. return -EROFS;
  614. instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN;
  615. if (!instr->len) {
  616. instr->state = MTD_ERASE_DONE;
  617. mtd_erase_callback(instr);
  618. return 0;
  619. }
  620. return mtd->_erase(mtd, instr);
  621. }
  622. EXPORT_SYMBOL_GPL(mtd_erase);
  623. /*
  624. * This stuff for eXecute-In-Place. phys is optional and may be set to NULL.
  625. */
  626. int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  627. void **virt, resource_size_t *phys)
  628. {
  629. *retlen = 0;
  630. *virt = NULL;
  631. if (phys)
  632. *phys = 0;
  633. if (!mtd->_point)
  634. return -EOPNOTSUPP;
  635. if (from < 0 || from > mtd->size || len > mtd->size - from)
  636. return -EINVAL;
  637. if (!len)
  638. return 0;
  639. return mtd->_point(mtd, from, len, retlen, virt, phys);
  640. }
  641. EXPORT_SYMBOL_GPL(mtd_point);
  642. /* We probably shouldn't allow XIP if the unpoint isn't a NULL */
  643. int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  644. {
  645. if (!mtd->_point)
  646. return -EOPNOTSUPP;
  647. if (from < 0 || from > mtd->size || len > mtd->size - from)
  648. return -EINVAL;
  649. if (!len)
  650. return 0;
  651. return mtd->_unpoint(mtd, from, len);
  652. }
  653. EXPORT_SYMBOL_GPL(mtd_unpoint);
  654. /*
  655. * Allow NOMMU mmap() to directly map the device (if not NULL)
  656. * - return the address to which the offset maps
  657. * - return -ENOSYS to indicate refusal to do the mapping
  658. */
  659. unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
  660. unsigned long offset, unsigned long flags)
  661. {
  662. if (!mtd->_get_unmapped_area)
  663. return -EOPNOTSUPP;
  664. if (offset > mtd->size || len > mtd->size - offset)
  665. return -EINVAL;
  666. return mtd->_get_unmapped_area(mtd, len, offset, flags);
  667. }
  668. EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
  669. int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
  670. u_char *buf)
  671. {
  672. int ret_code;
  673. *retlen = 0;
  674. if (from < 0 || from > mtd->size || len > mtd->size - from)
  675. return -EINVAL;
  676. if (!len)
  677. return 0;
  678. /*
  679. * In the absence of an error, drivers return a non-negative integer
  680. * representing the maximum number of bitflips that were corrected on
  681. * any one ecc region (if applicable; zero otherwise).
  682. */
  683. ret_code = mtd->_read(mtd, from, len, retlen, buf);
  684. if (unlikely(ret_code < 0))
  685. return ret_code;
  686. if (mtd->ecc_strength == 0)
  687. return 0; /* device lacks ecc */
  688. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  689. }
  690. EXPORT_SYMBOL_GPL(mtd_read);
  691. int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  692. const u_char *buf)
  693. {
  694. *retlen = 0;
  695. if (to < 0 || to > mtd->size || len > mtd->size - to)
  696. return -EINVAL;
  697. if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
  698. return -EROFS;
  699. if (!len)
  700. return 0;
  701. return mtd->_write(mtd, to, len, retlen, buf);
  702. }
  703. EXPORT_SYMBOL_GPL(mtd_write);
  704. /*
  705. * In blackbox flight recorder like scenarios we want to make successful writes
  706. * in interrupt context. panic_write() is only intended to be called when its
  707. * known the kernel is about to panic and we need the write to succeed. Since
  708. * the kernel is not going to be running for much longer, this function can
  709. * break locks and delay to ensure the write succeeds (but not sleep).
  710. */
  711. int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
  712. const u_char *buf)
  713. {
  714. *retlen = 0;
  715. if (!mtd->_panic_write)
  716. return -EOPNOTSUPP;
  717. if (to < 0 || to > mtd->size || len > mtd->size - to)
  718. return -EINVAL;
  719. if (!(mtd->flags & MTD_WRITEABLE))
  720. return -EROFS;
  721. if (!len)
  722. return 0;
  723. return mtd->_panic_write(mtd, to, len, retlen, buf);
  724. }
  725. EXPORT_SYMBOL_GPL(mtd_panic_write);
  726. int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
  727. {
  728. int ret_code;
  729. ops->retlen = ops->oobretlen = 0;
  730. if (!mtd->_read_oob)
  731. return -EOPNOTSUPP;
  732. /*
  733. * In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
  734. * similar to mtd->_read(), returning a non-negative integer
  735. * representing max bitflips. In other cases, mtd->_read_oob() may
  736. * return -EUCLEAN. In all cases, perform similar logic to mtd_read().
  737. */
  738. ret_code = mtd->_read_oob(mtd, from, ops);
  739. if (unlikely(ret_code < 0))
  740. return ret_code;
  741. if (mtd->ecc_strength == 0)
  742. return 0; /* device lacks ecc */
  743. return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
  744. }
  745. EXPORT_SYMBOL_GPL(mtd_read_oob);
  746. /*
  747. * Method to access the protection register area, present in some flash
  748. * devices. The user data is one time programmable but the factory data is read
  749. * only.
  750. */
  751. int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  752. struct otp_info *buf)
  753. {
  754. if (!mtd->_get_fact_prot_info)
  755. return -EOPNOTSUPP;
  756. if (!len)
  757. return 0;
  758. return mtd->_get_fact_prot_info(mtd, len, retlen, buf);
  759. }
  760. EXPORT_SYMBOL_GPL(mtd_get_fact_prot_info);
  761. int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  762. size_t *retlen, u_char *buf)
  763. {
  764. *retlen = 0;
  765. if (!mtd->_read_fact_prot_reg)
  766. return -EOPNOTSUPP;
  767. if (!len)
  768. return 0;
  769. return mtd->_read_fact_prot_reg(mtd, from, len, retlen, buf);
  770. }
  771. EXPORT_SYMBOL_GPL(mtd_read_fact_prot_reg);
  772. int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
  773. struct otp_info *buf)
  774. {
  775. if (!mtd->_get_user_prot_info)
  776. return -EOPNOTSUPP;
  777. if (!len)
  778. return 0;
  779. return mtd->_get_user_prot_info(mtd, len, retlen, buf);
  780. }
  781. EXPORT_SYMBOL_GPL(mtd_get_user_prot_info);
  782. int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
  783. size_t *retlen, u_char *buf)
  784. {
  785. *retlen = 0;
  786. if (!mtd->_read_user_prot_reg)
  787. return -EOPNOTSUPP;
  788. if (!len)
  789. return 0;
  790. return mtd->_read_user_prot_reg(mtd, from, len, retlen, buf);
  791. }
  792. EXPORT_SYMBOL_GPL(mtd_read_user_prot_reg);
  793. int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
  794. size_t *retlen, u_char *buf)
  795. {
  796. int ret;
  797. *retlen = 0;
  798. if (!mtd->_write_user_prot_reg)
  799. return -EOPNOTSUPP;
  800. if (!len)
  801. return 0;
  802. ret = mtd->_write_user_prot_reg(mtd, to, len, retlen, buf);
  803. if (ret)
  804. return ret;
  805. /*
  806. * If no data could be written at all, we are out of memory and
  807. * must return -ENOSPC.
  808. */
  809. return (*retlen) ? 0 : -ENOSPC;
  810. }
  811. EXPORT_SYMBOL_GPL(mtd_write_user_prot_reg);
  812. int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len)
  813. {
  814. if (!mtd->_lock_user_prot_reg)
  815. return -EOPNOTSUPP;
  816. if (!len)
  817. return 0;
  818. return mtd->_lock_user_prot_reg(mtd, from, len);
  819. }
  820. EXPORT_SYMBOL_GPL(mtd_lock_user_prot_reg);
  821. /* Chip-supported device locking */
  822. int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  823. {
  824. if (!mtd->_lock)
  825. return -EOPNOTSUPP;
  826. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  827. return -EINVAL;
  828. if (!len)
  829. return 0;
  830. return mtd->_lock(mtd, ofs, len);
  831. }
  832. EXPORT_SYMBOL_GPL(mtd_lock);
  833. int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  834. {
  835. if (!mtd->_unlock)
  836. return -EOPNOTSUPP;
  837. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  838. return -EINVAL;
  839. if (!len)
  840. return 0;
  841. return mtd->_unlock(mtd, ofs, len);
  842. }
  843. EXPORT_SYMBOL_GPL(mtd_unlock);
  844. int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  845. {
  846. if (!mtd->_is_locked)
  847. return -EOPNOTSUPP;
  848. if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
  849. return -EINVAL;
  850. if (!len)
  851. return 0;
  852. return mtd->_is_locked(mtd, ofs, len);
  853. }
  854. EXPORT_SYMBOL_GPL(mtd_is_locked);
  855. int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs)
  856. {
  857. if (!mtd->_block_isbad)
  858. return 0;
  859. if (ofs < 0 || ofs > mtd->size)
  860. return -EINVAL;
  861. return mtd->_block_isbad(mtd, ofs);
  862. }
  863. EXPORT_SYMBOL_GPL(mtd_block_isbad);
  864. int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs)
  865. {
  866. if (!mtd->_block_markbad)
  867. return -EOPNOTSUPP;
  868. if (ofs < 0 || ofs > mtd->size)
  869. return -EINVAL;
  870. if (!(mtd->flags & MTD_WRITEABLE))
  871. return -EROFS;
  872. return mtd->_block_markbad(mtd, ofs);
  873. }
  874. EXPORT_SYMBOL_GPL(mtd_block_markbad);
  875. /*
  876. * default_mtd_writev - the default writev method
  877. * @mtd: mtd device description object pointer
  878. * @vecs: the vectors to write
  879. * @count: count of vectors in @vecs
  880. * @to: the MTD device offset to write to
  881. * @retlen: on exit contains the count of bytes written to the MTD device.
  882. *
  883. * This function returns zero in case of success and a negative error code in
  884. * case of failure.
  885. */
  886. static int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  887. unsigned long count, loff_t to, size_t *retlen)
  888. {
  889. unsigned long i;
  890. size_t totlen = 0, thislen;
  891. int ret = 0;
  892. for (i = 0; i < count; i++) {
  893. if (!vecs[i].iov_len)
  894. continue;
  895. ret = mtd_write(mtd, to, vecs[i].iov_len, &thislen,
  896. vecs[i].iov_base);
  897. totlen += thislen;
  898. if (ret || thislen != vecs[i].iov_len)
  899. break;
  900. to += vecs[i].iov_len;
  901. }
  902. *retlen = totlen;
  903. return ret;
  904. }
  905. /*
  906. * mtd_writev - the vector-based MTD write method
  907. * @mtd: mtd device description object pointer
  908. * @vecs: the vectors to write
  909. * @count: count of vectors in @vecs
  910. * @to: the MTD device offset to write to
  911. * @retlen: on exit contains the count of bytes written to the MTD device.
  912. *
  913. * This function returns zero in case of success and a negative error code in
  914. * case of failure.
  915. */
  916. int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  917. unsigned long count, loff_t to, size_t *retlen)
  918. {
  919. *retlen = 0;
  920. if (!(mtd->flags & MTD_WRITEABLE))
  921. return -EROFS;
  922. if (!mtd->_writev)
  923. return default_mtd_writev(mtd, vecs, count, to, retlen);
  924. return mtd->_writev(mtd, vecs, count, to, retlen);
  925. }
  926. EXPORT_SYMBOL_GPL(mtd_writev);
  927. /**
  928. * mtd_kmalloc_up_to - allocate a contiguous buffer up to the specified size
  929. * @mtd: mtd device description object pointer
  930. * @size: a pointer to the ideal or maximum size of the allocation, points
  931. * to the actual allocation size on success.
  932. *
  933. * This routine attempts to allocate a contiguous kernel buffer up to
  934. * the specified size, backing off the size of the request exponentially
  935. * until the request succeeds or until the allocation size falls below
  936. * the system page size. This attempts to make sure it does not adversely
  937. * impact system performance, so when allocating more than one page, we
  938. * ask the memory allocator to avoid re-trying, swapping, writing back
  939. * or performing I/O.
  940. *
  941. * Note, this function also makes sure that the allocated buffer is aligned to
  942. * the MTD device's min. I/O unit, i.e. the "mtd->writesize" value.
  943. *
  944. * This is called, for example by mtd_{read,write} and jffs2_scan_medium,
  945. * to handle smaller (i.e. degraded) buffer allocations under low- or
  946. * fragmented-memory situations where such reduced allocations, from a
  947. * requested ideal, are allowed.
  948. *
  949. * Returns a pointer to the allocated buffer on success; otherwise, NULL.
  950. */
  951. void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size)
  952. {
  953. gfp_t flags = __GFP_NOWARN | __GFP_WAIT |
  954. __GFP_NORETRY | __GFP_NO_KSWAPD;
  955. size_t min_alloc = max_t(size_t, mtd->writesize, PAGE_SIZE);
  956. void *kbuf;
  957. *size = min_t(size_t, *size, KMALLOC_MAX_SIZE);
  958. while (*size > min_alloc) {
  959. kbuf = kmalloc(*size, flags);
  960. if (kbuf)
  961. return kbuf;
  962. *size >>= 1;
  963. *size = ALIGN(*size, mtd->writesize);
  964. }
  965. /*
  966. * For the last resort allocation allow 'kmalloc()' to do all sorts of
  967. * things (write-back, dropping caches, etc) by using GFP_KERNEL.
  968. */
  969. return kmalloc(*size, GFP_KERNEL);
  970. }
  971. EXPORT_SYMBOL_GPL(mtd_kmalloc_up_to);
  972. #ifdef CONFIG_PROC_FS
  973. /*====================================================================*/
  974. /* Support for /proc/mtd */
  975. static int mtd_proc_show(struct seq_file *m, void *v)
  976. {
  977. struct mtd_info *mtd;
  978. seq_puts(m, "dev: size erasesize name\n");
  979. mutex_lock(&mtd_table_mutex);
  980. mtd_for_each_device(mtd) {
  981. seq_printf(m, "mtd%d: %8.8llx %8.8x \"%s\"\n",
  982. mtd->index, (unsigned long long)mtd->size,
  983. mtd->erasesize, mtd->name);
  984. }
  985. mutex_unlock(&mtd_table_mutex);
  986. return 0;
  987. }
  988. static int mtd_proc_open(struct inode *inode, struct file *file)
  989. {
  990. return single_open(file, mtd_proc_show, NULL);
  991. }
  992. static const struct file_operations mtd_proc_ops = {
  993. .open = mtd_proc_open,
  994. .read = seq_read,
  995. .llseek = seq_lseek,
  996. .release = single_release,
  997. };
  998. #endif /* CONFIG_PROC_FS */
  999. /*====================================================================*/
  1000. /* Init code */
  1001. static int __init mtd_bdi_init(struct backing_dev_info *bdi, const char *name)
  1002. {
  1003. int ret;
  1004. ret = bdi_init(bdi);
  1005. if (!ret)
  1006. ret = bdi_register(bdi, NULL, "%s", name);
  1007. if (ret)
  1008. bdi_destroy(bdi);
  1009. return ret;
  1010. }
  1011. static struct proc_dir_entry *proc_mtd;
  1012. static int __init init_mtd(void)
  1013. {
  1014. int ret;
  1015. ret = class_register(&mtd_class);
  1016. if (ret)
  1017. goto err_reg;
  1018. ret = mtd_bdi_init(&mtd_bdi_unmappable, "mtd-unmap");
  1019. if (ret)
  1020. goto err_bdi1;
  1021. ret = mtd_bdi_init(&mtd_bdi_ro_mappable, "mtd-romap");
  1022. if (ret)
  1023. goto err_bdi2;
  1024. ret = mtd_bdi_init(&mtd_bdi_rw_mappable, "mtd-rwmap");
  1025. if (ret)
  1026. goto err_bdi3;
  1027. proc_mtd = proc_create("mtd", 0, NULL, &mtd_proc_ops);
  1028. ret = init_mtdchar();
  1029. if (ret)
  1030. goto out_procfs;
  1031. return 0;
  1032. out_procfs:
  1033. if (proc_mtd)
  1034. remove_proc_entry("mtd", NULL);
  1035. err_bdi3:
  1036. bdi_destroy(&mtd_bdi_ro_mappable);
  1037. err_bdi2:
  1038. bdi_destroy(&mtd_bdi_unmappable);
  1039. err_bdi1:
  1040. class_unregister(&mtd_class);
  1041. err_reg:
  1042. pr_err("Error registering mtd class or bdi: %d\n", ret);
  1043. return ret;
  1044. }
  1045. static void __exit cleanup_mtd(void)
  1046. {
  1047. cleanup_mtdchar();
  1048. if (proc_mtd)
  1049. remove_proc_entry("mtd", NULL);
  1050. class_unregister(&mtd_class);
  1051. bdi_destroy(&mtd_bdi_unmappable);
  1052. bdi_destroy(&mtd_bdi_ro_mappable);
  1053. bdi_destroy(&mtd_bdi_rw_mappable);
  1054. }
  1055. module_init(init_mtd);
  1056. module_exit(cleanup_mtd);
  1057. MODULE_LICENSE("GPL");
  1058. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1059. MODULE_DESCRIPTION("Core MTD registration and access routines");