fcp.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Function Control Protocol (IEC 61883-1) helper functions
  3. *
  4. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  5. * Licensed under the terms of the GNU General Public License, version 2.
  6. */
  7. #include <linux/device.h>
  8. #include <linux/firewire.h>
  9. #include <linux/firewire-constants.h>
  10. #include <linux/list.h>
  11. #include <linux/module.h>
  12. #include <linux/sched.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/wait.h>
  15. #include <linux/delay.h>
  16. #include "fcp.h"
  17. #include "lib.h"
  18. #define CTS_AVC 0x00
  19. #define ERROR_RETRIES 3
  20. #define ERROR_DELAY_MS 5
  21. #define FCP_TIMEOUT_MS 125
  22. static DEFINE_SPINLOCK(transactions_lock);
  23. static LIST_HEAD(transactions);
  24. enum fcp_state {
  25. STATE_PENDING,
  26. STATE_BUS_RESET,
  27. STATE_COMPLETE,
  28. };
  29. struct fcp_transaction {
  30. struct list_head list;
  31. struct fw_unit *unit;
  32. void *response_buffer;
  33. unsigned int response_size;
  34. unsigned int response_match_bytes;
  35. enum fcp_state state;
  36. wait_queue_head_t wait;
  37. };
  38. /**
  39. * fcp_avc_transaction - send an AV/C command and wait for its response
  40. * @unit: a unit on the target device
  41. * @command: a buffer containing the command frame; must be DMA-able
  42. * @command_size: the size of @command
  43. * @response: a buffer for the response frame
  44. * @response_size: the maximum size of @response
  45. * @response_match_bytes: a bitmap specifying the bytes used to detect the
  46. * correct response frame
  47. *
  48. * This function sends a FCP command frame to the target and waits for the
  49. * corresponding response frame to be returned.
  50. *
  51. * Because it is possible for multiple FCP transactions to be active at the
  52. * same time, the correct response frame is detected by the value of certain
  53. * bytes. These bytes must be set in @response before calling this function,
  54. * and the corresponding bits must be set in @response_match_bytes.
  55. *
  56. * @command and @response can point to the same buffer.
  57. *
  58. * Asynchronous operation (INTERIM, NOTIFY) is not supported at the moment.
  59. *
  60. * Returns the actual size of the response frame, or a negative error code.
  61. */
  62. int fcp_avc_transaction(struct fw_unit *unit,
  63. const void *command, unsigned int command_size,
  64. void *response, unsigned int response_size,
  65. unsigned int response_match_bytes)
  66. {
  67. struct fcp_transaction t;
  68. int tcode, ret, tries = 0;
  69. t.unit = unit;
  70. t.response_buffer = response;
  71. t.response_size = response_size;
  72. t.response_match_bytes = response_match_bytes;
  73. t.state = STATE_PENDING;
  74. init_waitqueue_head(&t.wait);
  75. spin_lock_irq(&transactions_lock);
  76. list_add_tail(&t.list, &transactions);
  77. spin_unlock_irq(&transactions_lock);
  78. for (;;) {
  79. tcode = command_size == 4 ? TCODE_WRITE_QUADLET_REQUEST
  80. : TCODE_WRITE_BLOCK_REQUEST;
  81. ret = snd_fw_transaction(t.unit, tcode,
  82. CSR_REGISTER_BASE + CSR_FCP_COMMAND,
  83. (void *)command, command_size);
  84. if (ret < 0)
  85. break;
  86. wait_event_timeout(t.wait, t.state != STATE_PENDING,
  87. msecs_to_jiffies(FCP_TIMEOUT_MS));
  88. if (t.state == STATE_COMPLETE) {
  89. ret = t.response_size;
  90. break;
  91. } else if (t.state == STATE_BUS_RESET) {
  92. msleep(ERROR_DELAY_MS);
  93. } else if (++tries >= ERROR_RETRIES) {
  94. dev_err(&t.unit->device, "FCP command timed out\n");
  95. ret = -EIO;
  96. break;
  97. }
  98. }
  99. spin_lock_irq(&transactions_lock);
  100. list_del(&t.list);
  101. spin_unlock_irq(&transactions_lock);
  102. return ret;
  103. }
  104. EXPORT_SYMBOL(fcp_avc_transaction);
  105. /**
  106. * fcp_bus_reset - inform the target handler about a bus reset
  107. * @unit: the unit that might be used by fcp_avc_transaction()
  108. *
  109. * This function must be called from the driver's .update handler to inform
  110. * the FCP transaction handler that a bus reset has happened. Any pending FCP
  111. * transactions are retried.
  112. */
  113. void fcp_bus_reset(struct fw_unit *unit)
  114. {
  115. struct fcp_transaction *t;
  116. spin_lock_irq(&transactions_lock);
  117. list_for_each_entry(t, &transactions, list) {
  118. if (t->unit == unit &&
  119. t->state == STATE_PENDING) {
  120. t->state = STATE_BUS_RESET;
  121. wake_up(&t->wait);
  122. }
  123. }
  124. spin_unlock_irq(&transactions_lock);
  125. }
  126. EXPORT_SYMBOL(fcp_bus_reset);
  127. /* checks whether the response matches the masked bytes in response_buffer */
  128. static bool is_matching_response(struct fcp_transaction *transaction,
  129. const void *response, size_t length)
  130. {
  131. const u8 *p1, *p2;
  132. unsigned int mask, i;
  133. p1 = response;
  134. p2 = transaction->response_buffer;
  135. mask = transaction->response_match_bytes;
  136. for (i = 0; ; ++i) {
  137. if ((mask & 1) && p1[i] != p2[i])
  138. return false;
  139. mask >>= 1;
  140. if (!mask)
  141. return true;
  142. if (--length == 0)
  143. return false;
  144. }
  145. }
  146. static void fcp_response(struct fw_card *card, struct fw_request *request,
  147. int tcode, int destination, int source,
  148. int generation, unsigned long long offset,
  149. void *data, size_t length, void *callback_data)
  150. {
  151. struct fcp_transaction *t;
  152. unsigned long flags;
  153. if (length < 1 || (*(const u8 *)data & 0xf0) != CTS_AVC)
  154. return;
  155. spin_lock_irqsave(&transactions_lock, flags);
  156. list_for_each_entry(t, &transactions, list) {
  157. struct fw_device *device = fw_parent_device(t->unit);
  158. if (device->card != card ||
  159. device->generation != generation)
  160. continue;
  161. smp_rmb(); /* node_id vs. generation */
  162. if (device->node_id != source)
  163. continue;
  164. if (t->state == STATE_PENDING &&
  165. is_matching_response(t, data, length)) {
  166. t->state = STATE_COMPLETE;
  167. t->response_size = min((unsigned int)length,
  168. t->response_size);
  169. memcpy(t->response_buffer, data, t->response_size);
  170. wake_up(&t->wait);
  171. }
  172. }
  173. spin_unlock_irqrestore(&transactions_lock, flags);
  174. }
  175. static struct fw_address_handler response_register_handler = {
  176. .length = 0x200,
  177. .address_callback = fcp_response,
  178. };
  179. static int __init fcp_module_init(void)
  180. {
  181. static const struct fw_address_region response_register_region = {
  182. .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
  183. .end = CSR_REGISTER_BASE + CSR_FCP_END,
  184. };
  185. fw_core_add_address_handler(&response_register_handler,
  186. &response_register_region);
  187. return 0;
  188. }
  189. static void __exit fcp_module_exit(void)
  190. {
  191. WARN_ON(!list_empty(&transactions));
  192. fw_core_remove_address_handler(&response_register_handler);
  193. }
  194. module_init(fcp_module_init);
  195. module_exit(fcp_module_exit);