Blame view

sound/core/rawmidi.c 47.2 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
  /*
   *  Abstract layer for MIDI v1.0 stream
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
3
   *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   *
   *
   *   This program is free software; you can redistribute it and/or modify
   *   it under the terms of the GNU General Public License as published by
   *   the Free Software Foundation; either version 2 of the License, or
   *   (at your option) any later version.
   *
   *   This program is distributed in the hope that it will be useful,
   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   *   GNU General Public License for more details.
   *
   *   You should have received a copy of the GNU General Public License
   *   along with this program; if not, write to the Free Software
   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
  #include <sound/core.h>
  #include <linux/major.h>
  #include <linux/init.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
26
27
  #include <linux/sched.h>
  #include <linux/slab.h>
  #include <linux/time.h>
  #include <linux/wait.h>
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
28
  #include <linux/mutex.h>
65a772172   Paul Gortmaker   sound: fix driver...
29
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
30
  #include <linux/delay.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
34
35
  #include <sound/rawmidi.h>
  #include <sound/info.h>
  #include <sound/control.h>
  #include <sound/minors.h>
  #include <sound/initval.h>
c1017a4cd   Jaroslav Kysela   [ALSA] Changed Ja...
36
  MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
38
39
40
  MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
  MODULE_LICENSE("GPL");
  
  #ifdef CONFIG_SND_OSSEMUL
6581f4e74   Takashi Iwai   [ALSA] Remove zer...
41
  static int midi_map[SNDRV_CARDS];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
44
45
46
47
  static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
  module_param_array(midi_map, int, NULL, 0444);
  MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
  module_param_array(amidi_map, int, NULL, 0444);
  MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
  #endif /* CONFIG_SND_OSSEMUL */
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
48
49
50
51
  static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
  static int snd_rawmidi_dev_free(struct snd_device *device);
  static int snd_rawmidi_dev_register(struct snd_device *device);
  static int snd_rawmidi_dev_disconnect(struct snd_device *device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52

f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
53
  static LIST_HEAD(snd_rawmidi_devices);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
54
  static DEFINE_MUTEX(register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55

f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
56
57
  static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
  {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
58
  	struct snd_rawmidi *rawmidi;
9244b2c30   Johannes Berg   [ALSA] alsa core:...
59
  	list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
60
61
  		if (rawmidi->card == card && rawmidi->device == device)
  			return rawmidi;
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
62
63
  	return NULL;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
67
68
69
70
71
72
73
74
  static inline unsigned short snd_rawmidi_file_flags(struct file *file)
  {
  	switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
  	case FMODE_WRITE:
  		return SNDRV_RAWMIDI_LFLG_OUTPUT;
  	case FMODE_READ:
  		return SNDRV_RAWMIDI_LFLG_INPUT;
  	default:
  		return SNDRV_RAWMIDI_LFLG_OPEN;
  	}
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
75
  static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
76
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
77
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
  	return runtime->avail >= runtime->avail_min;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
80
81
  static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
  					   size_t count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
83
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
86
  	return runtime->avail >= runtime->avail_min &&
  	       (!substream->append || runtime->avail >= count);
  }
b3c705aa9   Takashi Iwai   ALSA: rawmidi - U...
87
  static void snd_rawmidi_input_event_work(struct work_struct *work)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
88
  {
b3c705aa9   Takashi Iwai   ALSA: rawmidi - U...
89
90
91
92
  	struct snd_rawmidi_runtime *runtime =
  		container_of(work, struct snd_rawmidi_runtime, event_work);
  	if (runtime->event)
  		runtime->event(runtime->substream);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
94
  static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
96
  	struct snd_rawmidi_runtime *runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97

ca2c09665   Takashi Iwai   [ALSA] Replace wi...
98
  	if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
99
  		return -ENOMEM;
b3c705aa9   Takashi Iwai   ALSA: rawmidi - U...
100
  	runtime->substream = substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
102
  	spin_lock_init(&runtime->lock);
  	init_waitqueue_head(&runtime->sleep);
b3c705aa9   Takashi Iwai   ALSA: rawmidi - U...
103
  	INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  	runtime->event = NULL;
  	runtime->buffer_size = PAGE_SIZE;
  	runtime->avail_min = 1;
  	if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
  		runtime->avail = 0;
  	else
  		runtime->avail = runtime->buffer_size;
  	if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
  		kfree(runtime);
  		return -ENOMEM;
  	}
  	runtime->appl_ptr = runtime->hw_ptr = 0;
  	substream->runtime = runtime;
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
119
  static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
121
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
123
124
125
126
127
  
  	kfree(runtime->buffer);
  	kfree(runtime);
  	substream->runtime = NULL;
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
128
  static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
  {
219df32fa   Takashi Iwai   ALSA: rawmidi - A...
130
131
  	if (!substream->opened)
  		return;
b3c705aa9   Takashi Iwai   ALSA: rawmidi - U...
132
  	substream->ops->trigger(substream, up);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
134
  static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
135
  {
219df32fa   Takashi Iwai   ALSA: rawmidi - A...
136
137
  	if (!substream->opened)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
  	substream->ops->trigger(substream, up);
b3c705aa9   Takashi Iwai   ALSA: rawmidi - U...
139
140
  	if (!up)
  		cancel_work_sync(&substream->runtime->event_work);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
142
  int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143
144
  {
  	unsigned long flags;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
145
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
147
148
149
150
151
152
153
154
  
  	snd_rawmidi_output_trigger(substream, 0);
  	runtime->drain = 0;
  	spin_lock_irqsave(&runtime->lock, flags);
  	runtime->appl_ptr = runtime->hw_ptr = 0;
  	runtime->avail = runtime->buffer_size;
  	spin_unlock_irqrestore(&runtime->lock, flags);
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
155
  int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
  {
  	int err;
  	long timeout;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
159
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
  
  	err = 0;
  	runtime->drain = 1;
  	timeout = wait_event_interruptible_timeout(runtime->sleep,
  				(runtime->avail >= runtime->buffer_size),
  				10*HZ);
  	if (signal_pending(current))
  		err = -ERESTARTSYS;
  	if (runtime->avail < runtime->buffer_size && !timeout) {
  		snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)
  ", (long)runtime->avail, (long)runtime->buffer_size);
  		err = -EIO;
  	}
  	runtime->drain = 0;
  	if (err != -ERESTARTSYS) {
  		/* we need wait a while to make sure that Tx FIFOs are empty */
  		if (substream->ops->drain)
  			substream->ops->drain(substream);
  		else
  			msleep(50);
  		snd_rawmidi_drop_output(substream);
  	}
  	return err;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
184
  int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
185
186
  {
  	unsigned long flags;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
187
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
189
190
191
192
193
194
195
196
  
  	snd_rawmidi_input_trigger(substream, 0);
  	runtime->drain = 0;
  	spin_lock_irqsave(&runtime->lock, flags);
  	runtime->appl_ptr = runtime->hw_ptr = 0;
  	runtime->avail = 0;
  	spin_unlock_irqrestore(&runtime->lock, flags);
  	return 0;
  }
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
197
198
199
200
201
202
  /* look for an available substream for the given stream direction;
   * if a specific subdevice is given, try to assign it
   */
  static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
  			    int stream, int mode,
  			    struct snd_rawmidi_substream **sub_ret)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
  {
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
204
205
206
207
208
209
  	struct snd_rawmidi_substream *substream;
  	struct snd_rawmidi_str *s = &rmidi->streams[stream];
  	static unsigned int info_flags[2] = {
  		[SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
  		[SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
  	};
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
210

9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
211
  	if (!(rmidi->info_flags & info_flags[stream]))
f9d202833   Takashi Iwai   ALSA: rawmidi - F...
212
  		return -ENXIO;
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
213
214
  	if (subdevice >= 0 && subdevice >= s->substream_count)
  		return -ENODEV;
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
215
216
217
218
  
  	list_for_each_entry(substream, &s->substreams, list) {
  		if (substream->opened) {
  			if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
16fb10964   Clemens Ladisch   sound: rawmidi: f...
219
220
  			    !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
  			    !substream->append)
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
221
222
223
224
225
226
  				continue;
  		}
  		if (subdevice < 0 || subdevice == substream->number) {
  			*sub_ret = substream;
  			return 0;
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
  	}
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
228
229
  	return -EAGAIN;
  }
f9d202833   Takashi Iwai   ALSA: rawmidi - F...
230

9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
231
232
233
234
235
236
  /* open and do ref-counting for the given substream */
  static int open_substream(struct snd_rawmidi *rmidi,
  			  struct snd_rawmidi_substream *substream,
  			  int mode)
  {
  	int err;
8579d2d77   Clemens Ladisch   sound: rawmidi: f...
237
238
239
240
241
  	if (substream->use_count == 0) {
  		err = snd_rawmidi_runtime_create(substream);
  		if (err < 0)
  			return err;
  		err = substream->ops->open(substream);
b7fe750fc   Clemens Ladisch   sound: rawmidi: f...
242
243
  		if (err < 0) {
  			snd_rawmidi_runtime_free(substream);
8579d2d77   Clemens Ladisch   sound: rawmidi: f...
244
  			return err;
b7fe750fc   Clemens Ladisch   sound: rawmidi: f...
245
  		}
8579d2d77   Clemens Ladisch   sound: rawmidi: f...
246
  		substream->opened = 1;
2d4b84201   Clemens Ladisch   sound: rawmidi: d...
247
  		substream->active_sensing = 0;
8579d2d77   Clemens Ladisch   sound: rawmidi: f...
248
249
  		if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
  			substream->append = 1;
7584af10c   Clemens Ladisch   sound: rawmidi: r...
250
  		substream->pid = get_pid(task_pid(current));
91d12c485   Clemens Ladisch   sound: rawmidi: f...
251
  		rmidi->streams[substream->stream].substream_opened++;
8579d2d77   Clemens Ladisch   sound: rawmidi: f...
252
253
  	}
  	substream->use_count++;
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
254
255
256
257
258
259
260
261
262
263
264
265
266
267
  	return 0;
  }
  
  static void close_substream(struct snd_rawmidi *rmidi,
  			    struct snd_rawmidi_substream *substream,
  			    int cleanup);
  
  static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
  			     struct snd_rawmidi_file *rfile)
  {
  	struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
  	int err;
  
  	rfile->input = rfile->output = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
268
  	if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
269
270
271
272
  		err = assign_substream(rmidi, subdevice,
  				       SNDRV_RAWMIDI_STREAM_INPUT,
  				       mode, &sinput);
  		if (err < 0)
b7fe750fc   Clemens Ladisch   sound: rawmidi: f...
273
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
274
275
  	}
  	if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
276
277
278
279
  		err = assign_substream(rmidi, subdevice,
  				       SNDRV_RAWMIDI_STREAM_OUTPUT,
  				       mode, &soutput);
  		if (err < 0)
b7fe750fc   Clemens Ladisch   sound: rawmidi: f...
280
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
281
  	}
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
282
283
284
285
  
  	if (sinput) {
  		err = open_substream(rmidi, sinput, mode);
  		if (err < 0)
b7fe750fc   Clemens Ladisch   sound: rawmidi: f...
286
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
  	}
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
288
289
290
291
292
  	if (soutput) {
  		err = open_substream(rmidi, soutput, mode);
  		if (err < 0) {
  			if (sinput)
  				close_substream(rmidi, sinput, 0);
b7fe750fc   Clemens Ladisch   sound: rawmidi: f...
293
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
294
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
295
  	}
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
296
297
298
299
  
  	rfile->rmidi = rmidi;
  	rfile->input = sinput;
  	rfile->output = soutput;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
300
  	return 0;
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
  }
  
  /* called from sound/core/seq/seq_midi.c */
  int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
  			    int mode, struct snd_rawmidi_file * rfile)
  {
  	struct snd_rawmidi *rmidi;
  	int err;
  
  	if (snd_BUG_ON(!rfile))
  		return -EINVAL;
  
  	mutex_lock(&register_mutex);
  	rmidi = snd_rawmidi_search(card, device);
  	if (rmidi == NULL) {
  		mutex_unlock(&register_mutex);
  		return -ENODEV;
  	}
  	if (!try_module_get(rmidi->card->module)) {
  		mutex_unlock(&register_mutex);
  		return -ENXIO;
  	}
  	mutex_unlock(&register_mutex);
  
  	mutex_lock(&rmidi->open_mutex);
  	err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
  	mutex_unlock(&rmidi->open_mutex);
  	if (err < 0)
  		module_put(rmidi->card->module);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
330
331
332
333
334
335
  	return err;
  }
  
  static int snd_rawmidi_open(struct inode *inode, struct file *file)
  {
  	int maj = imajor(inode);
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
336
  	struct snd_card *card;
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
337
  	int subdevice;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
339
  	unsigned short fflags;
  	int err;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
340
  	struct snd_rawmidi *rmidi;
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
341
  	struct snd_rawmidi_file *rawmidi_file = NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
342
  	wait_queue_t wait;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
343
  	struct snd_ctl_file *kctl;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
344

9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
345
346
  	if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK)) 
  		return -EINVAL;		/* invalid combination */
02f4865fa   Takashi Iwai   ALSA: core - Defi...
347
348
349
  	err = nonseekable_open(inode, file);
  	if (err < 0)
  		return err;
f19028601   Clemens Ladisch   [ALSA] fix improp...
350
  	if (maj == snd_major) {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
351
352
  		rmidi = snd_lookup_minor_data(iminor(inode),
  					      SNDRV_DEVICE_TYPE_RAWMIDI);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
  #ifdef CONFIG_SND_OSSEMUL
f19028601   Clemens Ladisch   [ALSA] fix improp...
354
  	} else if (maj == SOUND_MAJOR) {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
355
356
  		rmidi = snd_lookup_oss_minor_data(iminor(inode),
  						  SNDRV_OSS_DEVICE_TYPE_MIDI);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
357
  #endif
f19028601   Clemens Ladisch   [ALSA] fix improp...
358
  	} else
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
  		return -ENXIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
360

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
361
362
  	if (rmidi == NULL)
  		return -ENODEV;
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
363
364
365
366
367
  
  	if (!try_module_get(rmidi->card->module))
  		return -ENXIO;
  
  	mutex_lock(&rmidi->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
368
369
370
  	card = rmidi->card;
  	err = snd_card_file_add(card, file);
  	if (err < 0)
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
371
  		goto __error_card;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
372
  	fflags = snd_rawmidi_file_flags(file);
f19028601   Clemens Ladisch   [ALSA] fix improp...
373
  	if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
374
  		fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
375
376
  	rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
  	if (rawmidi_file == NULL) {
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
377
378
  		err = -ENOMEM;
  		goto __error;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
379
380
381
  	}
  	init_waitqueue_entry(&wait, current);
  	add_wait_queue(&rmidi->open_wait, &wait);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
382
383
  	while (1) {
  		subdevice = -1;
399ccdc1c   Takashi Iwai   ALSA: fix locking...
384
  		read_lock(&card->ctl_files_rwlock);
9244b2c30   Johannes Berg   [ALSA] alsa core:...
385
  		list_for_each_entry(kctl, &card->ctl_files, list) {
25d27eded   Clemens Ladisch   control: use refe...
386
  			if (kctl->pid == task_pid(current)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
387
  				subdevice = kctl->prefer_rawmidi_subdevice;
2529bba76   Takashi Iwai   [ALSA] Fix substr...
388
389
  				if (subdevice != -1)
  					break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
390
391
  			}
  		}
399ccdc1c   Takashi Iwai   ALSA: fix locking...
392
  		read_unlock(&card->ctl_files_rwlock);
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
393
  		err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
394
395
396
397
398
399
400
401
402
403
  		if (err >= 0)
  			break;
  		if (err == -EAGAIN) {
  			if (file->f_flags & O_NONBLOCK) {
  				err = -EBUSY;
  				break;
  			}
  		} else
  			break;
  		set_current_state(TASK_INTERRUPTIBLE);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
404
  		mutex_unlock(&rmidi->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
405
  		schedule();
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
406
  		mutex_lock(&rmidi->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
408
409
410
411
  		if (signal_pending(current)) {
  			err = -ERESTARTSYS;
  			break;
  		}
  	}
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
412
413
414
415
416
  	remove_wait_queue(&rmidi->open_wait, &wait);
  	if (err < 0) {
  		kfree(rawmidi_file);
  		goto __error;
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
417
418
419
420
421
422
  #ifdef CONFIG_SND_OSSEMUL
  	if (rawmidi_file->input && rawmidi_file->input->runtime)
  		rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
  	if (rawmidi_file->output && rawmidi_file->output->runtime)
  		rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
  #endif
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
423
424
425
426
427
428
429
  	file->private_data = rawmidi_file;
  	mutex_unlock(&rmidi->open_mutex);
  	return 0;
  
   __error:
  	snd_card_file_remove(card, file);
   __error_card:
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
430
  	mutex_unlock(&rmidi->open_mutex);
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
431
  	module_put(rmidi->card->module);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
432
433
  	return err;
  }
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
434
435
436
  static void close_substream(struct snd_rawmidi *rmidi,
  			    struct snd_rawmidi_substream *substream,
  			    int cleanup)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
437
  {
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
438
439
  	if (--substream->use_count)
  		return;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
440

9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
441
442
443
444
  	if (cleanup) {
  		if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
  			snd_rawmidi_input_trigger(substream, 0);
  		else {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
445
446
  			if (substream->active_sensing) {
  				unsigned char buf = 0xfe;
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
447
448
449
  				/* sending single active sensing message
  				 * to shut the device up
  				 */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
452
453
  				snd_rawmidi_kernel_write(substream, &buf, 1);
  			}
  			if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
  				snd_rawmidi_output_trigger(substream, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
454
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
455
  	}
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
456
457
458
459
460
461
  	substream->ops->close(substream);
  	if (substream->runtime->private_free)
  		substream->runtime->private_free(substream);
  	snd_rawmidi_runtime_free(substream);
  	substream->opened = 0;
  	substream->append = 0;
7584af10c   Clemens Ladisch   sound: rawmidi: r...
462
463
  	put_pid(substream->pid);
  	substream->pid = NULL;
91d12c485   Clemens Ladisch   sound: rawmidi: f...
464
  	rmidi->streams[substream->stream].substream_opened--;
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
  }
  
  static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
  {
  	struct snd_rawmidi *rmidi;
  
  	rmidi = rfile->rmidi;
  	mutex_lock(&rmidi->open_mutex);
  	if (rfile->input) {
  		close_substream(rmidi, rfile->input, 1);
  		rfile->input = NULL;
  	}
  	if (rfile->output) {
  		close_substream(rmidi, rfile->output, 1);
  		rfile->output = NULL;
  	}
  	rfile->rmidi = NULL;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
482
  	mutex_unlock(&rmidi->open_mutex);
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
483
484
485
486
487
488
489
490
491
492
493
494
495
  	wake_up(&rmidi->open_wait);
  }
  
  /* called from sound/core/seq/seq_midi.c */
  int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
  {
  	struct snd_rawmidi *rmidi;
  
  	if (snd_BUG_ON(!rfile))
  		return -ENXIO;
  	
  	rmidi = rfile->rmidi;
  	rawmidi_release_priv(rfile);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
496
497
498
499
500
501
  	module_put(rmidi->card->module);
  	return 0;
  }
  
  static int snd_rawmidi_release(struct inode *inode, struct file *file)
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
502
503
  	struct snd_rawmidi_file *rfile;
  	struct snd_rawmidi *rmidi;
aa73aec6c   Clemens Ladisch   ALSA: rawmidi: fi...
504
  	struct module *module;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
505
506
  
  	rfile = file->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
507
  	rmidi = rfile->rmidi;
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
508
  	rawmidi_release_priv(rfile);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
509
  	kfree(rfile);
aa73aec6c   Clemens Ladisch   ALSA: rawmidi: fi...
510
  	module = rmidi->card->module;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
511
  	snd_card_file_remove(rmidi->card, file);
aa73aec6c   Clemens Ladisch   ALSA: rawmidi: fi...
512
  	module_put(module);
9a1b64caa   Takashi Iwai   ALSA: rawmidi - R...
513
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514
  }
18612048b   Adrian Bunk   [ALSA] sound/: po...
515
516
  static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
  			    struct snd_rawmidi_info *info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
517
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
518
  	struct snd_rawmidi *rmidi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
  	
  	if (substream == NULL)
  		return -ENODEV;
  	rmidi = substream->rmidi;
  	memset(info, 0, sizeof(*info));
  	info->card = rmidi->card->number;
  	info->device = rmidi->device;
  	info->subdevice = substream->number;
  	info->stream = substream->stream;
  	info->flags = rmidi->info_flags;
  	strcpy(info->id, rmidi->id);
  	strcpy(info->name, rmidi->name);
  	strcpy(info->subname, substream->name);
  	info->subdevices_count = substream->pstr->substream_count;
  	info->subdevices_avail = (substream->pstr->substream_count -
  				  substream->pstr->substream_opened);
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
537
538
  static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
  				 struct snd_rawmidi_info __user * _info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
540
  	struct snd_rawmidi_info info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
542
543
  	int err;
  	if ((err = snd_rawmidi_info(substream, &info)) < 0)
  		return err;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
544
  	if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
545
546
547
  		return -EFAULT;
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
548
  int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
549
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
550
551
552
  	struct snd_rawmidi *rmidi;
  	struct snd_rawmidi_str *pstr;
  	struct snd_rawmidi_substream *substream;
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
553

1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
554
  	mutex_lock(&register_mutex);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
555
  	rmidi = snd_rawmidi_search(card, info->device);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
556
  	mutex_unlock(&register_mutex);
a106cd3d9   Clemens Ladisch   [ALSA] rawmidi: p...
557
558
  	if (!rmidi)
  		return -ENXIO;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
560
561
562
563
564
565
  	if (info->stream < 0 || info->stream > 1)
  		return -EINVAL;
  	pstr = &rmidi->streams[info->stream];
  	if (pstr->substream_count == 0)
  		return -ENOENT;
  	if (info->subdevice >= pstr->substream_count)
  		return -ENXIO;
9244b2c30   Johannes Berg   [ALSA] alsa core:...
566
  	list_for_each_entry(substream, &pstr->substreams, list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
567
568
569
570
571
  		if ((unsigned int)substream->number == info->subdevice)
  			return snd_rawmidi_info(substream, info);
  	}
  	return -ENXIO;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
572
573
  static int snd_rawmidi_info_select_user(struct snd_card *card,
  					struct snd_rawmidi_info __user *_info)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
574
575
  {
  	int err;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
576
  	struct snd_rawmidi_info info;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
578
579
580
581
582
583
584
  	if (get_user(info.device, &_info->device))
  		return -EFAULT;
  	if (get_user(info.stream, &_info->stream))
  		return -EFAULT;
  	if (get_user(info.subdevice, &_info->subdevice))
  		return -EFAULT;
  	if ((err = snd_rawmidi_info_select(card, &info)) < 0)
  		return err;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
585
  	if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
586
587
588
  		return -EFAULT;
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
589
590
  int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
  			      struct snd_rawmidi_params * params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
591
592
  {
  	char *newbuf;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
593
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
594
595
596
597
598
599
600
601
602
603
604
  	
  	if (substream->append && substream->use_count > 1)
  		return -EBUSY;
  	snd_rawmidi_drain_output(substream);
  	if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  		return -EINVAL;
  	}
  	if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  		return -EINVAL;
  	}
  	if (params->buffer_size != runtime->buffer_size) {
3101ba035   Takashi Iwai   ALSA: Use kreallo...
605
606
  		newbuf = krealloc(runtime->buffer, params->buffer_size,
  				  GFP_KERNEL);
4fa95ef63   Jesper Juhl   [PATCH] sound: Re...
607
  		if (!newbuf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
608
  			return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
609
610
  		runtime->buffer = newbuf;
  		runtime->buffer_size = params->buffer_size;
1b98ea479   Clemens Ladisch   [ALSA] rawmidi: a...
611
  		runtime->avail = runtime->buffer_size;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
613
614
615
616
  	}
  	runtime->avail_min = params->avail_min;
  	substream->active_sensing = !params->no_active_sensing;
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
617
618
  int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
  			     struct snd_rawmidi_params * params)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
619
620
  {
  	char *newbuf;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
621
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
622
623
624
625
626
627
628
629
630
  
  	snd_rawmidi_drain_input(substream);
  	if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
  		return -EINVAL;
  	}
  	if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
  		return -EINVAL;
  	}
  	if (params->buffer_size != runtime->buffer_size) {
3101ba035   Takashi Iwai   ALSA: Use kreallo...
631
632
  		newbuf = krealloc(runtime->buffer, params->buffer_size,
  				  GFP_KERNEL);
4fa95ef63   Jesper Juhl   [PATCH] sound: Re...
633
  		if (!newbuf)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
634
  			return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
635
636
637
638
639
640
  		runtime->buffer = newbuf;
  		runtime->buffer_size = params->buffer_size;
  	}
  	runtime->avail_min = params->avail_min;
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
641
642
  static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
  				     struct snd_rawmidi_status * status)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
643
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
644
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
645
646
647
648
649
650
651
652
  
  	memset(status, 0, sizeof(*status));
  	status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
  	spin_lock_irq(&runtime->lock);
  	status->avail = runtime->avail;
  	spin_unlock_irq(&runtime->lock);
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
653
654
  static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
  				    struct snd_rawmidi_status * status)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
655
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
656
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
657
658
659
660
661
662
663
664
665
666
667
668
669
  
  	memset(status, 0, sizeof(*status));
  	status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
  	spin_lock_irq(&runtime->lock);
  	status->avail = runtime->avail;
  	status->xruns = runtime->xruns;
  	runtime->xruns = 0;
  	spin_unlock_irq(&runtime->lock);
  	return 0;
  }
  
  static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
670
  	struct snd_rawmidi_file *rfile;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
672
673
674
675
676
677
678
679
680
  	void __user *argp = (void __user *)arg;
  
  	rfile = file->private_data;
  	if (((cmd >> 8) & 0xff) != 'W')
  		return -ENOTTY;
  	switch (cmd) {
  	case SNDRV_RAWMIDI_IOCTL_PVERSION:
  		return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
  	case SNDRV_RAWMIDI_IOCTL_INFO:
  	{
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
681
682
  		int stream;
  		struct snd_rawmidi_info __user *info = argp;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683
684
685
686
687
688
689
690
691
692
693
694
695
  		if (get_user(stream, &info->stream))
  			return -EFAULT;
  		switch (stream) {
  		case SNDRV_RAWMIDI_STREAM_INPUT:
  			return snd_rawmidi_info_user(rfile->input, info);
  		case SNDRV_RAWMIDI_STREAM_OUTPUT:
  			return snd_rawmidi_info_user(rfile->output, info);
  		default:
  			return -EINVAL;
  		}
  	}
  	case SNDRV_RAWMIDI_IOCTL_PARAMS:
  	{
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
696
697
  		struct snd_rawmidi_params params;
  		if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
  			return -EFAULT;
  		switch (params.stream) {
  		case SNDRV_RAWMIDI_STREAM_OUTPUT:
  			if (rfile->output == NULL)
  				return -EINVAL;
  			return snd_rawmidi_output_params(rfile->output, &params);
  		case SNDRV_RAWMIDI_STREAM_INPUT:
  			if (rfile->input == NULL)
  				return -EINVAL;
  			return snd_rawmidi_input_params(rfile->input, &params);
  		default:
  			return -EINVAL;
  		}
  	}
  	case SNDRV_RAWMIDI_IOCTL_STATUS:
  	{
  		int err = 0;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
715
716
  		struct snd_rawmidi_status status;
  		if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
  			return -EFAULT;
  		switch (status.stream) {
  		case SNDRV_RAWMIDI_STREAM_OUTPUT:
  			if (rfile->output == NULL)
  				return -EINVAL;
  			err = snd_rawmidi_output_status(rfile->output, &status);
  			break;
  		case SNDRV_RAWMIDI_STREAM_INPUT:
  			if (rfile->input == NULL)
  				return -EINVAL;
  			err = snd_rawmidi_input_status(rfile->input, &status);
  			break;
  		default:
  			return -EINVAL;
  		}
  		if (err < 0)
  			return err;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
734
  		if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
  			return -EFAULT;
  		return 0;
  	}
  	case SNDRV_RAWMIDI_IOCTL_DROP:
  	{
  		int val;
  		if (get_user(val, (int __user *) argp))
  			return -EFAULT;
  		switch (val) {
  		case SNDRV_RAWMIDI_STREAM_OUTPUT:
  			if (rfile->output == NULL)
  				return -EINVAL;
  			return snd_rawmidi_drop_output(rfile->output);
  		default:
  			return -EINVAL;
  		}
  	}
  	case SNDRV_RAWMIDI_IOCTL_DRAIN:
  	{
  		int val;
  		if (get_user(val, (int __user *) argp))
  			return -EFAULT;
  		switch (val) {
  		case SNDRV_RAWMIDI_STREAM_OUTPUT:
  			if (rfile->output == NULL)
  				return -EINVAL;
  			return snd_rawmidi_drain_output(rfile->output);
  		case SNDRV_RAWMIDI_STREAM_INPUT:
  			if (rfile->input == NULL)
  				return -EINVAL;
  			return snd_rawmidi_drain_input(rfile->input);
  		default:
  			return -EINVAL;
  		}
  	}
  #ifdef CONFIG_SND_DEBUG
  	default:
  		snd_printk(KERN_WARNING "rawmidi: unknown command = 0x%x
  ", cmd);
  #endif
  	}
  	return -ENOTTY;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
778
779
  static int snd_rawmidi_control_ioctl(struct snd_card *card,
  				     struct snd_ctl_file *control,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
780
781
782
783
  				     unsigned int cmd,
  				     unsigned long arg)
  {
  	void __user *argp = (void __user *)arg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
784

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
785
786
787
788
789
790
791
  	switch (cmd) {
  	case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
  	{
  		int device;
  		
  		if (get_user(device, (int __user *)argp))
  			return -EFAULT;
a7a13d067   Dan Carpenter   ALSA: rawmidi: fi...
792
793
  		if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
  			device = SNDRV_RAWMIDI_DEVICES - 1;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
794
  		mutex_lock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
795
796
  		device = device < 0 ? 0 : device + 1;
  		while (device < SNDRV_RAWMIDI_DEVICES) {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
797
  			if (snd_rawmidi_search(card, device))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
798
799
800
801
802
  				break;
  			device++;
  		}
  		if (device == SNDRV_RAWMIDI_DEVICES)
  			device = -1;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
803
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
  		if (put_user(device, (int __user *)argp))
  			return -EFAULT;
  		return 0;
  	}
  	case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
  	{
  		int val;
  		
  		if (get_user(val, (int __user *)argp))
  			return -EFAULT;
  		control->prefer_rawmidi_subdevice = val;
  		return 0;
  	}
  	case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
  		return snd_rawmidi_info_select_user(card, argp);
  	}
  	return -ENOIOCTLCMD;
  }
  
  /**
   * snd_rawmidi_receive - receive the input data from the device
   * @substream: the rawmidi substream
   * @buffer: the buffer pointer
   * @count: the data size to read
   *
   * Reads the data from the internal buffer.
   *
   * Returns the size of read data, or a negative error code on failure.
   */
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
833
834
  int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
  			const unsigned char *buffer, int count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
835
836
837
  {
  	unsigned long flags;
  	int result = 0, count1;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
838
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
839

219df32fa   Takashi Iwai   ALSA: rawmidi - A...
840
841
  	if (!substream->opened)
  		return -EBADFD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
  	if (runtime->buffer == NULL) {
  		snd_printd("snd_rawmidi_receive: input is not active!!!
  ");
  		return -EINVAL;
  	}
  	spin_lock_irqsave(&runtime->lock, flags);
  	if (count == 1) {	/* special case, faster code */
  		substream->bytes++;
  		if (runtime->avail < runtime->buffer_size) {
  			runtime->buffer[runtime->hw_ptr++] = buffer[0];
  			runtime->hw_ptr %= runtime->buffer_size;
  			runtime->avail++;
  			result++;
  		} else {
  			runtime->xruns++;
  		}
  	} else {
  		substream->bytes += count;
  		count1 = runtime->buffer_size - runtime->hw_ptr;
  		if (count1 > count)
  			count1 = count;
  		if (count1 > (int)(runtime->buffer_size - runtime->avail))
  			count1 = runtime->buffer_size - runtime->avail;
  		memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
  		runtime->hw_ptr += count1;
  		runtime->hw_ptr %= runtime->buffer_size;
  		runtime->avail += count1;
  		count -= count1;
  		result += count1;
  		if (count > 0) {
  			buffer += count1;
  			count1 = count;
  			if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
  				count1 = runtime->buffer_size - runtime->avail;
  				runtime->xruns += count - count1;
  			}
  			if (count1 > 0) {
  				memcpy(runtime->buffer, buffer, count1);
  				runtime->hw_ptr = count1;
  				runtime->avail += count1;
  				result += count1;
  			}
  		}
  	}
  	if (result > 0) {
  		if (runtime->event)
b3c705aa9   Takashi Iwai   ALSA: rawmidi - U...
888
  			schedule_work(&runtime->event_work);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
889
890
891
892
893
894
  		else if (snd_rawmidi_ready(substream))
  			wake_up(&runtime->sleep);
  	}
  	spin_unlock_irqrestore(&runtime->lock, flags);
  	return result;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
895
  static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
896
897
  				     unsigned char __user *userbuf,
  				     unsigned char *kernelbuf, long count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
898
899
900
  {
  	unsigned long flags;
  	long result = 0, count1;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
901
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
902
903
904
905
906
907
908
909
  
  	while (count > 0 && runtime->avail) {
  		count1 = runtime->buffer_size - runtime->appl_ptr;
  		if (count1 > count)
  			count1 = count;
  		spin_lock_irqsave(&runtime->lock, flags);
  		if (count1 > (int)runtime->avail)
  			count1 = runtime->avail;
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
910
911
912
  		if (kernelbuf)
  			memcpy(kernelbuf + result, runtime->buffer + runtime->appl_ptr, count1);
  		if (userbuf) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
913
  			spin_unlock_irqrestore(&runtime->lock, flags);
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
914
  			if (copy_to_user(userbuf + result,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
915
916
917
918
919
920
921
922
923
924
925
926
927
928
  					 runtime->buffer + runtime->appl_ptr, count1)) {
  				return result > 0 ? result : -EFAULT;
  			}
  			spin_lock_irqsave(&runtime->lock, flags);
  		}
  		runtime->appl_ptr += count1;
  		runtime->appl_ptr %= runtime->buffer_size;
  		runtime->avail -= count1;
  		spin_unlock_irqrestore(&runtime->lock, flags);
  		result += count1;
  		count -= count1;
  	}
  	return result;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
929
930
  long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
  			     unsigned char *buf, long count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931
932
  {
  	snd_rawmidi_input_trigger(substream, 1);
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
933
  	return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
934
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
935
936
  static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
  				loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
937
938
939
  {
  	long result;
  	int count1;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
940
941
942
  	struct snd_rawmidi_file *rfile;
  	struct snd_rawmidi_substream *substream;
  	struct snd_rawmidi_runtime *runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
  
  	rfile = file->private_data;
  	substream = rfile->input;
  	if (substream == NULL)
  		return -EIO;
  	runtime = substream->runtime;
  	snd_rawmidi_input_trigger(substream, 1);
  	result = 0;
  	while (count > 0) {
  		spin_lock_irq(&runtime->lock);
  		while (!snd_rawmidi_ready(substream)) {
  			wait_queue_t wait;
  			if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
  				spin_unlock_irq(&runtime->lock);
  				return result > 0 ? result : -EAGAIN;
  			}
  			init_waitqueue_entry(&wait, current);
  			add_wait_queue(&runtime->sleep, &wait);
  			set_current_state(TASK_INTERRUPTIBLE);
  			spin_unlock_irq(&runtime->lock);
  			schedule();
  			remove_wait_queue(&runtime->sleep, &wait);
  			if (signal_pending(current))
  				return result > 0 ? result : -ERESTARTSYS;
  			if (!runtime->avail)
  				return result > 0 ? result : -EIO;
  			spin_lock_irq(&runtime->lock);
  		}
  		spin_unlock_irq(&runtime->lock);
4d23359b7   Clemens Ladisch   [ALSA] sparse add...
972
  		count1 = snd_rawmidi_kernel_read1(substream,
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
973
974
975
  						  (unsigned char __user *)buf,
  						  NULL/*kernelbuf*/,
  						  count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
  		if (count1 < 0)
  			return result > 0 ? result : count1;
  		result += count1;
  		buf += count1;
  		count -= count1;
  	}
  	return result;
  }
  
  /**
   * snd_rawmidi_transmit_empty - check whether the output buffer is empty
   * @substream: the rawmidi substream
   * 
   * Returns 1 if the internal output buffer is empty, 0 if not.
   */
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
991
  int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
992
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
993
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
  	int result;
  	unsigned long flags;
  
  	if (runtime->buffer == NULL) {
  		snd_printd("snd_rawmidi_transmit_empty: output is not active!!!
  ");
  		return 1;
  	}
  	spin_lock_irqsave(&runtime->lock, flags);
  	result = runtime->avail >= runtime->buffer_size;
  	spin_unlock_irqrestore(&runtime->lock, flags);
  	return result;		
  }
  
  /**
   * snd_rawmidi_transmit_peek - copy data from the internal buffer
   * @substream: the rawmidi substream
   * @buffer: the buffer pointer
   * @count: data size to transfer
   *
   * Copies data from the internal output buffer to the given buffer.
   *
   * Call this in the interrupt handler when the midi output is ready,
   * and call snd_rawmidi_transmit_ack() after the transmission is
   * finished.
   *
   * Returns the size of copied data, or a negative error code on failure.
   */
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1022
1023
  int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
  			      unsigned char *buffer, int count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1024
1025
1026
  {
  	unsigned long flags;
  	int result, count1;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1027
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
  
  	if (runtime->buffer == NULL) {
  		snd_printd("snd_rawmidi_transmit_peek: output is not active!!!
  ");
  		return -EINVAL;
  	}
  	result = 0;
  	spin_lock_irqsave(&runtime->lock, flags);
  	if (runtime->avail >= runtime->buffer_size) {
  		/* warning: lowlevel layer MUST trigger down the hardware */
  		goto __skip;
  	}
  	if (count == 1) {	/* special case, faster code */
  		*buffer = runtime->buffer[runtime->hw_ptr];
  		result++;
  	} else {
  		count1 = runtime->buffer_size - runtime->hw_ptr;
  		if (count1 > count)
  			count1 = count;
  		if (count1 > (int)(runtime->buffer_size - runtime->avail))
  			count1 = runtime->buffer_size - runtime->avail;
  		memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
  		count -= count1;
  		result += count1;
  		if (count > 0) {
  			if (count > (int)(runtime->buffer_size - runtime->avail - count1))
  				count = runtime->buffer_size - runtime->avail - count1;
  			memcpy(buffer + count1, runtime->buffer, count);
  			result += count;
  		}
  	}
        __skip:
  	spin_unlock_irqrestore(&runtime->lock, flags);
  	return result;
  }
  
  /**
   * snd_rawmidi_transmit_ack - acknowledge the transmission
   * @substream: the rawmidi substream
   * @count: the tranferred count
   *
   * Advances the hardware pointer for the internal output buffer with
   * the given size and updates the condition.
   * Call after the transmission is finished.
   *
   * Returns the advanced size if successful, or a negative error code on failure.
   */
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1075
  int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1076
1077
  {
  	unsigned long flags;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1078
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1079
1080
1081
1082
1083
1084
1085
  
  	if (runtime->buffer == NULL) {
  		snd_printd("snd_rawmidi_transmit_ack: output is not active!!!
  ");
  		return -EINVAL;
  	}
  	spin_lock_irqsave(&runtime->lock, flags);
7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
1086
  	snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
  	runtime->hw_ptr += count;
  	runtime->hw_ptr %= runtime->buffer_size;
  	runtime->avail += count;
  	substream->bytes += count;
  	if (count > 0) {
  		if (runtime->drain || snd_rawmidi_ready(substream))
  			wake_up(&runtime->sleep);
  	}
  	spin_unlock_irqrestore(&runtime->lock, flags);
  	return count;
  }
  
  /**
   * snd_rawmidi_transmit - copy from the buffer to the device
   * @substream: the rawmidi substream
df8db936e   Takashi Iwai   [ALSA] Fix DocBoo...
1102
   * @buffer: the buffer pointer
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1103
1104
1105
1106
1107
1108
   * @count: the data size to transfer
   * 
   * Copies data from the buffer to the device and advances the pointer.
   *
   * Returns the copied size if successful, or a negative error code on failure.
   */
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1109
1110
  int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
  			 unsigned char *buffer, int count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1111
  {
219df32fa   Takashi Iwai   ALSA: rawmidi - A...
1112
1113
  	if (!substream->opened)
  		return -EBADFD;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1114
1115
1116
1117
1118
  	count = snd_rawmidi_transmit_peek(substream, buffer, count);
  	if (count < 0)
  		return count;
  	return snd_rawmidi_transmit_ack(substream, count);
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1119
  static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
1120
1121
1122
  				      const unsigned char __user *userbuf,
  				      const unsigned char *kernelbuf,
  				      long count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1123
1124
1125
  {
  	unsigned long flags;
  	long count1, result;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1126
  	struct snd_rawmidi_runtime *runtime = substream->runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1127

7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
1128
1129
1130
1131
  	if (snd_BUG_ON(!kernelbuf && !userbuf))
  		return -EINVAL;
  	if (snd_BUG_ON(!runtime->buffer))
  		return -EINVAL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
  
  	result = 0;
  	spin_lock_irqsave(&runtime->lock, flags);
  	if (substream->append) {
  		if ((long)runtime->avail < count) {
  			spin_unlock_irqrestore(&runtime->lock, flags);
  			return -EAGAIN;
  		}
  	}
  	while (count > 0 && runtime->avail > 0) {
  		count1 = runtime->buffer_size - runtime->appl_ptr;
  		if (count1 > count)
  			count1 = count;
  		if (count1 > (long)runtime->avail)
  			count1 = runtime->avail;
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
1147
1148
1149
1150
  		if (kernelbuf)
  			memcpy(runtime->buffer + runtime->appl_ptr,
  			       kernelbuf + result, count1);
  		else if (userbuf) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1151
1152
  			spin_unlock_irqrestore(&runtime->lock, flags);
  			if (copy_from_user(runtime->buffer + runtime->appl_ptr,
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
1153
  					   userbuf + result, count1)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
  				spin_lock_irqsave(&runtime->lock, flags);
  				result = result > 0 ? result : -EFAULT;
  				goto __end;
  			}
  			spin_lock_irqsave(&runtime->lock, flags);
  		}
  		runtime->appl_ptr += count1;
  		runtime->appl_ptr %= runtime->buffer_size;
  		runtime->avail -= count1;
  		result += count1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1164
1165
1166
1167
1168
1169
1170
1171
1172
  		count -= count1;
  	}
        __end:
  	count1 = runtime->avail < runtime->buffer_size;
  	spin_unlock_irqrestore(&runtime->lock, flags);
  	if (count1)
  		snd_rawmidi_output_trigger(substream, 1);
  	return result;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1173
1174
  long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
  			      const unsigned char *buf, long count)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1175
  {
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
1176
  	return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1177
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1178
1179
  static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
  				 size_t count, loff_t *offset)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1180
1181
1182
  {
  	long result, timeout;
  	int count1;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1183
1184
1185
  	struct snd_rawmidi_file *rfile;
  	struct snd_rawmidi_runtime *runtime;
  	struct snd_rawmidi_substream *substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
  
  	rfile = file->private_data;
  	substream = rfile->output;
  	runtime = substream->runtime;
  	/* we cannot put an atomic message to our buffer */
  	if (substream->append && count > runtime->buffer_size)
  		return -EIO;
  	result = 0;
  	while (count > 0) {
  		spin_lock_irq(&runtime->lock);
  		while (!snd_rawmidi_ready_append(substream, count)) {
  			wait_queue_t wait;
  			if (file->f_flags & O_NONBLOCK) {
  				spin_unlock_irq(&runtime->lock);
  				return result > 0 ? result : -EAGAIN;
  			}
  			init_waitqueue_entry(&wait, current);
  			add_wait_queue(&runtime->sleep, &wait);
  			set_current_state(TASK_INTERRUPTIBLE);
  			spin_unlock_irq(&runtime->lock);
  			timeout = schedule_timeout(30 * HZ);
  			remove_wait_queue(&runtime->sleep, &wait);
  			if (signal_pending(current))
  				return result > 0 ? result : -ERESTARTSYS;
  			if (!runtime->avail && !timeout)
  				return result > 0 ? result : -EIO;
  			spin_lock_irq(&runtime->lock);
  		}
  		spin_unlock_irq(&runtime->lock);
17596a80d   Marcin Åšlusarz   [ALSA] rawmidi: l...
1215
  		count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1216
1217
1218
1219
1220
1221
1222
1223
  		if (count1 < 0)
  			return result > 0 ? result : count1;
  		result += count1;
  		buf += count1;
  		if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
  			break;
  		count -= count1;
  	}
6b2f3d1f7   Christoph Hellwig   vfs: Implement pr...
1224
  	if (file->f_flags & O_DSYNC) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
  		spin_lock_irq(&runtime->lock);
  		while (runtime->avail != runtime->buffer_size) {
  			wait_queue_t wait;
  			unsigned int last_avail = runtime->avail;
  			init_waitqueue_entry(&wait, current);
  			add_wait_queue(&runtime->sleep, &wait);
  			set_current_state(TASK_INTERRUPTIBLE);
  			spin_unlock_irq(&runtime->lock);
  			timeout = schedule_timeout(30 * HZ);
  			remove_wait_queue(&runtime->sleep, &wait);
  			if (signal_pending(current))
  				return result > 0 ? result : -ERESTARTSYS;
  			if (runtime->avail == last_avail && !timeout)
  				return result > 0 ? result : -EIO;
  			spin_lock_irq(&runtime->lock);
  		}
  		spin_unlock_irq(&runtime->lock);
  	}
  	return result;
  }
  
  static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1248
1249
  	struct snd_rawmidi_file *rfile;
  	struct snd_rawmidi_runtime *runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
  	unsigned int mask;
  
  	rfile = file->private_data;
  	if (rfile->input != NULL) {
  		runtime = rfile->input->runtime;
  		snd_rawmidi_input_trigger(rfile->input, 1);
  		poll_wait(file, &runtime->sleep, wait);
  	}
  	if (rfile->output != NULL) {
  		runtime = rfile->output->runtime;
  		poll_wait(file, &runtime->sleep, wait);
  	}
  	mask = 0;
  	if (rfile->input != NULL) {
  		if (snd_rawmidi_ready(rfile->input))
  			mask |= POLLIN | POLLRDNORM;
  	}
  	if (rfile->output != NULL) {
  		if (snd_rawmidi_ready(rfile->output))
  			mask |= POLLOUT | POLLWRNORM;
  	}
  	return mask;
  }
  
  /*
   */
  #ifdef CONFIG_COMPAT
  #include "rawmidi_compat.c"
  #else
  #define snd_rawmidi_ioctl_compat	NULL
  #endif
  
  /*
  
   */
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1285
1286
  static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
  				       struct snd_info_buffer *buffer)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1287
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1288
1289
1290
  	struct snd_rawmidi *rmidi;
  	struct snd_rawmidi_substream *substream;
  	struct snd_rawmidi_runtime *runtime;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1291
1292
1293
1294
1295
  
  	rmidi = entry->private_data;
  	snd_iprintf(buffer, "%s
  
  ", rmidi->name);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1296
  	mutex_lock(&rmidi->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1297
  	if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
9244b2c30   Johannes Berg   [ALSA] alsa core:...
1298
1299
1300
  		list_for_each_entry(substream,
  				    &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
  				    list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1301
1302
1303
1304
1305
1306
1307
1308
  			snd_iprintf(buffer,
  				    "Output %d
  "
  				    "  Tx bytes     : %lu
  ",
  				    substream->number,
  				    (unsigned long) substream->bytes);
  			if (substream->opened) {
7584af10c   Clemens Ladisch   sound: rawmidi: r...
1309
1310
1311
1312
  				snd_iprintf(buffer,
  				    "  Owner PID    : %d
  ",
  				    pid_vnr(substream->pid));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
  				runtime = substream->runtime;
  				snd_iprintf(buffer,
  				    "  Mode         : %s
  "
  				    "  Buffer size  : %lu
  "
  				    "  Avail        : %lu
  ",
  				    runtime->oss ? "OSS compatible" : "native",
  				    (unsigned long) runtime->buffer_size,
  				    (unsigned long) runtime->avail);
  			}
  		}
  	}
  	if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
9244b2c30   Johannes Berg   [ALSA] alsa core:...
1328
1329
1330
  		list_for_each_entry(substream,
  				    &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
  				    list) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1331
1332
1333
1334
1335
1336
1337
1338
  			snd_iprintf(buffer,
  				    "Input %d
  "
  				    "  Rx bytes     : %lu
  ",
  				    substream->number,
  				    (unsigned long) substream->bytes);
  			if (substream->opened) {
7584af10c   Clemens Ladisch   sound: rawmidi: r...
1339
1340
1341
1342
  				snd_iprintf(buffer,
  					    "  Owner PID    : %d
  ",
  					    pid_vnr(substream->pid));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
  				runtime = substream->runtime;
  				snd_iprintf(buffer,
  					    "  Buffer size  : %lu
  "
  					    "  Avail        : %lu
  "
  					    "  Overruns     : %lu
  ",
  					    (unsigned long) runtime->buffer_size,
  					    (unsigned long) runtime->avail,
  					    (unsigned long) runtime->xruns);
  			}
  		}
  	}
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1357
  	mutex_unlock(&rmidi->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1358
1359
1360
1361
1362
  }
  
  /*
   *  Register functions
   */
9c2e08c59   Arjan van de Ven   [PATCH] mark stru...
1363
  static const struct file_operations snd_rawmidi_f_ops =
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1364
1365
1366
1367
1368
1369
  {
  	.owner =	THIS_MODULE,
  	.read =		snd_rawmidi_read,
  	.write =	snd_rawmidi_write,
  	.open =		snd_rawmidi_open,
  	.release =	snd_rawmidi_release,
02f4865fa   Takashi Iwai   ALSA: core - Defi...
1370
  	.llseek =	no_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1371
1372
1373
1374
  	.poll =		snd_rawmidi_poll,
  	.unlocked_ioctl =	snd_rawmidi_ioctl,
  	.compat_ioctl =	snd_rawmidi_ioctl_compat,
  };
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1375
1376
  static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
  					struct snd_rawmidi_str *stream,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1377
1378
1379
  					int direction,
  					int count)
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1380
  	struct snd_rawmidi_substream *substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1381
  	int idx;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1382
  	for (idx = 0; idx < count; idx++) {
ca2c09665   Takashi Iwai   [ALSA] Replace wi...
1383
  		substream = kzalloc(sizeof(*substream), GFP_KERNEL);
73e77ba02   Takashi Iwai   [ALSA] Add error ...
1384
1385
1386
  		if (substream == NULL) {
  			snd_printk(KERN_ERR "rawmidi: cannot allocate substream
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1387
  			return -ENOMEM;
73e77ba02   Takashi Iwai   [ALSA] Add error ...
1388
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
  		substream->stream = direction;
  		substream->number = idx;
  		substream->rmidi = rmidi;
  		substream->pstr = stream;
  		list_add_tail(&substream->list, &stream->substreams);
  		stream->substream_count++;
  	}
  	return 0;
  }
  
  /**
   * snd_rawmidi_new - create a rawmidi instance
   * @card: the card instance
   * @id: the id string
   * @device: the device index
   * @output_count: the number of output streams
   * @input_count: the number of input streams
   * @rrawmidi: the pointer to store the new rawmidi instance
   *
   * Creates a new rawmidi instance.
   * Use snd_rawmidi_set_ops() to set the operators to the new instance.
   *
   * Returns zero if successful, or a negative error code on failure.
   */
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1413
  int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1414
  		    int output_count, int input_count,
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1415
  		    struct snd_rawmidi ** rrawmidi)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1416
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1417
  	struct snd_rawmidi *rmidi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1418
  	int err;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1419
  	static struct snd_device_ops ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1420
1421
1422
  		.dev_free = snd_rawmidi_dev_free,
  		.dev_register = snd_rawmidi_dev_register,
  		.dev_disconnect = snd_rawmidi_dev_disconnect,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1423
  	};
7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
1424
1425
1426
1427
  	if (snd_BUG_ON(!card))
  		return -ENXIO;
  	if (rrawmidi)
  		*rrawmidi = NULL;
ca2c09665   Takashi Iwai   [ALSA] Replace wi...
1428
  	rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
73e77ba02   Takashi Iwai   [ALSA] Add error ...
1429
1430
1431
  	if (rmidi == NULL) {
  		snd_printk(KERN_ERR "rawmidi: cannot allocate
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1432
  		return -ENOMEM;
73e77ba02   Takashi Iwai   [ALSA] Add error ...
1433
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1434
1435
  	rmidi->card = card;
  	rmidi->device = device;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1436
  	mutex_init(&rmidi->open_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1437
  	init_waitqueue_head(&rmidi->open_wait);
c13893d7b   Akinobu Mita   [ALSA] sound: ini...
1438
1439
  	INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
  	INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1440
1441
  	if (id != NULL)
  		strlcpy(rmidi->id, id, sizeof(rmidi->id));
73e77ba02   Takashi Iwai   [ALSA] Add error ...
1442
1443
1444
1445
  	if ((err = snd_rawmidi_alloc_substreams(rmidi,
  						&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
  						SNDRV_RAWMIDI_STREAM_INPUT,
  						input_count)) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1446
1447
1448
  		snd_rawmidi_free(rmidi);
  		return err;
  	}
73e77ba02   Takashi Iwai   [ALSA] Add error ...
1449
1450
1451
1452
  	if ((err = snd_rawmidi_alloc_substreams(rmidi,
  						&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
  						SNDRV_RAWMIDI_STREAM_OUTPUT,
  						output_count)) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1453
1454
1455
1456
1457
1458
1459
  		snd_rawmidi_free(rmidi);
  		return err;
  	}
  	if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
  		snd_rawmidi_free(rmidi);
  		return err;
  	}
7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
1460
1461
  	if (rrawmidi)
  		*rrawmidi = rmidi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1462
1463
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1464
  static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1465
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1466
  	struct snd_rawmidi_substream *substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1467
1468
  
  	while (!list_empty(&stream->substreams)) {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1469
  		substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1470
1471
1472
1473
  		list_del(&substream->list);
  		kfree(substream);
  	}
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1474
  static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1475
  {
7eaa943c8   Takashi Iwai   ALSA: Kill snd_as...
1476
1477
  	if (!rmidi)
  		return 0;
c461482c8   Takashi Iwai   [ALSA] Unregister...
1478
1479
1480
1481
1482
1483
1484
  
  	snd_info_free_entry(rmidi->proc_entry);
  	rmidi->proc_entry = NULL;
  	mutex_lock(&register_mutex);
  	if (rmidi->ops && rmidi->ops->dev_unregister)
  		rmidi->ops->dev_unregister(rmidi);
  	mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1485
1486
1487
1488
1489
1490
1491
  	snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
  	snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
  	if (rmidi->private_free)
  		rmidi->private_free(rmidi);
  	kfree(rmidi);
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1492
  static int snd_rawmidi_dev_free(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1493
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1494
  	struct snd_rawmidi *rmidi = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1495
1496
1497
1498
  	return snd_rawmidi_free(rmidi);
  }
  
  #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1499
  static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1500
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1501
  	struct snd_rawmidi *rmidi = device->private_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1502
1503
1504
  	rmidi->seq_dev = NULL;
  }
  #endif
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1505
  static int snd_rawmidi_dev_register(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1506
  {
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
1507
  	int err;
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1508
  	struct snd_info_entry *entry;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1509
  	char name[16];
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1510
  	struct snd_rawmidi *rmidi = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1511
1512
1513
  
  	if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
  		return -ENOMEM;
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1514
  	mutex_lock(&register_mutex);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
1515
  	if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1516
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1517
1518
  		return -EBUSY;
  	}
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
1519
  	list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1520
1521
1522
  	sprintf(name, "midiC%iD%i", rmidi->card->number, rmidi->device);
  	if ((err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
  				       rmidi->card, rmidi->device,
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
1523
  				       &snd_rawmidi_f_ops, rmidi, name)) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1524
1525
  		snd_printk(KERN_ERR "unable to register rawmidi device %i:%i
  ", rmidi->card->number, rmidi->device);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
1526
  		list_del(&rmidi->list);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1527
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1528
1529
1530
1531
1532
  		return err;
  	}
  	if (rmidi->ops && rmidi->ops->dev_register &&
  	    (err = rmidi->ops->dev_register(rmidi)) < 0) {
  		snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
1533
  		list_del(&rmidi->list);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1534
  		mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1535
1536
1537
1538
1539
1540
  		return err;
  	}
  #ifdef CONFIG_SND_OSSEMUL
  	rmidi->ossreg = 0;
  	if ((int)rmidi->device == midi_map[rmidi->card->number]) {
  		if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
1541
1542
  					    rmidi->card, 0, &snd_rawmidi_f_ops,
  					    rmidi, name) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
  			snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i
  ", rmidi->card->number, 0);
  		} else {
  			rmidi->ossreg++;
  #ifdef SNDRV_OSS_INFO_DEV_MIDI
  			snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
  #endif
  		}
  	}
  	if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
  		if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
1554
1555
  					    rmidi->card, 1, &snd_rawmidi_f_ops,
  					    rmidi, name) < 0) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1556
1557
1558
1559
1560
1561
1562
  			snd_printk(KERN_ERR "unable to register OSS rawmidi device %i:%i
  ", rmidi->card->number, 1);
  		} else {
  			rmidi->ossreg++;
  		}
  	}
  #endif /* CONFIG_SND_OSSEMUL */
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1563
  	mutex_unlock(&register_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1564
1565
1566
1567
  	sprintf(name, "midi%d", rmidi->device);
  	entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
  	if (entry) {
  		entry->private_data = rmidi;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
  		entry->c.text.read = snd_rawmidi_proc_info_read;
  		if (snd_info_register(entry) < 0) {
  			snd_info_free_entry(entry);
  			entry = NULL;
  		}
  	}
  	rmidi->proc_entry = entry;
  #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
  	if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
  		if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
  			rmidi->seq_dev->private_data = rmidi;
  			rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
  			sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
  			snd_device_register(rmidi->card, rmidi->seq_dev);
  		}
  	}
  #endif
  	return 0;
  }
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1587
  static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1588
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1589
  	struct snd_rawmidi *rmidi = device->device_data;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1590

1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1591
  	mutex_lock(&register_mutex);
f87135f56   Clemens Ladisch   [ALSA] dynamic mi...
1592
  	list_del_init(&rmidi->list);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
  #ifdef CONFIG_SND_OSSEMUL
  	if (rmidi->ossreg) {
  		if ((int)rmidi->device == midi_map[rmidi->card->number]) {
  			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
  #ifdef SNDRV_OSS_INFO_DEV_MIDI
  			snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
  #endif
  		}
  		if ((int)rmidi->device == amidi_map[rmidi->card->number])
  			snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
  		rmidi->ossreg = 0;
  	}
  #endif /* CONFIG_SND_OSSEMUL */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1606
  	snd_unregister_device(SNDRV_DEVICE_TYPE_RAWMIDI, rmidi->card, rmidi->device);
1a60d4c5a   Ingo Molnar   [ALSA] semaphore ...
1607
  	mutex_unlock(&register_mutex);
c461482c8   Takashi Iwai   [ALSA] Unregister...
1608
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
  }
  
  /**
   * snd_rawmidi_set_ops - set the rawmidi operators
   * @rmidi: the rawmidi instance
   * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
   * @ops: the operator table
   *
   * Sets the rawmidi operators for the given stream direction.
   */
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1619
1620
  void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
  			 struct snd_rawmidi_ops *ops)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1621
  {
48c9d417d   Takashi Iwai   [ALSA] Remove xxx...
1622
  	struct snd_rawmidi_substream *substream;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1623
  	
9244b2c30   Johannes Berg   [ALSA] alsa core:...
1624
  	list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1625
  		substream->ops = ops;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
  }
  
  /*
   *  ENTRY functions
   */
  
  static int __init alsa_rawmidi_init(void)
  {
  
  	snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
  	snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
  #ifdef CONFIG_SND_OSSEMUL
  	{ int i;
  	/* check device map table */
  	for (i = 0; i < SNDRV_CARDS; i++) {
  		if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
  			snd_printk(KERN_ERR "invalid midi_map[%d] = %d
  ", i, midi_map[i]);
  			midi_map[i] = 0;
  		}
  		if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
  			snd_printk(KERN_ERR "invalid amidi_map[%d] = %d
  ", i, amidi_map[i]);
  			amidi_map[i] = 1;
  		}
  	}
  	}
  #endif /* CONFIG_SND_OSSEMUL */
  	return 0;
  }
  
  static void __exit alsa_rawmidi_exit(void)
  {
  	snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
  	snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
  }
  
  module_init(alsa_rawmidi_init)
  module_exit(alsa_rawmidi_exit)
  
  EXPORT_SYMBOL(snd_rawmidi_output_params);
  EXPORT_SYMBOL(snd_rawmidi_input_params);
  EXPORT_SYMBOL(snd_rawmidi_drop_output);
  EXPORT_SYMBOL(snd_rawmidi_drain_output);
  EXPORT_SYMBOL(snd_rawmidi_drain_input);
  EXPORT_SYMBOL(snd_rawmidi_receive);
  EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
  EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
  EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
  EXPORT_SYMBOL(snd_rawmidi_transmit);
  EXPORT_SYMBOL(snd_rawmidi_new);
  EXPORT_SYMBOL(snd_rawmidi_set_ops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1678
1679
1680
1681
1682
  EXPORT_SYMBOL(snd_rawmidi_info_select);
  EXPORT_SYMBOL(snd_rawmidi_kernel_open);
  EXPORT_SYMBOL(snd_rawmidi_kernel_release);
  EXPORT_SYMBOL(snd_rawmidi_kernel_read);
  EXPORT_SYMBOL(snd_rawmidi_kernel_write);