mtdconcat.c 21 KB

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