cmp.c 6.9 KB

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