mtdcore.c 14 KB

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