midi.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2006,2007 Daniel Mack
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/usb.h>
  19. #include <sound/rawmidi.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include "device.h"
  23. #include "midi.h"
  24. static int snd_usb_caiaq_midi_input_open(struct snd_rawmidi_substream *substream)
  25. {
  26. return 0;
  27. }
  28. static int snd_usb_caiaq_midi_input_close(struct snd_rawmidi_substream *substream)
  29. {
  30. return 0;
  31. }
  32. static void snd_usb_caiaq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  33. {
  34. struct snd_usb_caiaqdev *dev = substream->rmidi->private_data;
  35. if (!dev)
  36. return;
  37. dev->midi_receive_substream = up ? substream : NULL;
  38. }
  39. static int snd_usb_caiaq_midi_output_open(struct snd_rawmidi_substream *substream)
  40. {
  41. return 0;
  42. }
  43. static int snd_usb_caiaq_midi_output_close(struct snd_rawmidi_substream *substream)
  44. {
  45. struct snd_usb_caiaqdev *dev = substream->rmidi->private_data;
  46. if (dev->midi_out_active) {
  47. usb_kill_urb(&dev->midi_out_urb);
  48. dev->midi_out_active = 0;
  49. }
  50. return 0;
  51. }
  52. static void snd_usb_caiaq_midi_send(struct snd_usb_caiaqdev *dev,
  53. struct snd_rawmidi_substream *substream)
  54. {
  55. int len, ret;
  56. dev->midi_out_buf[0] = EP1_CMD_MIDI_WRITE;
  57. dev->midi_out_buf[1] = 0; /* port */
  58. len = snd_rawmidi_transmit(substream, dev->midi_out_buf + 3,
  59. EP1_BUFSIZE - 3);
  60. if (len <= 0)
  61. return;
  62. dev->midi_out_buf[2] = len;
  63. dev->midi_out_urb.transfer_buffer_length = len+3;
  64. ret = usb_submit_urb(&dev->midi_out_urb, GFP_ATOMIC);
  65. if (ret < 0)
  66. log("snd_usb_caiaq_midi_send(%p): usb_submit_urb() failed,"
  67. "ret=%d, len=%d\n",
  68. substream, ret, len);
  69. else
  70. dev->midi_out_active = 1;
  71. }
  72. static void snd_usb_caiaq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  73. {
  74. struct snd_usb_caiaqdev *dev = substream->rmidi->private_data;
  75. if (up) {
  76. dev->midi_out_substream = substream;
  77. if (!dev->midi_out_active)
  78. snd_usb_caiaq_midi_send(dev, substream);
  79. } else {
  80. dev->midi_out_substream = NULL;
  81. }
  82. }
  83. static struct snd_rawmidi_ops snd_usb_caiaq_midi_output =
  84. {
  85. .open = snd_usb_caiaq_midi_output_open,
  86. .close = snd_usb_caiaq_midi_output_close,
  87. .trigger = snd_usb_caiaq_midi_output_trigger,
  88. };
  89. static struct snd_rawmidi_ops snd_usb_caiaq_midi_input =
  90. {
  91. .open = snd_usb_caiaq_midi_input_open,
  92. .close = snd_usb_caiaq_midi_input_close,
  93. .trigger = snd_usb_caiaq_midi_input_trigger,
  94. };
  95. void snd_usb_caiaq_midi_handle_input(struct snd_usb_caiaqdev *dev,
  96. int port, const char *buf, int len)
  97. {
  98. if (!dev->midi_receive_substream)
  99. return;
  100. snd_rawmidi_receive(dev->midi_receive_substream, buf, len);
  101. }
  102. int snd_usb_caiaq_midi_init(struct snd_usb_caiaqdev *device)
  103. {
  104. int ret;
  105. struct snd_rawmidi *rmidi;
  106. ret = snd_rawmidi_new(device->chip.card, device->product_name, 0,
  107. device->spec.num_midi_out,
  108. device->spec.num_midi_in,
  109. &rmidi);
  110. if (ret < 0)
  111. return ret;
  112. strcpy(rmidi->name, device->product_name);
  113. rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX;
  114. rmidi->private_data = device;
  115. if (device->spec.num_midi_out > 0) {
  116. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
  117. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  118. &snd_usb_caiaq_midi_output);
  119. }
  120. if (device->spec.num_midi_in > 0) {
  121. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  122. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  123. &snd_usb_caiaq_midi_input);
  124. }
  125. device->rmidi = rmidi;
  126. return 0;
  127. }
  128. void snd_usb_caiaq_midi_output_done(struct urb* urb)
  129. {
  130. struct snd_usb_caiaqdev *dev = urb->context;
  131. dev->midi_out_active = 0;
  132. if (urb->status != 0)
  133. return;
  134. if (!dev->midi_out_substream)
  135. return;
  136. snd_usb_caiaq_midi_send(dev, dev->midi_out_substream);
  137. }