^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * miscellaneous helper functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/firewire.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "lib.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define ERROR_RETRY_DELAY_MS 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * snd_fw_transaction - send a request and wait for its completion
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * @unit: the driver's unit on the target device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * @tcode: the transaction code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * @offset: the address in the target's address space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @buffer: input/output data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @length: length of @buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @flags: use %FW_FIXED_GENERATION and add the generation value to attempt the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * request only in that generation; use %FW_QUIET to suppress error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * Submits an asynchronous request to the target device, and waits for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * response. The node ID and the current generation are derived from @unit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * On a bus reset or an error, the transaction is retried a few times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * Returns zero on success, or a negative error code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int snd_fw_transaction(struct fw_unit *unit, int tcode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u64 offset, void *buffer, size_t length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct fw_device *device = fw_parent_device(unit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) int generation, rcode, tries = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) generation = flags & FW_GENERATION_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (!(flags & FW_FIXED_GENERATION)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) generation = device->generation;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) smp_rmb(); /* node_id vs. generation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) rcode = fw_run_transaction(device->card, tcode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) device->node_id, generation,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) device->max_speed, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) buffer, length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (rcode == RCODE_COMPLETE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (rcode == RCODE_GENERATION && (flags & FW_FIXED_GENERATION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (rcode_is_permanent_error(rcode) || ++tries >= 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!(flags & FW_QUIET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) dev_err(&unit->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) "transaction failed: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) fw_rcode_string(rcode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) msleep(ERROR_RETRY_DELAY_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) EXPORT_SYMBOL(snd_fw_transaction);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define PROBE_DELAY_MS (2 * MSEC_PER_SEC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * snd_fw_schedule_registration - schedule work for sound card registration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * @unit: an instance for unit on IEEE 1394 bus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * @dwork: delayed work with callback function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * This function is not designed for general purposes. When new unit is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * connected to IEEE 1394 bus, the bus is under bus-reset state because of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * topological change. In this state, units tend to fail both of asynchronous
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * and isochronous communication. To avoid this problem, this function is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * to postpone sound card registration after the state. The callers must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * set up instance of delayed work in advance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) void snd_fw_schedule_registration(struct fw_unit *unit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct delayed_work *dwork)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) u64 now, delay;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) now = get_jiffies_64();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) delay = fw_parent_device(unit)->card->reset_jiffies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) + msecs_to_jiffies(PROBE_DELAY_MS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (time_after64(delay, now))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) delay -= now;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) delay = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) mod_delayed_work(system_wq, dwork, delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) EXPORT_SYMBOL(snd_fw_schedule_registration);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) MODULE_DESCRIPTION("FireWire audio helper functions");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) MODULE_LICENSE("GPL v2");