diff options
Diffstat (limited to 'sound/firewire/fcp.c')
-rw-r--r-- | sound/firewire/fcp.c | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/sound/firewire/fcp.c b/sound/firewire/fcp.c new file mode 100644 index 000000000000..c20bd9c8f5ab --- /dev/null +++ b/sound/firewire/fcp.c | |||
@@ -0,0 +1,223 @@ | |||
1 | /* | ||
2 | * Function Control Protocol (IEC 61883-1) helper functions | ||
3 | * | ||
4 | * Copyright (c) Clemens Ladisch <clemens@ladisch.de> | ||
5 | * Licensed under the terms of the GNU General Public License, version 2. | ||
6 | */ | ||
7 | |||
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" | ||
18 | |||
19 | #define CTS_AVC 0x00 | ||
20 | |||
21 | #define ERROR_RETRIES 3 | ||
22 | #define ERROR_DELAY_MS 5 | ||
23 | #define FCP_TIMEOUT_MS 125 | ||
24 | |||
25 | static DEFINE_SPINLOCK(transactions_lock); | ||
26 | static LIST_HEAD(transactions); | ||
27 | |||
28 | enum fcp_state { | ||
29 | STATE_PENDING, | ||
30 | STATE_BUS_RESET, | ||
31 | STATE_COMPLETE, | ||
32 | }; | ||
33 | |||
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; | ||
42 | }; | ||
43 | |||
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 | ||
53 | * | ||
54 | * This function sends a FCP command frame to the target and waits for the | ||
55 | * corresponding response frame to be returned. | ||
56 | * | ||
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. | ||
61 | * | ||
62 | * @command and @response can point to the same buffer. | ||
63 | * | ||
64 | * Asynchronous operation (INTERIM, NOTIFY) is not supported at the moment. | ||
65 | * | ||
66 | * Returns the actual size of the response frame, or a negative error code. | ||
67 | */ | ||
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) | ||
72 | { | ||
73 | struct fcp_transaction t; | ||
74 | int tcode, ret, tries = 0; | ||
75 | |||
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); | ||
82 | |||
83 | spin_lock_irq(&transactions_lock); | ||
84 | list_add_tail(&t.list, &transactions); | ||
85 | spin_unlock_irq(&transactions_lock); | ||
86 | |||
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; | ||
95 | |||
96 | wait_event_timeout(t.wait, t.state != STATE_PENDING, | ||
97 | msecs_to_jiffies(FCP_TIMEOUT_MS)); | ||
98 | |||
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; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | spin_lock_irq(&transactions_lock); | ||
112 | list_del(&t.list); | ||
113 | spin_unlock_irq(&transactions_lock); | ||
114 | |||
115 | return ret; | ||
116 | } | ||
117 | EXPORT_SYMBOL(fcp_avc_transaction); | ||
118 | |||
119 | /** | ||
120 | * fcp_bus_reset - inform the target handler about a bus reset | ||
121 | * @unit: the unit that might be used by fcp_avc_transaction() | ||
122 | * | ||
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. | ||
126 | */ | ||
127 | void fcp_bus_reset(struct fw_unit *unit) | ||
128 | { | ||
129 | struct fcp_transaction *t; | ||
130 | |||
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); | ||
137 | } | ||
138 | } | ||
139 | spin_unlock_irq(&transactions_lock); | ||
140 | } | ||
141 | EXPORT_SYMBOL(fcp_bus_reset); | ||
142 | |||
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) | ||
146 | { | ||
147 | const u8 *p1, *p2; | ||
148 | unsigned int mask, i; | ||
149 | |||
150 | p1 = response; | ||
151 | p2 = transaction->response_buffer; | ||
152 | mask = transaction->response_match_bytes; | ||
153 | |||
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; | ||
162 | } | ||
163 | } | ||
164 | |||
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) | ||
169 | { | ||
170 | struct fcp_transaction *t; | ||
171 | unsigned long flags; | ||
172 | |||
173 | if (length < 1 || (*(const u8 *)data & 0xf0) != CTS_AVC) | ||
174 | return; | ||
175 | |||
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; | ||
185 | |||
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); | ||
193 | } | ||
194 | } | ||
195 | spin_unlock_irqrestore(&transactions_lock, flags); | ||
196 | } | ||
197 | |||
198 | static struct fw_address_handler response_register_handler = { | ||
199 | .length = 0x200, | ||
200 | .address_callback = fcp_response, | ||
201 | }; | ||
202 | |||
203 | static int __init fcp_module_init(void) | ||
204 | { | ||
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, | ||
208 | }; | ||
209 | |||
210 | fw_core_add_address_handler(&response_register_handler, | ||
211 | &response_register_region); | ||
212 | |||
213 | return 0; | ||
214 | } | ||
215 | |||
216 | static void __exit fcp_module_exit(void) | ||
217 | { | ||
218 | WARN_ON(!list_empty(&transactions)); | ||
219 | fw_core_remove_address_handler(&response_register_handler); | ||
220 | } | ||
221 | |||
222 | module_init(fcp_module_init); | ||
223 | module_exit(fcp_module_exit); | ||