0003-sf-move-malloc-of-spi_flash-to-spi_flash_probe.patch 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. From 36b7400465fe2339f1c78274b3fd258ade3a4c00 Mon Sep 17 00:00:00 2001
  2. From: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  3. Date: Sat, 12 Oct 2013 21:30:07 +0200
  4. Subject: sf: move malloc of spi_flash to spi_flash_probe()
  5. Signed-off-by: Daniel Schwierzeck <daniel.schwierzeck@gmail.com>
  6. --- a/drivers/mtd/spi/sf_probe.c
  7. +++ b/drivers/mtd/spi/sf_probe.c
  8. @@ -153,11 +153,10 @@ static const struct spi_flash_params spi
  9. */
  10. };
  11. -static struct spi_flash *spi_flash_validate_params(struct spi_slave *spi,
  12. +static int spi_flash_validate_params(struct spi_flash *flash,
  13. u8 *idcode)
  14. {
  15. const struct spi_flash_params *params;
  16. - struct spi_flash *flash;
  17. int i;
  18. u16 jedec = idcode[1] << 8 | idcode[2];
  19. u16 ext_jedec = idcode[3] << 8 | idcode[4];
  20. @@ -179,20 +178,12 @@ static struct spi_flash *spi_flash_valid
  21. debug("SF: Unsupported flash IDs: ");
  22. debug("manuf %02x, jedec %04x, ext_jedec %04x\n",
  23. idcode[0], jedec, ext_jedec);
  24. - return NULL;
  25. - }
  26. -
  27. - flash = malloc(sizeof(*flash));
  28. - if (!flash) {
  29. - debug("SF: Failed to allocate spi_flash\n");
  30. - return NULL;
  31. + return -1;
  32. }
  33. - memset(flash, '\0', sizeof(*flash));
  34. /* Assign spi data */
  35. - flash->spi = spi;
  36. flash->name = params->name;
  37. - flash->memory_map = spi->memory_map;
  38. + flash->memory_map = flash->spi->memory_map;
  39. /* Assign spi_flash ops */
  40. flash->write = spi_flash_cmd_write_ops;
  41. @@ -239,7 +230,7 @@ static struct spi_flash *spi_flash_valid
  42. if (spi_flash_read_common(flash, &flash->bank_read_cmd, 1,
  43. &curr_bank, 1)) {
  44. debug("SF: fail to read bank addr register\n");
  45. - return NULL;
  46. + return -1;
  47. }
  48. flash->bank_curr = curr_bank;
  49. } else {
  50. @@ -254,7 +245,7 @@ static struct spi_flash *spi_flash_valid
  51. spi_flash_cmd_write_status(flash, 0);
  52. #endif
  53. - return flash;
  54. + return 0;
  55. }
  56. #ifdef CONFIG_OF_CONTROL
  57. @@ -289,15 +280,22 @@ struct spi_flash *spi_flash_probe(unsign
  58. unsigned int max_hz, unsigned int spi_mode)
  59. {
  60. struct spi_slave *spi;
  61. - struct spi_flash *flash = NULL;
  62. + struct spi_flash *flash;
  63. u8 idcode[5];
  64. int ret;
  65. + flash = malloc(sizeof(*flash));
  66. + if (!flash) {
  67. + debug("SF: Failed to allocate spi_flash\n");
  68. + return NULL;
  69. + }
  70. + memset(flash, 0, sizeof(*flash));
  71. +
  72. /* Setup spi_slave */
  73. spi = spi_setup_slave(bus, cs, max_hz, spi_mode);
  74. if (!spi) {
  75. debug("SF: Failed to set up slave\n");
  76. - return NULL;
  77. + goto err_setup;
  78. }
  79. /* Claim spi bus */
  80. @@ -320,8 +318,9 @@ struct spi_flash *spi_flash_probe(unsign
  81. #endif
  82. /* Validate params from spi_flash_params table */
  83. - flash = spi_flash_validate_params(spi, idcode);
  84. - if (!flash)
  85. + flash->spi = spi;
  86. + ret = spi_flash_validate_params(flash, idcode);
  87. + if (ret)
  88. goto err_read_id;
  89. #ifdef CONFIG_OF_CONTROL
  90. @@ -355,6 +354,9 @@ err_read_id:
  91. spi_release_bus(spi);
  92. err_claim_bus:
  93. spi_free_slave(spi);
  94. +err_setup:
  95. + free(flash);
  96. +
  97. return NULL;
  98. }