6876bfa9f27d4dce07d1da3861c242183d250e3f
[firefly-linux-kernel-4.4.55.git] / sound / firewire / fcp.c
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 <linux/delay.h>
17 #include "fcp.h"
18 #include "lib.h"
19
20 #define CTS_AVC 0x00
21
22 #define ERROR_RETRIES   3
23 #define ERROR_DELAY_MS  5
24 #define FCP_TIMEOUT_MS  125
25
26 static DEFINE_SPINLOCK(transactions_lock);
27 static LIST_HEAD(transactions);
28
29 enum fcp_state {
30         STATE_PENDING,
31         STATE_BUS_RESET,
32         STATE_COMPLETE,
33         STATE_DEFERRED,
34 };
35
36 struct fcp_transaction {
37         struct list_head list;
38         struct fw_unit *unit;
39         void *response_buffer;
40         unsigned int response_size;
41         unsigned int response_match_bytes;
42         enum fcp_state state;
43         wait_queue_head_t wait;
44         bool deferrable;
45 };
46
47 /**
48  * fcp_avc_transaction - send an AV/C command and wait for its response
49  * @unit: a unit on the target device
50  * @command: a buffer containing the command frame; must be DMA-able
51  * @command_size: the size of @command
52  * @response: a buffer for the response frame
53  * @response_size: the maximum size of @response
54  * @response_match_bytes: a bitmap specifying the bytes used to detect the
55  *                        correct response frame
56  *
57  * This function sends a FCP command frame to the target and waits for the
58  * corresponding response frame to be returned.
59  *
60  * Because it is possible for multiple FCP transactions to be active at the
61  * same time, the correct response frame is detected by the value of certain
62  * bytes.  These bytes must be set in @response before calling this function,
63  * and the corresponding bits must be set in @response_match_bytes.
64  *
65  * @command and @response can point to the same buffer.
66  *
67  * Asynchronous operation (INTERIM, NOTIFY) is not supported at the moment.
68  *
69  * Returns the actual size of the response frame, or a negative error code.
70  */
71 int fcp_avc_transaction(struct fw_unit *unit,
72                         const void *command, unsigned int command_size,
73                         void *response, unsigned int response_size,
74                         unsigned int response_match_bytes)
75 {
76         struct fcp_transaction t;
77         int tcode, ret, tries = 0;
78
79         t.unit = unit;
80         t.response_buffer = response;
81         t.response_size = response_size;
82         t.response_match_bytes = response_match_bytes;
83         t.state = STATE_PENDING;
84         init_waitqueue_head(&t.wait);
85
86         if (*(const u8 *)command == 0x00 || *(const u8 *)command == 0x03)
87                 t.deferrable = true;
88
89         spin_lock_irq(&transactions_lock);
90         list_add_tail(&t.list, &transactions);
91         spin_unlock_irq(&transactions_lock);
92
93         for (;;) {
94                 tcode = command_size == 4 ? TCODE_WRITE_QUADLET_REQUEST
95                                           : TCODE_WRITE_BLOCK_REQUEST;
96                 ret = snd_fw_transaction(t.unit, tcode,
97                                          CSR_REGISTER_BASE + CSR_FCP_COMMAND,
98                                          (void *)command, command_size, 0);
99                 if (ret < 0)
100                         break;
101 deferred:
102                 wait_event_timeout(t.wait, t.state != STATE_PENDING,
103                                    msecs_to_jiffies(FCP_TIMEOUT_MS));
104
105                 if (t.state == STATE_DEFERRED) {
106                         /*
107                          * 'AV/C General Specification' define no time limit
108                          * on command completion once an INTERIM response has
109                          * been sent. but we promise to finish this function
110                          * for a caller. Here we use FCP_TIMEOUT_MS for next
111                          * interval. This is not in the specification.
112                          */
113                         t.state = STATE_PENDING;
114                         goto deferred;
115                 } else if (t.state == STATE_COMPLETE) {
116                         ret = t.response_size;
117                         break;
118                 } else if (t.state == STATE_BUS_RESET) {
119                         msleep(ERROR_DELAY_MS);
120                 } else if (++tries >= ERROR_RETRIES) {
121                         dev_err(&t.unit->device, "FCP command timed out\n");
122                         ret = -EIO;
123                         break;
124                 }
125         }
126
127         spin_lock_irq(&transactions_lock);
128         list_del(&t.list);
129         spin_unlock_irq(&transactions_lock);
130
131         return ret;
132 }
133 EXPORT_SYMBOL(fcp_avc_transaction);
134
135 /**
136  * fcp_bus_reset - inform the target handler about a bus reset
137  * @unit: the unit that might be used by fcp_avc_transaction()
138  *
139  * This function must be called from the driver's .update handler to inform
140  * the FCP transaction handler that a bus reset has happened.  Any pending FCP
141  * transactions are retried.
142  */
143 void fcp_bus_reset(struct fw_unit *unit)
144 {
145         struct fcp_transaction *t;
146
147         spin_lock_irq(&transactions_lock);
148         list_for_each_entry(t, &transactions, list) {
149                 if (t->unit == unit &&
150                     (t->state == STATE_PENDING ||
151                      t->state == STATE_DEFERRED)) {
152                         t->state = STATE_BUS_RESET;
153                         wake_up(&t->wait);
154                 }
155         }
156         spin_unlock_irq(&transactions_lock);
157 }
158 EXPORT_SYMBOL(fcp_bus_reset);
159
160 /* checks whether the response matches the masked bytes in response_buffer */
161 static bool is_matching_response(struct fcp_transaction *transaction,
162                                  const void *response, size_t length)
163 {
164         const u8 *p1, *p2;
165         unsigned int mask, i;
166
167         p1 = response;
168         p2 = transaction->response_buffer;
169         mask = transaction->response_match_bytes;
170
171         for (i = 0; ; ++i) {
172                 if ((mask & 1) && p1[i] != p2[i])
173                         return false;
174                 mask >>= 1;
175                 if (!mask)
176                         return true;
177                 if (--length == 0)
178                         return false;
179         }
180 }
181
182 static void fcp_response(struct fw_card *card, struct fw_request *request,
183                          int tcode, int destination, int source,
184                          int generation, unsigned long long offset,
185                          void *data, size_t length, void *callback_data)
186 {
187         struct fcp_transaction *t;
188         unsigned long flags;
189
190         if (length < 1 || (*(const u8 *)data & 0xf0) != CTS_AVC)
191                 return;
192
193         spin_lock_irqsave(&transactions_lock, flags);
194         list_for_each_entry(t, &transactions, list) {
195                 struct fw_device *device = fw_parent_device(t->unit);
196                 if (device->card != card ||
197                     device->generation != generation)
198                         continue;
199                 smp_rmb(); /* node_id vs. generation */
200                 if (device->node_id != source)
201                         continue;
202
203                 if (t->state == STATE_PENDING &&
204                     is_matching_response(t, data, length)) {
205                         if (t->deferrable && *(const u8 *)data == 0x0f) {
206                                 t->state = STATE_DEFERRED;
207                         } else {
208                                 t->state = STATE_COMPLETE;
209                                 t->response_size = min_t(unsigned int, length,
210                                                          t->response_size);
211                                 memcpy(t->response_buffer, data,
212                                        t->response_size);
213                         }
214                         wake_up(&t->wait);
215                 }
216         }
217         spin_unlock_irqrestore(&transactions_lock, flags);
218 }
219
220 static struct fw_address_handler response_register_handler = {
221         .length = 0x200,
222         .address_callback = fcp_response,
223 };
224
225 static int __init fcp_module_init(void)
226 {
227         static const struct fw_address_region response_register_region = {
228                 .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
229                 .end = CSR_REGISTER_BASE + CSR_FCP_END,
230         };
231
232         fw_core_add_address_handler(&response_register_handler,
233                                     &response_register_region);
234
235         return 0;
236 }
237
238 static void __exit fcp_module_exit(void)
239 {
240         WARN_ON(!list_empty(&transactions));
241         fw_core_remove_address_handler(&response_register_handler);
242 }
243
244 module_init(fcp_module_init);
245 module_exit(fcp_module_exit);