cmp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Connection Management Procedures (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/module.h>
  11. #include <linux/sched.h>
  12. #include "lib.h"
  13. #include "iso-resources.h"
  14. #include "cmp.h"
  15. #define IMPR_SPEED_MASK 0xc0000000
  16. #define IMPR_SPEED_SHIFT 30
  17. #define IMPR_XSPEED_MASK 0x00000060
  18. #define IMPR_XSPEED_SHIFT 5
  19. #define IMPR_PLUGS_MASK 0x0000001f
  20. #define IPCR_ONLINE 0x80000000
  21. #define IPCR_BCAST_CONN 0x40000000
  22. #define IPCR_P2P_CONN_MASK 0x3f000000
  23. #define IPCR_P2P_CONN_SHIFT 24
  24. #define IPCR_CHANNEL_MASK 0x003f0000
  25. #define IPCR_CHANNEL_SHIFT 16
  26. enum bus_reset_handling {
  27. ABORT_ON_BUS_RESET,
  28. SUCCEED_ON_BUS_RESET,
  29. };
  30. static __attribute__((format(printf, 2, 3)))
  31. void cmp_error(struct cmp_connection *c, const char *fmt, ...)
  32. {
  33. va_list va;
  34. va_start(va, fmt);
  35. dev_err(&c->resources.unit->device, "%cPCR%u: %pV",
  36. 'i', c->pcr_index, &(struct va_format){ fmt, &va });
  37. va_end(va);
  38. }
  39. static int pcr_modify(struct cmp_connection *c,
  40. __be32 (*modify)(struct cmp_connection *c, __be32 old),
  41. int (*check)(struct cmp_connection *c, __be32 pcr),
  42. enum bus_reset_handling bus_reset_handling)
  43. {
  44. struct fw_device *device = fw_parent_device(c->resources.unit);
  45. __be32 *buffer = c->resources.buffer;
  46. int generation = c->resources.generation;
  47. int rcode, errors = 0;
  48. __be32 old_arg;
  49. int err;
  50. buffer[0] = c->last_pcr_value;
  51. for (;;) {
  52. old_arg = buffer[0];
  53. buffer[1] = modify(c, buffer[0]);
  54. rcode = fw_run_transaction(
  55. device->card, TCODE_LOCK_COMPARE_SWAP,
  56. device->node_id, generation, device->max_speed,
  57. CSR_REGISTER_BASE + CSR_IPCR(c->pcr_index),
  58. buffer, 8);
  59. if (rcode == RCODE_COMPLETE) {
  60. if (buffer[0] == old_arg) /* success? */
  61. break;
  62. if (check) {
  63. err = check(c, buffer[0]);
  64. if (err < 0)
  65. return err;
  66. }
  67. } else if (rcode == RCODE_GENERATION)
  68. goto bus_reset;
  69. else if (rcode_is_permanent_error(rcode) || ++errors >= 3)
  70. goto io_error;
  71. }
  72. c->last_pcr_value = buffer[1];
  73. return 0;
  74. io_error:
  75. cmp_error(c, "transaction failed: %s\n", rcode_string(rcode));
  76. return -EIO;
  77. bus_reset:
  78. return bus_reset_handling == ABORT_ON_BUS_RESET ? -EAGAIN : 0;
  79. }
  80. /**
  81. * cmp_connection_init - initializes a connection manager
  82. * @c: the connection manager to initialize
  83. * @unit: a unit of the target device
  84. * @ipcr_index: the index of the iPCR on the target device
  85. */
  86. int cmp_connection_init(struct cmp_connection *c,
  87. struct fw_unit *unit,
  88. unsigned int ipcr_index)
  89. {
  90. __be32 impr_be;
  91. u32 impr;
  92. int err;
  93. err = snd_fw_transaction(unit, TCODE_READ_QUADLET_REQUEST,
  94. CSR_REGISTER_BASE + CSR_IMPR,
  95. &impr_be, 4);
  96. if (err < 0)
  97. return err;
  98. impr = be32_to_cpu(impr_be);
  99. if (ipcr_index >= (impr & IMPR_PLUGS_MASK))
  100. return -EINVAL;
  101. err = fw_iso_resources_init(&c->resources, unit);
  102. if (err < 0)
  103. return err;
  104. c->connected = false;
  105. mutex_init(&c->mutex);
  106. c->last_pcr_value = cpu_to_be32(0x80000000);
  107. c->pcr_index = ipcr_index;
  108. c->max_speed = (impr & IMPR_SPEED_MASK) >> IMPR_SPEED_SHIFT;
  109. if (c->max_speed == SCODE_BETA)
  110. c->max_speed += (impr & IMPR_XSPEED_MASK) >> IMPR_XSPEED_SHIFT;
  111. return 0;
  112. }
  113. EXPORT_SYMBOL(cmp_connection_init);
  114. /**
  115. * cmp_connection_destroy - free connection manager resources
  116. * @c: the connection manager
  117. */
  118. void cmp_connection_destroy(struct cmp_connection *c)
  119. {
  120. WARN_ON(c->connected);
  121. mutex_destroy(&c->mutex);
  122. fw_iso_resources_destroy(&c->resources);
  123. }
  124. EXPORT_SYMBOL(cmp_connection_destroy);
  125. static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr)
  126. {
  127. ipcr &= ~cpu_to_be32(IPCR_BCAST_CONN |
  128. IPCR_P2P_CONN_MASK |
  129. IPCR_CHANNEL_MASK);
  130. ipcr |= cpu_to_be32(1 << IPCR_P2P_CONN_SHIFT);
  131. ipcr |= cpu_to_be32(c->resources.channel << IPCR_CHANNEL_SHIFT);
  132. return ipcr;
  133. }
  134. static int ipcr_set_check(struct cmp_connection *c, __be32 ipcr)
  135. {
  136. if (ipcr & cpu_to_be32(IPCR_BCAST_CONN |
  137. IPCR_P2P_CONN_MASK)) {
  138. cmp_error(c, "plug is already in use\n");
  139. return -EBUSY;
  140. }
  141. if (!(ipcr & cpu_to_be32(IPCR_ONLINE))) {
  142. cmp_error(c, "plug is not on-line\n");
  143. return -ECONNREFUSED;
  144. }
  145. return 0;
  146. }
  147. /**
  148. * cmp_connection_establish - establish a connection to the target
  149. * @c: the connection manager
  150. * @max_payload_bytes: the amount of data (including CIP headers) per packet
  151. *
  152. * This function establishes a point-to-point connection from the local
  153. * computer to the target by allocating isochronous resources (channel and
  154. * bandwidth) and setting the target's input plug control register. When this
  155. * function succeeds, the caller is responsible for starting transmitting
  156. * packets.
  157. */
  158. int cmp_connection_establish(struct cmp_connection *c,
  159. unsigned int max_payload_bytes)
  160. {
  161. int err;
  162. if (WARN_ON(c->connected))
  163. return -EISCONN;
  164. c->speed = min(c->max_speed,
  165. fw_parent_device(c->resources.unit)->max_speed);
  166. mutex_lock(&c->mutex);
  167. retry_after_bus_reset:
  168. err = fw_iso_resources_allocate(&c->resources,
  169. max_payload_bytes, c->speed);
  170. if (err < 0)
  171. goto err_mutex;
  172. err = pcr_modify(c, ipcr_set_modify, ipcr_set_check,
  173. ABORT_ON_BUS_RESET);
  174. if (err == -EAGAIN) {
  175. fw_iso_resources_free(&c->resources);
  176. goto retry_after_bus_reset;
  177. }
  178. if (err < 0)
  179. goto err_resources;
  180. c->connected = true;
  181. mutex_unlock(&c->mutex);
  182. return 0;
  183. err_resources:
  184. fw_iso_resources_free(&c->resources);
  185. err_mutex:
  186. mutex_unlock(&c->mutex);
  187. return err;
  188. }
  189. EXPORT_SYMBOL(cmp_connection_establish);
  190. /**
  191. * cmp_connection_update - update the connection after a bus reset
  192. * @c: the connection manager
  193. *
  194. * This function must be called from the driver's .update handler to reestablish
  195. * any connection that might have been active.
  196. *
  197. * Returns zero on success, or a negative error code. On an error, the
  198. * connection is broken and the caller must stop transmitting iso packets.
  199. */
  200. int cmp_connection_update(struct cmp_connection *c)
  201. {
  202. int err;
  203. mutex_lock(&c->mutex);
  204. if (!c->connected) {
  205. mutex_unlock(&c->mutex);
  206. return 0;
  207. }
  208. err = fw_iso_resources_update(&c->resources);
  209. if (err < 0)
  210. goto err_unconnect;
  211. err = pcr_modify(c, ipcr_set_modify, ipcr_set_check,
  212. SUCCEED_ON_BUS_RESET);
  213. if (err < 0)
  214. goto err_resources;
  215. mutex_unlock(&c->mutex);
  216. return 0;
  217. err_resources:
  218. fw_iso_resources_free(&c->resources);
  219. err_unconnect:
  220. c->connected = false;
  221. mutex_unlock(&c->mutex);
  222. return err;
  223. }
  224. EXPORT_SYMBOL(cmp_connection_update);
  225. static __be32 ipcr_break_modify(struct cmp_connection *c, __be32 ipcr)
  226. {
  227. return ipcr & ~cpu_to_be32(IPCR_BCAST_CONN | IPCR_P2P_CONN_MASK);
  228. }
  229. /**
  230. * cmp_connection_break - break the connection to the target
  231. * @c: the connection manager
  232. *
  233. * This function deactives the connection in the target's input plug control
  234. * register, and frees the isochronous resources of the connection. Before
  235. * calling this function, the caller should cease transmitting packets.
  236. */
  237. void cmp_connection_break(struct cmp_connection *c)
  238. {
  239. int err;
  240. mutex_lock(&c->mutex);
  241. if (!c->connected) {
  242. mutex_unlock(&c->mutex);
  243. return;
  244. }
  245. err = pcr_modify(c, ipcr_break_modify, NULL, SUCCEED_ON_BUS_RESET);
  246. if (err < 0)
  247. cmp_error(c, "plug is still connected\n");
  248. fw_iso_resources_free(&c->resources);
  249. c->connected = false;
  250. mutex_unlock(&c->mutex);
  251. }
  252. EXPORT_SYMBOL(cmp_connection_break);