Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Virtual Raw MIDI client on Sequencer
0004  *
0005  *  Copyright (c) 2000 by Takashi Iwai <tiwai@suse.de>,
0006  *                        Jaroslav Kysela <perex@perex.cz>
0007  */
0008 
0009 /*
0010  * Virtual Raw MIDI client
0011  *
0012  * The virtual rawmidi client is a sequencer client which associate
0013  * a rawmidi device file.  The created rawmidi device file can be
0014  * accessed as a normal raw midi, but its MIDI source and destination
0015  * are arbitrary.  For example, a user-client software synth connected
0016  * to this port can be used as a normal midi device as well.
0017  *
0018  * The virtual rawmidi device accepts also multiple opens.  Each file
0019  * has its own input buffer, so that no conflict would occur.  The drain
0020  * of input/output buffer acts only to the local buffer.
0021  *
0022  */
0023 
0024 #include <linux/init.h>
0025 #include <linux/wait.h>
0026 #include <linux/module.h>
0027 #include <linux/slab.h>
0028 #include <sound/core.h>
0029 #include <sound/rawmidi.h>
0030 #include <sound/info.h>
0031 #include <sound/control.h>
0032 #include <sound/minors.h>
0033 #include <sound/seq_kernel.h>
0034 #include <sound/seq_midi_event.h>
0035 #include <sound/seq_virmidi.h>
0036 
0037 MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
0038 MODULE_DESCRIPTION("Virtual Raw MIDI client on Sequencer");
0039 MODULE_LICENSE("GPL");
0040 
0041 /*
0042  * initialize an event record
0043  */
0044 static void snd_virmidi_init_event(struct snd_virmidi *vmidi,
0045                    struct snd_seq_event *ev)
0046 {
0047     memset(ev, 0, sizeof(*ev));
0048     ev->source.port = vmidi->port;
0049     switch (vmidi->seq_mode) {
0050     case SNDRV_VIRMIDI_SEQ_DISPATCH:
0051         ev->dest.client = SNDRV_SEQ_ADDRESS_SUBSCRIBERS;
0052         break;
0053     case SNDRV_VIRMIDI_SEQ_ATTACH:
0054         /* FIXME: source and destination are same - not good.. */
0055         ev->dest.client = vmidi->client;
0056         ev->dest.port = vmidi->port;
0057         break;
0058     }
0059     ev->type = SNDRV_SEQ_EVENT_NONE;
0060 }
0061 
0062 /*
0063  * decode input event and put to read buffer of each opened file
0064  */
0065 static int snd_virmidi_dev_receive_event(struct snd_virmidi_dev *rdev,
0066                      struct snd_seq_event *ev,
0067                      bool atomic)
0068 {
0069     struct snd_virmidi *vmidi;
0070     unsigned char msg[4];
0071     int len;
0072 
0073     if (atomic)
0074         read_lock(&rdev->filelist_lock);
0075     else
0076         down_read(&rdev->filelist_sem);
0077     list_for_each_entry(vmidi, &rdev->filelist, list) {
0078         if (!READ_ONCE(vmidi->trigger))
0079             continue;
0080         if (ev->type == SNDRV_SEQ_EVENT_SYSEX) {
0081             if ((ev->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
0082                 continue;
0083             snd_seq_dump_var_event(ev, (snd_seq_dump_func_t)snd_rawmidi_receive, vmidi->substream);
0084             snd_midi_event_reset_decode(vmidi->parser);
0085         } else {
0086             len = snd_midi_event_decode(vmidi->parser, msg, sizeof(msg), ev);
0087             if (len > 0)
0088                 snd_rawmidi_receive(vmidi->substream, msg, len);
0089         }
0090     }
0091     if (atomic)
0092         read_unlock(&rdev->filelist_lock);
0093     else
0094         up_read(&rdev->filelist_sem);
0095 
0096     return 0;
0097 }
0098 
0099 /*
0100  * event handler of virmidi port
0101  */
0102 static int snd_virmidi_event_input(struct snd_seq_event *ev, int direct,
0103                    void *private_data, int atomic, int hop)
0104 {
0105     struct snd_virmidi_dev *rdev;
0106 
0107     rdev = private_data;
0108     if (!(rdev->flags & SNDRV_VIRMIDI_USE))
0109         return 0; /* ignored */
0110     return snd_virmidi_dev_receive_event(rdev, ev, atomic);
0111 }
0112 
0113 /*
0114  * trigger rawmidi stream for input
0115  */
0116 static void snd_virmidi_input_trigger(struct snd_rawmidi_substream *substream, int up)
0117 {
0118     struct snd_virmidi *vmidi = substream->runtime->private_data;
0119 
0120     WRITE_ONCE(vmidi->trigger, !!up);
0121 }
0122 
0123 /* process rawmidi bytes and send events;
0124  * we need no lock here for vmidi->event since it's handled only in this work
0125  */
0126 static void snd_vmidi_output_work(struct work_struct *work)
0127 {
0128     struct snd_virmidi *vmidi;
0129     struct snd_rawmidi_substream *substream;
0130     unsigned char input;
0131     int ret;
0132 
0133     vmidi = container_of(work, struct snd_virmidi, output_work);
0134     substream = vmidi->substream;
0135 
0136     /* discard the outputs in dispatch mode unless subscribed */
0137     if (vmidi->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH &&
0138         !(vmidi->rdev->flags & SNDRV_VIRMIDI_SUBSCRIBE)) {
0139         snd_rawmidi_proceed(substream);
0140         return;
0141     }
0142 
0143     while (READ_ONCE(vmidi->trigger)) {
0144         if (snd_rawmidi_transmit(substream, &input, 1) != 1)
0145             break;
0146         if (!snd_midi_event_encode_byte(vmidi->parser, input,
0147                         &vmidi->event))
0148             continue;
0149         if (vmidi->event.type != SNDRV_SEQ_EVENT_NONE) {
0150             ret = snd_seq_kernel_client_dispatch(vmidi->client,
0151                                  &vmidi->event,
0152                                  false, 0);
0153             vmidi->event.type = SNDRV_SEQ_EVENT_NONE;
0154             if (ret < 0)
0155                 break;
0156         }
0157         /* rawmidi input might be huge, allow to have a break */
0158         cond_resched();
0159     }
0160 }
0161 
0162 /*
0163  * trigger rawmidi stream for output
0164  */
0165 static void snd_virmidi_output_trigger(struct snd_rawmidi_substream *substream, int up)
0166 {
0167     struct snd_virmidi *vmidi = substream->runtime->private_data;
0168 
0169     WRITE_ONCE(vmidi->trigger, !!up);
0170     if (up)
0171         queue_work(system_highpri_wq, &vmidi->output_work);
0172 }
0173 
0174 /*
0175  * open rawmidi handle for input
0176  */
0177 static int snd_virmidi_input_open(struct snd_rawmidi_substream *substream)
0178 {
0179     struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
0180     struct snd_rawmidi_runtime *runtime = substream->runtime;
0181     struct snd_virmidi *vmidi;
0182 
0183     vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
0184     if (vmidi == NULL)
0185         return -ENOMEM;
0186     vmidi->substream = substream;
0187     if (snd_midi_event_new(0, &vmidi->parser) < 0) {
0188         kfree(vmidi);
0189         return -ENOMEM;
0190     }
0191     vmidi->seq_mode = rdev->seq_mode;
0192     vmidi->client = rdev->client;
0193     vmidi->port = rdev->port;   
0194     runtime->private_data = vmidi;
0195     down_write(&rdev->filelist_sem);
0196     write_lock_irq(&rdev->filelist_lock);
0197     list_add_tail(&vmidi->list, &rdev->filelist);
0198     write_unlock_irq(&rdev->filelist_lock);
0199     up_write(&rdev->filelist_sem);
0200     vmidi->rdev = rdev;
0201     return 0;
0202 }
0203 
0204 /*
0205  * open rawmidi handle for output
0206  */
0207 static int snd_virmidi_output_open(struct snd_rawmidi_substream *substream)
0208 {
0209     struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
0210     struct snd_rawmidi_runtime *runtime = substream->runtime;
0211     struct snd_virmidi *vmidi;
0212 
0213     vmidi = kzalloc(sizeof(*vmidi), GFP_KERNEL);
0214     if (vmidi == NULL)
0215         return -ENOMEM;
0216     vmidi->substream = substream;
0217     if (snd_midi_event_new(MAX_MIDI_EVENT_BUF, &vmidi->parser) < 0) {
0218         kfree(vmidi);
0219         return -ENOMEM;
0220     }
0221     vmidi->seq_mode = rdev->seq_mode;
0222     vmidi->client = rdev->client;
0223     vmidi->port = rdev->port;
0224     snd_virmidi_init_event(vmidi, &vmidi->event);
0225     vmidi->rdev = rdev;
0226     INIT_WORK(&vmidi->output_work, snd_vmidi_output_work);
0227     runtime->private_data = vmidi;
0228     return 0;
0229 }
0230 
0231 /*
0232  * close rawmidi handle for input
0233  */
0234 static int snd_virmidi_input_close(struct snd_rawmidi_substream *substream)
0235 {
0236     struct snd_virmidi_dev *rdev = substream->rmidi->private_data;
0237     struct snd_virmidi *vmidi = substream->runtime->private_data;
0238 
0239     down_write(&rdev->filelist_sem);
0240     write_lock_irq(&rdev->filelist_lock);
0241     list_del(&vmidi->list);
0242     write_unlock_irq(&rdev->filelist_lock);
0243     up_write(&rdev->filelist_sem);
0244     snd_midi_event_free(vmidi->parser);
0245     substream->runtime->private_data = NULL;
0246     kfree(vmidi);
0247     return 0;
0248 }
0249 
0250 /*
0251  * close rawmidi handle for output
0252  */
0253 static int snd_virmidi_output_close(struct snd_rawmidi_substream *substream)
0254 {
0255     struct snd_virmidi *vmidi = substream->runtime->private_data;
0256 
0257     WRITE_ONCE(vmidi->trigger, false); /* to be sure */
0258     cancel_work_sync(&vmidi->output_work);
0259     snd_midi_event_free(vmidi->parser);
0260     substream->runtime->private_data = NULL;
0261     kfree(vmidi);
0262     return 0;
0263 }
0264 
0265 /*
0266  * drain output work queue
0267  */
0268 static void snd_virmidi_output_drain(struct snd_rawmidi_substream *substream)
0269 {
0270     struct snd_virmidi *vmidi = substream->runtime->private_data;
0271 
0272     flush_work(&vmidi->output_work);
0273 }
0274 
0275 /*
0276  * subscribe callback - allow output to rawmidi device
0277  */
0278 static int snd_virmidi_subscribe(void *private_data,
0279                  struct snd_seq_port_subscribe *info)
0280 {
0281     struct snd_virmidi_dev *rdev;
0282 
0283     rdev = private_data;
0284     if (!try_module_get(rdev->card->module))
0285         return -EFAULT;
0286     rdev->flags |= SNDRV_VIRMIDI_SUBSCRIBE;
0287     return 0;
0288 }
0289 
0290 /*
0291  * unsubscribe callback - disallow output to rawmidi device
0292  */
0293 static int snd_virmidi_unsubscribe(void *private_data,
0294                    struct snd_seq_port_subscribe *info)
0295 {
0296     struct snd_virmidi_dev *rdev;
0297 
0298     rdev = private_data;
0299     rdev->flags &= ~SNDRV_VIRMIDI_SUBSCRIBE;
0300     module_put(rdev->card->module);
0301     return 0;
0302 }
0303 
0304 
0305 /*
0306  * use callback - allow input to rawmidi device
0307  */
0308 static int snd_virmidi_use(void *private_data,
0309                struct snd_seq_port_subscribe *info)
0310 {
0311     struct snd_virmidi_dev *rdev;
0312 
0313     rdev = private_data;
0314     if (!try_module_get(rdev->card->module))
0315         return -EFAULT;
0316     rdev->flags |= SNDRV_VIRMIDI_USE;
0317     return 0;
0318 }
0319 
0320 /*
0321  * unuse callback - disallow input to rawmidi device
0322  */
0323 static int snd_virmidi_unuse(void *private_data,
0324                  struct snd_seq_port_subscribe *info)
0325 {
0326     struct snd_virmidi_dev *rdev;
0327 
0328     rdev = private_data;
0329     rdev->flags &= ~SNDRV_VIRMIDI_USE;
0330     module_put(rdev->card->module);
0331     return 0;
0332 }
0333 
0334 
0335 /*
0336  *  Register functions
0337  */
0338 
0339 static const struct snd_rawmidi_ops snd_virmidi_input_ops = {
0340     .open = snd_virmidi_input_open,
0341     .close = snd_virmidi_input_close,
0342     .trigger = snd_virmidi_input_trigger,
0343 };
0344 
0345 static const struct snd_rawmidi_ops snd_virmidi_output_ops = {
0346     .open = snd_virmidi_output_open,
0347     .close = snd_virmidi_output_close,
0348     .trigger = snd_virmidi_output_trigger,
0349     .drain = snd_virmidi_output_drain,
0350 };
0351 
0352 /*
0353  * create a sequencer client and a port
0354  */
0355 static int snd_virmidi_dev_attach_seq(struct snd_virmidi_dev *rdev)
0356 {
0357     int client;
0358     struct snd_seq_port_callback pcallbacks;
0359     struct snd_seq_port_info *pinfo;
0360     int err;
0361 
0362     if (rdev->client >= 0)
0363         return 0;
0364 
0365     pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
0366     if (!pinfo) {
0367         err = -ENOMEM;
0368         goto __error;
0369     }
0370 
0371     client = snd_seq_create_kernel_client(rdev->card, rdev->device,
0372                           "%s %d-%d", rdev->rmidi->name,
0373                           rdev->card->number,
0374                           rdev->device);
0375     if (client < 0) {
0376         err = client;
0377         goto __error;
0378     }
0379     rdev->client = client;
0380 
0381     /* create a port */
0382     pinfo->addr.client = client;
0383     sprintf(pinfo->name, "VirMIDI %d-%d", rdev->card->number, rdev->device);
0384     /* set all capabilities */
0385     pinfo->capability |= SNDRV_SEQ_PORT_CAP_WRITE | SNDRV_SEQ_PORT_CAP_SYNC_WRITE | SNDRV_SEQ_PORT_CAP_SUBS_WRITE;
0386     pinfo->capability |= SNDRV_SEQ_PORT_CAP_READ | SNDRV_SEQ_PORT_CAP_SYNC_READ | SNDRV_SEQ_PORT_CAP_SUBS_READ;
0387     pinfo->capability |= SNDRV_SEQ_PORT_CAP_DUPLEX;
0388     pinfo->type = SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC
0389         | SNDRV_SEQ_PORT_TYPE_SOFTWARE
0390         | SNDRV_SEQ_PORT_TYPE_PORT;
0391     pinfo->midi_channels = 16;
0392     memset(&pcallbacks, 0, sizeof(pcallbacks));
0393     pcallbacks.owner = THIS_MODULE;
0394     pcallbacks.private_data = rdev;
0395     pcallbacks.subscribe = snd_virmidi_subscribe;
0396     pcallbacks.unsubscribe = snd_virmidi_unsubscribe;
0397     pcallbacks.use = snd_virmidi_use;
0398     pcallbacks.unuse = snd_virmidi_unuse;
0399     pcallbacks.event_input = snd_virmidi_event_input;
0400     pinfo->kernel = &pcallbacks;
0401     err = snd_seq_kernel_client_ctl(client, SNDRV_SEQ_IOCTL_CREATE_PORT, pinfo);
0402     if (err < 0) {
0403         snd_seq_delete_kernel_client(client);
0404         rdev->client = -1;
0405         goto __error;
0406     }
0407 
0408     rdev->port = pinfo->addr.port;
0409     err = 0; /* success */
0410 
0411  __error:
0412     kfree(pinfo);
0413     return err;
0414 }
0415 
0416 
0417 /*
0418  * release the sequencer client
0419  */
0420 static void snd_virmidi_dev_detach_seq(struct snd_virmidi_dev *rdev)
0421 {
0422     if (rdev->client >= 0) {
0423         snd_seq_delete_kernel_client(rdev->client);
0424         rdev->client = -1;
0425     }
0426 }
0427 
0428 /*
0429  * register the device
0430  */
0431 static int snd_virmidi_dev_register(struct snd_rawmidi *rmidi)
0432 {
0433     struct snd_virmidi_dev *rdev = rmidi->private_data;
0434     int err;
0435 
0436     switch (rdev->seq_mode) {
0437     case SNDRV_VIRMIDI_SEQ_DISPATCH:
0438         err = snd_virmidi_dev_attach_seq(rdev);
0439         if (err < 0)
0440             return err;
0441         break;
0442     case SNDRV_VIRMIDI_SEQ_ATTACH:
0443         if (rdev->client == 0)
0444             return -EINVAL;
0445         /* should check presence of port more strictly.. */
0446         break;
0447     default:
0448         pr_err("ALSA: seq_virmidi: seq_mode is not set: %d\n", rdev->seq_mode);
0449         return -EINVAL;
0450     }
0451     return 0;
0452 }
0453 
0454 
0455 /*
0456  * unregister the device
0457  */
0458 static int snd_virmidi_dev_unregister(struct snd_rawmidi *rmidi)
0459 {
0460     struct snd_virmidi_dev *rdev = rmidi->private_data;
0461 
0462     if (rdev->seq_mode == SNDRV_VIRMIDI_SEQ_DISPATCH)
0463         snd_virmidi_dev_detach_seq(rdev);
0464     return 0;
0465 }
0466 
0467 /*
0468  *
0469  */
0470 static const struct snd_rawmidi_global_ops snd_virmidi_global_ops = {
0471     .dev_register = snd_virmidi_dev_register,
0472     .dev_unregister = snd_virmidi_dev_unregister,
0473 };
0474 
0475 /*
0476  * free device
0477  */
0478 static void snd_virmidi_free(struct snd_rawmidi *rmidi)
0479 {
0480     struct snd_virmidi_dev *rdev = rmidi->private_data;
0481     kfree(rdev);
0482 }
0483 
0484 /*
0485  * create a new device
0486  *
0487  */
0488 /* exported */
0489 int snd_virmidi_new(struct snd_card *card, int device, struct snd_rawmidi **rrmidi)
0490 {
0491     struct snd_rawmidi *rmidi;
0492     struct snd_virmidi_dev *rdev;
0493     int err;
0494     
0495     *rrmidi = NULL;
0496     err = snd_rawmidi_new(card, "VirMidi", device,
0497                   16,   /* may be configurable */
0498                   16,   /* may be configurable */
0499                   &rmidi);
0500     if (err < 0)
0501         return err;
0502     strcpy(rmidi->name, rmidi->id);
0503     rdev = kzalloc(sizeof(*rdev), GFP_KERNEL);
0504     if (rdev == NULL) {
0505         snd_device_free(card, rmidi);
0506         return -ENOMEM;
0507     }
0508     rdev->card = card;
0509     rdev->rmidi = rmidi;
0510     rdev->device = device;
0511     rdev->client = -1;
0512     init_rwsem(&rdev->filelist_sem);
0513     rwlock_init(&rdev->filelist_lock);
0514     INIT_LIST_HEAD(&rdev->filelist);
0515     rdev->seq_mode = SNDRV_VIRMIDI_SEQ_DISPATCH;
0516     rmidi->private_data = rdev;
0517     rmidi->private_free = snd_virmidi_free;
0518     rmidi->ops = &snd_virmidi_global_ops;
0519     snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_virmidi_input_ops);
0520     snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_virmidi_output_ops);
0521     rmidi->info_flags = SNDRV_RAWMIDI_INFO_INPUT |
0522                 SNDRV_RAWMIDI_INFO_OUTPUT |
0523                 SNDRV_RAWMIDI_INFO_DUPLEX;
0524     *rrmidi = rmidi;
0525     return 0;
0526 }
0527 EXPORT_SYMBOL(snd_virmidi_new);