lib.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * miscellaneous 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/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/firewire.h>
  10. #include <linux/module.h>
  11. #include <linux/slab.h>
  12. #include "lib.h"
  13. #define ERROR_RETRY_DELAY_MS 20
  14. /**
  15. * snd_fw_transaction - send a request and wait for its completion
  16. * @unit: the driver's unit on the target device
  17. * @tcode: the transaction code
  18. * @offset: the address in the target's address space
  19. * @buffer: input/output data
  20. * @length: length of @buffer
  21. * @flags: use %FW_FIXED_GENERATION and add the generation value to attempt the
  22. * request only in that generation; use %FW_QUIET to suppress error
  23. * messages
  24. *
  25. * Submits an asynchronous request to the target device, and waits for the
  26. * response. The node ID and the current generation are derived from @unit.
  27. * On a bus reset or an error, the transaction is retried a few times.
  28. * Returns zero on success, or a negative error code.
  29. */
  30. int snd_fw_transaction(struct fw_unit *unit, int tcode,
  31. u64 offset, void *buffer, size_t length,
  32. unsigned int flags)
  33. {
  34. struct fw_device *device = fw_parent_device(unit);
  35. int generation, rcode, tries = 0;
  36. generation = flags & FW_GENERATION_MASK;
  37. for (;;) {
  38. if (!(flags & FW_FIXED_GENERATION)) {
  39. generation = device->generation;
  40. smp_rmb(); /* node_id vs. generation */
  41. }
  42. rcode = fw_run_transaction(device->card, tcode,
  43. device->node_id, generation,
  44. device->max_speed, offset,
  45. buffer, length);
  46. if (rcode == RCODE_COMPLETE)
  47. return 0;
  48. if (rcode == RCODE_GENERATION && (flags & FW_FIXED_GENERATION))
  49. return -EAGAIN;
  50. if (rcode_is_permanent_error(rcode) || ++tries >= 3) {
  51. if (!(flags & FW_QUIET))
  52. dev_err(&unit->device,
  53. "transaction failed: %s\n",
  54. fw_rcode_string(rcode));
  55. return -EIO;
  56. }
  57. msleep(ERROR_RETRY_DELAY_MS);
  58. }
  59. }
  60. EXPORT_SYMBOL(snd_fw_transaction);
  61. static void async_midi_port_callback(struct fw_card *card, int rcode,
  62. void *data, size_t length,
  63. void *callback_data)
  64. {
  65. struct snd_fw_async_midi_port *port = callback_data;
  66. struct snd_rawmidi_substream *substream = ACCESS_ONCE(port->substream);
  67. /* This port is closed. */
  68. if (substream == NULL)
  69. return;
  70. if (rcode == RCODE_COMPLETE)
  71. snd_rawmidi_transmit_ack(substream, port->consume_bytes);
  72. else if (!rcode_is_permanent_error(rcode))
  73. /* To start next transaction immediately for recovery. */
  74. port->next_ktime = ktime_set(0, 0);
  75. else
  76. /* Don't continue processing. */
  77. port->error = true;
  78. port->idling = true;
  79. if (!snd_rawmidi_transmit_empty(substream))
  80. schedule_work(&port->work);
  81. }
  82. static void midi_port_work(struct work_struct *work)
  83. {
  84. struct snd_fw_async_midi_port *port =
  85. container_of(work, struct snd_fw_async_midi_port, work);
  86. struct snd_rawmidi_substream *substream = ACCESS_ONCE(port->substream);
  87. int generation;
  88. int type;
  89. /* Under transacting or error state. */
  90. if (!port->idling || port->error)
  91. return;
  92. /* Nothing to do. */
  93. if (substream == NULL || snd_rawmidi_transmit_empty(substream))
  94. return;
  95. /* Do it in next chance. */
  96. if (ktime_after(port->next_ktime, ktime_get())) {
  97. schedule_work(&port->work);
  98. return;
  99. }
  100. /*
  101. * Fill the buffer. The callee must use snd_rawmidi_transmit_peek().
  102. * Later, snd_rawmidi_transmit_ack() is called.
  103. */
  104. memset(port->buf, 0, port->len);
  105. port->consume_bytes = port->fill(substream, port->buf);
  106. if (port->consume_bytes <= 0) {
  107. /* Do it in next chance, immediately. */
  108. if (port->consume_bytes == 0) {
  109. port->next_ktime = ktime_set(0, 0);
  110. schedule_work(&port->work);
  111. } else {
  112. /* Fatal error. */
  113. port->error = true;
  114. }
  115. return;
  116. }
  117. /* Calculate type of transaction. */
  118. if (port->len == 4)
  119. type = TCODE_WRITE_QUADLET_REQUEST;
  120. else
  121. type = TCODE_WRITE_BLOCK_REQUEST;
  122. /* Set interval to next transaction. */
  123. port->next_ktime = ktime_add_ns(ktime_get(),
  124. port->consume_bytes * 8 * NSEC_PER_SEC / 31250);
  125. /* Start this transaction. */
  126. port->idling = false;
  127. /*
  128. * In Linux FireWire core, when generation is updated with memory
  129. * barrier, node id has already been updated. In this module, After
  130. * this smp_rmb(), load/store instructions to memory are completed.
  131. * Thus, both of generation and node id are available with recent
  132. * values. This is a light-serialization solution to handle bus reset
  133. * events on IEEE 1394 bus.
  134. */
  135. generation = port->parent->generation;
  136. smp_rmb();
  137. fw_send_request(port->parent->card, &port->transaction, type,
  138. port->parent->node_id, generation,
  139. port->parent->max_speed, port->addr,
  140. port->buf, port->len, async_midi_port_callback,
  141. port);
  142. }
  143. /**
  144. * snd_fw_async_midi_port_init - initialize asynchronous MIDI port structure
  145. * @port: the asynchronous MIDI port to initialize
  146. * @unit: the target of the asynchronous transaction
  147. * @addr: the address to which transactions are transferred
  148. * @len: the length of transaction
  149. * @fill: the callback function to fill given buffer, and returns the
  150. * number of consumed bytes for MIDI message.
  151. *
  152. */
  153. int snd_fw_async_midi_port_init(struct snd_fw_async_midi_port *port,
  154. struct fw_unit *unit, u64 addr, unsigned int len,
  155. snd_fw_async_midi_port_fill fill)
  156. {
  157. port->len = DIV_ROUND_UP(len, 4) * 4;
  158. port->buf = kzalloc(port->len, GFP_KERNEL);
  159. if (port->buf == NULL)
  160. return -ENOMEM;
  161. port->parent = fw_parent_device(unit);
  162. port->addr = addr;
  163. port->fill = fill;
  164. port->idling = true;
  165. port->next_ktime = ktime_set(0, 0);
  166. port->error = false;
  167. INIT_WORK(&port->work, midi_port_work);
  168. return 0;
  169. }
  170. EXPORT_SYMBOL(snd_fw_async_midi_port_init);
  171. /**
  172. * snd_fw_async_midi_port_destroy - free asynchronous MIDI port structure
  173. * @port: the asynchronous MIDI port structure
  174. */
  175. void snd_fw_async_midi_port_destroy(struct snd_fw_async_midi_port *port)
  176. {
  177. snd_fw_async_midi_port_finish(port);
  178. cancel_work_sync(&port->work);
  179. kfree(port->buf);
  180. }
  181. EXPORT_SYMBOL(snd_fw_async_midi_port_destroy);
  182. MODULE_DESCRIPTION("FireWire audio helper functions");
  183. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  184. MODULE_LICENSE("GPL v2");