midi.c 6.8 KB
Newer Older
M
Markus Grabner 已提交
1
/*
2
 * Line 6 Linux USB driver
M
Markus Grabner 已提交
3
 *
4
 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
M
Markus Grabner 已提交
5 6 7 8 9 10 11
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License as
 *	published by the Free Software Foundation, version 2.
 *
 */

12
#include <linux/slab.h>
13
#include <linux/usb.h>
T
Takashi Iwai 已提交
14
#include <linux/export.h>
M
Markus Grabner 已提交
15 16 17
#include <sound/core.h>
#include <sound/rawmidi.h>

18
#include "driver.h"
M
Markus Grabner 已提交
19 20
#include "midi.h"

21 22
#define line6_rawmidi_substream_midi(substream) \
	((struct snd_line6_midi *)((substream)->rmidi->private_data))
M
Markus Grabner 已提交
23

24 25
static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
			   int length);
M
Markus Grabner 已提交
26 27 28 29

/*
	Pass data received via USB to MIDI.
*/
30 31
void line6_midi_receive(struct usb_line6 *line6, unsigned char *data,
			int length)
M
Markus Grabner 已提交
32
{
33 34 35
	if (line6->line6midi->substream_receive)
		snd_rawmidi_receive(line6->line6midi->substream_receive,
				    data, length);
M
Markus Grabner 已提交
36 37 38 39 40 41 42
}

/*
	Read data from MIDI buffer and transmit them via USB.
*/
static void line6_midi_transmit(struct snd_rawmidi_substream *substream)
{
43 44
	struct usb_line6 *line6 =
	    line6_rawmidi_substream_midi(substream)->line6;
M
Markus Grabner 已提交
45
	struct snd_line6_midi *line6midi = line6->line6midi;
46
	struct midi_buffer *mb = &line6midi->midibuf_out;
47
	unsigned char chunk[LINE6_FALLBACK_MAXPACKETSIZE];
M
Markus Grabner 已提交
48 49
	int req, done;

50
	for (;;) {
51
		req = min(line6_midibuf_bytes_free(mb), line6->max_packet_size);
M
Markus Grabner 已提交
52 53
		done = snd_rawmidi_transmit_peek(substream, chunk, req);

54
		if (done == 0)
M
Markus Grabner 已提交
55 56
			break;

57
		line6_midibuf_write(mb, chunk, done);
M
Markus Grabner 已提交
58 59 60
		snd_rawmidi_transmit_ack(substream, done);
	}

61
	for (;;) {
62 63
		done = line6_midibuf_read(mb, chunk,
					  LINE6_FALLBACK_MAXPACKETSIZE);
M
Markus Grabner 已提交
64

65
		if (done == 0)
M
Markus Grabner 已提交
66 67 68 69 70 71 72 73 74
			break;

		send_midi_async(line6, chunk, done);
	}
}

/*
	Notification of completion of MIDI transmission.
*/
75
static void midi_sent(struct urb *urb)
M
Markus Grabner 已提交
76 77 78 79 80 81 82 83 84 85
{
	unsigned long flags;
	int status;
	int num;
	struct usb_line6 *line6 = (struct usb_line6 *)urb->context;

	status = urb->status;
	kfree(urb->transfer_buffer);
	usb_free_urb(urb);

86
	if (status == -ESHUTDOWN)
M
Markus Grabner 已提交
87 88
		return;

89
	spin_lock_irqsave(&line6->line6midi->lock, flags);
M
Markus Grabner 已提交
90 91
	num = --line6->line6midi->num_active_send_urbs;

92
	if (num == 0) {
M
Markus Grabner 已提交
93 94 95 96
		line6_midi_transmit(line6->line6midi->substream_transmit);
		num = line6->line6midi->num_active_send_urbs;
	}

97
	if (num == 0)
98
		wake_up(&line6->line6midi->send_wait);
M
Markus Grabner 已提交
99

100
	spin_unlock_irqrestore(&line6->line6midi->lock, flags);
M
Markus Grabner 已提交
101 102 103 104
}

/*
	Send an asynchronous MIDI message.
105
	Assumes that line6->line6midi->lock is held
M
Markus Grabner 已提交
106 107
	(i.e., this function is serialized).
*/
108 109
static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
			   int length)
M
Markus Grabner 已提交
110 111 112 113 114 115 116
{
	struct urb *urb;
	int retval;
	unsigned char *transfer_buffer;

	urb = usb_alloc_urb(0, GFP_ATOMIC);

117
	if (urb == NULL)
M
Markus Grabner 已提交
118 119
		return -ENOMEM;

120
	transfer_buffer = kmemdup(data, length, GFP_ATOMIC);
M
Markus Grabner 已提交
121

122
	if (transfer_buffer == NULL) {
M
Markus Grabner 已提交
123 124 125 126
		usb_free_urb(urb);
		return -ENOMEM;
	}

127 128
	usb_fill_int_urb(urb, line6->usbdev,
			 usb_sndbulkpipe(line6->usbdev,
129
					 line6->properties->ep_ctrl_w),
130 131
			 transfer_buffer, length, midi_sent, line6,
			 line6->interval);
M
Markus Grabner 已提交
132 133 134
	urb->actual_length = 0;
	retval = usb_submit_urb(urb, GFP_ATOMIC);

135
	if (retval < 0) {
M
Markus Grabner 已提交
136 137
		dev_err(line6->ifcdev, "usb_submit_urb failed\n");
		usb_free_urb(urb);
138
		return retval;
M
Markus Grabner 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	}

	++line6->line6midi->num_active_send_urbs;
	return 0;
}

static int line6_midi_output_open(struct snd_rawmidi_substream *substream)
{
	return 0;
}

static int line6_midi_output_close(struct snd_rawmidi_substream *substream)
{
	return 0;
}

155 156
static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream,
				      int up)
M
Markus Grabner 已提交
157 158
{
	unsigned long flags;
159 160
	struct usb_line6 *line6 =
	    line6_rawmidi_substream_midi(substream)->line6;
M
Markus Grabner 已提交
161 162

	line6->line6midi->substream_transmit = substream;
163
	spin_lock_irqsave(&line6->line6midi->lock, flags);
M
Markus Grabner 已提交
164

165
	if (line6->line6midi->num_active_send_urbs == 0)
M
Markus Grabner 已提交
166 167
		line6_midi_transmit(substream);

168
	spin_unlock_irqrestore(&line6->line6midi->lock, flags);
M
Markus Grabner 已提交
169 170 171 172
}

static void line6_midi_output_drain(struct snd_rawmidi_substream *substream)
{
173 174
	struct usb_line6 *line6 =
	    line6_rawmidi_substream_midi(substream)->line6;
175
	struct snd_line6_midi *midi = line6->line6midi;
176

177 178
	wait_event_interruptible(midi->send_wait,
				 midi->num_active_send_urbs == 0);
M
Markus Grabner 已提交
179 180 181 182 183 184 185 186 187 188 189 190
}

static int line6_midi_input_open(struct snd_rawmidi_substream *substream)
{
	return 0;
}

static int line6_midi_input_close(struct snd_rawmidi_substream *substream)
{
	return 0;
}

191 192
static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream,
				     int up)
M
Markus Grabner 已提交
193
{
194 195
	struct usb_line6 *line6 =
	    line6_rawmidi_substream_midi(substream)->line6;
M
Markus Grabner 已提交
196

197
	if (up)
M
Markus Grabner 已提交
198 199
		line6->line6midi->substream_receive = substream;
	else
200
		line6->line6midi->substream_receive = NULL;
M
Markus Grabner 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
}

static struct snd_rawmidi_ops line6_midi_output_ops = {
	.open = line6_midi_output_open,
	.close = line6_midi_output_close,
	.trigger = line6_midi_output_trigger,
	.drain = line6_midi_output_drain,
};

static struct snd_rawmidi_ops line6_midi_input_ops = {
	.open = line6_midi_input_open,
	.close = line6_midi_input_close,
	.trigger = line6_midi_input_trigger,
};

/* Create a MIDI device */
217 218
static int snd_line6_new_midi(struct usb_line6 *line6,
			      struct snd_rawmidi **rmidi_ret)
M
Markus Grabner 已提交
219 220 221 222
{
	struct snd_rawmidi *rmidi;
	int err;

223
	err = snd_rawmidi_new(line6->card, "Line 6 MIDI", 0, 1, 1, rmidi_ret);
224
	if (err < 0)
M
Markus Grabner 已提交
225 226
		return err;

227 228 229
	rmidi = *rmidi_ret;
	strcpy(rmidi->id, line6->properties->id);
	strcpy(rmidi->name, line6->properties->name);
M
Markus Grabner 已提交
230 231

	rmidi->info_flags =
232 233
	    SNDRV_RAWMIDI_INFO_OUTPUT |
	    SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
M
Markus Grabner 已提交
234

235 236 237 238
	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
			    &line6_midi_output_ops);
	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
			    &line6_midi_input_ops);
M
Markus Grabner 已提交
239 240 241 242
	return 0;
}

/* MIDI device destructor */
243
static void snd_line6_midi_free(struct snd_rawmidi *rmidi)
M
Markus Grabner 已提交
244
{
245
	struct snd_line6_midi *line6midi = rmidi->private_data;
246

247 248
	line6_midibuf_destroy(&line6midi->midibuf_in);
	line6_midibuf_destroy(&line6midi->midibuf_out);
249
	kfree(line6midi);
M
Markus Grabner 已提交
250 251 252
}

/*
253
	Initialize the Line 6 MIDI subsystem.
M
Markus Grabner 已提交
254 255 256 257
*/
int line6_init_midi(struct usb_line6 *line6)
{
	int err;
258
	struct snd_rawmidi *rmidi;
M
Markus Grabner 已提交
259 260
	struct snd_line6_midi *line6midi;

261
	if (!(line6->properties->capabilities & LINE6_CAP_CONTROL)) {
262 263 264
		/* skip MIDI initialization and report success */
		return 0;
	}
M
Markus Grabner 已提交
265

266 267 268
	err = snd_line6_new_midi(line6, &rmidi);
	if (err < 0)
		return err;
M
Markus Grabner 已提交
269

270 271
	line6midi = kzalloc(sizeof(struct snd_line6_midi), GFP_KERNEL);
	if (!line6midi)
M
Markus Grabner 已提交
272 273
		return -ENOMEM;

274 275
	rmidi->private_data = line6midi;
	rmidi->private_free = snd_line6_midi_free;
M
Markus Grabner 已提交
276

277
	init_waitqueue_head(&line6midi->send_wait);
278
	spin_lock_init(&line6midi->lock);
M
Markus Grabner 已提交
279 280
	line6midi->line6 = line6;

281
	err = line6_midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0);
282
	if (err < 0)
M
Markus Grabner 已提交
283 284
		return err;

285
	err = line6_midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1);
286
	if (err < 0)
M
Markus Grabner 已提交
287 288
		return err;

289
	line6->line6midi = line6midi;
M
Markus Grabner 已提交
290 291
	return 0;
}
T
Takashi Iwai 已提交
292
EXPORT_SYMBOL_GPL(line6_init_midi);