fireworks_transaction.c 8.0 KB

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