2 * Linux driver for TerraTec DMX 6Fire USB
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
8 * Copyright: (C) Torsten Schenk
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <sound/rawmidi.h>
26 static void usb6fire_midi_out_handler(struct urb
*urb
)
28 struct midi_runtime
*rt
= urb
->context
;
32 spin_lock_irqsave(&rt
->out_lock
, flags
);
35 ret
= snd_rawmidi_transmit(rt
->out
, rt
->out_buffer
+ 4,
37 if (ret
> 0) { /* more data available, send next packet */
38 rt
->out_buffer
[1] = ret
+ 2;
39 rt
->out_buffer
[3] = rt
->out_serial
++;
40 urb
->transfer_buffer_length
= ret
+ 4;
42 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
44 dev_err(&urb
->dev
->dev
,
45 "midi out urb submit failed: %d\n",
47 } else /* no more data to transmit */
50 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
53 static void usb6fire_midi_in_received(
54 struct midi_runtime
*rt
, u8
*data
, int length
)
58 spin_lock_irqsave(&rt
->in_lock
, flags
);
60 snd_rawmidi_receive(rt
->in
, data
, length
);
61 spin_unlock_irqrestore(&rt
->in_lock
, flags
);
64 static int usb6fire_midi_out_open(struct snd_rawmidi_substream
*alsa_sub
)
69 static int usb6fire_midi_out_close(struct snd_rawmidi_substream
*alsa_sub
)
74 static void usb6fire_midi_out_trigger(
75 struct snd_rawmidi_substream
*alsa_sub
, int up
)
77 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
78 struct urb
*urb
= &rt
->out_urb
;
82 spin_lock_irqsave(&rt
->out_lock
, flags
);
83 if (up
) { /* start transfer */
84 if (rt
->out
) { /* we are already transmitting so just return */
85 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
89 ret
= snd_rawmidi_transmit(alsa_sub
, rt
->out_buffer
+ 4,
92 rt
->out_buffer
[1] = ret
+ 2;
93 rt
->out_buffer
[3] = rt
->out_serial
++;
94 urb
->transfer_buffer_length
= ret
+ 4;
96 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
98 dev_err(&urb
->dev
->dev
,
99 "midi out urb submit failed: %d\n",
104 } else if (rt
->out
== alsa_sub
)
106 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
109 static void usb6fire_midi_out_drain(struct snd_rawmidi_substream
*alsa_sub
)
111 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
114 while (rt
->out
&& retry
++ < 100)
118 static int usb6fire_midi_in_open(struct snd_rawmidi_substream
*alsa_sub
)
123 static int usb6fire_midi_in_close(struct snd_rawmidi_substream
*alsa_sub
)
128 static void usb6fire_midi_in_trigger(
129 struct snd_rawmidi_substream
*alsa_sub
, int up
)
131 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
134 spin_lock_irqsave(&rt
->in_lock
, flags
);
139 spin_unlock_irqrestore(&rt
->in_lock
, flags
);
142 static struct snd_rawmidi_ops out_ops
= {
143 .open
= usb6fire_midi_out_open
,
144 .close
= usb6fire_midi_out_close
,
145 .trigger
= usb6fire_midi_out_trigger
,
146 .drain
= usb6fire_midi_out_drain
149 static struct snd_rawmidi_ops in_ops
= {
150 .open
= usb6fire_midi_in_open
,
151 .close
= usb6fire_midi_in_close
,
152 .trigger
= usb6fire_midi_in_trigger
155 int usb6fire_midi_init(struct sfire_chip
*chip
)
158 struct midi_runtime
*rt
= kzalloc(sizeof(struct midi_runtime
),
160 struct comm_runtime
*comm_rt
= chip
->comm
;
165 rt
->out_buffer
= kzalloc(MIDI_BUFSIZE
, GFP_KERNEL
);
166 if (!rt
->out_buffer
) {
172 rt
->in_received
= usb6fire_midi_in_received
;
173 rt
->out_buffer
[0] = 0x80; /* 'send midi' command */
174 rt
->out_buffer
[1] = 0x00; /* size of data */
175 rt
->out_buffer
[2] = 0x00; /* always 0 */
176 spin_lock_init(&rt
->in_lock
);
177 spin_lock_init(&rt
->out_lock
);
179 comm_rt
->init_urb(comm_rt
, &rt
->out_urb
, rt
->out_buffer
, rt
,
180 usb6fire_midi_out_handler
);
182 ret
= snd_rawmidi_new(chip
->card
, "6FireUSB", 0, 1, 1, &rt
->instance
);
184 kfree(rt
->out_buffer
);
186 dev_err(&chip
->dev
->dev
, "unable to create midi.\n");
189 rt
->instance
->private_data
= rt
;
190 strcpy(rt
->instance
->name
, "DMX6FireUSB MIDI");
191 rt
->instance
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
192 SNDRV_RAWMIDI_INFO_INPUT
|
193 SNDRV_RAWMIDI_INFO_DUPLEX
;
194 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
196 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_INPUT
,
203 void usb6fire_midi_abort(struct sfire_chip
*chip
)
205 struct midi_runtime
*rt
= chip
->midi
;
208 usb_poison_urb(&rt
->out_urb
);
211 void usb6fire_midi_destroy(struct sfire_chip
*chip
)
213 struct midi_runtime
*rt
= chip
->midi
;
215 kfree(rt
->out_buffer
);