2 * Linux driver for TerraTec DMX 6Fire USB
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
9 * Copyright: (C) Torsten Schenk
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <sound/rawmidi.h>
23 static void usb6fire_midi_out_handler(struct urb
*urb
)
25 struct midi_runtime
*rt
= urb
->context
;
29 spin_lock_irqsave(&rt
->out_lock
, flags
);
32 ret
= snd_rawmidi_transmit(rt
->out
, rt
->out_buffer
+ 4,
34 if (ret
> 0) { /* more data available, send next packet */
35 rt
->out_buffer
[1] = ret
+ 2;
36 rt
->out_buffer
[3] = rt
->out_serial
++;
37 urb
->transfer_buffer_length
= ret
+ 4;
39 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
41 snd_printk(KERN_ERR PREFIX
"midi out urb "
42 "submit failed: %d\n", ret
);
43 } else /* no more data to transmit */
46 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
49 static void usb6fire_midi_in_received(
50 struct midi_runtime
*rt
, u8
*data
, int length
)
54 spin_lock_irqsave(&rt
->in_lock
, flags
);
56 snd_rawmidi_receive(rt
->in
, data
, length
);
57 spin_unlock_irqrestore(&rt
->in_lock
, flags
);
60 static int usb6fire_midi_out_open(struct snd_rawmidi_substream
*alsa_sub
)
65 static int usb6fire_midi_out_close(struct snd_rawmidi_substream
*alsa_sub
)
70 static void usb6fire_midi_out_trigger(
71 struct snd_rawmidi_substream
*alsa_sub
, int up
)
73 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
74 struct urb
*urb
= &rt
->out_urb
;
78 spin_lock_irqsave(&rt
->out_lock
, flags
);
79 if (up
) { /* start transfer */
80 if (rt
->out
) { /* we are already transmitting so just return */
81 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
85 ret
= snd_rawmidi_transmit(alsa_sub
, rt
->out_buffer
+ 4,
88 rt
->out_buffer
[1] = ret
+ 2;
89 rt
->out_buffer
[3] = rt
->out_serial
++;
90 urb
->transfer_buffer_length
= ret
+ 4;
92 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
94 snd_printk(KERN_ERR PREFIX
"midi out urb "
95 "submit failed: %d\n", ret
);
99 } else if (rt
->out
== alsa_sub
)
101 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
104 static void usb6fire_midi_out_drain(struct snd_rawmidi_substream
*alsa_sub
)
106 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
109 while (rt
->out
&& retry
++ < 100)
113 static int usb6fire_midi_in_open(struct snd_rawmidi_substream
*alsa_sub
)
118 static int usb6fire_midi_in_close(struct snd_rawmidi_substream
*alsa_sub
)
123 static void usb6fire_midi_in_trigger(
124 struct snd_rawmidi_substream
*alsa_sub
, int up
)
126 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
129 spin_lock_irqsave(&rt
->in_lock
, flags
);
134 spin_unlock_irqrestore(&rt
->in_lock
, flags
);
137 static struct snd_rawmidi_ops out_ops
= {
138 .open
= usb6fire_midi_out_open
,
139 .close
= usb6fire_midi_out_close
,
140 .trigger
= usb6fire_midi_out_trigger
,
141 .drain
= usb6fire_midi_out_drain
144 static struct snd_rawmidi_ops in_ops
= {
145 .open
= usb6fire_midi_in_open
,
146 .close
= usb6fire_midi_in_close
,
147 .trigger
= usb6fire_midi_in_trigger
150 int __devinit
usb6fire_midi_init(struct sfire_chip
*chip
)
153 struct midi_runtime
*rt
= kzalloc(sizeof(struct midi_runtime
),
155 struct comm_runtime
*comm_rt
= chip
->comm
;
161 rt
->in_received
= usb6fire_midi_in_received
;
162 rt
->out_buffer
[0] = 0x80; /* 'send midi' command */
163 rt
->out_buffer
[1] = 0x00; /* size of data */
164 rt
->out_buffer
[2] = 0x00; /* always 0 */
165 spin_lock_init(&rt
->in_lock
);
166 spin_lock_init(&rt
->out_lock
);
168 comm_rt
->init_urb(comm_rt
, &rt
->out_urb
, rt
->out_buffer
, rt
,
169 usb6fire_midi_out_handler
);
171 ret
= snd_rawmidi_new(chip
->card
, "6FireUSB", 0, 1, 1, &rt
->instance
);
174 snd_printk(KERN_ERR PREFIX
"unable to create midi.\n");
177 rt
->instance
->private_data
= rt
;
178 strcpy(rt
->instance
->name
, "DMX6FireUSB MIDI");
179 rt
->instance
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
180 SNDRV_RAWMIDI_INFO_INPUT
|
181 SNDRV_RAWMIDI_INFO_DUPLEX
;
182 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
184 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_INPUT
,
191 void usb6fire_midi_abort(struct sfire_chip
*chip
)
193 struct midi_runtime
*rt
= chip
->midi
;
196 usb_poison_urb(&rt
->out_urb
);
199 void usb6fire_midi_destroy(struct sfire_chip
*chip
)