rawmidi.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #ifndef __SOUND_RAWMIDI_H
  2. #define __SOUND_RAWMIDI_H
  3. /*
  4. * Abstract layer for MIDI v1.0 stream
  5. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  6. *
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. #include <sound/asound.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/wait.h>
  27. #include <linux/mutex.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/device.h>
  30. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  31. #include <sound/seq_device.h>
  32. #endif
  33. /*
  34. * Raw MIDI interface
  35. */
  36. #define SNDRV_RAWMIDI_DEVICES 8
  37. #define SNDRV_RAWMIDI_LFLG_OUTPUT (1<<0)
  38. #define SNDRV_RAWMIDI_LFLG_INPUT (1<<1)
  39. #define SNDRV_RAWMIDI_LFLG_OPEN (3<<0)
  40. #define SNDRV_RAWMIDI_LFLG_APPEND (1<<2)
  41. struct snd_rawmidi;
  42. struct snd_rawmidi_substream;
  43. struct snd_seq_port_info;
  44. struct pid;
  45. struct snd_rawmidi_ops {
  46. int (*open) (struct snd_rawmidi_substream * substream);
  47. int (*close) (struct snd_rawmidi_substream * substream);
  48. void (*trigger) (struct snd_rawmidi_substream * substream, int up);
  49. void (*drain) (struct snd_rawmidi_substream * substream);
  50. };
  51. struct snd_rawmidi_global_ops {
  52. int (*dev_register) (struct snd_rawmidi * rmidi);
  53. int (*dev_unregister) (struct snd_rawmidi * rmidi);
  54. void (*get_port_info)(struct snd_rawmidi *rmidi, int number,
  55. struct snd_seq_port_info *info);
  56. };
  57. struct snd_rawmidi_runtime {
  58. struct snd_rawmidi_substream *substream;
  59. unsigned int drain: 1, /* drain stage */
  60. oss: 1; /* OSS compatible mode */
  61. /* midi stream buffer */
  62. unsigned char *buffer; /* buffer for MIDI data */
  63. size_t buffer_size; /* size of buffer */
  64. size_t appl_ptr; /* application pointer */
  65. size_t hw_ptr; /* hardware pointer */
  66. size_t avail_min; /* min avail for wakeup */
  67. size_t avail; /* max used buffer for wakeup */
  68. size_t xruns; /* over/underruns counter */
  69. /* misc */
  70. spinlock_t lock;
  71. wait_queue_head_t sleep;
  72. /* event handler (new bytes, input only) */
  73. void (*event)(struct snd_rawmidi_substream *substream);
  74. /* defers calls to event [input] or ops->trigger [output] */
  75. struct work_struct event_work;
  76. /* private data */
  77. void *private_data;
  78. void (*private_free)(struct snd_rawmidi_substream *substream);
  79. };
  80. struct snd_rawmidi_substream {
  81. struct list_head list; /* list of all substream for given stream */
  82. int stream; /* direction */
  83. int number; /* substream number */
  84. bool opened; /* open flag */
  85. bool append; /* append flag (merge more streams) */
  86. bool active_sensing; /* send active sensing when close */
  87. int use_count; /* use counter (for output) */
  88. size_t bytes;
  89. struct snd_rawmidi *rmidi;
  90. struct snd_rawmidi_str *pstr;
  91. char name[32];
  92. struct snd_rawmidi_runtime *runtime;
  93. struct pid *pid;
  94. /* hardware layer */
  95. const struct snd_rawmidi_ops *ops;
  96. };
  97. struct snd_rawmidi_file {
  98. struct snd_rawmidi *rmidi;
  99. struct snd_rawmidi_substream *input;
  100. struct snd_rawmidi_substream *output;
  101. };
  102. struct snd_rawmidi_str {
  103. unsigned int substream_count;
  104. unsigned int substream_opened;
  105. struct list_head substreams;
  106. };
  107. struct snd_rawmidi {
  108. struct snd_card *card;
  109. struct list_head list;
  110. unsigned int device; /* device number */
  111. unsigned int info_flags; /* SNDRV_RAWMIDI_INFO_XXXX */
  112. char id[64];
  113. char name[80];
  114. #ifdef CONFIG_SND_OSSEMUL
  115. int ossreg;
  116. #endif
  117. const struct snd_rawmidi_global_ops *ops;
  118. struct snd_rawmidi_str streams[2];
  119. void *private_data;
  120. void (*private_free) (struct snd_rawmidi *rmidi);
  121. struct mutex open_mutex;
  122. wait_queue_head_t open_wait;
  123. struct device dev;
  124. struct snd_info_entry *proc_entry;
  125. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  126. struct snd_seq_device *seq_dev;
  127. #endif
  128. };
  129. /* main rawmidi functions */
  130. int snd_rawmidi_new(struct snd_card *card, char *id, int device,
  131. int output_count, int input_count,
  132. struct snd_rawmidi **rmidi);
  133. void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
  134. const struct snd_rawmidi_ops *ops);
  135. /* callbacks */
  136. int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
  137. const unsigned char *buffer, int count);
  138. int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream);
  139. int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
  140. unsigned char *buffer, int count);
  141. int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count);
  142. int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
  143. unsigned char *buffer, int count);
  144. int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
  145. unsigned char *buffer, int count);
  146. int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream,
  147. int count);
  148. /* main midi functions */
  149. int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info);
  150. int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
  151. int mode, struct snd_rawmidi_file *rfile);
  152. int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile);
  153. int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
  154. struct snd_rawmidi_params *params);
  155. int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
  156. struct snd_rawmidi_params *params);
  157. int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream);
  158. int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream);
  159. int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream);
  160. long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
  161. unsigned char *buf, long count);
  162. long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
  163. const unsigned char *buf, long count);
  164. #endif /* __SOUND_RAWMIDI_H */