File Index
Files and directories
Sign in
FILE

fireworks_transaction.c

File preview with syntax highlighting.

linux-6.19.14/sound/firewire/fireworks/fireworks_transaction.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * fireworks_transaction.c - a part of driver for Fireworks based devices
  4 *
  5 * Copyright (c) 2013-2014 Takashi Sakamoto
  6 */
  7
  8/*
  9 * Fireworks have its own transaction. The transaction can be delivered by AV/C
 10 * Vendor Specific command frame or usual asynchronous transaction. At least,
 11 * Windows driver and firmware version 5.5 or later don't use AV/C command.
 12 *
 13 * Transaction substance:
 14 *  At first, 6 data exist. Following to the data, parameters for each command
 15 *  exist. All of the parameters are 32 bit aligned to big endian.
 16 *   data[0]:	Length of transaction substance
 17 *   data[1]:	Transaction version
 18 *   data[2]:	Sequence number. This is incremented by the device
 19 *   data[3]:	Transaction category
 20 *   data[4]:	Transaction command
 21 *   data[5]:	Return value in response.
 22 *   data[6-]:	Parameters
 23 *
 24 * Transaction address:
 25 *  command:	0xecc000000000
 26 *  response:	0xecc080000000 (default)
 27 *
 28 * I note that the address for response can be changed by command. But this
 29 * module uses the default address.
 30 */
 31#include "./fireworks.h"
 32
 33#define MEMORY_SPACE_EFW_COMMAND	0xecc000000000ULL
 34#define MEMORY_SPACE_EFW_RESPONSE	0xecc080000000ULL
 35
 36#define ERROR_RETRIES 3
 37#define ERROR_DELAY_MS 5
 38#define EFC_TIMEOUT_MS 125
 39
 40static DEFINE_SPINLOCK(instances_lock);
 41static struct snd_efw *instances[SNDRV_CARDS] = SNDRV_DEFAULT_PTR;
 42
 43static DEFINE_SPINLOCK(transaction_queues_lock);
 44static LIST_HEAD(transaction_queues);
 45
 46enum transaction_queue_state {
 47	STATE_PENDING,
 48	STATE_BUS_RESET,
 49	STATE_COMPLETE
 50};
 51
 52struct transaction_queue {
 53	struct list_head list;
 54	struct fw_unit *unit;
 55	void *buf;
 56	unsigned int size;
 57	u32 seqnum;
 58	enum transaction_queue_state state;
 59	wait_queue_head_t wait;
 60};
 61
 62int snd_efw_transaction_cmd(struct fw_unit *unit,
 63			    const void *cmd, unsigned int size)
 64{
 65	return snd_fw_transaction(unit, TCODE_WRITE_BLOCK_REQUEST,
 66				  MEMORY_SPACE_EFW_COMMAND,
 67				  (void *)cmd, size, 0);
 68}
 69
 70int snd_efw_transaction_run(struct fw_unit *unit,
 71			    const void *cmd, unsigned int cmd_size,
 72			    void *resp, unsigned int resp_size)
 73{
 74	struct transaction_queue t;
 75	unsigned int tries;
 76	int ret;
 77
 78	t.unit = unit;
 79	t.buf = resp;
 80	t.size = resp_size;
 81	t.seqnum = be32_to_cpu(((struct snd_efw_transaction *)cmd)->seqnum) + 1;
 82	t.state = STATE_PENDING;
 83	init_waitqueue_head(&t.wait);
 84
 85	scoped_guard(spinlock_irq, &transaction_queues_lock) {
 86		list_add_tail(&t.list, &transaction_queues);
 87	}
 88
 89	tries = 0;
 90	do {
 91		ret = snd_efw_transaction_cmd(t.unit, (void *)cmd, cmd_size);
 92		if (ret < 0)
 93			break;
 94
 95		wait_event_timeout(t.wait, t.state != STATE_PENDING,
 96				   msecs_to_jiffies(EFC_TIMEOUT_MS));
 97
 98		if (t.state == STATE_COMPLETE) {
 99			ret = t.size;
100			break;
101		} else if (t.state == STATE_BUS_RESET) {
102			msleep(ERROR_DELAY_MS);
103		} else if (++tries >= ERROR_RETRIES) {
104			dev_err(&t.unit->device, "EFW transaction timed out\n");
105			ret = -EIO;
106			break;
107		}
108	} while (1);
109
110	scoped_guard(spinlock_irq, &transaction_queues_lock) {
111		list_del(&t.list);
112	}
113
114	return ret;
115}
116
117static void
118copy_resp_to_buf(struct snd_efw *efw, void *data, size_t length, int *rcode)
119{
120	size_t capacity, till_end;
121	struct snd_efw_transaction *t;
122
123	t = (struct snd_efw_transaction *)data;
124	length = min_t(size_t, be32_to_cpu(t->length) * sizeof(u32), length);
125
126	guard(spinlock)(&efw->lock);
127
128	if (efw->push_ptr < efw->pull_ptr)
129		capacity = (unsigned int)(efw->pull_ptr - efw->push_ptr);
130	else
131		capacity = snd_efw_resp_buf_size -
132			   (unsigned int)(efw->push_ptr - efw->pull_ptr);
133
134	/* confirm enough space for this response */
135	if (capacity < length) {
136		*rcode = RCODE_CONFLICT_ERROR;
137		return;
138	}
139
140	/* copy to ring buffer */
141	while (length > 0) {
142		till_end = snd_efw_resp_buf_size -
143			   (unsigned int)(efw->push_ptr - efw->resp_buf);
144		till_end = min_t(unsigned int, length, till_end);
145
146		memcpy(efw->push_ptr, data, till_end);
147
148		efw->push_ptr += till_end;
149		if (efw->push_ptr >= efw->resp_buf + snd_efw_resp_buf_size)
150			efw->push_ptr -= snd_efw_resp_buf_size;
151
152		length -= till_end;
153		data += till_end;
154	}
155
156	/* for hwdep */
157	wake_up(&efw->hwdep_wait);
158
159	*rcode = RCODE_COMPLETE;
160}
161
162static void
163handle_resp_for_user(struct fw_card *card, int generation, int source,
164		     void *data, size_t length, int *rcode)
165{
166	struct fw_device *device;
167	struct snd_efw *efw;
168	unsigned int i;
169
170	guard(spinlock_irq)(&instances_lock);
171
172	for (i = 0; i < SNDRV_CARDS; i++) {
173		efw = instances[i];
174		if (efw == NULL)
175			continue;
176		device = fw_parent_device(efw->unit);
177		if ((device->card != card) ||
178		    (device->generation != generation))
179			continue;
180		smp_rmb();	/* node id vs. generation */
181		if (device->node_id != source)
182			continue;
183
184		break;
185	}
186	if (i == SNDRV_CARDS)
187		return;
188
189	copy_resp_to_buf(efw, data, length, rcode);
190}
191
192static void
193handle_resp_for_kernel(struct fw_card *card, int generation, int source,
194		       void *data, size_t length, int *rcode, u32 seqnum)
195{
196	struct fw_device *device;
197	struct transaction_queue *t;
198
199	guard(spinlock_irqsave)(&transaction_queues_lock);
200	list_for_each_entry(t, &transaction_queues, list) {
201		device = fw_parent_device(t->unit);
202		if ((device->card != card) ||
203		    (device->generation != generation))
204			continue;
205		smp_rmb();	/* node_id vs. generation */
206		if (device->node_id != source)
207			continue;
208
209		if ((t->state == STATE_PENDING) && (t->seqnum == seqnum)) {
210			t->state = STATE_COMPLETE;
211			t->size = min_t(unsigned int, length, t->size);
212			memcpy(t->buf, data, t->size);
213			wake_up(&t->wait);
214			*rcode = RCODE_COMPLETE;
215		}
216	}
217}
218
219static void
220efw_response(struct fw_card *card, struct fw_request *request,
221	     int tcode, int destination, int source,
222	     int generation, unsigned long long offset,
223	     void *data, size_t length, void *callback_data)
224{
225	int rcode, dummy;
226	u32 seqnum;
227
228	rcode = RCODE_TYPE_ERROR;
229	if (length < sizeof(struct snd_efw_transaction)) {
230		rcode = RCODE_DATA_ERROR;
231		goto end;
232	} else if (offset != MEMORY_SPACE_EFW_RESPONSE) {
233		rcode = RCODE_ADDRESS_ERROR;
234		goto end;
235	}
236
237	seqnum = be32_to_cpu(((struct snd_efw_transaction *)data)->seqnum);
238	if (seqnum > SND_EFW_TRANSACTION_USER_SEQNUM_MAX + 1) {
239		handle_resp_for_kernel(card, generation, source,
240				       data, length, &rcode, seqnum);
241		if (snd_efw_resp_buf_debug)
242			handle_resp_for_user(card, generation, source,
243					     data, length, &dummy);
244	} else {
245		handle_resp_for_user(card, generation, source,
246				     data, length, &rcode);
247	}
248end:
249	fw_send_response(card, request, rcode);
250}
251
252void snd_efw_transaction_add_instance(struct snd_efw *efw)
253{
254	unsigned int i;
255
256	guard(spinlock_irq)(&instances_lock);
257
258	for (i = 0; i < SNDRV_CARDS; i++) {
259		if (instances[i] != NULL)
260			continue;
261		instances[i] = efw;
262		break;
263	}
264}
265
266void snd_efw_transaction_remove_instance(struct snd_efw *efw)
267{
268	unsigned int i;
269
270	guard(spinlock_irq)(&instances_lock);
271
272	for (i = 0; i < SNDRV_CARDS; i++) {
273		if (instances[i] != efw)
274			continue;
275		instances[i] = NULL;
276	}
277}
278
279void snd_efw_transaction_bus_reset(struct fw_unit *unit)
280{
281	struct transaction_queue *t;
282
283	guard(spinlock_irq)(&transaction_queues_lock);
284	list_for_each_entry(t, &transaction_queues, list) {
285		if ((t->unit == unit) &&
286		    (t->state == STATE_PENDING)) {
287			t->state = STATE_BUS_RESET;
288			wake_up(&t->wait);
289		}
290	}
291}
292
293static struct fw_address_handler resp_register_handler = {
294	.length = SND_EFW_RESPONSE_MAXIMUM_BYTES,
295	.address_callback = efw_response
296};
297
298int snd_efw_transaction_register(void)
299{
300	static const struct fw_address_region resp_register_region = {
301		.start	= MEMORY_SPACE_EFW_RESPONSE,
302		.end	= MEMORY_SPACE_EFW_RESPONSE +
303			  SND_EFW_RESPONSE_MAXIMUM_BYTES
304	};
305	return fw_core_add_address_handler(&resp_register_handler,
306					   &resp_register_region);
307}
308
309void snd_efw_transaction_unregister(void)
310{
311	WARN_ON(!list_empty(&transaction_queues));
312	fw_core_remove_address_handler(&resp_register_handler);
313}
~/ Read-only file preview.