fireworks_transaction.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fireworks_transaction.c - a part of driver for Fireworks based devices
  4. *
  5. * Copyright (c) 2013-2014 Takashi Sakamoto
  6. */
  7. /*
  8. * Fireworks have its own transaction. The transaction can be delivered by AV/C
  9. * Vendor Specific command frame or usual asynchronous transaction. At least,
  10. * Windows driver and firmware version 5.5 or later don't use AV/C command.
  11. *
  12. * Transaction substance:
  13. * At first, 6 data exist. Following to the data, parameters for each command
  14. * exist. All of the parameters are 32 bit aligned to big endian.
  15. * data[0]: Length of transaction substance
  16. * data[1]: Transaction version
  17. * data[2]: Sequence number. This is incremented by the device
  18. * data[3]: Transaction category
  19. * data[4]: Transaction command
  20. * data[5]: Return value in response.
  21. * data[6-]: Parameters
  22. *
  23. * Transaction address:
  24. * command: 0xecc000000000
  25. * response: 0xecc080000000 (default)
  26. *
  27. * I note that the address for response can be changed by command. But this
  28. * module uses the default address.
  29. */
  30. #include "./fireworks.h"
  31. #define MEMORY_SPACE_EFW_COMMAND 0xecc000000000ULL
  32. #define MEMORY_SPACE_EFW_RESPONSE 0xecc080000000ULL
  33. #define ERROR_RETRIES 3
  34. #define ERROR_DELAY_MS 5
  35. #define EFC_TIMEOUT_MS 125
  36. static DEFINE_SPINLOCK(instances_lock);
  37. static struct snd_efw *instances[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
  38. static DEFINE_SPINLOCK(transaction_queues_lock);
  39. static LIST_HEAD(transaction_queues);
  40. enum transaction_queue_state {
  41. STATE_PENDING,
  42. STATE_BUS_RESET,
  43. STATE_COMPLETE
  44. };
  45. struct transaction_queue {
  46. struct list_head list;
  47. struct fw_unit *unit;
  48. void *buf;
  49. unsigned int size;
  50. u32 seqnum;
  51. enum transaction_queue_state state;
  52. wait_queue_head_t wait;
  53. };
  54. int snd_efw_transaction_cmd(struct fw_unit *unit,
  55. const void *cmd, unsigned int size)
  56. {
  57. return snd_fw_transaction(unit, TCODE_WRITE_BLOCK_REQUEST,
  58. MEMORY_SPACE_EFW_COMMAND,
  59. (void *)cmd, size, 0);
  60. }
  61. int snd_efw_transaction_run(struct fw_unit *unit,
  62. const void *cmd, unsigned int cmd_size,
  63. void *resp, unsigned int resp_size)
  64. {
  65. struct transaction_queue t;
  66. unsigned int tries;
  67. int ret;
  68. t.unit = unit;
  69. t.buf = resp;
  70. t.size = resp_size;
  71. t.seqnum = be32_to_cpu(((struct snd_efw_transaction *)cmd)->seqnum) + 1;
  72. t.state = STATE_PENDING;
  73. init_waitqueue_head(&t.wait);
  74. spin_lock_irq(&transaction_queues_lock);
  75. list_add_tail(&t.list, &transaction_queues);
  76. spin_unlock_irq(&transaction_queues_lock);
  77. tries = 0;
  78. do {
  79. ret = snd_efw_transaction_cmd(t.unit, (void *)cmd, cmd_size);
  80. if (ret < 0)
  81. break;
  82. wait_event_timeout(t.wait, t.state != STATE_PENDING,
  83. msecs_to_jiffies(EFC_TIMEOUT_MS));
  84. if (t.state == STATE_COMPLETE) {
  85. ret = t.size;
  86. break;
  87. } else if (t.state == STATE_BUS_RESET) {
  88. msleep(ERROR_DELAY_MS);
  89. } else if (++tries >= ERROR_RETRIES) {
  90. dev_err(&t.unit->device, "EFW transaction timed out\n");
  91. ret = -EIO;
  92. break;
  93. }
  94. } while (1);
  95. spin_lock_irq(&transaction_queues_lock);
  96. list_del(&t.list);
  97. spin_unlock_irq(&transaction_queues_lock);
  98. return ret;
  99. }
  100. static void
  101. copy_resp_to_buf(struct snd_efw *efw, void *data, size_t length, int *rcode)
  102. {
  103. size_t capacity, till_end;
  104. struct snd_efw_transaction *t;
  105. t = (struct snd_efw_transaction *)data;
  106. length = min_t(size_t, be32_to_cpu(t->length) * sizeof(u32), length);
  107. spin_lock(&efw->lock);
  108. if (efw->push_ptr < efw->pull_ptr)
  109. capacity = (unsigned int)(efw->pull_ptr - efw->push_ptr);
  110. else
  111. capacity = snd_efw_resp_buf_size -
  112. (unsigned int)(efw->push_ptr - efw->pull_ptr);
  113. /* confirm enough space for this response */
  114. if (capacity < length) {
  115. *rcode = RCODE_CONFLICT_ERROR;
  116. goto end;
  117. }
  118. /* copy to ring buffer */
  119. while (length > 0) {
  120. till_end = snd_efw_resp_buf_size -
  121. (unsigned int)(efw->push_ptr - efw->resp_buf);
  122. till_end = min_t(unsigned int, length, till_end);
  123. memcpy(efw->push_ptr, data, till_end);
  124. efw->push_ptr += till_end;
  125. if (efw->push_ptr >= efw->resp_buf + snd_efw_resp_buf_size)
  126. efw->push_ptr -= snd_efw_resp_buf_size;
  127. length -= till_end;
  128. data += till_end;
  129. }
  130. /* for hwdep */
  131. wake_up(&efw->hwdep_wait);
  132. *rcode = RCODE_COMPLETE;
  133. end:
  134. spin_unlock_irq(&efw->lock);
  135. }
  136. static void
  137. handle_resp_for_user(struct fw_card *card, int generation, int source,
  138. void *data, size_t length, int *rcode)
  139. {
  140. struct fw_device *device;
  141. struct snd_efw *efw;
  142. unsigned int i;
  143. spin_lock_irq(&instances_lock);
  144. for (i = 0; i < SNDRV_CARDS; i++) {
  145. efw = instances[i];
  146. if (efw == NULL)
  147. continue;
  148. device = fw_parent_device(efw->unit);
  149. if ((device->card != card) ||
  150. (device->generation != generation))
  151. continue;
  152. smp_rmb(); /* node id vs. generation */
  153. if (device->node_id != source)
  154. continue;
  155. break;
  156. }
  157. if (i == SNDRV_CARDS)
  158. goto end;
  159. copy_resp_to_buf(efw, data, length, rcode);
  160. end:
  161. spin_unlock(&instances_lock);
  162. }
  163. static void
  164. handle_resp_for_kernel(struct fw_card *card, int generation, int source,
  165. void *data, size_t length, int *rcode, u32 seqnum)
  166. {
  167. struct fw_device *device;
  168. struct transaction_queue *t;
  169. unsigned long flags;
  170. spin_lock_irqsave(&transaction_queues_lock, flags);
  171. list_for_each_entry(t, &transaction_queues, list) {
  172. device = fw_parent_device(t->unit);
  173. if ((device->card != card) ||
  174. (device->generation != generation))
  175. continue;
  176. smp_rmb(); /* node_id vs. generation */
  177. if (device->node_id != source)
  178. continue;
  179. if ((t->state == STATE_PENDING) && (t->seqnum == seqnum)) {
  180. t->state = STATE_COMPLETE;
  181. t->size = min_t(unsigned int, length, t->size);
  182. memcpy(t->buf, data, t->size);
  183. wake_up(&t->wait);
  184. *rcode = RCODE_COMPLETE;
  185. }
  186. }
  187. spin_unlock_irqrestore(&transaction_queues_lock, flags);
  188. }
  189. static void
  190. efw_response(struct fw_card *card, struct fw_request *request,
  191. int tcode, int destination, int source,
  192. int generation, unsigned long long offset,
  193. void *data, size_t length, void *callback_data)
  194. {
  195. int rcode, dummy;
  196. u32 seqnum;
  197. rcode = RCODE_TYPE_ERROR;
  198. if (length < sizeof(struct snd_efw_transaction)) {
  199. rcode = RCODE_DATA_ERROR;
  200. goto end;
  201. } else if (offset != MEMORY_SPACE_EFW_RESPONSE) {
  202. rcode = RCODE_ADDRESS_ERROR;
  203. goto end;
  204. }
  205. seqnum = be32_to_cpu(((struct snd_efw_transaction *)data)->seqnum);
  206. if (seqnum > SND_EFW_TRANSACTION_USER_SEQNUM_MAX + 1) {
  207. handle_resp_for_kernel(card, generation, source,
  208. data, length, &rcode, seqnum);
  209. if (snd_efw_resp_buf_debug)
  210. handle_resp_for_user(card, generation, source,
  211. data, length, &dummy);
  212. } else {
  213. handle_resp_for_user(card, generation, source,
  214. data, length, &rcode);
  215. }
  216. end:
  217. fw_send_response(card, request, rcode);
  218. }
  219. void snd_efw_transaction_add_instance(struct snd_efw *efw)
  220. {
  221. unsigned int i;
  222. spin_lock_irq(&instances_lock);
  223. for (i = 0; i < SNDRV_CARDS; i++) {
  224. if (instances[i] != NULL)
  225. continue;
  226. instances[i] = efw;
  227. break;
  228. }
  229. spin_unlock_irq(&instances_lock);
  230. }
  231. void snd_efw_transaction_remove_instance(struct snd_efw *efw)
  232. {
  233. unsigned int i;
  234. spin_lock_irq(&instances_lock);
  235. for (i = 0; i < SNDRV_CARDS; i++) {
  236. if (instances[i] != efw)
  237. continue;
  238. instances[i] = NULL;
  239. }
  240. spin_unlock_irq(&instances_lock);
  241. }
  242. void snd_efw_transaction_bus_reset(struct fw_unit *unit)
  243. {
  244. struct transaction_queue *t;
  245. spin_lock_irq(&transaction_queues_lock);
  246. list_for_each_entry(t, &transaction_queues, list) {
  247. if ((t->unit == unit) &&
  248. (t->state == STATE_PENDING)) {
  249. t->state = STATE_BUS_RESET;
  250. wake_up(&t->wait);
  251. }
  252. }
  253. spin_unlock_irq(&transaction_queues_lock);
  254. }
  255. static struct fw_address_handler resp_register_handler = {
  256. .length = SND_EFW_RESPONSE_MAXIMUM_BYTES,
  257. .address_callback = efw_response
  258. };
  259. int snd_efw_transaction_register(void)
  260. {
  261. static const struct fw_address_region resp_register_region = {
  262. .start = MEMORY_SPACE_EFW_RESPONSE,
  263. .end = MEMORY_SPACE_EFW_RESPONSE +
  264. SND_EFW_RESPONSE_MAXIMUM_BYTES
  265. };
  266. return fw_core_add_address_handler(&resp_register_handler,
  267. &resp_register_region);
  268. }
  269. void snd_efw_transaction_unregister(void)
  270. {
  271. WARN_ON(!list_empty(&transaction_queues));
  272. fw_core_remove_address_handler(&resp_register_handler);
  273. }