dice-transaction.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. #define NOTIFICATION_TIMEOUT_MS 100
  11. static u64 get_subaddr(struct snd_dice *dice, enum snd_dice_addr_type type,
  12. u64 offset)
  13. {
  14. switch (type) {
  15. case SND_DICE_ADDR_TYPE_TX:
  16. offset += dice->tx_offset;
  17. break;
  18. case SND_DICE_ADDR_TYPE_RX:
  19. offset += dice->rx_offset;
  20. break;
  21. case SND_DICE_ADDR_TYPE_SYNC:
  22. offset += dice->sync_offset;
  23. break;
  24. case SND_DICE_ADDR_TYPE_RSRV:
  25. offset += dice->rsrv_offset;
  26. break;
  27. case SND_DICE_ADDR_TYPE_GLOBAL:
  28. default:
  29. offset += dice->global_offset;
  30. break;
  31. }
  32. offset += DICE_PRIVATE_SPACE;
  33. return offset;
  34. }
  35. int snd_dice_transaction_write(struct snd_dice *dice,
  36. enum snd_dice_addr_type type,
  37. unsigned int offset, void *buf, unsigned int len)
  38. {
  39. return snd_fw_transaction(dice->unit,
  40. (len == 4) ? TCODE_WRITE_QUADLET_REQUEST :
  41. TCODE_WRITE_BLOCK_REQUEST,
  42. get_subaddr(dice, type, offset), buf, len, 0);
  43. }
  44. int snd_dice_transaction_read(struct snd_dice *dice,
  45. enum snd_dice_addr_type type, unsigned int offset,
  46. void *buf, unsigned int len)
  47. {
  48. return snd_fw_transaction(dice->unit,
  49. (len == 4) ? TCODE_READ_QUADLET_REQUEST :
  50. TCODE_READ_BLOCK_REQUEST,
  51. get_subaddr(dice, type, offset), buf, len, 0);
  52. }
  53. static unsigned int get_clock_info(struct snd_dice *dice, __be32 *info)
  54. {
  55. return snd_dice_transaction_read_global(dice, GLOBAL_CLOCK_SELECT,
  56. info, 4);
  57. }
  58. static int set_clock_info(struct snd_dice *dice,
  59. unsigned int rate, unsigned int source)
  60. {
  61. unsigned int retries = 3;
  62. unsigned int i;
  63. __be32 info;
  64. u32 mask;
  65. u32 clock;
  66. int err;
  67. retry:
  68. err = get_clock_info(dice, &info);
  69. if (err < 0)
  70. goto end;
  71. clock = be32_to_cpu(info);
  72. if (source != UINT_MAX) {
  73. mask = CLOCK_SOURCE_MASK;
  74. clock &= ~mask;
  75. clock |= source;
  76. }
  77. if (rate != UINT_MAX) {
  78. for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) {
  79. if (snd_dice_rates[i] == rate)
  80. break;
  81. }
  82. if (i == ARRAY_SIZE(snd_dice_rates)) {
  83. err = -EINVAL;
  84. goto end;
  85. }
  86. mask = CLOCK_RATE_MASK;
  87. clock &= ~mask;
  88. clock |= i << CLOCK_RATE_SHIFT;
  89. }
  90. info = cpu_to_be32(clock);
  91. if (completion_done(&dice->clock_accepted))
  92. reinit_completion(&dice->clock_accepted);
  93. err = snd_dice_transaction_write_global(dice, GLOBAL_CLOCK_SELECT,
  94. &info, 4);
  95. if (err < 0)
  96. goto end;
  97. /* Timeout means it's invalid request, probably bus reset occurred. */
  98. if (wait_for_completion_timeout(&dice->clock_accepted,
  99. msecs_to_jiffies(NOTIFICATION_TIMEOUT_MS)) == 0) {
  100. if (retries-- == 0) {
  101. err = -ETIMEDOUT;
  102. goto end;
  103. }
  104. err = snd_dice_transaction_reinit(dice);
  105. if (err < 0)
  106. goto end;
  107. msleep(500); /* arbitrary */
  108. goto retry;
  109. }
  110. end:
  111. return err;
  112. }
  113. int snd_dice_transaction_get_clock_source(struct snd_dice *dice,
  114. unsigned int *source)
  115. {
  116. __be32 info;
  117. int err;
  118. err = get_clock_info(dice, &info);
  119. if (err >= 0)
  120. *source = be32_to_cpu(info) & CLOCK_SOURCE_MASK;
  121. return err;
  122. }
  123. int snd_dice_transaction_get_rate(struct snd_dice *dice, unsigned int *rate)
  124. {
  125. __be32 info;
  126. unsigned int index;
  127. int err;
  128. err = get_clock_info(dice, &info);
  129. if (err < 0)
  130. goto end;
  131. index = (be32_to_cpu(info) & CLOCK_RATE_MASK) >> CLOCK_RATE_SHIFT;
  132. if (index >= SND_DICE_RATES_COUNT) {
  133. err = -ENOSYS;
  134. goto end;
  135. }
  136. *rate = snd_dice_rates[index];
  137. end:
  138. return err;
  139. }
  140. int snd_dice_transaction_set_rate(struct snd_dice *dice, unsigned int rate)
  141. {
  142. return set_clock_info(dice, rate, UINT_MAX);
  143. }
  144. int snd_dice_transaction_set_enable(struct snd_dice *dice)
  145. {
  146. __be32 value;
  147. int err = 0;
  148. if (dice->global_enabled)
  149. goto end;
  150. value = cpu_to_be32(1);
  151. err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  152. get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
  153. GLOBAL_ENABLE),
  154. &value, 4,
  155. FW_FIXED_GENERATION | dice->owner_generation);
  156. if (err < 0)
  157. goto end;
  158. dice->global_enabled = true;
  159. end:
  160. return err;
  161. }
  162. void snd_dice_transaction_clear_enable(struct snd_dice *dice)
  163. {
  164. __be32 value;
  165. value = 0;
  166. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  167. get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
  168. GLOBAL_ENABLE),
  169. &value, 4, FW_QUIET |
  170. FW_FIXED_GENERATION | dice->owner_generation);
  171. dice->global_enabled = false;
  172. }
  173. static void dice_notification(struct fw_card *card, struct fw_request *request,
  174. int tcode, int destination, int source,
  175. int generation, unsigned long long offset,
  176. void *data, size_t length, void *callback_data)
  177. {
  178. struct snd_dice *dice = callback_data;
  179. u32 bits;
  180. unsigned long flags;
  181. if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
  182. fw_send_response(card, request, RCODE_TYPE_ERROR);
  183. return;
  184. }
  185. if ((offset & 3) != 0) {
  186. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  187. return;
  188. }
  189. bits = be32_to_cpup(data);
  190. spin_lock_irqsave(&dice->lock, flags);
  191. dice->notification_bits |= bits;
  192. spin_unlock_irqrestore(&dice->lock, flags);
  193. fw_send_response(card, request, RCODE_COMPLETE);
  194. if (bits & NOTIFY_CLOCK_ACCEPTED)
  195. complete(&dice->clock_accepted);
  196. wake_up(&dice->hwdep_wait);
  197. }
  198. static int register_notification_address(struct snd_dice *dice, bool retry)
  199. {
  200. struct fw_device *device = fw_parent_device(dice->unit);
  201. __be64 *buffer;
  202. unsigned int retries;
  203. int err;
  204. retries = (retry) ? 3 : 0;
  205. buffer = kmalloc(2 * 8, GFP_KERNEL);
  206. if (!buffer)
  207. return -ENOMEM;
  208. for (;;) {
  209. buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
  210. buffer[1] = cpu_to_be64(
  211. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  212. dice->notification_handler.offset);
  213. dice->owner_generation = device->generation;
  214. smp_rmb(); /* node_id vs. generation */
  215. err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  216. get_subaddr(dice,
  217. SND_DICE_ADDR_TYPE_GLOBAL,
  218. GLOBAL_OWNER),
  219. buffer, 2 * 8,
  220. FW_FIXED_GENERATION |
  221. dice->owner_generation);
  222. if (err == 0) {
  223. /* success */
  224. if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER))
  225. break;
  226. /* The address seems to be already registered. */
  227. if (buffer[0] == buffer[1])
  228. break;
  229. dev_err(&dice->unit->device,
  230. "device is already in use\n");
  231. err = -EBUSY;
  232. }
  233. if (err != -EAGAIN || retries-- > 0)
  234. break;
  235. msleep(20);
  236. }
  237. kfree(buffer);
  238. if (err < 0)
  239. dice->owner_generation = -1;
  240. return err;
  241. }
  242. static void unregister_notification_address(struct snd_dice *dice)
  243. {
  244. struct fw_device *device = fw_parent_device(dice->unit);
  245. __be64 *buffer;
  246. buffer = kmalloc(2 * 8, GFP_KERNEL);
  247. if (buffer == NULL)
  248. return;
  249. buffer[0] = cpu_to_be64(
  250. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  251. dice->notification_handler.offset);
  252. buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
  253. snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  254. get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
  255. GLOBAL_OWNER),
  256. buffer, 2 * 8, FW_QUIET |
  257. FW_FIXED_GENERATION | dice->owner_generation);
  258. kfree(buffer);
  259. dice->owner_generation = -1;
  260. }
  261. void snd_dice_transaction_destroy(struct snd_dice *dice)
  262. {
  263. struct fw_address_handler *handler = &dice->notification_handler;
  264. if (handler->callback_data == NULL)
  265. return;
  266. unregister_notification_address(dice);
  267. fw_core_remove_address_handler(handler);
  268. handler->callback_data = NULL;
  269. }
  270. int snd_dice_transaction_reinit(struct snd_dice *dice)
  271. {
  272. struct fw_address_handler *handler = &dice->notification_handler;
  273. if (handler->callback_data == NULL)
  274. return -EINVAL;
  275. return register_notification_address(dice, false);
  276. }
  277. int snd_dice_transaction_init(struct snd_dice *dice)
  278. {
  279. struct fw_address_handler *handler = &dice->notification_handler;
  280. __be32 *pointers;
  281. int err;
  282. /* Use the same way which dice_interface_check() does. */
  283. pointers = kmalloc(sizeof(__be32) * 10, GFP_KERNEL);
  284. if (pointers == NULL)
  285. return -ENOMEM;
  286. /* Get offsets for sub-addresses */
  287. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  288. DICE_PRIVATE_SPACE,
  289. pointers, sizeof(__be32) * 10, 0);
  290. if (err < 0)
  291. goto end;
  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. goto end;
  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. goto end;
  307. }
  308. dice->global_offset = be32_to_cpu(pointers[0]) * 4;
  309. dice->tx_offset = be32_to_cpu(pointers[2]) * 4;
  310. dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
  311. dice->sync_offset = be32_to_cpu(pointers[6]) * 4;
  312. dice->rsrv_offset = be32_to_cpu(pointers[8]) * 4;
  313. /* Set up later. */
  314. if (be32_to_cpu(pointers[1]) * 4 >= GLOBAL_CLOCK_CAPABILITIES + 4)
  315. dice->clock_caps = 1;
  316. end:
  317. kfree(pointers);
  318. return err;
  319. }