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>
22 static void usb6fire_midi_out_handler(struct urb
*urb
)
24 struct midi_runtime
*rt
= urb
->context
;
28 spin_lock_irqsave(&rt
->out_lock
, flags
);
31 ret
= snd_rawmidi_transmit(rt
->out
, rt
->out_buffer
+ 4,
33 if (ret
> 0) { /* more data available, send next packet */
34 rt
->out_buffer
[1] = ret
+ 2;
35 rt
->out_buffer
[3] = rt
->out_serial
++;
36 urb
->transfer_buffer_length
= ret
+ 4;
38 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
40 snd_printk(KERN_ERR PREFIX
"midi out urb "
41 "submit failed: %d\n", ret
);
42 } else /* no more data to transmit */
45 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
48 static void usb6fire_midi_in_received(
49 struct midi_runtime
*rt
, u8
*data
, int length
)
53 spin_lock_irqsave(&rt
->in_lock
, flags
);
55 snd_rawmidi_receive(rt
->in
, data
, length
);
56 spin_unlock_irqrestore(&rt
->in_lock
, flags
);
59 static int usb6fire_midi_out_open(struct snd_rawmidi_substream
*alsa_sub
)
64 static int usb6fire_midi_out_close(struct snd_rawmidi_substream
*alsa_sub
)
69 static void usb6fire_midi_out_trigger(
70 struct snd_rawmidi_substream
*alsa_sub
, int up
)
72 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
73 struct urb
*urb
= &rt
->out_urb
;
77 spin_lock_irqsave(&rt
->out_lock
, flags
);
78 if (up
) { /* start transfer */
79 if (rt
->out
) { /* we are already transmitting so just return */
80 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
84 ret
= snd_rawmidi_transmit(alsa_sub
, rt
->out_buffer
+ 4,
87 rt
->out_buffer
[1] = ret
+ 2;
88 rt
->out_buffer
[3] = rt
->out_serial
++;
89 urb
->transfer_buffer_length
= ret
+ 4;
91 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
93 snd_printk(KERN_ERR PREFIX
"midi out urb "
94 "submit failed: %d\n", ret
);
98 } else if (rt
->out
== alsa_sub
)
100 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
103 static void usb6fire_midi_out_drain(struct snd_rawmidi_substream
*alsa_sub
)
105 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
108 while (rt
->out
&& retry
++ < 100)
112 static int usb6fire_midi_in_open(struct snd_rawmidi_substream
*alsa_sub
)
117 static int usb6fire_midi_in_close(struct snd_rawmidi_substream
*alsa_sub
)
122 static void usb6fire_midi_in_trigger(
123 struct snd_rawmidi_substream
*alsa_sub
, int up
)
125 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
128 spin_lock_irqsave(&rt
->in_lock
, flags
);
133 spin_unlock_irqrestore(&rt
->in_lock
, flags
);
136 static struct snd_rawmidi_ops out_ops
= {
137 .open
= usb6fire_midi_out_open
,
138 .close
= usb6fire_midi_out_close
,
139 .trigger
= usb6fire_midi_out_trigger
,
140 .drain
= usb6fire_midi_out_drain
143 static struct snd_rawmidi_ops in_ops
= {
144 .open
= usb6fire_midi_in_open
,
145 .close
= usb6fire_midi_in_close
,
146 .trigger
= usb6fire_midi_in_trigger
149 int usb6fire_midi_init(struct sfire_chip
*chip
)
152 struct midi_runtime
*rt
= kzalloc(sizeof(struct midi_runtime
),
154 struct comm_runtime
*comm_rt
= chip
->comm
;
160 rt
->in_received
= usb6fire_midi_in_received
;
161 rt
->out_buffer
[0] = 0x80; /* 'send midi' command */
162 rt
->out_buffer
[1] = 0x00; /* size of data */
163 rt
->out_buffer
[2] = 0x00; /* always 0 */
164 spin_lock_init(&rt
->in_lock
);
165 spin_lock_init(&rt
->out_lock
);
167 comm_rt
->init_urb(comm_rt
, &rt
->out_urb
, rt
->out_buffer
, rt
,
168 usb6fire_midi_out_handler
);
170 ret
= snd_rawmidi_new(chip
->card
, "6FireUSB", 0, 1, 1, &rt
->instance
);
173 snd_printk(KERN_ERR PREFIX
"unable to create midi.\n");
176 rt
->instance
->private_data
= rt
;
177 strcpy(rt
->instance
->name
, "DMX6FireUSB MIDI");
178 rt
->instance
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
179 SNDRV_RAWMIDI_INFO_INPUT
|
180 SNDRV_RAWMIDI_INFO_DUPLEX
;
181 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
183 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_INPUT
,
190 void usb6fire_midi_abort(struct sfire_chip
*chip
)
192 struct midi_runtime
*rt
= chip
->midi
;
195 usb_poison_urb(&rt
->out_urb
);
198 void usb6fire_midi_destroy(struct sfire_chip
*chip
)