~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/sound/core/rawmidi.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp