162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Function Control Protocol (IEC 61883-1) helper functions
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include <linux/device.h>
962306a36Sopenharmony_ci#include <linux/firewire.h>
1062306a36Sopenharmony_ci#include <linux/firewire-constants.h>
1162306a36Sopenharmony_ci#include <linux/list.h>
1262306a36Sopenharmony_ci#include <linux/module.h>
1362306a36Sopenharmony_ci#include <linux/slab.h>
1462306a36Sopenharmony_ci#include <linux/sched.h>
1562306a36Sopenharmony_ci#include <linux/spinlock.h>
1662306a36Sopenharmony_ci#include <linux/wait.h>
1762306a36Sopenharmony_ci#include <linux/delay.h>
1862306a36Sopenharmony_ci#include "fcp.h"
1962306a36Sopenharmony_ci#include "lib.h"
2062306a36Sopenharmony_ci#include "amdtp-stream.h"
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci#define CTS_AVC 0x00
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define ERROR_RETRIES	3
2562306a36Sopenharmony_ci#define ERROR_DELAY_MS	5
2662306a36Sopenharmony_ci#define FCP_TIMEOUT_MS	125
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ciint avc_general_set_sig_fmt(struct fw_unit *unit, unsigned int rate,
2962306a36Sopenharmony_ci			    enum avc_general_plug_dir dir,
3062306a36Sopenharmony_ci			    unsigned short pid)
3162306a36Sopenharmony_ci{
3262306a36Sopenharmony_ci	unsigned int sfc;
3362306a36Sopenharmony_ci	u8 *buf;
3462306a36Sopenharmony_ci	bool flag;
3562306a36Sopenharmony_ci	int err;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	flag = false;
3862306a36Sopenharmony_ci	for (sfc = 0; sfc < CIP_SFC_COUNT; sfc++) {
3962306a36Sopenharmony_ci		if (amdtp_rate_table[sfc] == rate) {
4062306a36Sopenharmony_ci			flag = true;
4162306a36Sopenharmony_ci			break;
4262306a36Sopenharmony_ci		}
4362306a36Sopenharmony_ci	}
4462306a36Sopenharmony_ci	if (!flag)
4562306a36Sopenharmony_ci		return -EINVAL;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	buf = kzalloc(8, GFP_KERNEL);
4862306a36Sopenharmony_ci	if (buf == NULL)
4962306a36Sopenharmony_ci		return -ENOMEM;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	buf[0] = 0x00;		/* AV/C CONTROL */
5262306a36Sopenharmony_ci	buf[1] = 0xff;		/* UNIT */
5362306a36Sopenharmony_ci	if (dir == AVC_GENERAL_PLUG_DIR_IN)
5462306a36Sopenharmony_ci		buf[2] = 0x19;	/* INPUT PLUG SIGNAL FORMAT */
5562306a36Sopenharmony_ci	else
5662306a36Sopenharmony_ci		buf[2] = 0x18;	/* OUTPUT PLUG SIGNAL FORMAT */
5762306a36Sopenharmony_ci	buf[3] = 0xff & pid;	/* plug id */
5862306a36Sopenharmony_ci	buf[4] = 0x90;		/* EOH_1, Form_1, FMT. AM824 */
5962306a36Sopenharmony_ci	buf[5] = 0x07 & sfc;	/* FDF-hi. AM824, frequency */
6062306a36Sopenharmony_ci	buf[6] = 0xff;		/* FDF-mid. AM824, SYT hi (not used)*/
6162306a36Sopenharmony_ci	buf[7] = 0xff;		/* FDF-low. AM824, SYT lo (not used) */
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	/* do transaction and check buf[1-5] are the same against command */
6462306a36Sopenharmony_ci	err = fcp_avc_transaction(unit, buf, 8, buf, 8,
6562306a36Sopenharmony_ci				  BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5));
6662306a36Sopenharmony_ci	if (err < 0)
6762306a36Sopenharmony_ci		;
6862306a36Sopenharmony_ci	else if (err < 8)
6962306a36Sopenharmony_ci		err = -EIO;
7062306a36Sopenharmony_ci	else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
7162306a36Sopenharmony_ci		err = -ENOSYS;
7262306a36Sopenharmony_ci	else if (buf[0] == 0x0a) /* REJECTED */
7362306a36Sopenharmony_ci		err = -EINVAL;
7462306a36Sopenharmony_ci	if (err < 0)
7562306a36Sopenharmony_ci		goto end;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	err = 0;
7862306a36Sopenharmony_ciend:
7962306a36Sopenharmony_ci	kfree(buf);
8062306a36Sopenharmony_ci	return err;
8162306a36Sopenharmony_ci}
8262306a36Sopenharmony_ciEXPORT_SYMBOL(avc_general_set_sig_fmt);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciint avc_general_get_sig_fmt(struct fw_unit *unit, unsigned int *rate,
8562306a36Sopenharmony_ci			    enum avc_general_plug_dir dir,
8662306a36Sopenharmony_ci			    unsigned short pid)
8762306a36Sopenharmony_ci{
8862306a36Sopenharmony_ci	unsigned int sfc;
8962306a36Sopenharmony_ci	u8 *buf;
9062306a36Sopenharmony_ci	int err;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	buf = kzalloc(8, GFP_KERNEL);
9362306a36Sopenharmony_ci	if (buf == NULL)
9462306a36Sopenharmony_ci		return -ENOMEM;
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci	buf[0] = 0x01;		/* AV/C STATUS */
9762306a36Sopenharmony_ci	buf[1] = 0xff;		/* Unit */
9862306a36Sopenharmony_ci	if (dir == AVC_GENERAL_PLUG_DIR_IN)
9962306a36Sopenharmony_ci		buf[2] = 0x19;	/* INPUT PLUG SIGNAL FORMAT */
10062306a36Sopenharmony_ci	else
10162306a36Sopenharmony_ci		buf[2] = 0x18;	/* OUTPUT PLUG SIGNAL FORMAT */
10262306a36Sopenharmony_ci	buf[3] = 0xff & pid;	/* plug id */
10362306a36Sopenharmony_ci	buf[4] = 0x90;		/* EOH_1, Form_1, FMT. AM824 */
10462306a36Sopenharmony_ci	buf[5] = 0xff;		/* FDF-hi. AM824, frequency */
10562306a36Sopenharmony_ci	buf[6] = 0xff;		/* FDF-mid. AM824, SYT hi (not used) */
10662306a36Sopenharmony_ci	buf[7] = 0xff;		/* FDF-low. AM824, SYT lo (not used) */
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	/* do transaction and check buf[1-4] are the same against command */
10962306a36Sopenharmony_ci	err = fcp_avc_transaction(unit, buf, 8, buf, 8,
11062306a36Sopenharmony_ci				  BIT(1) | BIT(2) | BIT(3) | BIT(4));
11162306a36Sopenharmony_ci	if (err < 0)
11262306a36Sopenharmony_ci		;
11362306a36Sopenharmony_ci	else if (err < 8)
11462306a36Sopenharmony_ci		err = -EIO;
11562306a36Sopenharmony_ci	else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
11662306a36Sopenharmony_ci		err = -ENOSYS;
11762306a36Sopenharmony_ci	else if (buf[0] == 0x0a) /* REJECTED */
11862306a36Sopenharmony_ci		err = -EINVAL;
11962306a36Sopenharmony_ci	else if (buf[0] == 0x0b) /* IN TRANSITION */
12062306a36Sopenharmony_ci		err = -EAGAIN;
12162306a36Sopenharmony_ci	if (err < 0)
12262306a36Sopenharmony_ci		goto end;
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	/* check sfc field and pick up rate */
12562306a36Sopenharmony_ci	sfc = 0x07 & buf[5];
12662306a36Sopenharmony_ci	if (sfc >= CIP_SFC_COUNT) {
12762306a36Sopenharmony_ci		err = -EAGAIN;	/* also in transition */
12862306a36Sopenharmony_ci		goto end;
12962306a36Sopenharmony_ci	}
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	*rate = amdtp_rate_table[sfc];
13262306a36Sopenharmony_ci	err = 0;
13362306a36Sopenharmony_ciend:
13462306a36Sopenharmony_ci	kfree(buf);
13562306a36Sopenharmony_ci	return err;
13662306a36Sopenharmony_ci}
13762306a36Sopenharmony_ciEXPORT_SYMBOL(avc_general_get_sig_fmt);
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ciint avc_general_get_plug_info(struct fw_unit *unit, unsigned int subunit_type,
14062306a36Sopenharmony_ci			      unsigned int subunit_id, unsigned int subfunction,
14162306a36Sopenharmony_ci			      u8 info[AVC_PLUG_INFO_BUF_BYTES])
14262306a36Sopenharmony_ci{
14362306a36Sopenharmony_ci	u8 *buf;
14462306a36Sopenharmony_ci	int err;
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci	/* extended subunit in spec.4.2 is not supported */
14762306a36Sopenharmony_ci	if ((subunit_type == 0x1E) || (subunit_id == 5))
14862306a36Sopenharmony_ci		return -EINVAL;
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	buf = kzalloc(8, GFP_KERNEL);
15162306a36Sopenharmony_ci	if (buf == NULL)
15262306a36Sopenharmony_ci		return -ENOMEM;
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	buf[0] = 0x01;	/* AV/C STATUS */
15562306a36Sopenharmony_ci	/* UNIT or Subunit, Functionblock */
15662306a36Sopenharmony_ci	buf[1] = ((subunit_type & 0x1f) << 3) | (subunit_id & 0x7);
15762306a36Sopenharmony_ci	buf[2] = 0x02;	/* PLUG INFO */
15862306a36Sopenharmony_ci	buf[3] = 0xff & subfunction;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	err = fcp_avc_transaction(unit, buf, 8, buf, 8, BIT(1) | BIT(2));
16162306a36Sopenharmony_ci	if (err < 0)
16262306a36Sopenharmony_ci		;
16362306a36Sopenharmony_ci	else if (err < 8)
16462306a36Sopenharmony_ci		err = -EIO;
16562306a36Sopenharmony_ci	else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
16662306a36Sopenharmony_ci		err = -ENOSYS;
16762306a36Sopenharmony_ci	else if (buf[0] == 0x0a) /* REJECTED */
16862306a36Sopenharmony_ci		err = -EINVAL;
16962306a36Sopenharmony_ci	else if (buf[0] == 0x0b) /* IN TRANSITION */
17062306a36Sopenharmony_ci		err = -EAGAIN;
17162306a36Sopenharmony_ci	if (err < 0)
17262306a36Sopenharmony_ci		goto end;
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	info[0] = buf[4];
17562306a36Sopenharmony_ci	info[1] = buf[5];
17662306a36Sopenharmony_ci	info[2] = buf[6];
17762306a36Sopenharmony_ci	info[3] = buf[7];
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	err = 0;
18062306a36Sopenharmony_ciend:
18162306a36Sopenharmony_ci	kfree(buf);
18262306a36Sopenharmony_ci	return err;
18362306a36Sopenharmony_ci}
18462306a36Sopenharmony_ciEXPORT_SYMBOL(avc_general_get_plug_info);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_cistatic DEFINE_SPINLOCK(transactions_lock);
18762306a36Sopenharmony_cistatic LIST_HEAD(transactions);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_cienum fcp_state {
19062306a36Sopenharmony_ci	STATE_PENDING,
19162306a36Sopenharmony_ci	STATE_BUS_RESET,
19262306a36Sopenharmony_ci	STATE_COMPLETE,
19362306a36Sopenharmony_ci	STATE_DEFERRED,
19462306a36Sopenharmony_ci};
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_cistruct fcp_transaction {
19762306a36Sopenharmony_ci	struct list_head list;
19862306a36Sopenharmony_ci	struct fw_unit *unit;
19962306a36Sopenharmony_ci	void *response_buffer;
20062306a36Sopenharmony_ci	unsigned int response_size;
20162306a36Sopenharmony_ci	unsigned int response_match_bytes;
20262306a36Sopenharmony_ci	enum fcp_state state;
20362306a36Sopenharmony_ci	wait_queue_head_t wait;
20462306a36Sopenharmony_ci	bool deferrable;
20562306a36Sopenharmony_ci};
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci/**
20862306a36Sopenharmony_ci * fcp_avc_transaction - send an AV/C command and wait for its response
20962306a36Sopenharmony_ci * @unit: a unit on the target device
21062306a36Sopenharmony_ci * @command: a buffer containing the command frame; must be DMA-able
21162306a36Sopenharmony_ci * @command_size: the size of @command
21262306a36Sopenharmony_ci * @response: a buffer for the response frame
21362306a36Sopenharmony_ci * @response_size: the maximum size of @response
21462306a36Sopenharmony_ci * @response_match_bytes: a bitmap specifying the bytes used to detect the
21562306a36Sopenharmony_ci *                        correct response frame
21662306a36Sopenharmony_ci *
21762306a36Sopenharmony_ci * This function sends a FCP command frame to the target and waits for the
21862306a36Sopenharmony_ci * corresponding response frame to be returned.
21962306a36Sopenharmony_ci *
22062306a36Sopenharmony_ci * Because it is possible for multiple FCP transactions to be active at the
22162306a36Sopenharmony_ci * same time, the correct response frame is detected by the value of certain
22262306a36Sopenharmony_ci * bytes.  These bytes must be set in @response before calling this function,
22362306a36Sopenharmony_ci * and the corresponding bits must be set in @response_match_bytes.
22462306a36Sopenharmony_ci *
22562306a36Sopenharmony_ci * @command and @response can point to the same buffer.
22662306a36Sopenharmony_ci *
22762306a36Sopenharmony_ci * Returns the actual size of the response frame, or a negative error code.
22862306a36Sopenharmony_ci */
22962306a36Sopenharmony_ciint fcp_avc_transaction(struct fw_unit *unit,
23062306a36Sopenharmony_ci			const void *command, unsigned int command_size,
23162306a36Sopenharmony_ci			void *response, unsigned int response_size,
23262306a36Sopenharmony_ci			unsigned int response_match_bytes)
23362306a36Sopenharmony_ci{
23462306a36Sopenharmony_ci	struct fcp_transaction t;
23562306a36Sopenharmony_ci	int tcode, ret, tries = 0;
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci	t.unit = unit;
23862306a36Sopenharmony_ci	t.response_buffer = response;
23962306a36Sopenharmony_ci	t.response_size = response_size;
24062306a36Sopenharmony_ci	t.response_match_bytes = response_match_bytes;
24162306a36Sopenharmony_ci	t.state = STATE_PENDING;
24262306a36Sopenharmony_ci	init_waitqueue_head(&t.wait);
24362306a36Sopenharmony_ci	t.deferrable = (*(const u8 *)command == 0x00 || *(const u8 *)command == 0x03);
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	spin_lock_irq(&transactions_lock);
24662306a36Sopenharmony_ci	list_add_tail(&t.list, &transactions);
24762306a36Sopenharmony_ci	spin_unlock_irq(&transactions_lock);
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	for (;;) {
25062306a36Sopenharmony_ci		tcode = command_size == 4 ? TCODE_WRITE_QUADLET_REQUEST
25162306a36Sopenharmony_ci					  : TCODE_WRITE_BLOCK_REQUEST;
25262306a36Sopenharmony_ci		ret = snd_fw_transaction(t.unit, tcode,
25362306a36Sopenharmony_ci					 CSR_REGISTER_BASE + CSR_FCP_COMMAND,
25462306a36Sopenharmony_ci					 (void *)command, command_size, 0);
25562306a36Sopenharmony_ci		if (ret < 0)
25662306a36Sopenharmony_ci			break;
25762306a36Sopenharmony_cideferred:
25862306a36Sopenharmony_ci		wait_event_timeout(t.wait, t.state != STATE_PENDING,
25962306a36Sopenharmony_ci				   msecs_to_jiffies(FCP_TIMEOUT_MS));
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci		if (t.state == STATE_DEFERRED) {
26262306a36Sopenharmony_ci			/*
26362306a36Sopenharmony_ci			 * 'AV/C General Specification' define no time limit
26462306a36Sopenharmony_ci			 * on command completion once an INTERIM response has
26562306a36Sopenharmony_ci			 * been sent. but we promise to finish this function
26662306a36Sopenharmony_ci			 * for a caller. Here we use FCP_TIMEOUT_MS for next
26762306a36Sopenharmony_ci			 * interval. This is not in the specification.
26862306a36Sopenharmony_ci			 */
26962306a36Sopenharmony_ci			t.state = STATE_PENDING;
27062306a36Sopenharmony_ci			goto deferred;
27162306a36Sopenharmony_ci		} else if (t.state == STATE_COMPLETE) {
27262306a36Sopenharmony_ci			ret = t.response_size;
27362306a36Sopenharmony_ci			break;
27462306a36Sopenharmony_ci		} else if (t.state == STATE_BUS_RESET) {
27562306a36Sopenharmony_ci			msleep(ERROR_DELAY_MS);
27662306a36Sopenharmony_ci		} else if (++tries >= ERROR_RETRIES) {
27762306a36Sopenharmony_ci			dev_err(&t.unit->device, "FCP command timed out\n");
27862306a36Sopenharmony_ci			ret = -EIO;
27962306a36Sopenharmony_ci			break;
28062306a36Sopenharmony_ci		}
28162306a36Sopenharmony_ci	}
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci	spin_lock_irq(&transactions_lock);
28462306a36Sopenharmony_ci	list_del(&t.list);
28562306a36Sopenharmony_ci	spin_unlock_irq(&transactions_lock);
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	return ret;
28862306a36Sopenharmony_ci}
28962306a36Sopenharmony_ciEXPORT_SYMBOL(fcp_avc_transaction);
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci/**
29262306a36Sopenharmony_ci * fcp_bus_reset - inform the target handler about a bus reset
29362306a36Sopenharmony_ci * @unit: the unit that might be used by fcp_avc_transaction()
29462306a36Sopenharmony_ci *
29562306a36Sopenharmony_ci * This function must be called from the driver's .update handler to inform
29662306a36Sopenharmony_ci * the FCP transaction handler that a bus reset has happened.  Any pending FCP
29762306a36Sopenharmony_ci * transactions are retried.
29862306a36Sopenharmony_ci */
29962306a36Sopenharmony_civoid fcp_bus_reset(struct fw_unit *unit)
30062306a36Sopenharmony_ci{
30162306a36Sopenharmony_ci	struct fcp_transaction *t;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	spin_lock_irq(&transactions_lock);
30462306a36Sopenharmony_ci	list_for_each_entry(t, &transactions, list) {
30562306a36Sopenharmony_ci		if (t->unit == unit &&
30662306a36Sopenharmony_ci		    (t->state == STATE_PENDING ||
30762306a36Sopenharmony_ci		     t->state == STATE_DEFERRED)) {
30862306a36Sopenharmony_ci			t->state = STATE_BUS_RESET;
30962306a36Sopenharmony_ci			wake_up(&t->wait);
31062306a36Sopenharmony_ci		}
31162306a36Sopenharmony_ci	}
31262306a36Sopenharmony_ci	spin_unlock_irq(&transactions_lock);
31362306a36Sopenharmony_ci}
31462306a36Sopenharmony_ciEXPORT_SYMBOL(fcp_bus_reset);
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci/* checks whether the response matches the masked bytes in response_buffer */
31762306a36Sopenharmony_cistatic bool is_matching_response(struct fcp_transaction *transaction,
31862306a36Sopenharmony_ci				 const void *response, size_t length)
31962306a36Sopenharmony_ci{
32062306a36Sopenharmony_ci	const u8 *p1, *p2;
32162306a36Sopenharmony_ci	unsigned int mask, i;
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	p1 = response;
32462306a36Sopenharmony_ci	p2 = transaction->response_buffer;
32562306a36Sopenharmony_ci	mask = transaction->response_match_bytes;
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	for (i = 0; ; ++i) {
32862306a36Sopenharmony_ci		if ((mask & 1) && p1[i] != p2[i])
32962306a36Sopenharmony_ci			return false;
33062306a36Sopenharmony_ci		mask >>= 1;
33162306a36Sopenharmony_ci		if (!mask)
33262306a36Sopenharmony_ci			return true;
33362306a36Sopenharmony_ci		if (--length == 0)
33462306a36Sopenharmony_ci			return false;
33562306a36Sopenharmony_ci	}
33662306a36Sopenharmony_ci}
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_cistatic void fcp_response(struct fw_card *card, struct fw_request *request,
33962306a36Sopenharmony_ci			 int tcode, int destination, int source,
34062306a36Sopenharmony_ci			 int generation, unsigned long long offset,
34162306a36Sopenharmony_ci			 void *data, size_t length, void *callback_data)
34262306a36Sopenharmony_ci{
34362306a36Sopenharmony_ci	struct fcp_transaction *t;
34462306a36Sopenharmony_ci	unsigned long flags;
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	if (length < 1 || (*(const u8 *)data & 0xf0) != CTS_AVC)
34762306a36Sopenharmony_ci		return;
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	spin_lock_irqsave(&transactions_lock, flags);
35062306a36Sopenharmony_ci	list_for_each_entry(t, &transactions, list) {
35162306a36Sopenharmony_ci		struct fw_device *device = fw_parent_device(t->unit);
35262306a36Sopenharmony_ci		if (device->card != card ||
35362306a36Sopenharmony_ci		    device->generation != generation)
35462306a36Sopenharmony_ci			continue;
35562306a36Sopenharmony_ci		smp_rmb(); /* node_id vs. generation */
35662306a36Sopenharmony_ci		if (device->node_id != source)
35762306a36Sopenharmony_ci			continue;
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci		if (t->state == STATE_PENDING &&
36062306a36Sopenharmony_ci		    is_matching_response(t, data, length)) {
36162306a36Sopenharmony_ci			if (t->deferrable && *(const u8 *)data == 0x0f) {
36262306a36Sopenharmony_ci				t->state = STATE_DEFERRED;
36362306a36Sopenharmony_ci			} else {
36462306a36Sopenharmony_ci				t->state = STATE_COMPLETE;
36562306a36Sopenharmony_ci				t->response_size = min_t(unsigned int, length,
36662306a36Sopenharmony_ci							 t->response_size);
36762306a36Sopenharmony_ci				memcpy(t->response_buffer, data,
36862306a36Sopenharmony_ci				       t->response_size);
36962306a36Sopenharmony_ci			}
37062306a36Sopenharmony_ci			wake_up(&t->wait);
37162306a36Sopenharmony_ci		}
37262306a36Sopenharmony_ci	}
37362306a36Sopenharmony_ci	spin_unlock_irqrestore(&transactions_lock, flags);
37462306a36Sopenharmony_ci}
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_cistatic struct fw_address_handler response_register_handler = {
37762306a36Sopenharmony_ci	.length = 0x200,
37862306a36Sopenharmony_ci	.address_callback = fcp_response,
37962306a36Sopenharmony_ci};
38062306a36Sopenharmony_ci
38162306a36Sopenharmony_cistatic int __init fcp_module_init(void)
38262306a36Sopenharmony_ci{
38362306a36Sopenharmony_ci	static const struct fw_address_region response_register_region = {
38462306a36Sopenharmony_ci		.start = CSR_REGISTER_BASE + CSR_FCP_RESPONSE,
38562306a36Sopenharmony_ci		.end = CSR_REGISTER_BASE + CSR_FCP_END,
38662306a36Sopenharmony_ci	};
38762306a36Sopenharmony_ci
38862306a36Sopenharmony_ci	fw_core_add_address_handler(&response_register_handler,
38962306a36Sopenharmony_ci				    &response_register_region);
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci	return 0;
39262306a36Sopenharmony_ci}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_cistatic void __exit fcp_module_exit(void)
39562306a36Sopenharmony_ci{
39662306a36Sopenharmony_ci	WARN_ON(!list_empty(&transactions));
39762306a36Sopenharmony_ci	fw_core_remove_address_handler(&response_register_handler);
39862306a36Sopenharmony_ci}
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_cimodule_init(fcp_module_init);
40162306a36Sopenharmony_cimodule_exit(fcp_module_exit);
402