caiaq-midi.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. 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_peek(substream, dev->midi_out_buf+3, 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, %d\n",
  67. substream, ret);
  68. }
  69. static void snd_usb_caiaq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
  70. {
  71. struct snd_usb_caiaqdev *dev = substream->rmidi->private_data;
  72. if (dev->midi_out_substream != NULL)
  73. return;
  74. if (!up) {
  75. dev->midi_out_substream = NULL;
  76. return;
  77. }
  78. dev->midi_out_substream = substream;
  79. snd_usb_caiaq_midi_send(dev, substream);
  80. }
  81. static struct snd_rawmidi_ops snd_usb_caiaq_midi_output =
  82. {
  83. .open = snd_usb_caiaq_midi_output_open,
  84. .close = snd_usb_caiaq_midi_output_close,
  85. .trigger = snd_usb_caiaq_midi_output_trigger,
  86. };
  87. static struct snd_rawmidi_ops snd_usb_caiaq_midi_input =
  88. {
  89. .open = snd_usb_caiaq_midi_input_open,
  90. .close = snd_usb_caiaq_midi_input_close,
  91. .trigger = snd_usb_caiaq_midi_input_trigger,
  92. };
  93. void snd_usb_caiaq_midi_handle_input(struct snd_usb_caiaqdev *dev,
  94. int port, const char *buf, int len)
  95. {
  96. if (!dev->midi_receive_substream)
  97. return;
  98. snd_rawmidi_receive(dev->midi_receive_substream, buf, len);
  99. }
  100. int snd_usb_caiaq_midi_init(struct snd_usb_caiaqdev *device)
  101. {
  102. int ret;
  103. struct snd_rawmidi *rmidi;
  104. ret = snd_rawmidi_new(device->chip.card, device->product_name, 0,
  105. device->spec.num_midi_out,
  106. device->spec.num_midi_in,
  107. &rmidi);
  108. if (ret < 0)
  109. return ret;
  110. strcpy(rmidi->name, device->product_name);
  111. rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX;
  112. rmidi->private_data = device;
  113. if (device->spec.num_midi_out > 0) {
  114. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
  115. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  116. &snd_usb_caiaq_midi_output);
  117. }
  118. if (device->spec.num_midi_in > 0) {
  119. rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
  120. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  121. &snd_usb_caiaq_midi_input);
  122. }
  123. device->rmidi = rmidi;
  124. return 0;
  125. }
  126. void snd_usb_caiaq_midi_output_done(struct urb* urb)
  127. {
  128. struct snd_usb_caiaqdev *dev = urb->context;
  129. char *buf = urb->transfer_buffer;
  130. if (urb->status != 0)
  131. return;
  132. if (!dev->midi_out_substream)
  133. return;
  134. snd_rawmidi_transmit_ack(dev->midi_out_substream, buf[2]);
  135. dev->midi_out_substream = NULL;
  136. snd_usb_caiaq_midi_send(dev, dev->midi_out_substream);
  137. }