18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * fireworks_transaction.c - a part of driver for Fireworks based devices
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2013-2014 Takashi Sakamoto
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci/*
98c2ecf20Sopenharmony_ci * Fireworks have its own transaction. The transaction can be delivered by AV/C
108c2ecf20Sopenharmony_ci * Vendor Specific command frame or usual asynchronous transaction. At least,
118c2ecf20Sopenharmony_ci * Windows driver and firmware version 5.5 or later don't use AV/C command.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Transaction substance:
148c2ecf20Sopenharmony_ci *  At first, 6 data exist. Following to the data, parameters for each command
158c2ecf20Sopenharmony_ci *  exist. All of the parameters are 32 bit aligned to big endian.
168c2ecf20Sopenharmony_ci *   data[0]:	Length of transaction substance
178c2ecf20Sopenharmony_ci *   data[1]:	Transaction version
188c2ecf20Sopenharmony_ci *   data[2]:	Sequence number. This is incremented by the device
198c2ecf20Sopenharmony_ci *   data[3]:	Transaction category
208c2ecf20Sopenharmony_ci *   data[4]:	Transaction command
218c2ecf20Sopenharmony_ci *   data[5]:	Return value in response.
228c2ecf20Sopenharmony_ci *   data[6-]:	Parameters
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Transaction address:
258c2ecf20Sopenharmony_ci *  command:	0xecc000000000
268c2ecf20Sopenharmony_ci *  response:	0xecc080000000 (default)
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * I note that the address for response can be changed by command. But this
298c2ecf20Sopenharmony_ci * module uses the default address.
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci#include "./fireworks.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define MEMORY_SPACE_EFW_COMMAND	0xecc000000000ULL
348c2ecf20Sopenharmony_ci#define MEMORY_SPACE_EFW_RESPONSE	0xecc080000000ULL
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define ERROR_RETRIES 3
378c2ecf20Sopenharmony_ci#define ERROR_DELAY_MS 5
388c2ecf20Sopenharmony_ci#define EFC_TIMEOUT_MS 125
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(instances_lock);
418c2ecf20Sopenharmony_cistatic struct snd_efw *instances[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(transaction_queues_lock);
448c2ecf20Sopenharmony_cistatic LIST_HEAD(transaction_queues);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cienum transaction_queue_state {
478c2ecf20Sopenharmony_ci	STATE_PENDING,
488c2ecf20Sopenharmony_ci	STATE_BUS_RESET,
498c2ecf20Sopenharmony_ci	STATE_COMPLETE
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct transaction_queue {
538c2ecf20Sopenharmony_ci	struct list_head list;
548c2ecf20Sopenharmony_ci	struct fw_unit *unit;
558c2ecf20Sopenharmony_ci	void *buf;
568c2ecf20Sopenharmony_ci	unsigned int size;
578c2ecf20Sopenharmony_ci	u32 seqnum;
588c2ecf20Sopenharmony_ci	enum transaction_queue_state state;
598c2ecf20Sopenharmony_ci	wait_queue_head_t wait;
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ciint snd_efw_transaction_cmd(struct fw_unit *unit,
638c2ecf20Sopenharmony_ci			    const void *cmd, unsigned int size)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	return snd_fw_transaction(unit, TCODE_WRITE_BLOCK_REQUEST,
668c2ecf20Sopenharmony_ci				  MEMORY_SPACE_EFW_COMMAND,
678c2ecf20Sopenharmony_ci				  (void *)cmd, size, 0);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciint snd_efw_transaction_run(struct fw_unit *unit,
718c2ecf20Sopenharmony_ci			    const void *cmd, unsigned int cmd_size,
728c2ecf20Sopenharmony_ci			    void *resp, unsigned int resp_size)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct transaction_queue t;
758c2ecf20Sopenharmony_ci	unsigned int tries;
768c2ecf20Sopenharmony_ci	int ret;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	t.unit = unit;
798c2ecf20Sopenharmony_ci	t.buf = resp;
808c2ecf20Sopenharmony_ci	t.size = resp_size;
818c2ecf20Sopenharmony_ci	t.seqnum = be32_to_cpu(((struct snd_efw_transaction *)cmd)->seqnum) + 1;
828c2ecf20Sopenharmony_ci	t.state = STATE_PENDING;
838c2ecf20Sopenharmony_ci	init_waitqueue_head(&t.wait);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	spin_lock_irq(&transaction_queues_lock);
868c2ecf20Sopenharmony_ci	list_add_tail(&t.list, &transaction_queues);
878c2ecf20Sopenharmony_ci	spin_unlock_irq(&transaction_queues_lock);
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	tries = 0;
908c2ecf20Sopenharmony_ci	do {
918c2ecf20Sopenharmony_ci		ret = snd_efw_transaction_cmd(t.unit, (void *)cmd, cmd_size);
928c2ecf20Sopenharmony_ci		if (ret < 0)
938c2ecf20Sopenharmony_ci			break;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci		wait_event_timeout(t.wait, t.state != STATE_PENDING,
968c2ecf20Sopenharmony_ci				   msecs_to_jiffies(EFC_TIMEOUT_MS));
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci		if (t.state == STATE_COMPLETE) {
998c2ecf20Sopenharmony_ci			ret = t.size;
1008c2ecf20Sopenharmony_ci			break;
1018c2ecf20Sopenharmony_ci		} else if (t.state == STATE_BUS_RESET) {
1028c2ecf20Sopenharmony_ci			msleep(ERROR_DELAY_MS);
1038c2ecf20Sopenharmony_ci		} else if (++tries >= ERROR_RETRIES) {
1048c2ecf20Sopenharmony_ci			dev_err(&t.unit->device, "EFW transaction timed out\n");
1058c2ecf20Sopenharmony_ci			ret = -EIO;
1068c2ecf20Sopenharmony_ci			break;
1078c2ecf20Sopenharmony_ci		}
1088c2ecf20Sopenharmony_ci	} while (1);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	spin_lock_irq(&transaction_queues_lock);
1118c2ecf20Sopenharmony_ci	list_del(&t.list);
1128c2ecf20Sopenharmony_ci	spin_unlock_irq(&transaction_queues_lock);
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return ret;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_cistatic void
1188c2ecf20Sopenharmony_cicopy_resp_to_buf(struct snd_efw *efw, void *data, size_t length, int *rcode)
1198c2ecf20Sopenharmony_ci{
1208c2ecf20Sopenharmony_ci	size_t capacity, till_end;
1218c2ecf20Sopenharmony_ci	struct snd_efw_transaction *t;
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	t = (struct snd_efw_transaction *)data;
1248c2ecf20Sopenharmony_ci	length = min_t(size_t, be32_to_cpu(t->length) * sizeof(u32), length);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	spin_lock(&efw->lock);
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (efw->push_ptr < efw->pull_ptr)
1298c2ecf20Sopenharmony_ci		capacity = (unsigned int)(efw->pull_ptr - efw->push_ptr);
1308c2ecf20Sopenharmony_ci	else
1318c2ecf20Sopenharmony_ci		capacity = snd_efw_resp_buf_size -
1328c2ecf20Sopenharmony_ci			   (unsigned int)(efw->push_ptr - efw->pull_ptr);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	/* confirm enough space for this response */
1358c2ecf20Sopenharmony_ci	if (capacity < length) {
1368c2ecf20Sopenharmony_ci		*rcode = RCODE_CONFLICT_ERROR;
1378c2ecf20Sopenharmony_ci		goto end;
1388c2ecf20Sopenharmony_ci	}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	/* copy to ring buffer */
1418c2ecf20Sopenharmony_ci	while (length > 0) {
1428c2ecf20Sopenharmony_ci		till_end = snd_efw_resp_buf_size -
1438c2ecf20Sopenharmony_ci			   (unsigned int)(efw->push_ptr - efw->resp_buf);
1448c2ecf20Sopenharmony_ci		till_end = min_t(unsigned int, length, till_end);
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci		memcpy(efw->push_ptr, data, till_end);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci		efw->push_ptr += till_end;
1498c2ecf20Sopenharmony_ci		if (efw->push_ptr >= efw->resp_buf + snd_efw_resp_buf_size)
1508c2ecf20Sopenharmony_ci			efw->push_ptr -= snd_efw_resp_buf_size;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci		length -= till_end;
1538c2ecf20Sopenharmony_ci		data += till_end;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* for hwdep */
1578c2ecf20Sopenharmony_ci	wake_up(&efw->hwdep_wait);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	*rcode = RCODE_COMPLETE;
1608c2ecf20Sopenharmony_ciend:
1618c2ecf20Sopenharmony_ci	spin_unlock_irq(&efw->lock);
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic void
1658c2ecf20Sopenharmony_cihandle_resp_for_user(struct fw_card *card, int generation, int source,
1668c2ecf20Sopenharmony_ci		     void *data, size_t length, int *rcode)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct fw_device *device;
1698c2ecf20Sopenharmony_ci	struct snd_efw *efw;
1708c2ecf20Sopenharmony_ci	unsigned int i;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	spin_lock_irq(&instances_lock);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	for (i = 0; i < SNDRV_CARDS; i++) {
1758c2ecf20Sopenharmony_ci		efw = instances[i];
1768c2ecf20Sopenharmony_ci		if (efw == NULL)
1778c2ecf20Sopenharmony_ci			continue;
1788c2ecf20Sopenharmony_ci		device = fw_parent_device(efw->unit);
1798c2ecf20Sopenharmony_ci		if ((device->card != card) ||
1808c2ecf20Sopenharmony_ci		    (device->generation != generation))
1818c2ecf20Sopenharmony_ci			continue;
1828c2ecf20Sopenharmony_ci		smp_rmb();	/* node id vs. generation */
1838c2ecf20Sopenharmony_ci		if (device->node_id != source)
1848c2ecf20Sopenharmony_ci			continue;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci		break;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci	if (i == SNDRV_CARDS)
1898c2ecf20Sopenharmony_ci		goto end;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	copy_resp_to_buf(efw, data, length, rcode);
1928c2ecf20Sopenharmony_ciend:
1938c2ecf20Sopenharmony_ci	spin_unlock(&instances_lock);
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic void
1978c2ecf20Sopenharmony_cihandle_resp_for_kernel(struct fw_card *card, int generation, int source,
1988c2ecf20Sopenharmony_ci		       void *data, size_t length, int *rcode, u32 seqnum)
1998c2ecf20Sopenharmony_ci{
2008c2ecf20Sopenharmony_ci	struct fw_device *device;
2018c2ecf20Sopenharmony_ci	struct transaction_queue *t;
2028c2ecf20Sopenharmony_ci	unsigned long flags;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	spin_lock_irqsave(&transaction_queues_lock, flags);
2058c2ecf20Sopenharmony_ci	list_for_each_entry(t, &transaction_queues, list) {
2068c2ecf20Sopenharmony_ci		device = fw_parent_device(t->unit);
2078c2ecf20Sopenharmony_ci		if ((device->card != card) ||
2088c2ecf20Sopenharmony_ci		    (device->generation != generation))
2098c2ecf20Sopenharmony_ci			continue;
2108c2ecf20Sopenharmony_ci		smp_rmb();	/* node_id vs. generation */
2118c2ecf20Sopenharmony_ci		if (device->node_id != source)
2128c2ecf20Sopenharmony_ci			continue;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci		if ((t->state == STATE_PENDING) && (t->seqnum == seqnum)) {
2158c2ecf20Sopenharmony_ci			t->state = STATE_COMPLETE;
2168c2ecf20Sopenharmony_ci			t->size = min_t(unsigned int, length, t->size);
2178c2ecf20Sopenharmony_ci			memcpy(t->buf, data, t->size);
2188c2ecf20Sopenharmony_ci			wake_up(&t->wait);
2198c2ecf20Sopenharmony_ci			*rcode = RCODE_COMPLETE;
2208c2ecf20Sopenharmony_ci		}
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&transaction_queues_lock, flags);
2238c2ecf20Sopenharmony_ci}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_cistatic void
2268c2ecf20Sopenharmony_ciefw_response(struct fw_card *card, struct fw_request *request,
2278c2ecf20Sopenharmony_ci	     int tcode, int destination, int source,
2288c2ecf20Sopenharmony_ci	     int generation, unsigned long long offset,
2298c2ecf20Sopenharmony_ci	     void *data, size_t length, void *callback_data)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	int rcode, dummy;
2328c2ecf20Sopenharmony_ci	u32 seqnum;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	rcode = RCODE_TYPE_ERROR;
2358c2ecf20Sopenharmony_ci	if (length < sizeof(struct snd_efw_transaction)) {
2368c2ecf20Sopenharmony_ci		rcode = RCODE_DATA_ERROR;
2378c2ecf20Sopenharmony_ci		goto end;
2388c2ecf20Sopenharmony_ci	} else if (offset != MEMORY_SPACE_EFW_RESPONSE) {
2398c2ecf20Sopenharmony_ci		rcode = RCODE_ADDRESS_ERROR;
2408c2ecf20Sopenharmony_ci		goto end;
2418c2ecf20Sopenharmony_ci	}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	seqnum = be32_to_cpu(((struct snd_efw_transaction *)data)->seqnum);
2448c2ecf20Sopenharmony_ci	if (seqnum > SND_EFW_TRANSACTION_USER_SEQNUM_MAX + 1) {
2458c2ecf20Sopenharmony_ci		handle_resp_for_kernel(card, generation, source,
2468c2ecf20Sopenharmony_ci				       data, length, &rcode, seqnum);
2478c2ecf20Sopenharmony_ci		if (snd_efw_resp_buf_debug)
2488c2ecf20Sopenharmony_ci			handle_resp_for_user(card, generation, source,
2498c2ecf20Sopenharmony_ci					     data, length, &dummy);
2508c2ecf20Sopenharmony_ci	} else {
2518c2ecf20Sopenharmony_ci		handle_resp_for_user(card, generation, source,
2528c2ecf20Sopenharmony_ci				     data, length, &rcode);
2538c2ecf20Sopenharmony_ci	}
2548c2ecf20Sopenharmony_ciend:
2558c2ecf20Sopenharmony_ci	fw_send_response(card, request, rcode);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_civoid snd_efw_transaction_add_instance(struct snd_efw *efw)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	unsigned int i;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	spin_lock_irq(&instances_lock);
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	for (i = 0; i < SNDRV_CARDS; i++) {
2658c2ecf20Sopenharmony_ci		if (instances[i] != NULL)
2668c2ecf20Sopenharmony_ci			continue;
2678c2ecf20Sopenharmony_ci		instances[i] = efw;
2688c2ecf20Sopenharmony_ci		break;
2698c2ecf20Sopenharmony_ci	}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	spin_unlock_irq(&instances_lock);
2728c2ecf20Sopenharmony_ci}
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_civoid snd_efw_transaction_remove_instance(struct snd_efw *efw)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	unsigned int i;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	spin_lock_irq(&instances_lock);
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	for (i = 0; i < SNDRV_CARDS; i++) {
2818c2ecf20Sopenharmony_ci		if (instances[i] != efw)
2828c2ecf20Sopenharmony_ci			continue;
2838c2ecf20Sopenharmony_ci		instances[i] = NULL;
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	spin_unlock_irq(&instances_lock);
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_civoid snd_efw_transaction_bus_reset(struct fw_unit *unit)
2908c2ecf20Sopenharmony_ci{
2918c2ecf20Sopenharmony_ci	struct transaction_queue *t;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	spin_lock_irq(&transaction_queues_lock);
2948c2ecf20Sopenharmony_ci	list_for_each_entry(t, &transaction_queues, list) {
2958c2ecf20Sopenharmony_ci		if ((t->unit == unit) &&
2968c2ecf20Sopenharmony_ci		    (t->state == STATE_PENDING)) {
2978c2ecf20Sopenharmony_ci			t->state = STATE_BUS_RESET;
2988c2ecf20Sopenharmony_ci			wake_up(&t->wait);
2998c2ecf20Sopenharmony_ci		}
3008c2ecf20Sopenharmony_ci	}
3018c2ecf20Sopenharmony_ci	spin_unlock_irq(&transaction_queues_lock);
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic struct fw_address_handler resp_register_handler = {
3058c2ecf20Sopenharmony_ci	.length = SND_EFW_RESPONSE_MAXIMUM_BYTES,
3068c2ecf20Sopenharmony_ci	.address_callback = efw_response
3078c2ecf20Sopenharmony_ci};
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ciint snd_efw_transaction_register(void)
3108c2ecf20Sopenharmony_ci{
3118c2ecf20Sopenharmony_ci	static const struct fw_address_region resp_register_region = {
3128c2ecf20Sopenharmony_ci		.start	= MEMORY_SPACE_EFW_RESPONSE,
3138c2ecf20Sopenharmony_ci		.end	= MEMORY_SPACE_EFW_RESPONSE +
3148c2ecf20Sopenharmony_ci			  SND_EFW_RESPONSE_MAXIMUM_BYTES
3158c2ecf20Sopenharmony_ci	};
3168c2ecf20Sopenharmony_ci	return fw_core_add_address_handler(&resp_register_handler,
3178c2ecf20Sopenharmony_ci					   &resp_register_region);
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_civoid snd_efw_transaction_unregister(void)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	WARN_ON(!list_empty(&transaction_queues));
3238c2ecf20Sopenharmony_ci	fw_core_remove_address_handler(&resp_register_handler);
3248c2ecf20Sopenharmony_ci}
325