ff-transaction.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * ff-transaction.c - a part of driver for RME Fireface series
  3. *
  4. * Copyright (c) 2015-2017 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include "ff.h"
  9. static void finish_transmit_midi_msg(struct snd_ff *ff, unsigned int port,
  10. int rcode)
  11. {
  12. struct snd_rawmidi_substream *substream =
  13. READ_ONCE(ff->rx_midi_substreams[port]);
  14. if (rcode_is_permanent_error(rcode)) {
  15. ff->rx_midi_error[port] = true;
  16. return;
  17. }
  18. if (rcode != RCODE_COMPLETE) {
  19. /* Transfer the message again, immediately. */
  20. ff->next_ktime[port] = 0;
  21. schedule_work(&ff->rx_midi_work[port]);
  22. return;
  23. }
  24. snd_rawmidi_transmit_ack(substream, ff->rx_bytes[port]);
  25. ff->rx_bytes[port] = 0;
  26. if (!snd_rawmidi_transmit_empty(substream))
  27. schedule_work(&ff->rx_midi_work[port]);
  28. }
  29. static void finish_transmit_midi0_msg(struct fw_card *card, int rcode,
  30. void *data, size_t length,
  31. void *callback_data)
  32. {
  33. struct snd_ff *ff =
  34. container_of(callback_data, struct snd_ff, transactions[0]);
  35. finish_transmit_midi_msg(ff, 0, rcode);
  36. }
  37. static void finish_transmit_midi1_msg(struct fw_card *card, int rcode,
  38. void *data, size_t length,
  39. void *callback_data)
  40. {
  41. struct snd_ff *ff =
  42. container_of(callback_data, struct snd_ff, transactions[1]);
  43. finish_transmit_midi_msg(ff, 1, rcode);
  44. }
  45. static inline void fill_midi_buf(struct snd_ff *ff, unsigned int port,
  46. unsigned int index, u8 byte)
  47. {
  48. ff->msg_buf[port][index] = cpu_to_le32(byte);
  49. }
  50. static void transmit_midi_msg(struct snd_ff *ff, unsigned int port)
  51. {
  52. struct snd_rawmidi_substream *substream =
  53. READ_ONCE(ff->rx_midi_substreams[port]);
  54. u8 *buf = (u8 *)ff->msg_buf[port];
  55. int i, len;
  56. struct fw_device *fw_dev = fw_parent_device(ff->unit);
  57. unsigned long long addr;
  58. int generation;
  59. fw_transaction_callback_t callback;
  60. if (substream == NULL || snd_rawmidi_transmit_empty(substream))
  61. return;
  62. if (ff->rx_bytes[port] > 0 || ff->rx_midi_error[port])
  63. return;
  64. /* Do it in next chance. */
  65. if (ktime_after(ff->next_ktime[port], ktime_get())) {
  66. schedule_work(&ff->rx_midi_work[port]);
  67. return;
  68. }
  69. len = snd_rawmidi_transmit_peek(substream, buf,
  70. SND_FF_MAXIMIM_MIDI_QUADS);
  71. if (len <= 0)
  72. return;
  73. for (i = len - 1; i >= 0; i--)
  74. fill_midi_buf(ff, port, i, buf[i]);
  75. if (port == 0) {
  76. addr = ff->spec->protocol->midi_rx_port_0_reg;
  77. callback = finish_transmit_midi0_msg;
  78. } else {
  79. addr = ff->spec->protocol->midi_rx_port_1_reg;
  80. callback = finish_transmit_midi1_msg;
  81. }
  82. /* Set interval to next transaction. */
  83. ff->next_ktime[port] = ktime_add_ns(ktime_get(),
  84. len * 8 * (NSEC_PER_SEC / 31250));
  85. ff->rx_bytes[port] = len;
  86. /*
  87. * In Linux FireWire core, when generation is updated with memory
  88. * barrier, node id has already been updated. In this module, After
  89. * this smp_rmb(), load/store instructions to memory are completed.
  90. * Thus, both of generation and node id are available with recent
  91. * values. This is a light-serialization solution to handle bus reset
  92. * events on IEEE 1394 bus.
  93. */
  94. generation = fw_dev->generation;
  95. smp_rmb();
  96. fw_send_request(fw_dev->card, &ff->transactions[port],
  97. TCODE_WRITE_BLOCK_REQUEST,
  98. fw_dev->node_id, generation, fw_dev->max_speed,
  99. addr, &ff->msg_buf[port], len * 4,
  100. callback, &ff->transactions[port]);
  101. }
  102. static void transmit_midi0_msg(struct work_struct *work)
  103. {
  104. struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[0]);
  105. transmit_midi_msg(ff, 0);
  106. }
  107. static void transmit_midi1_msg(struct work_struct *work)
  108. {
  109. struct snd_ff *ff = container_of(work, struct snd_ff, rx_midi_work[1]);
  110. transmit_midi_msg(ff, 1);
  111. }
  112. static void handle_midi_msg(struct fw_card *card, struct fw_request *request,
  113. int tcode, int destination, int source,
  114. int generation, unsigned long long offset,
  115. void *data, size_t length, void *callback_data)
  116. {
  117. struct snd_ff *ff = callback_data;
  118. __le32 *buf = data;
  119. u32 quad;
  120. u8 byte;
  121. unsigned int index;
  122. struct snd_rawmidi_substream *substream;
  123. int i;
  124. fw_send_response(card, request, RCODE_COMPLETE);
  125. for (i = 0; i < length / 4; i++) {
  126. quad = le32_to_cpu(buf[i]);
  127. /* Message in first port. */
  128. /*
  129. * This value may represent the index of this unit when the same
  130. * units are on the same IEEE 1394 bus. This driver doesn't use
  131. * it.
  132. */
  133. index = (quad >> 8) & 0xff;
  134. if (index > 0) {
  135. substream = READ_ONCE(ff->tx_midi_substreams[0]);
  136. if (substream != NULL) {
  137. byte = quad & 0xff;
  138. snd_rawmidi_receive(substream, &byte, 1);
  139. }
  140. }
  141. /* Message in second port. */
  142. index = (quad >> 24) & 0xff;
  143. if (index > 0) {
  144. substream = READ_ONCE(ff->tx_midi_substreams[1]);
  145. if (substream != NULL) {
  146. byte = (quad >> 16) & 0xff;
  147. snd_rawmidi_receive(substream, &byte, 1);
  148. }
  149. }
  150. }
  151. }
  152. static int allocate_own_address(struct snd_ff *ff, int i)
  153. {
  154. struct fw_address_region midi_msg_region;
  155. int err;
  156. ff->async_handler.length = SND_FF_MAXIMIM_MIDI_QUADS * 4;
  157. ff->async_handler.address_callback = handle_midi_msg;
  158. ff->async_handler.callback_data = ff;
  159. midi_msg_region.start = 0x000100000000ull * i;
  160. midi_msg_region.end = midi_msg_region.start + ff->async_handler.length;
  161. err = fw_core_add_address_handler(&ff->async_handler, &midi_msg_region);
  162. if (err >= 0) {
  163. /* Controllers are allowed to register this region. */
  164. if (ff->async_handler.offset & 0x0000ffffffff) {
  165. fw_core_remove_address_handler(&ff->async_handler);
  166. err = -EAGAIN;
  167. }
  168. }
  169. return err;
  170. }
  171. /*
  172. * The configuration to start asynchronous transactions for MIDI messages is in
  173. * 0x'0000'8010'051c. This register includes the other options, thus this driver
  174. * doesn't touch it and leaves the decision to userspace. The userspace MUST add
  175. * 0x04000000 to write transactions to the register to receive any MIDI
  176. * messages.
  177. *
  178. * Here, I just describe MIDI-related offsets of the register, in little-endian
  179. * order.
  180. *
  181. * Controllers are allowed to register higher 4 bytes of address to receive
  182. * the transactions. The register is 0x'0000'8010'03f4. On the other hand, the
  183. * controllers are not allowed to register lower 4 bytes of the address. They
  184. * are forced to select from 4 options by writing corresponding bits to
  185. * 0x'0000'8010'051c.
  186. *
  187. * The 3rd-6th bits in MSB of this register are used to indicate lower 4 bytes
  188. * of address to which the device transferrs the transactions.
  189. * - 6th: 0x'....'....'0000'0180
  190. * - 5th: 0x'....'....'0000'0100
  191. * - 4th: 0x'....'....'0000'0080
  192. * - 3rd: 0x'....'....'0000'0000
  193. *
  194. * This driver configure 0x'....'....'0000'0000 for units to receive MIDI
  195. * messages. 3rd bit of the register should be configured, however this driver
  196. * deligates this task to user space applications due to a restriction that
  197. * this register is write-only and the other bits have own effects.
  198. *
  199. * The 1st and 2nd bits in LSB of this register are used to cancel transferring
  200. * asynchronous transactions. These two bits have the same effect.
  201. * - 1st/2nd: cancel transferring
  202. */
  203. int snd_ff_transaction_reregister(struct snd_ff *ff)
  204. {
  205. struct fw_card *fw_card = fw_parent_device(ff->unit)->card;
  206. u32 addr;
  207. __le32 reg;
  208. /*
  209. * Controllers are allowed to register its node ID and upper 2 byte of
  210. * local address to listen asynchronous transactions.
  211. */
  212. addr = (fw_card->node_id << 16) | (ff->async_handler.offset >> 32);
  213. reg = cpu_to_le32(addr);
  214. return snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  215. ff->spec->protocol->midi_high_addr_reg,
  216. &reg, sizeof(reg), 0);
  217. }
  218. int snd_ff_transaction_register(struct snd_ff *ff)
  219. {
  220. int i, err;
  221. /*
  222. * Allocate in Memory Space of IEC 13213, but lower 4 byte in LSB should
  223. * be zero due to device specification.
  224. */
  225. for (i = 0; i < 0xffff; i++) {
  226. err = allocate_own_address(ff, i);
  227. if (err != -EBUSY && err != -EAGAIN)
  228. break;
  229. }
  230. if (err < 0)
  231. return err;
  232. err = snd_ff_transaction_reregister(ff);
  233. if (err < 0)
  234. return err;
  235. INIT_WORK(&ff->rx_midi_work[0], transmit_midi0_msg);
  236. INIT_WORK(&ff->rx_midi_work[1], transmit_midi1_msg);
  237. return 0;
  238. }
  239. void snd_ff_transaction_unregister(struct snd_ff *ff)
  240. {
  241. __le32 reg;
  242. if (ff->async_handler.callback_data == NULL)
  243. return;
  244. ff->async_handler.callback_data = NULL;
  245. /* Release higher 4 bytes of address. */
  246. reg = cpu_to_le32(0x00000000);
  247. snd_fw_transaction(ff->unit, TCODE_WRITE_QUADLET_REQUEST,
  248. ff->spec->protocol->midi_high_addr_reg,
  249. &reg, sizeof(reg), 0);
  250. fw_core_remove_address_handler(&ff->async_handler);
  251. }