midibuf.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Line6 Linux USB driver - 0.9.1beta
  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 "midibuf.h"
  13. static int midibuf_message_length(unsigned char code)
  14. {
  15. int message_length;
  16. if (code < 0x80)
  17. message_length = -1;
  18. else if (code < 0xf0) {
  19. static const int length[] = { 3, 3, 3, 3, 2, 2, 3 };
  20. message_length = length[(code >> 4) - 8];
  21. } else {
  22. /*
  23. Note that according to the MIDI specification 0xf2 is
  24. the "Song Position Pointer", but this is used by Line6
  25. to send sysex messages to the host.
  26. */
  27. static const int length[] = { -1, 2, -1, 2, -1, -1, 1, 1, 1, 1,
  28. 1, 1, 1, -1, 1, 1
  29. };
  30. message_length = length[code & 0x0f];
  31. }
  32. return message_length;
  33. }
  34. static int midibuf_is_empty(struct midi_buffer *this)
  35. {
  36. return (this->pos_read == this->pos_write) && !this->full;
  37. }
  38. static int midibuf_is_full(struct midi_buffer *this)
  39. {
  40. return this->full;
  41. }
  42. void line6_midibuf_reset(struct midi_buffer *this)
  43. {
  44. this->pos_read = this->pos_write = this->full = 0;
  45. this->command_prev = -1;
  46. }
  47. int line6_midibuf_init(struct midi_buffer *this, int size, int split)
  48. {
  49. this->buf = kmalloc(size, GFP_KERNEL);
  50. if (this->buf == NULL)
  51. return -ENOMEM;
  52. this->size = size;
  53. this->split = split;
  54. line6_midibuf_reset(this);
  55. return 0;
  56. }
  57. void line6_midibuf_status(struct midi_buffer *this)
  58. {
  59. pr_debug("midibuf size=%d split=%d pos_read=%d pos_write=%d full=%d command_prev=%02x\n",
  60. this->size, this->split, this->pos_read, this->pos_write,
  61. this->full, this->command_prev);
  62. }
  63. int line6_midibuf_bytes_free(struct midi_buffer *this)
  64. {
  65. return
  66. midibuf_is_full(this) ?
  67. 0 :
  68. (this->pos_read - this->pos_write + this->size - 1) % this->size +
  69. 1;
  70. }
  71. int line6_midibuf_bytes_used(struct midi_buffer *this)
  72. {
  73. return
  74. midibuf_is_empty(this) ?
  75. 0 :
  76. (this->pos_write - this->pos_read + this->size - 1) % this->size +
  77. 1;
  78. }
  79. int line6_midibuf_write(struct midi_buffer *this, unsigned char *data,
  80. int length)
  81. {
  82. int bytes_free;
  83. int length1, length2;
  84. int skip_active_sense = 0;
  85. if (midibuf_is_full(this) || (length <= 0))
  86. return 0;
  87. /* skip trailing active sense */
  88. if (data[length - 1] == 0xfe) {
  89. --length;
  90. skip_active_sense = 1;
  91. }
  92. bytes_free = line6_midibuf_bytes_free(this);
  93. if (length > bytes_free)
  94. length = bytes_free;
  95. if (length > 0) {
  96. length1 = this->size - this->pos_write;
  97. if (length < length1) {
  98. /* no buffer wraparound */
  99. memcpy(this->buf + this->pos_write, data, length);
  100. this->pos_write += length;
  101. } else {
  102. /* buffer wraparound */
  103. length2 = length - length1;
  104. memcpy(this->buf + this->pos_write, data, length1);
  105. memcpy(this->buf, data + length1, length2);
  106. this->pos_write = length2;
  107. }
  108. if (this->pos_write == this->pos_read)
  109. this->full = 1;
  110. }
  111. return length + skip_active_sense;
  112. }
  113. int line6_midibuf_read(struct midi_buffer *this, unsigned char *data,
  114. int length)
  115. {
  116. int bytes_used;
  117. int length1, length2;
  118. int command;
  119. int midi_length;
  120. int repeat = 0;
  121. int i;
  122. /* we need to be able to store at least a 3 byte MIDI message */
  123. if (length < 3)
  124. return -EINVAL;
  125. if (midibuf_is_empty(this))
  126. return 0;
  127. bytes_used = line6_midibuf_bytes_used(this);
  128. if (length > bytes_used)
  129. length = bytes_used;
  130. length1 = this->size - this->pos_read;
  131. /* check MIDI command length */
  132. command = this->buf[this->pos_read];
  133. if (command & 0x80) {
  134. midi_length = midibuf_message_length(command);
  135. this->command_prev = command;
  136. } else {
  137. if (this->command_prev > 0) {
  138. int midi_length_prev =
  139. midibuf_message_length(this->command_prev);
  140. if (midi_length_prev > 0) {
  141. midi_length = midi_length_prev - 1;
  142. repeat = 1;
  143. } else
  144. midi_length = -1;
  145. } else
  146. midi_length = -1;
  147. }
  148. if (midi_length < 0) {
  149. /* search for end of message */
  150. if (length < length1) {
  151. /* no buffer wraparound */
  152. for (i = 1; i < length; ++i)
  153. if (this->buf[this->pos_read + i] & 0x80)
  154. break;
  155. midi_length = i;
  156. } else {
  157. /* buffer wraparound */
  158. length2 = length - length1;
  159. for (i = 1; i < length1; ++i)
  160. if (this->buf[this->pos_read + i] & 0x80)
  161. break;
  162. if (i < length1)
  163. midi_length = i;
  164. else {
  165. for (i = 0; i < length2; ++i)
  166. if (this->buf[i] & 0x80)
  167. break;
  168. midi_length = length1 + i;
  169. }
  170. }
  171. if (midi_length == length)
  172. midi_length = -1; /* end of message not found */
  173. }
  174. if (midi_length < 0) {
  175. if (!this->split)
  176. return 0; /* command is not yet complete */
  177. } else {
  178. if (length < midi_length)
  179. return 0; /* command is not yet complete */
  180. length = midi_length;
  181. }
  182. if (length < length1) {
  183. /* no buffer wraparound */
  184. memcpy(data + repeat, this->buf + this->pos_read, length);
  185. this->pos_read += length;
  186. } else {
  187. /* buffer wraparound */
  188. length2 = length - length1;
  189. memcpy(data + repeat, this->buf + this->pos_read, length1);
  190. memcpy(data + repeat + length1, this->buf, length2);
  191. this->pos_read = length2;
  192. }
  193. if (repeat)
  194. data[0] = this->command_prev;
  195. this->full = 0;
  196. return length + repeat;
  197. }
  198. int line6_midibuf_ignore(struct midi_buffer *this, int length)
  199. {
  200. int bytes_used = line6_midibuf_bytes_used(this);
  201. if (length > bytes_used)
  202. length = bytes_used;
  203. this->pos_read = (this->pos_read + length) % this->size;
  204. this->full = 0;
  205. return length;
  206. }
  207. int line6_midibuf_skip_message(struct midi_buffer *this, unsigned short mask)
  208. {
  209. int cmd = this->command_prev;
  210. if ((cmd >= 0x80) && (cmd < 0xf0))
  211. if ((mask & (1 << (cmd & 0x0f))) == 0)
  212. return 1;
  213. return 0;
  214. }
  215. void line6_midibuf_destroy(struct midi_buffer *this)
  216. {
  217. kfree(this->buf);
  218. this->buf = NULL;
  219. }