cmp.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. c->connected = false;
  102. mutex_init(&c->mutex);
  103. fw_iso_resources_init(&c->resources, unit);
  104. c->last_pcr_value = cpu_to_be32(0x80000000);
  105. c->pcr_index = ipcr_index;
  106. c->max_speed = (impr & IMPR_SPEED_MASK) >> IMPR_SPEED_SHIFT;
  107. if (c->max_speed == SCODE_BETA)
  108. c->max_speed += (impr & IMPR_XSPEED_MASK) >> IMPR_XSPEED_SHIFT;
  109. return 0;
  110. }
  111. EXPORT_SYMBOL(cmp_connection_init);
  112. /**
  113. * cmp_connection_destroy - free connection manager resources
  114. * @c: the connection manager
  115. */
  116. void cmp_connection_destroy(struct cmp_connection *c)
  117. {
  118. WARN_ON(c->connected);
  119. mutex_destroy(&c->mutex);
  120. fw_iso_resources_destroy(&c->resources);
  121. }
  122. EXPORT_SYMBOL(cmp_connection_destroy);
  123. static __be32 ipcr_set_modify(struct cmp_connection *c, __be32 ipcr)
  124. {
  125. ipcr &= ~cpu_to_be32(IPCR_BCAST_CONN |
  126. IPCR_P2P_CONN_MASK |
  127. IPCR_CHANNEL_MASK);
  128. ipcr |= cpu_to_be32(1 << IPCR_P2P_CONN_SHIFT);
  129. ipcr |= cpu_to_be32(c->resources.channel << IPCR_CHANNEL_SHIFT);
  130. return ipcr;
  131. }
  132. static int ipcr_set_check(struct cmp_connection *c, __be32 ipcr)
  133. {
  134. if (ipcr & cpu_to_be32(IPCR_BCAST_CONN |
  135. IPCR_P2P_CONN_MASK)) {
  136. cmp_error(c, "plug is already in use\n");
  137. return -EBUSY;
  138. }
  139. if (!(ipcr & cpu_to_be32(IPCR_ONLINE))) {
  140. cmp_error(c, "plug is not on-line\n");
  141. return -ECONNREFUSED;
  142. }
  143. return 0;
  144. }
  145. /**
  146. * cmp_connection_establish - establish a connection to the target
  147. * @c: the connection manager
  148. * @max_payload_bytes: the amount of data (including CIP headers) per packet
  149. *
  150. * This function establishes a point-to-point connection from the local
  151. * computer to the target by allocating isochronous resources (channel and
  152. * bandwidth) and setting the target's input plug control register. When this
  153. * function succeeds, the caller is responsible for starting transmitting
  154. * packets.
  155. */
  156. int cmp_connection_establish(struct cmp_connection *c,
  157. unsigned int max_payload_bytes)
  158. {
  159. int err;
  160. if (WARN_ON(c->connected))
  161. return -EISCONN;
  162. c->speed = min(c->max_speed,
  163. fw_parent_device(c->resources.unit)->max_speed);
  164. mutex_lock(&c->mutex);
  165. retry_after_bus_reset:
  166. err = fw_iso_resources_allocate(&c->resources,
  167. max_payload_bytes, c->speed);
  168. if (err < 0)
  169. goto err_mutex;
  170. err = pcr_modify(c, ipcr_set_modify, ipcr_set_check,
  171. ABORT_ON_BUS_RESET);
  172. if (err == -EAGAIN) {
  173. fw_iso_resources_free(&c->resources);
  174. goto retry_after_bus_reset;
  175. }
  176. if (err < 0)
  177. goto err_resources;
  178. c->connected = true;
  179. mutex_unlock(&c->mutex);
  180. return 0;
  181. err_resources:
  182. fw_iso_resources_free(&c->resources);
  183. err_mutex:
  184. mutex_unlock(&c->mutex);
  185. return err;
  186. }
  187. EXPORT_SYMBOL(cmp_connection_establish);
  188. /**
  189. * cmp_connection_update - update the connection after a bus reset
  190. * @c: the connection manager
  191. *
  192. * This function must be called from the driver's .update handler to reestablish
  193. * any connection that might have been active.
  194. *
  195. * Returns zero on success, or a negative error code. On an error, the
  196. * connection is broken and the caller must stop transmitting iso packets.
  197. */
  198. int cmp_connection_update(struct cmp_connection *c)
  199. {
  200. int err;
  201. mutex_lock(&c->mutex);
  202. if (!c->connected) {
  203. mutex_unlock(&c->mutex);
  204. return 0;
  205. }
  206. err = fw_iso_resources_update(&c->resources);
  207. if (err < 0)
  208. goto err_unconnect;
  209. err = pcr_modify(c, ipcr_set_modify, ipcr_set_check,
  210. SUCCEED_ON_BUS_RESET);
  211. if (err < 0)
  212. goto err_resources;
  213. mutex_unlock(&c->mutex);
  214. return 0;
  215. err_resources:
  216. fw_iso_resources_free(&c->resources);
  217. err_unconnect:
  218. c->connected = false;
  219. mutex_unlock(&c->mutex);
  220. return err;
  221. }
  222. EXPORT_SYMBOL(cmp_connection_update);
  223. static __be32 ipcr_break_modify(struct cmp_connection *c, __be32 ipcr)
  224. {
  225. return ipcr & ~cpu_to_be32(IPCR_BCAST_CONN | IPCR_P2P_CONN_MASK);
  226. }
  227. /**
  228. * cmp_connection_break - break the connection to the target
  229. * @c: the connection manager
  230. *
  231. * This function deactives the connection in the target's input plug control
  232. * register, and frees the isochronous resources of the connection. Before
  233. * calling this function, the caller should cease transmitting packets.
  234. */
  235. void cmp_connection_break(struct cmp_connection *c)
  236. {
  237. int err;
  238. mutex_lock(&c->mutex);
  239. if (!c->connected) {
  240. mutex_unlock(&c->mutex);
  241. return;
  242. }
  243. err = pcr_modify(c, ipcr_break_modify, NULL, SUCCEED_ON_BUS_RESET);
  244. if (err < 0)
  245. cmp_error(c, "plug is still connected\n");
  246. fw_iso_resources_free(&c->resources);
  247. c->connected = false;
  248. mutex_unlock(&c->mutex);
  249. }
  250. EXPORT_SYMBOL(cmp_connection_break);