lib.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 "lib.h"
  12. #define ERROR_RETRY_DELAY_MS 20
  13. /**
  14. * snd_fw_transaction - send a request and wait for its completion
  15. * @unit: the driver's unit on the target device
  16. * @tcode: the transaction code
  17. * @offset: the address in the target's address space
  18. * @buffer: input/output data
  19. * @length: length of @buffer
  20. * @flags: use %FW_FIXED_GENERATION and add the generation value to attempt the
  21. * request only in that generation; use %FW_QUIET to suppress error
  22. * messages
  23. *
  24. * Submits an asynchronous request to the target device, and waits for the
  25. * response. The node ID and the current generation are derived from @unit.
  26. * On a bus reset or an error, the transaction is retried a few times.
  27. * Returns zero on success, or a negative error code.
  28. */
  29. int snd_fw_transaction(struct fw_unit *unit, int tcode,
  30. u64 offset, void *buffer, size_t length,
  31. unsigned int flags)
  32. {
  33. struct fw_device *device = fw_parent_device(unit);
  34. int generation, rcode, tries = 0;
  35. generation = flags & FW_GENERATION_MASK;
  36. for (;;) {
  37. if (!(flags & FW_FIXED_GENERATION)) {
  38. generation = device->generation;
  39. smp_rmb(); /* node_id vs. generation */
  40. }
  41. rcode = fw_run_transaction(device->card, tcode,
  42. device->node_id, generation,
  43. device->max_speed, offset,
  44. buffer, length);
  45. if (rcode == RCODE_COMPLETE)
  46. return 0;
  47. if (rcode == RCODE_GENERATION && (flags & FW_FIXED_GENERATION))
  48. return -EAGAIN;
  49. if (rcode_is_permanent_error(rcode) || ++tries >= 3) {
  50. if (!(flags & FW_QUIET))
  51. dev_err(&unit->device,
  52. "transaction failed: %s\n",
  53. fw_rcode_string(rcode));
  54. return -EIO;
  55. }
  56. msleep(ERROR_RETRY_DELAY_MS);
  57. }
  58. }
  59. EXPORT_SYMBOL(snd_fw_transaction);
  60. MODULE_DESCRIPTION("FireWire audio helper functions");
  61. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  62. MODULE_LICENSE("GPL v2");