midi.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <linux/gfp.h>
  20. #include <sound/rawmidi.h>
  21. #include <sound/core.h>
  22. #include <sound/pcm.h>
  23. #include "device.h"
  24. #include "midi.h"
  25. static int snd_usb_caiaq_midi_input_open(struct snd_rawmidi_substream *substream)
  26. {
  27. return 0;
  28. }
  29. static int snd_usb_caiaq_midi_input_close(struct snd_rawmidi_substream *substream)
  30. {
  31. return 0;
  32. }
  33. static void snd_usb_caiaq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
  34. {
  35. struct snd_usb_caiaqdev *dev = substream->rmidi->private_data;
  36. if (!dev)
  37. return;
  38. dev->midi_receive_substream = up ? substream : NULL;
  39. }
  40. static int snd_usb_caiaq_midi_output_open(struct snd_rawmidi_substream *substream)
  41. {
  42. return 0;
  43. }
  44. static int snd_usb_caiaq_midi_output_close(struct snd_rawmidi_substream *substream)
  45. {
  46. struct snd_usb_caiaqdev *dev = substream->rmidi->private_data;
  47. if (dev->midi_out_active) {
  48. usb_kill_urb(&dev->midi_out_urb);
  49. dev->midi_out_active = 0;
  50. }
  51. return 0;
  52. }
  53. static void snd_usb_caiaq_midi_send(struct snd_usb_caiaqdev *dev,
  54. struct snd_rawmidi_substream *substream)
  55. {
  56. int len, ret;
  57. dev->midi_out_buf[0] = EP1_CMD_MIDI_WRITE;
  58. dev->midi_out_buf[1] = 0; /* port */
  59. len = snd_rawmidi_transmit(substream, dev->midi_out_buf + 3,
  60. EP1_BUFSIZE - 3);
  61. if (len <= 0)
  62. return;
  63. dev->midi_out_buf[2] = len;
  64. dev->midi_out_urb.transfer_buffer_length = len+3;
  65. ret = usb_submit_urb(&dev->midi_out_urb, GFP_ATOMIC);
  66. if (ret < 0)
  67. log("snd_usb_caiaq_midi_send(%p): usb_submit_urb() failed,"
  68. "ret=%d, len=%d\n",
  69. substream, ret, len);
  70. else
  71. dev->midi_out_active = 1;
  72. }
  73. static void snd_usb_caiaq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  74. {
  75. struct snd_usb_caiaqdev *dev = substream->rmidi->private_data;
  76. if (up) {
  77. dev->midi_out_substream = substream;
  78. if (!dev->midi_out_active)
  79. snd_usb_caiaq_midi_send(dev, substream);
  80. } else {
  81. dev->midi_out_substream = NULL;
  82. }
  83. }
  84. static struct snd_rawmidi_ops snd_usb_caiaq_midi_output =
  85. {
  86. .open = snd_usb_caiaq_midi_output_open,
  87. .close = snd_usb_caiaq_midi_output_close,
  88. .trigger = snd_usb_caiaq_midi_output_trigger,
  89. };
  90. static struct snd_rawmidi_ops snd_usb_caiaq_midi_input =
  91. {
  92. .open = snd_usb_caiaq_midi_input_open,
  93. .close = snd_usb_caiaq_midi_input_close,
  94. .trigger = snd_usb_caiaq_midi_input_trigger,
  95. };
  96. void snd_usb_caiaq_midi_handle_input(struct snd_usb_caiaqdev *dev,
  97. int port, const char *buf, int len)
  98. {
  99. if (!dev->midi_receive_substream)
  100. return;
  101. snd_rawmidi_receive(dev->midi_receive_substream, buf, len);
  102. }
  103. int snd_usb_caiaq_midi_init(struct snd_usb_caiaqdev *device)
  104. {
  105. int ret;
  106. struct snd_rawmidi *rmidi;
  107. ret = snd_rawmidi_new(device->chip.card, device->product_name, 0,
  108. device->spec.num_midi_out,
  109. device->spec.num_midi_in,
  110. &rmidi);
  111. if (ret < 0)
  112. return ret;
  113. strlcpy(rmidi->name, device->product_name, sizeof(rmidi->name));
  114. rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX;
  115. rmidi->private_data = device;
  116. if (device->spec.num_midi_out > 0) {
  117. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
  118. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  119. &snd_usb_caiaq_midi_output);
  120. }
  121. if (device->spec.num_midi_in > 0) {
  122. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  123. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  124. &snd_usb_caiaq_midi_input);
  125. }
  126. device->rmidi = rmidi;
  127. return 0;
  128. }
  129. void snd_usb_caiaq_midi_output_done(struct urb* urb)
  130. {
  131. struct snd_usb_caiaqdev *dev = urb->context;
  132. dev->midi_out_active = 0;
  133. if (urb->status != 0)
  134. return;
  135. if (!dev->midi_out_substream)
  136. return;
  137. snd_usb_caiaq_midi_send(dev, dev->midi_out_substream);
  138. }