mtdcore.c 33 KB

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