0001
0002
0003
0004
0005
0006
0007 #include <sound/core.h>
0008 #include <linux/major.h>
0009 #include <linux/init.h>
0010 #include <linux/sched/signal.h>
0011 #include <linux/slab.h>
0012 #include <linux/time.h>
0013 #include <linux/wait.h>
0014 #include <linux/mutex.h>
0015 #include <linux/module.h>
0016 #include <linux/delay.h>
0017 #include <linux/mm.h>
0018 #include <linux/nospec.h>
0019 #include <sound/rawmidi.h>
0020 #include <sound/info.h>
0021 #include <sound/control.h>
0022 #include <sound/minors.h>
0023 #include <sound/initval.h>
0024
0025 MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
0026 MODULE_DESCRIPTION("Midlevel RawMidi code for ALSA.");
0027 MODULE_LICENSE("GPL");
0028
0029 #ifdef CONFIG_SND_OSSEMUL
0030 static int midi_map[SNDRV_CARDS];
0031 static int amidi_map[SNDRV_CARDS] = {[0 ... (SNDRV_CARDS-1)] = 1};
0032 module_param_array(midi_map, int, NULL, 0444);
0033 MODULE_PARM_DESC(midi_map, "Raw MIDI device number assigned to 1st OSS device.");
0034 module_param_array(amidi_map, int, NULL, 0444);
0035 MODULE_PARM_DESC(amidi_map, "Raw MIDI device number assigned to 2nd OSS device.");
0036 #endif
0037
0038 static int snd_rawmidi_free(struct snd_rawmidi *rmidi);
0039 static int snd_rawmidi_dev_free(struct snd_device *device);
0040 static int snd_rawmidi_dev_register(struct snd_device *device);
0041 static int snd_rawmidi_dev_disconnect(struct snd_device *device);
0042
0043 static LIST_HEAD(snd_rawmidi_devices);
0044 static DEFINE_MUTEX(register_mutex);
0045
0046 #define rmidi_err(rmidi, fmt, args...) \
0047 dev_err(&(rmidi)->dev, fmt, ##args)
0048 #define rmidi_warn(rmidi, fmt, args...) \
0049 dev_warn(&(rmidi)->dev, fmt, ##args)
0050 #define rmidi_dbg(rmidi, fmt, args...) \
0051 dev_dbg(&(rmidi)->dev, fmt, ##args)
0052
0053 struct snd_rawmidi_status32 {
0054 s32 stream;
0055 s32 tstamp_sec;
0056 s32 tstamp_nsec;
0057 u32 avail;
0058 u32 xruns;
0059 unsigned char reserved[16];
0060 };
0061
0062 #define SNDRV_RAWMIDI_IOCTL_STATUS32 _IOWR('W', 0x20, struct snd_rawmidi_status32)
0063
0064 struct snd_rawmidi_status64 {
0065 int stream;
0066 u8 rsvd[4];
0067 s64 tstamp_sec;
0068 s64 tstamp_nsec;
0069 size_t avail;
0070 size_t xruns;
0071 unsigned char reserved[16];
0072 };
0073
0074 #define SNDRV_RAWMIDI_IOCTL_STATUS64 _IOWR('W', 0x20, struct snd_rawmidi_status64)
0075
0076 static struct snd_rawmidi *snd_rawmidi_search(struct snd_card *card, int device)
0077 {
0078 struct snd_rawmidi *rawmidi;
0079
0080 list_for_each_entry(rawmidi, &snd_rawmidi_devices, list)
0081 if (rawmidi->card == card && rawmidi->device == device)
0082 return rawmidi;
0083 return NULL;
0084 }
0085
0086 static inline unsigned short snd_rawmidi_file_flags(struct file *file)
0087 {
0088 switch (file->f_mode & (FMODE_READ | FMODE_WRITE)) {
0089 case FMODE_WRITE:
0090 return SNDRV_RAWMIDI_LFLG_OUTPUT;
0091 case FMODE_READ:
0092 return SNDRV_RAWMIDI_LFLG_INPUT;
0093 default:
0094 return SNDRV_RAWMIDI_LFLG_OPEN;
0095 }
0096 }
0097
0098 static inline bool __snd_rawmidi_ready(struct snd_rawmidi_runtime *runtime)
0099 {
0100 return runtime->avail >= runtime->avail_min;
0101 }
0102
0103 static bool snd_rawmidi_ready(struct snd_rawmidi_substream *substream)
0104 {
0105 unsigned long flags;
0106 bool ready;
0107
0108 spin_lock_irqsave(&substream->lock, flags);
0109 ready = __snd_rawmidi_ready(substream->runtime);
0110 spin_unlock_irqrestore(&substream->lock, flags);
0111 return ready;
0112 }
0113
0114 static inline int snd_rawmidi_ready_append(struct snd_rawmidi_substream *substream,
0115 size_t count)
0116 {
0117 struct snd_rawmidi_runtime *runtime = substream->runtime;
0118
0119 return runtime->avail >= runtime->avail_min &&
0120 (!substream->append || runtime->avail >= count);
0121 }
0122
0123 static void snd_rawmidi_input_event_work(struct work_struct *work)
0124 {
0125 struct snd_rawmidi_runtime *runtime =
0126 container_of(work, struct snd_rawmidi_runtime, event_work);
0127
0128 if (runtime->event)
0129 runtime->event(runtime->substream);
0130 }
0131
0132
0133 static inline void snd_rawmidi_buffer_ref(struct snd_rawmidi_runtime *runtime)
0134 {
0135 runtime->buffer_ref++;
0136 }
0137
0138 static inline void snd_rawmidi_buffer_unref(struct snd_rawmidi_runtime *runtime)
0139 {
0140 runtime->buffer_ref--;
0141 }
0142
0143 static void snd_rawmidi_buffer_ref_sync(struct snd_rawmidi_substream *substream)
0144 {
0145 int loop = HZ;
0146
0147 spin_lock_irq(&substream->lock);
0148 while (substream->runtime->buffer_ref) {
0149 spin_unlock_irq(&substream->lock);
0150 if (!--loop) {
0151 rmidi_err(substream->rmidi, "Buffer ref sync timeout\n");
0152 return;
0153 }
0154 schedule_timeout_uninterruptible(1);
0155 spin_lock_irq(&substream->lock);
0156 }
0157 spin_unlock_irq(&substream->lock);
0158 }
0159
0160 static int snd_rawmidi_runtime_create(struct snd_rawmidi_substream *substream)
0161 {
0162 struct snd_rawmidi_runtime *runtime;
0163
0164 runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
0165 if (!runtime)
0166 return -ENOMEM;
0167 runtime->substream = substream;
0168 init_waitqueue_head(&runtime->sleep);
0169 INIT_WORK(&runtime->event_work, snd_rawmidi_input_event_work);
0170 runtime->event = NULL;
0171 runtime->buffer_size = PAGE_SIZE;
0172 runtime->avail_min = 1;
0173 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
0174 runtime->avail = 0;
0175 else
0176 runtime->avail = runtime->buffer_size;
0177 runtime->buffer = kvzalloc(runtime->buffer_size, GFP_KERNEL);
0178 if (!runtime->buffer) {
0179 kfree(runtime);
0180 return -ENOMEM;
0181 }
0182 runtime->appl_ptr = runtime->hw_ptr = 0;
0183 substream->runtime = runtime;
0184 return 0;
0185 }
0186
0187 static int snd_rawmidi_runtime_free(struct snd_rawmidi_substream *substream)
0188 {
0189 struct snd_rawmidi_runtime *runtime = substream->runtime;
0190
0191 kvfree(runtime->buffer);
0192 kfree(runtime);
0193 substream->runtime = NULL;
0194 return 0;
0195 }
0196
0197 static inline void snd_rawmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
0198 {
0199 if (!substream->opened)
0200 return;
0201 substream->ops->trigger(substream, up);
0202 }
0203
0204 static void snd_rawmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
0205 {
0206 if (!substream->opened)
0207 return;
0208 substream->ops->trigger(substream, up);
0209 if (!up)
0210 cancel_work_sync(&substream->runtime->event_work);
0211 }
0212
0213 static void __reset_runtime_ptrs(struct snd_rawmidi_runtime *runtime,
0214 bool is_input)
0215 {
0216 runtime->drain = 0;
0217 runtime->appl_ptr = runtime->hw_ptr = 0;
0218 runtime->avail = is_input ? 0 : runtime->buffer_size;
0219 }
0220
0221 static void reset_runtime_ptrs(struct snd_rawmidi_substream *substream,
0222 bool is_input)
0223 {
0224 unsigned long flags;
0225
0226 spin_lock_irqsave(&substream->lock, flags);
0227 if (substream->opened && substream->runtime)
0228 __reset_runtime_ptrs(substream->runtime, is_input);
0229 spin_unlock_irqrestore(&substream->lock, flags);
0230 }
0231
0232 int snd_rawmidi_drop_output(struct snd_rawmidi_substream *substream)
0233 {
0234 snd_rawmidi_output_trigger(substream, 0);
0235 reset_runtime_ptrs(substream, false);
0236 return 0;
0237 }
0238 EXPORT_SYMBOL(snd_rawmidi_drop_output);
0239
0240 int snd_rawmidi_drain_output(struct snd_rawmidi_substream *substream)
0241 {
0242 int err = 0;
0243 long timeout;
0244 struct snd_rawmidi_runtime *runtime;
0245
0246 spin_lock_irq(&substream->lock);
0247 runtime = substream->runtime;
0248 if (!substream->opened || !runtime || !runtime->buffer) {
0249 err = -EINVAL;
0250 } else {
0251 snd_rawmidi_buffer_ref(runtime);
0252 runtime->drain = 1;
0253 }
0254 spin_unlock_irq(&substream->lock);
0255 if (err < 0)
0256 return err;
0257
0258 timeout = wait_event_interruptible_timeout(runtime->sleep,
0259 (runtime->avail >= runtime->buffer_size),
0260 10*HZ);
0261
0262 spin_lock_irq(&substream->lock);
0263 if (signal_pending(current))
0264 err = -ERESTARTSYS;
0265 if (runtime->avail < runtime->buffer_size && !timeout) {
0266 rmidi_warn(substream->rmidi,
0267 "rawmidi drain error (avail = %li, buffer_size = %li)\n",
0268 (long)runtime->avail, (long)runtime->buffer_size);
0269 err = -EIO;
0270 }
0271 runtime->drain = 0;
0272 spin_unlock_irq(&substream->lock);
0273
0274 if (err != -ERESTARTSYS) {
0275
0276 if (substream->ops->drain)
0277 substream->ops->drain(substream);
0278 else
0279 msleep(50);
0280 snd_rawmidi_drop_output(substream);
0281 }
0282
0283 spin_lock_irq(&substream->lock);
0284 snd_rawmidi_buffer_unref(runtime);
0285 spin_unlock_irq(&substream->lock);
0286
0287 return err;
0288 }
0289 EXPORT_SYMBOL(snd_rawmidi_drain_output);
0290
0291 int snd_rawmidi_drain_input(struct snd_rawmidi_substream *substream)
0292 {
0293 snd_rawmidi_input_trigger(substream, 0);
0294 reset_runtime_ptrs(substream, true);
0295 return 0;
0296 }
0297 EXPORT_SYMBOL(snd_rawmidi_drain_input);
0298
0299
0300
0301
0302 static int assign_substream(struct snd_rawmidi *rmidi, int subdevice,
0303 int stream, int mode,
0304 struct snd_rawmidi_substream **sub_ret)
0305 {
0306 struct snd_rawmidi_substream *substream;
0307 struct snd_rawmidi_str *s = &rmidi->streams[stream];
0308 static const unsigned int info_flags[2] = {
0309 [SNDRV_RAWMIDI_STREAM_OUTPUT] = SNDRV_RAWMIDI_INFO_OUTPUT,
0310 [SNDRV_RAWMIDI_STREAM_INPUT] = SNDRV_RAWMIDI_INFO_INPUT,
0311 };
0312
0313 if (!(rmidi->info_flags & info_flags[stream]))
0314 return -ENXIO;
0315 if (subdevice >= 0 && subdevice >= s->substream_count)
0316 return -ENODEV;
0317
0318 list_for_each_entry(substream, &s->substreams, list) {
0319 if (substream->opened) {
0320 if (stream == SNDRV_RAWMIDI_STREAM_INPUT ||
0321 !(mode & SNDRV_RAWMIDI_LFLG_APPEND) ||
0322 !substream->append)
0323 continue;
0324 }
0325 if (subdevice < 0 || subdevice == substream->number) {
0326 *sub_ret = substream;
0327 return 0;
0328 }
0329 }
0330 return -EAGAIN;
0331 }
0332
0333
0334 static int open_substream(struct snd_rawmidi *rmidi,
0335 struct snd_rawmidi_substream *substream,
0336 int mode)
0337 {
0338 int err;
0339
0340 if (substream->use_count == 0) {
0341 err = snd_rawmidi_runtime_create(substream);
0342 if (err < 0)
0343 return err;
0344 err = substream->ops->open(substream);
0345 if (err < 0) {
0346 snd_rawmidi_runtime_free(substream);
0347 return err;
0348 }
0349 spin_lock_irq(&substream->lock);
0350 substream->opened = 1;
0351 substream->active_sensing = 0;
0352 if (mode & SNDRV_RAWMIDI_LFLG_APPEND)
0353 substream->append = 1;
0354 substream->pid = get_pid(task_pid(current));
0355 rmidi->streams[substream->stream].substream_opened++;
0356 spin_unlock_irq(&substream->lock);
0357 }
0358 substream->use_count++;
0359 return 0;
0360 }
0361
0362 static void close_substream(struct snd_rawmidi *rmidi,
0363 struct snd_rawmidi_substream *substream,
0364 int cleanup);
0365
0366 static int rawmidi_open_priv(struct snd_rawmidi *rmidi, int subdevice, int mode,
0367 struct snd_rawmidi_file *rfile)
0368 {
0369 struct snd_rawmidi_substream *sinput = NULL, *soutput = NULL;
0370 int err;
0371
0372 rfile->input = rfile->output = NULL;
0373 if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
0374 err = assign_substream(rmidi, subdevice,
0375 SNDRV_RAWMIDI_STREAM_INPUT,
0376 mode, &sinput);
0377 if (err < 0)
0378 return err;
0379 }
0380 if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
0381 err = assign_substream(rmidi, subdevice,
0382 SNDRV_RAWMIDI_STREAM_OUTPUT,
0383 mode, &soutput);
0384 if (err < 0)
0385 return err;
0386 }
0387
0388 if (sinput) {
0389 err = open_substream(rmidi, sinput, mode);
0390 if (err < 0)
0391 return err;
0392 }
0393 if (soutput) {
0394 err = open_substream(rmidi, soutput, mode);
0395 if (err < 0) {
0396 if (sinput)
0397 close_substream(rmidi, sinput, 0);
0398 return err;
0399 }
0400 }
0401
0402 rfile->rmidi = rmidi;
0403 rfile->input = sinput;
0404 rfile->output = soutput;
0405 return 0;
0406 }
0407
0408
0409 int snd_rawmidi_kernel_open(struct snd_card *card, int device, int subdevice,
0410 int mode, struct snd_rawmidi_file *rfile)
0411 {
0412 struct snd_rawmidi *rmidi;
0413 int err = 0;
0414
0415 if (snd_BUG_ON(!rfile))
0416 return -EINVAL;
0417
0418 mutex_lock(®ister_mutex);
0419 rmidi = snd_rawmidi_search(card, device);
0420 if (!rmidi)
0421 err = -ENODEV;
0422 else if (!try_module_get(rmidi->card->module))
0423 err = -ENXIO;
0424 mutex_unlock(®ister_mutex);
0425 if (err < 0)
0426 return err;
0427
0428 mutex_lock(&rmidi->open_mutex);
0429 err = rawmidi_open_priv(rmidi, subdevice, mode, rfile);
0430 mutex_unlock(&rmidi->open_mutex);
0431 if (err < 0)
0432 module_put(rmidi->card->module);
0433 return err;
0434 }
0435 EXPORT_SYMBOL(snd_rawmidi_kernel_open);
0436
0437 static int snd_rawmidi_open(struct inode *inode, struct file *file)
0438 {
0439 int maj = imajor(inode);
0440 struct snd_card *card;
0441 int subdevice;
0442 unsigned short fflags;
0443 int err;
0444 struct snd_rawmidi *rmidi;
0445 struct snd_rawmidi_file *rawmidi_file = NULL;
0446 wait_queue_entry_t wait;
0447
0448 if ((file->f_flags & O_APPEND) && !(file->f_flags & O_NONBLOCK))
0449 return -EINVAL;
0450
0451 err = stream_open(inode, file);
0452 if (err < 0)
0453 return err;
0454
0455 if (maj == snd_major) {
0456 rmidi = snd_lookup_minor_data(iminor(inode),
0457 SNDRV_DEVICE_TYPE_RAWMIDI);
0458 #ifdef CONFIG_SND_OSSEMUL
0459 } else if (maj == SOUND_MAJOR) {
0460 rmidi = snd_lookup_oss_minor_data(iminor(inode),
0461 SNDRV_OSS_DEVICE_TYPE_MIDI);
0462 #endif
0463 } else
0464 return -ENXIO;
0465
0466 if (rmidi == NULL)
0467 return -ENODEV;
0468
0469 if (!try_module_get(rmidi->card->module)) {
0470 snd_card_unref(rmidi->card);
0471 return -ENXIO;
0472 }
0473
0474 mutex_lock(&rmidi->open_mutex);
0475 card = rmidi->card;
0476 err = snd_card_file_add(card, file);
0477 if (err < 0)
0478 goto __error_card;
0479 fflags = snd_rawmidi_file_flags(file);
0480 if ((file->f_flags & O_APPEND) || maj == SOUND_MAJOR)
0481 fflags |= SNDRV_RAWMIDI_LFLG_APPEND;
0482 rawmidi_file = kmalloc(sizeof(*rawmidi_file), GFP_KERNEL);
0483 if (rawmidi_file == NULL) {
0484 err = -ENOMEM;
0485 goto __error;
0486 }
0487 rawmidi_file->user_pversion = 0;
0488 init_waitqueue_entry(&wait, current);
0489 add_wait_queue(&rmidi->open_wait, &wait);
0490 while (1) {
0491 subdevice = snd_ctl_get_preferred_subdevice(card, SND_CTL_SUBDEV_RAWMIDI);
0492 err = rawmidi_open_priv(rmidi, subdevice, fflags, rawmidi_file);
0493 if (err >= 0)
0494 break;
0495 if (err == -EAGAIN) {
0496 if (file->f_flags & O_NONBLOCK) {
0497 err = -EBUSY;
0498 break;
0499 }
0500 } else
0501 break;
0502 set_current_state(TASK_INTERRUPTIBLE);
0503 mutex_unlock(&rmidi->open_mutex);
0504 schedule();
0505 mutex_lock(&rmidi->open_mutex);
0506 if (rmidi->card->shutdown) {
0507 err = -ENODEV;
0508 break;
0509 }
0510 if (signal_pending(current)) {
0511 err = -ERESTARTSYS;
0512 break;
0513 }
0514 }
0515 remove_wait_queue(&rmidi->open_wait, &wait);
0516 if (err < 0) {
0517 kfree(rawmidi_file);
0518 goto __error;
0519 }
0520 #ifdef CONFIG_SND_OSSEMUL
0521 if (rawmidi_file->input && rawmidi_file->input->runtime)
0522 rawmidi_file->input->runtime->oss = (maj == SOUND_MAJOR);
0523 if (rawmidi_file->output && rawmidi_file->output->runtime)
0524 rawmidi_file->output->runtime->oss = (maj == SOUND_MAJOR);
0525 #endif
0526 file->private_data = rawmidi_file;
0527 mutex_unlock(&rmidi->open_mutex);
0528 snd_card_unref(rmidi->card);
0529 return 0;
0530
0531 __error:
0532 snd_card_file_remove(card, file);
0533 __error_card:
0534 mutex_unlock(&rmidi->open_mutex);
0535 module_put(rmidi->card->module);
0536 snd_card_unref(rmidi->card);
0537 return err;
0538 }
0539
0540 static void close_substream(struct snd_rawmidi *rmidi,
0541 struct snd_rawmidi_substream *substream,
0542 int cleanup)
0543 {
0544 if (--substream->use_count)
0545 return;
0546
0547 if (cleanup) {
0548 if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
0549 snd_rawmidi_input_trigger(substream, 0);
0550 else {
0551 if (substream->active_sensing) {
0552 unsigned char buf = 0xfe;
0553
0554
0555
0556 snd_rawmidi_kernel_write(substream, &buf, 1);
0557 }
0558 if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
0559 snd_rawmidi_output_trigger(substream, 0);
0560 }
0561 snd_rawmidi_buffer_ref_sync(substream);
0562 }
0563 spin_lock_irq(&substream->lock);
0564 substream->opened = 0;
0565 substream->append = 0;
0566 spin_unlock_irq(&substream->lock);
0567 substream->ops->close(substream);
0568 if (substream->runtime->private_free)
0569 substream->runtime->private_free(substream);
0570 snd_rawmidi_runtime_free(substream);
0571 put_pid(substream->pid);
0572 substream->pid = NULL;
0573 rmidi->streams[substream->stream].substream_opened--;
0574 }
0575
0576 static void rawmidi_release_priv(struct snd_rawmidi_file *rfile)
0577 {
0578 struct snd_rawmidi *rmidi;
0579
0580 rmidi = rfile->rmidi;
0581 mutex_lock(&rmidi->open_mutex);
0582 if (rfile->input) {
0583 close_substream(rmidi, rfile->input, 1);
0584 rfile->input = NULL;
0585 }
0586 if (rfile->output) {
0587 close_substream(rmidi, rfile->output, 1);
0588 rfile->output = NULL;
0589 }
0590 rfile->rmidi = NULL;
0591 mutex_unlock(&rmidi->open_mutex);
0592 wake_up(&rmidi->open_wait);
0593 }
0594
0595
0596 int snd_rawmidi_kernel_release(struct snd_rawmidi_file *rfile)
0597 {
0598 struct snd_rawmidi *rmidi;
0599
0600 if (snd_BUG_ON(!rfile))
0601 return -ENXIO;
0602
0603 rmidi = rfile->rmidi;
0604 rawmidi_release_priv(rfile);
0605 module_put(rmidi->card->module);
0606 return 0;
0607 }
0608 EXPORT_SYMBOL(snd_rawmidi_kernel_release);
0609
0610 static int snd_rawmidi_release(struct inode *inode, struct file *file)
0611 {
0612 struct snd_rawmidi_file *rfile;
0613 struct snd_rawmidi *rmidi;
0614 struct module *module;
0615
0616 rfile = file->private_data;
0617 rmidi = rfile->rmidi;
0618 rawmidi_release_priv(rfile);
0619 kfree(rfile);
0620 module = rmidi->card->module;
0621 snd_card_file_remove(rmidi->card, file);
0622 module_put(module);
0623 return 0;
0624 }
0625
0626 static int snd_rawmidi_info(struct snd_rawmidi_substream *substream,
0627 struct snd_rawmidi_info *info)
0628 {
0629 struct snd_rawmidi *rmidi;
0630
0631 if (substream == NULL)
0632 return -ENODEV;
0633 rmidi = substream->rmidi;
0634 memset(info, 0, sizeof(*info));
0635 info->card = rmidi->card->number;
0636 info->device = rmidi->device;
0637 info->subdevice = substream->number;
0638 info->stream = substream->stream;
0639 info->flags = rmidi->info_flags;
0640 strcpy(info->id, rmidi->id);
0641 strcpy(info->name, rmidi->name);
0642 strcpy(info->subname, substream->name);
0643 info->subdevices_count = substream->pstr->substream_count;
0644 info->subdevices_avail = (substream->pstr->substream_count -
0645 substream->pstr->substream_opened);
0646 return 0;
0647 }
0648
0649 static int snd_rawmidi_info_user(struct snd_rawmidi_substream *substream,
0650 struct snd_rawmidi_info __user *_info)
0651 {
0652 struct snd_rawmidi_info info;
0653 int err;
0654
0655 err = snd_rawmidi_info(substream, &info);
0656 if (err < 0)
0657 return err;
0658 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
0659 return -EFAULT;
0660 return 0;
0661 }
0662
0663 static int __snd_rawmidi_info_select(struct snd_card *card,
0664 struct snd_rawmidi_info *info)
0665 {
0666 struct snd_rawmidi *rmidi;
0667 struct snd_rawmidi_str *pstr;
0668 struct snd_rawmidi_substream *substream;
0669
0670 rmidi = snd_rawmidi_search(card, info->device);
0671 if (!rmidi)
0672 return -ENXIO;
0673 if (info->stream < 0 || info->stream > 1)
0674 return -EINVAL;
0675 info->stream = array_index_nospec(info->stream, 2);
0676 pstr = &rmidi->streams[info->stream];
0677 if (pstr->substream_count == 0)
0678 return -ENOENT;
0679 if (info->subdevice >= pstr->substream_count)
0680 return -ENXIO;
0681 list_for_each_entry(substream, &pstr->substreams, list) {
0682 if ((unsigned int)substream->number == info->subdevice)
0683 return snd_rawmidi_info(substream, info);
0684 }
0685 return -ENXIO;
0686 }
0687
0688 int snd_rawmidi_info_select(struct snd_card *card, struct snd_rawmidi_info *info)
0689 {
0690 int ret;
0691
0692 mutex_lock(®ister_mutex);
0693 ret = __snd_rawmidi_info_select(card, info);
0694 mutex_unlock(®ister_mutex);
0695 return ret;
0696 }
0697 EXPORT_SYMBOL(snd_rawmidi_info_select);
0698
0699 static int snd_rawmidi_info_select_user(struct snd_card *card,
0700 struct snd_rawmidi_info __user *_info)
0701 {
0702 int err;
0703 struct snd_rawmidi_info info;
0704
0705 if (get_user(info.device, &_info->device))
0706 return -EFAULT;
0707 if (get_user(info.stream, &_info->stream))
0708 return -EFAULT;
0709 if (get_user(info.subdevice, &_info->subdevice))
0710 return -EFAULT;
0711 err = snd_rawmidi_info_select(card, &info);
0712 if (err < 0)
0713 return err;
0714 if (copy_to_user(_info, &info, sizeof(struct snd_rawmidi_info)))
0715 return -EFAULT;
0716 return 0;
0717 }
0718
0719 static int resize_runtime_buffer(struct snd_rawmidi_substream *substream,
0720 struct snd_rawmidi_params *params,
0721 bool is_input)
0722 {
0723 struct snd_rawmidi_runtime *runtime = substream->runtime;
0724 char *newbuf, *oldbuf;
0725 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
0726
0727 if (params->buffer_size < 32 || params->buffer_size > 1024L * 1024L)
0728 return -EINVAL;
0729 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP && (params->buffer_size & 0x1f) != 0)
0730 return -EINVAL;
0731 if (params->avail_min < 1 || params->avail_min > params->buffer_size)
0732 return -EINVAL;
0733 if (params->buffer_size != runtime->buffer_size) {
0734 newbuf = kvzalloc(params->buffer_size, GFP_KERNEL);
0735 if (!newbuf)
0736 return -ENOMEM;
0737 spin_lock_irq(&substream->lock);
0738 if (runtime->buffer_ref) {
0739 spin_unlock_irq(&substream->lock);
0740 kvfree(newbuf);
0741 return -EBUSY;
0742 }
0743 oldbuf = runtime->buffer;
0744 runtime->buffer = newbuf;
0745 runtime->buffer_size = params->buffer_size;
0746 __reset_runtime_ptrs(runtime, is_input);
0747 spin_unlock_irq(&substream->lock);
0748 kvfree(oldbuf);
0749 }
0750 runtime->avail_min = params->avail_min;
0751 return 0;
0752 }
0753
0754 int snd_rawmidi_output_params(struct snd_rawmidi_substream *substream,
0755 struct snd_rawmidi_params *params)
0756 {
0757 int err;
0758
0759 snd_rawmidi_drain_output(substream);
0760 mutex_lock(&substream->rmidi->open_mutex);
0761 if (substream->append && substream->use_count > 1)
0762 err = -EBUSY;
0763 else
0764 err = resize_runtime_buffer(substream, params, false);
0765
0766 if (!err)
0767 substream->active_sensing = !params->no_active_sensing;
0768 mutex_unlock(&substream->rmidi->open_mutex);
0769 return err;
0770 }
0771 EXPORT_SYMBOL(snd_rawmidi_output_params);
0772
0773 int snd_rawmidi_input_params(struct snd_rawmidi_substream *substream,
0774 struct snd_rawmidi_params *params)
0775 {
0776 unsigned int framing = params->mode & SNDRV_RAWMIDI_MODE_FRAMING_MASK;
0777 unsigned int clock_type = params->mode & SNDRV_RAWMIDI_MODE_CLOCK_MASK;
0778 int err;
0779
0780 snd_rawmidi_drain_input(substream);
0781 mutex_lock(&substream->rmidi->open_mutex);
0782 if (framing == SNDRV_RAWMIDI_MODE_FRAMING_NONE && clock_type != SNDRV_RAWMIDI_MODE_CLOCK_NONE)
0783 err = -EINVAL;
0784 else if (clock_type > SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW)
0785 err = -EINVAL;
0786 else if (framing > SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP)
0787 err = -EINVAL;
0788 else
0789 err = resize_runtime_buffer(substream, params, true);
0790
0791 if (!err) {
0792 substream->framing = framing;
0793 substream->clock_type = clock_type;
0794 }
0795 mutex_unlock(&substream->rmidi->open_mutex);
0796 return 0;
0797 }
0798 EXPORT_SYMBOL(snd_rawmidi_input_params);
0799
0800 static int snd_rawmidi_output_status(struct snd_rawmidi_substream *substream,
0801 struct snd_rawmidi_status64 *status)
0802 {
0803 struct snd_rawmidi_runtime *runtime = substream->runtime;
0804
0805 memset(status, 0, sizeof(*status));
0806 status->stream = SNDRV_RAWMIDI_STREAM_OUTPUT;
0807 spin_lock_irq(&substream->lock);
0808 status->avail = runtime->avail;
0809 spin_unlock_irq(&substream->lock);
0810 return 0;
0811 }
0812
0813 static int snd_rawmidi_input_status(struct snd_rawmidi_substream *substream,
0814 struct snd_rawmidi_status64 *status)
0815 {
0816 struct snd_rawmidi_runtime *runtime = substream->runtime;
0817
0818 memset(status, 0, sizeof(*status));
0819 status->stream = SNDRV_RAWMIDI_STREAM_INPUT;
0820 spin_lock_irq(&substream->lock);
0821 status->avail = runtime->avail;
0822 status->xruns = runtime->xruns;
0823 runtime->xruns = 0;
0824 spin_unlock_irq(&substream->lock);
0825 return 0;
0826 }
0827
0828 static int snd_rawmidi_ioctl_status32(struct snd_rawmidi_file *rfile,
0829 struct snd_rawmidi_status32 __user *argp)
0830 {
0831 int err = 0;
0832 struct snd_rawmidi_status32 __user *status = argp;
0833 struct snd_rawmidi_status32 status32;
0834 struct snd_rawmidi_status64 status64;
0835
0836 if (copy_from_user(&status32, argp,
0837 sizeof(struct snd_rawmidi_status32)))
0838 return -EFAULT;
0839
0840 switch (status32.stream) {
0841 case SNDRV_RAWMIDI_STREAM_OUTPUT:
0842 if (rfile->output == NULL)
0843 return -EINVAL;
0844 err = snd_rawmidi_output_status(rfile->output, &status64);
0845 break;
0846 case SNDRV_RAWMIDI_STREAM_INPUT:
0847 if (rfile->input == NULL)
0848 return -EINVAL;
0849 err = snd_rawmidi_input_status(rfile->input, &status64);
0850 break;
0851 default:
0852 return -EINVAL;
0853 }
0854 if (err < 0)
0855 return err;
0856
0857 status32 = (struct snd_rawmidi_status32) {
0858 .stream = status64.stream,
0859 .tstamp_sec = status64.tstamp_sec,
0860 .tstamp_nsec = status64.tstamp_nsec,
0861 .avail = status64.avail,
0862 .xruns = status64.xruns,
0863 };
0864
0865 if (copy_to_user(status, &status32, sizeof(*status)))
0866 return -EFAULT;
0867
0868 return 0;
0869 }
0870
0871 static int snd_rawmidi_ioctl_status64(struct snd_rawmidi_file *rfile,
0872 struct snd_rawmidi_status64 __user *argp)
0873 {
0874 int err = 0;
0875 struct snd_rawmidi_status64 status;
0876
0877 if (copy_from_user(&status, argp, sizeof(struct snd_rawmidi_status64)))
0878 return -EFAULT;
0879
0880 switch (status.stream) {
0881 case SNDRV_RAWMIDI_STREAM_OUTPUT:
0882 if (rfile->output == NULL)
0883 return -EINVAL;
0884 err = snd_rawmidi_output_status(rfile->output, &status);
0885 break;
0886 case SNDRV_RAWMIDI_STREAM_INPUT:
0887 if (rfile->input == NULL)
0888 return -EINVAL;
0889 err = snd_rawmidi_input_status(rfile->input, &status);
0890 break;
0891 default:
0892 return -EINVAL;
0893 }
0894 if (err < 0)
0895 return err;
0896 if (copy_to_user(argp, &status,
0897 sizeof(struct snd_rawmidi_status64)))
0898 return -EFAULT;
0899 return 0;
0900 }
0901
0902 static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0903 {
0904 struct snd_rawmidi_file *rfile;
0905 void __user *argp = (void __user *)arg;
0906
0907 rfile = file->private_data;
0908 if (((cmd >> 8) & 0xff) != 'W')
0909 return -ENOTTY;
0910 switch (cmd) {
0911 case SNDRV_RAWMIDI_IOCTL_PVERSION:
0912 return put_user(SNDRV_RAWMIDI_VERSION, (int __user *)argp) ? -EFAULT : 0;
0913 case SNDRV_RAWMIDI_IOCTL_INFO:
0914 {
0915 int stream;
0916 struct snd_rawmidi_info __user *info = argp;
0917
0918 if (get_user(stream, &info->stream))
0919 return -EFAULT;
0920 switch (stream) {
0921 case SNDRV_RAWMIDI_STREAM_INPUT:
0922 return snd_rawmidi_info_user(rfile->input, info);
0923 case SNDRV_RAWMIDI_STREAM_OUTPUT:
0924 return snd_rawmidi_info_user(rfile->output, info);
0925 default:
0926 return -EINVAL;
0927 }
0928 }
0929 case SNDRV_RAWMIDI_IOCTL_USER_PVERSION:
0930 if (get_user(rfile->user_pversion, (unsigned int __user *)arg))
0931 return -EFAULT;
0932 return 0;
0933
0934 case SNDRV_RAWMIDI_IOCTL_PARAMS:
0935 {
0936 struct snd_rawmidi_params params;
0937
0938 if (copy_from_user(¶ms, argp, sizeof(struct snd_rawmidi_params)))
0939 return -EFAULT;
0940 if (rfile->user_pversion < SNDRV_PROTOCOL_VERSION(2, 0, 2)) {
0941 params.mode = 0;
0942 memset(params.reserved, 0, sizeof(params.reserved));
0943 }
0944 switch (params.stream) {
0945 case SNDRV_RAWMIDI_STREAM_OUTPUT:
0946 if (rfile->output == NULL)
0947 return -EINVAL;
0948 return snd_rawmidi_output_params(rfile->output, ¶ms);
0949 case SNDRV_RAWMIDI_STREAM_INPUT:
0950 if (rfile->input == NULL)
0951 return -EINVAL;
0952 return snd_rawmidi_input_params(rfile->input, ¶ms);
0953 default:
0954 return -EINVAL;
0955 }
0956 }
0957 case SNDRV_RAWMIDI_IOCTL_STATUS32:
0958 return snd_rawmidi_ioctl_status32(rfile, argp);
0959 case SNDRV_RAWMIDI_IOCTL_STATUS64:
0960 return snd_rawmidi_ioctl_status64(rfile, argp);
0961 case SNDRV_RAWMIDI_IOCTL_DROP:
0962 {
0963 int val;
0964
0965 if (get_user(val, (int __user *) argp))
0966 return -EFAULT;
0967 switch (val) {
0968 case SNDRV_RAWMIDI_STREAM_OUTPUT:
0969 if (rfile->output == NULL)
0970 return -EINVAL;
0971 return snd_rawmidi_drop_output(rfile->output);
0972 default:
0973 return -EINVAL;
0974 }
0975 }
0976 case SNDRV_RAWMIDI_IOCTL_DRAIN:
0977 {
0978 int val;
0979
0980 if (get_user(val, (int __user *) argp))
0981 return -EFAULT;
0982 switch (val) {
0983 case SNDRV_RAWMIDI_STREAM_OUTPUT:
0984 if (rfile->output == NULL)
0985 return -EINVAL;
0986 return snd_rawmidi_drain_output(rfile->output);
0987 case SNDRV_RAWMIDI_STREAM_INPUT:
0988 if (rfile->input == NULL)
0989 return -EINVAL;
0990 return snd_rawmidi_drain_input(rfile->input);
0991 default:
0992 return -EINVAL;
0993 }
0994 }
0995 default:
0996 rmidi_dbg(rfile->rmidi,
0997 "rawmidi: unknown command = 0x%x\n", cmd);
0998 }
0999 return -ENOTTY;
1000 }
1001
1002 static int snd_rawmidi_control_ioctl(struct snd_card *card,
1003 struct snd_ctl_file *control,
1004 unsigned int cmd,
1005 unsigned long arg)
1006 {
1007 void __user *argp = (void __user *)arg;
1008
1009 switch (cmd) {
1010 case SNDRV_CTL_IOCTL_RAWMIDI_NEXT_DEVICE:
1011 {
1012 int device;
1013
1014 if (get_user(device, (int __user *)argp))
1015 return -EFAULT;
1016 if (device >= SNDRV_RAWMIDI_DEVICES)
1017 device = SNDRV_RAWMIDI_DEVICES - 1;
1018 mutex_lock(®ister_mutex);
1019 device = device < 0 ? 0 : device + 1;
1020 while (device < SNDRV_RAWMIDI_DEVICES) {
1021 if (snd_rawmidi_search(card, device))
1022 break;
1023 device++;
1024 }
1025 if (device == SNDRV_RAWMIDI_DEVICES)
1026 device = -1;
1027 mutex_unlock(®ister_mutex);
1028 if (put_user(device, (int __user *)argp))
1029 return -EFAULT;
1030 return 0;
1031 }
1032 case SNDRV_CTL_IOCTL_RAWMIDI_PREFER_SUBDEVICE:
1033 {
1034 int val;
1035
1036 if (get_user(val, (int __user *)argp))
1037 return -EFAULT;
1038 control->preferred_subdevice[SND_CTL_SUBDEV_RAWMIDI] = val;
1039 return 0;
1040 }
1041 case SNDRV_CTL_IOCTL_RAWMIDI_INFO:
1042 return snd_rawmidi_info_select_user(card, argp);
1043 }
1044 return -ENOIOCTLCMD;
1045 }
1046
1047 static int receive_with_tstamp_framing(struct snd_rawmidi_substream *substream,
1048 const unsigned char *buffer, int src_count, const struct timespec64 *tstamp)
1049 {
1050 struct snd_rawmidi_runtime *runtime = substream->runtime;
1051 struct snd_rawmidi_framing_tstamp *dest_ptr;
1052 struct snd_rawmidi_framing_tstamp frame = { .tv_sec = tstamp->tv_sec, .tv_nsec = tstamp->tv_nsec };
1053 int dest_frames = 0;
1054 int orig_count = src_count;
1055 int frame_size = sizeof(struct snd_rawmidi_framing_tstamp);
1056
1057 BUILD_BUG_ON(frame_size != 0x20);
1058 if (snd_BUG_ON((runtime->hw_ptr & 0x1f) != 0))
1059 return -EINVAL;
1060
1061 while (src_count > 0) {
1062 if ((int)(runtime->buffer_size - runtime->avail) < frame_size) {
1063 runtime->xruns += src_count;
1064 break;
1065 }
1066 if (src_count >= SNDRV_RAWMIDI_FRAMING_DATA_LENGTH)
1067 frame.length = SNDRV_RAWMIDI_FRAMING_DATA_LENGTH;
1068 else {
1069 frame.length = src_count;
1070 memset(frame.data, 0, SNDRV_RAWMIDI_FRAMING_DATA_LENGTH);
1071 }
1072 memcpy(frame.data, buffer, frame.length);
1073 buffer += frame.length;
1074 src_count -= frame.length;
1075 dest_ptr = (struct snd_rawmidi_framing_tstamp *) (runtime->buffer + runtime->hw_ptr);
1076 *dest_ptr = frame;
1077 runtime->avail += frame_size;
1078 runtime->hw_ptr += frame_size;
1079 runtime->hw_ptr %= runtime->buffer_size;
1080 dest_frames++;
1081 }
1082 return orig_count - src_count;
1083 }
1084
1085 static struct timespec64 get_framing_tstamp(struct snd_rawmidi_substream *substream)
1086 {
1087 struct timespec64 ts64 = {0, 0};
1088
1089 switch (substream->clock_type) {
1090 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC_RAW:
1091 ktime_get_raw_ts64(&ts64);
1092 break;
1093 case SNDRV_RAWMIDI_MODE_CLOCK_MONOTONIC:
1094 ktime_get_ts64(&ts64);
1095 break;
1096 case SNDRV_RAWMIDI_MODE_CLOCK_REALTIME:
1097 ktime_get_real_ts64(&ts64);
1098 break;
1099 }
1100 return ts64;
1101 }
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113 int snd_rawmidi_receive(struct snd_rawmidi_substream *substream,
1114 const unsigned char *buffer, int count)
1115 {
1116 unsigned long flags;
1117 struct timespec64 ts64 = get_framing_tstamp(substream);
1118 int result = 0, count1;
1119 struct snd_rawmidi_runtime *runtime;
1120
1121 spin_lock_irqsave(&substream->lock, flags);
1122 if (!substream->opened) {
1123 result = -EBADFD;
1124 goto unlock;
1125 }
1126 runtime = substream->runtime;
1127 if (!runtime || !runtime->buffer) {
1128 rmidi_dbg(substream->rmidi,
1129 "snd_rawmidi_receive: input is not active!!!\n");
1130 result = -EINVAL;
1131 goto unlock;
1132 }
1133
1134 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1135 result = receive_with_tstamp_framing(substream, buffer, count, &ts64);
1136 } else if (count == 1) {
1137 substream->bytes++;
1138 if (runtime->avail < runtime->buffer_size) {
1139 runtime->buffer[runtime->hw_ptr++] = buffer[0];
1140 runtime->hw_ptr %= runtime->buffer_size;
1141 runtime->avail++;
1142 result++;
1143 } else {
1144 runtime->xruns++;
1145 }
1146 } else {
1147 substream->bytes += count;
1148 count1 = runtime->buffer_size - runtime->hw_ptr;
1149 if (count1 > count)
1150 count1 = count;
1151 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1152 count1 = runtime->buffer_size - runtime->avail;
1153 memcpy(runtime->buffer + runtime->hw_ptr, buffer, count1);
1154 runtime->hw_ptr += count1;
1155 runtime->hw_ptr %= runtime->buffer_size;
1156 runtime->avail += count1;
1157 count -= count1;
1158 result += count1;
1159 if (count > 0) {
1160 buffer += count1;
1161 count1 = count;
1162 if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
1163 count1 = runtime->buffer_size - runtime->avail;
1164 runtime->xruns += count - count1;
1165 }
1166 if (count1 > 0) {
1167 memcpy(runtime->buffer, buffer, count1);
1168 runtime->hw_ptr = count1;
1169 runtime->avail += count1;
1170 result += count1;
1171 }
1172 }
1173 }
1174 if (result > 0) {
1175 if (runtime->event)
1176 schedule_work(&runtime->event_work);
1177 else if (__snd_rawmidi_ready(runtime))
1178 wake_up(&runtime->sleep);
1179 }
1180 unlock:
1181 spin_unlock_irqrestore(&substream->lock, flags);
1182 return result;
1183 }
1184 EXPORT_SYMBOL(snd_rawmidi_receive);
1185
1186 static long snd_rawmidi_kernel_read1(struct snd_rawmidi_substream *substream,
1187 unsigned char __user *userbuf,
1188 unsigned char *kernelbuf, long count)
1189 {
1190 unsigned long flags;
1191 long result = 0, count1;
1192 struct snd_rawmidi_runtime *runtime = substream->runtime;
1193 unsigned long appl_ptr;
1194 int err = 0;
1195
1196 spin_lock_irqsave(&substream->lock, flags);
1197 snd_rawmidi_buffer_ref(runtime);
1198 while (count > 0 && runtime->avail) {
1199 count1 = runtime->buffer_size - runtime->appl_ptr;
1200 if (count1 > count)
1201 count1 = count;
1202 if (count1 > (int)runtime->avail)
1203 count1 = runtime->avail;
1204
1205
1206 appl_ptr = runtime->appl_ptr;
1207 runtime->appl_ptr += count1;
1208 runtime->appl_ptr %= runtime->buffer_size;
1209 runtime->avail -= count1;
1210
1211 if (kernelbuf)
1212 memcpy(kernelbuf + result, runtime->buffer + appl_ptr, count1);
1213 if (userbuf) {
1214 spin_unlock_irqrestore(&substream->lock, flags);
1215 if (copy_to_user(userbuf + result,
1216 runtime->buffer + appl_ptr, count1))
1217 err = -EFAULT;
1218 spin_lock_irqsave(&substream->lock, flags);
1219 if (err)
1220 goto out;
1221 }
1222 result += count1;
1223 count -= count1;
1224 }
1225 out:
1226 snd_rawmidi_buffer_unref(runtime);
1227 spin_unlock_irqrestore(&substream->lock, flags);
1228 return result > 0 ? result : err;
1229 }
1230
1231 long snd_rawmidi_kernel_read(struct snd_rawmidi_substream *substream,
1232 unsigned char *buf, long count)
1233 {
1234 snd_rawmidi_input_trigger(substream, 1);
1235 return snd_rawmidi_kernel_read1(substream, NULL, buf, count);
1236 }
1237 EXPORT_SYMBOL(snd_rawmidi_kernel_read);
1238
1239 static ssize_t snd_rawmidi_read(struct file *file, char __user *buf, size_t count,
1240 loff_t *offset)
1241 {
1242 long result;
1243 int count1;
1244 struct snd_rawmidi_file *rfile;
1245 struct snd_rawmidi_substream *substream;
1246 struct snd_rawmidi_runtime *runtime;
1247
1248 rfile = file->private_data;
1249 substream = rfile->input;
1250 if (substream == NULL)
1251 return -EIO;
1252 runtime = substream->runtime;
1253 snd_rawmidi_input_trigger(substream, 1);
1254 result = 0;
1255 while (count > 0) {
1256 spin_lock_irq(&substream->lock);
1257 while (!__snd_rawmidi_ready(runtime)) {
1258 wait_queue_entry_t wait;
1259
1260 if ((file->f_flags & O_NONBLOCK) != 0 || result > 0) {
1261 spin_unlock_irq(&substream->lock);
1262 return result > 0 ? result : -EAGAIN;
1263 }
1264 init_waitqueue_entry(&wait, current);
1265 add_wait_queue(&runtime->sleep, &wait);
1266 set_current_state(TASK_INTERRUPTIBLE);
1267 spin_unlock_irq(&substream->lock);
1268 schedule();
1269 remove_wait_queue(&runtime->sleep, &wait);
1270 if (rfile->rmidi->card->shutdown)
1271 return -ENODEV;
1272 if (signal_pending(current))
1273 return result > 0 ? result : -ERESTARTSYS;
1274 spin_lock_irq(&substream->lock);
1275 if (!runtime->avail) {
1276 spin_unlock_irq(&substream->lock);
1277 return result > 0 ? result : -EIO;
1278 }
1279 }
1280 spin_unlock_irq(&substream->lock);
1281 count1 = snd_rawmidi_kernel_read1(substream,
1282 (unsigned char __user *)buf,
1283 NULL,
1284 count);
1285 if (count1 < 0)
1286 return result > 0 ? result : count1;
1287 result += count1;
1288 buf += count1;
1289 count -= count1;
1290 }
1291 return result;
1292 }
1293
1294
1295
1296
1297
1298
1299
1300 int snd_rawmidi_transmit_empty(struct snd_rawmidi_substream *substream)
1301 {
1302 struct snd_rawmidi_runtime *runtime;
1303 int result;
1304 unsigned long flags;
1305
1306 spin_lock_irqsave(&substream->lock, flags);
1307 runtime = substream->runtime;
1308 if (!substream->opened || !runtime || !runtime->buffer) {
1309 rmidi_dbg(substream->rmidi,
1310 "snd_rawmidi_transmit_empty: output is not active!!!\n");
1311 result = 1;
1312 } else {
1313 result = runtime->avail >= runtime->buffer_size;
1314 }
1315 spin_unlock_irqrestore(&substream->lock, flags);
1316 return result;
1317 }
1318 EXPORT_SYMBOL(snd_rawmidi_transmit_empty);
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328 static int __snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1329 unsigned char *buffer, int count)
1330 {
1331 int result, count1;
1332 struct snd_rawmidi_runtime *runtime = substream->runtime;
1333
1334 if (runtime->buffer == NULL) {
1335 rmidi_dbg(substream->rmidi,
1336 "snd_rawmidi_transmit_peek: output is not active!!!\n");
1337 return -EINVAL;
1338 }
1339 result = 0;
1340 if (runtime->avail >= runtime->buffer_size) {
1341
1342 goto __skip;
1343 }
1344 if (count == 1) {
1345 *buffer = runtime->buffer[runtime->hw_ptr];
1346 result++;
1347 } else {
1348 count1 = runtime->buffer_size - runtime->hw_ptr;
1349 if (count1 > count)
1350 count1 = count;
1351 if (count1 > (int)(runtime->buffer_size - runtime->avail))
1352 count1 = runtime->buffer_size - runtime->avail;
1353 memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
1354 count -= count1;
1355 result += count1;
1356 if (count > 0) {
1357 if (count > (int)(runtime->buffer_size - runtime->avail - count1))
1358 count = runtime->buffer_size - runtime->avail - count1;
1359 memcpy(buffer + count1, runtime->buffer, count);
1360 result += count;
1361 }
1362 }
1363 __skip:
1364 return result;
1365 }
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381 int snd_rawmidi_transmit_peek(struct snd_rawmidi_substream *substream,
1382 unsigned char *buffer, int count)
1383 {
1384 int result;
1385 unsigned long flags;
1386
1387 spin_lock_irqsave(&substream->lock, flags);
1388 if (!substream->opened || !substream->runtime)
1389 result = -EBADFD;
1390 else
1391 result = __snd_rawmidi_transmit_peek(substream, buffer, count);
1392 spin_unlock_irqrestore(&substream->lock, flags);
1393 return result;
1394 }
1395 EXPORT_SYMBOL(snd_rawmidi_transmit_peek);
1396
1397
1398
1399
1400
1401
1402
1403
1404 static int __snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream,
1405 int count)
1406 {
1407 struct snd_rawmidi_runtime *runtime = substream->runtime;
1408
1409 if (runtime->buffer == NULL) {
1410 rmidi_dbg(substream->rmidi,
1411 "snd_rawmidi_transmit_ack: output is not active!!!\n");
1412 return -EINVAL;
1413 }
1414 snd_BUG_ON(runtime->avail + count > runtime->buffer_size);
1415 runtime->hw_ptr += count;
1416 runtime->hw_ptr %= runtime->buffer_size;
1417 runtime->avail += count;
1418 substream->bytes += count;
1419 if (count > 0) {
1420 if (runtime->drain || __snd_rawmidi_ready(runtime))
1421 wake_up(&runtime->sleep);
1422 }
1423 return count;
1424 }
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437 int snd_rawmidi_transmit_ack(struct snd_rawmidi_substream *substream, int count)
1438 {
1439 int result;
1440 unsigned long flags;
1441
1442 spin_lock_irqsave(&substream->lock, flags);
1443 if (!substream->opened || !substream->runtime)
1444 result = -EBADFD;
1445 else
1446 result = __snd_rawmidi_transmit_ack(substream, count);
1447 spin_unlock_irqrestore(&substream->lock, flags);
1448 return result;
1449 }
1450 EXPORT_SYMBOL(snd_rawmidi_transmit_ack);
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462 int snd_rawmidi_transmit(struct snd_rawmidi_substream *substream,
1463 unsigned char *buffer, int count)
1464 {
1465 int result;
1466 unsigned long flags;
1467
1468 spin_lock_irqsave(&substream->lock, flags);
1469 if (!substream->opened)
1470 result = -EBADFD;
1471 else {
1472 count = __snd_rawmidi_transmit_peek(substream, buffer, count);
1473 if (count <= 0)
1474 result = count;
1475 else
1476 result = __snd_rawmidi_transmit_ack(substream, count);
1477 }
1478 spin_unlock_irqrestore(&substream->lock, flags);
1479 return result;
1480 }
1481 EXPORT_SYMBOL(snd_rawmidi_transmit);
1482
1483
1484
1485
1486
1487
1488
1489 int snd_rawmidi_proceed(struct snd_rawmidi_substream *substream)
1490 {
1491 struct snd_rawmidi_runtime *runtime;
1492 unsigned long flags;
1493 int count = 0;
1494
1495 spin_lock_irqsave(&substream->lock, flags);
1496 runtime = substream->runtime;
1497 if (substream->opened && runtime &&
1498 runtime->avail < runtime->buffer_size) {
1499 count = runtime->buffer_size - runtime->avail;
1500 __snd_rawmidi_transmit_ack(substream, count);
1501 }
1502 spin_unlock_irqrestore(&substream->lock, flags);
1503 return count;
1504 }
1505 EXPORT_SYMBOL(snd_rawmidi_proceed);
1506
1507 static long snd_rawmidi_kernel_write1(struct snd_rawmidi_substream *substream,
1508 const unsigned char __user *userbuf,
1509 const unsigned char *kernelbuf,
1510 long count)
1511 {
1512 unsigned long flags;
1513 long count1, result;
1514 struct snd_rawmidi_runtime *runtime = substream->runtime;
1515 unsigned long appl_ptr;
1516
1517 if (!kernelbuf && !userbuf)
1518 return -EINVAL;
1519 if (snd_BUG_ON(!runtime->buffer))
1520 return -EINVAL;
1521
1522 result = 0;
1523 spin_lock_irqsave(&substream->lock, flags);
1524 if (substream->append) {
1525 if ((long)runtime->avail < count) {
1526 spin_unlock_irqrestore(&substream->lock, flags);
1527 return -EAGAIN;
1528 }
1529 }
1530 snd_rawmidi_buffer_ref(runtime);
1531 while (count > 0 && runtime->avail > 0) {
1532 count1 = runtime->buffer_size - runtime->appl_ptr;
1533 if (count1 > count)
1534 count1 = count;
1535 if (count1 > (long)runtime->avail)
1536 count1 = runtime->avail;
1537
1538
1539 appl_ptr = runtime->appl_ptr;
1540 runtime->appl_ptr += count1;
1541 runtime->appl_ptr %= runtime->buffer_size;
1542 runtime->avail -= count1;
1543
1544 if (kernelbuf)
1545 memcpy(runtime->buffer + appl_ptr,
1546 kernelbuf + result, count1);
1547 else if (userbuf) {
1548 spin_unlock_irqrestore(&substream->lock, flags);
1549 if (copy_from_user(runtime->buffer + appl_ptr,
1550 userbuf + result, count1)) {
1551 spin_lock_irqsave(&substream->lock, flags);
1552 result = result > 0 ? result : -EFAULT;
1553 goto __end;
1554 }
1555 spin_lock_irqsave(&substream->lock, flags);
1556 }
1557 result += count1;
1558 count -= count1;
1559 }
1560 __end:
1561 count1 = runtime->avail < runtime->buffer_size;
1562 snd_rawmidi_buffer_unref(runtime);
1563 spin_unlock_irqrestore(&substream->lock, flags);
1564 if (count1)
1565 snd_rawmidi_output_trigger(substream, 1);
1566 return result;
1567 }
1568
1569 long snd_rawmidi_kernel_write(struct snd_rawmidi_substream *substream,
1570 const unsigned char *buf, long count)
1571 {
1572 return snd_rawmidi_kernel_write1(substream, NULL, buf, count);
1573 }
1574 EXPORT_SYMBOL(snd_rawmidi_kernel_write);
1575
1576 static ssize_t snd_rawmidi_write(struct file *file, const char __user *buf,
1577 size_t count, loff_t *offset)
1578 {
1579 long result, timeout;
1580 int count1;
1581 struct snd_rawmidi_file *rfile;
1582 struct snd_rawmidi_runtime *runtime;
1583 struct snd_rawmidi_substream *substream;
1584
1585 rfile = file->private_data;
1586 substream = rfile->output;
1587 runtime = substream->runtime;
1588
1589 if (substream->append && count > runtime->buffer_size)
1590 return -EIO;
1591 result = 0;
1592 while (count > 0) {
1593 spin_lock_irq(&substream->lock);
1594 while (!snd_rawmidi_ready_append(substream, count)) {
1595 wait_queue_entry_t wait;
1596
1597 if (file->f_flags & O_NONBLOCK) {
1598 spin_unlock_irq(&substream->lock);
1599 return result > 0 ? result : -EAGAIN;
1600 }
1601 init_waitqueue_entry(&wait, current);
1602 add_wait_queue(&runtime->sleep, &wait);
1603 set_current_state(TASK_INTERRUPTIBLE);
1604 spin_unlock_irq(&substream->lock);
1605 timeout = schedule_timeout(30 * HZ);
1606 remove_wait_queue(&runtime->sleep, &wait);
1607 if (rfile->rmidi->card->shutdown)
1608 return -ENODEV;
1609 if (signal_pending(current))
1610 return result > 0 ? result : -ERESTARTSYS;
1611 spin_lock_irq(&substream->lock);
1612 if (!runtime->avail && !timeout) {
1613 spin_unlock_irq(&substream->lock);
1614 return result > 0 ? result : -EIO;
1615 }
1616 }
1617 spin_unlock_irq(&substream->lock);
1618 count1 = snd_rawmidi_kernel_write1(substream, buf, NULL, count);
1619 if (count1 < 0)
1620 return result > 0 ? result : count1;
1621 result += count1;
1622 buf += count1;
1623 if ((size_t)count1 < count && (file->f_flags & O_NONBLOCK))
1624 break;
1625 count -= count1;
1626 }
1627 if (file->f_flags & O_DSYNC) {
1628 spin_lock_irq(&substream->lock);
1629 while (runtime->avail != runtime->buffer_size) {
1630 wait_queue_entry_t wait;
1631 unsigned int last_avail = runtime->avail;
1632
1633 init_waitqueue_entry(&wait, current);
1634 add_wait_queue(&runtime->sleep, &wait);
1635 set_current_state(TASK_INTERRUPTIBLE);
1636 spin_unlock_irq(&substream->lock);
1637 timeout = schedule_timeout(30 * HZ);
1638 remove_wait_queue(&runtime->sleep, &wait);
1639 if (signal_pending(current))
1640 return result > 0 ? result : -ERESTARTSYS;
1641 if (runtime->avail == last_avail && !timeout)
1642 return result > 0 ? result : -EIO;
1643 spin_lock_irq(&substream->lock);
1644 }
1645 spin_unlock_irq(&substream->lock);
1646 }
1647 return result;
1648 }
1649
1650 static __poll_t snd_rawmidi_poll(struct file *file, poll_table *wait)
1651 {
1652 struct snd_rawmidi_file *rfile;
1653 struct snd_rawmidi_runtime *runtime;
1654 __poll_t mask;
1655
1656 rfile = file->private_data;
1657 if (rfile->input != NULL) {
1658 runtime = rfile->input->runtime;
1659 snd_rawmidi_input_trigger(rfile->input, 1);
1660 poll_wait(file, &runtime->sleep, wait);
1661 }
1662 if (rfile->output != NULL) {
1663 runtime = rfile->output->runtime;
1664 poll_wait(file, &runtime->sleep, wait);
1665 }
1666 mask = 0;
1667 if (rfile->input != NULL) {
1668 if (snd_rawmidi_ready(rfile->input))
1669 mask |= EPOLLIN | EPOLLRDNORM;
1670 }
1671 if (rfile->output != NULL) {
1672 if (snd_rawmidi_ready(rfile->output))
1673 mask |= EPOLLOUT | EPOLLWRNORM;
1674 }
1675 return mask;
1676 }
1677
1678
1679
1680 #ifdef CONFIG_COMPAT
1681 #include "rawmidi_compat.c"
1682 #else
1683 #define snd_rawmidi_ioctl_compat NULL
1684 #endif
1685
1686
1687
1688
1689 static void snd_rawmidi_proc_info_read(struct snd_info_entry *entry,
1690 struct snd_info_buffer *buffer)
1691 {
1692 struct snd_rawmidi *rmidi;
1693 struct snd_rawmidi_substream *substream;
1694 struct snd_rawmidi_runtime *runtime;
1695 unsigned long buffer_size, avail, xruns;
1696 unsigned int clock_type;
1697 static const char *clock_names[4] = { "none", "realtime", "monotonic", "monotonic raw" };
1698
1699 rmidi = entry->private_data;
1700 snd_iprintf(buffer, "%s\n\n", rmidi->name);
1701 mutex_lock(&rmidi->open_mutex);
1702 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_OUTPUT) {
1703 list_for_each_entry(substream,
1704 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams,
1705 list) {
1706 snd_iprintf(buffer,
1707 "Output %d\n"
1708 " Tx bytes : %lu\n",
1709 substream->number,
1710 (unsigned long) substream->bytes);
1711 if (substream->opened) {
1712 snd_iprintf(buffer,
1713 " Owner PID : %d\n",
1714 pid_vnr(substream->pid));
1715 runtime = substream->runtime;
1716 spin_lock_irq(&substream->lock);
1717 buffer_size = runtime->buffer_size;
1718 avail = runtime->avail;
1719 spin_unlock_irq(&substream->lock);
1720 snd_iprintf(buffer,
1721 " Mode : %s\n"
1722 " Buffer size : %lu\n"
1723 " Avail : %lu\n",
1724 runtime->oss ? "OSS compatible" : "native",
1725 buffer_size, avail);
1726 }
1727 }
1728 }
1729 if (rmidi->info_flags & SNDRV_RAWMIDI_INFO_INPUT) {
1730 list_for_each_entry(substream,
1731 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams,
1732 list) {
1733 snd_iprintf(buffer,
1734 "Input %d\n"
1735 " Rx bytes : %lu\n",
1736 substream->number,
1737 (unsigned long) substream->bytes);
1738 if (substream->opened) {
1739 snd_iprintf(buffer,
1740 " Owner PID : %d\n",
1741 pid_vnr(substream->pid));
1742 runtime = substream->runtime;
1743 spin_lock_irq(&substream->lock);
1744 buffer_size = runtime->buffer_size;
1745 avail = runtime->avail;
1746 xruns = runtime->xruns;
1747 spin_unlock_irq(&substream->lock);
1748 snd_iprintf(buffer,
1749 " Buffer size : %lu\n"
1750 " Avail : %lu\n"
1751 " Overruns : %lu\n",
1752 buffer_size, avail, xruns);
1753 if (substream->framing == SNDRV_RAWMIDI_MODE_FRAMING_TSTAMP) {
1754 clock_type = substream->clock_type >> SNDRV_RAWMIDI_MODE_CLOCK_SHIFT;
1755 if (!snd_BUG_ON(clock_type >= ARRAY_SIZE(clock_names)))
1756 snd_iprintf(buffer,
1757 " Framing : tstamp\n"
1758 " Clock type : %s\n",
1759 clock_names[clock_type]);
1760 }
1761 }
1762 }
1763 }
1764 mutex_unlock(&rmidi->open_mutex);
1765 }
1766
1767
1768
1769
1770
1771 static const struct file_operations snd_rawmidi_f_ops = {
1772 .owner = THIS_MODULE,
1773 .read = snd_rawmidi_read,
1774 .write = snd_rawmidi_write,
1775 .open = snd_rawmidi_open,
1776 .release = snd_rawmidi_release,
1777 .llseek = no_llseek,
1778 .poll = snd_rawmidi_poll,
1779 .unlocked_ioctl = snd_rawmidi_ioctl,
1780 .compat_ioctl = snd_rawmidi_ioctl_compat,
1781 };
1782
1783 static int snd_rawmidi_alloc_substreams(struct snd_rawmidi *rmidi,
1784 struct snd_rawmidi_str *stream,
1785 int direction,
1786 int count)
1787 {
1788 struct snd_rawmidi_substream *substream;
1789 int idx;
1790
1791 for (idx = 0; idx < count; idx++) {
1792 substream = kzalloc(sizeof(*substream), GFP_KERNEL);
1793 if (!substream)
1794 return -ENOMEM;
1795 substream->stream = direction;
1796 substream->number = idx;
1797 substream->rmidi = rmidi;
1798 substream->pstr = stream;
1799 spin_lock_init(&substream->lock);
1800 list_add_tail(&substream->list, &stream->substreams);
1801 stream->substream_count++;
1802 }
1803 return 0;
1804 }
1805
1806 static void release_rawmidi_device(struct device *dev)
1807 {
1808 kfree(container_of(dev, struct snd_rawmidi, dev));
1809 }
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825 int snd_rawmidi_new(struct snd_card *card, char *id, int device,
1826 int output_count, int input_count,
1827 struct snd_rawmidi **rrawmidi)
1828 {
1829 struct snd_rawmidi *rmidi;
1830 int err;
1831 static const struct snd_device_ops ops = {
1832 .dev_free = snd_rawmidi_dev_free,
1833 .dev_register = snd_rawmidi_dev_register,
1834 .dev_disconnect = snd_rawmidi_dev_disconnect,
1835 };
1836
1837 if (snd_BUG_ON(!card))
1838 return -ENXIO;
1839 if (rrawmidi)
1840 *rrawmidi = NULL;
1841 rmidi = kzalloc(sizeof(*rmidi), GFP_KERNEL);
1842 if (!rmidi)
1843 return -ENOMEM;
1844 rmidi->card = card;
1845 rmidi->device = device;
1846 mutex_init(&rmidi->open_mutex);
1847 init_waitqueue_head(&rmidi->open_wait);
1848 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substreams);
1849 INIT_LIST_HEAD(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT].substreams);
1850
1851 if (id != NULL)
1852 strscpy(rmidi->id, id, sizeof(rmidi->id));
1853
1854 snd_device_initialize(&rmidi->dev, card);
1855 rmidi->dev.release = release_rawmidi_device;
1856 dev_set_name(&rmidi->dev, "midiC%iD%i", card->number, device);
1857
1858 err = snd_rawmidi_alloc_substreams(rmidi,
1859 &rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT],
1860 SNDRV_RAWMIDI_STREAM_INPUT,
1861 input_count);
1862 if (err < 0)
1863 goto error;
1864 err = snd_rawmidi_alloc_substreams(rmidi,
1865 &rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT],
1866 SNDRV_RAWMIDI_STREAM_OUTPUT,
1867 output_count);
1868 if (err < 0)
1869 goto error;
1870 err = snd_device_new(card, SNDRV_DEV_RAWMIDI, rmidi, &ops);
1871 if (err < 0)
1872 goto error;
1873
1874 if (rrawmidi)
1875 *rrawmidi = rmidi;
1876 return 0;
1877
1878 error:
1879 snd_rawmidi_free(rmidi);
1880 return err;
1881 }
1882 EXPORT_SYMBOL(snd_rawmidi_new);
1883
1884 static void snd_rawmidi_free_substreams(struct snd_rawmidi_str *stream)
1885 {
1886 struct snd_rawmidi_substream *substream;
1887
1888 while (!list_empty(&stream->substreams)) {
1889 substream = list_entry(stream->substreams.next, struct snd_rawmidi_substream, list);
1890 list_del(&substream->list);
1891 kfree(substream);
1892 }
1893 }
1894
1895 static int snd_rawmidi_free(struct snd_rawmidi *rmidi)
1896 {
1897 if (!rmidi)
1898 return 0;
1899
1900 snd_info_free_entry(rmidi->proc_entry);
1901 rmidi->proc_entry = NULL;
1902 mutex_lock(®ister_mutex);
1903 if (rmidi->ops && rmidi->ops->dev_unregister)
1904 rmidi->ops->dev_unregister(rmidi);
1905 mutex_unlock(®ister_mutex);
1906
1907 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT]);
1908 snd_rawmidi_free_substreams(&rmidi->streams[SNDRV_RAWMIDI_STREAM_OUTPUT]);
1909 if (rmidi->private_free)
1910 rmidi->private_free(rmidi);
1911 put_device(&rmidi->dev);
1912 return 0;
1913 }
1914
1915 static int snd_rawmidi_dev_free(struct snd_device *device)
1916 {
1917 struct snd_rawmidi *rmidi = device->device_data;
1918
1919 return snd_rawmidi_free(rmidi);
1920 }
1921
1922 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
1923 static void snd_rawmidi_dev_seq_free(struct snd_seq_device *device)
1924 {
1925 struct snd_rawmidi *rmidi = device->private_data;
1926
1927 rmidi->seq_dev = NULL;
1928 }
1929 #endif
1930
1931 static int snd_rawmidi_dev_register(struct snd_device *device)
1932 {
1933 int err;
1934 struct snd_info_entry *entry;
1935 char name[16];
1936 struct snd_rawmidi *rmidi = device->device_data;
1937
1938 if (rmidi->device >= SNDRV_RAWMIDI_DEVICES)
1939 return -ENOMEM;
1940 err = 0;
1941 mutex_lock(®ister_mutex);
1942 if (snd_rawmidi_search(rmidi->card, rmidi->device))
1943 err = -EBUSY;
1944 else
1945 list_add_tail(&rmidi->list, &snd_rawmidi_devices);
1946 mutex_unlock(®ister_mutex);
1947 if (err < 0)
1948 return err;
1949
1950 err = snd_register_device(SNDRV_DEVICE_TYPE_RAWMIDI,
1951 rmidi->card, rmidi->device,
1952 &snd_rawmidi_f_ops, rmidi, &rmidi->dev);
1953 if (err < 0) {
1954 rmidi_err(rmidi, "unable to register\n");
1955 goto error;
1956 }
1957 if (rmidi->ops && rmidi->ops->dev_register) {
1958 err = rmidi->ops->dev_register(rmidi);
1959 if (err < 0)
1960 goto error_unregister;
1961 }
1962 #ifdef CONFIG_SND_OSSEMUL
1963 rmidi->ossreg = 0;
1964 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
1965 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1966 rmidi->card, 0, &snd_rawmidi_f_ops,
1967 rmidi) < 0) {
1968 rmidi_err(rmidi,
1969 "unable to register OSS rawmidi device %i:%i\n",
1970 rmidi->card->number, 0);
1971 } else {
1972 rmidi->ossreg++;
1973 #ifdef SNDRV_OSS_INFO_DEV_MIDI
1974 snd_oss_info_register(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number, rmidi->name);
1975 #endif
1976 }
1977 }
1978 if ((int)rmidi->device == amidi_map[rmidi->card->number]) {
1979 if (snd_register_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI,
1980 rmidi->card, 1, &snd_rawmidi_f_ops,
1981 rmidi) < 0) {
1982 rmidi_err(rmidi,
1983 "unable to register OSS rawmidi device %i:%i\n",
1984 rmidi->card->number, 1);
1985 } else {
1986 rmidi->ossreg++;
1987 }
1988 }
1989 #endif
1990 sprintf(name, "midi%d", rmidi->device);
1991 entry = snd_info_create_card_entry(rmidi->card, name, rmidi->card->proc_root);
1992 if (entry) {
1993 entry->private_data = rmidi;
1994 entry->c.text.read = snd_rawmidi_proc_info_read;
1995 if (snd_info_register(entry) < 0) {
1996 snd_info_free_entry(entry);
1997 entry = NULL;
1998 }
1999 }
2000 rmidi->proc_entry = entry;
2001 #if IS_ENABLED(CONFIG_SND_SEQUENCER)
2002 if (!rmidi->ops || !rmidi->ops->dev_register) {
2003 if (snd_seq_device_new(rmidi->card, rmidi->device, SNDRV_SEQ_DEV_ID_MIDISYNTH, 0, &rmidi->seq_dev) >= 0) {
2004 rmidi->seq_dev->private_data = rmidi;
2005 rmidi->seq_dev->private_free = snd_rawmidi_dev_seq_free;
2006 sprintf(rmidi->seq_dev->name, "MIDI %d-%d", rmidi->card->number, rmidi->device);
2007 snd_device_register(rmidi->card, rmidi->seq_dev);
2008 }
2009 }
2010 #endif
2011 return 0;
2012
2013 error_unregister:
2014 snd_unregister_device(&rmidi->dev);
2015 error:
2016 mutex_lock(®ister_mutex);
2017 list_del(&rmidi->list);
2018 mutex_unlock(®ister_mutex);
2019 return err;
2020 }
2021
2022 static int snd_rawmidi_dev_disconnect(struct snd_device *device)
2023 {
2024 struct snd_rawmidi *rmidi = device->device_data;
2025 int dir;
2026
2027 mutex_lock(®ister_mutex);
2028 mutex_lock(&rmidi->open_mutex);
2029 wake_up(&rmidi->open_wait);
2030 list_del_init(&rmidi->list);
2031 for (dir = 0; dir < 2; dir++) {
2032 struct snd_rawmidi_substream *s;
2033
2034 list_for_each_entry(s, &rmidi->streams[dir].substreams, list) {
2035 if (s->runtime)
2036 wake_up(&s->runtime->sleep);
2037 }
2038 }
2039
2040 #ifdef CONFIG_SND_OSSEMUL
2041 if (rmidi->ossreg) {
2042 if ((int)rmidi->device == midi_map[rmidi->card->number]) {
2043 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 0);
2044 #ifdef SNDRV_OSS_INFO_DEV_MIDI
2045 snd_oss_info_unregister(SNDRV_OSS_INFO_DEV_MIDI, rmidi->card->number);
2046 #endif
2047 }
2048 if ((int)rmidi->device == amidi_map[rmidi->card->number])
2049 snd_unregister_oss_device(SNDRV_OSS_DEVICE_TYPE_MIDI, rmidi->card, 1);
2050 rmidi->ossreg = 0;
2051 }
2052 #endif
2053 snd_unregister_device(&rmidi->dev);
2054 mutex_unlock(&rmidi->open_mutex);
2055 mutex_unlock(®ister_mutex);
2056 return 0;
2057 }
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067 void snd_rawmidi_set_ops(struct snd_rawmidi *rmidi, int stream,
2068 const struct snd_rawmidi_ops *ops)
2069 {
2070 struct snd_rawmidi_substream *substream;
2071
2072 list_for_each_entry(substream, &rmidi->streams[stream].substreams, list)
2073 substream->ops = ops;
2074 }
2075 EXPORT_SYMBOL(snd_rawmidi_set_ops);
2076
2077
2078
2079
2080
2081 static int __init alsa_rawmidi_init(void)
2082 {
2083
2084 snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
2085 snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
2086 #ifdef CONFIG_SND_OSSEMUL
2087 { int i;
2088
2089 for (i = 0; i < SNDRV_CARDS; i++) {
2090 if (midi_map[i] < 0 || midi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
2091 pr_err("ALSA: rawmidi: invalid midi_map[%d] = %d\n",
2092 i, midi_map[i]);
2093 midi_map[i] = 0;
2094 }
2095 if (amidi_map[i] < 0 || amidi_map[i] >= SNDRV_RAWMIDI_DEVICES) {
2096 pr_err("ALSA: rawmidi: invalid amidi_map[%d] = %d\n",
2097 i, amidi_map[i]);
2098 amidi_map[i] = 1;
2099 }
2100 }
2101 }
2102 #endif
2103 return 0;
2104 }
2105
2106 static void __exit alsa_rawmidi_exit(void)
2107 {
2108 snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
2109 snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
2110 }
2111
2112 module_init(alsa_rawmidi_init)
2113 module_exit(alsa_rawmidi_exit)