fireworks_command.c 9.8 KB

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