fcp.c 6.0 KB

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