rawmidi.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. bool inactive; /* inactive substream (for UMP legacy) */
  82. size_t bytes;
  83. spinlock_t lock;
  84. struct snd_rawmidi *rmidi;
  85. struct snd_rawmidi_str *pstr;
  86. char name[32];
  87. struct snd_rawmidi_runtime *runtime;
  88. struct pid *pid;
  89. /* hardware layer */
  90. const struct snd_rawmidi_ops *ops;
  91. };
  92. struct snd_rawmidi_file {
  93. struct snd_rawmidi *rmidi;
  94. struct snd_rawmidi_substream *input;
  95. struct snd_rawmidi_substream *output;
  96. unsigned int user_pversion; /* supported protocol version */
  97. };
  98. struct snd_rawmidi_str {
  99. unsigned int substream_count;
  100. unsigned int substream_opened;
  101. struct list_head substreams;
  102. };
  103. struct snd_rawmidi {
  104. struct snd_card *card;
  105. struct list_head list;
  106. unsigned int device; /* device number */
  107. unsigned int info_flags; /* SNDRV_RAWMIDI_INFO_XXXX */
  108. unsigned int tied_device;
  109. char id[64];
  110. char name[80];
  111. #ifdef CONFIG_SND_OSSEMUL
  112. int ossreg;
  113. #endif
  114. const struct snd_rawmidi_global_ops *ops;
  115. struct snd_rawmidi_str streams[2];
  116. void *private_data;
  117. void (*private_free) (struct snd_rawmidi *rmidi);
  118. struct mutex open_mutex;
  119. wait_queue_head_t open_wait;
  120. struct device *dev;
  121. struct snd_info_entry *proc_entry;
  122. #if IS_ENABLED(CONFIG_SND_SEQUENCER)
  123. struct snd_seq_device *seq_dev;
  124. #endif
  125. };
  126. /* main rawmidi functions */
  127. int snd_rawmidi_new(struct snd_card *card, char *id, int device,
  128. int output_count, int input_count,
  129. struct snd_rawmidi **rmidi);
  130. void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
  131. const struct snd_rawmidi_ops *ops);
  132. /* internal */
  133. int snd_rawmidi_init(struct snd_rawmidi *rmidi,
  134. struct snd_card *card, char *id, int device,
  135. int output_count, int input_count,
  136. unsigned int info_flags);
  137. int snd_rawmidi_free(struct snd_rawmidi *rmidi);
  138. /* callbacks */
  139. int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
  140. const unsigned char *buffer, int count);
  141. int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream);
  142. int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
  143. unsigned char *buffer, int count);
  144. int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count);
  145. int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
  146. unsigned char *buffer, int count);
  147. int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream);
  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_rawmidi *rmidi, 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. /* set up the tied devices */
  165. static inline void snd_rawmidi_tie_devices(struct snd_rawmidi *r1,
  166. struct snd_rawmidi *r2)
  167. {
  168. /* tied_device field keeps the device+1 (so that 0 being unknown) */
  169. r1->tied_device = r2->device + 1;
  170. r2->tied_device = r1->device + 1;
  171. }
  172. #endif /* __SOUND_RAWMIDI_H */