lib.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. MODULE_DESCRIPTION("FireWire audio helper functions");
  89. MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
  90. MODULE_LICENSE("GPL v2");