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 snd_printk(KERN_ERR PREFIX
"midi out urb "
45 "submit failed: %d\n", ret
);
46 } else /* no more data to transmit */
49 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
52 static void usb6fire_midi_in_received(
53 struct midi_runtime
*rt
, u8
*data
, int length
)
57 spin_lock_irqsave(&rt
->in_lock
, flags
);
59 snd_rawmidi_receive(rt
->in
, data
, length
);
60 spin_unlock_irqrestore(&rt
->in_lock
, flags
);
63 static int usb6fire_midi_out_open(struct snd_rawmidi_substream
*alsa_sub
)
68 static int usb6fire_midi_out_close(struct snd_rawmidi_substream
*alsa_sub
)
73 static void usb6fire_midi_out_trigger(
74 struct snd_rawmidi_substream
*alsa_sub
, int up
)
76 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
77 struct urb
*urb
= &rt
->out_urb
;
81 spin_lock_irqsave(&rt
->out_lock
, flags
);
82 if (up
) { /* start transfer */
83 if (rt
->out
) { /* we are already transmitting so just return */
84 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
88 ret
= snd_rawmidi_transmit(alsa_sub
, rt
->out_buffer
+ 4,
91 rt
->out_buffer
[1] = ret
+ 2;
92 rt
->out_buffer
[3] = rt
->out_serial
++;
93 urb
->transfer_buffer_length
= ret
+ 4;
95 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
97 snd_printk(KERN_ERR PREFIX
"midi out urb "
98 "submit failed: %d\n", ret
);
102 } else if (rt
->out
== alsa_sub
)
104 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
107 static void usb6fire_midi_out_drain(struct snd_rawmidi_substream
*alsa_sub
)
109 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
112 while (rt
->out
&& retry
++ < 100)
116 static int usb6fire_midi_in_open(struct snd_rawmidi_substream
*alsa_sub
)
121 static int usb6fire_midi_in_close(struct snd_rawmidi_substream
*alsa_sub
)
126 static void usb6fire_midi_in_trigger(
127 struct snd_rawmidi_substream
*alsa_sub
, int up
)
129 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
132 spin_lock_irqsave(&rt
->in_lock
, flags
);
137 spin_unlock_irqrestore(&rt
->in_lock
, flags
);
140 static struct snd_rawmidi_ops out_ops
= {
141 .open
= usb6fire_midi_out_open
,
142 .close
= usb6fire_midi_out_close
,
143 .trigger
= usb6fire_midi_out_trigger
,
144 .drain
= usb6fire_midi_out_drain
147 static struct snd_rawmidi_ops in_ops
= {
148 .open
= usb6fire_midi_in_open
,
149 .close
= usb6fire_midi_in_close
,
150 .trigger
= usb6fire_midi_in_trigger
153 int usb6fire_midi_init(struct sfire_chip
*chip
)
156 struct midi_runtime
*rt
= kzalloc(sizeof(struct midi_runtime
),
158 struct comm_runtime
*comm_rt
= chip
->comm
;
163 rt
->out_buffer
= kzalloc(MIDI_BUFSIZE
, GFP_KERNEL
);
164 if (!rt
->out_buffer
) {
170 rt
->in_received
= usb6fire_midi_in_received
;
171 rt
->out_buffer
[0] = 0x80; /* 'send midi' command */
172 rt
->out_buffer
[1] = 0x00; /* size of data */
173 rt
->out_buffer
[2] = 0x00; /* always 0 */
174 spin_lock_init(&rt
->in_lock
);
175 spin_lock_init(&rt
->out_lock
);
177 comm_rt
->init_urb(comm_rt
, &rt
->out_urb
, rt
->out_buffer
, rt
,
178 usb6fire_midi_out_handler
);
180 ret
= snd_rawmidi_new(chip
->card
, "6FireUSB", 0, 1, 1, &rt
->instance
);
182 kfree(rt
->out_buffer
);
184 snd_printk(KERN_ERR PREFIX
"unable to create midi.\n");
187 rt
->instance
->private_data
= rt
;
188 strcpy(rt
->instance
->name
, "DMX6FireUSB MIDI");
189 rt
->instance
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
190 SNDRV_RAWMIDI_INFO_INPUT
|
191 SNDRV_RAWMIDI_INFO_DUPLEX
;
192 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
194 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_INPUT
,
201 void usb6fire_midi_abort(struct sfire_chip
*chip
)
203 struct midi_runtime
*rt
= chip
->midi
;
206 usb_poison_urb(&rt
->out_urb
);
209 void usb6fire_midi_destroy(struct sfire_chip
*chip
)
211 struct midi_runtime
*rt
= chip
->midi
;
213 kfree(rt
->out_buffer
);