dice-transaction.c 8.5 KB

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