sigmadsp.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Load Analog Devices SigmaStudio firmware files
  3. *
  4. * Copyright 2009-2011 Analog Devices Inc.
  5. *
  6. * Licensed under the GPL-2 or later.
  7. */
  8. #include <linux/crc32.h>
  9. #include <linux/delay.h>
  10. #include <linux/firmware.h>
  11. #include <linux/kernel.h>
  12. #include <linux/i2c.h>
  13. #include <linux/module.h>
  14. #include "sigmadsp.h"
  15. #define SIGMA_MAGIC "ADISIGM"
  16. struct sigma_firmware_header {
  17. unsigned char magic[7];
  18. u8 version;
  19. __le32 crc;
  20. } __packed;
  21. enum {
  22. SIGMA_ACTION_WRITEXBYTES = 0,
  23. SIGMA_ACTION_WRITESINGLE,
  24. SIGMA_ACTION_WRITESAFELOAD,
  25. SIGMA_ACTION_DELAY,
  26. SIGMA_ACTION_PLLWAIT,
  27. SIGMA_ACTION_NOOP,
  28. SIGMA_ACTION_END,
  29. };
  30. struct sigma_action {
  31. u8 instr;
  32. u8 len_hi;
  33. __le16 len;
  34. __be16 addr;
  35. unsigned char payload[];
  36. } __packed;
  37. struct sigma_firmware {
  38. const struct firmware *fw;
  39. size_t pos;
  40. };
  41. static inline u32 sigma_action_len(struct sigma_action *sa)
  42. {
  43. return (sa->len_hi << 16) | le16_to_cpu(sa->len);
  44. }
  45. static size_t sigma_action_size(struct sigma_action *sa)
  46. {
  47. size_t payload = 0;
  48. switch (sa->instr) {
  49. case SIGMA_ACTION_WRITEXBYTES:
  50. case SIGMA_ACTION_WRITESINGLE:
  51. case SIGMA_ACTION_WRITESAFELOAD:
  52. payload = sigma_action_len(sa);
  53. break;
  54. default:
  55. break;
  56. }
  57. payload = ALIGN(payload, 2);
  58. return payload + sizeof(struct sigma_action);
  59. }
  60. /*
  61. * Returns a negative error value in case of an error, 0 if processing of
  62. * the firmware should be stopped after this action, 1 otherwise.
  63. */
  64. static int
  65. process_sigma_action(struct i2c_client *client, struct sigma_action *sa)
  66. {
  67. size_t len = sigma_action_len(sa);
  68. int ret;
  69. pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
  70. sa->instr, sa->addr, len);
  71. switch (sa->instr) {
  72. case SIGMA_ACTION_WRITEXBYTES:
  73. case SIGMA_ACTION_WRITESINGLE:
  74. case SIGMA_ACTION_WRITESAFELOAD:
  75. ret = i2c_master_send(client, (void *)&sa->addr, len);
  76. if (ret < 0)
  77. return -EINVAL;
  78. break;
  79. case SIGMA_ACTION_DELAY:
  80. udelay(len);
  81. len = 0;
  82. break;
  83. case SIGMA_ACTION_END:
  84. return 0;
  85. default:
  86. return -EINVAL;
  87. }
  88. return 1;
  89. }
  90. static int
  91. process_sigma_actions(struct i2c_client *client, struct sigma_firmware *ssfw)
  92. {
  93. struct sigma_action *sa;
  94. size_t size;
  95. int ret;
  96. while (ssfw->pos + sizeof(*sa) <= ssfw->fw->size) {
  97. sa = (struct sigma_action *)(ssfw->fw->data + ssfw->pos);
  98. size = sigma_action_size(sa);
  99. ssfw->pos += size;
  100. if (ssfw->pos > ssfw->fw->size || size == 0)
  101. break;
  102. ret = process_sigma_action(client, sa);
  103. pr_debug("%s: action returned %i\n", __func__, ret);
  104. if (ret <= 0)
  105. return ret;
  106. }
  107. if (ssfw->pos != ssfw->fw->size)
  108. return -EINVAL;
  109. return 0;
  110. }
  111. int process_sigma_firmware(struct i2c_client *client, const char *name)
  112. {
  113. int ret;
  114. struct sigma_firmware_header *ssfw_head;
  115. struct sigma_firmware ssfw;
  116. const struct firmware *fw;
  117. u32 crc;
  118. pr_debug("%s: loading firmware %s\n", __func__, name);
  119. /* first load the blob */
  120. ret = request_firmware(&fw, name, &client->dev);
  121. if (ret) {
  122. pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
  123. return ret;
  124. }
  125. ssfw.fw = fw;
  126. /* then verify the header */
  127. ret = -EINVAL;
  128. /*
  129. * Reject too small or unreasonable large files. The upper limit has been
  130. * chosen a bit arbitrarily, but it should be enough for all practical
  131. * purposes and having the limit makes it easier to avoid integer
  132. * overflows later in the loading process.
  133. */
  134. if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000) {
  135. dev_err(&client->dev, "Failed to load firmware: Invalid size\n");
  136. goto done;
  137. }
  138. ssfw_head = (void *)fw->data;
  139. if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic))) {
  140. dev_err(&client->dev, "Failed to load firmware: Invalid magic\n");
  141. goto done;
  142. }
  143. crc = crc32(0, fw->data + sizeof(*ssfw_head),
  144. fw->size - sizeof(*ssfw_head));
  145. pr_debug("%s: crc=%x\n", __func__, crc);
  146. if (crc != le32_to_cpu(ssfw_head->crc)) {
  147. dev_err(&client->dev, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
  148. le32_to_cpu(ssfw_head->crc), crc);
  149. goto done;
  150. }
  151. ssfw.pos = sizeof(*ssfw_head);
  152. /* finally process all of the actions */
  153. ret = process_sigma_actions(client, &ssfw);
  154. done:
  155. release_firmware(fw);
  156. pr_debug("%s: loaded %s\n", __func__, name);
  157. return ret;
  158. }
  159. EXPORT_SYMBOL(process_sigma_firmware);
  160. MODULE_LICENSE("GPL");