rawmidi.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. int buffer_ref; /* buffer reference count */
  70. /* misc */
  71. spinlock_t lock;
  72. wait_queue_head_t sleep;
  73. /* event handler (new bytes, input only) */
  74. void (*event)(struct snd_rawmidi_substream *substream);
  75. /* defers calls to event [input] or ops->trigger [output] */
  76. struct work_struct event_work;
  77. /* private data */
  78. void *private_data;
  79. void (*private_free)(struct snd_rawmidi_substream *substream);
  80. };
  81. struct snd_rawmidi_substream {
  82. struct list_head list; /* list of all substream for given stream */
  83. int stream; /* direction */
  84. int number; /* substream number */
  85. bool opened; /* open flag */
  86. bool append; /* append flag (merge more streams) */
  87. bool active_sensing; /* send active sensing when close */
  88. int use_count; /* use counter (for output) */
  89. size_t bytes;
  90. struct snd_rawmidi *rmidi;
  91. struct snd_rawmidi_str *pstr;
  92. char name[32];
  93. struct snd_rawmidi_runtime *runtime;
  94. struct pid *pid;
  95. /* hardware layer */
  96. const struct snd_rawmidi_ops *ops;
  97. };
  98. struct snd_rawmidi_file {
  99. struct snd_rawmidi *rmidi;
  100. struct snd_rawmidi_substream *input;
  101. struct snd_rawmidi_substream *output;
  102. };
  103. struct snd_rawmidi_str {
  104. unsigned int substream_count;
  105. unsigned int substream_opened;
  106. struct list_head substreams;
  107. };
  108. struct snd_rawmidi {
  109. struct snd_card *card;
  110. struct list_head list;
  111. unsigned int device; /* device number */
  112. unsigned int info_flags; /* SNDRV_RAWMIDI_INFO_XXXX */
  113. char id[64];
  114. char name[80];
  115. #ifdef CONFIG_SND_OSSEMUL
  116. int ossreg;
  117. #endif
  118. const struct snd_rawmidi_global_ops *ops;
  119. struct snd_rawmidi_str streams[2];
  120. void *private_data;
  121. void (*private_free) (struct snd_rawmidi *rmidi);
  122. struct mutex open_mutex;
  123. wait_queue_head_t open_wait;
  124. struct device dev;
  125. struct snd_info_entry *proc_entry;
  126. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  127. struct snd_seq_device *seq_dev;
  128. #endif
  129. };
  130. /* main rawmidi functions */
  131. int snd_rawmidi_new(struct snd_card *card, char *id, int device,
  132. int output_count, int input_count,
  133. struct snd_rawmidi **rmidi);
  134. void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
  135. const struct snd_rawmidi_ops *ops);
  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_transmit_peek(struct snd_rawmidi_substream *substream,
  146. unsigned char *buffer, int count);
  147. int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream,
  148. int count);
  149. /* main midi functions */
  150. int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info);
  151. int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
  152. int mode, struct snd_rawmidi_file *rfile);
  153. int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile);
  154. int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
  155. struct snd_rawmidi_params *params);
  156. int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
  157. struct snd_rawmidi_params *params);
  158. int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream);
  159. int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream);
  160. int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream);
  161. long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
  162. unsigned char *buf, long count);
  163. long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
  164. const unsigned char *buf, long count);
  165. #endif /* __SOUND_RAWMIDI_H */