digi00x-transaction.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * digi00x-transaction.c - a part of driver for Digidesign Digi 002/003 family
  3. *
  4. * Copyright (c) 2014-2015 Takashi Sakamoto
  5. *
  6. * Licensed under the terms of the GNU General Public License, version 2.
  7. */
  8. #include <sound/asound.h>
  9. #include "digi00x.h"
  10. static int fill_midi_message(struct snd_rawmidi_substream *substream, u8 *buf)
  11. {
  12. int bytes;
  13. buf[0] = 0x80;
  14. bytes = snd_rawmidi_transmit_peek(substream, buf + 1, 2);
  15. if (bytes >= 0)
  16. buf[3] = 0xc0 | bytes;
  17. return bytes;
  18. }
  19. static void handle_midi_control(struct snd_dg00x *dg00x, __be32 *buf,
  20. unsigned int length)
  21. {
  22. struct snd_rawmidi_substream *substream;
  23. unsigned int i;
  24. unsigned int len;
  25. u8 *b;
  26. substream = ACCESS_ONCE(dg00x->in_control);
  27. if (substream == NULL)
  28. return;
  29. length /= 4;
  30. for (i = 0; i < length; i++) {
  31. b = (u8 *)&buf[i];
  32. len = b[3] & 0xf;
  33. if (len > 0)
  34. snd_rawmidi_receive(dg00x->in_control, b + 1, len);
  35. }
  36. }
  37. static void handle_unknown_message(struct snd_dg00x *dg00x,
  38. unsigned long long offset, __be32 *buf)
  39. {
  40. unsigned long flags;
  41. spin_lock_irqsave(&dg00x->lock, flags);
  42. dg00x->msg = be32_to_cpu(*buf);
  43. spin_unlock_irqrestore(&dg00x->lock, flags);
  44. wake_up(&dg00x->hwdep_wait);
  45. }
  46. static void handle_message(struct fw_card *card, struct fw_request *request,
  47. int tcode, int destination, int source,
  48. int generation, unsigned long long offset,
  49. void *data, size_t length, void *callback_data)
  50. {
  51. struct snd_dg00x *dg00x = callback_data;
  52. __be32 *buf = (__be32 *)data;
  53. if (offset == dg00x->async_handler.offset)
  54. handle_unknown_message(dg00x, offset, buf);
  55. else if (offset == dg00x->async_handler.offset + 4)
  56. handle_midi_control(dg00x, buf, length);
  57. fw_send_response(card, request, RCODE_COMPLETE);
  58. }
  59. int snd_dg00x_transaction_reregister(struct snd_dg00x *dg00x)
  60. {
  61. struct fw_device *device = fw_parent_device(dg00x->unit);
  62. __be32 data[2];
  63. int err;
  64. /* Unknown. 4bytes. */
  65. data[0] = cpu_to_be32((device->card->node_id << 16) |
  66. (dg00x->async_handler.offset >> 32));
  67. data[1] = cpu_to_be32(dg00x->async_handler.offset);
  68. err = snd_fw_transaction(dg00x->unit, TCODE_WRITE_BLOCK_REQUEST,
  69. DG00X_ADDR_BASE + DG00X_OFFSET_MESSAGE_ADDR,
  70. &data, sizeof(data), 0);
  71. if (err < 0)
  72. return err;
  73. /* Asynchronous transactions for MIDI control message. */
  74. data[0] = cpu_to_be32((device->card->node_id << 16) |
  75. (dg00x->async_handler.offset >> 32));
  76. data[1] = cpu_to_be32(dg00x->async_handler.offset + 4);
  77. return snd_fw_transaction(dg00x->unit, TCODE_WRITE_BLOCK_REQUEST,
  78. DG00X_ADDR_BASE + DG00X_OFFSET_MIDI_CTL_ADDR,
  79. &data, sizeof(data), 0);
  80. }
  81. int snd_dg00x_transaction_register(struct snd_dg00x *dg00x)
  82. {
  83. static const struct fw_address_region resp_register_region = {
  84. .start = 0xffffe0000000ull,
  85. .end = 0xffffe000ffffull,
  86. };
  87. int err;
  88. dg00x->async_handler.length = 12;
  89. dg00x->async_handler.address_callback = handle_message;
  90. dg00x->async_handler.callback_data = dg00x;
  91. err = fw_core_add_address_handler(&dg00x->async_handler,
  92. &resp_register_region);
  93. if (err < 0)
  94. return err;
  95. err = snd_dg00x_transaction_reregister(dg00x);
  96. if (err < 0)
  97. goto error;
  98. err = snd_fw_async_midi_port_init(&dg00x->out_control, dg00x->unit,
  99. DG00X_ADDR_BASE + DG00X_OFFSET_MMC,
  100. 4, fill_midi_message);
  101. if (err < 0)
  102. goto error;
  103. return err;
  104. error:
  105. fw_core_remove_address_handler(&dg00x->async_handler);
  106. dg00x->async_handler.callback_data = NULL;
  107. return err;
  108. }
  109. void snd_dg00x_transaction_unregister(struct snd_dg00x *dg00x)
  110. {
  111. if (dg00x->async_handler.callback_data == NULL)
  112. return;
  113. snd_fw_async_midi_port_destroy(&dg00x->out_control);
  114. fw_core_remove_address_handler(&dg00x->async_handler);
  115. dg00x->async_handler.callback_data = NULL;
  116. }