dice-transaction.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * dice_transaction.c - a part of driver for Dice based devices
  4. *
  5. * Copyright (c) Clemens Ladisch
  6. * Copyright (c) 2014 Takashi Sakamoto
  7. */
  8. #include "dice.h"
  9. static u64 get_subaddr(struct snd_dice *dice, enum snd_dice_addr_type type,
  10. u64 offset)
  11. {
  12. switch (type) {
  13. case SND_DICE_ADDR_TYPE_TX:
  14. offset += dice->tx_offset;
  15. break;
  16. case SND_DICE_ADDR_TYPE_RX:
  17. offset += dice->rx_offset;
  18. break;
  19. case SND_DICE_ADDR_TYPE_SYNC:
  20. offset += dice->sync_offset;
  21. break;
  22. case SND_DICE_ADDR_TYPE_RSRV:
  23. offset += dice->rsrv_offset;
  24. break;
  25. case SND_DICE_ADDR_TYPE_GLOBAL:
  26. default:
  27. offset += dice->global_offset;
  28. break;
  29. }
  30. offset += DICE_PRIVATE_SPACE;
  31. return offset;
  32. }
  33. int snd_dice_transaction_write(struct snd_dice *dice,
  34. enum snd_dice_addr_type type,
  35. unsigned int offset, void *buf, unsigned int len)
  36. {
  37. return snd_fw_transaction(dice->unit,
  38. (len == 4) ? TCODE_WRITE_QUADLET_REQUEST :
  39. TCODE_WRITE_BLOCK_REQUEST,
  40. get_subaddr(dice, type, offset), buf, len, 0);
  41. }
  42. int snd_dice_transaction_read(struct snd_dice *dice,
  43. enum snd_dice_addr_type type, unsigned int offset,
  44. void *buf, unsigned int len)
  45. {
  46. return snd_fw_transaction(dice->unit,
  47. (len == 4) ? TCODE_READ_QUADLET_REQUEST :
  48. TCODE_READ_BLOCK_REQUEST,
  49. get_subaddr(dice, type, offset), buf, len, 0);
  50. }
  51. static unsigned int get_clock_info(struct snd_dice *dice, __be32 *info)
  52. {
  53. return snd_dice_transaction_read_global(dice, GLOBAL_CLOCK_SELECT,
  54. info, 4);
  55. }
  56. int snd_dice_transaction_get_clock_source(struct snd_dice *dice,
  57. unsigned int *source)
  58. {
  59. __be32 info;
  60. int err;
  61. err = get_clock_info(dice, &info);
  62. if (err >= 0)
  63. *source = be32_to_cpu(info) & CLOCK_SOURCE_MASK;
  64. return err;
  65. }
  66. int snd_dice_transaction_get_rate(struct snd_dice *dice, unsigned int *rate)
  67. {
  68. __be32 info;
  69. unsigned int index;
  70. int err;
  71. err = get_clock_info(dice, &info);
  72. if (err < 0)
  73. goto end;
  74. index = (be32_to_cpu(info) & CLOCK_RATE_MASK) >> CLOCK_RATE_SHIFT;
  75. if (index >= SND_DICE_RATES_COUNT) {
  76. err = -ENOSYS;
  77. goto end;
  78. }
  79. *rate = snd_dice_rates[index];
  80. end:
  81. return err;
  82. }
  83. int snd_dice_transaction_set_enable(struct snd_dice *dice)
  84. {
  85. __be32 value;
  86. int err = 0;
  87. if (dice->global_enabled)
  88. goto end;
  89. value = cpu_to_be32(1);
  90. err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  91. get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
  92. GLOBAL_ENABLE),
  93. &value, 4,
  94. FW_FIXED_GENERATION | dice->owner_generation);
  95. if (err < 0)
  96. goto end;
  97. dice->global_enabled = true;
  98. end:
  99. return err;
  100. }
  101. void snd_dice_transaction_clear_enable(struct snd_dice *dice)
  102. {
  103. __be32 value;
  104. value = 0;
  105. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  106. get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
  107. GLOBAL_ENABLE),
  108. &value, 4, FW_QUIET |
  109. FW_FIXED_GENERATION | dice->owner_generation);
  110. dice->global_enabled = false;
  111. }
  112. static void dice_notification(struct fw_card *card, struct fw_request *request,
  113. int tcode, int destination, int source,
  114. int generation, unsigned long long offset,
  115. void *data, size_t length, void *callback_data)
  116. {
  117. struct snd_dice *dice = callback_data;
  118. u32 bits;
  119. unsigned long flags;
  120. if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
  121. fw_send_response(card, request, RCODE_TYPE_ERROR);
  122. return;
  123. }
  124. if ((offset & 3) != 0) {
  125. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  126. return;
  127. }
  128. bits = be32_to_cpup(data);
  129. spin_lock_irqsave(&dice->lock, flags);
  130. dice->notification_bits |= bits;
  131. spin_unlock_irqrestore(&dice->lock, flags);
  132. fw_send_response(card, request, RCODE_COMPLETE);
  133. if (bits & NOTIFY_CLOCK_ACCEPTED)
  134. complete(&dice->clock_accepted);
  135. wake_up(&dice->hwdep_wait);
  136. }
  137. static int register_notification_address(struct snd_dice *dice, bool retry)
  138. {
  139. struct fw_device *device = fw_parent_device(dice->unit);
  140. __be64 *buffer;
  141. unsigned int retries;
  142. int err;
  143. retries = (retry) ? 3 : 0;
  144. buffer = kmalloc(2 * 8, GFP_KERNEL);
  145. if (!buffer)
  146. return -ENOMEM;
  147. for (;;) {
  148. buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
  149. buffer[1] = cpu_to_be64(
  150. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  151. dice->notification_handler.offset);
  152. dice->owner_generation = device->generation;
  153. smp_rmb(); /* node_id vs. generation */
  154. err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  155. get_subaddr(dice,
  156. SND_DICE_ADDR_TYPE_GLOBAL,
  157. GLOBAL_OWNER),
  158. buffer, 2 * 8,
  159. FW_FIXED_GENERATION |
  160. dice->owner_generation);
  161. if (err == 0) {
  162. /* success */
  163. if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER))
  164. break;
  165. /* The address seems to be already registered. */
  166. if (buffer[0] == buffer[1])
  167. break;
  168. dev_err(&dice->unit->device,
  169. "device is already in use\n");
  170. err = -EBUSY;
  171. }
  172. if (err != -EAGAIN || retries-- > 0)
  173. break;
  174. msleep(20);
  175. }
  176. kfree(buffer);
  177. if (err < 0)
  178. dice->owner_generation = -1;
  179. return err;
  180. }
  181. static void unregister_notification_address(struct snd_dice *dice)
  182. {
  183. struct fw_device *device = fw_parent_device(dice->unit);
  184. __be64 *buffer;
  185. buffer = kmalloc(2 * 8, GFP_KERNEL);
  186. if (buffer == NULL)
  187. return;
  188. buffer[0] = cpu_to_be64(
  189. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  190. dice->notification_handler.offset);
  191. buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
  192. snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  193. get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
  194. GLOBAL_OWNER),
  195. buffer, 2 * 8, FW_QUIET |
  196. FW_FIXED_GENERATION | dice->owner_generation);
  197. kfree(buffer);
  198. dice->owner_generation = -1;
  199. }
  200. void snd_dice_transaction_destroy(struct snd_dice *dice)
  201. {
  202. struct fw_address_handler *handler = &dice->notification_handler;
  203. if (handler->callback_data == NULL)
  204. return;
  205. unregister_notification_address(dice);
  206. fw_core_remove_address_handler(handler);
  207. handler->callback_data = NULL;
  208. }
  209. int snd_dice_transaction_reinit(struct snd_dice *dice)
  210. {
  211. struct fw_address_handler *handler = &dice->notification_handler;
  212. if (handler->callback_data == NULL)
  213. return -EINVAL;
  214. return register_notification_address(dice, false);
  215. }
  216. static int get_subaddrs(struct snd_dice *dice)
  217. {
  218. static const int min_values[10] = {
  219. 10, 0x60 / 4,
  220. 10, 0x18 / 4,
  221. 10, 0x18 / 4,
  222. 0, 0,
  223. 0, 0,
  224. };
  225. __be32 *pointers;
  226. __be32 version;
  227. u32 data;
  228. unsigned int i;
  229. int err;
  230. pointers = kmalloc_array(ARRAY_SIZE(min_values), sizeof(__be32),
  231. GFP_KERNEL);
  232. if (pointers == NULL)
  233. return -ENOMEM;
  234. /*
  235. * Check that the sub address spaces exist and are located inside the
  236. * private address space. The minimum values are chosen so that all
  237. * minimally required registers are included.
  238. */
  239. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  240. DICE_PRIVATE_SPACE, pointers,
  241. sizeof(__be32) * ARRAY_SIZE(min_values), 0);
  242. if (err < 0)
  243. goto end;
  244. for (i = 0; i < ARRAY_SIZE(min_values); ++i) {
  245. data = be32_to_cpu(pointers[i]);
  246. if (data < min_values[i] || data >= 0x40000) {
  247. err = -ENODEV;
  248. goto end;
  249. }
  250. }
  251. if (be32_to_cpu(pointers[1]) > 0x18) {
  252. /*
  253. * Check that the implemented DICE driver specification major
  254. * version number matches.
  255. */
  256. err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
  257. DICE_PRIVATE_SPACE +
  258. be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
  259. &version, sizeof(version), 0);
  260. if (err < 0)
  261. goto end;
  262. if ((version & cpu_to_be32(0xff000000)) !=
  263. cpu_to_be32(0x01000000)) {
  264. dev_err(&dice->unit->device,
  265. "unknown DICE version: 0x%08x\n",
  266. be32_to_cpu(version));
  267. err = -ENODEV;
  268. goto end;
  269. }
  270. /* Set up later. */
  271. dice->clock_caps = 1;
  272. }
  273. dice->global_offset = be32_to_cpu(pointers[0]) * 4;
  274. dice->tx_offset = be32_to_cpu(pointers[2]) * 4;
  275. dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
  276. /* Old firmware doesn't support these fields. */
  277. if (pointers[7])
  278. dice->sync_offset = be32_to_cpu(pointers[6]) * 4;
  279. if (pointers[9])
  280. dice->rsrv_offset = be32_to_cpu(pointers[8]) * 4;
  281. end:
  282. kfree(pointers);
  283. return err;
  284. }
  285. int snd_dice_transaction_init(struct snd_dice *dice)
  286. {
  287. struct fw_address_handler *handler = &dice->notification_handler;
  288. int err;
  289. err = get_subaddrs(dice);
  290. if (err < 0)
  291. return err;
  292. /* Allocation callback in address space over host controller */
  293. handler->length = 4;
  294. handler->address_callback = dice_notification;
  295. handler->callback_data = dice;
  296. err = fw_core_add_address_handler(handler, &fw_high_memory_region);
  297. if (err < 0) {
  298. handler->callback_data = NULL;
  299. return err;
  300. }
  301. /* Register the address space */
  302. err = register_notification_address(dice, true);
  303. if (err < 0) {
  304. fw_core_remove_address_handler(handler);
  305. handler->callback_data = NULL;
  306. }
  307. return err;
  308. }