fireworks_command.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * fireworks_command.c - a part of driver for Fireworks based devices
  4. *
  5. * Copyright (c) 2013-2014 Takashi Sakamoto
  6. */
  7. #include "./fireworks.h"
  8. /*
  9. * This driver uses transaction version 1 or later to use extended hardware
  10. * information. Then too old devices are not available.
  11. *
  12. * Each commands are not required to have continuous sequence numbers. This
  13. * number is just used to match command and response.
  14. *
  15. * This module support a part of commands. Please see FFADO if you want to see
  16. * whole commands. But there are some commands which FFADO don't implement.
  17. *
  18. * Fireworks also supports AV/C general commands and AV/C Stream Format
  19. * Information commands. But this module don't use them.
  20. */
  21. #define KERNEL_SEQNUM_MIN (SND_EFW_TRANSACTION_USER_SEQNUM_MAX + 2)
  22. #define KERNEL_SEQNUM_MAX ((u32)~0)
  23. /* for clock source and sampling rate */
  24. struct efc_clock {
  25. u32 source;
  26. u32 sampling_rate;
  27. u32 index;
  28. };
  29. /* command categories */
  30. enum efc_category {
  31. EFC_CAT_HWINFO = 0,
  32. EFC_CAT_TRANSPORT = 2,
  33. EFC_CAT_HWCTL = 3,
  34. };
  35. /* hardware info category commands */
  36. enum efc_cmd_hwinfo {
  37. EFC_CMD_HWINFO_GET_CAPS = 0,
  38. EFC_CMD_HWINFO_GET_POLLED = 1,
  39. EFC_CMD_HWINFO_SET_RESP_ADDR = 2
  40. };
  41. enum efc_cmd_transport {
  42. EFC_CMD_TRANSPORT_SET_TX_MODE = 0
  43. };
  44. /* hardware control category commands */
  45. enum efc_cmd_hwctl {
  46. EFC_CMD_HWCTL_SET_CLOCK = 0,
  47. EFC_CMD_HWCTL_GET_CLOCK = 1,
  48. EFC_CMD_HWCTL_IDENTIFY = 5
  49. };
  50. /* return values in response */
  51. enum efr_status {
  52. EFR_STATUS_OK = 0,
  53. EFR_STATUS_BAD = 1,
  54. EFR_STATUS_BAD_COMMAND = 2,
  55. EFR_STATUS_COMM_ERR = 3,
  56. EFR_STATUS_BAD_QUAD_COUNT = 4,
  57. EFR_STATUS_UNSUPPORTED = 5,
  58. EFR_STATUS_1394_TIMEOUT = 6,
  59. EFR_STATUS_DSP_TIMEOUT = 7,
  60. EFR_STATUS_BAD_RATE = 8,
  61. EFR_STATUS_BAD_CLOCK = 9,
  62. EFR_STATUS_BAD_CHANNEL = 10,
  63. EFR_STATUS_BAD_PAN = 11,
  64. EFR_STATUS_FLASH_BUSY = 12,
  65. EFR_STATUS_BAD_MIRROR = 13,
  66. EFR_STATUS_BAD_LED = 14,
  67. EFR_STATUS_BAD_PARAMETER = 15,
  68. EFR_STATUS_INCOMPLETE = 0x80000000
  69. };
  70. static const char *const efr_status_names[] = {
  71. [EFR_STATUS_OK] = "OK",
  72. [EFR_STATUS_BAD] = "bad",
  73. [EFR_STATUS_BAD_COMMAND] = "bad command",
  74. [EFR_STATUS_COMM_ERR] = "comm err",
  75. [EFR_STATUS_BAD_QUAD_COUNT] = "bad quad count",
  76. [EFR_STATUS_UNSUPPORTED] = "unsupported",
  77. [EFR_STATUS_1394_TIMEOUT] = "1394 timeout",
  78. [EFR_STATUS_DSP_TIMEOUT] = "DSP timeout",
  79. [EFR_STATUS_BAD_RATE] = "bad rate",
  80. [EFR_STATUS_BAD_CLOCK] = "bad clock",
  81. [EFR_STATUS_BAD_CHANNEL] = "bad channel",
  82. [EFR_STATUS_BAD_PAN] = "bad pan",
  83. [EFR_STATUS_FLASH_BUSY] = "flash busy",
  84. [EFR_STATUS_BAD_MIRROR] = "bad mirror",
  85. [EFR_STATUS_BAD_LED] = "bad LED",
  86. [EFR_STATUS_BAD_PARAMETER] = "bad parameter",
  87. [EFR_STATUS_BAD_PARAMETER + 1] = "incomplete"
  88. };
  89. static int
  90. efw_transaction(struct snd_efw *efw, unsigned int category,
  91. unsigned int command,
  92. const __be32 *params, unsigned int param_bytes,
  93. const __be32 *resp, unsigned int resp_bytes)
  94. {
  95. struct snd_efw_transaction *header;
  96. __be32 *buf;
  97. u32 seqnum;
  98. unsigned int buf_bytes, cmd_bytes;
  99. int err;
  100. /* calculate buffer size*/
  101. buf_bytes = sizeof(struct snd_efw_transaction) +
  102. max(param_bytes, resp_bytes);
  103. /* keep buffer */
  104. buf = kzalloc(buf_bytes, GFP_KERNEL);
  105. if (buf == NULL)
  106. return -ENOMEM;
  107. /* to keep consistency of sequence number */
  108. spin_lock(&efw->lock);
  109. if ((efw->seqnum < KERNEL_SEQNUM_MIN) ||
  110. (efw->seqnum >= KERNEL_SEQNUM_MAX - 2))
  111. efw->seqnum = KERNEL_SEQNUM_MIN;
  112. else
  113. efw->seqnum += 2;
  114. seqnum = efw->seqnum;
  115. spin_unlock(&efw->lock);
  116. /* fill transaction header fields */
  117. cmd_bytes = sizeof(struct snd_efw_transaction) + param_bytes;
  118. header = (struct snd_efw_transaction *)buf;
  119. header->length = cpu_to_be32(cmd_bytes / sizeof(__be32));
  120. header->version = cpu_to_be32(1);
  121. header->seqnum = cpu_to_be32(seqnum);
  122. header->category = cpu_to_be32(category);
  123. header->command = cpu_to_be32(command);
  124. header->status = 0;
  125. /* fill transaction command parameters */
  126. memcpy(header->params, params, param_bytes);
  127. err = snd_efw_transaction_run(efw->unit, buf, cmd_bytes,
  128. buf, buf_bytes);
  129. if (err < 0)
  130. goto end;
  131. /* check transaction header fields */
  132. if ((be32_to_cpu(header->version) < 1) ||
  133. (be32_to_cpu(header->category) != category) ||
  134. (be32_to_cpu(header->command) != command) ||
  135. (be32_to_cpu(header->status) != EFR_STATUS_OK)) {
  136. dev_err(&efw->unit->device, "EFW command failed [%u/%u]: %s\n",
  137. be32_to_cpu(header->category),
  138. be32_to_cpu(header->command),
  139. efr_status_names[be32_to_cpu(header->status)]);
  140. err = -EIO;
  141. goto end;
  142. }
  143. if (resp == NULL)
  144. goto end;
  145. /* fill transaction response parameters */
  146. memset((void *)resp, 0, resp_bytes);
  147. resp_bytes = min_t(unsigned int, resp_bytes,
  148. be32_to_cpu(header->length) * sizeof(__be32) -
  149. sizeof(struct snd_efw_transaction));
  150. memcpy((void *)resp, &buf[6], resp_bytes);
  151. end:
  152. kfree(buf);
  153. return err;
  154. }
  155. /*
  156. * The address in host system for transaction response is changable when the
  157. * device supports. struct hwinfo.flags includes its flag. The default is
  158. * MEMORY_SPACE_EFW_RESPONSE.
  159. */
  160. int snd_efw_command_set_resp_addr(struct snd_efw *efw,
  161. u16 addr_high, u32 addr_low)
  162. {
  163. __be32 addr[2];
  164. addr[0] = cpu_to_be32(addr_high);
  165. addr[1] = cpu_to_be32(addr_low);
  166. if (!efw->resp_addr_changable)
  167. return -ENOSYS;
  168. return efw_transaction(efw, EFC_CAT_HWCTL,
  169. EFC_CMD_HWINFO_SET_RESP_ADDR,
  170. addr, sizeof(addr), NULL, 0);
  171. }
  172. /*
  173. * This is for timestamp processing. In Windows mode, all 32bit fields of second
  174. * CIP header in AMDTP transmit packet is used for 'presentation timestamp'. In
  175. * 'no data' packet the value of this field is 0x90ffffff.
  176. */
  177. int snd_efw_command_set_tx_mode(struct snd_efw *efw,
  178. enum snd_efw_transport_mode mode)
  179. {
  180. __be32 param = cpu_to_be32(mode);
  181. return efw_transaction(efw, EFC_CAT_TRANSPORT,
  182. EFC_CMD_TRANSPORT_SET_TX_MODE,
  183. &param, sizeof(param), NULL, 0);
  184. }
  185. int snd_efw_command_get_hwinfo(struct snd_efw *efw,
  186. struct snd_efw_hwinfo *hwinfo)
  187. {
  188. int err;
  189. err = efw_transaction(efw, EFC_CAT_HWINFO,
  190. EFC_CMD_HWINFO_GET_CAPS,
  191. NULL, 0, (__be32 *)hwinfo, sizeof(*hwinfo));
  192. if (err < 0)
  193. goto end;
  194. be32_to_cpus(&hwinfo->flags);
  195. be32_to_cpus(&hwinfo->guid_hi);
  196. be32_to_cpus(&hwinfo->guid_lo);
  197. be32_to_cpus(&hwinfo->type);
  198. be32_to_cpus(&hwinfo->version);
  199. be32_to_cpus(&hwinfo->supported_clocks);
  200. be32_to_cpus(&hwinfo->amdtp_rx_pcm_channels);
  201. be32_to_cpus(&hwinfo->amdtp_tx_pcm_channels);
  202. be32_to_cpus(&hwinfo->phys_out);
  203. be32_to_cpus(&hwinfo->phys_in);
  204. be32_to_cpus(&hwinfo->phys_out_grp_count);
  205. be32_to_cpus(&hwinfo->phys_in_grp_count);
  206. be32_to_cpus(&hwinfo->midi_out_ports);
  207. be32_to_cpus(&hwinfo->midi_in_ports);
  208. be32_to_cpus(&hwinfo->max_sample_rate);
  209. be32_to_cpus(&hwinfo->min_sample_rate);
  210. be32_to_cpus(&hwinfo->dsp_version);
  211. be32_to_cpus(&hwinfo->arm_version);
  212. be32_to_cpus(&hwinfo->mixer_playback_channels);
  213. be32_to_cpus(&hwinfo->mixer_capture_channels);
  214. be32_to_cpus(&hwinfo->fpga_version);
  215. be32_to_cpus(&hwinfo->amdtp_rx_pcm_channels_2x);
  216. be32_to_cpus(&hwinfo->amdtp_tx_pcm_channels_2x);
  217. be32_to_cpus(&hwinfo->amdtp_rx_pcm_channels_4x);
  218. be32_to_cpus(&hwinfo->amdtp_tx_pcm_channels_4x);
  219. /* ensure terminated */
  220. hwinfo->vendor_name[HWINFO_NAME_SIZE_BYTES - 1] = '\0';
  221. hwinfo->model_name[HWINFO_NAME_SIZE_BYTES - 1] = '\0';
  222. end:
  223. return err;
  224. }
  225. int snd_efw_command_get_phys_meters(struct snd_efw *efw,
  226. struct snd_efw_phys_meters *meters,
  227. unsigned int len)
  228. {
  229. u32 *buf = (u32 *)meters;
  230. unsigned int i;
  231. int err;
  232. err = efw_transaction(efw, EFC_CAT_HWINFO,
  233. EFC_CMD_HWINFO_GET_POLLED,
  234. NULL, 0, (__be32 *)meters, len);
  235. if (err >= 0)
  236. for (i = 0; i < len / sizeof(u32); i++)
  237. be32_to_cpus(&buf[i]);
  238. return err;
  239. }
  240. static int
  241. command_get_clock(struct snd_efw *efw, struct efc_clock *clock)
  242. {
  243. int err;
  244. err = efw_transaction(efw, EFC_CAT_HWCTL,
  245. EFC_CMD_HWCTL_GET_CLOCK,
  246. NULL, 0,
  247. (__be32 *)clock, sizeof(struct efc_clock));
  248. if (err >= 0) {
  249. be32_to_cpus(&clock->source);
  250. be32_to_cpus(&clock->sampling_rate);
  251. be32_to_cpus(&clock->index);
  252. }
  253. return err;
  254. }
  255. /* give UINT_MAX if set nothing */
  256. static int
  257. command_set_clock(struct snd_efw *efw,
  258. unsigned int source, unsigned int rate)
  259. {
  260. struct efc_clock clock = {0};
  261. int err;
  262. /* check arguments */
  263. if ((source == UINT_MAX) && (rate == UINT_MAX)) {
  264. err = -EINVAL;
  265. goto end;
  266. }
  267. /* get current status */
  268. err = command_get_clock(efw, &clock);
  269. if (err < 0)
  270. goto end;
  271. /* no need */
  272. if ((clock.source == source) && (clock.sampling_rate == rate))
  273. goto end;
  274. /* set params */
  275. if ((source != UINT_MAX) && (clock.source != source))
  276. clock.source = source;
  277. if ((rate != UINT_MAX) && (clock.sampling_rate != rate))
  278. clock.sampling_rate = rate;
  279. clock.index = 0;
  280. cpu_to_be32s(&clock.source);
  281. cpu_to_be32s(&clock.sampling_rate);
  282. cpu_to_be32s(&clock.index);
  283. err = efw_transaction(efw, EFC_CAT_HWCTL,
  284. EFC_CMD_HWCTL_SET_CLOCK,
  285. (__be32 *)&clock, sizeof(struct efc_clock),
  286. NULL, 0);
  287. if (err < 0)
  288. goto end;
  289. /*
  290. * With firmware version 5.8, just after changing clock state, these
  291. * parameters are not immediately retrieved by get command. In my
  292. * trial, there needs to be 100msec to get changed parameters.
  293. */
  294. msleep(150);
  295. end:
  296. return err;
  297. }
  298. int snd_efw_command_get_clock_source(struct snd_efw *efw,
  299. enum snd_efw_clock_source *source)
  300. {
  301. int err;
  302. struct efc_clock clock = {0};
  303. err = command_get_clock(efw, &clock);
  304. if (err >= 0)
  305. *source = clock.source;
  306. return err;
  307. }
  308. int snd_efw_command_get_sampling_rate(struct snd_efw *efw, unsigned int *rate)
  309. {
  310. int err;
  311. struct efc_clock clock = {0};
  312. err = command_get_clock(efw, &clock);
  313. if (err >= 0)
  314. *rate = clock.sampling_rate;
  315. return err;
  316. }
  317. int snd_efw_command_set_sampling_rate(struct snd_efw *efw, unsigned int rate)
  318. {
  319. return command_set_clock(efw, UINT_MAX, rate);
  320. }