ALSA: add LaCie FireWire Speakers/Griffin FireWave Surround driver
[firewire-audio.git] / sound / firewire / fcp.c
blobc20bd9c8f5ab28e1eac852f2c6a5b1fbe304d8e6
1 /*
2 * Function Control Protocol (IEC 61883-1) helper functions
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
6 */
8 #include <linux/device.h>
9 #include <linux/firewire.h>
10 #include <linux/firewire-constants.h>
11 #include <linux/list.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/spinlock.h>
15 #include <linux/wait.h>
16 #include "fcp.h"
17 #include "lib.h"
19 #define CTS_AVC 0x00
21 #define ERROR_RETRIES 3
22 #define ERROR_DELAY_MS 5
23 #define FCP_TIMEOUT_MS 125
25 static DEFINE_SPINLOCK(transactions_lock);
26 static LIST_HEAD(transactions);
28 enum fcp_state {
29 STATE_PENDING,
30 STATE_BUS_RESET,
31 STATE_COMPLETE,
34 struct fcp_transaction {
35 struct list_head list;
36 struct fw_unit *unit;
37 void *response_buffer;
38 unsigned int response_size;
39 unsigned int response_match_bytes;
40 enum fcp_state state;
41 wait_queue_head_t wait;
44 /**
45 * fcp_avc_transaction - send an AV/C command and wait for its response
46 * @unit: a unit on the target device
47 * @command: a buffer containing the command frame; must be DMA-able
48 * @command_size: the size of @command
49 * @response: a buffer for the response frame
50 * @response_size: the maximum size of @response
51 * @response_match_bytes: a bitmap specifying the bytes used to detect the
52 * correct response frame
54 * This function sends a FCP command frame to the target and waits for the
55 * corresponding response frame to be returned.
57 * Because it is possible for multiple FCP transactions to be active at the
58 * same time, the correct response frame is detected by the value of certain
59 * bytes. These bytes must be set in @response before calling this function,
60 * and the corresponding bits must be set in @response_match_bytes.
62 * @command and @response can point to the same buffer.
64 * Asynchronous operation (INTERIM, NOTIFY) is not supported at the moment.
66 * Returns the actual size of the response frame, or a negative error code.
68 int fcp_avc_transaction(struct fw_unit *unit,
69 const void *command, unsigned int command_size,
70 void *response, unsigned int response_size,
71 unsigned int response_match_bytes)
73 struct fcp_transaction t;
74 int tcode, ret, tries = 0;
76 t.unit = unit;
77 t.response_buffer = response;
78 t.response_size = response_size;
79 t.response_match_bytes = response_match_bytes;
80 t.state = STATE_PENDING;
81 init_waitqueue_head(&t.wait);
83 spin_lock_irq(&transactions_lock);
84 list_add_tail(&t.list, &transactions);
85 spin_unlock_irq(&transactions_lock);
87 for (;;) {
88 tcode = command_size == 4 ? TCODE_WRITE_QUADLET_REQUEST
89 : TCODE_WRITE_BLOCK_REQUEST;
90 ret = snd_fw_transaction(t.unit, tcode,
91 CSR_REGISTER_BASE + CSR_FCP_COMMAND,
92 (void *)command, command_size);
93 if (ret < 0)
94 break;
96 wait_event_timeout(t.wait, t.state != STATE_PENDING,
97 msecs_to_jiffies(FCP_TIMEOUT_MS));
99 if (t.state == STATE_COMPLETE) {
100 ret = t.response_size;
101 break;
102 } else if (t.state == STATE_BUS_RESET) {
103 msleep(ERROR_DELAY_MS);
104 } else if (++tries >= ERROR_RETRIES) {
105 dev_err(&t.unit->device, "FCP command timed out\n");
106 ret = -EIO;
107 break;
111 spin_lock_irq(&transactions_lock);
112 list_del(&t.list);
113 spin_unlock_irq(&transactions_lock);
115 return ret;
117 EXPORT_SYMBOL(fcp_avc_transaction);
120 * fcp_bus_reset - inform the target handler about a bus reset
121 * @unit: the unit that might be used by fcp_avc_transaction()
123 * This function must be called from the driver's .update handler to inform
124 * the FCP transaction handler that a bus reset has happened. Any pending FCP
125 * transactions are retried.
127 void fcp_bus_reset(struct fw_unit *unit)
129 struct fcp_transaction *t;
131 spin_lock_irq(&transactions_lock);
132 list_for_each_entry(t, &transactions, list) {
133 if (t->unit == unit &&
134 t->state == STATE_PENDING) {
135 t->state = STATE_BUS_RESET;
136 wake_up(&t->wait);
139 spin_unlock_irq(&transactions_lock);
141 EXPORT_SYMBOL(fcp_bus_reset);
143 /* checks whether the response matches the masked bytes in response_buffer */
144 static bool is_matching_response(struct fcp_transaction *transaction,
145 const void *response, size_t length)
147 const u8 *p1, *p2;
148 unsigned int mask, i;
150 p1 = response;
151 p2 = transaction->response_buffer;
152 mask = transaction->response_match_bytes;
154 for (i = 0; ; ++i) {
155 if ((mask & 1) && p1[i] != p2[i])
156 return false;
157 mask >>= 1;
158 if (!mask)
159 return true;
160 if (--length == 0)
161 return false;
165 static void fcp_response(struct fw_card *card, struct fw_request *request,
166 int tcode, int destination, int source,
167 int generation, unsigned long long offset,
168 void *data, size_t length, void *callback_data)
170 struct fcp_transaction *t;
171 unsigned long flags;
173 if (length < 1 || (*(const u8 *)data & 0xf0) != CTS_AVC)
174 return;
176 spin_lock_irqsave(&transactions_lock, flags);
177 list_for_each_entry(t, &transactions, list) {
178 struct fw_device *device = fw_parent_device(t->unit);
179 if (device->card != card ||
180 device->generation != generation)
181 continue;
182 smp_rmb(); /* node_id vs. generation */
183 if (device->node_id != source)
184 continue;
186 if (t->state == STATE_PENDING &&
187 is_matching_response(t, data, length)) {
188 t->state = STATE_COMPLETE;
189 t->response_size = min((unsigned int)length,
190 t->response_size);
191 memcpy(t->response_buffer, data, t->response_size);
192 wake_up(&t->wait);
195 spin_unlock_irqrestore(&transactions_lock, flags);
198 static struct fw_address_handler response_register_handler = {
199 .length = 0x200,
200 .address_callback = fcp_response,
203 static int __init fcp_module_init(void)
205 static const struct fw_address_region response_register_region = {
206 .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
207 .end = CSR_REGISTER_BASE + CSR_FCP_END,
210 fw_core_add_address_handler(&response_register_handler,
211 &response_register_region);
213 return 0;
216 static void __exit fcp_module_exit(void)
218 WARN_ON(!list_empty(&transactions));
219 fw_core_remove_address_handler(&response_register_handler);
222 module_init(fcp_module_init);
223 module_exit(fcp_module_exit);