xref: /linux/include/sound/rawmidi.h (revision 4c4e4be4edd5dbf5f7f6a3ef4fc0c243f2d01b3c)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef __SOUND_RAWMIDI_H
3 #define __SOUND_RAWMIDI_H
4 
5 /*
6  *  Abstract layer for MIDI v1.0 stream
7  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
8  */
9 
10 #include <sound/asound.h>
11 #include <linux/interrupt.h>
12 #include <linux/spinlock.h>
13 #include <linux/wait.h>
14 #include <linux/mutex.h>
15 #include <linux/workqueue.h>
16 #include <linux/device.h>
17 
18 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
19 #include <sound/seq_device.h>
20 #endif
21 #include <sound/info.h>
22 
23 /*
24  *  Raw MIDI interface
25  */
26 
27 #define SNDRV_RAWMIDI_DEVICES		8
28 
29 #define SNDRV_RAWMIDI_LFLG_OUTPUT	(1<<0)
30 #define SNDRV_RAWMIDI_LFLG_INPUT	(1<<1)
31 #define SNDRV_RAWMIDI_LFLG_OPEN		(3<<0)
32 #define SNDRV_RAWMIDI_LFLG_APPEND	(1<<2)
33 
34 struct snd_rawmidi;
35 struct snd_rawmidi_substream;
36 struct snd_seq_port_info;
37 struct pid;
38 
39 struct snd_rawmidi_ops {
40 	int (*open) (struct snd_rawmidi_substream * substream);
41 	int (*close) (struct snd_rawmidi_substream * substream);
42 	void (*trigger) (struct snd_rawmidi_substream * substream, int up);
43 	void (*drain) (struct snd_rawmidi_substream * substream);
44 };
45 
46 struct snd_rawmidi_global_ops {
47 	int (*dev_register) (struct snd_rawmidi * rmidi);
48 	int (*dev_unregister) (struct snd_rawmidi * rmidi);
49 	void (*get_port_info)(struct snd_rawmidi *rmidi, int number,
50 			      struct snd_seq_port_info *info);
51 	long (*ioctl)(struct snd_rawmidi *rmidi, unsigned int cmd,
52 		      void __user *argp);
53 	void (*proc_read)(struct snd_info_entry *entry,
54 			  struct snd_info_buffer *buf);
55 };
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 	size_t align;		/* alignment (0 = byte stream, 3 = UMP) */
70 	int buffer_ref;		/* buffer reference count */
71 	/* misc */
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 
82 struct snd_rawmidi_substream {
83 	struct list_head list;		/* list of all substream for given stream */
84 	int stream;			/* direction */
85 	int number;			/* substream number */
86 	bool opened;			/* open flag */
87 	bool append;			/* append flag (merge more streams) */
88 	bool active_sensing;		/* send active sensing when close */
89 	bool disconnected;		/* already disconnected */
90 	unsigned int framing;		/* whether to frame input data */
91 	unsigned int clock_type;	/* clock source to use for input framing */
92 	int use_count;			/* use counter (for output) */
93 	bool inactive;			/* inactive substream (for UMP legacy) */
94 	size_t bytes;
95 	spinlock_t lock;
96 	struct snd_rawmidi *rmidi;
97 	struct snd_rawmidi_str *pstr;
98 	char name[32];
99 	struct snd_rawmidi_runtime *runtime;
100 	struct pid *pid;
101 	/* hardware layer */
102 	const struct snd_rawmidi_ops *ops;
103 };
104 
105 struct snd_rawmidi_file {
106 	struct snd_rawmidi *rmidi;
107 	struct snd_rawmidi_substream *input;
108 	struct snd_rawmidi_substream *output;
109 	unsigned int user_pversion;	/* supported protocol version */
110 };
111 
112 struct snd_rawmidi_str {
113 	unsigned int substream_count;
114 	unsigned int substream_opened;
115 	struct list_head substreams;
116 };
117 
118 struct snd_rawmidi {
119 	struct snd_card *card;
120 	struct list_head list;
121 	unsigned int device;		/* device number */
122 	unsigned int info_flags;	/* SNDRV_RAWMIDI_INFO_XXXX */
123 	unsigned int tied_device;
124 	char id[64];
125 	char name[80];
126 
127 #ifdef CONFIG_SND_OSSEMUL
128 	int ossreg;
129 #endif
130 
131 	const struct snd_rawmidi_global_ops *ops;
132 
133 	struct snd_rawmidi_str streams[2];
134 
135 	void *private_data;
136 	void (*private_free) (struct snd_rawmidi *rmidi);
137 
138 	struct mutex open_mutex;
139 	wait_queue_head_t open_wait;
140 
141 	struct device *dev;
142 
143 	struct snd_info_entry *proc_entry;
144 
145 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
146 	struct snd_seq_device *seq_dev;
147 #endif
148 };
149 
150 /* main rawmidi functions */
151 
152 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
153 		    int output_count, int input_count,
154 		    struct snd_rawmidi **rmidi);
155 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
156 			 const struct snd_rawmidi_ops *ops);
157 
158 /* internal */
159 int snd_rawmidi_init(struct snd_rawmidi *rmidi,
160 		     struct snd_card *card, char *id, int device,
161 		     int output_count, int input_count,
162 		     unsigned int info_flags);
163 int snd_rawmidi_free(struct snd_rawmidi *rmidi);
164 
165 /* callbacks */
166 
167 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
168 			const unsigned char *buffer, int count);
169 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream);
170 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
171 			      unsigned char *buffer, int count);
172 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count);
173 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
174 			 unsigned char *buffer, int count);
175 int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream);
176 
177 /* main midi functions */
178 
179 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info);
180 int snd_rawmidi_kernel_open_nested(struct snd_rawmidi *rmidi, int subdevice,
181 				   int mode, struct snd_rawmidi_file *rfile,
182 				   int depth);
183 int snd_rawmidi_kernel_release_nested(struct snd_rawmidi_file *rfile,
184 				      int depth);
185 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
186 			      struct snd_rawmidi_params *params);
187 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
188 			     struct snd_rawmidi_params *params);
189 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream);
190 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream);
191 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream);
192 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
193 			     unsigned char *buf, long count);
194 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
195 			      const unsigned char *buf, long count);
196 
197 /* non-nested version */
198 static inline int snd_rawmidi_kernel_open(struct snd_rawmidi *rmidi,
199 					  int subdevice,
200 					  int mode,
201 					  struct snd_rawmidi_file *rfile)
202 {
203 	return snd_rawmidi_kernel_open_nested(rmidi, subdevice, mode, rfile, 0);
204 }
205 
206 static inline int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
207 {
208 	return snd_rawmidi_kernel_release_nested(rfile, 0);
209 }
210 
211 /* set up the tied devices */
212 static inline void snd_rawmidi_tie_devices(struct snd_rawmidi *r1,
213 					   struct snd_rawmidi *r2)
214 {
215 	/* tied_device field keeps the device+1 (so that 0 being unknown) */
216 	r1->tied_device = r2->device + 1;
217 	r2->tied_device = r1->device + 1;
218 }
219 
220 #endif /* __SOUND_RAWMIDI_H */
221