dice-transaction.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 (2 * MSEC_PER_SEC)
  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 i;
  62. __be32 info;
  63. u32 mask;
  64. u32 clock;
  65. int err;
  66. err = get_clock_info(dice, &info);
  67. if (err < 0)
  68. return err;
  69. clock = be32_to_cpu(info);
  70. if (source != UINT_MAX) {
  71. mask = CLOCK_SOURCE_MASK;
  72. clock &= ~mask;
  73. clock |= source;
  74. }
  75. if (rate != UINT_MAX) {
  76. for (i = 0; i < ARRAY_SIZE(snd_dice_rates); i++) {
  77. if (snd_dice_rates[i] == rate)
  78. break;
  79. }
  80. if (i == ARRAY_SIZE(snd_dice_rates))
  81. return -EINVAL;
  82. mask = CLOCK_RATE_MASK;
  83. clock &= ~mask;
  84. clock |= i << CLOCK_RATE_SHIFT;
  85. }
  86. info = cpu_to_be32(clock);
  87. if (completion_done(&dice->clock_accepted))
  88. reinit_completion(&dice->clock_accepted);
  89. err = snd_dice_transaction_write_global(dice, GLOBAL_CLOCK_SELECT,
  90. &info, 4);
  91. if (err < 0)
  92. return err;
  93. if (wait_for_completion_timeout(&dice->clock_accepted,
  94. msecs_to_jiffies(NOTIFICATION_TIMEOUT_MS)) == 0)
  95. return -ETIMEDOUT;
  96. return 0;
  97. }
  98. int snd_dice_transaction_get_clock_source(struct snd_dice *dice,
  99. unsigned int *source)
  100. {
  101. __be32 info;
  102. int err;
  103. err = get_clock_info(dice, &info);
  104. if (err >= 0)
  105. *source = be32_to_cpu(info) & CLOCK_SOURCE_MASK;
  106. return err;
  107. }
  108. int snd_dice_transaction_get_rate(struct snd_dice *dice, unsigned int *rate)
  109. {
  110. __be32 info;
  111. unsigned int index;
  112. int err;
  113. err = get_clock_info(dice, &info);
  114. if (err < 0)
  115. goto end;
  116. index = (be32_to_cpu(info) & CLOCK_RATE_MASK) >> CLOCK_RATE_SHIFT;
  117. if (index >= SND_DICE_RATES_COUNT) {
  118. err = -ENOSYS;
  119. goto end;
  120. }
  121. *rate = snd_dice_rates[index];
  122. end:
  123. return err;
  124. }
  125. int snd_dice_transaction_set_rate(struct snd_dice *dice, unsigned int rate)
  126. {
  127. return set_clock_info(dice, rate, UINT_MAX);
  128. }
  129. int snd_dice_transaction_set_enable(struct snd_dice *dice)
  130. {
  131. __be32 value;
  132. int err = 0;
  133. if (dice->global_enabled)
  134. goto end;
  135. value = cpu_to_be32(1);
  136. err = snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  137. get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
  138. GLOBAL_ENABLE),
  139. &value, 4,
  140. FW_FIXED_GENERATION | dice->owner_generation);
  141. if (err < 0)
  142. goto end;
  143. dice->global_enabled = true;
  144. end:
  145. return err;
  146. }
  147. void snd_dice_transaction_clear_enable(struct snd_dice *dice)
  148. {
  149. __be32 value;
  150. value = 0;
  151. snd_fw_transaction(dice->unit, TCODE_WRITE_QUADLET_REQUEST,
  152. get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
  153. GLOBAL_ENABLE),
  154. &value, 4, FW_QUIET |
  155. FW_FIXED_GENERATION | dice->owner_generation);
  156. dice->global_enabled = false;
  157. }
  158. static void dice_notification(struct fw_card *card, struct fw_request *request,
  159. int tcode, int destination, int source,
  160. int generation, unsigned long long offset,
  161. void *data, size_t length, void *callback_data)
  162. {
  163. struct snd_dice *dice = callback_data;
  164. u32 bits;
  165. unsigned long flags;
  166. if (tcode != TCODE_WRITE_QUADLET_REQUEST) {
  167. fw_send_response(card, request, RCODE_TYPE_ERROR);
  168. return;
  169. }
  170. if ((offset & 3) != 0) {
  171. fw_send_response(card, request, RCODE_ADDRESS_ERROR);
  172. return;
  173. }
  174. bits = be32_to_cpup(data);
  175. spin_lock_irqsave(&dice->lock, flags);
  176. dice->notification_bits |= bits;
  177. spin_unlock_irqrestore(&dice->lock, flags);
  178. fw_send_response(card, request, RCODE_COMPLETE);
  179. if (bits & NOTIFY_CLOCK_ACCEPTED)
  180. complete(&dice->clock_accepted);
  181. wake_up(&dice->hwdep_wait);
  182. }
  183. static int register_notification_address(struct snd_dice *dice, bool retry)
  184. {
  185. struct fw_device *device = fw_parent_device(dice->unit);
  186. __be64 *buffer;
  187. unsigned int retries;
  188. int err;
  189. retries = (retry) ? 3 : 0;
  190. buffer = kmalloc(2 * 8, GFP_KERNEL);
  191. if (!buffer)
  192. return -ENOMEM;
  193. for (;;) {
  194. buffer[0] = cpu_to_be64(OWNER_NO_OWNER);
  195. buffer[1] = cpu_to_be64(
  196. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  197. dice->notification_handler.offset);
  198. dice->owner_generation = device->generation;
  199. smp_rmb(); /* node_id vs. generation */
  200. err = snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  201. get_subaddr(dice,
  202. SND_DICE_ADDR_TYPE_GLOBAL,
  203. GLOBAL_OWNER),
  204. buffer, 2 * 8,
  205. FW_FIXED_GENERATION |
  206. dice->owner_generation);
  207. if (err == 0) {
  208. /* success */
  209. if (buffer[0] == cpu_to_be64(OWNER_NO_OWNER))
  210. break;
  211. /* The address seems to be already registered. */
  212. if (buffer[0] == buffer[1])
  213. break;
  214. dev_err(&dice->unit->device,
  215. "device is already in use\n");
  216. err = -EBUSY;
  217. }
  218. if (err != -EAGAIN || retries-- > 0)
  219. break;
  220. msleep(20);
  221. }
  222. kfree(buffer);
  223. if (err < 0)
  224. dice->owner_generation = -1;
  225. return err;
  226. }
  227. static void unregister_notification_address(struct snd_dice *dice)
  228. {
  229. struct fw_device *device = fw_parent_device(dice->unit);
  230. __be64 *buffer;
  231. buffer = kmalloc(2 * 8, GFP_KERNEL);
  232. if (buffer == NULL)
  233. return;
  234. buffer[0] = cpu_to_be64(
  235. ((u64)device->card->node_id << OWNER_NODE_SHIFT) |
  236. dice->notification_handler.offset);
  237. buffer[1] = cpu_to_be64(OWNER_NO_OWNER);
  238. snd_fw_transaction(dice->unit, TCODE_LOCK_COMPARE_SWAP,
  239. get_subaddr(dice, SND_DICE_ADDR_TYPE_GLOBAL,
  240. GLOBAL_OWNER),
  241. buffer, 2 * 8, FW_QUIET |
  242. FW_FIXED_GENERATION | dice->owner_generation);
  243. kfree(buffer);
  244. dice->owner_generation = -1;
  245. }
  246. void snd_dice_transaction_destroy(struct snd_dice *dice)
  247. {
  248. struct fw_address_handler *handler = &dice->notification_handler;
  249. if (handler->callback_data == NULL)
  250. return;
  251. unregister_notification_address(dice);
  252. fw_core_remove_address_handler(handler);
  253. handler->callback_data = NULL;
  254. }
  255. int snd_dice_transaction_reinit(struct snd_dice *dice)
  256. {
  257. struct fw_address_handler *handler = &dice->notification_handler;
  258. if (handler->callback_data == NULL)
  259. return -EINVAL;
  260. return register_notification_address(dice, false);
  261. }
  262. static int get_subaddrs(struct snd_dice *dice)
  263. {
  264. static const int min_values[10] = {
  265. 10, 0x64 / 4,
  266. 10, 0x18 / 4,
  267. 10, 0x18 / 4,
  268. 0, 0,
  269. 0, 0,
  270. };
  271. __be32 *pointers;
  272. __be32 version;
  273. u32 data;
  274. unsigned int i;
  275. int err;
  276. pointers = kmalloc_array(ARRAY_SIZE(min_values), sizeof(__be32),
  277. GFP_KERNEL);
  278. if (pointers == NULL)
  279. return -ENOMEM;
  280. /*
  281. * Check that the sub address spaces exist and are located inside the
  282. * private address space. The minimum values are chosen so that all
  283. * minimally required registers are included.
  284. */
  285. err = snd_fw_transaction(dice->unit, TCODE_READ_BLOCK_REQUEST,
  286. DICE_PRIVATE_SPACE, pointers,
  287. sizeof(__be32) * ARRAY_SIZE(min_values), 0);
  288. if (err < 0)
  289. goto end;
  290. for (i = 0; i < ARRAY_SIZE(min_values); ++i) {
  291. data = be32_to_cpu(pointers[i]);
  292. if (data < min_values[i] || data >= 0x40000) {
  293. err = -ENODEV;
  294. goto end;
  295. }
  296. }
  297. /*
  298. * Check that the implemented DICE driver specification major version
  299. * number matches.
  300. */
  301. err = snd_fw_transaction(dice->unit, TCODE_READ_QUADLET_REQUEST,
  302. DICE_PRIVATE_SPACE +
  303. be32_to_cpu(pointers[0]) * 4 + GLOBAL_VERSION,
  304. &version, sizeof(version), 0);
  305. if (err < 0)
  306. goto end;
  307. if ((version & cpu_to_be32(0xff000000)) != cpu_to_be32(0x01000000)) {
  308. dev_err(&dice->unit->device,
  309. "unknown DICE version: 0x%08x\n", be32_to_cpu(version));
  310. err = -ENODEV;
  311. goto end;
  312. }
  313. dice->global_offset = be32_to_cpu(pointers[0]) * 4;
  314. dice->tx_offset = be32_to_cpu(pointers[2]) * 4;
  315. dice->rx_offset = be32_to_cpu(pointers[4]) * 4;
  316. dice->sync_offset = be32_to_cpu(pointers[6]) * 4;
  317. dice->rsrv_offset = be32_to_cpu(pointers[8]) * 4;
  318. /* Set up later. */
  319. if (be32_to_cpu(pointers[1]) * 4 >= GLOBAL_CLOCK_CAPABILITIES + 4)
  320. dice->clock_caps = 1;
  321. end:
  322. kfree(pointers);
  323. return err;
  324. }
  325. int snd_dice_transaction_init(struct snd_dice *dice)
  326. {
  327. struct fw_address_handler *handler = &dice->notification_handler;
  328. int err;
  329. err = get_subaddrs(dice);
  330. if (err < 0)
  331. return err;
  332. /* Allocation callback in address space over host controller */
  333. handler->length = 4;
  334. handler->address_callback = dice_notification;
  335. handler->callback_data = dice;
  336. err = fw_core_add_address_handler(handler, &fw_high_memory_region);
  337. if (err < 0) {
  338. handler->callback_data = NULL;
  339. return err;
  340. }
  341. /* Register the address space */
  342. err = register_notification_address(dice, true);
  343. if (err < 0) {
  344. fw_core_remove_address_handler(handler);
  345. handler->callback_data = NULL;
  346. }
  347. return err;
  348. }