rawmidi.h 5.5 KB

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