Path: blob/master/sound/firewire/fireworks/fireworks_command.c
26444 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* fireworks_command.c - a part of driver for Fireworks based devices3*4* Copyright (c) 2013-2014 Takashi Sakamoto5*/67#include "./fireworks.h"89/*10* This driver uses transaction version 1 or later to use extended hardware11* information. Then too old devices are not available.12*13* Each commands are not required to have continuous sequence numbers. This14* number is just used to match command and response.15*16* This module support a part of commands. Please see FFADO if you want to see17* whole commands. But there are some commands which FFADO don't implement.18*19* Fireworks also supports AV/C general commands and AV/C Stream Format20* Information commands. But this module don't use them.21*/2223#define KERNEL_SEQNUM_MIN (SND_EFW_TRANSACTION_USER_SEQNUM_MAX + 2)24#define KERNEL_SEQNUM_MAX ((u32)~0)2526/* for clock source and sampling rate */27struct efc_clock {28u32 source;29u32 sampling_rate;30u32 index;31};3233/* command categories */34enum efc_category {35EFC_CAT_HWINFO = 0,36EFC_CAT_TRANSPORT = 2,37EFC_CAT_HWCTL = 3,38};3940/* hardware info category commands */41enum efc_cmd_hwinfo {42EFC_CMD_HWINFO_GET_CAPS = 0,43EFC_CMD_HWINFO_GET_POLLED = 1,44EFC_CMD_HWINFO_SET_RESP_ADDR = 245};4647enum efc_cmd_transport {48EFC_CMD_TRANSPORT_SET_TX_MODE = 049};5051/* hardware control category commands */52enum efc_cmd_hwctl {53EFC_CMD_HWCTL_SET_CLOCK = 0,54EFC_CMD_HWCTL_GET_CLOCK = 1,55EFC_CMD_HWCTL_IDENTIFY = 556};5758/* return values in response */59enum efr_status {60EFR_STATUS_OK = 0,61EFR_STATUS_BAD = 1,62EFR_STATUS_BAD_COMMAND = 2,63EFR_STATUS_COMM_ERR = 3,64EFR_STATUS_BAD_QUAD_COUNT = 4,65EFR_STATUS_UNSUPPORTED = 5,66EFR_STATUS_1394_TIMEOUT = 6,67EFR_STATUS_DSP_TIMEOUT = 7,68EFR_STATUS_BAD_RATE = 8,69EFR_STATUS_BAD_CLOCK = 9,70EFR_STATUS_BAD_CHANNEL = 10,71EFR_STATUS_BAD_PAN = 11,72EFR_STATUS_FLASH_BUSY = 12,73EFR_STATUS_BAD_MIRROR = 13,74EFR_STATUS_BAD_LED = 14,75EFR_STATUS_BAD_PARAMETER = 15,76EFR_STATUS_INCOMPLETE = 0x8000000077};7879static const char *const efr_status_names[] = {80[EFR_STATUS_OK] = "OK",81[EFR_STATUS_BAD] = "bad",82[EFR_STATUS_BAD_COMMAND] = "bad command",83[EFR_STATUS_COMM_ERR] = "comm err",84[EFR_STATUS_BAD_QUAD_COUNT] = "bad quad count",85[EFR_STATUS_UNSUPPORTED] = "unsupported",86[EFR_STATUS_1394_TIMEOUT] = "1394 timeout",87[EFR_STATUS_DSP_TIMEOUT] = "DSP timeout",88[EFR_STATUS_BAD_RATE] = "bad rate",89[EFR_STATUS_BAD_CLOCK] = "bad clock",90[EFR_STATUS_BAD_CHANNEL] = "bad channel",91[EFR_STATUS_BAD_PAN] = "bad pan",92[EFR_STATUS_FLASH_BUSY] = "flash busy",93[EFR_STATUS_BAD_MIRROR] = "bad mirror",94[EFR_STATUS_BAD_LED] = "bad LED",95[EFR_STATUS_BAD_PARAMETER] = "bad parameter",96[EFR_STATUS_BAD_PARAMETER + 1] = "incomplete"97};9899static int100efw_transaction(struct snd_efw *efw, unsigned int category,101unsigned int command,102const __be32 *params, unsigned int param_bytes,103const __be32 *resp, unsigned int resp_bytes)104{105struct snd_efw_transaction *header;106__be32 *buf;107u32 seqnum;108unsigned int buf_bytes, cmd_bytes;109int err;110111/* calculate buffer size*/112buf_bytes = sizeof(struct snd_efw_transaction) +113max(param_bytes, resp_bytes);114115/* keep buffer */116buf = kzalloc(buf_bytes, GFP_KERNEL);117if (buf == NULL)118return -ENOMEM;119120/* to keep consistency of sequence number */121spin_lock(&efw->lock);122if ((efw->seqnum < KERNEL_SEQNUM_MIN) ||123(efw->seqnum >= KERNEL_SEQNUM_MAX - 2))124efw->seqnum = KERNEL_SEQNUM_MIN;125else126efw->seqnum += 2;127seqnum = efw->seqnum;128spin_unlock(&efw->lock);129130/* fill transaction header fields */131cmd_bytes = sizeof(struct snd_efw_transaction) + param_bytes;132header = (struct snd_efw_transaction *)buf;133header->length = cpu_to_be32(cmd_bytes / sizeof(__be32));134header->version = cpu_to_be32(1);135header->seqnum = cpu_to_be32(seqnum);136header->category = cpu_to_be32(category);137header->command = cpu_to_be32(command);138header->status = 0;139140/* fill transaction command parameters */141memcpy(header->params, params, param_bytes);142143err = snd_efw_transaction_run(efw->unit, buf, cmd_bytes,144buf, buf_bytes);145if (err < 0)146goto end;147148/* check transaction header fields */149if ((be32_to_cpu(header->version) < 1) ||150(be32_to_cpu(header->category) != category) ||151(be32_to_cpu(header->command) != command) ||152(be32_to_cpu(header->status) != EFR_STATUS_OK)) {153dev_err(&efw->unit->device, "EFW command failed [%u/%u]: %s\n",154be32_to_cpu(header->category),155be32_to_cpu(header->command),156efr_status_names[be32_to_cpu(header->status)]);157err = -EIO;158goto end;159}160161if (resp == NULL)162goto end;163164/* fill transaction response parameters */165memset((void *)resp, 0, resp_bytes);166resp_bytes = min_t(unsigned int, resp_bytes,167be32_to_cpu(header->length) * sizeof(__be32) -168sizeof(struct snd_efw_transaction));169memcpy((void *)resp, &buf[6], resp_bytes);170end:171kfree(buf);172return err;173}174175/*176* The address in host system for transaction response is changable when the177* device supports. struct hwinfo.flags includes its flag. The default is178* MEMORY_SPACE_EFW_RESPONSE.179*/180int snd_efw_command_set_resp_addr(struct snd_efw *efw,181u16 addr_high, u32 addr_low)182{183__be32 addr[2];184185addr[0] = cpu_to_be32(addr_high);186addr[1] = cpu_to_be32(addr_low);187188if (!efw->resp_addr_changable)189return -ENOSYS;190191return efw_transaction(efw, EFC_CAT_HWCTL,192EFC_CMD_HWINFO_SET_RESP_ADDR,193addr, sizeof(addr), NULL, 0);194}195196/*197* This is for timestamp processing. In Windows mode, all 32bit fields of second198* CIP header in AMDTP transmit packet is used for 'presentation timestamp'. In199* 'no data' packet the value of this field is 0x90ffffff.200*/201int snd_efw_command_set_tx_mode(struct snd_efw *efw,202enum snd_efw_transport_mode mode)203{204__be32 param = cpu_to_be32(mode);205return efw_transaction(efw, EFC_CAT_TRANSPORT,206EFC_CMD_TRANSPORT_SET_TX_MODE,207¶m, sizeof(param), NULL, 0);208}209210int snd_efw_command_get_hwinfo(struct snd_efw *efw,211struct snd_efw_hwinfo *hwinfo)212{213int err;214215err = efw_transaction(efw, EFC_CAT_HWINFO,216EFC_CMD_HWINFO_GET_CAPS,217NULL, 0, (__be32 *)hwinfo, sizeof(*hwinfo));218if (err < 0)219goto end;220221be32_to_cpus(&hwinfo->flags);222be32_to_cpus(&hwinfo->guid_hi);223be32_to_cpus(&hwinfo->guid_lo);224be32_to_cpus(&hwinfo->type);225be32_to_cpus(&hwinfo->version);226be32_to_cpus(&hwinfo->supported_clocks);227be32_to_cpus(&hwinfo->amdtp_rx_pcm_channels);228be32_to_cpus(&hwinfo->amdtp_tx_pcm_channels);229be32_to_cpus(&hwinfo->phys_out);230be32_to_cpus(&hwinfo->phys_in);231be32_to_cpus(&hwinfo->phys_out_grp_count);232be32_to_cpus(&hwinfo->phys_in_grp_count);233be32_to_cpus(&hwinfo->midi_out_ports);234be32_to_cpus(&hwinfo->midi_in_ports);235be32_to_cpus(&hwinfo->max_sample_rate);236be32_to_cpus(&hwinfo->min_sample_rate);237be32_to_cpus(&hwinfo->dsp_version);238be32_to_cpus(&hwinfo->arm_version);239be32_to_cpus(&hwinfo->mixer_playback_channels);240be32_to_cpus(&hwinfo->mixer_capture_channels);241be32_to_cpus(&hwinfo->fpga_version);242be32_to_cpus(&hwinfo->amdtp_rx_pcm_channels_2x);243be32_to_cpus(&hwinfo->amdtp_tx_pcm_channels_2x);244be32_to_cpus(&hwinfo->amdtp_rx_pcm_channels_4x);245be32_to_cpus(&hwinfo->amdtp_tx_pcm_channels_4x);246247/* ensure terminated */248hwinfo->vendor_name[HWINFO_NAME_SIZE_BYTES - 1] = '\0';249hwinfo->model_name[HWINFO_NAME_SIZE_BYTES - 1] = '\0';250end:251return err;252}253254int snd_efw_command_get_phys_meters(struct snd_efw *efw,255struct snd_efw_phys_meters *meters,256unsigned int len)257{258u32 *buf = (u32 *)meters;259unsigned int i;260int err;261262err = efw_transaction(efw, EFC_CAT_HWINFO,263EFC_CMD_HWINFO_GET_POLLED,264NULL, 0, (__be32 *)meters, len);265if (err >= 0)266for (i = 0; i < len / sizeof(u32); i++)267be32_to_cpus(&buf[i]);268269return err;270}271272static int273command_get_clock(struct snd_efw *efw, struct efc_clock *clock)274{275int err;276277err = efw_transaction(efw, EFC_CAT_HWCTL,278EFC_CMD_HWCTL_GET_CLOCK,279NULL, 0,280(__be32 *)clock, sizeof(struct efc_clock));281if (err >= 0) {282be32_to_cpus(&clock->source);283be32_to_cpus(&clock->sampling_rate);284be32_to_cpus(&clock->index);285}286287return err;288}289290/* give UINT_MAX if set nothing */291static int292command_set_clock(struct snd_efw *efw,293unsigned int source, unsigned int rate)294{295struct efc_clock clock = {0};296int err;297298/* check arguments */299if ((source == UINT_MAX) && (rate == UINT_MAX)) {300err = -EINVAL;301goto end;302}303304/* get current status */305err = command_get_clock(efw, &clock);306if (err < 0)307goto end;308309/* no need */310if ((clock.source == source) && (clock.sampling_rate == rate))311goto end;312313/* set params */314if ((source != UINT_MAX) && (clock.source != source))315clock.source = source;316if ((rate != UINT_MAX) && (clock.sampling_rate != rate))317clock.sampling_rate = rate;318clock.index = 0;319320cpu_to_be32s(&clock.source);321cpu_to_be32s(&clock.sampling_rate);322cpu_to_be32s(&clock.index);323324err = efw_transaction(efw, EFC_CAT_HWCTL,325EFC_CMD_HWCTL_SET_CLOCK,326(__be32 *)&clock, sizeof(struct efc_clock),327NULL, 0);328if (err < 0)329goto end;330331/*332* With firmware version 5.8, just after changing clock state, these333* parameters are not immediately retrieved by get command. In my334* trial, there needs to be 100msec to get changed parameters.335*/336msleep(150);337end:338return err;339}340341int snd_efw_command_get_clock_source(struct snd_efw *efw,342enum snd_efw_clock_source *source)343{344int err;345struct efc_clock clock = {0};346347err = command_get_clock(efw, &clock);348if (err >= 0)349*source = clock.source;350351return err;352}353354int snd_efw_command_get_sampling_rate(struct snd_efw *efw, unsigned int *rate)355{356int err;357struct efc_clock clock = {0};358359err = command_get_clock(efw, &clock);360if (err >= 0)361*rate = clock.sampling_rate;362363return err;364}365366int snd_efw_command_set_sampling_rate(struct snd_efw *efw, unsigned int rate)367{368return command_set_clock(efw, UINT_MAX, rate);369}370371372373