midi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Linux driver for TerraTec DMX 6Fire USB
  3. *
  4. * Rawmidi driver
  5. *
  6. * Author: Torsten Schenk <torsten.schenk@zoho.com>
  7. * Created: Jan 01, 2011
  8. * Copyright: (C) Torsten Schenk
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <sound/rawmidi.h>
  16. #include "midi.h"
  17. #include "chip.h"
  18. #include "comm.h"
  19. enum {
  20. MIDI_BUFSIZE = 64
  21. };
  22. static void usb6fire_midi_out_handler(struct urb *urb)
  23. {
  24. struct midi_runtime *rt = urb->context;
  25. int ret;
  26. unsigned long flags;
  27. spin_lock_irqsave(&rt->out_lock, flags);
  28. if (rt->out) {
  29. ret = snd_rawmidi_transmit(rt->out, rt->out_buffer + 4,
  30. MIDI_BUFSIZE - 4);
  31. if (ret > 0) { /* more data available, send next packet */
  32. rt->out_buffer[1] = ret + 2;
  33. rt->out_buffer[3] = rt->out_serial++;
  34. urb->transfer_buffer_length = ret + 4;
  35. ret = usb_submit_urb(urb, GFP_ATOMIC);
  36. if (ret < 0)
  37. dev_err(&urb->dev->dev,
  38. "midi out urb submit failed: %d\n",
  39. ret);
  40. } else /* no more data to transmit */
  41. rt->out = NULL;
  42. }
  43. spin_unlock_irqrestore(&rt->out_lock, flags);
  44. }
  45. static void usb6fire_midi_in_received(
  46. struct midi_runtime *rt, u8 *data, int length)
  47. {
  48. unsigned long flags;
  49. spin_lock_irqsave(&rt->in_lock, flags);
  50. if (rt->in)
  51. snd_rawmidi_receive(rt->in, data, length);
  52. spin_unlock_irqrestore(&rt->in_lock, flags);
  53. }
  54. static int usb6fire_midi_out_open(struct snd_rawmidi_substream *alsa_sub)
  55. {
  56. return 0;
  57. }
  58. static int usb6fire_midi_out_close(struct snd_rawmidi_substream *alsa_sub)
  59. {
  60. return 0;
  61. }
  62. static void usb6fire_midi_out_trigger(
  63. struct snd_rawmidi_substream *alsa_sub, int up)
  64. {
  65. struct midi_runtime *rt = alsa_sub->rmidi->private_data;
  66. struct urb *urb = &rt->out_urb;
  67. __s8 ret;
  68. unsigned long flags;
  69. spin_lock_irqsave(&rt->out_lock, flags);
  70. if (up) { /* start transfer */
  71. if (rt->out) { /* we are already transmitting so just return */
  72. spin_unlock_irqrestore(&rt->out_lock, flags);
  73. return;
  74. }
  75. ret = snd_rawmidi_transmit(alsa_sub, rt->out_buffer + 4,
  76. MIDI_BUFSIZE - 4);
  77. if (ret > 0) {
  78. rt->out_buffer[1] = ret + 2;
  79. rt->out_buffer[3] = rt->out_serial++;
  80. urb->transfer_buffer_length = ret + 4;
  81. ret = usb_submit_urb(urb, GFP_ATOMIC);
  82. if (ret < 0)
  83. dev_err(&urb->dev->dev,
  84. "midi out urb submit failed: %d\n",
  85. ret);
  86. else
  87. rt->out = alsa_sub;
  88. }
  89. } else if (rt->out == alsa_sub)
  90. rt->out = NULL;
  91. spin_unlock_irqrestore(&rt->out_lock, flags);
  92. }
  93. static void usb6fire_midi_out_drain(struct snd_rawmidi_substream *alsa_sub)
  94. {
  95. struct midi_runtime *rt = alsa_sub->rmidi->private_data;
  96. int retry = 0;
  97. while (rt->out && retry++ < 100)
  98. msleep(10);
  99. }
  100. static int usb6fire_midi_in_open(struct snd_rawmidi_substream *alsa_sub)
  101. {
  102. return 0;
  103. }
  104. static int usb6fire_midi_in_close(struct snd_rawmidi_substream *alsa_sub)
  105. {
  106. return 0;
  107. }
  108. static void usb6fire_midi_in_trigger(
  109. struct snd_rawmidi_substream *alsa_sub, int up)
  110. {
  111. struct midi_runtime *rt = alsa_sub->rmidi->private_data;
  112. unsigned long flags;
  113. spin_lock_irqsave(&rt->in_lock, flags);
  114. if (up)
  115. rt->in = alsa_sub;
  116. else
  117. rt->in = NULL;
  118. spin_unlock_irqrestore(&rt->in_lock, flags);
  119. }
  120. static struct snd_rawmidi_ops out_ops = {
  121. .open = usb6fire_midi_out_open,
  122. .close = usb6fire_midi_out_close,
  123. .trigger = usb6fire_midi_out_trigger,
  124. .drain = usb6fire_midi_out_drain
  125. };
  126. static struct snd_rawmidi_ops in_ops = {
  127. .open = usb6fire_midi_in_open,
  128. .close = usb6fire_midi_in_close,
  129. .trigger = usb6fire_midi_in_trigger
  130. };
  131. int usb6fire_midi_init(struct sfire_chip *chip)
  132. {
  133. int ret;
  134. struct midi_runtime *rt = kzalloc(sizeof(struct midi_runtime),
  135. GFP_KERNEL);
  136. struct comm_runtime *comm_rt = chip->comm;
  137. if (!rt)
  138. return -ENOMEM;
  139. rt->out_buffer = kzalloc(MIDI_BUFSIZE, GFP_KERNEL);
  140. if (!rt->out_buffer) {
  141. kfree(rt);
  142. return -ENOMEM;
  143. }
  144. rt->chip = chip;
  145. rt->in_received = usb6fire_midi_in_received;
  146. rt->out_buffer[0] = 0x80; /* 'send midi' command */
  147. rt->out_buffer[1] = 0x00; /* size of data */
  148. rt->out_buffer[2] = 0x00; /* always 0 */
  149. spin_lock_init(&rt->in_lock);
  150. spin_lock_init(&rt->out_lock);
  151. comm_rt->init_urb(comm_rt, &rt->out_urb, rt->out_buffer, rt,
  152. usb6fire_midi_out_handler);
  153. ret = snd_rawmidi_new(chip->card, "6FireUSB", 0, 1, 1, &rt->instance);
  154. if (ret < 0) {
  155. kfree(rt->out_buffer);
  156. kfree(rt);
  157. dev_err(&chip->dev->dev, "unable to create midi.\n");
  158. return ret;
  159. }
  160. rt->instance->private_data = rt;
  161. strcpy(rt->instance->name, "DMX6FireUSB MIDI");
  162. rt->instance->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  163. SNDRV_RAWMIDI_INFO_INPUT |
  164. SNDRV_RAWMIDI_INFO_DUPLEX;
  165. snd_rawmidi_set_ops(rt->instance, SNDRV_RAWMIDI_STREAM_OUTPUT,
  166. &out_ops);
  167. snd_rawmidi_set_ops(rt->instance, SNDRV_RAWMIDI_STREAM_INPUT,
  168. &in_ops);
  169. chip->midi = rt;
  170. return 0;
  171. }
  172. void usb6fire_midi_abort(struct sfire_chip *chip)
  173. {
  174. struct midi_runtime *rt = chip->midi;
  175. if (rt)
  176. usb_poison_urb(&rt->out_urb);
  177. }
  178. void usb6fire_midi_destroy(struct sfire_chip *chip)
  179. {
  180. struct midi_runtime *rt = chip->midi;
  181. kfree(rt->out_buffer);
  182. kfree(rt);
  183. chip->midi = NULL;
  184. }