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