fireworks_transaction.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. t = (struct snd_efw_transaction *)data;
  107. length = min_t(size_t, be32_to_cpu(t->length) * sizeof(u32), length);
  108. spin_lock_irq(&efw->lock);
  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. wake_up(&efw->hwdep_wait);
  133. *rcode = RCODE_COMPLETE;
  134. end:
  135. spin_unlock_irq(&efw->lock);
  136. }
  137. static void
  138. handle_resp_for_user(struct fw_card *card, int generation, int source,
  139. void *data, size_t length, int *rcode)
  140. {
  141. struct fw_device *device;
  142. struct snd_efw *efw;
  143. unsigned int i;
  144. spin_lock_irq(&instances_lock);
  145. for (i = 0; i < SNDRV_CARDS; i++) {
  146. efw = instances[i];
  147. if (efw == NULL)
  148. continue;
  149. device = fw_parent_device(efw->unit);
  150. if ((device->card != card) ||
  151. (device->generation != generation))
  152. continue;
  153. smp_rmb(); /* node id vs. generation */
  154. if (device->node_id != source)
  155. continue;
  156. break;
  157. }
  158. if (i == SNDRV_CARDS)
  159. goto end;
  160. copy_resp_to_buf(efw, data, length, rcode);
  161. end:
  162. spin_unlock_irq(&instances_lock);
  163. }
  164. static void
  165. handle_resp_for_kernel(struct fw_card *card, int generation, int source,
  166. void *data, size_t length, int *rcode, u32 seqnum)
  167. {
  168. struct fw_device *device;
  169. struct transaction_queue *t;
  170. unsigned long flags;
  171. spin_lock_irqsave(&transaction_queues_lock, flags);
  172. list_for_each_entry(t, &transaction_queues, list) {
  173. device = fw_parent_device(t->unit);
  174. if ((device->card != card) ||
  175. (device->generation != generation))
  176. continue;
  177. smp_rmb(); /* node_id vs. generation */
  178. if (device->node_id != source)
  179. continue;
  180. if ((t->state == STATE_PENDING) && (t->seqnum == seqnum)) {
  181. t->state = STATE_COMPLETE;
  182. t->size = min_t(unsigned int, length, t->size);
  183. memcpy(t->buf, data, t->size);
  184. wake_up(&t->wait);
  185. *rcode = RCODE_COMPLETE;
  186. }
  187. }
  188. spin_unlock_irqrestore(&transaction_queues_lock, flags);
  189. }
  190. static void
  191. efw_response(struct fw_card *card, struct fw_request *request,
  192. int tcode, int destination, int source,
  193. int generation, unsigned long long offset,
  194. void *data, size_t length, void *callback_data)
  195. {
  196. int rcode, dummy;
  197. u32 seqnum;
  198. rcode = RCODE_TYPE_ERROR;
  199. if (length < sizeof(struct snd_efw_transaction)) {
  200. rcode = RCODE_DATA_ERROR;
  201. goto end;
  202. } else if (offset != MEMORY_SPACE_EFW_RESPONSE) {
  203. rcode = RCODE_ADDRESS_ERROR;
  204. goto end;
  205. }
  206. seqnum = be32_to_cpu(((struct snd_efw_transaction *)data)->seqnum);
  207. if (seqnum > SND_EFW_TRANSACTION_USER_SEQNUM_MAX + 1) {
  208. handle_resp_for_kernel(card, generation, source,
  209. data, length, &rcode, seqnum);
  210. if (snd_efw_resp_buf_debug)
  211. handle_resp_for_user(card, generation, source,
  212. data, length, &dummy);
  213. } else {
  214. handle_resp_for_user(card, generation, source,
  215. data, length, &rcode);
  216. }
  217. end:
  218. fw_send_response(card, request, rcode);
  219. }
  220. void snd_efw_transaction_add_instance(struct snd_efw *efw)
  221. {
  222. unsigned int i;
  223. spin_lock_irq(&instances_lock);
  224. for (i = 0; i < SNDRV_CARDS; i++) {
  225. if (instances[i] != NULL)
  226. continue;
  227. instances[i] = efw;
  228. break;
  229. }
  230. spin_unlock_irq(&instances_lock);
  231. }
  232. void snd_efw_transaction_remove_instance(struct snd_efw *efw)
  233. {
  234. unsigned int i;
  235. spin_lock_irq(&instances_lock);
  236. for (i = 0; i < SNDRV_CARDS; i++) {
  237. if (instances[i] != efw)
  238. continue;
  239. instances[i] = NULL;
  240. }
  241. spin_unlock_irq(&instances_lock);
  242. }
  243. void snd_efw_transaction_bus_reset(struct fw_unit *unit)
  244. {
  245. struct transaction_queue *t;
  246. spin_lock_irq(&transaction_queues_lock);
  247. list_for_each_entry(t, &transaction_queues, list) {
  248. if ((t->unit == unit) &&
  249. (t->state == STATE_PENDING)) {
  250. t->state = STATE_BUS_RESET;
  251. wake_up(&t->wait);
  252. }
  253. }
  254. spin_unlock_irq(&transaction_queues_lock);
  255. }
  256. static struct fw_address_handler resp_register_handler = {
  257. .length = SND_EFW_RESPONSE_MAXIMUM_BYTES,
  258. .address_callback = efw_response
  259. };
  260. int snd_efw_transaction_register(void)
  261. {
  262. static const struct fw_address_region resp_register_region = {
  263. .start = MEMORY_SPACE_EFW_RESPONSE,
  264. .end = MEMORY_SPACE_EFW_RESPONSE +
  265. SND_EFW_RESPONSE_MAXIMUM_BYTES
  266. };
  267. return fw_core_add_address_handler(&resp_register_handler,
  268. &resp_register_region);
  269. }
  270. void snd_efw_transaction_unregister(void)
  271. {
  272. WARN_ON(!list_empty(&transaction_queues));
  273. fw_core_remove_address_handler(&resp_register_handler);
  274. }