caiaq-midi.c 4.5 KB

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