midi.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Line 6 Linux USB driver
  3. *
  4. * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. */
  11. #include <linux/slab.h>
  12. #include <linux/usb.h>
  13. #include <linux/export.h>
  14. #include <sound/core.h>
  15. #include <sound/rawmidi.h>
  16. #include "driver.h"
  17. #include "midi.h"
  18. #define line6_rawmidi_substream_midi(substream) \
  19. ((struct snd_line6_midi *)((substream)->rmidi->private_data))
  20. static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
  21. int length);
  22. /*
  23. Pass data received via USB to MIDI.
  24. */
  25. void line6_midi_receive(struct usb_line6 *line6, unsigned char *data,
  26. int length)
  27. {
  28. if (line6->line6midi->substream_receive)
  29. snd_rawmidi_receive(line6->line6midi->substream_receive,
  30. data, length);
  31. }
  32. /*
  33. Read data from MIDI buffer and transmit them via USB.
  34. */
  35. static void line6_midi_transmit(struct snd_rawmidi_substream *substream)
  36. {
  37. struct usb_line6 *line6 =
  38. line6_rawmidi_substream_midi(substream)->line6;
  39. struct snd_line6_midi *line6midi = line6->line6midi;
  40. struct midi_buffer *mb = &line6midi->midibuf_out;
  41. unsigned char chunk[LINE6_FALLBACK_MAXPACKETSIZE];
  42. int req, done;
  43. for (;;) {
  44. req = min(line6_midibuf_bytes_free(mb), line6->max_packet_size);
  45. done = snd_rawmidi_transmit_peek(substream, chunk, req);
  46. if (done == 0)
  47. break;
  48. line6_midibuf_write(mb, chunk, done);
  49. snd_rawmidi_transmit_ack(substream, done);
  50. }
  51. for (;;) {
  52. done = line6_midibuf_read(mb, chunk,
  53. LINE6_FALLBACK_MAXPACKETSIZE);
  54. if (done == 0)
  55. break;
  56. send_midi_async(line6, chunk, done);
  57. }
  58. }
  59. /*
  60. Notification of completion of MIDI transmission.
  61. */
  62. static void midi_sent(struct urb *urb)
  63. {
  64. unsigned long flags;
  65. int status;
  66. int num;
  67. struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
  68. status = urb->status;
  69. kfree(urb->transfer_buffer);
  70. usb_free_urb(urb);
  71. if (status == -ESHUTDOWN)
  72. return;
  73. spin_lock_irqsave(&line6->line6midi->lock, flags);
  74. num = --line6->line6midi->num_active_send_urbs;
  75. if (num == 0) {
  76. line6_midi_transmit(line6->line6midi->substream_transmit);
  77. num = line6->line6midi->num_active_send_urbs;
  78. }
  79. if (num == 0)
  80. wake_up(&line6->line6midi->send_wait);
  81. spin_unlock_irqrestore(&line6->line6midi->lock, flags);
  82. }
  83. /*
  84. Send an asynchronous MIDI message.
  85. Assumes that line6->line6midi->lock is held
  86. (i.e., this function is serialized).
  87. */
  88. static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
  89. int length)
  90. {
  91. struct urb *urb;
  92. int retval;
  93. unsigned char *transfer_buffer;
  94. urb = usb_alloc_urb(0, GFP_ATOMIC);
  95. if (urb == NULL)
  96. return -ENOMEM;
  97. transfer_buffer = kmemdup(data, length, GFP_ATOMIC);
  98. if (transfer_buffer == NULL) {
  99. usb_free_urb(urb);
  100. return -ENOMEM;
  101. }
  102. usb_fill_int_urb(urb, line6->usbdev,
  103. usb_sndintpipe(line6->usbdev,
  104. line6->properties->ep_ctrl_w),
  105. transfer_buffer, length, midi_sent, line6,
  106. line6->interval);
  107. urb->actual_length = 0;
  108. retval = usb_submit_urb(urb, GFP_ATOMIC);
  109. if (retval < 0) {
  110. dev_err(line6->ifcdev, "usb_submit_urb failed\n");
  111. usb_free_urb(urb);
  112. return retval;
  113. }
  114. ++line6->line6midi->num_active_send_urbs;
  115. return 0;
  116. }
  117. static int line6_midi_output_open(struct snd_rawmidi_substream *substream)
  118. {
  119. return 0;
  120. }
  121. static int line6_midi_output_close(struct snd_rawmidi_substream *substream)
  122. {
  123. return 0;
  124. }
  125. static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream,
  126. int up)
  127. {
  128. unsigned long flags;
  129. struct usb_line6 *line6 =
  130. line6_rawmidi_substream_midi(substream)->line6;
  131. line6->line6midi->substream_transmit = substream;
  132. spin_lock_irqsave(&line6->line6midi->lock, flags);
  133. if (line6->line6midi->num_active_send_urbs == 0)
  134. line6_midi_transmit(substream);
  135. spin_unlock_irqrestore(&line6->line6midi->lock, flags);
  136. }
  137. static void line6_midi_output_drain(struct snd_rawmidi_substream *substream)
  138. {
  139. struct usb_line6 *line6 =
  140. line6_rawmidi_substream_midi(substream)->line6;
  141. struct snd_line6_midi *midi = line6->line6midi;
  142. wait_event_interruptible(midi->send_wait,
  143. midi->num_active_send_urbs == 0);
  144. }
  145. static int line6_midi_input_open(struct snd_rawmidi_substream *substream)
  146. {
  147. return 0;
  148. }
  149. static int line6_midi_input_close(struct snd_rawmidi_substream *substream)
  150. {
  151. return 0;
  152. }
  153. static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream,
  154. int up)
  155. {
  156. struct usb_line6 *line6 =
  157. line6_rawmidi_substream_midi(substream)->line6;
  158. if (up)
  159. line6->line6midi->substream_receive = substream;
  160. else
  161. line6->line6midi->substream_receive = NULL;
  162. }
  163. static struct snd_rawmidi_ops line6_midi_output_ops = {
  164. .open = line6_midi_output_open,
  165. .close = line6_midi_output_close,
  166. .trigger = line6_midi_output_trigger,
  167. .drain = line6_midi_output_drain,
  168. };
  169. static struct snd_rawmidi_ops line6_midi_input_ops = {
  170. .open = line6_midi_input_open,
  171. .close = line6_midi_input_close,
  172. .trigger = line6_midi_input_trigger,
  173. };
  174. /* Create a MIDI device */
  175. static int snd_line6_new_midi(struct usb_line6 *line6,
  176. struct snd_rawmidi **rmidi_ret)
  177. {
  178. struct snd_rawmidi *rmidi;
  179. int err;
  180. err = snd_rawmidi_new(line6->card, "Line 6 MIDI", 0, 1, 1, rmidi_ret);
  181. if (err < 0)
  182. return err;
  183. rmidi = *rmidi_ret;
  184. strcpy(rmidi->id, line6->properties->id);
  185. strcpy(rmidi->name, line6->properties->name);
  186. rmidi->info_flags =
  187. SNDRV_RAWMIDI_INFO_OUTPUT |
  188. SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
  189. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
  190. &line6_midi_output_ops);
  191. snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
  192. &line6_midi_input_ops);
  193. return 0;
  194. }
  195. /* MIDI device destructor */
  196. static void snd_line6_midi_free(struct snd_rawmidi *rmidi)
  197. {
  198. struct snd_line6_midi *line6midi = rmidi->private_data;
  199. line6_midibuf_destroy(&line6midi->midibuf_in);
  200. line6_midibuf_destroy(&line6midi->midibuf_out);
  201. kfree(line6midi);
  202. }
  203. /*
  204. Initialize the Line 6 MIDI subsystem.
  205. */
  206. int line6_init_midi(struct usb_line6 *line6)
  207. {
  208. int err;
  209. struct snd_rawmidi *rmidi;
  210. struct snd_line6_midi *line6midi;
  211. if (!(line6->properties->capabilities & LINE6_CAP_CONTROL)) {
  212. /* skip MIDI initialization and report success */
  213. return 0;
  214. }
  215. err = snd_line6_new_midi(line6, &rmidi);
  216. if (err < 0)
  217. return err;
  218. line6midi = kzalloc(sizeof(struct snd_line6_midi), GFP_KERNEL);
  219. if (!line6midi)
  220. return -ENOMEM;
  221. rmidi->private_data = line6midi;
  222. rmidi->private_free = snd_line6_midi_free;
  223. init_waitqueue_head(&line6midi->send_wait);
  224. spin_lock_init(&line6midi->lock);
  225. line6midi->line6 = line6;
  226. err = line6_midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0);
  227. if (err < 0)
  228. return err;
  229. err = line6_midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1);
  230. if (err < 0)
  231. return err;
  232. line6->line6midi = line6midi;
  233. return 0;
  234. }
  235. EXPORT_SYMBOL_GPL(line6_init_midi);