GNU Linux-libre 4.9.283-gnu1
[releases.git] / sound / core / rawmidi.c
1 /*
2  *  Abstract layer for MIDI v1.0 stream
3  *  Copyright (c) by Jaroslav Kysela <perex@perex.cz>
4  *
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19  *
20  */
21
22 #include <sound/core.h>
23 #include <linux/major.h>
24 #include <linux/init.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/time.h>
28 #include <linux/wait.h>
29 #include <linux/mutex.h>
30 #include <linux/module.h>
31 #include <linux/delay.h>
32 #include <linux/nospec.h>
33 #include <sound/rawmidi.h>
34 #include <sound/info.h>
35 #include <sound/control.h>
36 #include <sound/minors.h>
37 #include <sound/initval.h>
38
39 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
40 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
41 MODULE_LICENSE("GPL");
42
43 #ifdef CONFIG_SND_OSSEMUL
44 static int midi_map[SNDRV_CARDS];
45 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
46 module_param_array(midi_map, int, NULL, 0444);
47 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
48 module_param_array(amidi_map, int, NULL, 0444);
49 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
50 #endif /* CONFIG_SND_OSSEMUL */
51
52 static int snd_rawmidi_free(struct snd_rawmidi *rawmidi);
53 static int snd_rawmidi_dev_free(struct snd_device *device);
54 static int snd_rawmidi_dev_register(struct snd_device *device);
55 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
56
57 static LIST_HEAD(snd_rawmidi_devices);
58 static DEFINE_MUTEX(register_mutex);
59
60 #define rmidi_err(rmidi, fmt, args...) \
61         dev_err(&(rmidi)->dev, fmt, ##args)
62 #define rmidi_warn(rmidi, fmt, args...) \
63         dev_warn(&(rmidi)->dev, fmt, ##args)
64 #define rmidi_dbg(rmidi, fmt, args...) \
65         dev_dbg(&(rmidi)->dev, fmt, ##args)
66
67 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
68 {
69         struct snd_rawmidi *rawmidi;
70
71         list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
72                 if (rawmidi->card == card && rawmidi->device == device)
73                         return rawmidi;
74         return NULL;
75 }
76
77 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
78 {
79         switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
80         case FMODE_WRITE:
81                 return SNDRV_RAWMIDI_LFLG_OUTPUT;
82         case FMODE_READ:
83                 return SNDRV_RAWMIDI_LFLG_INPUT;
84         default:
85                 return SNDRV_RAWMIDI_LFLG_OPEN;
86         }
87 }
88
89 static inline int snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
90 {
91         struct snd_rawmidi_runtime *runtime = substream->runtime;
92         return runtime->avail >= runtime->avail_min;
93 }
94
95 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
96                                            size_t count)
97 {
98         struct snd_rawmidi_runtime *runtime = substream->runtime;
99         return runtime->avail >= runtime->avail_min &&
100                (!substream->append || runtime->avail >= count);
101 }
102
103 static void snd_rawmidi_input_event_work(struct work_struct *work)
104 {
105         struct snd_rawmidi_runtime *runtime =
106                 container_of(work, struct snd_rawmidi_runtime, event_work);
107         if (runtime->event)
108                 runtime->event(runtime->substream);
109 }
110
111 /* buffer refcount management: call with runtime->lock held */
112 static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime *runtime)
113 {
114         runtime->buffer_ref++;
115 }
116
117 static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime *runtime)
118 {
119         runtime->buffer_ref--;
120 }
121
122 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
123 {
124         struct snd_rawmidi_runtime *runtime;
125
126         if ((runtime = kzalloc(sizeof(*runtime), GFP_KERNEL)) == NULL)
127                 return -ENOMEM;
128         runtime->substream = substream;
129         spin_lock_init(&runtime->lock);
130         init_waitqueue_head(&runtime->sleep);
131         INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
132         runtime->event = NULL;
133         runtime->buffer_size = PAGE_SIZE;
134         runtime->avail_min = 1;
135         if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
136                 runtime->avail = 0;
137         else
138                 runtime->avail = runtime->buffer_size;
139         if ((runtime->buffer = kzalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
140                 kfree(runtime);
141                 return -ENOMEM;
142         }
143         runtime->appl_ptr = runtime->hw_ptr = 0;
144         substream->runtime = runtime;
145         return 0;
146 }
147
148 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
149 {
150         struct snd_rawmidi_runtime *runtime = substream->runtime;
151
152         kfree(runtime->buffer);
153         kfree(runtime);
154         substream->runtime = NULL;
155         return 0;
156 }
157
158 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream,int up)
159 {
160         if (!substream->opened)
161                 return;
162         substream->ops->trigger(substream, up);
163 }
164
165 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
166 {
167         if (!substream->opened)
168                 return;
169         substream->ops->trigger(substream, up);
170         if (!up)
171                 cancel_work_sync(&substream->runtime->event_work);
172 }
173
174 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
175 {
176         unsigned long flags;
177         struct snd_rawmidi_runtime *runtime = substream->runtime;
178
179         snd_rawmidi_output_trigger(substream, 0);
180         runtime->drain = 0;
181         spin_lock_irqsave(&runtime->lock, flags);
182         runtime->appl_ptr = runtime->hw_ptr = 0;
183         runtime->avail = runtime->buffer_size;
184         spin_unlock_irqrestore(&runtime->lock, flags);
185         return 0;
186 }
187 EXPORT_SYMBOL(snd_rawmidi_drop_output);
188
189 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
190 {
191         int err;
192         long timeout;
193         struct snd_rawmidi_runtime *runtime = substream->runtime;
194
195         err = 0;
196         runtime->drain = 1;
197         timeout = wait_event_interruptible_timeout(runtime->sleep,
198                                 (runtime->avail >= runtime->buffer_size),
199                                 10*HZ);
200         if (signal_pending(current))
201                 err = -ERESTARTSYS;
202         if (runtime->avail < runtime->buffer_size && !timeout) {
203                 rmidi_warn(substream->rmidi,
204                            "rawmidi drain error (avail = %li, buffer_size = %li)\n",
205                            (long)runtime->avail, (long)runtime->buffer_size);
206                 err = -EIO;
207         }
208         runtime->drain = 0;
209         if (err != -ERESTARTSYS) {
210                 /* we need wait a while to make sure that Tx FIFOs are empty */
211                 if (substream->ops->drain)
212                         substream->ops->drain(substream);
213                 else
214                         msleep(50);
215                 snd_rawmidi_drop_output(substream);
216         }
217         return err;
218 }
219 EXPORT_SYMBOL(snd_rawmidi_drain_output);
220
221 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
222 {
223         unsigned long flags;
224         struct snd_rawmidi_runtime *runtime = substream->runtime;
225
226         snd_rawmidi_input_trigger(substream, 0);
227         runtime->drain = 0;
228         spin_lock_irqsave(&runtime->lock, flags);
229         runtime->appl_ptr = runtime->hw_ptr = 0;
230         runtime->avail = 0;
231         spin_unlock_irqrestore(&runtime->lock, flags);
232         return 0;
233 }
234 EXPORT_SYMBOL(snd_rawmidi_drain_input);
235
236 /* look for an available substream for the given stream direction;
237  * if a specific subdevice is given, try to assign it
238  */
239 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
240                             int stream, int mode,
241                             struct snd_rawmidi_substream **sub_ret)
242 {
243         struct snd_rawmidi_substream *substream;
244         struct snd_rawmidi_str *s = &rmidi->streams[stream];
245         static unsigned int info_flags[2] = {
246                 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
247                 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
248         };
249
250         if (!(rmidi->info_flags & info_flags[stream]))
251                 return -ENXIO;
252         if (subdevice >= 0 && subdevice >= s->substream_count)
253                 return -ENODEV;
254
255         list_for_each_entry(substream, &s->substreams, list) {
256                 if (substream->opened) {
257                         if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
258                             !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
259                             !substream->append)
260                                 continue;
261                 }
262                 if (subdevice < 0 || subdevice == substream->number) {
263                         *sub_ret = substream;
264                         return 0;
265                 }
266         }
267         return -EAGAIN;
268 }
269
270 /* open and do ref-counting for the given substream */
271 static int open_substream(struct snd_rawmidi *rmidi,
272                           struct snd_rawmidi_substream *substream,
273                           int mode)
274 {
275         int err;
276
277         if (substream->use_count == 0) {
278                 err = snd_rawmidi_runtime_create(substream);
279                 if (err < 0)
280                         return err;
281                 err = substream->ops->open(substream);
282                 if (err < 0) {
283                         snd_rawmidi_runtime_free(substream);
284                         return err;
285                 }
286                 substream->opened = 1;
287                 substream->active_sensing = 0;
288                 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
289                         substream->append = 1;
290                 substream->pid = get_pid(task_pid(current));
291                 rmidi->streams[substream->stream].substream_opened++;
292         }
293         substream->use_count++;
294         return 0;
295 }
296
297 static void close_substream(struct snd_rawmidi *rmidi,
298                             struct snd_rawmidi_substream *substream,
299                             int cleanup);
300
301 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
302                              struct snd_rawmidi_file *rfile)
303 {
304         struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
305         int err;
306
307         rfile->input = rfile->output = NULL;
308         if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
309                 err = assign_substream(rmidi, subdevice,
310                                        SNDRV_RAWMIDI_STREAM_INPUT,
311                                        mode, &sinput);
312                 if (err < 0)
313                         return err;
314         }
315         if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
316                 err = assign_substream(rmidi, subdevice,
317                                        SNDRV_RAWMIDI_STREAM_OUTPUT,
318                                        mode, &soutput);
319                 if (err < 0)
320                         return err;
321         }
322
323         if (sinput) {
324                 err = open_substream(rmidi, sinput, mode);
325                 if (err < 0)
326                         return err;
327         }
328         if (soutput) {
329                 err = open_substream(rmidi, soutput, mode);
330                 if (err < 0) {
331                         if (sinput)
332                                 close_substream(rmidi, sinput, 0);
333                         return err;
334                 }
335         }
336
337         rfile->rmidi = rmidi;
338         rfile->input = sinput;
339         rfile->output = soutput;
340         return 0;
341 }
342
343 /* called from sound/core/seq/seq_midi.c */
344 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
345                             int mode, struct snd_rawmidi_file * rfile)
346 {
347         struct snd_rawmidi *rmidi;
348         int err;
349
350         if (snd_BUG_ON(!rfile))
351                 return -EINVAL;
352
353         mutex_lock(&register_mutex);
354         rmidi = snd_rawmidi_search(card, device);
355         if (rmidi == NULL) {
356                 mutex_unlock(&register_mutex);
357                 return -ENODEV;
358         }
359         if (!try_module_get(rmidi->card->module)) {
360                 mutex_unlock(&register_mutex);
361                 return -ENXIO;
362         }
363         mutex_unlock(&register_mutex);
364
365         mutex_lock(&rmidi->open_mutex);
366         err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
367         mutex_unlock(&rmidi->open_mutex);
368         if (err < 0)
369                 module_put(rmidi->card->module);
370         return err;
371 }
372 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
373
374 static int snd_rawmidi_open(struct inode *inode, struct file *file)
375 {
376         int maj = imajor(inode);
377         struct snd_card *card;
378         int subdevice;
379         unsigned short fflags;
380         int err;
381         struct snd_rawmidi *rmidi;
382         struct snd_rawmidi_file *rawmidi_file = NULL;
383         wait_queue_t wait;
384
385         if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK)) 
386                 return -EINVAL;         /* invalid combination */
387
388         err = nonseekable_open(inode, file);
389         if (err < 0)
390                 return err;
391
392         if (maj == snd_major) {
393                 rmidi = snd_lookup_minor_data(iminor(inode),
394                                               SNDRV_DEVICE_TYPE_RAWMIDI);
395 #ifdef CONFIG_SND_OSSEMUL
396         } else if (maj == SOUND_MAJOR) {
397                 rmidi = snd_lookup_oss_minor_data(iminor(inode),
398                                                   SNDRV_OSS_DEVICE_TYPE_MIDI);
399 #endif
400         } else
401                 return -ENXIO;
402
403         if (rmidi == NULL)
404                 return -ENODEV;
405
406         if (!try_module_get(rmidi->card->module)) {
407                 snd_card_unref(rmidi->card);
408                 return -ENXIO;
409         }
410
411         mutex_lock(&rmidi->open_mutex);
412         card = rmidi->card;
413         err = snd_card_file_add(card, file);
414         if (err < 0)
415                 goto __error_card;
416         fflags = snd_rawmidi_file_flags(file);
417         if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR) /* OSS emul? */
418                 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
419         rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
420         if (rawmidi_file == NULL) {
421                 err = -ENOMEM;
422                 goto __error;
423         }
424         init_waitqueue_entry(&wait, current);
425         add_wait_queue(&rmidi->open_wait, &wait);
426         while (1) {
427                 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
428                 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
429                 if (err >= 0)
430                         break;
431                 if (err == -EAGAIN) {
432                         if (file->f_flags & O_NONBLOCK) {
433                                 err = -EBUSY;
434                                 break;
435                         }
436                 } else
437                         break;
438                 set_current_state(TASK_INTERRUPTIBLE);
439                 mutex_unlock(&rmidi->open_mutex);
440                 schedule();
441                 mutex_lock(&rmidi->open_mutex);
442                 if (rmidi->card->shutdown) {
443                         err = -ENODEV;
444                         break;
445                 }
446                 if (signal_pending(current)) {
447                         err = -ERESTARTSYS;
448                         break;
449                 }
450         }
451         remove_wait_queue(&rmidi->open_wait, &wait);
452         if (err < 0) {
453                 kfree(rawmidi_file);
454                 goto __error;
455         }
456 #ifdef CONFIG_SND_OSSEMUL
457         if (rawmidi_file->input && rawmidi_file->input->runtime)
458                 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
459         if (rawmidi_file->output && rawmidi_file->output->runtime)
460                 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
461 #endif
462         file->private_data = rawmidi_file;
463         mutex_unlock(&rmidi->open_mutex);
464         snd_card_unref(rmidi->card);
465         return 0;
466
467  __error:
468         snd_card_file_remove(card, file);
469  __error_card:
470         mutex_unlock(&rmidi->open_mutex);
471         module_put(rmidi->card->module);
472         snd_card_unref(rmidi->card);
473         return err;
474 }
475
476 static void close_substream(struct snd_rawmidi *rmidi,
477                             struct snd_rawmidi_substream *substream,
478                             int cleanup)
479 {
480         if (--substream->use_count)
481                 return;
482
483         if (cleanup) {
484                 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
485                         snd_rawmidi_input_trigger(substream, 0);
486                 else {
487                         if (substream->active_sensing) {
488                                 unsigned char buf = 0xfe;
489                                 /* sending single active sensing message
490                                  * to shut the device up
491                                  */
492                                 snd_rawmidi_kernel_write(substream, &buf, 1);
493                         }
494                         if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
495                                 snd_rawmidi_output_trigger(substream, 0);
496                 }
497         }
498         substream->ops->close(substream);
499         if (substream->runtime->private_free)
500                 substream->runtime->private_free(substream);
501         snd_rawmidi_runtime_free(substream);
502         substream->opened = 0;
503         substream->append = 0;
504         put_pid(substream->pid);
505         substream->pid = NULL;
506         rmidi->streams[substream->stream].substream_opened--;
507 }
508
509 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
510 {
511         struct snd_rawmidi *rmidi;
512
513         rmidi = rfile->rmidi;
514         mutex_lock(&rmidi->open_mutex);
515         if (rfile->input) {
516                 close_substream(rmidi, rfile->input, 1);
517                 rfile->input = NULL;
518         }
519         if (rfile->output) {
520                 close_substream(rmidi, rfile->output, 1);
521                 rfile->output = NULL;
522         }
523         rfile->rmidi = NULL;
524         mutex_unlock(&rmidi->open_mutex);
525         wake_up(&rmidi->open_wait);
526 }
527
528 /* called from sound/core/seq/seq_midi.c */
529 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
530 {
531         struct snd_rawmidi *rmidi;
532
533         if (snd_BUG_ON(!rfile))
534                 return -ENXIO;
535         
536         rmidi = rfile->rmidi;
537         rawmidi_release_priv(rfile);
538         module_put(rmidi->card->module);
539         return 0;
540 }
541 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
542
543 static int snd_rawmidi_release(struct inode *inode, struct file *file)
544 {
545         struct snd_rawmidi_file *rfile;
546         struct snd_rawmidi *rmidi;
547         struct module *module;
548
549         rfile = file->private_data;
550         rmidi = rfile->rmidi;
551         rawmidi_release_priv(rfile);
552         kfree(rfile);
553         module = rmidi->card->module;
554         snd_card_file_remove(rmidi->card, file);
555         module_put(module);
556         return 0;
557 }
558
559 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
560                             struct snd_rawmidi_info *info)
561 {
562         struct snd_rawmidi *rmidi;
563         
564         if (substream == NULL)
565                 return -ENODEV;
566         rmidi = substream->rmidi;
567         memset(info, 0, sizeof(*info));
568         info->card = rmidi->card->number;
569         info->device = rmidi->device;
570         info->subdevice = substream->number;
571         info->stream = substream->stream;
572         info->flags = rmidi->info_flags;
573         strcpy(info->id, rmidi->id);
574         strcpy(info->name, rmidi->name);
575         strcpy(info->subname, substream->name);
576         info->subdevices_count = substream->pstr->substream_count;
577         info->subdevices_avail = (substream->pstr->substream_count -
578                                   substream->pstr->substream_opened);
579         return 0;
580 }
581
582 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
583                                  struct snd_rawmidi_info __user * _info)
584 {
585         struct snd_rawmidi_info info;
586         int err;
587         if ((err = snd_rawmidi_info(substream, &info)) < 0)
588                 return err;
589         if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
590                 return -EFAULT;
591         return 0;
592 }
593
594 static int __snd_rawmidi_info_select(struct snd_card *card,
595                                      struct snd_rawmidi_info *info)
596 {
597         struct snd_rawmidi *rmidi;
598         struct snd_rawmidi_str *pstr;
599         struct snd_rawmidi_substream *substream;
600
601         rmidi = snd_rawmidi_search(card, info->device);
602         if (!rmidi)
603                 return -ENXIO;
604         if (info->stream < 0 || info->stream > 1)
605                 return -EINVAL;
606         info->stream = array_index_nospec(info->stream, 2);
607         pstr = &rmidi->streams[info->stream];
608         if (pstr->substream_count == 0)
609                 return -ENOENT;
610         if (info->subdevice >= pstr->substream_count)
611                 return -ENXIO;
612         list_for_each_entry(substream, &pstr->substreams, list) {
613                 if ((unsigned int)substream->number == info->subdevice)
614                         return snd_rawmidi_info(substream, info);
615         }
616         return -ENXIO;
617 }
618
619 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
620 {
621         int ret;
622
623         mutex_lock(&register_mutex);
624         ret = __snd_rawmidi_info_select(card, info);
625         mutex_unlock(&register_mutex);
626         return ret;
627 }
628 EXPORT_SYMBOL(snd_rawmidi_info_select);
629
630 static int snd_rawmidi_info_select_user(struct snd_card *card,
631                                         struct snd_rawmidi_info __user *_info)
632 {
633         int err;
634         struct snd_rawmidi_info info;
635         if (get_user(info.device, &_info->device))
636                 return -EFAULT;
637         if (get_user(info.stream, &_info->stream))
638                 return -EFAULT;
639         if (get_user(info.subdevice, &_info->subdevice))
640                 return -EFAULT;
641         if ((err = snd_rawmidi_info_select(card, &info)) < 0)
642                 return err;
643         if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
644                 return -EFAULT;
645         return 0;
646 }
647
648 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
649                               struct snd_rawmidi_params * params)
650 {
651         char *newbuf, *oldbuf;
652         struct snd_rawmidi_runtime *runtime = substream->runtime;
653         
654         if (substream->append && substream->use_count > 1)
655                 return -EBUSY;
656         snd_rawmidi_drain_output(substream);
657         if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
658                 return -EINVAL;
659         }
660         if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
661                 return -EINVAL;
662         }
663         if (params->buffer_size != runtime->buffer_size) {
664                 newbuf = kzalloc(params->buffer_size, GFP_KERNEL);
665                 if (!newbuf)
666                         return -ENOMEM;
667                 spin_lock_irq(&runtime->lock);
668                 if (runtime->buffer_ref) {
669                         spin_unlock_irq(&runtime->lock);
670                         kfree(newbuf);
671                         return -EBUSY;
672                 }
673                 oldbuf = runtime->buffer;
674                 runtime->buffer = newbuf;
675                 runtime->buffer_size = params->buffer_size;
676                 runtime->avail = runtime->buffer_size;
677                 runtime->appl_ptr = runtime->hw_ptr = 0;
678                 spin_unlock_irq(&runtime->lock);
679                 kfree(oldbuf);
680         }
681         runtime->avail_min = params->avail_min;
682         substream->active_sensing = !params->no_active_sensing;
683         return 0;
684 }
685 EXPORT_SYMBOL(snd_rawmidi_output_params);
686
687 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
688                              struct snd_rawmidi_params * params)
689 {
690         char *newbuf, *oldbuf;
691         struct snd_rawmidi_runtime *runtime = substream->runtime;
692
693         snd_rawmidi_drain_input(substream);
694         if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L) {
695                 return -EINVAL;
696         }
697         if (params->avail_min < 1 || params->avail_min > params->buffer_size) {
698                 return -EINVAL;
699         }
700         if (params->buffer_size != runtime->buffer_size) {
701                 newbuf = kmalloc(params->buffer_size, GFP_KERNEL);
702                 if (!newbuf)
703                         return -ENOMEM;
704                 spin_lock_irq(&runtime->lock);
705                 oldbuf = runtime->buffer;
706                 runtime->buffer = newbuf;
707                 runtime->buffer_size = params->buffer_size;
708                 runtime->appl_ptr = runtime->hw_ptr = 0;
709                 spin_unlock_irq(&runtime->lock);
710                 kfree(oldbuf);
711         }
712         runtime->avail_min = params->avail_min;
713         return 0;
714 }
715 EXPORT_SYMBOL(snd_rawmidi_input_params);
716
717 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
718                                      struct snd_rawmidi_status * status)
719 {
720         struct snd_rawmidi_runtime *runtime = substream->runtime;
721
722         memset(status, 0, sizeof(*status));
723         status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
724         spin_lock_irq(&runtime->lock);
725         status->avail = runtime->avail;
726         spin_unlock_irq(&runtime->lock);
727         return 0;
728 }
729
730 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
731                                     struct snd_rawmidi_status * status)
732 {
733         struct snd_rawmidi_runtime *runtime = substream->runtime;
734
735         memset(status, 0, sizeof(*status));
736         status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
737         spin_lock_irq(&runtime->lock);
738         status->avail = runtime->avail;
739         status->xruns = runtime->xruns;
740         runtime->xruns = 0;
741         spin_unlock_irq(&runtime->lock);
742         return 0;
743 }
744
745 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
746 {
747         struct snd_rawmidi_file *rfile;
748         void __user *argp = (void __user *)arg;
749
750         rfile = file->private_data;
751         if (((cmd >> 8) & 0xff) != 'W')
752                 return -ENOTTY;
753         switch (cmd) {
754         case SNDRV_RAWMIDI_IOCTL_PVERSION:
755                 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
756         case SNDRV_RAWMIDI_IOCTL_INFO:
757         {
758                 int stream;
759                 struct snd_rawmidi_info __user *info = argp;
760                 if (get_user(stream, &info->stream))
761                         return -EFAULT;
762                 switch (stream) {
763                 case SNDRV_RAWMIDI_STREAM_INPUT:
764                         return snd_rawmidi_info_user(rfile->input, info);
765                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
766                         return snd_rawmidi_info_user(rfile->output, info);
767                 default:
768                         return -EINVAL;
769                 }
770         }
771         case SNDRV_RAWMIDI_IOCTL_PARAMS:
772         {
773                 struct snd_rawmidi_params params;
774                 if (copy_from_user(&params, argp, sizeof(struct snd_rawmidi_params)))
775                         return -EFAULT;
776                 switch (params.stream) {
777                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
778                         if (rfile->output == NULL)
779                                 return -EINVAL;
780                         return snd_rawmidi_output_params(rfile->output, &params);
781                 case SNDRV_RAWMIDI_STREAM_INPUT:
782                         if (rfile->input == NULL)
783                                 return -EINVAL;
784                         return snd_rawmidi_input_params(rfile->input, &params);
785                 default:
786                         return -EINVAL;
787                 }
788         }
789         case SNDRV_RAWMIDI_IOCTL_STATUS:
790         {
791                 int err = 0;
792                 struct snd_rawmidi_status status;
793                 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status)))
794                         return -EFAULT;
795                 switch (status.stream) {
796                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
797                         if (rfile->output == NULL)
798                                 return -EINVAL;
799                         err = snd_rawmidi_output_status(rfile->output, &status);
800                         break;
801                 case SNDRV_RAWMIDI_STREAM_INPUT:
802                         if (rfile->input == NULL)
803                                 return -EINVAL;
804                         err = snd_rawmidi_input_status(rfile->input, &status);
805                         break;
806                 default:
807                         return -EINVAL;
808                 }
809                 if (err < 0)
810                         return err;
811                 if (copy_to_user(argp, &status, sizeof(struct snd_rawmidi_status)))
812                         return -EFAULT;
813                 return 0;
814         }
815         case SNDRV_RAWMIDI_IOCTL_DROP:
816         {
817                 int val;
818                 if (get_user(val, (int __user *) argp))
819                         return -EFAULT;
820                 switch (val) {
821                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
822                         if (rfile->output == NULL)
823                                 return -EINVAL;
824                         return snd_rawmidi_drop_output(rfile->output);
825                 default:
826                         return -EINVAL;
827                 }
828         }
829         case SNDRV_RAWMIDI_IOCTL_DRAIN:
830         {
831                 int val;
832                 if (get_user(val, (int __user *) argp))
833                         return -EFAULT;
834                 switch (val) {
835                 case SNDRV_RAWMIDI_STREAM_OUTPUT:
836                         if (rfile->output == NULL)
837                                 return -EINVAL;
838                         return snd_rawmidi_drain_output(rfile->output);
839                 case SNDRV_RAWMIDI_STREAM_INPUT:
840                         if (rfile->input == NULL)
841                                 return -EINVAL;
842                         return snd_rawmidi_drain_input(rfile->input);
843                 default:
844                         return -EINVAL;
845                 }
846         }
847         default:
848                 rmidi_dbg(rfile->rmidi,
849                           "rawmidi: unknown command = 0x%x\n", cmd);
850         }
851         return -ENOTTY;
852 }
853
854 static int snd_rawmidi_control_ioctl(struct snd_card *card,
855                                      struct snd_ctl_file *control,
856                                      unsigned int cmd,
857                                      unsigned long arg)
858 {
859         void __user *argp = (void __user *)arg;
860
861         switch (cmd) {
862         case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
863         {
864                 int device;
865                 
866                 if (get_user(device, (int __user *)argp))
867                         return -EFAULT;
868                 if (device >= SNDRV_RAWMIDI_DEVICES) /* next device is -1 */
869                         device = SNDRV_RAWMIDI_DEVICES - 1;
870                 mutex_lock(&register_mutex);
871                 device = device < 0 ? 0 : device + 1;
872                 while (device < SNDRV_RAWMIDI_DEVICES) {
873                         if (snd_rawmidi_search(card, device))
874                                 break;
875                         device++;
876                 }
877                 if (device == SNDRV_RAWMIDI_DEVICES)
878                         device = -1;
879                 mutex_unlock(&register_mutex);
880                 if (put_user(device, (int __user *)argp))
881                         return -EFAULT;
882                 return 0;
883         }
884         case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
885         {
886                 int val;
887                 
888                 if (get_user(val, (int __user *)argp))
889                         return -EFAULT;
890                 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
891                 return 0;
892         }
893         case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
894                 return snd_rawmidi_info_select_user(card, argp);
895         }
896         return -ENOIOCTLCMD;
897 }
898
899 /**
900  * snd_rawmidi_receive - receive the input data from the device
901  * @substream: the rawmidi substream
902  * @buffer: the buffer pointer
903  * @count: the data size to read
904  *
905  * Reads the data from the internal buffer.
906  *
907  * Return: The size of read data, or a negative error code on failure.
908  */
909 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
910                         const unsigned char *buffer, int count)
911 {
912         unsigned long flags;
913         int result = 0, count1;
914         struct snd_rawmidi_runtime *runtime = substream->runtime;
915
916         if (!substream->opened)
917                 return -EBADFD;
918         if (runtime->buffer == NULL) {
919                 rmidi_dbg(substream->rmidi,
920                           "snd_rawmidi_receive: input is not active!!!\n");
921                 return -EINVAL;
922         }
923         spin_lock_irqsave(&runtime->lock, flags);
924         if (count == 1) {       /* special case, faster code */
925                 substream->bytes++;
926                 if (runtime->avail < runtime->buffer_size) {
927                         runtime->buffer[runtime->hw_ptr++] = buffer[0];
928                         runtime->hw_ptr %= runtime->buffer_size;
929                         runtime->avail++;
930                         result++;
931                 } else {
932                         runtime->xruns++;
933                 }
934         } else {
935                 substream->bytes += count;
936                 count1 = runtime->buffer_size - runtime->hw_ptr;
937                 if (count1 > count)
938                         count1 = count;
939                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
940                         count1 = runtime->buffer_size - runtime->avail;
941                 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
942                 runtime->hw_ptr += count1;
943                 runtime->hw_ptr %= runtime->buffer_size;
944                 runtime->avail += count1;
945                 count -= count1;
946                 result += count1;
947                 if (count > 0) {
948                         buffer += count1;
949                         count1 = count;
950                         if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
951                                 count1 = runtime->buffer_size - runtime->avail;
952                                 runtime->xruns += count - count1;
953                         }
954                         if (count1 > 0) {
955                                 memcpy(runtime->buffer, buffer, count1);
956                                 runtime->hw_ptr = count1;
957                                 runtime->avail += count1;
958                                 result += count1;
959                         }
960                 }
961         }
962         if (result > 0) {
963                 if (runtime->event)
964                         schedule_work(&runtime->event_work);
965                 else if (snd_rawmidi_ready(substream))
966                         wake_up(&runtime->sleep);
967         }
968         spin_unlock_irqrestore(&runtime->lock, flags);
969         return result;
970 }
971 EXPORT_SYMBOL(snd_rawmidi_receive);
972
973 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
974                                      unsigned char __user *userbuf,
975                                      unsigned char *kernelbuf, long count)
976 {
977         unsigned long flags;
978         long result = 0, count1;
979         struct snd_rawmidi_runtime *runtime = substream->runtime;
980         unsigned long appl_ptr;
981         int err = 0;
982
983         spin_lock_irqsave(&runtime->lock, flags);
984         snd_rawmidi_buffer_ref(runtime);
985         while (count > 0 && runtime->avail) {
986                 count1 = runtime->buffer_size - runtime->appl_ptr;
987                 if (count1 > count)
988                         count1 = count;
989                 if (count1 > (int)runtime->avail)
990                         count1 = runtime->avail;
991
992                 /* update runtime->appl_ptr before unlocking for userbuf */
993                 appl_ptr = runtime->appl_ptr;
994                 runtime->appl_ptr += count1;
995                 runtime->appl_ptr %= runtime->buffer_size;
996                 runtime->avail -= count1;
997
998                 if (kernelbuf)
999                         memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
1000                 if (userbuf) {
1001                         spin_unlock_irqrestore(&runtime->lock, flags);
1002                         if (copy_to_user(userbuf + result,
1003                                          runtime->buffer + appl_ptr, count1))
1004                                 err = -EFAULT;
1005                         spin_lock_irqsave(&runtime->lock, flags);
1006                         if (err)
1007                                 goto out;
1008                 }
1009                 result += count1;
1010                 count -= count1;
1011         }
1012  out:
1013         snd_rawmidi_buffer_unref(runtime);
1014         spin_unlock_irqrestore(&runtime->lock, flags);
1015         return result > 0 ? result : err;
1016 }
1017
1018 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1019                              unsigned char *buf, long count)
1020 {
1021         snd_rawmidi_input_trigger(substream, 1);
1022         return snd_rawmidi_kernel_read1(substream, NULL/*userbuf*/, buf, count);
1023 }
1024 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1025
1026 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1027                                 loff_t *offset)
1028 {
1029         long result;
1030         int count1;
1031         struct snd_rawmidi_file *rfile;
1032         struct snd_rawmidi_substream *substream;
1033         struct snd_rawmidi_runtime *runtime;
1034
1035         rfile = file->private_data;
1036         substream = rfile->input;
1037         if (substream == NULL)
1038                 return -EIO;
1039         runtime = substream->runtime;
1040         snd_rawmidi_input_trigger(substream, 1);
1041         result = 0;
1042         while (count > 0) {
1043                 spin_lock_irq(&runtime->lock);
1044                 while (!snd_rawmidi_ready(substream)) {
1045                         wait_queue_t wait;
1046                         if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1047                                 spin_unlock_irq(&runtime->lock);
1048                                 return result > 0 ? result : -EAGAIN;
1049                         }
1050                         init_waitqueue_entry(&wait, current);
1051                         add_wait_queue(&runtime->sleep, &wait);
1052                         set_current_state(TASK_INTERRUPTIBLE);
1053                         spin_unlock_irq(&runtime->lock);
1054                         schedule();
1055                         remove_wait_queue(&runtime->sleep, &wait);
1056                         if (rfile->rmidi->card->shutdown)
1057                                 return -ENODEV;
1058                         if (signal_pending(current))
1059                                 return result > 0 ? result : -ERESTARTSYS;
1060                         if (!runtime->avail)
1061                                 return result > 0 ? result : -EIO;
1062                         spin_lock_irq(&runtime->lock);
1063                 }
1064                 spin_unlock_irq(&runtime->lock);
1065                 count1 = snd_rawmidi_kernel_read1(substream,
1066                                                   (unsigned char __user *)buf,
1067                                                   NULL/*kernelbuf*/,
1068                                                   count);
1069                 if (count1 < 0)
1070                         return result > 0 ? result : count1;
1071                 result += count1;
1072                 buf += count1;
1073                 count -= count1;
1074         }
1075         return result;
1076 }
1077
1078 /**
1079  * snd_rawmidi_transmit_empty - check whether the output buffer is empty
1080  * @substream: the rawmidi substream
1081  *
1082  * Return: 1 if the internal output buffer is empty, 0 if not.
1083  */
1084 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1085 {
1086         struct snd_rawmidi_runtime *runtime = substream->runtime;
1087         int result;
1088         unsigned long flags;
1089
1090         if (runtime->buffer == NULL) {
1091                 rmidi_dbg(substream->rmidi,
1092                           "snd_rawmidi_transmit_empty: output is not active!!!\n");
1093                 return 1;
1094         }
1095         spin_lock_irqsave(&runtime->lock, flags);
1096         result = runtime->avail >= runtime->buffer_size;
1097         spin_unlock_irqrestore(&runtime->lock, flags);
1098         return result;          
1099 }
1100 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1101
1102 /**
1103  * __snd_rawmidi_transmit_peek - copy data from the internal buffer
1104  * @substream: the rawmidi substream
1105  * @buffer: the buffer pointer
1106  * @count: data size to transfer
1107  *
1108  * This is a variant of snd_rawmidi_transmit_peek() without spinlock.
1109  */
1110 int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1111                               unsigned char *buffer, int count)
1112 {
1113         int result, count1;
1114         struct snd_rawmidi_runtime *runtime = substream->runtime;
1115
1116         if (runtime->buffer == NULL) {
1117                 rmidi_dbg(substream->rmidi,
1118                           "snd_rawmidi_transmit_peek: output is not active!!!\n");
1119                 return -EINVAL;
1120         }
1121         result = 0;
1122         if (runtime->avail >= runtime->buffer_size) {
1123                 /* warning: lowlevel layer MUST trigger down the hardware */
1124                 goto __skip;
1125         }
1126         if (count == 1) {       /* special case, faster code */
1127                 *buffer = runtime->buffer[runtime->hw_ptr];
1128                 result++;
1129         } else {
1130                 count1 = runtime->buffer_size - runtime->hw_ptr;
1131                 if (count1 > count)
1132                         count1 = count;
1133                 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1134                         count1 = runtime->buffer_size - runtime->avail;
1135                 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1136                 count -= count1;
1137                 result += count1;
1138                 if (count > 0) {
1139                         if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1140                                 count = runtime->buffer_size - runtime->avail - count1;
1141                         memcpy(buffer + count1, runtime->buffer, count);
1142                         result += count;
1143                 }
1144         }
1145       __skip:
1146         return result;
1147 }
1148 EXPORT_SYMBOL(__snd_rawmidi_transmit_peek);
1149
1150 /**
1151  * snd_rawmidi_transmit_peek - copy data from the internal buffer
1152  * @substream: the rawmidi substream
1153  * @buffer: the buffer pointer
1154  * @count: data size to transfer
1155  *
1156  * Copies data from the internal output buffer to the given buffer.
1157  *
1158  * Call this in the interrupt handler when the midi output is ready,
1159  * and call snd_rawmidi_transmit_ack() after the transmission is
1160  * finished.
1161  *
1162  * Return: The size of copied data, or a negative error code on failure.
1163  */
1164 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1165                               unsigned char *buffer, int count)
1166 {
1167         struct snd_rawmidi_runtime *runtime = substream->runtime;
1168         int result;
1169         unsigned long flags;
1170
1171         spin_lock_irqsave(&runtime->lock, flags);
1172         result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1173         spin_unlock_irqrestore(&runtime->lock, flags);
1174         return result;
1175 }
1176 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1177
1178 /**
1179  * __snd_rawmidi_transmit_ack - acknowledge the transmission
1180  * @substream: the rawmidi substream
1181  * @count: the transferred count
1182  *
1183  * This is a variant of __snd_rawmidi_transmit_ack() without spinlock.
1184  */
1185 int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1186 {
1187         struct snd_rawmidi_runtime *runtime = substream->runtime;
1188
1189         if (runtime->buffer == NULL) {
1190                 rmidi_dbg(substream->rmidi,
1191                           "snd_rawmidi_transmit_ack: output is not active!!!\n");
1192                 return -EINVAL;
1193         }
1194         snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1195         runtime->hw_ptr += count;
1196         runtime->hw_ptr %= runtime->buffer_size;
1197         runtime->avail += count;
1198         substream->bytes += count;
1199         if (count > 0) {
1200                 if (runtime->drain || snd_rawmidi_ready(substream))
1201                         wake_up(&runtime->sleep);
1202         }
1203         return count;
1204 }
1205 EXPORT_SYMBOL(__snd_rawmidi_transmit_ack);
1206
1207 /**
1208  * snd_rawmidi_transmit_ack - acknowledge the transmission
1209  * @substream: the rawmidi substream
1210  * @count: the transferred count
1211  *
1212  * Advances the hardware pointer for the internal output buffer with
1213  * the given size and updates the condition.
1214  * Call after the transmission is finished.
1215  *
1216  * Return: The advanced size if successful, or a negative error code on failure.
1217  */
1218 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1219 {
1220         struct snd_rawmidi_runtime *runtime = substream->runtime;
1221         int result;
1222         unsigned long flags;
1223
1224         spin_lock_irqsave(&runtime->lock, flags);
1225         result = __snd_rawmidi_transmit_ack(substream, count);
1226         spin_unlock_irqrestore(&runtime->lock, flags);
1227         return result;
1228 }
1229 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1230
1231 /**
1232  * snd_rawmidi_transmit - copy from the buffer to the device
1233  * @substream: the rawmidi substream
1234  * @buffer: the buffer pointer
1235  * @count: the data size to transfer
1236  * 
1237  * Copies data from the buffer to the device and advances the pointer.
1238  *
1239  * Return: The copied size if successful, or a negative error code on failure.
1240  */
1241 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1242                          unsigned char *buffer, int count)
1243 {
1244         struct snd_rawmidi_runtime *runtime = substream->runtime;
1245         int result;
1246         unsigned long flags;
1247
1248         spin_lock_irqsave(&runtime->lock, flags);
1249         if (!substream->opened)
1250                 result = -EBADFD;
1251         else {
1252                 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1253                 if (count <= 0)
1254                         result = count;
1255                 else
1256                         result = __snd_rawmidi_transmit_ack(substream, count);
1257         }
1258         spin_unlock_irqrestore(&runtime->lock, flags);
1259         return result;
1260 }
1261 EXPORT_SYMBOL(snd_rawmidi_transmit);
1262
1263 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1264                                       const unsigned char __user *userbuf,
1265                                       const unsigned char *kernelbuf,
1266                                       long count)
1267 {
1268         unsigned long flags;
1269         long count1, result;
1270         struct snd_rawmidi_runtime *runtime = substream->runtime;
1271         unsigned long appl_ptr;
1272
1273         if (!kernelbuf && !userbuf)
1274                 return -EINVAL;
1275         if (snd_BUG_ON(!runtime->buffer))
1276                 return -EINVAL;
1277
1278         result = 0;
1279         spin_lock_irqsave(&runtime->lock, flags);
1280         if (substream->append) {
1281                 if ((long)runtime->avail < count) {
1282                         spin_unlock_irqrestore(&runtime->lock, flags);
1283                         return -EAGAIN;
1284                 }
1285         }
1286         snd_rawmidi_buffer_ref(runtime);
1287         while (count > 0 && runtime->avail > 0) {
1288                 count1 = runtime->buffer_size - runtime->appl_ptr;
1289                 if (count1 > count)
1290                         count1 = count;
1291                 if (count1 > (long)runtime->avail)
1292                         count1 = runtime->avail;
1293
1294                 /* update runtime->appl_ptr before unlocking for userbuf */
1295                 appl_ptr = runtime->appl_ptr;
1296                 runtime->appl_ptr += count1;
1297                 runtime->appl_ptr %= runtime->buffer_size;
1298                 runtime->avail -= count1;
1299
1300                 if (kernelbuf)
1301                         memcpy(runtime->buffer + appl_ptr,
1302                                kernelbuf + result, count1);
1303                 else if (userbuf) {
1304                         spin_unlock_irqrestore(&runtime->lock, flags);
1305                         if (copy_from_user(runtime->buffer + appl_ptr,
1306                                            userbuf + result, count1)) {
1307                                 spin_lock_irqsave(&runtime->lock, flags);
1308                                 result = result > 0 ? result : -EFAULT;
1309                                 goto __end;
1310                         }
1311                         spin_lock_irqsave(&runtime->lock, flags);
1312                 }
1313                 result += count1;
1314                 count -= count1;
1315         }
1316       __end:
1317         count1 = runtime->avail < runtime->buffer_size;
1318         snd_rawmidi_buffer_unref(runtime);
1319         spin_unlock_irqrestore(&runtime->lock, flags);
1320         if (count1)
1321                 snd_rawmidi_output_trigger(substream, 1);
1322         return result;
1323 }
1324
1325 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1326                               const unsigned char *buf, long count)
1327 {
1328         return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1329 }
1330 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1331
1332 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1333                                  size_t count, loff_t *offset)
1334 {
1335         long result, timeout;
1336         int count1;
1337         struct snd_rawmidi_file *rfile;
1338         struct snd_rawmidi_runtime *runtime;
1339         struct snd_rawmidi_substream *substream;
1340
1341         rfile = file->private_data;
1342         substream = rfile->output;
1343         runtime = substream->runtime;
1344         /* we cannot put an atomic message to our buffer */
1345         if (substream->append && count > runtime->buffer_size)
1346                 return -EIO;
1347         result = 0;
1348         while (count > 0) {
1349                 spin_lock_irq(&runtime->lock);
1350                 while (!snd_rawmidi_ready_append(substream, count)) {
1351                         wait_queue_t wait;
1352                         if (file->f_flags & O_NONBLOCK) {
1353                                 spin_unlock_irq(&runtime->lock);
1354                                 return result > 0 ? result : -EAGAIN;
1355                         }
1356                         init_waitqueue_entry(&wait, current);
1357                         add_wait_queue(&runtime->sleep, &wait);
1358                         set_current_state(TASK_INTERRUPTIBLE);
1359                         spin_unlock_irq(&runtime->lock);
1360                         timeout = schedule_timeout(30 * HZ);
1361                         remove_wait_queue(&runtime->sleep, &wait);
1362                         if (rfile->rmidi->card->shutdown)
1363                                 return -ENODEV;
1364                         if (signal_pending(current))
1365                                 return result > 0 ? result : -ERESTARTSYS;
1366                         if (!runtime->avail && !timeout)
1367                                 return result > 0 ? result : -EIO;
1368                         spin_lock_irq(&runtime->lock);
1369                 }
1370                 spin_unlock_irq(&runtime->lock);
1371                 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1372                 if (count1 < 0)
1373                         return result > 0 ? result : count1;
1374                 result += count1;
1375                 buf += count1;
1376                 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1377                         break;
1378                 count -= count1;
1379         }
1380         if (file->f_flags & O_DSYNC) {
1381                 spin_lock_irq(&runtime->lock);
1382                 while (runtime->avail != runtime->buffer_size) {
1383                         wait_queue_t wait;
1384                         unsigned int last_avail = runtime->avail;
1385                         init_waitqueue_entry(&wait, current);
1386                         add_wait_queue(&runtime->sleep, &wait);
1387                         set_current_state(TASK_INTERRUPTIBLE);
1388                         spin_unlock_irq(&runtime->lock);
1389                         timeout = schedule_timeout(30 * HZ);
1390                         remove_wait_queue(&runtime->sleep, &wait);
1391                         if (signal_pending(current))
1392                                 return result > 0 ? result : -ERESTARTSYS;
1393                         if (runtime->avail == last_avail && !timeout)
1394                                 return result > 0 ? result : -EIO;
1395                         spin_lock_irq(&runtime->lock);
1396                 }
1397                 spin_unlock_irq(&runtime->lock);
1398         }
1399         return result;
1400 }
1401
1402 static unsigned int snd_rawmidi_poll(struct file *file, poll_table * wait)
1403 {
1404         struct snd_rawmidi_file *rfile;
1405         struct snd_rawmidi_runtime *runtime;
1406         unsigned int mask;
1407
1408         rfile = file->private_data;
1409         if (rfile->input != NULL) {
1410                 runtime = rfile->input->runtime;
1411                 snd_rawmidi_input_trigger(rfile->input, 1);
1412                 poll_wait(file, &runtime->sleep, wait);
1413         }
1414         if (rfile->output != NULL) {
1415                 runtime = rfile->output->runtime;
1416                 poll_wait(file, &runtime->sleep, wait);
1417         }
1418         mask = 0;
1419         if (rfile->input != NULL) {
1420                 if (snd_rawmidi_ready(rfile->input))
1421                         mask |= POLLIN | POLLRDNORM;
1422         }
1423         if (rfile->output != NULL) {
1424                 if (snd_rawmidi_ready(rfile->output))
1425                         mask |= POLLOUT | POLLWRNORM;
1426         }
1427         return mask;
1428 }
1429
1430 /*
1431  */
1432 #ifdef CONFIG_COMPAT
1433 #include "rawmidi_compat.c"
1434 #else
1435 #define snd_rawmidi_ioctl_compat        NULL
1436 #endif
1437
1438 /*
1439
1440  */
1441
1442 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1443                                        struct snd_info_buffer *buffer)
1444 {
1445         struct snd_rawmidi *rmidi;
1446         struct snd_rawmidi_substream *substream;
1447         struct snd_rawmidi_runtime *runtime;
1448
1449         rmidi = entry->private_data;
1450         snd_iprintf(buffer, "%s\n\n", rmidi->name);
1451         mutex_lock(&rmidi->open_mutex);
1452         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1453                 list_for_each_entry(substream,
1454                                     &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1455                                     list) {
1456                         snd_iprintf(buffer,
1457                                     "Output %d\n"
1458                                     "  Tx bytes     : %lu\n",
1459                                     substream->number,
1460                                     (unsigned long) substream->bytes);
1461                         if (substream->opened) {
1462                                 snd_iprintf(buffer,
1463                                     "  Owner PID    : %d\n",
1464                                     pid_vnr(substream->pid));
1465                                 runtime = substream->runtime;
1466                                 snd_iprintf(buffer,
1467                                     "  Mode         : %s\n"
1468                                     "  Buffer size  : %lu\n"
1469                                     "  Avail        : %lu\n",
1470                                     runtime->oss ? "OSS compatible" : "native",
1471                                     (unsigned long) runtime->buffer_size,
1472                                     (unsigned long) runtime->avail);
1473                         }
1474                 }
1475         }
1476         if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1477                 list_for_each_entry(substream,
1478                                     &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1479                                     list) {
1480                         snd_iprintf(buffer,
1481                                     "Input %d\n"
1482                                     "  Rx bytes     : %lu\n",
1483                                     substream->number,
1484                                     (unsigned long) substream->bytes);
1485                         if (substream->opened) {
1486                                 snd_iprintf(buffer,
1487                                             "  Owner PID    : %d\n",
1488                                             pid_vnr(substream->pid));
1489                                 runtime = substream->runtime;
1490                                 snd_iprintf(buffer,
1491                                             "  Buffer size  : %lu\n"
1492                                             "  Avail        : %lu\n"
1493                                             "  Overruns     : %lu\n",
1494                                             (unsigned long) runtime->buffer_size,
1495                                             (unsigned long) runtime->avail,
1496                                             (unsigned long) runtime->xruns);
1497                         }
1498                 }
1499         }
1500         mutex_unlock(&rmidi->open_mutex);
1501 }
1502
1503 /*
1504  *  Register functions
1505  */
1506
1507 static const struct file_operations snd_rawmidi_f_ops =
1508 {
1509         .owner =        THIS_MODULE,
1510         .read =         snd_rawmidi_read,
1511         .write =        snd_rawmidi_write,
1512         .open =         snd_rawmidi_open,
1513         .release =      snd_rawmidi_release,
1514         .llseek =       no_llseek,
1515         .poll =         snd_rawmidi_poll,
1516         .unlocked_ioctl =       snd_rawmidi_ioctl,
1517         .compat_ioctl = snd_rawmidi_ioctl_compat,
1518 };
1519
1520 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1521                                         struct snd_rawmidi_str *stream,
1522                                         int direction,
1523                                         int count)
1524 {
1525         struct snd_rawmidi_substream *substream;
1526         int idx;
1527
1528         for (idx = 0; idx < count; idx++) {
1529                 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1530                 if (!substream)
1531                         return -ENOMEM;
1532                 substream->stream = direction;
1533                 substream->number = idx;
1534                 substream->rmidi = rmidi;
1535                 substream->pstr = stream;
1536                 list_add_tail(&substream->list, &stream->substreams);
1537                 stream->substream_count++;
1538         }
1539         return 0;
1540 }
1541
1542 static void release_rawmidi_device(struct device *dev)
1543 {
1544         kfree(container_of(dev, struct snd_rawmidi, dev));
1545 }
1546
1547 /**
1548  * snd_rawmidi_new - create a rawmidi instance
1549  * @card: the card instance
1550  * @id: the id string
1551  * @device: the device index
1552  * @output_count: the number of output streams
1553  * @input_count: the number of input streams
1554  * @rrawmidi: the pointer to store the new rawmidi instance
1555  *
1556  * Creates a new rawmidi instance.
1557  * Use snd_rawmidi_set_ops() to set the operators to the new instance.
1558  *
1559  * Return: Zero if successful, or a negative error code on failure.
1560  */
1561 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1562                     int output_count, int input_count,
1563                     struct snd_rawmidi ** rrawmidi)
1564 {
1565         struct snd_rawmidi *rmidi;
1566         int err;
1567         static struct snd_device_ops ops = {
1568                 .dev_free = snd_rawmidi_dev_free,
1569                 .dev_register = snd_rawmidi_dev_register,
1570                 .dev_disconnect = snd_rawmidi_dev_disconnect,
1571         };
1572
1573         if (snd_BUG_ON(!card))
1574                 return -ENXIO;
1575         if (rrawmidi)
1576                 *rrawmidi = NULL;
1577         rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1578         if (!rmidi)
1579                 return -ENOMEM;
1580         rmidi->card = card;
1581         rmidi->device = device;
1582         mutex_init(&rmidi->open_mutex);
1583         init_waitqueue_head(&rmidi->open_wait);
1584         INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1585         INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1586
1587         if (id != NULL)
1588                 strlcpy(rmidi->id, id, sizeof(rmidi->id));
1589
1590         snd_device_initialize(&rmidi->dev, card);
1591         rmidi->dev.release = release_rawmidi_device;
1592         dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1593
1594         if ((err = snd_rawmidi_alloc_substreams(rmidi,
1595                                                 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1596                                                 SNDRV_RAWMIDI_STREAM_INPUT,
1597                                                 input_count)) < 0) {
1598                 snd_rawmidi_free(rmidi);
1599                 return err;
1600         }
1601         if ((err = snd_rawmidi_alloc_substreams(rmidi,
1602                                                 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1603                                                 SNDRV_RAWMIDI_STREAM_OUTPUT,
1604                                                 output_count)) < 0) {
1605                 snd_rawmidi_free(rmidi);
1606                 return err;
1607         }
1608         if ((err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops)) < 0) {
1609                 snd_rawmidi_free(rmidi);
1610                 return err;
1611         }
1612         if (rrawmidi)
1613                 *rrawmidi = rmidi;
1614         return 0;
1615 }
1616 EXPORT_SYMBOL(snd_rawmidi_new);
1617
1618 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1619 {
1620         struct snd_rawmidi_substream *substream;
1621
1622         while (!list_empty(&stream->substreams)) {
1623                 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1624                 list_del(&substream->list);
1625                 kfree(substream);
1626         }
1627 }
1628
1629 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1630 {
1631         if (!rmidi)
1632                 return 0;
1633
1634         snd_info_free_entry(rmidi->proc_entry);
1635         rmidi->proc_entry = NULL;
1636         mutex_lock(&register_mutex);
1637         if (rmidi->ops && rmidi->ops->dev_unregister)
1638                 rmidi->ops->dev_unregister(rmidi);
1639         mutex_unlock(&register_mutex);
1640
1641         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1642         snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1643         if (rmidi->private_free)
1644                 rmidi->private_free(rmidi);
1645         put_device(&rmidi->dev);
1646         return 0;
1647 }
1648
1649 static int snd_rawmidi_dev_free(struct snd_device *device)
1650 {
1651         struct snd_rawmidi *rmidi = device->device_data;
1652         return snd_rawmidi_free(rmidi);
1653 }
1654
1655 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1656 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1657 {
1658         struct snd_rawmidi *rmidi = device->private_data;
1659         rmidi->seq_dev = NULL;
1660 }
1661 #endif
1662
1663 static int snd_rawmidi_dev_register(struct snd_device *device)
1664 {
1665         int err;
1666         struct snd_info_entry *entry;
1667         char name[16];
1668         struct snd_rawmidi *rmidi = device->device_data;
1669
1670         if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1671                 return -ENOMEM;
1672         mutex_lock(&register_mutex);
1673         if (snd_rawmidi_search(rmidi->card, rmidi->device)) {
1674                 mutex_unlock(&register_mutex);
1675                 return -EBUSY;
1676         }
1677         list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1678         mutex_unlock(&register_mutex);
1679         err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1680                                   rmidi->card, rmidi->device,
1681                                   &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1682         if (err < 0) {
1683                 rmidi_err(rmidi, "unable to register\n");
1684                 mutex_lock(&register_mutex);
1685                 list_del(&rmidi->list);
1686                 mutex_unlock(&register_mutex);
1687                 return err;
1688         }
1689         if (rmidi->ops && rmidi->ops->dev_register &&
1690             (err = rmidi->ops->dev_register(rmidi)) < 0) {
1691                 snd_unregister_device(&rmidi->dev);
1692                 mutex_lock(&register_mutex);
1693                 list_del(&rmidi->list);
1694                 mutex_unlock(&register_mutex);
1695                 return err;
1696         }
1697 #ifdef CONFIG_SND_OSSEMUL
1698         rmidi->ossreg = 0;
1699         if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1700                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1701                                             rmidi->card, 0, &snd_rawmidi_f_ops,
1702                                             rmidi) < 0) {
1703                         rmidi_err(rmidi,
1704                                   "unable to register OSS rawmidi device %i:%i\n",
1705                                   rmidi->card->number, 0);
1706                 } else {
1707                         rmidi->ossreg++;
1708 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1709                         snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1710 #endif
1711                 }
1712         }
1713         if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1714                 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1715                                             rmidi->card, 1, &snd_rawmidi_f_ops,
1716                                             rmidi) < 0) {
1717                         rmidi_err(rmidi,
1718                                   "unable to register OSS rawmidi device %i:%i\n",
1719                                   rmidi->card->number, 1);
1720                 } else {
1721                         rmidi->ossreg++;
1722                 }
1723         }
1724 #endif /* CONFIG_SND_OSSEMUL */
1725         sprintf(name, "midi%d", rmidi->device);
1726         entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1727         if (entry) {
1728                 entry->private_data = rmidi;
1729                 entry->c.text.read = snd_rawmidi_proc_info_read;
1730                 if (snd_info_register(entry) < 0) {
1731                         snd_info_free_entry(entry);
1732                         entry = NULL;
1733                 }
1734         }
1735         rmidi->proc_entry = entry;
1736 #if defined(CONFIG_SND_SEQUENCER) || (defined(MODULE) && defined(CONFIG_SND_SEQUENCER_MODULE))
1737         if (!rmidi->ops || !rmidi->ops->dev_register) { /* own registration mechanism */
1738                 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
1739                         rmidi->seq_dev->private_data = rmidi;
1740                         rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
1741                         sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
1742                         snd_device_register(rmidi->card, rmidi->seq_dev);
1743                 }
1744         }
1745 #endif
1746         return 0;
1747 }
1748
1749 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
1750 {
1751         struct snd_rawmidi *rmidi = device->device_data;
1752         int dir;
1753
1754         mutex_lock(&register_mutex);
1755         mutex_lock(&rmidi->open_mutex);
1756         wake_up(&rmidi->open_wait);
1757         list_del_init(&rmidi->list);
1758         for (dir = 0; dir < 2; dir++) {
1759                 struct snd_rawmidi_substream *s;
1760                 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
1761                         if (s->runtime)
1762                                 wake_up(&s->runtime->sleep);
1763                 }
1764         }
1765
1766 #ifdef CONFIG_SND_OSSEMUL
1767         if (rmidi->ossreg) {
1768                 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1769                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
1770 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1771                         snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
1772 #endif
1773                 }
1774                 if ((int)rmidi->device == amidi_map[rmidi->card->number])
1775                         snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
1776                 rmidi->ossreg = 0;
1777         }
1778 #endif /* CONFIG_SND_OSSEMUL */
1779         snd_unregister_device(&rmidi->dev);
1780         mutex_unlock(&rmidi->open_mutex);
1781         mutex_unlock(&register_mutex);
1782         return 0;
1783 }
1784
1785 /**
1786  * snd_rawmidi_set_ops - set the rawmidi operators
1787  * @rmidi: the rawmidi instance
1788  * @stream: the stream direction, SNDRV_RAWMIDI_STREAM_XXX
1789  * @ops: the operator table
1790  *
1791  * Sets the rawmidi operators for the given stream direction.
1792  */
1793 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
1794                          struct snd_rawmidi_ops *ops)
1795 {
1796         struct snd_rawmidi_substream *substream;
1797         
1798         list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
1799                 substream->ops = ops;
1800 }
1801 EXPORT_SYMBOL(snd_rawmidi_set_ops);
1802
1803 /*
1804  *  ENTRY functions
1805  */
1806
1807 static int __init alsa_rawmidi_init(void)
1808 {
1809
1810         snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
1811         snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
1812 #ifdef CONFIG_SND_OSSEMUL
1813         { int i;
1814         /* check device map table */
1815         for (i = 0; i < SNDRV_CARDS; i++) {
1816                 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1817                         pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
1818                                i, midi_map[i]);
1819                         midi_map[i] = 0;
1820                 }
1821                 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
1822                         pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
1823                                i, amidi_map[i]);
1824                         amidi_map[i] = 1;
1825                 }
1826         }
1827         }
1828 #endif /* CONFIG_SND_OSSEMUL */
1829         return 0;
1830 }
1831
1832 static void __exit alsa_rawmidi_exit(void)
1833 {
1834         snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
1835         snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
1836 }
1837
1838 module_init(alsa_rawmidi_init)
1839 module_exit(alsa_rawmidi_exit)