ump.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Universal MIDI Packet (UMP) Support
  4. */
  5. #ifndef __SOUND_UMP_H
  6. #define __SOUND_UMP_H
  7. #include <sound/rawmidi.h>
  8. struct snd_ump_endpoint;
  9. struct snd_ump_block;
  10. struct snd_ump_ops;
  11. struct ump_cvt_to_ump;
  12. struct snd_seq_ump_ops;
  13. struct snd_ump_group {
  14. int group; /* group index (0-based) */
  15. unsigned int dir_bits; /* directions */
  16. bool active; /* activeness */
  17. bool valid; /* valid group (referred by blocks) */
  18. bool is_midi1; /* belongs to a MIDI1 FB */
  19. char name[64]; /* group name */
  20. };
  21. struct snd_ump_endpoint {
  22. struct snd_rawmidi core; /* raw UMP access */
  23. struct snd_ump_endpoint_info info;
  24. const struct snd_ump_ops *ops; /* UMP ops set by the driver */
  25. struct snd_rawmidi_substream *substreams[2]; /* opened substreams */
  26. void *private_data;
  27. void (*private_free)(struct snd_ump_endpoint *ump);
  28. /* UMP Stream message processing */
  29. u32 stream_wait_for; /* expected stream message status */
  30. bool stream_finished; /* set when message has been processed */
  31. bool parsed; /* UMP / FB parse finished? */
  32. bool no_process_stream; /* suppress UMP stream messages handling */
  33. wait_queue_head_t stream_wait;
  34. struct snd_rawmidi_file stream_rfile;
  35. struct list_head block_list; /* list of snd_ump_block objects */
  36. /* intermediate buffer for UMP input */
  37. u32 input_buf[4];
  38. int input_buf_head;
  39. int input_pending;
  40. struct mutex open_mutex;
  41. struct snd_ump_group groups[SNDRV_UMP_MAX_GROUPS]; /* table of groups */
  42. #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI)
  43. spinlock_t legacy_locks[2];
  44. struct snd_rawmidi *legacy_rmidi;
  45. struct snd_rawmidi_substream *legacy_substreams[2][SNDRV_UMP_MAX_GROUPS];
  46. unsigned char legacy_mapping[SNDRV_UMP_MAX_GROUPS];
  47. /* for legacy output; need to open the actual substream unlike input */
  48. int legacy_out_opens;
  49. struct snd_rawmidi_file legacy_out_rfile;
  50. struct ump_cvt_to_ump *out_cvts;
  51. #endif
  52. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  53. struct snd_seq_device *seq_dev;
  54. const struct snd_seq_ump_ops *seq_ops;
  55. void *seq_client;
  56. #endif
  57. };
  58. /* ops filled by UMP drivers */
  59. struct snd_ump_ops {
  60. int (*open)(struct snd_ump_endpoint *ump, int dir);
  61. void (*close)(struct snd_ump_endpoint *ump, int dir);
  62. void (*trigger)(struct snd_ump_endpoint *ump, int dir, int up);
  63. void (*drain)(struct snd_ump_endpoint *ump, int dir);
  64. };
  65. /* ops filled by sequencer binding */
  66. struct snd_seq_ump_ops {
  67. void (*input_receive)(struct snd_ump_endpoint *ump,
  68. const u32 *data, int words);
  69. int (*notify_ep_change)(struct snd_ump_endpoint *ump);
  70. int (*notify_fb_change)(struct snd_ump_endpoint *ump,
  71. struct snd_ump_block *fb);
  72. int (*switch_protocol)(struct snd_ump_endpoint *ump);
  73. };
  74. struct snd_ump_block {
  75. struct snd_ump_block_info info;
  76. struct snd_ump_endpoint *ump;
  77. void *private_data;
  78. void (*private_free)(struct snd_ump_block *blk);
  79. struct list_head list;
  80. };
  81. #define rawmidi_to_ump(rmidi) container_of(rmidi, struct snd_ump_endpoint, core)
  82. int snd_ump_endpoint_new(struct snd_card *card, char *id, int device,
  83. int output, int input,
  84. struct snd_ump_endpoint **ump_ret);
  85. int snd_ump_parse_endpoint(struct snd_ump_endpoint *ump);
  86. int snd_ump_block_new(struct snd_ump_endpoint *ump, unsigned int blk,
  87. unsigned int direction, unsigned int first_group,
  88. unsigned int num_groups, struct snd_ump_block **blk_ret);
  89. int snd_ump_receive(struct snd_ump_endpoint *ump, const u32 *buffer, int count);
  90. int snd_ump_transmit(struct snd_ump_endpoint *ump, u32 *buffer, int count);
  91. #if IS_ENABLED(CONFIG_SND_UMP_LEGACY_RAWMIDI)
  92. int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump,
  93. char *id, int device);
  94. #else
  95. static inline int snd_ump_attach_legacy_rawmidi(struct snd_ump_endpoint *ump,
  96. char *id, int device)
  97. {
  98. return 0;
  99. }
  100. #endif
  101. int snd_ump_receive_ump_val(struct snd_ump_endpoint *ump, u32 val);
  102. int snd_ump_switch_protocol(struct snd_ump_endpoint *ump, unsigned int protocol);
  103. void snd_ump_update_group_attrs(struct snd_ump_endpoint *ump);
  104. /*
  105. * Some definitions for UMP
  106. */
  107. /* MIDI 2.0 Message Type */
  108. enum {
  109. UMP_MSG_TYPE_UTILITY = 0x00,
  110. UMP_MSG_TYPE_SYSTEM = 0x01,
  111. UMP_MSG_TYPE_MIDI1_CHANNEL_VOICE = 0x02,
  112. UMP_MSG_TYPE_DATA = 0x03,
  113. UMP_MSG_TYPE_MIDI2_CHANNEL_VOICE = 0x04,
  114. UMP_MSG_TYPE_EXTENDED_DATA = 0x05,
  115. UMP_MSG_TYPE_FLEX_DATA = 0x0d,
  116. UMP_MSG_TYPE_STREAM = 0x0f,
  117. };
  118. /* MIDI 2.0 SysEx / Data Status; same values for both 7-bit and 8-bit SysEx */
  119. enum {
  120. UMP_SYSEX_STATUS_SINGLE = 0,
  121. UMP_SYSEX_STATUS_START = 1,
  122. UMP_SYSEX_STATUS_CONTINUE = 2,
  123. UMP_SYSEX_STATUS_END = 3,
  124. };
  125. /* UMP Utility Type Status (type 0x0) */
  126. enum {
  127. UMP_UTILITY_MSG_STATUS_NOOP = 0x00,
  128. UMP_UTILITY_MSG_STATUS_JR_CLOCK = 0x01,
  129. UMP_UTILITY_MSG_STATUS_JR_TSTAMP = 0x02,
  130. UMP_UTILITY_MSG_STATUS_DCTPQ = 0x03,
  131. UMP_UTILITY_MSG_STATUS_DC = 0x04,
  132. };
  133. /* UMP Stream Message Status (type 0xf) */
  134. enum {
  135. UMP_STREAM_MSG_STATUS_EP_DISCOVERY = 0x00,
  136. UMP_STREAM_MSG_STATUS_EP_INFO = 0x01,
  137. UMP_STREAM_MSG_STATUS_DEVICE_INFO = 0x02,
  138. UMP_STREAM_MSG_STATUS_EP_NAME = 0x03,
  139. UMP_STREAM_MSG_STATUS_PRODUCT_ID = 0x04,
  140. UMP_STREAM_MSG_STATUS_STREAM_CFG_REQUEST = 0x05,
  141. UMP_STREAM_MSG_STATUS_STREAM_CFG = 0x06,
  142. UMP_STREAM_MSG_STATUS_FB_DISCOVERY = 0x10,
  143. UMP_STREAM_MSG_STATUS_FB_INFO = 0x11,
  144. UMP_STREAM_MSG_STATUS_FB_NAME = 0x12,
  145. UMP_STREAM_MSG_STATUS_START_CLIP = 0x20,
  146. UMP_STREAM_MSG_STATUS_END_CLIP = 0x21,
  147. };
  148. /* UMP Endpoint Discovery filter bitmap */
  149. enum {
  150. UMP_STREAM_MSG_REQUEST_EP_INFO = (1U << 0),
  151. UMP_STREAM_MSG_REQUEST_DEVICE_INFO = (1U << 1),
  152. UMP_STREAM_MSG_REQUEST_EP_NAME = (1U << 2),
  153. UMP_STREAM_MSG_REQUEST_PRODUCT_ID = (1U << 3),
  154. UMP_STREAM_MSG_REQUEST_STREAM_CFG = (1U << 4),
  155. };
  156. /* UMP Function Block Discovery filter bitmap */
  157. enum {
  158. UMP_STREAM_MSG_REQUEST_FB_INFO = (1U << 0),
  159. UMP_STREAM_MSG_REQUEST_FB_NAME = (1U << 1),
  160. };
  161. /* UMP Endpoint Info capability bits (used for protocol request/notify, too) */
  162. enum {
  163. UMP_STREAM_MSG_EP_INFO_CAP_TXJR = (1U << 0), /* Sending JRTS */
  164. UMP_STREAM_MSG_EP_INFO_CAP_RXJR = (1U << 1), /* Receiving JRTS */
  165. UMP_STREAM_MSG_EP_INFO_CAP_MIDI1 = (1U << 8), /* MIDI 1.0 */
  166. UMP_STREAM_MSG_EP_INFO_CAP_MIDI2 = (1U << 9), /* MIDI 2.0 */
  167. };
  168. /* UMP EP / FB name string format; same as SysEx string handling */
  169. enum {
  170. UMP_STREAM_MSG_FORMAT_SINGLE = 0,
  171. UMP_STREAM_MSG_FORMAT_START = 1,
  172. UMP_STREAM_MSG_FORMAT_CONTINUE = 2,
  173. UMP_STREAM_MSG_FORMAT_END = 3,
  174. };
  175. /*
  176. * Helpers for retrieving / filling bits from UMP
  177. */
  178. /* get the message type (4bit) from a UMP packet (header) */
  179. static inline unsigned char ump_message_type(u32 data)
  180. {
  181. return data >> 28;
  182. }
  183. /* get the group number (0-based, 4bit) from a UMP packet (header) */
  184. static inline unsigned char ump_message_group(u32 data)
  185. {
  186. return (data >> 24) & 0x0f;
  187. }
  188. /* get the MIDI status code (4bit) from a UMP packet (header) */
  189. static inline unsigned char ump_message_status_code(u32 data)
  190. {
  191. return (data >> 20) & 0x0f;
  192. }
  193. /* get the MIDI channel number (0-based, 4bit) from a UMP packet (header) */
  194. static inline unsigned char ump_message_channel(u32 data)
  195. {
  196. return (data >> 16) & 0x0f;
  197. }
  198. /* get the MIDI status + channel combo byte (8bit) from a UMP packet (header) */
  199. static inline unsigned char ump_message_status_channel(u32 data)
  200. {
  201. return (data >> 16) & 0xff;
  202. }
  203. /* compose a UMP packet (header) from type, group and status values */
  204. static inline u32 ump_compose(unsigned char type, unsigned char group,
  205. unsigned char status, unsigned char channel)
  206. {
  207. return ((u32)type << 28) | ((u32)group << 24) | ((u32)status << 20) |
  208. ((u32)channel << 16);
  209. }
  210. /* get SysEx message status (for both 7 and 8bits) from a UMP packet (header) */
  211. static inline unsigned char ump_sysex_message_status(u32 data)
  212. {
  213. return (data >> 20) & 0xf;
  214. }
  215. /* get SysEx message length (for both 7 and 8bits) from a UMP packet (header) */
  216. static inline unsigned char ump_sysex_message_length(u32 data)
  217. {
  218. return (data >> 16) & 0xf;
  219. }
  220. /* For Stream Messages */
  221. static inline unsigned char ump_stream_message_format(u32 data)
  222. {
  223. return (data >> 26) & 0x03;
  224. }
  225. static inline unsigned int ump_stream_message_status(u32 data)
  226. {
  227. return (data >> 16) & 0x3ff;
  228. }
  229. static inline u32 ump_stream_compose(unsigned char status, unsigned short form)
  230. {
  231. return (UMP_MSG_TYPE_STREAM << 28) | ((u32)form << 26) |
  232. ((u32)status << 16);
  233. }
  234. #define ump_is_groupless_msg(type) \
  235. ((type) == UMP_MSG_TYPE_UTILITY || (type) == UMP_MSG_TYPE_STREAM)
  236. #endif /* __SOUND_UMP_H */