-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmidi.c
345 lines (286 loc) · 7.83 KB
/
midi.c
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Linux driver for D.O.Tec EXBOX.UMA (PloyTec USB interface)
*
* Rawmidi driver
*
* Copyright 2015 (C) Google Inc.
*
* Authors: Adrian Knoth <[email protected]>
* Adrian Knoth <[email protected]>
*
* The driver is based on the 6fire driver.
*
* 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; either version 2 of the License, or
* (at your option) any later version.
*/
#include <linux/slab.h>
#include <sound/rawmidi.h>
#include "midi.h"
#include "chip.h"
enum {
MIDI_BUFSIZE = 512
};
static int exbox_midi_init_urb(struct midi_urb *urb,
struct exbox_chip *chip,
unsigned int ep,
void (*handler)(struct urb *))
{
urb->chip = chip;
usb_init_urb(&urb->instance);
urb->buffer = kzalloc(MIDI_BUFSIZE, GFP_KERNEL);
if (!urb->buffer)
return -ENOMEM;
usb_fill_bulk_urb(&urb->instance, chip->dev,
usb_rcvbulkpipe(chip->dev, ep),
(void *)urb->buffer,
MIDI_BUFSIZE, handler, urb);
init_usb_anchor(&urb->submitted);
return 0;
}
static void exbox_midi_in_handler(struct urb *usb_urb)
{
struct midi_urb *in_urb = usb_urb->context;
struct midi_runtime *rt = in_urb->chip->midi;
struct exbox_chip *chip = in_urb->chip;
int i;
int packet_size = 4; /* 4xMIDI, 1xStatus (NOT MIDI-status) */
unsigned long flags;
int ret;
chip->midi_in_count++;
if (rt->panic || rt->stream_state == STREAM_STOPPING)
return;
if (unlikely(usb_urb->status == -ENOENT || /* unlinked */
usb_urb->status == -ENODEV || /* device removed */
usb_urb->status == -ECONNRESET || /* unlinked */
usb_urb->status == -ESHUTDOWN)) { /* device disabled */
goto out_fail;
}
spin_lock_irqsave(&rt->in_lock, flags);
if (!rt->in) {
goto out_ok;
}
if (chip->samplerate > 48000) {
packet_size = 1; /* 1xMIDI, 1xStatus for rates above 48kHz */
}
for (i=0; i < packet_size; i++) {
/* The device uses 0xfd to indicate "no data" */
if (in_urb->buffer[i] != 0xfd && in_urb->buffer[i] != 0xff) {
snd_rawmidi_receive(rt->in, &in_urb->buffer[i], 1);
dev_dbg(&in_urb->chip->dev->dev, "%s: receiving MIDI byte %x", __func__, in_urb->buffer[i]);
}
}
out_ok:
spin_unlock_irqrestore(&rt->in_lock, flags);
ret = usb_submit_urb(&in_urb->instance, GFP_ATOMIC);
if (ret < 0)
goto out_fail;
return;
out_fail:
rt->panic = true;
return;
}
static int exbox_midi_out_open(struct snd_rawmidi_substream *alsa_sub)
{
return 0;
}
static int exbox_midi_out_close(struct snd_rawmidi_substream *alsa_sub)
{
return 0;
}
static void exbox_midi_out_trigger(
struct snd_rawmidi_substream *alsa_sub, int up)
{
struct midi_runtime *rt = alsa_sub->rmidi->private_data;
int ret;
unsigned long flags;
int size;
spin_lock_irqsave(&rt->out_lock, flags);
if (up) { /* start transfer */
if (rt->out) { /* we are already transmitting so just return */
goto out;
}
size = kfifo_avail(&rt->chip->midi2pcm);
if (size <= 0) {
dev_err(&rt->chip->dev->dev, "%s kfifo_avail negative/zero", __func__);
goto out;
}
ret = snd_rawmidi_transmit(alsa_sub, rt->out_buffer, size);
dev_err(&rt->chip->dev->dev, "%s rawmidi: %i/%i (buf)", __func__, ret, size);
rt->out = alsa_sub;
if (ret > 0) {
/* Copy 'ret' elements from the out_buffer to the
* fifo.
*/
kfifo_in(&rt->chip->midi2pcm, rt->out_buffer, ret);
}
} else {
rt->out = NULL;
}
out:
spin_unlock_irqrestore(&rt->out_lock, flags);
return;
}
static void exbox_midi_out_drain(struct snd_rawmidi_substream *alsa_sub)
{
struct midi_runtime *rt = alsa_sub->rmidi->private_data;
int retry = 0;
while (rt->out && retry++ < 100)
msleep(10);
}
/* call with stream_mutex locked */
int exbox_midi_stream_start(struct midi_runtime *rt)
{
int ret = 0;
int i;
struct usb_device *device = rt->chip->dev;
if (rt->stream_state == STREAM_DISABLED) {
/* reset panic state when starting a new stream */
rt->panic = false;
dev_dbg(&device->dev, "MIDI starting up: %s\n",
__func__);
/* submit our out urbs zero init */
rt->stream_state = STREAM_STARTING;
for (i = 0; i < MIDI_N_URBS; i++) {
/* Indiciate no midi by sending 0xfd */
memset(rt->in_urbs[i].buffer, 0xfd, MIDI_BUFSIZE);
usb_anchor_urb(&rt->in_urbs[i].instance,
&rt->in_urbs[i].submitted);
ret = usb_submit_urb(&rt->in_urbs[i].instance,
GFP_ATOMIC);
if (ret) {
dev_dbg(&device->dev, "%s: maybe something is wrong\n",
__func__);
return ret;
}
}
rt->stream_state = STREAM_RUNNING;
dev_dbg(&device->dev, "%s: MIDI URB sent, should be good now\n",
__func__);
return 0;
}
return ret;
}
/* call with stream_mutex locked */
void exbox_midi_stream_stop(struct midi_runtime *rt)
{
int i;
int time;
struct usb_device *device = rt->chip->dev;
if (rt->stream_state != STREAM_DISABLED) {
rt->stream_state = STREAM_STOPPING;
for (i = 0; i < MIDI_N_URBS; i++) {
time = usb_wait_anchor_empty_timeout(
&rt->in_urbs[i].submitted, 100);
if (!time)
usb_kill_anchored_urbs(
&rt->in_urbs[i].submitted);
usb_kill_urb(&rt->in_urbs[i].instance);
}
rt->stream_state = STREAM_DISABLED;
dev_dbg(&device->dev, "USB MIDI stopped in %s\n",
__func__);
}
}
static int exbox_midi_in_open(struct snd_rawmidi_substream *alsa_sub)
{
/* FIXME: Check if the device is already streaming, and if not, start
* the streams. Right now, streaming state is manually triggered from
* userspace.
*/
return 0;
}
static int exbox_midi_in_close(struct snd_rawmidi_substream *alsa_sub)
{
return 0;
}
static void exbox_midi_in_trigger(
struct snd_rawmidi_substream *alsa_sub, int up)
{
struct midi_runtime *rt = alsa_sub->rmidi->private_data;
unsigned long flags;
spin_lock_irqsave(&rt->in_lock, flags);
if (up)
rt->in = alsa_sub;
else
rt->in = NULL;
spin_unlock_irqrestore(&rt->in_lock, flags);
}
static struct snd_rawmidi_ops out_ops = {
.open = exbox_midi_out_open,
.close = exbox_midi_out_close,
.trigger = exbox_midi_out_trigger,
/* .drain = exbox_midi_out_drain */
};
static struct snd_rawmidi_ops in_ops = {
.open = exbox_midi_in_open,
.close = exbox_midi_in_close,
.trigger = exbox_midi_in_trigger
};
int exbox_midi_init(struct exbox_chip *chip)
{
int ret;
int i;
struct midi_runtime *rt = kzalloc(sizeof(struct midi_runtime),
GFP_KERNEL);
//struct comm_runtime *comm_rt = chip->comm;
if (!rt)
return -ENOMEM;
rt->out_buffer = kzalloc(MIDI2PCM_MUX_FIFO_SIZE, GFP_KERNEL);
if (!rt->out_buffer) {
kfree(rt);
return -ENOMEM;
}
rt->chip = chip;
rt->stream_state = STREAM_DISABLED;
init_waitqueue_head(&rt->stream_wait_queue);
mutex_init(&rt->stream_mutex);
spin_lock_init(&rt->in_lock);
spin_lock_init(&rt->out_lock);
for (i = 0; i < MIDI_N_URBS; i++) {
exbox_midi_init_urb(&rt->in_urbs[i], chip, EP_MIDI_IN,
exbox_midi_in_handler);
}
ret = snd_rawmidi_new(chip->card, "EXBOXUMA", 0, 1, 1, &rt->instance);
if (ret < 0) {
kfree(rt->out_buffer);
kfree(rt);
dev_err(&chip->dev->dev, "unable to create midi.\n");
return ret;
}
rt->instance->private_data = rt;
strcpy(rt->instance->name, "D.O.Tec EXBOX UMA MIDI");
rt->instance->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
SNDRV_RAWMIDI_INFO_INPUT |
SNDRV_RAWMIDI_INFO_DUPLEX;
snd_rawmidi_set_ops(rt->instance, SNDRV_RAWMIDI_STREAM_OUTPUT,
&out_ops);
snd_rawmidi_set_ops(rt->instance, SNDRV_RAWMIDI_STREAM_INPUT,
&in_ops);
chip->midi = rt;
return 0;
}
void exbox_midi_abort(struct exbox_chip *chip)
{
struct midi_runtime *rt = chip->midi;
int i;
if (!rt) {
return;
}
usb_poison_urb(&rt->out_urb);
for (i = 0; i < MIDI_N_URBS; i++) {
usb_poison_urb(&rt->in_urbs[i].instance);
}
}
void exbox_midi_destroy(struct exbox_chip *chip)
{
struct midi_runtime *rt = chip->midi;
int i;
for (i = 0; i < MIDI_N_URBS; i++) {
kfree(rt->in_urbs[i].buffer);
}
kfree(rt->out_buffer);
kfree(rt);
chip->midi = NULL;
}