18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * oxfw_command.c - a part of driver for OXFW970/971 based devices
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2014 Takashi Sakamoto
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include "oxfw.h"
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ciint avc_stream_set_format(struct fw_unit *unit, enum avc_general_plug_dir dir,
118c2ecf20Sopenharmony_ci			  unsigned int pid, u8 *format, unsigned int len)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	u8 *buf;
148c2ecf20Sopenharmony_ci	int err;
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci	buf = kmalloc(len + 10, GFP_KERNEL);
178c2ecf20Sopenharmony_ci	if (buf == NULL)
188c2ecf20Sopenharmony_ci		return -ENOMEM;
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci	buf[0] = 0x00;		/* CONTROL */
218c2ecf20Sopenharmony_ci	buf[1] = 0xff;		/* UNIT */
228c2ecf20Sopenharmony_ci	buf[2] = 0xbf;		/* EXTENDED STREAM FORMAT INFORMATION */
238c2ecf20Sopenharmony_ci	buf[3] = 0xc0;		/* SINGLE subfunction */
248c2ecf20Sopenharmony_ci	buf[4] = dir;		/* Plug Direction */
258c2ecf20Sopenharmony_ci	buf[5] = 0x00;		/* UNIT */
268c2ecf20Sopenharmony_ci	buf[6] = 0x00;		/* PCR (Isochronous Plug) */
278c2ecf20Sopenharmony_ci	buf[7] = 0xff & pid;	/* Plug ID */
288c2ecf20Sopenharmony_ci	buf[8] = 0xff;		/* Padding */
298c2ecf20Sopenharmony_ci	buf[9] = 0xff;		/* Support status in response */
308c2ecf20Sopenharmony_ci	memcpy(buf + 10, format, len);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	/* do transaction and check buf[1-8] are the same against command */
338c2ecf20Sopenharmony_ci	err = fcp_avc_transaction(unit, buf, len + 10, buf, len + 10,
348c2ecf20Sopenharmony_ci				  BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) |
358c2ecf20Sopenharmony_ci				  BIT(6) | BIT(7) | BIT(8));
368c2ecf20Sopenharmony_ci	if (err < 0)
378c2ecf20Sopenharmony_ci		;
388c2ecf20Sopenharmony_ci	else if (err < len + 10)
398c2ecf20Sopenharmony_ci		err = -EIO;
408c2ecf20Sopenharmony_ci	else if (buf[0] == 0x08) /* NOT IMPLEMENTED */
418c2ecf20Sopenharmony_ci		err = -ENXIO;
428c2ecf20Sopenharmony_ci	else if (buf[0] == 0x0a) /* REJECTED */
438c2ecf20Sopenharmony_ci		err = -EINVAL;
448c2ecf20Sopenharmony_ci	else
458c2ecf20Sopenharmony_ci		err = 0;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	kfree(buf);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	return err;
508c2ecf20Sopenharmony_ci}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciint avc_stream_get_format(struct fw_unit *unit,
538c2ecf20Sopenharmony_ci			  enum avc_general_plug_dir dir, unsigned int pid,
548c2ecf20Sopenharmony_ci			  u8 *buf, unsigned int *len, unsigned int eid)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	unsigned int subfunc;
578c2ecf20Sopenharmony_ci	int err;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (eid == 0xff)
608c2ecf20Sopenharmony_ci		subfunc = 0xc0;	/* SINGLE */
618c2ecf20Sopenharmony_ci	else
628c2ecf20Sopenharmony_ci		subfunc = 0xc1;	/* LIST */
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	buf[0] = 0x01;		/* STATUS */
658c2ecf20Sopenharmony_ci	buf[1] = 0xff;		/* UNIT */
668c2ecf20Sopenharmony_ci	buf[2] = 0xbf;		/* EXTENDED STREAM FORMAT INFORMATION */
678c2ecf20Sopenharmony_ci	buf[3] = subfunc;	/* SINGLE or LIST */
688c2ecf20Sopenharmony_ci	buf[4] = dir;		/* Plug Direction */
698c2ecf20Sopenharmony_ci	buf[5] = 0x00;		/* Unit */
708c2ecf20Sopenharmony_ci	buf[6] = 0x00;		/* PCR (Isochronous Plug) */
718c2ecf20Sopenharmony_ci	buf[7] = 0xff & pid;	/* Plug ID */
728c2ecf20Sopenharmony_ci	buf[8] = 0xff;		/* Padding */
738c2ecf20Sopenharmony_ci	buf[9] = 0xff;		/* support status in response */
748c2ecf20Sopenharmony_ci	buf[10] = 0xff & eid;	/* entry ID for LIST subfunction */
758c2ecf20Sopenharmony_ci	buf[11] = 0xff;		/* padding */
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	/* do transaction and check buf[1-7] are the same against command */
788c2ecf20Sopenharmony_ci	err = fcp_avc_transaction(unit, buf, 12, buf, *len,
798c2ecf20Sopenharmony_ci				  BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5) |
808c2ecf20Sopenharmony_ci				  BIT(6) | BIT(7));
818c2ecf20Sopenharmony_ci	if (err < 0)
828c2ecf20Sopenharmony_ci		;
838c2ecf20Sopenharmony_ci	else if (err < 12)
848c2ecf20Sopenharmony_ci		err = -EIO;
858c2ecf20Sopenharmony_ci	else if (buf[0] == 0x08)	/* NOT IMPLEMENTED */
868c2ecf20Sopenharmony_ci		err = -ENXIO;
878c2ecf20Sopenharmony_ci	else if (buf[0] == 0x0a)	/* REJECTED */
888c2ecf20Sopenharmony_ci		err = -EINVAL;
898c2ecf20Sopenharmony_ci	else if (buf[0] == 0x0b)	/* IN TRANSITION */
908c2ecf20Sopenharmony_ci		err = -EAGAIN;
918c2ecf20Sopenharmony_ci	/* LIST subfunction has entry ID */
928c2ecf20Sopenharmony_ci	else if ((subfunc == 0xc1) && (buf[10] != eid))
938c2ecf20Sopenharmony_ci		err = -EIO;
948c2ecf20Sopenharmony_ci	if (err < 0)
958c2ecf20Sopenharmony_ci		goto end;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	/* keep just stream format information */
988c2ecf20Sopenharmony_ci	if (subfunc == 0xc0) {
998c2ecf20Sopenharmony_ci		memmove(buf, buf + 10, err - 10);
1008c2ecf20Sopenharmony_ci		*len = err - 10;
1018c2ecf20Sopenharmony_ci	} else {
1028c2ecf20Sopenharmony_ci		memmove(buf, buf + 11, err - 11);
1038c2ecf20Sopenharmony_ci		*len = err - 11;
1048c2ecf20Sopenharmony_ci	}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	err = 0;
1078c2ecf20Sopenharmony_ciend:
1088c2ecf20Sopenharmony_ci	return err;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciint avc_general_inquiry_sig_fmt(struct fw_unit *unit, unsigned int rate,
1128c2ecf20Sopenharmony_ci				enum avc_general_plug_dir dir,
1138c2ecf20Sopenharmony_ci				unsigned short pid)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	unsigned int sfc;
1168c2ecf20Sopenharmony_ci	u8 *buf;
1178c2ecf20Sopenharmony_ci	int err;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	for (sfc = 0; sfc < CIP_SFC_COUNT; sfc++) {
1208c2ecf20Sopenharmony_ci		if (amdtp_rate_table[sfc] == rate)
1218c2ecf20Sopenharmony_ci			break;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci	if (sfc == CIP_SFC_COUNT)
1248c2ecf20Sopenharmony_ci		return -EINVAL;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	buf = kzalloc(8, GFP_KERNEL);
1278c2ecf20Sopenharmony_ci	if (buf == NULL)
1288c2ecf20Sopenharmony_ci		return -ENOMEM;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	buf[0] = 0x02;		/* SPECIFIC INQUIRY */
1318c2ecf20Sopenharmony_ci	buf[1] = 0xff;		/* UNIT */
1328c2ecf20Sopenharmony_ci	if (dir == AVC_GENERAL_PLUG_DIR_IN)
1338c2ecf20Sopenharmony_ci		buf[2] = 0x19;	/* INPUT PLUG SIGNAL FORMAT */
1348c2ecf20Sopenharmony_ci	else
1358c2ecf20Sopenharmony_ci		buf[2] = 0x18;	/* OUTPUT PLUG SIGNAL FORMAT */
1368c2ecf20Sopenharmony_ci	buf[3] = 0xff & pid;	/* plug id */
1378c2ecf20Sopenharmony_ci	buf[4] = 0x90;		/* EOH_1, Form_1, FMT. AM824 */
1388c2ecf20Sopenharmony_ci	buf[5] = 0x07 & sfc;	/* FDF-hi. AM824, frequency */
1398c2ecf20Sopenharmony_ci	buf[6] = 0xff;		/* FDF-mid. AM824, SYT hi (not used) */
1408c2ecf20Sopenharmony_ci	buf[7] = 0xff;		/* FDF-low. AM824, SYT lo (not used) */
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	/* do transaction and check buf[1-5] are the same against command */
1438c2ecf20Sopenharmony_ci	err = fcp_avc_transaction(unit, buf, 8, buf, 8,
1448c2ecf20Sopenharmony_ci				  BIT(1) | BIT(2) | BIT(3) | BIT(4) | BIT(5));
1458c2ecf20Sopenharmony_ci	if (err < 0)
1468c2ecf20Sopenharmony_ci		;
1478c2ecf20Sopenharmony_ci	else if (err < 8)
1488c2ecf20Sopenharmony_ci		err = -EIO;
1498c2ecf20Sopenharmony_ci	else if (buf[0] == 0x08)	/* NOT IMPLEMENTED */
1508c2ecf20Sopenharmony_ci		err = -ENXIO;
1518c2ecf20Sopenharmony_ci	if (err < 0)
1528c2ecf20Sopenharmony_ci		goto end;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	err = 0;
1558c2ecf20Sopenharmony_ciend:
1568c2ecf20Sopenharmony_ci	kfree(buf);
1578c2ecf20Sopenharmony_ci	return err;
1588c2ecf20Sopenharmony_ci}
159