midi.c 7.1 KB
Newer Older
M
Markus Grabner 已提交
1
/*
2
 * Line 6 Linux USB driver - 0.9.1beta
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 21
#include "midi.h"
#include "usbdefs.h"

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

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

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

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

	spin_lock_irqsave(&line6->line6midi->midi_transmit_lock, flags);

54
	for (;;) {
55
		req = min(line6_midibuf_bytes_free(mb), line6->max_packet_size);
M
Markus Grabner 已提交
56 57
		done = snd_rawmidi_transmit_peek(substream, chunk, req);

58
		if (done == 0)
M
Markus Grabner 已提交
59 60
			break;

61
		line6_midibuf_write(mb, chunk, done);
M
Markus Grabner 已提交
62 63 64
		snd_rawmidi_transmit_ack(substream, done);
	}

65
	for (;;) {
66 67
		done = line6_midibuf_read(mb, chunk,
					  LINE6_FALLBACK_MAXPACKETSIZE);
M
Markus Grabner 已提交
68

69
		if (done == 0)
M
Markus Grabner 已提交
70 71 72 73 74 75 76 77 78 79 80
			break;

		send_midi_async(line6, chunk, done);
	}

	spin_unlock_irqrestore(&line6->line6midi->midi_transmit_lock, flags);
}

/*
	Notification of completion of MIDI transmission.
*/
81
static void midi_sent(struct urb *urb)
M
Markus Grabner 已提交
82 83 84 85 86 87 88 89 90 91
{
	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);

92
	if (status == -ESHUTDOWN)
M
Markus Grabner 已提交
93 94 95 96 97
		return;

	spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags);
	num = --line6->line6midi->num_active_send_urbs;

98
	if (num == 0) {
M
Markus Grabner 已提交
99 100 101 102
		line6_midi_transmit(line6->line6midi->substream_transmit);
		num = line6->line6midi->num_active_send_urbs;
	}

103
	if (num == 0)
104
		wake_up(&line6->line6midi->send_wait);
M
Markus Grabner 已提交
105 106 107 108 109 110 111 112 113

	spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags);
}

/*
	Send an asynchronous MIDI message.
	Assumes that line6->line6midi->send_urb_lock is held
	(i.e., this function is serialized).
*/
114 115
static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
			   int length)
M
Markus Grabner 已提交
116 117 118 119 120 121 122
{
	struct urb *urb;
	int retval;
	unsigned char *transfer_buffer;

	urb = usb_alloc_urb(0, GFP_ATOMIC);

123
	if (urb == NULL)
M
Markus Grabner 已提交
124 125
		return -ENOMEM;

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

128
	if (transfer_buffer == NULL) {
M
Markus Grabner 已提交
129 130 131 132
		usb_free_urb(urb);
		return -ENOMEM;
	}

133 134
	usb_fill_int_urb(urb, line6->usbdev,
			 usb_sndbulkpipe(line6->usbdev,
135
					 line6->properties->ep_ctrl_w),
136 137
			 transfer_buffer, length, midi_sent, line6,
			 line6->interval);
M
Markus Grabner 已提交
138 139 140
	urb->actual_length = 0;
	retval = usb_submit_urb(urb, GFP_ATOMIC);

141
	if (retval < 0) {
M
Markus Grabner 已提交
142 143
		dev_err(line6->ifcdev, "usb_submit_urb failed\n");
		usb_free_urb(urb);
144
		return retval;
M
Markus Grabner 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
	}

	++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;
}

161 162
static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream,
				      int up)
M
Markus Grabner 已提交
163 164
{
	unsigned long flags;
165 166
	struct usb_line6 *line6 =
	    line6_rawmidi_substream_midi(substream)->line6;
M
Markus Grabner 已提交
167 168 169 170

	line6->line6midi->substream_transmit = substream;
	spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags);

171
	if (line6->line6midi->num_active_send_urbs == 0)
M
Markus Grabner 已提交
172 173 174 175 176 177 178
		line6_midi_transmit(substream);

	spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags);
}

static void line6_midi_output_drain(struct snd_rawmidi_substream *substream)
{
179 180
	struct usb_line6 *line6 =
	    line6_rawmidi_substream_midi(substream)->line6;
181
	struct snd_line6_midi *midi = line6->line6midi;
182

183 184
	wait_event_interruptible(midi->send_wait,
				 midi->num_active_send_urbs == 0);
M
Markus Grabner 已提交
185 186 187 188 189 190 191 192 193 194 195 196
}

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;
}

197 198
static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream,
				     int up)
M
Markus Grabner 已提交
199
{
200 201
	struct usb_line6 *line6 =
	    line6_rawmidi_substream_midi(substream)->line6;
M
Markus Grabner 已提交
202

203
	if (up)
M
Markus Grabner 已提交
204 205
		line6->line6midi->substream_receive = substream;
	else
206
		line6->line6midi->substream_receive = NULL;
M
Markus Grabner 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
}

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 */
223 224
static int snd_line6_new_midi(struct usb_line6 *line6,
			      struct snd_rawmidi **rmidi_ret)
M
Markus Grabner 已提交
225 226 227 228
{
	struct snd_rawmidi *rmidi;
	int err;

229
	err = snd_rawmidi_new(line6->card, "Line 6 MIDI", 0, 1, 1, rmidi_ret);
230
	if (err < 0)
M
Markus Grabner 已提交
231 232
		return err;

233 234 235
	rmidi = *rmidi_ret;
	strcpy(rmidi->id, line6->properties->id);
	strcpy(rmidi->name, line6->properties->name);
M
Markus Grabner 已提交
236 237

	rmidi->info_flags =
238 239
	    SNDRV_RAWMIDI_INFO_OUTPUT |
	    SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
M
Markus Grabner 已提交
240

241 242 243 244
	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 已提交
245 246 247 248
	return 0;
}

/* MIDI device destructor */
249
static void snd_line6_midi_free(struct snd_rawmidi *rmidi)
M
Markus Grabner 已提交
250
{
251
	struct snd_line6_midi *line6midi = rmidi->private_data;
252

253 254
	line6_midibuf_destroy(&line6midi->midibuf_in);
	line6_midibuf_destroy(&line6midi->midibuf_out);
255
	kfree(line6midi);
M
Markus Grabner 已提交
256 257 258
}

/*
259
	Initialize the Line 6 MIDI subsystem.
M
Markus Grabner 已提交
260 261 262 263
*/
int line6_init_midi(struct usb_line6 *line6)
{
	int err;
264
	struct snd_rawmidi *rmidi;
M
Markus Grabner 已提交
265 266
	struct snd_line6_midi *line6midi;

267
	if (!(line6->properties->capabilities & LINE6_CAP_CONTROL)) {
268 269 270
		/* skip MIDI initialization and report success */
		return 0;
	}
M
Markus Grabner 已提交
271

272 273 274
	err = snd_line6_new_midi(line6, &rmidi);
	if (err < 0)
		return err;
M
Markus Grabner 已提交
275

276 277
	line6midi = kzalloc(sizeof(struct snd_line6_midi), GFP_KERNEL);
	if (!line6midi)
M
Markus Grabner 已提交
278 279
		return -ENOMEM;

280 281
	rmidi->private_data = line6midi;
	rmidi->private_free = snd_line6_midi_free;
M
Markus Grabner 已提交
282

283 284 285
	init_waitqueue_head(&line6midi->send_wait);
	spin_lock_init(&line6midi->send_urb_lock);
	spin_lock_init(&line6midi->midi_transmit_lock);
M
Markus Grabner 已提交
286 287
	line6midi->line6 = line6;

288
	err = line6_midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0);
289
	if (err < 0)
M
Markus Grabner 已提交
290 291
		return err;

292
	err = line6_midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1);
293
	if (err < 0)
M
Markus Grabner 已提交
294 295
		return err;

296
	line6->line6midi = line6midi;
M
Markus Grabner 已提交
297 298
	return 0;
}
T
Takashi Iwai 已提交
299
EXPORT_SYMBOL_GPL(line6_init_midi);