caiaq-midi.c 4.4 KB

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