mtdcore.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. * Core registration and callback routines for MTD
  3. * drivers and users.
  4. *
  5. */
  6. #include <linux/module.h>
  7. #include <linux/kernel.h>
  8. #include <linux/ptrace.h>
  9. #include <linux/string.h>
  10. #include <linux/timer.h>
  11. #include <linux/major.h>
  12. #include <linux/fs.h>
  13. #include <linux/err.h>
  14. #include <linux/ioctl.h>
  15. #include <linux/init.h>
  16. #include <linux/mtd/compatmac.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/mtd/mtd.h>
  19. #include "internal.h"
  20. #include "mtdcore.h"
  21. static int mtd_cls_suspend(struct device *dev, pm_message_t state);
  22. static int mtd_cls_resume(struct device *dev);
  23. static struct class mtd_class = {
  24. .name = "mtd",
  25. .owner = THIS_MODULE,
  26. .suspend = mtd_cls_suspend,
  27. .resume = mtd_cls_resume,
  28. };
  29. /* These are exported solely for the purpose of mtd_blkdevs.c. You
  30. should not use them for _anything_ else */
  31. DEFINE_MUTEX(mtd_table_mutex);
  32. struct mtd_info *mtd_table[MAX_MTD_DEVICES];
  33. EXPORT_SYMBOL_GPL(mtd_table_mutex);
  34. EXPORT_SYMBOL_GPL(mtd_table);
  35. static LIST_HEAD(mtd_notifiers);
  36. #if defined(CONFIG_MTD_CHAR) || defined(CONFIG_MTD_CHAR_MODULE)
  37. #define MTD_DEVT(index) MKDEV(MTD_CHAR_MAJOR, (index)*2)
  38. #else
  39. #define MTD_DEVT(index) 0
  40. #endif
  41. /* REVISIT once MTD uses the driver model better, whoever allocates
  42. * the mtd_info will probably want to use the release() hook...
  43. */
  44. static void mtd_release(struct device *dev)
  45. {
  46. dev_t index = MTD_DEVT(dev_to_mtd(dev)->index);
  47. /* remove /dev/mtdXro node if needed */
  48. if (index)
  49. device_destroy(&mtd_class, index + 1);
  50. }
  51. static int mtd_cls_suspend(struct device *dev, pm_message_t state)
  52. {
  53. struct mtd_info *mtd = dev_to_mtd(dev);
  54. if (mtd && mtd->suspend)
  55. return mtd->suspend(mtd);
  56. else
  57. return 0;
  58. }
  59. static int mtd_cls_resume(struct device *dev)
  60. {
  61. struct mtd_info *mtd = dev_to_mtd(dev);
  62. if (mtd && mtd->resume)
  63. mtd->resume(mtd);
  64. return 0;
  65. }
  66. static ssize_t mtd_type_show(struct device *dev,
  67. struct device_attribute *attr, char *buf)
  68. {
  69. struct mtd_info *mtd = dev_to_mtd(dev);
  70. char *type;
  71. switch (mtd->type) {
  72. case MTD_ABSENT:
  73. type = "absent";
  74. break;
  75. case MTD_RAM:
  76. type = "ram";
  77. break;
  78. case MTD_ROM:
  79. type = "rom";
  80. break;
  81. case MTD_NORFLASH:
  82. type = "nor";
  83. break;
  84. case MTD_NANDFLASH:
  85. type = "nand";
  86. break;
  87. case MTD_DATAFLASH:
  88. type = "dataflash";
  89. break;
  90. case MTD_UBIVOLUME:
  91. type = "ubi";
  92. break;
  93. default:
  94. type = "unknown";
  95. }
  96. return snprintf(buf, PAGE_SIZE, "%s\n", type);
  97. }
  98. static DEVICE_ATTR(type, S_IRUGO, mtd_type_show, NULL);
  99. static ssize_t mtd_flags_show(struct device *dev,
  100. struct device_attribute *attr, char *buf)
  101. {
  102. struct mtd_info *mtd = dev_to_mtd(dev);
  103. return snprintf(buf, PAGE_SIZE, "0x%lx\n", (unsigned long)mtd->flags);
  104. }
  105. static DEVICE_ATTR(flags, S_IRUGO, mtd_flags_show, NULL);
  106. static ssize_t mtd_size_show(struct device *dev,
  107. struct device_attribute *attr, char *buf)
  108. {
  109. struct mtd_info *mtd = dev_to_mtd(dev);
  110. return snprintf(buf, PAGE_SIZE, "%llu\n",
  111. (unsigned long long)mtd->size);
  112. }
  113. static DEVICE_ATTR(size, S_IRUGO, mtd_size_show, NULL);
  114. static ssize_t mtd_erasesize_show(struct device *dev,
  115. struct device_attribute *attr, char *buf)
  116. {
  117. struct mtd_info *mtd = dev_to_mtd(dev);
  118. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->erasesize);
  119. }
  120. static DEVICE_ATTR(erasesize, S_IRUGO, mtd_erasesize_show, NULL);
  121. static ssize_t mtd_writesize_show(struct device *dev,
  122. struct device_attribute *attr, char *buf)
  123. {
  124. struct mtd_info *mtd = dev_to_mtd(dev);
  125. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->writesize);
  126. }
  127. static DEVICE_ATTR(writesize, S_IRUGO, mtd_writesize_show, NULL);
  128. static ssize_t mtd_subpagesize_show(struct device *dev,
  129. struct device_attribute *attr, char *buf)
  130. {
  131. struct mtd_info *mtd = dev_to_mtd(dev);
  132. unsigned int subpagesize = mtd->writesize >> mtd->subpage_sft;
  133. return snprintf(buf, PAGE_SIZE, "%u\n", subpagesize);
  134. }
  135. static DEVICE_ATTR(subpagesize, S_IRUGO, mtd_subpagesize_show, NULL);
  136. static ssize_t mtd_oobsize_show(struct device *dev,
  137. struct device_attribute *attr, char *buf)
  138. {
  139. struct mtd_info *mtd = dev_to_mtd(dev);
  140. return snprintf(buf, PAGE_SIZE, "%lu\n", (unsigned long)mtd->oobsize);
  141. }
  142. static DEVICE_ATTR(oobsize, S_IRUGO, mtd_oobsize_show, NULL);
  143. static ssize_t mtd_numeraseregions_show(struct device *dev,
  144. struct device_attribute *attr, char *buf)
  145. {
  146. struct mtd_info *mtd = dev_to_mtd(dev);
  147. return snprintf(buf, PAGE_SIZE, "%u\n", mtd->numeraseregions);
  148. }
  149. static DEVICE_ATTR(numeraseregions, S_IRUGO, mtd_numeraseregions_show,
  150. NULL);
  151. static ssize_t mtd_name_show(struct device *dev,
  152. struct device_attribute *attr, char *buf)
  153. {
  154. struct mtd_info *mtd = dev_to_mtd(dev);
  155. return snprintf(buf, PAGE_SIZE, "%s\n", mtd->name);
  156. }
  157. static DEVICE_ATTR(name, S_IRUGO, mtd_name_show, NULL);
  158. static struct attribute *mtd_attrs[] = {
  159. &dev_attr_type.attr,
  160. &dev_attr_flags.attr,
  161. &dev_attr_size.attr,
  162. &dev_attr_erasesize.attr,
  163. &dev_attr_writesize.attr,
  164. &dev_attr_subpagesize.attr,
  165. &dev_attr_oobsize.attr,
  166. &dev_attr_numeraseregions.attr,
  167. &dev_attr_name.attr,
  168. NULL,
  169. };
  170. static struct attribute_group mtd_group = {
  171. .attrs = mtd_attrs,
  172. };
  173. static const struct attribute_group *mtd_groups[] = {
  174. &mtd_group,
  175. NULL,
  176. };
  177. static struct device_type mtd_devtype = {
  178. .name = "mtd",
  179. .groups = mtd_groups,
  180. .release = mtd_release,
  181. };
  182. /**
  183. * add_mtd_device - register an MTD device
  184. * @mtd: pointer to new MTD device info structure
  185. *
  186. * Add a device to the list of MTD devices present in the system, and
  187. * notify each currently active MTD 'user' of its arrival. Returns
  188. * zero on success or 1 on failure, which currently will only happen
  189. * if the number of present devices exceeds MAX_MTD_DEVICES (i.e. 16)
  190. * or there's a sysfs error.
  191. */
  192. int add_mtd_device(struct mtd_info *mtd)
  193. {
  194. int i;
  195. if (!mtd->backing_dev_info) {
  196. switch (mtd->type) {
  197. case MTD_RAM:
  198. mtd->backing_dev_info = &mtd_bdi_rw_mappable;
  199. break;
  200. case MTD_ROM:
  201. mtd->backing_dev_info = &mtd_bdi_ro_mappable;
  202. break;
  203. default:
  204. mtd->backing_dev_info = &mtd_bdi_unmappable;
  205. break;
  206. }
  207. }
  208. BUG_ON(mtd->writesize == 0);
  209. mutex_lock(&mtd_table_mutex);
  210. for (i=0; i < MAX_MTD_DEVICES; i++)
  211. if (!mtd_table[i]) {
  212. struct mtd_notifier *not;
  213. mtd_table[i] = mtd;
  214. mtd->index = i;
  215. mtd->usecount = 0;
  216. if (is_power_of_2(mtd->erasesize))
  217. mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
  218. else
  219. mtd->erasesize_shift = 0;
  220. if (is_power_of_2(mtd->writesize))
  221. mtd->writesize_shift = ffs(mtd->writesize) - 1;
  222. else
  223. mtd->writesize_shift = 0;
  224. mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
  225. mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
  226. /* Some chips always power up locked. Unlock them now */
  227. if ((mtd->flags & MTD_WRITEABLE)
  228. && (mtd->flags & MTD_POWERUP_LOCK) && mtd->unlock) {
  229. if (mtd->unlock(mtd, 0, mtd->size))
  230. printk(KERN_WARNING
  231. "%s: unlock failed, "
  232. "writes may not work\n",
  233. mtd->name);
  234. }
  235. /* Caller should have set dev.parent to match the
  236. * physical device.
  237. */
  238. mtd->dev.type = &mtd_devtype;
  239. mtd->dev.class = &mtd_class;
  240. mtd->dev.devt = MTD_DEVT(i);
  241. dev_set_name(&mtd->dev, "mtd%d", i);
  242. dev_set_drvdata(&mtd->dev, mtd);
  243. if (device_register(&mtd->dev) != 0) {
  244. mtd_table[i] = NULL;
  245. break;
  246. }
  247. if (MTD_DEVT(i))
  248. device_create(&mtd_class, mtd->dev.parent,
  249. MTD_DEVT(i) + 1,
  250. NULL, "mtd%dro", i);
  251. DEBUG(0, "mtd: Giving out device %d to %s\n",i, mtd->name);
  252. /* No need to get a refcount on the module containing
  253. the notifier, since we hold the mtd_table_mutex */
  254. list_for_each_entry(not, &mtd_notifiers, list)
  255. not->add(mtd);
  256. mutex_unlock(&mtd_table_mutex);
  257. /* We _know_ we aren't being removed, because
  258. our caller is still holding us here. So none
  259. of this try_ nonsense, and no bitching about it
  260. either. :) */
  261. __module_get(THIS_MODULE);
  262. return 0;
  263. }
  264. mutex_unlock(&mtd_table_mutex);
  265. return 1;
  266. }
  267. /**
  268. * del_mtd_device - unregister an MTD device
  269. * @mtd: pointer to MTD device info structure
  270. *
  271. * Remove a device from the list of MTD devices present in the system,
  272. * and notify each currently active MTD 'user' of its departure.
  273. * Returns zero on success or 1 on failure, which currently will happen
  274. * if the requested device does not appear to be present in the list.
  275. */
  276. int del_mtd_device (struct mtd_info *mtd)
  277. {
  278. int ret;
  279. mutex_lock(&mtd_table_mutex);
  280. if (mtd_table[mtd->index] != mtd) {
  281. ret = -ENODEV;
  282. } else if (mtd->usecount) {
  283. printk(KERN_NOTICE "Removing MTD device #%d (%s) with use count %d\n",
  284. mtd->index, mtd->name, mtd->usecount);
  285. ret = -EBUSY;
  286. } else {
  287. struct mtd_notifier *not;
  288. device_unregister(&mtd->dev);
  289. /* No need to get a refcount on the module containing
  290. the notifier, since we hold the mtd_table_mutex */
  291. list_for_each_entry(not, &mtd_notifiers, list)
  292. not->remove(mtd);
  293. mtd_table[mtd->index] = NULL;
  294. module_put(THIS_MODULE);
  295. ret = 0;
  296. }
  297. mutex_unlock(&mtd_table_mutex);
  298. return ret;
  299. }
  300. /**
  301. * register_mtd_user - register a 'user' of MTD devices.
  302. * @new: pointer to notifier info structure
  303. *
  304. * Registers a pair of callbacks function to be called upon addition
  305. * or removal of MTD devices. Causes the 'add' callback to be immediately
  306. * invoked for each MTD device currently present in the system.
  307. */
  308. void register_mtd_user (struct mtd_notifier *new)
  309. {
  310. int i;
  311. mutex_lock(&mtd_table_mutex);
  312. list_add(&new->list, &mtd_notifiers);
  313. __module_get(THIS_MODULE);
  314. for (i=0; i< MAX_MTD_DEVICES; i++)
  315. if (mtd_table[i])
  316. new->add(mtd_table[i]);
  317. mutex_unlock(&mtd_table_mutex);
  318. }
  319. /**
  320. * unregister_mtd_user - unregister a 'user' of MTD devices.
  321. * @old: pointer to notifier info structure
  322. *
  323. * Removes a callback function pair from the list of 'users' to be
  324. * notified upon addition or removal of MTD devices. Causes the
  325. * 'remove' callback to be immediately invoked for each MTD device
  326. * currently present in the system.
  327. */
  328. int unregister_mtd_user (struct mtd_notifier *old)
  329. {
  330. int i;
  331. mutex_lock(&mtd_table_mutex);
  332. module_put(THIS_MODULE);
  333. for (i=0; i< MAX_MTD_DEVICES; i++)
  334. if (mtd_table[i])
  335. old->remove(mtd_table[i]);
  336. list_del(&old->list);
  337. mutex_unlock(&mtd_table_mutex);
  338. return 0;
  339. }
  340. /**
  341. * get_mtd_device - obtain a validated handle for an MTD device
  342. * @mtd: last known address of the required MTD device
  343. * @num: internal device number of the required MTD device
  344. *
  345. * Given a number and NULL address, return the num'th entry in the device
  346. * table, if any. Given an address and num == -1, search the device table
  347. * for a device with that address and return if it's still present. Given
  348. * both, return the num'th driver only if its address matches. Return
  349. * error code if not.
  350. */
  351. struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num)
  352. {
  353. struct mtd_info *ret = NULL;
  354. int i, err = -ENODEV;
  355. mutex_lock(&mtd_table_mutex);
  356. if (num == -1) {
  357. for (i=0; i< MAX_MTD_DEVICES; i++)
  358. if (mtd_table[i] == mtd)
  359. ret = mtd_table[i];
  360. } else if (num >= 0 && num < MAX_MTD_DEVICES) {
  361. ret = mtd_table[num];
  362. if (mtd && mtd != ret)
  363. ret = NULL;
  364. }
  365. if (!ret)
  366. goto out_unlock;
  367. if (!try_module_get(ret->owner))
  368. goto out_unlock;
  369. if (ret->get_device) {
  370. err = ret->get_device(ret);
  371. if (err)
  372. goto out_put;
  373. }
  374. ret->usecount++;
  375. mutex_unlock(&mtd_table_mutex);
  376. return ret;
  377. out_put:
  378. module_put(ret->owner);
  379. out_unlock:
  380. mutex_unlock(&mtd_table_mutex);
  381. return ERR_PTR(err);
  382. }
  383. /**
  384. * get_mtd_device_nm - obtain a validated handle for an MTD device by
  385. * device name
  386. * @name: MTD device name to open
  387. *
  388. * This function returns MTD device description structure in case of
  389. * success and an error code in case of failure.
  390. */
  391. struct mtd_info *get_mtd_device_nm(const char *name)
  392. {
  393. int i, err = -ENODEV;
  394. struct mtd_info *mtd = NULL;
  395. mutex_lock(&mtd_table_mutex);
  396. for (i = 0; i < MAX_MTD_DEVICES; i++) {
  397. if (mtd_table[i] && !strcmp(name, mtd_table[i]->name)) {
  398. mtd = mtd_table[i];
  399. break;
  400. }
  401. }
  402. if (!mtd)
  403. goto out_unlock;
  404. if (!try_module_get(mtd->owner))
  405. goto out_unlock;
  406. if (mtd->get_device) {
  407. err = mtd->get_device(mtd);
  408. if (err)
  409. goto out_put;
  410. }
  411. mtd->usecount++;
  412. mutex_unlock(&mtd_table_mutex);
  413. return mtd;
  414. out_put:
  415. module_put(mtd->owner);
  416. out_unlock:
  417. mutex_unlock(&mtd_table_mutex);
  418. return ERR_PTR(err);
  419. }
  420. void put_mtd_device(struct mtd_info *mtd)
  421. {
  422. int c;
  423. mutex_lock(&mtd_table_mutex);
  424. c = --mtd->usecount;
  425. if (mtd->put_device)
  426. mtd->put_device(mtd);
  427. mutex_unlock(&mtd_table_mutex);
  428. BUG_ON(c < 0);
  429. module_put(mtd->owner);
  430. }
  431. /* default_mtd_writev - default mtd writev method for MTD devices that
  432. * don't implement their own
  433. */
  434. int default_mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
  435. unsigned long count, loff_t to, size_t *retlen)
  436. {
  437. unsigned long i;
  438. size_t totlen = 0, thislen;
  439. int ret = 0;
  440. if(!mtd->write) {
  441. ret = -EROFS;
  442. } else {
  443. for (i=0; i<count; i++) {
  444. if (!vecs[i].iov_len)
  445. continue;
  446. ret = mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
  447. totlen += thislen;
  448. if (ret || thislen != vecs[i].iov_len)
  449. break;
  450. to += vecs[i].iov_len;
  451. }
  452. }
  453. if (retlen)
  454. *retlen = totlen;
  455. return ret;
  456. }
  457. EXPORT_SYMBOL_GPL(add_mtd_device);
  458. EXPORT_SYMBOL_GPL(del_mtd_device);
  459. EXPORT_SYMBOL_GPL(get_mtd_device);
  460. EXPORT_SYMBOL_GPL(get_mtd_device_nm);
  461. EXPORT_SYMBOL_GPL(put_mtd_device);
  462. EXPORT_SYMBOL_GPL(register_mtd_user);
  463. EXPORT_SYMBOL_GPL(unregister_mtd_user);
  464. EXPORT_SYMBOL_GPL(default_mtd_writev);
  465. #ifdef CONFIG_PROC_FS
  466. /*====================================================================*/
  467. /* Support for /proc/mtd */
  468. static struct proc_dir_entry *proc_mtd;
  469. static inline int mtd_proc_info (char *buf, int i)
  470. {
  471. struct mtd_info *this = mtd_table[i];
  472. if (!this)
  473. return 0;
  474. return sprintf(buf, "mtd%d: %8.8llx %8.8x \"%s\"\n", i,
  475. (unsigned long long)this->size,
  476. this->erasesize, this->name);
  477. }
  478. static int mtd_read_proc (char *page, char **start, off_t off, int count,
  479. int *eof, void *data_unused)
  480. {
  481. int len, l, i;
  482. off_t begin = 0;
  483. mutex_lock(&mtd_table_mutex);
  484. len = sprintf(page, "dev: size erasesize name\n");
  485. for (i=0; i< MAX_MTD_DEVICES; i++) {
  486. l = mtd_proc_info(page + len, i);
  487. len += l;
  488. if (len+begin > off+count)
  489. goto done;
  490. if (len+begin < off) {
  491. begin += len;
  492. len = 0;
  493. }
  494. }
  495. *eof = 1;
  496. done:
  497. mutex_unlock(&mtd_table_mutex);
  498. if (off >= len+begin)
  499. return 0;
  500. *start = page + (off-begin);
  501. return ((count < begin+len-off) ? count : begin+len-off);
  502. }
  503. #endif /* CONFIG_PROC_FS */
  504. /*====================================================================*/
  505. /* Init code */
  506. static int __init init_mtd(void)
  507. {
  508. int ret;
  509. ret = class_register(&mtd_class);
  510. if (ret) {
  511. pr_err("Error registering mtd class: %d\n", ret);
  512. return ret;
  513. }
  514. #ifdef CONFIG_PROC_FS
  515. if ((proc_mtd = create_proc_entry( "mtd", 0, NULL )))
  516. proc_mtd->read_proc = mtd_read_proc;
  517. #endif /* CONFIG_PROC_FS */
  518. return 0;
  519. }
  520. static void __exit cleanup_mtd(void)
  521. {
  522. #ifdef CONFIG_PROC_FS
  523. if (proc_mtd)
  524. remove_proc_entry( "mtd", NULL);
  525. #endif /* CONFIG_PROC_FS */
  526. class_unregister(&mtd_class);
  527. }
  528. module_init(init_mtd);
  529. module_exit(cleanup_mtd);
  530. MODULE_LICENSE("GPL");
  531. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  532. MODULE_DESCRIPTION("Core MTD registration and access routines");