fcp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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/slab.h>
  13. #include <linux/sched.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/wait.h>
  16. #include <linux/delay.h>
  17. #include "fcp.h"
  18. #include "lib.h"
  19. #include "amdtp-stream.h"
  20. #define CTS_AVC 0x00
  21. #define ERROR_RETRIES 3
  22. #define ERROR_DELAY_MS 5
  23. #define FCP_TIMEOUT_MS 125
  24. int avc_general_set_sig_fmt(struct fw_unit *unit, unsigned int rate,
  25. enum avc_general_plug_dir dir,
  26. unsigned short pid)
  27. {
  28. unsigned int sfc;
  29. u8 *buf;
  30. bool flag;
  31. int err;
  32. flag = false;
  33. for (sfc = 0; sfc < CIP_SFC_COUNT; sfc++) {
  34. if (amdtp_rate_table[sfc] == rate) {
  35. flag = true;
  36. break;
  37. }
  38. }
  39. if (!flag)
  40. return -EINVAL;
  41. buf = kzalloc(8, GFP_KERNEL);
  42. if (buf == NULL)
  43. return -ENOMEM;
  44. buf[0] = 0x00; /* AV/C CONTROL */
  45. buf[1] = 0xff; /* UNIT */
  46. if (dir == AVC_GENERAL_PLUG_DIR_IN)
  47. buf[2] = 0x19; /* INPUT PLUG SIGNAL FORMAT */
  48. else
  49. buf[2] = 0x18; /* OUTPUT PLUG SIGNAL FORMAT */
  50. buf[3] = 0xff & pid; /* plug id */
  51. buf[4] = 0x90; /* EOH_1, Form_1, FMT. AM824 */
  52. buf[5] = 0x07 & sfc; /* FDF-hi. AM824, frequency */
  53. buf[6] = 0xff; /* FDF-mid. AM824, SYT hi (not used)*/
  54. buf[7] = 0xff; /* FDF-low. AM824, SYT lo (not used) */
  55. /* do transaction and check buf[1-5] are the same against command */
  56. err = fcp_avc_transaction(unit, buf, 8, buf, 8,
  57. BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5));
  58. if (err < 0)
  59. ;
  60. else if (err < 8)
  61. err = -EIO;
  62. else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
  63. err = -ENOSYS;
  64. else if (buf[0] == 0x0a) /* REJECTED */
  65. err = -EINVAL;
  66. if (err < 0)
  67. goto end;
  68. err = 0;
  69. end:
  70. kfree(buf);
  71. return err;
  72. }
  73. EXPORT_SYMBOL(avc_general_set_sig_fmt);
  74. int avc_general_get_sig_fmt(struct fw_unit *unit, unsigned int *rate,
  75. enum avc_general_plug_dir dir,
  76. unsigned short pid)
  77. {
  78. unsigned int sfc;
  79. u8 *buf;
  80. int err;
  81. buf = kzalloc(8, GFP_KERNEL);
  82. if (buf == NULL)
  83. return -ENOMEM;
  84. buf[0] = 0x01; /* AV/C STATUS */
  85. buf[1] = 0xff; /* Unit */
  86. if (dir == AVC_GENERAL_PLUG_DIR_IN)
  87. buf[2] = 0x19; /* INPUT PLUG SIGNAL FORMAT */
  88. else
  89. buf[2] = 0x18; /* OUTPUT PLUG SIGNAL FORMAT */
  90. buf[3] = 0xff & pid; /* plug id */
  91. buf[4] = 0x90; /* EOH_1, Form_1, FMT. AM824 */
  92. buf[5] = 0xff; /* FDF-hi. AM824, frequency */
  93. buf[6] = 0xff; /* FDF-mid. AM824, SYT hi (not used) */
  94. buf[7] = 0xff; /* FDF-low. AM824, SYT lo (not used) */
  95. /* do transaction and check buf[1-4] are the same against command */
  96. err = fcp_avc_transaction(unit, buf, 8, buf, 8,
  97. BIT(1) | BIT(2) | BIT(3) | BIT(4));
  98. if (err < 0)
  99. ;
  100. else if (err < 8)
  101. err = -EIO;
  102. else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
  103. err = -ENOSYS;
  104. else if (buf[0] == 0x0a) /* REJECTED */
  105. err = -EINVAL;
  106. else if (buf[0] == 0x0b) /* IN TRANSITION */
  107. err = -EAGAIN;
  108. if (err < 0)
  109. goto end;
  110. /* check sfc field and pick up rate */
  111. sfc = 0x07 & buf[5];
  112. if (sfc >= CIP_SFC_COUNT) {
  113. err = -EAGAIN; /* also in transition */
  114. goto end;
  115. }
  116. *rate = amdtp_rate_table[sfc];
  117. err = 0;
  118. end:
  119. kfree(buf);
  120. return err;
  121. }
  122. EXPORT_SYMBOL(avc_general_get_sig_fmt);
  123. int avc_general_get_plug_info(struct fw_unit *unit, unsigned int subunit_type,
  124. unsigned int subunit_id, unsigned int subfunction,
  125. u8 info[AVC_PLUG_INFO_BUF_BYTES])
  126. {
  127. u8 *buf;
  128. int err;
  129. /* extended subunit in spec.4.2 is not supported */
  130. if ((subunit_type == 0x1E) || (subunit_id == 5))
  131. return -EINVAL;
  132. buf = kzalloc(8, GFP_KERNEL);
  133. if (buf == NULL)
  134. return -ENOMEM;
  135. buf[0] = 0x01; /* AV/C STATUS */
  136. /* UNIT or Subunit, Functionblock */
  137. buf[1] = ((subunit_type & 0x1f) << 3) | (subunit_id & 0x7);
  138. buf[2] = 0x02; /* PLUG INFO */
  139. buf[3] = 0xff & subfunction;
  140. err = fcp_avc_transaction(unit, buf, 8, buf, 8, BIT(1) | BIT(2));
  141. if (err < 0)
  142. ;
  143. else if (err < 8)
  144. err = -EIO;
  145. else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
  146. err = -ENOSYS;
  147. else if (buf[0] == 0x0a) /* REJECTED */
  148. err = -EINVAL;
  149. else if (buf[0] == 0x0b) /* IN TRANSITION */
  150. err = -EAGAIN;
  151. if (err < 0)
  152. goto end;
  153. info[0] = buf[4];
  154. info[1] = buf[5];
  155. info[2] = buf[6];
  156. info[3] = buf[7];
  157. err = 0;
  158. end:
  159. kfree(buf);
  160. return err;
  161. }
  162. EXPORT_SYMBOL(avc_general_get_plug_info);
  163. static DEFINE_SPINLOCK(transactions_lock);
  164. static LIST_HEAD(transactions);
  165. enum fcp_state {
  166. STATE_PENDING,
  167. STATE_BUS_RESET,
  168. STATE_COMPLETE,
  169. STATE_DEFERRED,
  170. };
  171. struct fcp_transaction {
  172. struct list_head list;
  173. struct fw_unit *unit;
  174. void *response_buffer;
  175. unsigned int response_size;
  176. unsigned int response_match_bytes;
  177. enum fcp_state state;
  178. wait_queue_head_t wait;
  179. bool deferrable;
  180. };
  181. /**
  182. * fcp_avc_transaction - send an AV/C command and wait for its response
  183. * @unit: a unit on the target device
  184. * @command: a buffer containing the command frame; must be DMA-able
  185. * @command_size: the size of @command
  186. * @response: a buffer for the response frame
  187. * @response_size: the maximum size of @response
  188. * @response_match_bytes: a bitmap specifying the bytes used to detect the
  189. * correct response frame
  190. *
  191. * This function sends a FCP command frame to the target and waits for the
  192. * corresponding response frame to be returned.
  193. *
  194. * Because it is possible for multiple FCP transactions to be active at the
  195. * same time, the correct response frame is detected by the value of certain
  196. * bytes. These bytes must be set in @response before calling this function,
  197. * and the corresponding bits must be set in @response_match_bytes.
  198. *
  199. * @command and @response can point to the same buffer.
  200. *
  201. * Returns the actual size of the response frame, or a negative error code.
  202. */
  203. int fcp_avc_transaction(struct fw_unit *unit,
  204. const void *command, unsigned int command_size,
  205. void *response, unsigned int response_size,
  206. unsigned int response_match_bytes)
  207. {
  208. struct fcp_transaction t;
  209. int tcode, ret, tries = 0;
  210. t.unit = unit;
  211. t.response_buffer = response;
  212. t.response_size = response_size;
  213. t.response_match_bytes = response_match_bytes;
  214. t.state = STATE_PENDING;
  215. init_waitqueue_head(&t.wait);
  216. if (*(const u8 *)command == 0x00 || *(const u8 *)command == 0x03)
  217. t.deferrable = true;
  218. spin_lock_irq(&transactions_lock);
  219. list_add_tail(&t.list, &transactions);
  220. spin_unlock_irq(&transactions_lock);
  221. for (;;) {
  222. tcode = command_size == 4 ? TCODE_WRITE_QUADLET_REQUEST
  223. : TCODE_WRITE_BLOCK_REQUEST;
  224. ret = snd_fw_transaction(t.unit, tcode,
  225. CSR_REGISTER_BASE + CSR_FCP_COMMAND,
  226. (void *)command, command_size, 0);
  227. if (ret < 0)
  228. break;
  229. deferred:
  230. wait_event_timeout(t.wait, t.state != STATE_PENDING,
  231. msecs_to_jiffies(FCP_TIMEOUT_MS));
  232. if (t.state == STATE_DEFERRED) {
  233. /*
  234. * 'AV/C General Specification' define no time limit
  235. * on command completion once an INTERIM response has
  236. * been sent. but we promise to finish this function
  237. * for a caller. Here we use FCP_TIMEOUT_MS for next
  238. * interval. This is not in the specification.
  239. */
  240. t.state = STATE_PENDING;
  241. goto deferred;
  242. } else if (t.state == STATE_COMPLETE) {
  243. ret = t.response_size;
  244. break;
  245. } else if (t.state == STATE_BUS_RESET) {
  246. msleep(ERROR_DELAY_MS);
  247. } else if (++tries >= ERROR_RETRIES) {
  248. dev_err(&t.unit->device, "FCP command timed out\n");
  249. ret = -EIO;
  250. break;
  251. }
  252. }
  253. spin_lock_irq(&transactions_lock);
  254. list_del(&t.list);
  255. spin_unlock_irq(&transactions_lock);
  256. return ret;
  257. }
  258. EXPORT_SYMBOL(fcp_avc_transaction);
  259. /**
  260. * fcp_bus_reset - inform the target handler about a bus reset
  261. * @unit: the unit that might be used by fcp_avc_transaction()
  262. *
  263. * This function must be called from the driver's .update handler to inform
  264. * the FCP transaction handler that a bus reset has happened. Any pending FCP
  265. * transactions are retried.
  266. */
  267. void fcp_bus_reset(struct fw_unit *unit)
  268. {
  269. struct fcp_transaction *t;
  270. spin_lock_irq(&transactions_lock);
  271. list_for_each_entry(t, &transactions, list) {
  272. if (t->unit == unit &&
  273. (t->state == STATE_PENDING ||
  274. t->state == STATE_DEFERRED)) {
  275. t->state = STATE_BUS_RESET;
  276. wake_up(&t->wait);
  277. }
  278. }
  279. spin_unlock_irq(&transactions_lock);
  280. }
  281. EXPORT_SYMBOL(fcp_bus_reset);
  282. /* checks whether the response matches the masked bytes in response_buffer */
  283. static bool is_matching_response(struct fcp_transaction *transaction,
  284. const void *response, size_t length)
  285. {
  286. const u8 *p1, *p2;
  287. unsigned int mask, i;
  288. p1 = response;
  289. p2 = transaction->response_buffer;
  290. mask = transaction->response_match_bytes;
  291. for (i = 0; ; ++i) {
  292. if ((mask & 1) && p1[i] != p2[i])
  293. return false;
  294. mask >>= 1;
  295. if (!mask)
  296. return true;
  297. if (--length == 0)
  298. return false;
  299. }
  300. }
  301. static void fcp_response(struct fw_card *card, struct fw_request *request,
  302. int tcode, int destination, int source,
  303. int generation, unsigned long long offset,
  304. void *data, size_t length, void *callback_data)
  305. {
  306. struct fcp_transaction *t;
  307. unsigned long flags;
  308. if (length < 1 || (*(const u8 *)data & 0xf0) != CTS_AVC)
  309. return;
  310. spin_lock_irqsave(&transactions_lock, flags);
  311. list_for_each_entry(t, &transactions, list) {
  312. struct fw_device *device = fw_parent_device(t->unit);
  313. if (device->card != card ||
  314. device->generation != generation)
  315. continue;
  316. smp_rmb(); /* node_id vs. generation */
  317. if (device->node_id != source)
  318. continue;
  319. if (t->state == STATE_PENDING &&
  320. is_matching_response(t, data, length)) {
  321. if (t->deferrable && *(const u8 *)data == 0x0f) {
  322. t->state = STATE_DEFERRED;
  323. } else {
  324. t->state = STATE_COMPLETE;
  325. t->response_size = min_t(unsigned int, length,
  326. t->response_size);
  327. memcpy(t->response_buffer, data,
  328. t->response_size);
  329. }
  330. wake_up(&t->wait);
  331. }
  332. }
  333. spin_unlock_irqrestore(&transactions_lock, flags);
  334. }
  335. static struct fw_address_handler response_register_handler = {
  336. .length = 0x200,
  337. .address_callback = fcp_response,
  338. };
  339. static int __init fcp_module_init(void)
  340. {
  341. static const struct fw_address_region response_register_region = {
  342. .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
  343. .end = CSR_REGISTER_BASE + CSR_FCP_END,
  344. };
  345. fw_core_add_address_handler(&response_register_handler,
  346. &response_register_region);
  347. return 0;
  348. }
  349. static void __exit fcp_module_exit(void)
  350. {
  351. WARN_ON(!list_empty(&transactions));
  352. fw_core_remove_address_handler(&response_register_handler);
  353. }
  354. module_init(fcp_module_init);
  355. module_exit(fcp_module_exit);