lib.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #define PROBE_DELAY_MS (2 * MSEC_PER_SEC)
  62. /**
  63. * snd_fw_schedule_registration - schedule work for sound card registration
  64. * @unit: an instance for unit on IEEE 1394 bus
  65. * @dwork: delayed work with callback function
  66. *
  67. * This function is not designed for general purposes. When new unit is
  68. * connected to IEEE 1394 bus, the bus is under bus-reset state because of
  69. * topological change. In this state, units tend to fail both of asynchronous
  70. * and isochronous communication. To avoid this problem, this function is used
  71. * to postpone sound card registration after the state. The callers must
  72. * set up instance of delayed work in advance.
  73. */
  74. void snd_fw_schedule_registration(struct fw_unit *unit,
  75. struct delayed_work *dwork)
  76. {
  77. u64 now, delay;
  78. now = get_jiffies_64();
  79. delay = fw_parent_device(unit)->card->reset_jiffies
  80. + msecs_to_jiffies(PROBE_DELAY_MS);
  81. if (time_after64(delay, now))
  82. delay -= now;
  83. else
  84. delay = 0;
  85. mod_delayed_work(system_wq, dwork, delay);
  86. }
  87. EXPORT_SYMBOL(snd_fw_schedule_registration);
  88. static void async_midi_port_callback(struct fw_card *card, int rcode,
  89. void *data, size_t length,
  90. void *callback_data)
  91. {
  92. struct snd_fw_async_midi_port *port = callback_data;
  93. struct snd_rawmidi_substream *substream = ACCESS_ONCE(port->substream);
  94. /* This port is closed. */
  95. if (substream == NULL)
  96. return;
  97. if (rcode == RCODE_COMPLETE)
  98. snd_rawmidi_transmit_ack(substream, port->consume_bytes);
  99. else if (!rcode_is_permanent_error(rcode))
  100. /* To start next transaction immediately for recovery. */
  101. port->next_ktime = ktime_set(0, 0);
  102. else
  103. /* Don't continue processing. */
  104. port->error = true;
  105. port->idling = true;
  106. if (!snd_rawmidi_transmit_empty(substream))
  107. schedule_work(&port->work);
  108. }
  109. static void midi_port_work(struct work_struct *work)
  110. {
  111. struct snd_fw_async_midi_port *port =
  112. container_of(work, struct snd_fw_async_midi_port, work);
  113. struct snd_rawmidi_substream *substream = ACCESS_ONCE(port->substream);
  114. int generation;
  115. int type;
  116. /* Under transacting or error state. */
  117. if (!port->idling || port->error)
  118. return;
  119. /* Nothing to do. */
  120. if (substream == NULL || snd_rawmidi_transmit_empty(substream))
  121. return;
  122. /* Do it in next chance. */
  123. if (ktime_after(port->next_ktime, ktime_get())) {
  124. schedule_work(&port->work);
  125. return;
  126. }
  127. /*
  128. * Fill the buffer. The callee must use snd_rawmidi_transmit_peek().
  129. * Later, snd_rawmidi_transmit_ack() is called.
  130. */
  131. memset(port->buf, 0, port->len);
  132. port->consume_bytes = port->fill(substream, port->buf);
  133. if (port->consume_bytes <= 0) {
  134. /* Do it in next chance, immediately. */
  135. if (port->consume_bytes == 0) {
  136. port->next_ktime = ktime_set(0, 0);
  137. schedule_work(&port->work);
  138. } else {
  139. /* Fatal error. */
  140. port->error = true;
  141. }
  142. return;
  143. }
  144. /* Calculate type of transaction. */
  145. if (port->len == 4)
  146. type = TCODE_WRITE_QUADLET_REQUEST;
  147. else
  148. type = TCODE_WRITE_BLOCK_REQUEST;
  149. /* Set interval to next transaction. */
  150. port->next_ktime = ktime_add_ns(ktime_get(),
  151. port->consume_bytes * 8 * NSEC_PER_SEC / 31250);
  152. /* Start this transaction. */
  153. port->idling = false;
  154. /*
  155. * In Linux FireWire core, when generation is updated with memory
  156. * barrier, node id has already been updated. In this module, After
  157. * this smp_rmb(), load/store instructions to memory are completed.
  158. * Thus, both of generation and node id are available with recent
  159. * values. This is a light-serialization solution to handle bus reset
  160. * events on IEEE 1394 bus.
  161. */
  162. generation = port->parent->generation;
  163. smp_rmb();
  164. fw_send_request(port->parent->card, &port->transaction, type,
  165. port->parent->node_id, generation,
  166. port->parent->max_speed, port->addr,
  167. port->buf, port->len, async_midi_port_callback,
  168. port);
  169. }
  170. /**
  171. * snd_fw_async_midi_port_init - initialize asynchronous MIDI port structure
  172. * @port: the asynchronous MIDI port to initialize
  173. * @unit: the target of the asynchronous transaction
  174. * @addr: the address to which transactions are transferred
  175. * @len: the length of transaction
  176. * @fill: the callback function to fill given buffer, and returns the
  177. * number of consumed bytes for MIDI message.
  178. *
  179. */
  180. int snd_fw_async_midi_port_init(struct snd_fw_async_midi_port *port,
  181. struct fw_unit *unit, u64 addr, unsigned int len,
  182. snd_fw_async_midi_port_fill fill)
  183. {
  184. port->len = DIV_ROUND_UP(len, 4) * 4;
  185. port->buf = kzalloc(port->len, GFP_KERNEL);
  186. if (port->buf == NULL)
  187. return -ENOMEM;
  188. port->parent = fw_parent_device(unit);
  189. port->addr = addr;
  190. port->fill = fill;
  191. port->idling = true;
  192. port->next_ktime = ktime_set(0, 0);
  193. port->error = false;
  194. INIT_WORK(&port->work, midi_port_work);
  195. return 0;
  196. }
  197. EXPORT_SYMBOL(snd_fw_async_midi_port_init);
  198. /**
  199. * snd_fw_async_midi_port_destroy - free asynchronous MIDI port structure
  200. * @port: the asynchronous MIDI port structure
  201. */
  202. void snd_fw_async_midi_port_destroy(struct snd_fw_async_midi_port *port)
  203. {
  204. snd_fw_async_midi_port_finish(port);
  205. cancel_work_sync(&port->work);
  206. kfree(port->buf);
  207. }
  208. EXPORT_SYMBOL(snd_fw_async_midi_port_destroy);
  209. MODULE_DESCRIPTION("FireWire audio helper functions");
  210. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  211. MODULE_LICENSE("GPL v2");