rawmidi.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. #ifndef __SOUND_RAWMIDI_H
  3. #define __SOUND_RAWMIDI_H
  4. /*
  5. * Abstract layer for MIDI v1.0 stream
  6. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  7. */
  8. #include <sound/asound.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/wait.h>
  12. #include <linux/mutex.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/device.h>
  15. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  16. #include <sound/seq_device.h>
  17. #endif
  18. #include <sound/info.h>
  19. /*
  20. * Raw MIDI interface
  21. */
  22. #define SNDRV_RAWMIDI_DEVICES 8
  23. #define SNDRV_RAWMIDI_LFLG_OUTPUT (1<<0)
  24. #define SNDRV_RAWMIDI_LFLG_INPUT (1<<1)
  25. #define SNDRV_RAWMIDI_LFLG_OPEN (3<<0)
  26. #define SNDRV_RAWMIDI_LFLG_APPEND (1<<2)
  27. struct snd_rawmidi;
  28. struct snd_rawmidi_substream;
  29. struct snd_seq_port_info;
  30. struct pid;
  31. struct snd_rawmidi_ops {
  32. int (*open) (struct snd_rawmidi_substream * substream);
  33. int (*close) (struct snd_rawmidi_substream * substream);
  34. void (*trigger) (struct snd_rawmidi_substream * substream, int up);
  35. void (*drain) (struct snd_rawmidi_substream * substream);
  36. };
  37. struct snd_rawmidi_global_ops {
  38. int (*dev_register) (struct snd_rawmidi * rmidi);
  39. int (*dev_unregister) (struct snd_rawmidi * rmidi);
  40. void (*get_port_info)(struct snd_rawmidi *rmidi, int number,
  41. struct snd_seq_port_info *info);
  42. long (*ioctl)(struct snd_rawmidi *rmidi, unsigned int cmd,
  43. void __user *argp);
  44. void (*proc_read)(struct snd_info_entry *entry,
  45. struct snd_info_buffer *buf);
  46. };
  47. struct snd_rawmidi_runtime {
  48. struct snd_rawmidi_substream *substream;
  49. unsigned int drain: 1, /* drain stage */
  50. oss: 1; /* OSS compatible mode */
  51. /* midi stream buffer */
  52. unsigned char *buffer; /* buffer for MIDI data */
  53. size_t buffer_size; /* size of buffer */
  54. size_t appl_ptr; /* application pointer */
  55. size_t hw_ptr; /* hardware pointer */
  56. size_t avail_min; /* min avail for wakeup */
  57. size_t avail; /* max used buffer for wakeup */
  58. size_t xruns; /* over/underruns counter */
  59. size_t align; /* alignment (0 = byte stream, 3 = UMP) */
  60. int buffer_ref; /* buffer reference count */
  61. /* misc */
  62. wait_queue_head_t sleep;
  63. /* event handler (new bytes, input only) */
  64. void (*event)(struct snd_rawmidi_substream *substream);
  65. /* defers calls to event [input] or ops->trigger [output] */
  66. struct work_struct event_work;
  67. /* private data */
  68. void *private_data;
  69. void (*private_free)(struct snd_rawmidi_substream *substream);
  70. };
  71. struct snd_rawmidi_substream {
  72. struct list_head list; /* list of all substream for given stream */
  73. int stream; /* direction */
  74. int number; /* substream number */
  75. bool opened; /* open flag */
  76. bool append; /* append flag (merge more streams) */
  77. bool active_sensing; /* send active sensing when close */
  78. unsigned int framing; /* whether to frame input data */
  79. unsigned int clock_type; /* clock source to use for input framing */
  80. int use_count; /* use counter (for output) */
  81. size_t bytes;
  82. spinlock_t lock;
  83. struct snd_rawmidi *rmidi;
  84. struct snd_rawmidi_str *pstr;
  85. char name[32];
  86. struct snd_rawmidi_runtime *runtime;
  87. struct pid *pid;
  88. /* hardware layer */
  89. const struct snd_rawmidi_ops *ops;
  90. };
  91. struct snd_rawmidi_file {
  92. struct snd_rawmidi *rmidi;
  93. struct snd_rawmidi_substream *input;
  94. struct snd_rawmidi_substream *output;
  95. unsigned int user_pversion; /* supported protocol version */
  96. };
  97. struct snd_rawmidi_str {
  98. unsigned int substream_count;
  99. unsigned int substream_opened;
  100. struct list_head substreams;
  101. };
  102. struct snd_rawmidi {
  103. struct snd_card *card;
  104. struct list_head list;
  105. unsigned int device; /* device number */
  106. unsigned int info_flags; /* SNDRV_RAWMIDI_INFO_XXXX */
  107. char id[64];
  108. char name[80];
  109. #ifdef CONFIG_SND_OSSEMUL
  110. int ossreg;
  111. #endif
  112. const struct snd_rawmidi_global_ops *ops;
  113. struct snd_rawmidi_str streams[2];
  114. void *private_data;
  115. void (*private_free) (struct snd_rawmidi *rmidi);
  116. struct mutex open_mutex;
  117. wait_queue_head_t open_wait;
  118. struct device *dev;
  119. struct snd_info_entry *proc_entry;
  120. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  121. struct snd_seq_device *seq_dev;
  122. #endif
  123. };
  124. /* main rawmidi functions */
  125. int snd_rawmidi_new(struct snd_card *card, char *id, int device,
  126. int output_count, int input_count,
  127. struct snd_rawmidi **rmidi);
  128. void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
  129. const struct snd_rawmidi_ops *ops);
  130. /* internal */
  131. int snd_rawmidi_init(struct snd_rawmidi *rmidi,
  132. struct snd_card *card, char *id, int device,
  133. int output_count, int input_count,
  134. unsigned int info_flags);
  135. int snd_rawmidi_free(struct snd_rawmidi *rmidi);
  136. /* callbacks */
  137. int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
  138. const unsigned char *buffer, int count);
  139. int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream);
  140. int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
  141. unsigned char *buffer, int count);
  142. int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count);
  143. int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
  144. unsigned char *buffer, int count);
  145. int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream);
  146. /* main midi functions */
  147. int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info);
  148. int snd_rawmidi_kernel_open(struct snd_rawmidi *rmidi, int subdevice,
  149. int mode, struct snd_rawmidi_file *rfile);
  150. int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile);
  151. int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
  152. struct snd_rawmidi_params *params);
  153. int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
  154. struct snd_rawmidi_params *params);
  155. int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream);
  156. int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream);
  157. int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream);
  158. long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
  159. unsigned char *buf, long count);
  160. long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
  161. const unsigned char *buf, long count);
  162. #endif /* __SOUND_RAWMIDI_H */