162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * oxfw_command.c - a part of driver for OXFW970/971 based devices
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2014 Takashi Sakamoto
662306a36Sopenharmony_ci */
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci#include "oxfw.h"
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciint avc_stream_set_format(struct fw_unit *unit, enum avc_general_plug_dir dir,
1162306a36Sopenharmony_ci			  unsigned int pid, u8 *format, unsigned int len)
1262306a36Sopenharmony_ci{
1362306a36Sopenharmony_ci	u8 *buf;
1462306a36Sopenharmony_ci	int err;
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci	buf = kmalloc(len + 10, GFP_KERNEL);
1762306a36Sopenharmony_ci	if (buf == NULL)
1862306a36Sopenharmony_ci		return -ENOMEM;
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci	buf[0] = 0x00;		/* CONTROL */
2162306a36Sopenharmony_ci	buf[1] = 0xff;		/* UNIT */
2262306a36Sopenharmony_ci	buf[2] = 0xbf;		/* EXTENDED STREAM FORMAT INFORMATION */
2362306a36Sopenharmony_ci	buf[3] = 0xc0;		/* SINGLE subfunction */
2462306a36Sopenharmony_ci	buf[4] = dir;		/* Plug Direction */
2562306a36Sopenharmony_ci	buf[5] = 0x00;		/* UNIT */
2662306a36Sopenharmony_ci	buf[6] = 0x00;		/* PCR (Isochronous Plug) */
2762306a36Sopenharmony_ci	buf[7] = 0xff & pid;	/* Plug ID */
2862306a36Sopenharmony_ci	buf[8] = 0xff;		/* Padding */
2962306a36Sopenharmony_ci	buf[9] = 0xff;		/* Support status in response */
3062306a36Sopenharmony_ci	memcpy(buf + 10, format, len);
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	/* do transaction and check buf[1-8] are the same against command */
3362306a36Sopenharmony_ci	err = fcp_avc_transaction(unit, buf, len + 10, buf, len + 10,
3462306a36Sopenharmony_ci				  BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) |
3562306a36Sopenharmony_ci				  BIT(6) | BIT(7) | BIT(8));
3662306a36Sopenharmony_ci	if (err < 0)
3762306a36Sopenharmony_ci		;
3862306a36Sopenharmony_ci	else if (err < len + 10)
3962306a36Sopenharmony_ci		err = -EIO;
4062306a36Sopenharmony_ci	else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
4162306a36Sopenharmony_ci		err = -ENXIO;
4262306a36Sopenharmony_ci	else if (buf[0] == 0x0a) /* REJECTED */
4362306a36Sopenharmony_ci		err = -EINVAL;
4462306a36Sopenharmony_ci	else
4562306a36Sopenharmony_ci		err = 0;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	kfree(buf);
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	return err;
5062306a36Sopenharmony_ci}
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ciint avc_stream_get_format(struct fw_unit *unit,
5362306a36Sopenharmony_ci			  enum avc_general_plug_dir dir, unsigned int pid,
5462306a36Sopenharmony_ci			  u8 *buf, unsigned int *len, unsigned int eid)
5562306a36Sopenharmony_ci{
5662306a36Sopenharmony_ci	unsigned int subfunc;
5762306a36Sopenharmony_ci	int err;
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci	if (eid == 0xff)
6062306a36Sopenharmony_ci		subfunc = 0xc0;	/* SINGLE */
6162306a36Sopenharmony_ci	else
6262306a36Sopenharmony_ci		subfunc = 0xc1;	/* LIST */
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	buf[0] = 0x01;		/* STATUS */
6562306a36Sopenharmony_ci	buf[1] = 0xff;		/* UNIT */
6662306a36Sopenharmony_ci	buf[2] = 0xbf;		/* EXTENDED STREAM FORMAT INFORMATION */
6762306a36Sopenharmony_ci	buf[3] = subfunc;	/* SINGLE or LIST */
6862306a36Sopenharmony_ci	buf[4] = dir;		/* Plug Direction */
6962306a36Sopenharmony_ci	buf[5] = 0x00;		/* Unit */
7062306a36Sopenharmony_ci	buf[6] = 0x00;		/* PCR (Isochronous Plug) */
7162306a36Sopenharmony_ci	buf[7] = 0xff & pid;	/* Plug ID */
7262306a36Sopenharmony_ci	buf[8] = 0xff;		/* Padding */
7362306a36Sopenharmony_ci	buf[9] = 0xff;		/* support status in response */
7462306a36Sopenharmony_ci	buf[10] = 0xff & eid;	/* entry ID for LIST subfunction */
7562306a36Sopenharmony_ci	buf[11] = 0xff;		/* padding */
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	/* do transaction and check buf[1-7] are the same against command */
7862306a36Sopenharmony_ci	err = fcp_avc_transaction(unit, buf, 12, buf, *len,
7962306a36Sopenharmony_ci				  BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) |
8062306a36Sopenharmony_ci				  BIT(6) | BIT(7));
8162306a36Sopenharmony_ci	if (err < 0)
8262306a36Sopenharmony_ci		;
8362306a36Sopenharmony_ci	else if (err < 12)
8462306a36Sopenharmony_ci		err = -EIO;
8562306a36Sopenharmony_ci	else if (buf[0] == 0x08)	/* NOT IMPLEMENTED */
8662306a36Sopenharmony_ci		err = -ENXIO;
8762306a36Sopenharmony_ci	else if (buf[0] == 0x0a)	/* REJECTED */
8862306a36Sopenharmony_ci		err = -EINVAL;
8962306a36Sopenharmony_ci	else if (buf[0] == 0x0b)	/* IN TRANSITION */
9062306a36Sopenharmony_ci		err = -EAGAIN;
9162306a36Sopenharmony_ci	/* LIST subfunction has entry ID */
9262306a36Sopenharmony_ci	else if ((subfunc == 0xc1) && (buf[10] != eid))
9362306a36Sopenharmony_ci		err = -EIO;
9462306a36Sopenharmony_ci	if (err < 0)
9562306a36Sopenharmony_ci		goto end;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/* keep just stream format information */
9862306a36Sopenharmony_ci	if (subfunc == 0xc0) {
9962306a36Sopenharmony_ci		memmove(buf, buf + 10, err - 10);
10062306a36Sopenharmony_ci		*len = err - 10;
10162306a36Sopenharmony_ci	} else {
10262306a36Sopenharmony_ci		memmove(buf, buf + 11, err - 11);
10362306a36Sopenharmony_ci		*len = err - 11;
10462306a36Sopenharmony_ci	}
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	err = 0;
10762306a36Sopenharmony_ciend:
10862306a36Sopenharmony_ci	return err;
10962306a36Sopenharmony_ci}
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciint avc_general_inquiry_sig_fmt(struct fw_unit *unit, unsigned int rate,
11262306a36Sopenharmony_ci				enum avc_general_plug_dir dir,
11362306a36Sopenharmony_ci				unsigned short pid)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	unsigned int sfc;
11662306a36Sopenharmony_ci	u8 *buf;
11762306a36Sopenharmony_ci	int err;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	for (sfc = 0; sfc < CIP_SFC_COUNT; sfc++) {
12062306a36Sopenharmony_ci		if (amdtp_rate_table[sfc] == rate)
12162306a36Sopenharmony_ci			break;
12262306a36Sopenharmony_ci	}
12362306a36Sopenharmony_ci	if (sfc == CIP_SFC_COUNT)
12462306a36Sopenharmony_ci		return -EINVAL;
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci	buf = kzalloc(8, GFP_KERNEL);
12762306a36Sopenharmony_ci	if (buf == NULL)
12862306a36Sopenharmony_ci		return -ENOMEM;
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ci	buf[0] = 0x02;		/* SPECIFIC INQUIRY */
13162306a36Sopenharmony_ci	buf[1] = 0xff;		/* UNIT */
13262306a36Sopenharmony_ci	if (dir == AVC_GENERAL_PLUG_DIR_IN)
13362306a36Sopenharmony_ci		buf[2] = 0x19;	/* INPUT PLUG SIGNAL FORMAT */
13462306a36Sopenharmony_ci	else
13562306a36Sopenharmony_ci		buf[2] = 0x18;	/* OUTPUT PLUG SIGNAL FORMAT */
13662306a36Sopenharmony_ci	buf[3] = 0xff & pid;	/* plug id */
13762306a36Sopenharmony_ci	buf[4] = 0x90;		/* EOH_1, Form_1, FMT. AM824 */
13862306a36Sopenharmony_ci	buf[5] = 0x07 & sfc;	/* FDF-hi. AM824, frequency */
13962306a36Sopenharmony_ci	buf[6] = 0xff;		/* FDF-mid. AM824, SYT hi (not used) */
14062306a36Sopenharmony_ci	buf[7] = 0xff;		/* FDF-low. AM824, SYT lo (not used) */
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	/* do transaction and check buf[1-5] are the same against command */
14362306a36Sopenharmony_ci	err = fcp_avc_transaction(unit, buf, 8, buf, 8,
14462306a36Sopenharmony_ci				  BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5));
14562306a36Sopenharmony_ci	if (err < 0)
14662306a36Sopenharmony_ci		;
14762306a36Sopenharmony_ci	else if (err < 8)
14862306a36Sopenharmony_ci		err = -EIO;
14962306a36Sopenharmony_ci	else if (buf[0] == 0x08)	/* NOT IMPLEMENTED */
15062306a36Sopenharmony_ci		err = -ENXIO;
15162306a36Sopenharmony_ci	if (err < 0)
15262306a36Sopenharmony_ci		goto end;
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci	err = 0;
15562306a36Sopenharmony_ciend:
15662306a36Sopenharmony_ci	kfree(buf);
15762306a36Sopenharmony_ci	return err;
15862306a36Sopenharmony_ci}
159