summaryrefslogtreecommitdiffstats
path: root/kernel/sound/firewire/digi00x/digi00x-midi.c
blob: 1a72a382b384beb5b8ab92c3f65c72920c4d510b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* User memory access */
#include "qemu/osdep.h"
#include "qemu/cutils.h"

#include "qemu.h"

/* copy_from_user() and copy_to_user() are usually used to copy data
 * buffers between the target and host.  These internally perform
 * locking/unlocking of the memory.
 */
abi_long copy_from_user(void .highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * digi00x-midi.h - a part of driver for Digidesign Digi 002/003 family
 *
 * Copyright (c) 2014-2015 Takashi Sakamoto
 *
 * Licensed under the terms of the GNU General Public License, version 2.
 */

#include "digi00x.h"

static int midi_phys_open(struct snd_rawmidi_substream *substream)
{
	struct snd_dg00x *dg00x = substream->rmidi->private_data;
	int err;

	err = snd_dg00x_stream_lock_try(dg00x);
	if (err < 0)
		return err;

	mutex_lock(&dg00x->mutex);
	dg00x->substreams_counter++;
	err = snd_dg00x_stream_start_duplex(dg00x, 0);
	mutex_unlock(&dg00x->mutex);
	if (err < 0)
		snd_dg00x_stream_lock_release(dg00x);

	return err;
}

static int midi_phys_close(struct snd_rawmidi_substream *substream)
{
	struct snd_dg00x *dg00x = substream->rmidi->private_data;

	mutex_lock(&dg00x->mutex);
	dg00x->substreams_counter--;
	snd_dg00x_stream_stop_duplex(dg00x);
	mutex_unlock(&dg00x->mutex);

	snd_dg00x_stream_lock_release(dg00x);
	return 0;
}

static void midi_phys_capture_trigger(struct snd_rawmidi_substream *substream,
				      int up)
{
	struct snd_dg00x *dg00x = substream->rmidi->private_data;
	unsigned long flags;

	spin_lock_irqsave(&dg00x->lock, flags);

	if (up)
		amdtp_dot_midi_trigger(&dg00x->tx_stream, substream->number,
				       substream);
	else
		amdtp_dot_midi_trigger(&dg00x->tx_stream, substream->number,
				       NULL);

	spin_unlock_irqrestore(&dg00x->lock, flags);
}

static void midi_phys_playback_trigger(struct snd_rawmidi_substream *substream,
				       int up)
{
	struct snd_dg00x *dg00x = substream->rmidi->private_data;
	unsigned long flags;

	spin_lock_irqsave(&dg00x->lock, flags);

	if (up)
		amdtp_dot_midi_trigger(&dg00x->rx_stream, substream->number,
				       substream);
	else
		amdtp_dot_midi_trigger(&dg00x->rx_stream, substream->number,
				       NULL);

	spin_unlock_irqrestore(&dg00x->lock, flags);
}

static struct snd_rawmidi_ops midi_phys_capture_ops = {
	.open		= midi_phys_open,
	.close		= midi_phys_close,
	.trigger	= midi_phys_capture_trigger,
};

static struct snd_rawmidi_ops midi_phys_playback_ops = {
	.open		= midi_phys_open,
	.close		= midi_phys_close,
	.trigger	= midi_phys_playback_trigger,
};

static int midi_ctl_open(struct snd_rawmidi_substream *substream)
{
	/* Do nothing. */
	return 0;
}

static int midi_ctl_capture_close(struct snd_rawmidi_substream *substream)
{
	/* Do nothing. */
	return 0;
}

static int midi_ctl_playback_close(struct snd_rawmidi_substream *substream)
{
	struct snd_dg00x *dg00x = substream->rmidi->private_data;

	snd_fw_async_midi_port_finish(&dg00x->out_control);

	return 0;
}

static void midi_ctl_capture_trigger(struct snd_rawmidi_substream *substream,
				     int up)
{
	struct snd_dg00x *dg00x = substream->rmidi->private_data;
	unsigned long flags;

	spin_lock_irqsave(&dg00x->lock, flags);

	if (up)
		dg00x->in_control = substream;
	else
		dg00x->in_control = NULL;

	spin_unlock_irqrestore(&dg00x->lock, flags);
}

static void midi_ctl_playback_trigger(struct snd_rawmidi_substream *substream,
				      int up)
{
	struct snd_dg00x *dg00x = substream->rmidi->private_data;
	unsigned long flags;

	spin_lock_irqsave(&dg00x->lock, flags);

	if (up)
		snd_fw_async_midi_port_run(&dg00x->out_control, substream);

	spin_unlock_irqrestore(&dg00x->lock, flags);
}

static struct snd_rawmidi_ops midi_ctl_capture_ops = {
	.open		= midi_ctl_open,
	.close		= midi_ctl_capture_close,
	.trigger	= midi_ctl_capture_trigger,
};

static struct snd_rawmidi_ops midi_ctl_playback_ops = {
	.open		= midi_ctl_open,
	.close		= midi_ctl_playback_close,
	.trigger	= midi_ctl_playback_trigger,
};

static void set_midi_substream_names(struct snd_dg00x *dg00x,
				     struct snd_rawmidi_str *str,
				     bool is_ctl)
{
	struct snd_rawmidi_substream *subs;

	list_for_each_entry(subs, &str->substreams, list) {
		if (!is_ctl)
			snprintf(subs->name, sizeof(subs->name),
				 "%s MIDI %d",
				 dg00x->card->shortname, subs->number + 1);
		else
			/* This port is for asynchronous transaction. */
			snprintf(subs->name, sizeof(subs->name),
				 "%s control",
				 dg00x->card->shortname);
	}
}

int snd_dg00x_create_midi_devices(struct snd_dg00x *dg00x)
{
	struct snd_rawmidi *rmidi[2];
	struct snd_rawmidi_str *str;
	unsigned int i;
	int err;

	/* Add physical midi ports. */
	err = snd_rawmidi_new(dg00x->card, dg00x->card->driver, 0,
			DOT_MIDI_OUT_PORTS, DOT_MIDI_IN_PORTS, &rmidi[0]);
	if (err < 0)
		return err;

	snprintf(rmidi[0]->name, sizeof(rmidi[0]->name),
		 "%s MIDI", dg00x->card->shortname);

	snd_rawmidi_set_ops(rmidi[0], SNDRV_RAWMIDI_STREAM_INPUT,
			    &midi_phys_capture_ops);
	snd_rawmidi_set_ops(rmidi[0], SNDRV_RAWMIDI_STREAM_OUTPUT,
			    &midi_phys_playback_ops);

	/* Add a pair of control midi ports. */
	err = snd_rawmidi_new(dg00x->card, dg00x->card->driver, 1,
			      1, 1, &rmidi[1]);
	if (err < 0)
		return err;

	snprintf(rmidi[1]->name, sizeof(rmidi[1]->name),
		 "%s control", dg00x->card->shortname);

	snd_rawmidi_set_ops(rmidi[1], SNDRV_RAWMIDI_STREAM_INPUT,
			    &midi_ctl_capture_ops);
	snd_rawmidi_set_ops(rmidi[1], SNDRV_RAWMIDI_STREAM_OUTPUT,
			    &midi_ctl_playback_ops);

	for (i = 0; i < ARRAY_SIZE(rmidi); i++) {
		rmidi[i]->private_data = dg00x;

		rmidi[i]->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
		str = &rmidi[i]->streams[SNDRV_RAWMIDI_STREAM_INPUT];
		set_midi_substream_names(dg00x, str, i);

		rmidi[i]->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
		str = &rmidi[i]->streams[SNDRV_RAWMIDI_STREAM_OUTPUT];
		set_midi_substream_names(dg00x, str, i);

		rmidi[i]->info_flags |= SNDRV_RAWMIDI_INFO_DUPLEX;
	}

	return 0;
}