midi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. snd_printk(KERN_ERR PREFIX "midi out urb "
  38. "submit failed: %d\n", ret);
  39. } else /* no more data to transmit */
  40. rt->out = NULL;
  41. }
  42. spin_unlock_irqrestore(&rt->out_lock, flags);
  43. }
  44. static void usb6fire_midi_in_received(
  45. struct midi_runtime *rt, u8 *data, int length)
  46. {
  47. unsigned long flags;
  48. spin_lock_irqsave(&rt->in_lock, flags);
  49. if (rt->in)
  50. snd_rawmidi_receive(rt->in, data, length);
  51. spin_unlock_irqrestore(&rt->in_lock, flags);
  52. }
  53. static int usb6fire_midi_out_open(struct snd_rawmidi_substream *alsa_sub)
  54. {
  55. return 0;
  56. }
  57. static int usb6fire_midi_out_close(struct snd_rawmidi_substream *alsa_sub)
  58. {
  59. return 0;
  60. }
  61. static void usb6fire_midi_out_trigger(
  62. struct snd_rawmidi_substream *alsa_sub, int up)
  63. {
  64. struct midi_runtime *rt = alsa_sub->rmidi->private_data;
  65. struct urb *urb = &rt->out_urb;
  66. __s8 ret;
  67. unsigned long flags;
  68. spin_lock_irqsave(&rt->out_lock, flags);
  69. if (up) { /* start transfer */
  70. if (rt->out) { /* we are already transmitting so just return */
  71. spin_unlock_irqrestore(&rt->out_lock, flags);
  72. return;
  73. }
  74. ret = snd_rawmidi_transmit(alsa_sub, rt->out_buffer + 4,
  75. MIDI_BUFSIZE - 4);
  76. if (ret > 0) {
  77. rt->out_buffer[1] = ret + 2;
  78. rt->out_buffer[3] = rt->out_serial++;
  79. urb->transfer_buffer_length = ret + 4;
  80. ret = usb_submit_urb(urb, GFP_ATOMIC);
  81. if (ret < 0)
  82. snd_printk(KERN_ERR PREFIX "midi out urb "
  83. "submit failed: %d\n", ret);
  84. else
  85. rt->out = alsa_sub;
  86. }
  87. } else if (rt->out == alsa_sub)
  88. rt->out = NULL;
  89. spin_unlock_irqrestore(&rt->out_lock, flags);
  90. }
  91. static void usb6fire_midi_out_drain(struct snd_rawmidi_substream *alsa_sub)
  92. {
  93. struct midi_runtime *rt = alsa_sub->rmidi->private_data;
  94. int retry = 0;
  95. while (rt->out && retry++ < 100)
  96. msleep(10);
  97. }
  98. static int usb6fire_midi_in_open(struct snd_rawmidi_substream *alsa_sub)
  99. {
  100. return 0;
  101. }
  102. static int usb6fire_midi_in_close(struct snd_rawmidi_substream *alsa_sub)
  103. {
  104. return 0;
  105. }
  106. static void usb6fire_midi_in_trigger(
  107. struct snd_rawmidi_substream *alsa_sub, int up)
  108. {
  109. struct midi_runtime *rt = alsa_sub->rmidi->private_data;
  110. unsigned long flags;
  111. spin_lock_irqsave(&rt->in_lock, flags);
  112. if (up)
  113. rt->in = alsa_sub;
  114. else
  115. rt->in = NULL;
  116. spin_unlock_irqrestore(&rt->in_lock, flags);
  117. }
  118. static struct snd_rawmidi_ops out_ops = {
  119. .open = usb6fire_midi_out_open,
  120. .close = usb6fire_midi_out_close,
  121. .trigger = usb6fire_midi_out_trigger,
  122. .drain = usb6fire_midi_out_drain
  123. };
  124. static struct snd_rawmidi_ops in_ops = {
  125. .open = usb6fire_midi_in_open,
  126. .close = usb6fire_midi_in_close,
  127. .trigger = usb6fire_midi_in_trigger
  128. };
  129. int __devinit usb6fire_midi_init(struct sfire_chip *chip)
  130. {
  131. int ret;
  132. struct midi_runtime *rt = kzalloc(sizeof(struct midi_runtime),
  133. GFP_KERNEL);
  134. struct comm_runtime *comm_rt = chip->comm;
  135. if (!rt)
  136. return -ENOMEM;
  137. rt->out_buffer = kzalloc(MIDI_BUFSIZE, GFP_KERNEL);
  138. if (!rt->out_buffer) {
  139. kfree(rt);
  140. return -ENOMEM;
  141. }
  142. rt->chip = chip;
  143. rt->in_received = usb6fire_midi_in_received;
  144. rt->out_buffer[0] = 0x80; /* 'send midi' command */
  145. rt->out_buffer[1] = 0x00; /* size of data */
  146. rt->out_buffer[2] = 0x00; /* always 0 */
  147. spin_lock_init(&rt->in_lock);
  148. spin_lock_init(&rt->out_lock);
  149. comm_rt->init_urb(comm_rt, &rt->out_urb, rt->out_buffer, rt,
  150. usb6fire_midi_out_handler);
  151. ret = snd_rawmidi_new(chip->card, "6FireUSB", 0, 1, 1, &rt->instance);
  152. if (ret < 0) {
  153. kfree(rt->out_buffer);
  154. kfree(rt);
  155. snd_printk(KERN_ERR PREFIX "unable to create midi.\n");
  156. return ret;
  157. }
  158. rt->instance->private_data = rt;
  159. strcpy(rt->instance->name, "DMX6FireUSB MIDI");
  160. rt->instance->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
  161. SNDRV_RAWMIDI_INFO_INPUT |
  162. SNDRV_RAWMIDI_INFO_DUPLEX;
  163. snd_rawmidi_set_ops(rt->instance, SNDRV_RAWMIDI_STREAM_OUTPUT,
  164. &out_ops);
  165. snd_rawmidi_set_ops(rt->instance, SNDRV_RAWMIDI_STREAM_INPUT,
  166. &in_ops);
  167. chip->midi = rt;
  168. return 0;
  169. }
  170. void usb6fire_midi_abort(struct sfire_chip *chip)
  171. {
  172. struct midi_runtime *rt = chip->midi;
  173. if (rt)
  174. usb_poison_urb(&rt->out_urb);
  175. }
  176. void usb6fire_midi_destroy(struct sfire_chip *chip)
  177. {
  178. struct midi_runtime *rt = chip->midi;
  179. kfree(rt->out_buffer);
  180. kfree(rt);
  181. chip->midi = NULL;
  182. }