lib.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * miscellaneous helper functions
  4. *
  5. * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
  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. MODULE_DESCRIPTION("FireWire audio helper functions");
  62. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  63. MODULE_LICENSE("GPL");