midi.c 4.9 KB

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