]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - sound/firewire/fireworks/fireworks_transaction.c
ALSA: fireworks: Add transaction and some commands
[mirror_ubuntu-bionic-kernel.git] / sound / firewire / fireworks / fireworks_transaction.c
CommitLineData
bde8a8f2
TS
1/*
2 * fireworks_transaction.c - a part of driver for Fireworks based devices
3 *
4 * Copyright (c) 2013-2014 Takashi Sakamoto
5 *
6 * Licensed under the terms of the GNU General Public License, version 2.
7 */
8
9/*
10 * Fireworks have its own transaction. The transaction can be delivered by AV/C
11 * Vendor Specific command. But at least Windows driver and firmware version 5.5
12 * or later don't use it.
13 *
14 * Transaction substance:
15 * At first, 6 data exist. Following to the 6 data, parameters for each
16 * commands exists. All of parameters are 32 bit alighed to big endian.
17 * data[0]: Length of transaction substance
18 * data[1]: Transaction version
19 * data[2]: Sequence number. This is incremented by the device
20 * data[3]: transaction category
21 * data[4]: transaction command
22 * data[5]: return value in response.
23 * data[6-]: parameters
24 *
25 * Transaction address:
26 * command: 0xecc000000000
27 * response: 0xecc080000000 (default)
28 *
29 * I note that the address for response can be changed by command. But this
30 * module uses the default address.
31 */
32#include "./fireworks.h"
33
34#define MEMORY_SPACE_EFW_COMMAND 0xecc000000000
35#define MEMORY_SPACE_EFW_RESPONSE 0xecc080000000
36
37#define ERROR_RETRIES 3
38#define ERROR_DELAY_MS 5
39#define EFC_TIMEOUT_MS 125
40
41static DEFINE_SPINLOCK(transaction_queues_lock);
42static LIST_HEAD(transaction_queues);
43
44enum transaction_queue_state {
45 STATE_PENDING,
46 STATE_BUS_RESET,
47 STATE_COMPLETE
48};
49
50struct transaction_queue {
51 struct list_head list;
52 struct fw_unit *unit;
53 void *buf;
54 unsigned int size;
55 u32 seqnum;
56 enum transaction_queue_state state;
57 wait_queue_head_t wait;
58};
59
60int snd_efw_transaction_run(struct fw_unit *unit,
61 const void *cmd, unsigned int cmd_size,
62 void *resp, unsigned int resp_size)
63{
64 struct transaction_queue t;
65 unsigned int tries;
66 int ret;
67
68 t.unit = unit;
69 t.buf = resp;
70 t.size = resp_size;
71 t.seqnum = be32_to_cpu(((struct snd_efw_transaction *)cmd)->seqnum) + 1;
72 t.state = STATE_PENDING;
73 init_waitqueue_head(&t.wait);
74
75 spin_lock_irq(&transaction_queues_lock);
76 list_add_tail(&t.list, &transaction_queues);
77 spin_unlock_irq(&transaction_queues_lock);
78
79 tries = 0;
80 do {
81 ret = snd_fw_transaction(unit, TCODE_WRITE_BLOCK_REQUEST,
82 MEMORY_SPACE_EFW_COMMAND,
83 (void *)cmd, cmd_size, 0);
84 if (ret < 0)
85 break;
86
87 wait_event_timeout(t.wait, t.state != STATE_PENDING,
88 msecs_to_jiffies(EFC_TIMEOUT_MS));
89
90 if (t.state == STATE_COMPLETE) {
91 ret = t.size;
92 break;
93 } else if (t.state == STATE_BUS_RESET) {
94 msleep(ERROR_DELAY_MS);
95 } else if (++tries >= ERROR_RETRIES) {
96 dev_err(&t.unit->device, "EFW transaction timed out\n");
97 ret = -EIO;
98 break;
99 }
100 } while (1);
101
102 spin_lock_irq(&transaction_queues_lock);
103 list_del(&t.list);
104 spin_unlock_irq(&transaction_queues_lock);
105
106 return ret;
107}
108
109static void
110efw_response(struct fw_card *card, struct fw_request *request,
111 int tcode, int destination, int source,
112 int generation, unsigned long long offset,
113 void *data, size_t length, void *callback_data)
114{
115 struct fw_device *device;
116 struct transaction_queue *t;
117 unsigned long flags;
118 int rcode;
119 u32 seqnum;
120
121 rcode = RCODE_TYPE_ERROR;
122 if (length < sizeof(struct snd_efw_transaction)) {
123 rcode = RCODE_DATA_ERROR;
124 goto end;
125 } else if (offset != MEMORY_SPACE_EFW_RESPONSE) {
126 rcode = RCODE_ADDRESS_ERROR;
127 goto end;
128 }
129
130 seqnum = be32_to_cpu(((struct snd_efw_transaction *)data)->seqnum);
131
132 spin_lock_irqsave(&transaction_queues_lock, flags);
133 list_for_each_entry(t, &transaction_queues, list) {
134 device = fw_parent_device(t->unit);
135 if ((device->card != card) ||
136 (device->generation != generation))
137 continue;
138 smp_rmb(); /* node_id vs. generation */
139 if (device->node_id != source)
140 continue;
141
142 if ((t->state == STATE_PENDING) && (t->seqnum == seqnum)) {
143 t->state = STATE_COMPLETE;
144 t->size = min_t(unsigned int, length, t->size);
145 memcpy(t->buf, data, t->size);
146 wake_up(&t->wait);
147 rcode = RCODE_COMPLETE;
148 }
149 }
150 spin_unlock_irqrestore(&transaction_queues_lock, flags);
151end:
152 fw_send_response(card, request, rcode);
153}
154
155void snd_efw_transaction_bus_reset(struct fw_unit *unit)
156{
157 struct transaction_queue *t;
158
159 spin_lock_irq(&transaction_queues_lock);
160 list_for_each_entry(t, &transaction_queues, list) {
161 if ((t->unit == unit) &&
162 (t->state == STATE_PENDING)) {
163 t->state = STATE_BUS_RESET;
164 wake_up(&t->wait);
165 }
166 }
167 spin_unlock_irq(&transaction_queues_lock);
168}
169
170static struct fw_address_handler resp_register_handler = {
171 .length = SND_EFW_RESPONSE_MAXIMUM_BYTES,
172 .address_callback = efw_response
173};
174
175int snd_efw_transaction_register(void)
176{
177 static const struct fw_address_region resp_register_region = {
178 .start = MEMORY_SPACE_EFW_RESPONSE,
179 .end = MEMORY_SPACE_EFW_RESPONSE +
180 SND_EFW_RESPONSE_MAXIMUM_BYTES
181 };
182 return fw_core_add_address_handler(&resp_register_handler,
183 &resp_register_region);
184}
185
186void snd_efw_transaction_unregister(void)
187{
188 WARN_ON(!list_empty(&transaction_queues));
189 fw_core_remove_address_handler(&resp_register_handler);
190}