1 // SPDX-License-Identifier: GPL-2.0-only
3 * fireworks_command.c - a part of driver for Fireworks based devices
5 * Copyright (c) 2013-2014 Takashi Sakamoto
8 #include "./fireworks.h"
11 * This driver uses transaction version 1 or later to use extended hardware
12 * information. Then too old devices are not available.
14 * Each commands are not required to have continuous sequence numbers. This
15 * number is just used to match command and response.
17 * This module support a part of commands. Please see FFADO if you want to see
18 * whole commands. But there are some commands which FFADO don't implement.
20 * Fireworks also supports AV/C general commands and AV/C Stream Format
21 * Information commands. But this module don't use them.
24 #define KERNEL_SEQNUM_MIN (SND_EFW_TRANSACTION_USER_SEQNUM_MAX + 2)
25 #define KERNEL_SEQNUM_MAX ((u32)~0)
27 /* for clock source and sampling rate */
34 /* command categories */
37 EFC_CAT_TRANSPORT
= 2,
41 /* hardware info category commands */
43 EFC_CMD_HWINFO_GET_CAPS
= 0,
44 EFC_CMD_HWINFO_GET_POLLED
= 1,
45 EFC_CMD_HWINFO_SET_RESP_ADDR
= 2
48 enum efc_cmd_transport
{
49 EFC_CMD_TRANSPORT_SET_TX_MODE
= 0
52 /* hardware control category commands */
54 EFC_CMD_HWCTL_SET_CLOCK
= 0,
55 EFC_CMD_HWCTL_GET_CLOCK
= 1,
56 EFC_CMD_HWCTL_IDENTIFY
= 5
59 /* return values in response */
63 EFR_STATUS_BAD_COMMAND
= 2,
64 EFR_STATUS_COMM_ERR
= 3,
65 EFR_STATUS_BAD_QUAD_COUNT
= 4,
66 EFR_STATUS_UNSUPPORTED
= 5,
67 EFR_STATUS_1394_TIMEOUT
= 6,
68 EFR_STATUS_DSP_TIMEOUT
= 7,
69 EFR_STATUS_BAD_RATE
= 8,
70 EFR_STATUS_BAD_CLOCK
= 9,
71 EFR_STATUS_BAD_CHANNEL
= 10,
72 EFR_STATUS_BAD_PAN
= 11,
73 EFR_STATUS_FLASH_BUSY
= 12,
74 EFR_STATUS_BAD_MIRROR
= 13,
75 EFR_STATUS_BAD_LED
= 14,
76 EFR_STATUS_BAD_PARAMETER
= 15,
77 EFR_STATUS_INCOMPLETE
= 0x80000000
80 static const char *const efr_status_names
[] = {
81 [EFR_STATUS_OK
] = "OK",
82 [EFR_STATUS_BAD
] = "bad",
83 [EFR_STATUS_BAD_COMMAND
] = "bad command",
84 [EFR_STATUS_COMM_ERR
] = "comm err",
85 [EFR_STATUS_BAD_QUAD_COUNT
] = "bad quad count",
86 [EFR_STATUS_UNSUPPORTED
] = "unsupported",
87 [EFR_STATUS_1394_TIMEOUT
] = "1394 timeout",
88 [EFR_STATUS_DSP_TIMEOUT
] = "DSP timeout",
89 [EFR_STATUS_BAD_RATE
] = "bad rate",
90 [EFR_STATUS_BAD_CLOCK
] = "bad clock",
91 [EFR_STATUS_BAD_CHANNEL
] = "bad channel",
92 [EFR_STATUS_BAD_PAN
] = "bad pan",
93 [EFR_STATUS_FLASH_BUSY
] = "flash busy",
94 [EFR_STATUS_BAD_MIRROR
] = "bad mirror",
95 [EFR_STATUS_BAD_LED
] = "bad LED",
96 [EFR_STATUS_BAD_PARAMETER
] = "bad parameter",
97 [EFR_STATUS_BAD_PARAMETER
+ 1] = "incomplete"
101 efw_transaction(struct snd_efw
*efw
, unsigned int category
,
102 unsigned int command
,
103 const __be32
*params
, unsigned int param_bytes
,
104 const __be32
*resp
, unsigned int resp_bytes
)
106 struct snd_efw_transaction
*header
;
109 unsigned int buf_bytes
, cmd_bytes
;
112 /* calculate buffer size*/
113 buf_bytes
= sizeof(struct snd_efw_transaction
) +
114 max(param_bytes
, resp_bytes
);
117 buf
= kzalloc(buf_bytes
, GFP_KERNEL
);
121 /* to keep consistency of sequence number */
122 spin_lock(&efw
->lock
);
123 if ((efw
->seqnum
< KERNEL_SEQNUM_MIN
) ||
124 (efw
->seqnum
>= KERNEL_SEQNUM_MAX
- 2))
125 efw
->seqnum
= KERNEL_SEQNUM_MIN
;
128 seqnum
= efw
->seqnum
;
129 spin_unlock(&efw
->lock
);
131 /* fill transaction header fields */
132 cmd_bytes
= sizeof(struct snd_efw_transaction
) + param_bytes
;
133 header
= (struct snd_efw_transaction
*)buf
;
134 header
->length
= cpu_to_be32(cmd_bytes
/ sizeof(__be32
));
135 header
->version
= cpu_to_be32(1);
136 header
->seqnum
= cpu_to_be32(seqnum
);
137 header
->category
= cpu_to_be32(category
);
138 header
->command
= cpu_to_be32(command
);
141 /* fill transaction command parameters */
142 memcpy(header
->params
, params
, param_bytes
);
144 err
= snd_efw_transaction_run(efw
->unit
, buf
, cmd_bytes
,
149 /* check transaction header fields */
150 if ((be32_to_cpu(header
->version
) < 1) ||
151 (be32_to_cpu(header
->category
) != category
) ||
152 (be32_to_cpu(header
->command
) != command
) ||
153 (be32_to_cpu(header
->status
) != EFR_STATUS_OK
)) {
154 dev_err(&efw
->unit
->device
, "EFW command failed [%u/%u]: %s\n",
155 be32_to_cpu(header
->category
),
156 be32_to_cpu(header
->command
),
157 efr_status_names
[be32_to_cpu(header
->status
)]);
165 /* fill transaction response parameters */
166 memset((void *)resp
, 0, resp_bytes
);
167 resp_bytes
= min_t(unsigned int, resp_bytes
,
168 be32_to_cpu(header
->length
) * sizeof(__be32
) -
169 sizeof(struct snd_efw_transaction
));
170 memcpy((void *)resp
, &buf
[6], resp_bytes
);
177 * The address in host system for transaction response is changable when the
178 * device supports. struct hwinfo.flags includes its flag. The default is
179 * MEMORY_SPACE_EFW_RESPONSE.
181 int snd_efw_command_set_resp_addr(struct snd_efw
*efw
,
182 u16 addr_high
, u32 addr_low
)
186 addr
[0] = cpu_to_be32(addr_high
);
187 addr
[1] = cpu_to_be32(addr_low
);
189 if (!efw
->resp_addr_changable
)
192 return efw_transaction(efw
, EFC_CAT_HWCTL
,
193 EFC_CMD_HWINFO_SET_RESP_ADDR
,
194 addr
, sizeof(addr
), NULL
, 0);
198 * This is for timestamp processing. In Windows mode, all 32bit fields of second
199 * CIP header in AMDTP transmit packet is used for 'presentation timestamp'. In
200 * 'no data' packet the value of this field is 0x90ffffff.
202 int snd_efw_command_set_tx_mode(struct snd_efw
*efw
,
203 enum snd_efw_transport_mode mode
)
205 __be32 param
= cpu_to_be32(mode
);
206 return efw_transaction(efw
, EFC_CAT_TRANSPORT
,
207 EFC_CMD_TRANSPORT_SET_TX_MODE
,
208 ¶m
, sizeof(param
), NULL
, 0);
211 int snd_efw_command_get_hwinfo(struct snd_efw
*efw
,
212 struct snd_efw_hwinfo
*hwinfo
)
216 err
= efw_transaction(efw
, EFC_CAT_HWINFO
,
217 EFC_CMD_HWINFO_GET_CAPS
,
218 NULL
, 0, (__be32
*)hwinfo
, sizeof(*hwinfo
));
222 be32_to_cpus(&hwinfo
->flags
);
223 be32_to_cpus(&hwinfo
->guid_hi
);
224 be32_to_cpus(&hwinfo
->guid_lo
);
225 be32_to_cpus(&hwinfo
->type
);
226 be32_to_cpus(&hwinfo
->version
);
227 be32_to_cpus(&hwinfo
->supported_clocks
);
228 be32_to_cpus(&hwinfo
->amdtp_rx_pcm_channels
);
229 be32_to_cpus(&hwinfo
->amdtp_tx_pcm_channels
);
230 be32_to_cpus(&hwinfo
->phys_out
);
231 be32_to_cpus(&hwinfo
->phys_in
);
232 be32_to_cpus(&hwinfo
->phys_out_grp_count
);
233 be32_to_cpus(&hwinfo
->phys_in_grp_count
);
234 be32_to_cpus(&hwinfo
->midi_out_ports
);
235 be32_to_cpus(&hwinfo
->midi_in_ports
);
236 be32_to_cpus(&hwinfo
->max_sample_rate
);
237 be32_to_cpus(&hwinfo
->min_sample_rate
);
238 be32_to_cpus(&hwinfo
->dsp_version
);
239 be32_to_cpus(&hwinfo
->arm_version
);
240 be32_to_cpus(&hwinfo
->mixer_playback_channels
);
241 be32_to_cpus(&hwinfo
->mixer_capture_channels
);
242 be32_to_cpus(&hwinfo
->fpga_version
);
243 be32_to_cpus(&hwinfo
->amdtp_rx_pcm_channels_2x
);
244 be32_to_cpus(&hwinfo
->amdtp_tx_pcm_channels_2x
);
245 be32_to_cpus(&hwinfo
->amdtp_rx_pcm_channels_4x
);
246 be32_to_cpus(&hwinfo
->amdtp_tx_pcm_channels_4x
);
248 /* ensure terminated */
249 hwinfo
->vendor_name
[HWINFO_NAME_SIZE_BYTES
- 1] = '\0';
250 hwinfo
->model_name
[HWINFO_NAME_SIZE_BYTES
- 1] = '\0';
255 int snd_efw_command_get_phys_meters(struct snd_efw
*efw
,
256 struct snd_efw_phys_meters
*meters
,
259 u32
*buf
= (u32
*)meters
;
263 err
= efw_transaction(efw
, EFC_CAT_HWINFO
,
264 EFC_CMD_HWINFO_GET_POLLED
,
265 NULL
, 0, (__be32
*)meters
, len
);
267 for (i
= 0; i
< len
/ sizeof(u32
); i
++)
268 be32_to_cpus(&buf
[i
]);
274 command_get_clock(struct snd_efw
*efw
, struct efc_clock
*clock
)
278 err
= efw_transaction(efw
, EFC_CAT_HWCTL
,
279 EFC_CMD_HWCTL_GET_CLOCK
,
281 (__be32
*)clock
, sizeof(struct efc_clock
));
283 be32_to_cpus(&clock
->source
);
284 be32_to_cpus(&clock
->sampling_rate
);
285 be32_to_cpus(&clock
->index
);
291 /* give UINT_MAX if set nothing */
293 command_set_clock(struct snd_efw
*efw
,
294 unsigned int source
, unsigned int rate
)
296 struct efc_clock clock
= {0};
299 /* check arguments */
300 if ((source
== UINT_MAX
) && (rate
== UINT_MAX
)) {
305 /* get current status */
306 err
= command_get_clock(efw
, &clock
);
311 if ((clock
.source
== source
) && (clock
.sampling_rate
== rate
))
315 if ((source
!= UINT_MAX
) && (clock
.source
!= source
))
316 clock
.source
= source
;
317 if ((rate
!= UINT_MAX
) && (clock
.sampling_rate
!= rate
))
318 clock
.sampling_rate
= rate
;
321 cpu_to_be32s(&clock
.source
);
322 cpu_to_be32s(&clock
.sampling_rate
);
323 cpu_to_be32s(&clock
.index
);
325 err
= efw_transaction(efw
, EFC_CAT_HWCTL
,
326 EFC_CMD_HWCTL_SET_CLOCK
,
327 (__be32
*)&clock
, sizeof(struct efc_clock
),
333 * With firmware version 5.8, just after changing clock state, these
334 * parameters are not immediately retrieved by get command. In my
335 * trial, there needs to be 100msec to get changed parameters.
342 int snd_efw_command_get_clock_source(struct snd_efw
*efw
,
343 enum snd_efw_clock_source
*source
)
346 struct efc_clock clock
= {0};
348 err
= command_get_clock(efw
, &clock
);
350 *source
= clock
.source
;
355 int snd_efw_command_get_sampling_rate(struct snd_efw
*efw
, unsigned int *rate
)
358 struct efc_clock clock
= {0};
360 err
= command_get_clock(efw
, &clock
);
362 *rate
= clock
.sampling_rate
;
367 int snd_efw_command_set_sampling_rate(struct snd_efw
*efw
, unsigned int rate
)
369 return command_set_clock(efw
, UINT_MAX
, rate
);