1 // SPDX-License-Identifier: GPL-2.0-only
3 * Function Control Protocol (IEC 61883-1) helper functions
5 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
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/slab.h>
14 #include <linux/sched.h>
15 #include <linux/spinlock.h>
16 #include <linux/wait.h>
17 #include <linux/delay.h>
20 #include "amdtp-stream.h"
24 #define ERROR_RETRIES 3
25 #define ERROR_DELAY_MS 5
26 #define FCP_TIMEOUT_MS 125
28 int avc_general_set_sig_fmt(struct fw_unit *unit, unsigned int rate,
29 enum avc_general_plug_dir dir,
38 for (sfc = 0; sfc < CIP_SFC_COUNT; sfc++) {
39 if (amdtp_rate_table[sfc] == rate) {
47 buf = kzalloc(8, GFP_KERNEL);
51 buf[0] = 0x00; /* AV/C CONTROL */
52 buf[1] = 0xff; /* UNIT */
53 if (dir == AVC_GENERAL_PLUG_DIR_IN)
54 buf[2] = 0x19; /* INPUT PLUG SIGNAL FORMAT */
56 buf[2] = 0x18; /* OUTPUT PLUG SIGNAL FORMAT */
57 buf[3] = 0xff & pid; /* plug id */
58 buf[4] = 0x90; /* EOH_1, Form_1, FMT. AM824 */
59 buf[5] = 0x07 & sfc; /* FDF-hi. AM824, frequency */
60 buf[6] = 0xff; /* FDF-mid. AM824, SYT hi (not used)*/
61 buf[7] = 0xff; /* FDF-low. AM824, SYT lo (not used) */
63 /* do transaction and check buf[1-5] are the same against command */
64 err = fcp_avc_transaction(unit, buf, 8, buf, 8,
65 BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5));
70 else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
72 else if (buf[0] == 0x0a) /* REJECTED */
82 EXPORT_SYMBOL(avc_general_set_sig_fmt);
84 int avc_general_get_sig_fmt(struct fw_unit *unit, unsigned int *rate,
85 enum avc_general_plug_dir dir,
92 buf = kzalloc(8, GFP_KERNEL);
96 buf[0] = 0x01; /* AV/C STATUS */
97 buf[1] = 0xff; /* Unit */
98 if (dir == AVC_GENERAL_PLUG_DIR_IN)
99 buf[2] = 0x19; /* INPUT PLUG SIGNAL FORMAT */
101 buf[2] = 0x18; /* OUTPUT PLUG SIGNAL FORMAT */
102 buf[3] = 0xff & pid; /* plug id */
103 buf[4] = 0x90; /* EOH_1, Form_1, FMT. AM824 */
104 buf[5] = 0xff; /* FDF-hi. AM824, frequency */
105 buf[6] = 0xff; /* FDF-mid. AM824, SYT hi (not used) */
106 buf[7] = 0xff; /* FDF-low. AM824, SYT lo (not used) */
108 /* do transaction and check buf[1-4] are the same against command */
109 err = fcp_avc_transaction(unit, buf, 8, buf, 8,
110 BIT(1) | BIT(2) | BIT(3) | BIT(4));
115 else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
117 else if (buf[0] == 0x0a) /* REJECTED */
119 else if (buf[0] == 0x0b) /* IN TRANSITION */
124 /* check sfc field and pick up rate */
126 if (sfc >= CIP_SFC_COUNT) {
127 err = -EAGAIN; /* also in transition */
131 *rate = amdtp_rate_table[sfc];
137 EXPORT_SYMBOL(avc_general_get_sig_fmt);
139 int avc_general_get_plug_info(struct fw_unit *unit, unsigned int subunit_type,
140 unsigned int subunit_id, unsigned int subfunction,
141 u8 info[AVC_PLUG_INFO_BUF_BYTES])
146 /* extended subunit in spec.4.2 is not supported */
147 if ((subunit_type == 0x1E) || (subunit_id == 5))
150 buf = kzalloc(8, GFP_KERNEL);
154 buf[0] = 0x01; /* AV/C STATUS */
155 /* UNIT or Subunit, Functionblock */
156 buf[1] = ((subunit_type & 0x1f) << 3) | (subunit_id & 0x7);
157 buf[2] = 0x02; /* PLUG INFO */
158 buf[3] = 0xff & subfunction;
160 err = fcp_avc_transaction(unit, buf, 8, buf, 8, BIT(1) | BIT(2));
165 else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
167 else if (buf[0] == 0x0a) /* REJECTED */
169 else if (buf[0] == 0x0b) /* IN TRANSITION */
184 EXPORT_SYMBOL(avc_general_get_plug_info);
186 static DEFINE_SPINLOCK(transactions_lock);
187 static LIST_HEAD(transactions);
196 struct fcp_transaction {
197 struct list_head list;
198 struct fw_unit *unit;
199 void *response_buffer;
200 unsigned int response_size;
201 unsigned int response_match_bytes;
202 enum fcp_state state;
203 wait_queue_head_t wait;
208 * fcp_avc_transaction - send an AV/C command and wait for its response
209 * @unit: a unit on the target device
210 * @command: a buffer containing the command frame; must be DMA-able
211 * @command_size: the size of @command
212 * @response: a buffer for the response frame
213 * @response_size: the maximum size of @response
214 * @response_match_bytes: a bitmap specifying the bytes used to detect the
215 * correct response frame
217 * This function sends a FCP command frame to the target and waits for the
218 * corresponding response frame to be returned.
220 * Because it is possible for multiple FCP transactions to be active at the
221 * same time, the correct response frame is detected by the value of certain
222 * bytes. These bytes must be set in @response before calling this function,
223 * and the corresponding bits must be set in @response_match_bytes.
225 * @command and @response can point to the same buffer.
227 * Returns the actual size of the response frame, or a negative error code.
229 int fcp_avc_transaction(struct fw_unit *unit,
230 const void *command, unsigned int command_size,
231 void *response, unsigned int response_size,
232 unsigned int response_match_bytes)
234 struct fcp_transaction t;
235 int tcode, ret, tries = 0;
238 t.response_buffer = response;
239 t.response_size = response_size;
240 t.response_match_bytes = response_match_bytes;
241 t.state = STATE_PENDING;
242 init_waitqueue_head(&t.wait);
243 t.deferrable = (*(const u8 *)command == 0x00 || *(const u8 *)command == 0x03);
245 spin_lock_irq(&transactions_lock);
246 list_add_tail(&t.list, &transactions);
247 spin_unlock_irq(&transactions_lock);
250 tcode = command_size == 4 ? TCODE_WRITE_QUADLET_REQUEST
251 : TCODE_WRITE_BLOCK_REQUEST;
252 ret = snd_fw_transaction(t.unit, tcode,
253 CSR_REGISTER_BASE + CSR_FCP_COMMAND,
254 (void *)command, command_size, 0);
258 wait_event_timeout(t.wait, t.state != STATE_PENDING,
259 msecs_to_jiffies(FCP_TIMEOUT_MS));
261 if (t.state == STATE_DEFERRED) {
263 * 'AV/C General Specification' define no time limit
264 * on command completion once an INTERIM response has
265 * been sent. but we promise to finish this function
266 * for a caller. Here we use FCP_TIMEOUT_MS for next
267 * interval. This is not in the specification.
269 t.state = STATE_PENDING;
271 } else if (t.state == STATE_COMPLETE) {
272 ret = t.response_size;
274 } else if (t.state == STATE_BUS_RESET) {
275 msleep(ERROR_DELAY_MS);
276 } else if (++tries >= ERROR_RETRIES) {
277 dev_err(&t.unit->device, "FCP command timed out\n");
283 spin_lock_irq(&transactions_lock);
285 spin_unlock_irq(&transactions_lock);
289 EXPORT_SYMBOL(fcp_avc_transaction);
292 * fcp_bus_reset - inform the target handler about a bus reset
293 * @unit: the unit that might be used by fcp_avc_transaction()
295 * This function must be called from the driver's .update handler to inform
296 * the FCP transaction handler that a bus reset has happened. Any pending FCP
297 * transactions are retried.
299 void fcp_bus_reset(struct fw_unit *unit)
301 struct fcp_transaction *t;
303 spin_lock_irq(&transactions_lock);
304 list_for_each_entry(t, &transactions, list) {
305 if (t->unit == unit &&
306 (t->state == STATE_PENDING ||
307 t->state == STATE_DEFERRED)) {
308 t->state = STATE_BUS_RESET;
312 spin_unlock_irq(&transactions_lock);
314 EXPORT_SYMBOL(fcp_bus_reset);
316 /* checks whether the response matches the masked bytes in response_buffer */
317 static bool is_matching_response(struct fcp_transaction *transaction,
318 const void *response, size_t length)
321 unsigned int mask, i;
324 p2 = transaction->response_buffer;
325 mask = transaction->response_match_bytes;
328 if ((mask & 1) && p1[i] != p2[i])
338 static void fcp_response(struct fw_card *card, struct fw_request *request,
339 int tcode, int destination, int source,
340 int generation, unsigned long long offset,
341 void *data, size_t length, void *callback_data)
343 struct fcp_transaction *t;
346 if (length < 1 || (*(const u8 *)data & 0xf0) != CTS_AVC)
349 spin_lock_irqsave(&transactions_lock, flags);
350 list_for_each_entry(t, &transactions, list) {
351 struct fw_device *device = fw_parent_device(t->unit);
352 if (device->card != card ||
353 device->generation != generation)
355 smp_rmb(); /* node_id vs. generation */
356 if (device->node_id != source)
359 if (t->state == STATE_PENDING &&
360 is_matching_response(t, data, length)) {
361 if (t->deferrable && *(const u8 *)data == 0x0f) {
362 t->state = STATE_DEFERRED;
364 t->state = STATE_COMPLETE;
365 t->response_size = min_t(unsigned int, length,
367 memcpy(t->response_buffer, data,
373 spin_unlock_irqrestore(&transactions_lock, flags);
376 static struct fw_address_handler response_register_handler = {
378 .address_callback = fcp_response,
381 static int __init fcp_module_init(void)
383 static const struct fw_address_region response_register_region = {
384 .start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
385 .end = CSR_REGISTER_BASE + CSR_FCP_END,
388 fw_core_add_address_handler(&response_register_handler,
389 &response_register_region);
394 static void __exit fcp_module_exit(void)
396 WARN_ON(!list_empty(&transactions));
397 fw_core_remove_address_handler(&response_register_handler);
400 module_init(fcp_module_init);
401 module_exit(fcp_module_exit);