xref: /openbmc/linux/sound/firewire/lib.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
231ef9134SClemens Ladisch /*
331ef9134SClemens Ladisch  * miscellaneous helper functions
431ef9134SClemens Ladisch  *
531ef9134SClemens Ladisch  * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
631ef9134SClemens Ladisch  */
731ef9134SClemens Ladisch 
831ef9134SClemens Ladisch #include <linux/delay.h>
931ef9134SClemens Ladisch #include <linux/device.h>
1031ef9134SClemens Ladisch #include <linux/firewire.h>
1131ef9134SClemens Ladisch #include <linux/module.h>
12585d7cbaSTakashi Sakamoto #include <linux/slab.h>
1331ef9134SClemens Ladisch #include "lib.h"
1431ef9134SClemens Ladisch 
151b70485fSClemens Ladisch #define ERROR_RETRY_DELAY_MS	20
1631ef9134SClemens Ladisch 
1731ef9134SClemens Ladisch /**
1831ef9134SClemens Ladisch  * snd_fw_transaction - send a request and wait for its completion
1931ef9134SClemens Ladisch  * @unit: the driver's unit on the target device
2031ef9134SClemens Ladisch  * @tcode: the transaction code
2131ef9134SClemens Ladisch  * @offset: the address in the target's address space
2231ef9134SClemens Ladisch  * @buffer: input/output data
2331ef9134SClemens Ladisch  * @length: length of @buffer
241b70485fSClemens Ladisch  * @flags: use %FW_FIXED_GENERATION and add the generation value to attempt the
251b70485fSClemens Ladisch  *         request only in that generation; use %FW_QUIET to suppress error
261b70485fSClemens Ladisch  *         messages
2731ef9134SClemens Ladisch  *
2831ef9134SClemens Ladisch  * Submits an asynchronous request to the target device, and waits for the
2931ef9134SClemens Ladisch  * response.  The node ID and the current generation are derived from @unit.
3031ef9134SClemens Ladisch  * On a bus reset or an error, the transaction is retried a few times.
3131ef9134SClemens Ladisch  * Returns zero on success, or a negative error code.
3231ef9134SClemens Ladisch  */
snd_fw_transaction(struct fw_unit * unit,int tcode,u64 offset,void * buffer,size_t length,unsigned int flags)3331ef9134SClemens Ladisch int snd_fw_transaction(struct fw_unit *unit, int tcode,
341b70485fSClemens Ladisch 		       u64 offset, void *buffer, size_t length,
351b70485fSClemens Ladisch 		       unsigned int flags)
3631ef9134SClemens Ladisch {
3731ef9134SClemens Ladisch 	struct fw_device *device = fw_parent_device(unit);
3831ef9134SClemens Ladisch 	int generation, rcode, tries = 0;
3931ef9134SClemens Ladisch 
401b70485fSClemens Ladisch 	generation = flags & FW_GENERATION_MASK;
4131ef9134SClemens Ladisch 	for (;;) {
421b70485fSClemens Ladisch 		if (!(flags & FW_FIXED_GENERATION)) {
4331ef9134SClemens Ladisch 			generation = device->generation;
4431ef9134SClemens Ladisch 			smp_rmb(); /* node_id vs. generation */
451b70485fSClemens Ladisch 		}
4631ef9134SClemens Ladisch 		rcode = fw_run_transaction(device->card, tcode,
4731ef9134SClemens Ladisch 					   device->node_id, generation,
4831ef9134SClemens Ladisch 					   device->max_speed, offset,
4931ef9134SClemens Ladisch 					   buffer, length);
5031ef9134SClemens Ladisch 
5131ef9134SClemens Ladisch 		if (rcode == RCODE_COMPLETE)
5231ef9134SClemens Ladisch 			return 0;
5331ef9134SClemens Ladisch 
541b70485fSClemens Ladisch 		if (rcode == RCODE_GENERATION && (flags & FW_FIXED_GENERATION))
551b70485fSClemens Ladisch 			return -EAGAIN;
561b70485fSClemens Ladisch 
5731ef9134SClemens Ladisch 		if (rcode_is_permanent_error(rcode) || ++tries >= 3) {
581b70485fSClemens Ladisch 			if (!(flags & FW_QUIET))
591b70485fSClemens Ladisch 				dev_err(&unit->device,
601b70485fSClemens Ladisch 					"transaction failed: %s\n",
617bdbff67SClemens Ladisch 					fw_rcode_string(rcode));
6231ef9134SClemens Ladisch 			return -EIO;
6331ef9134SClemens Ladisch 		}
6431ef9134SClemens Ladisch 
6531ef9134SClemens Ladisch 		msleep(ERROR_RETRY_DELAY_MS);
6631ef9134SClemens Ladisch 	}
6731ef9134SClemens Ladisch }
6831ef9134SClemens Ladisch EXPORT_SYMBOL(snd_fw_transaction);
6931ef9134SClemens Ladisch 
7031ef9134SClemens Ladisch MODULE_DESCRIPTION("FireWire audio helper functions");
7131ef9134SClemens Ladisch MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
72*9b446941STakashi Sakamoto MODULE_LICENSE("GPL");
73