mtdconcat.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * MTD device concatenation layer
  3. *
  4. * (C) 2002 Robert Kaiser <rkaiser@sysgo.de>
  5. *
  6. * NAND support by Christian Gan <cgan@iders.ca>
  7. *
  8. * This code is GPL
  9. *
  10. * $Id: mtdconcat.c,v 1.11 2005/11/07 11:14:20 gleixner Exp $
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include <linux/types.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/concat.h>
  19. /*
  20. * Our storage structure:
  21. * Subdev points to an array of pointers to struct mtd_info objects
  22. * which is allocated along with this structure
  23. *
  24. */
  25. struct mtd_concat {
  26. struct mtd_info mtd;
  27. int num_subdev;
  28. struct mtd_info **subdev;
  29. };
  30. /*
  31. * how to calculate the size required for the above structure,
  32. * including the pointer array subdev points to:
  33. */
  34. #define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
  35. ((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
  36. /*
  37. * Given a pointer to the MTD object in the mtd_concat structure,
  38. * we can retrieve the pointer to that structure with this macro.
  39. */
  40. #define CONCAT(x) ((struct mtd_concat *)(x))
  41. /*
  42. * MTD methods which look up the relevant subdevice, translate the
  43. * effective address and pass through to the subdevice.
  44. */
  45. static int
  46. concat_read(struct mtd_info *mtd, loff_t from, size_t len,
  47. size_t * retlen, u_char * buf)
  48. {
  49. struct mtd_concat *concat = CONCAT(mtd);
  50. int err = -EINVAL;
  51. int i;
  52. *retlen = 0;
  53. for (i = 0; i < concat->num_subdev; i++) {
  54. struct mtd_info *subdev = concat->subdev[i];
  55. size_t size, retsize;
  56. if (from >= subdev->size) {
  57. /* Not destined for this subdev */
  58. size = 0;
  59. from -= subdev->size;
  60. continue;
  61. }
  62. if (from + len > subdev->size)
  63. /* First part goes into this subdev */
  64. size = subdev->size - from;
  65. else
  66. /* Entire transaction goes into this subdev */
  67. size = len;
  68. err = subdev->read(subdev, from, size, &retsize, buf);
  69. if (err)
  70. break;
  71. *retlen += retsize;
  72. len -= size;
  73. if (len == 0)
  74. break;
  75. err = -EINVAL;
  76. buf += size;
  77. from = 0;
  78. }
  79. return err;
  80. }
  81. static int
  82. concat_write(struct mtd_info *mtd, loff_t to, size_t len,
  83. size_t * retlen, const u_char * buf)
  84. {
  85. struct mtd_concat *concat = CONCAT(mtd);
  86. int err = -EINVAL;
  87. int i;
  88. if (!(mtd->flags & MTD_WRITEABLE))
  89. return -EROFS;
  90. *retlen = 0;
  91. for (i = 0; i < concat->num_subdev; i++) {
  92. struct mtd_info *subdev = concat->subdev[i];
  93. size_t size, retsize;
  94. if (to >= subdev->size) {
  95. size = 0;
  96. to -= subdev->size;
  97. continue;
  98. }
  99. if (to + len > subdev->size)
  100. size = subdev->size - to;
  101. else
  102. size = len;
  103. if (!(subdev->flags & MTD_WRITEABLE))
  104. err = -EROFS;
  105. else
  106. err = subdev->write(subdev, to, size, &retsize, buf);
  107. if (err)
  108. break;
  109. *retlen += retsize;
  110. len -= size;
  111. if (len == 0)
  112. break;
  113. err = -EINVAL;
  114. buf += size;
  115. to = 0;
  116. }
  117. return err;
  118. }
  119. static int
  120. concat_read_ecc(struct mtd_info *mtd, loff_t from, size_t len,
  121. size_t * retlen, u_char * buf, u_char * eccbuf,
  122. struct nand_oobinfo *oobsel)
  123. {
  124. struct mtd_concat *concat = CONCAT(mtd);
  125. int err = -EINVAL;
  126. int i;
  127. *retlen = 0;
  128. for (i = 0; i < concat->num_subdev; i++) {
  129. struct mtd_info *subdev = concat->subdev[i];
  130. size_t size, retsize;
  131. if (from >= subdev->size) {
  132. /* Not destined for this subdev */
  133. size = 0;
  134. from -= subdev->size;
  135. continue;
  136. }
  137. if (from + len > subdev->size)
  138. /* First part goes into this subdev */
  139. size = subdev->size - from;
  140. else
  141. /* Entire transaction goes into this subdev */
  142. size = len;
  143. if (subdev->read_ecc)
  144. err = subdev->read_ecc(subdev, from, size,
  145. &retsize, buf, eccbuf, oobsel);
  146. else
  147. err = -EINVAL;
  148. if (err)
  149. break;
  150. *retlen += retsize;
  151. len -= size;
  152. if (len == 0)
  153. break;
  154. err = -EINVAL;
  155. buf += size;
  156. if (eccbuf) {
  157. eccbuf += subdev->oobsize;
  158. /* in nand.c at least, eccbufs are
  159. tagged with 2 (int)eccstatus'; we
  160. must account for these */
  161. eccbuf += 2 * (sizeof (int));
  162. }
  163. from = 0;
  164. }
  165. return err;
  166. }
  167. static int
  168. concat_write_ecc(struct mtd_info *mtd, loff_t to, size_t len,
  169. size_t * retlen, const u_char * buf, u_char * eccbuf,
  170. struct nand_oobinfo *oobsel)
  171. {
  172. struct mtd_concat *concat = CONCAT(mtd);
  173. int err = -EINVAL;
  174. int i;
  175. if (!(mtd->flags & MTD_WRITEABLE))
  176. return -EROFS;
  177. *retlen = 0;
  178. for (i = 0; i < concat->num_subdev; i++) {
  179. struct mtd_info *subdev = concat->subdev[i];
  180. size_t size, retsize;
  181. if (to >= subdev->size) {
  182. size = 0;
  183. to -= subdev->size;
  184. continue;
  185. }
  186. if (to + len > subdev->size)
  187. size = subdev->size - to;
  188. else
  189. size = len;
  190. if (!(subdev->flags & MTD_WRITEABLE))
  191. err = -EROFS;
  192. else if (subdev->write_ecc)
  193. err = subdev->write_ecc(subdev, to, size,
  194. &retsize, buf, eccbuf, oobsel);
  195. else
  196. err = -EINVAL;
  197. if (err)
  198. break;
  199. *retlen += retsize;
  200. len -= size;
  201. if (len == 0)
  202. break;
  203. err = -EINVAL;
  204. buf += size;
  205. if (eccbuf)
  206. eccbuf += subdev->oobsize;
  207. to = 0;
  208. }
  209. return err;
  210. }
  211. static int
  212. concat_read_oob(struct mtd_info *mtd, loff_t from, size_t len,
  213. size_t * retlen, u_char * buf)
  214. {
  215. struct mtd_concat *concat = CONCAT(mtd);
  216. int err = -EINVAL;
  217. int i;
  218. *retlen = 0;
  219. for (i = 0; i < concat->num_subdev; i++) {
  220. struct mtd_info *subdev = concat->subdev[i];
  221. size_t size, retsize;
  222. if (from >= subdev->size) {
  223. /* Not destined for this subdev */
  224. size = 0;
  225. from -= subdev->size;
  226. continue;
  227. }
  228. if (from + len > subdev->size)
  229. /* First part goes into this subdev */
  230. size = subdev->size - from;
  231. else
  232. /* Entire transaction goes into this subdev */
  233. size = len;
  234. if (subdev->read_oob)
  235. err = subdev->read_oob(subdev, from, size,
  236. &retsize, buf);
  237. else
  238. err = -EINVAL;
  239. if (err)
  240. break;
  241. *retlen += retsize;
  242. len -= size;
  243. if (len == 0)
  244. break;
  245. err = -EINVAL;
  246. buf += size;
  247. from = 0;
  248. }
  249. return err;
  250. }
  251. static int
  252. concat_write_oob(struct mtd_info *mtd, loff_t to, size_t len,
  253. size_t * retlen, const u_char * buf)
  254. {
  255. struct mtd_concat *concat = CONCAT(mtd);
  256. int err = -EINVAL;
  257. int i;
  258. if (!(mtd->flags & MTD_WRITEABLE))
  259. return -EROFS;
  260. *retlen = 0;
  261. for (i = 0; i < concat->num_subdev; i++) {
  262. struct mtd_info *subdev = concat->subdev[i];
  263. size_t size, retsize;
  264. if (to >= subdev->size) {
  265. size = 0;
  266. to -= subdev->size;
  267. continue;
  268. }
  269. if (to + len > subdev->size)
  270. size = subdev->size - to;
  271. else
  272. size = len;
  273. if (!(subdev->flags & MTD_WRITEABLE))
  274. err = -EROFS;
  275. else if (subdev->write_oob)
  276. err = subdev->write_oob(subdev, to, size, &retsize,
  277. buf);
  278. else
  279. err = -EINVAL;
  280. if (err)
  281. break;
  282. *retlen += retsize;
  283. len -= size;
  284. if (len == 0)
  285. break;
  286. err = -EINVAL;
  287. buf += size;
  288. to = 0;
  289. }
  290. return err;
  291. }
  292. static void concat_erase_callback(struct erase_info *instr)
  293. {
  294. wake_up((wait_queue_head_t *) instr->priv);
  295. }
  296. static int concat_dev_erase(struct mtd_info *mtd, struct erase_info *erase)
  297. {
  298. int err;
  299. wait_queue_head_t waitq;
  300. DECLARE_WAITQUEUE(wait, current);
  301. /*
  302. * This code was stol^H^H^H^Hinspired by mtdchar.c
  303. */
  304. init_waitqueue_head(&waitq);
  305. erase->mtd = mtd;
  306. erase->callback = concat_erase_callback;
  307. erase->priv = (unsigned long) &waitq;
  308. /*
  309. * FIXME: Allow INTERRUPTIBLE. Which means
  310. * not having the wait_queue head on the stack.
  311. */
  312. err = mtd->erase(mtd, erase);
  313. if (!err) {
  314. set_current_state(TASK_UNINTERRUPTIBLE);
  315. add_wait_queue(&waitq, &wait);
  316. if (erase->state != MTD_ERASE_DONE
  317. && erase->state != MTD_ERASE_FAILED)
  318. schedule();
  319. remove_wait_queue(&waitq, &wait);
  320. set_current_state(TASK_RUNNING);
  321. err = (erase->state == MTD_ERASE_FAILED) ? -EIO : 0;
  322. }
  323. return err;
  324. }
  325. static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
  326. {
  327. struct mtd_concat *concat = CONCAT(mtd);
  328. struct mtd_info *subdev;
  329. int i, err;
  330. u_int32_t length, offset = 0;
  331. struct erase_info *erase;
  332. if (!(mtd->flags & MTD_WRITEABLE))
  333. return -EROFS;
  334. if (instr->addr > concat->mtd.size)
  335. return -EINVAL;
  336. if (instr->len + instr->addr > concat->mtd.size)
  337. return -EINVAL;
  338. /*
  339. * Check for proper erase block alignment of the to-be-erased area.
  340. * It is easier to do this based on the super device's erase
  341. * region info rather than looking at each particular sub-device
  342. * in turn.
  343. */
  344. if (!concat->mtd.numeraseregions) {
  345. /* the easy case: device has uniform erase block size */
  346. if (instr->addr & (concat->mtd.erasesize - 1))
  347. return -EINVAL;
  348. if (instr->len & (concat->mtd.erasesize - 1))
  349. return -EINVAL;
  350. } else {
  351. /* device has variable erase size */
  352. struct mtd_erase_region_info *erase_regions =
  353. concat->mtd.eraseregions;
  354. /*
  355. * Find the erase region where the to-be-erased area begins:
  356. */
  357. for (i = 0; i < concat->mtd.numeraseregions &&
  358. instr->addr >= erase_regions[i].offset; i++) ;
  359. --i;
  360. /*
  361. * Now erase_regions[i] is the region in which the
  362. * to-be-erased area begins. Verify that the starting
  363. * offset is aligned to this region's erase size:
  364. */
  365. if (instr->addr & (erase_regions[i].erasesize - 1))
  366. return -EINVAL;
  367. /*
  368. * now find the erase region where the to-be-erased area ends:
  369. */
  370. for (; i < concat->mtd.numeraseregions &&
  371. (instr->addr + instr->len) >= erase_regions[i].offset;
  372. ++i) ;
  373. --i;
  374. /*
  375. * check if the ending offset is aligned to this region's erase size
  376. */
  377. if ((instr->addr + instr->len) & (erase_regions[i].erasesize -
  378. 1))
  379. return -EINVAL;
  380. }
  381. instr->fail_addr = 0xffffffff;
  382. /* make a local copy of instr to avoid modifying the caller's struct */
  383. erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
  384. if (!erase)
  385. return -ENOMEM;
  386. *erase = *instr;
  387. length = instr->len;
  388. /*
  389. * find the subdevice where the to-be-erased area begins, adjust
  390. * starting offset to be relative to the subdevice start
  391. */
  392. for (i = 0; i < concat->num_subdev; i++) {
  393. subdev = concat->subdev[i];
  394. if (subdev->size <= erase->addr) {
  395. erase->addr -= subdev->size;
  396. offset += subdev->size;
  397. } else {
  398. break;
  399. }
  400. }
  401. /* must never happen since size limit has been verified above */
  402. BUG_ON(i >= concat->num_subdev);
  403. /* now do the erase: */
  404. err = 0;
  405. for (; length > 0; i++) {
  406. /* loop for all subdevices affected by this request */
  407. subdev = concat->subdev[i]; /* get current subdevice */
  408. /* limit length to subdevice's size: */
  409. if (erase->addr + length > subdev->size)
  410. erase->len = subdev->size - erase->addr;
  411. else
  412. erase->len = length;
  413. if (!(subdev->flags & MTD_WRITEABLE)) {
  414. err = -EROFS;
  415. break;
  416. }
  417. length -= erase->len;
  418. if ((err = concat_dev_erase(subdev, erase))) {
  419. /* sanity check: should never happen since
  420. * block alignment has been checked above */
  421. BUG_ON(err == -EINVAL);
  422. if (erase->fail_addr != 0xffffffff)
  423. instr->fail_addr = erase->fail_addr + offset;
  424. break;
  425. }
  426. /*
  427. * erase->addr specifies the offset of the area to be
  428. * erased *within the current subdevice*. It can be
  429. * non-zero only the first time through this loop, i.e.
  430. * for the first subdevice where blocks need to be erased.
  431. * All the following erases must begin at the start of the
  432. * current subdevice, i.e. at offset zero.
  433. */
  434. erase->addr = 0;
  435. offset += subdev->size;
  436. }
  437. instr->state = erase->state;
  438. kfree(erase);
  439. if (err)
  440. return err;
  441. if (instr->callback)
  442. instr->callback(instr);
  443. return 0;
  444. }
  445. static int concat_lock(struct mtd_info *mtd, loff_t ofs, size_t len)
  446. {
  447. struct mtd_concat *concat = CONCAT(mtd);
  448. int i, err = -EINVAL;
  449. if ((len + ofs) > mtd->size)
  450. return -EINVAL;
  451. for (i = 0; i < concat->num_subdev; i++) {
  452. struct mtd_info *subdev = concat->subdev[i];
  453. size_t size;
  454. if (ofs >= subdev->size) {
  455. size = 0;
  456. ofs -= subdev->size;
  457. continue;
  458. }
  459. if (ofs + len > subdev->size)
  460. size = subdev->size - ofs;
  461. else
  462. size = len;
  463. err = subdev->lock(subdev, ofs, size);
  464. if (err)
  465. break;
  466. len -= size;
  467. if (len == 0)
  468. break;
  469. err = -EINVAL;
  470. ofs = 0;
  471. }
  472. return err;
  473. }
  474. static int concat_unlock(struct mtd_info *mtd, loff_t ofs, size_t len)
  475. {
  476. struct mtd_concat *concat = CONCAT(mtd);
  477. int i, err = 0;
  478. if ((len + ofs) > mtd->size)
  479. return -EINVAL;
  480. for (i = 0; i < concat->num_subdev; i++) {
  481. struct mtd_info *subdev = concat->subdev[i];
  482. size_t size;
  483. if (ofs >= subdev->size) {
  484. size = 0;
  485. ofs -= subdev->size;
  486. continue;
  487. }
  488. if (ofs + len > subdev->size)
  489. size = subdev->size - ofs;
  490. else
  491. size = len;
  492. err = subdev->unlock(subdev, ofs, size);
  493. if (err)
  494. break;
  495. len -= size;
  496. if (len == 0)
  497. break;
  498. err = -EINVAL;
  499. ofs = 0;
  500. }
  501. return err;
  502. }
  503. static void concat_sync(struct mtd_info *mtd)
  504. {
  505. struct mtd_concat *concat = CONCAT(mtd);
  506. int i;
  507. for (i = 0; i < concat->num_subdev; i++) {
  508. struct mtd_info *subdev = concat->subdev[i];
  509. subdev->sync(subdev);
  510. }
  511. }
  512. static int concat_suspend(struct mtd_info *mtd)
  513. {
  514. struct mtd_concat *concat = CONCAT(mtd);
  515. int i, rc = 0;
  516. for (i = 0; i < concat->num_subdev; i++) {
  517. struct mtd_info *subdev = concat->subdev[i];
  518. if ((rc = subdev->suspend(subdev)) < 0)
  519. return rc;
  520. }
  521. return rc;
  522. }
  523. static void concat_resume(struct mtd_info *mtd)
  524. {
  525. struct mtd_concat *concat = CONCAT(mtd);
  526. int i;
  527. for (i = 0; i < concat->num_subdev; i++) {
  528. struct mtd_info *subdev = concat->subdev[i];
  529. subdev->resume(subdev);
  530. }
  531. }
  532. /*
  533. * This function constructs a virtual MTD device by concatenating
  534. * num_devs MTD devices. A pointer to the new device object is
  535. * stored to *new_dev upon success. This function does _not_
  536. * register any devices: this is the caller's responsibility.
  537. */
  538. struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
  539. int num_devs, /* number of subdevices */
  540. char *name)
  541. { /* name for the new device */
  542. int i;
  543. size_t size;
  544. struct mtd_concat *concat;
  545. u_int32_t max_erasesize, curr_erasesize;
  546. int num_erase_region;
  547. printk(KERN_NOTICE "Concatenating MTD devices:\n");
  548. for (i = 0; i < num_devs; i++)
  549. printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
  550. printk(KERN_NOTICE "into device \"%s\"\n", name);
  551. /* allocate the device structure */
  552. size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
  553. concat = kmalloc(size, GFP_KERNEL);
  554. if (!concat) {
  555. printk
  556. ("memory allocation error while creating concatenated device \"%s\"\n",
  557. name);
  558. return NULL;
  559. }
  560. memset(concat, 0, size);
  561. concat->subdev = (struct mtd_info **) (concat + 1);
  562. /*
  563. * Set up the new "super" device's MTD object structure, check for
  564. * incompatibilites between the subdevices.
  565. */
  566. concat->mtd.type = subdev[0]->type;
  567. concat->mtd.flags = subdev[0]->flags;
  568. concat->mtd.size = subdev[0]->size;
  569. concat->mtd.erasesize = subdev[0]->erasesize;
  570. concat->mtd.oobblock = subdev[0]->oobblock;
  571. concat->mtd.oobsize = subdev[0]->oobsize;
  572. concat->mtd.ecctype = subdev[0]->ecctype;
  573. concat->mtd.eccsize = subdev[0]->eccsize;
  574. if (subdev[0]->read_ecc)
  575. concat->mtd.read_ecc = concat_read_ecc;
  576. if (subdev[0]->write_ecc)
  577. concat->mtd.write_ecc = concat_write_ecc;
  578. if (subdev[0]->read_oob)
  579. concat->mtd.read_oob = concat_read_oob;
  580. if (subdev[0]->write_oob)
  581. concat->mtd.write_oob = concat_write_oob;
  582. concat->subdev[0] = subdev[0];
  583. for (i = 1; i < num_devs; i++) {
  584. if (concat->mtd.type != subdev[i]->type) {
  585. kfree(concat);
  586. printk("Incompatible device type on \"%s\"\n",
  587. subdev[i]->name);
  588. return NULL;
  589. }
  590. if (concat->mtd.flags != subdev[i]->flags) {
  591. /*
  592. * Expect all flags except MTD_WRITEABLE to be
  593. * equal on all subdevices.
  594. */
  595. if ((concat->mtd.flags ^ subdev[i]->
  596. flags) & ~MTD_WRITEABLE) {
  597. kfree(concat);
  598. printk("Incompatible device flags on \"%s\"\n",
  599. subdev[i]->name);
  600. return NULL;
  601. } else
  602. /* if writeable attribute differs,
  603. make super device writeable */
  604. concat->mtd.flags |=
  605. subdev[i]->flags & MTD_WRITEABLE;
  606. }
  607. concat->mtd.size += subdev[i]->size;
  608. if (concat->mtd.oobblock != subdev[i]->oobblock ||
  609. concat->mtd.oobsize != subdev[i]->oobsize ||
  610. concat->mtd.ecctype != subdev[i]->ecctype ||
  611. concat->mtd.eccsize != subdev[i]->eccsize ||
  612. !concat->mtd.read_ecc != !subdev[i]->read_ecc ||
  613. !concat->mtd.write_ecc != !subdev[i]->write_ecc ||
  614. !concat->mtd.read_oob != !subdev[i]->read_oob ||
  615. !concat->mtd.write_oob != !subdev[i]->write_oob) {
  616. kfree(concat);
  617. printk("Incompatible OOB or ECC data on \"%s\"\n",
  618. subdev[i]->name);
  619. return NULL;
  620. }
  621. concat->subdev[i] = subdev[i];
  622. }
  623. concat->num_subdev = num_devs;
  624. concat->mtd.name = name;
  625. /*
  626. * NOTE: for now, we do not provide any readv()/writev() methods
  627. * because they are messy to implement and they are not
  628. * used to a great extent anyway.
  629. */
  630. concat->mtd.erase = concat_erase;
  631. concat->mtd.read = concat_read;
  632. concat->mtd.write = concat_write;
  633. concat->mtd.sync = concat_sync;
  634. concat->mtd.lock = concat_lock;
  635. concat->mtd.unlock = concat_unlock;
  636. concat->mtd.suspend = concat_suspend;
  637. concat->mtd.resume = concat_resume;
  638. /*
  639. * Combine the erase block size info of the subdevices:
  640. *
  641. * first, walk the map of the new device and see how
  642. * many changes in erase size we have
  643. */
  644. max_erasesize = curr_erasesize = subdev[0]->erasesize;
  645. num_erase_region = 1;
  646. for (i = 0; i < num_devs; i++) {
  647. if (subdev[i]->numeraseregions == 0) {
  648. /* current subdevice has uniform erase size */
  649. if (subdev[i]->erasesize != curr_erasesize) {
  650. /* if it differs from the last subdevice's erase size, count it */
  651. ++num_erase_region;
  652. curr_erasesize = subdev[i]->erasesize;
  653. if (curr_erasesize > max_erasesize)
  654. max_erasesize = curr_erasesize;
  655. }
  656. } else {
  657. /* current subdevice has variable erase size */
  658. int j;
  659. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  660. /* walk the list of erase regions, count any changes */
  661. if (subdev[i]->eraseregions[j].erasesize !=
  662. curr_erasesize) {
  663. ++num_erase_region;
  664. curr_erasesize =
  665. subdev[i]->eraseregions[j].
  666. erasesize;
  667. if (curr_erasesize > max_erasesize)
  668. max_erasesize = curr_erasesize;
  669. }
  670. }
  671. }
  672. }
  673. if (num_erase_region == 1) {
  674. /*
  675. * All subdevices have the same uniform erase size.
  676. * This is easy:
  677. */
  678. concat->mtd.erasesize = curr_erasesize;
  679. concat->mtd.numeraseregions = 0;
  680. } else {
  681. /*
  682. * erase block size varies across the subdevices: allocate
  683. * space to store the data describing the variable erase regions
  684. */
  685. struct mtd_erase_region_info *erase_region_p;
  686. u_int32_t begin, position;
  687. concat->mtd.erasesize = max_erasesize;
  688. concat->mtd.numeraseregions = num_erase_region;
  689. concat->mtd.eraseregions = erase_region_p =
  690. kmalloc(num_erase_region *
  691. sizeof (struct mtd_erase_region_info), GFP_KERNEL);
  692. if (!erase_region_p) {
  693. kfree(concat);
  694. printk
  695. ("memory allocation error while creating erase region list"
  696. " for device \"%s\"\n", name);
  697. return NULL;
  698. }
  699. /*
  700. * walk the map of the new device once more and fill in
  701. * in erase region info:
  702. */
  703. curr_erasesize = subdev[0]->erasesize;
  704. begin = position = 0;
  705. for (i = 0; i < num_devs; i++) {
  706. if (subdev[i]->numeraseregions == 0) {
  707. /* current subdevice has uniform erase size */
  708. if (subdev[i]->erasesize != curr_erasesize) {
  709. /*
  710. * fill in an mtd_erase_region_info structure for the area
  711. * we have walked so far:
  712. */
  713. erase_region_p->offset = begin;
  714. erase_region_p->erasesize =
  715. curr_erasesize;
  716. erase_region_p->numblocks =
  717. (position - begin) / curr_erasesize;
  718. begin = position;
  719. curr_erasesize = subdev[i]->erasesize;
  720. ++erase_region_p;
  721. }
  722. position += subdev[i]->size;
  723. } else {
  724. /* current subdevice has variable erase size */
  725. int j;
  726. for (j = 0; j < subdev[i]->numeraseregions; j++) {
  727. /* walk the list of erase regions, count any changes */
  728. if (subdev[i]->eraseregions[j].
  729. erasesize != curr_erasesize) {
  730. erase_region_p->offset = begin;
  731. erase_region_p->erasesize =
  732. curr_erasesize;
  733. erase_region_p->numblocks =
  734. (position -
  735. begin) / curr_erasesize;
  736. begin = position;
  737. curr_erasesize =
  738. subdev[i]->eraseregions[j].
  739. erasesize;
  740. ++erase_region_p;
  741. }
  742. position +=
  743. subdev[i]->eraseregions[j].
  744. numblocks * curr_erasesize;
  745. }
  746. }
  747. }
  748. /* Now write the final entry */
  749. erase_region_p->offset = begin;
  750. erase_region_p->erasesize = curr_erasesize;
  751. erase_region_p->numblocks = (position - begin) / curr_erasesize;
  752. }
  753. return &concat->mtd;
  754. }
  755. /*
  756. * This function destroys an MTD object obtained from concat_mtd_devs()
  757. */
  758. void mtd_concat_destroy(struct mtd_info *mtd)
  759. {
  760. struct mtd_concat *concat = CONCAT(mtd);
  761. if (concat->mtd.numeraseregions)
  762. kfree(concat->mtd.eraseregions);
  763. kfree(concat);
  764. }
  765. EXPORT_SYMBOL(mtd_concat_create);
  766. EXPORT_SYMBOL(mtd_concat_destroy);
  767. MODULE_LICENSE("GPL");
  768. MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
  769. MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");