1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux driver for TerraTec DMX 6Fire USB
7 * Author: Torsten Schenk <torsten.schenk@zoho.com>
8 * Created: Jan 01, 2011
9 * Copyright: (C) Torsten Schenk
12 #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 dev_err(&urb
->dev
->dev
,
41 "midi out urb submit failed: %d\n",
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 dev_err(&urb
->dev
->dev
,
95 "midi out urb submit failed: %d\n",
100 } else if (rt
->out
== alsa_sub
)
102 spin_unlock_irqrestore(&rt
->out_lock
, flags
);
105 static void usb6fire_midi_out_drain(struct snd_rawmidi_substream
*alsa_sub
)
107 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
110 while (rt
->out
&& retry
++ < 100)
114 static int usb6fire_midi_in_open(struct snd_rawmidi_substream
*alsa_sub
)
119 static int usb6fire_midi_in_close(struct snd_rawmidi_substream
*alsa_sub
)
124 static void usb6fire_midi_in_trigger(
125 struct snd_rawmidi_substream
*alsa_sub
, int up
)
127 struct midi_runtime
*rt
= alsa_sub
->rmidi
->private_data
;
130 spin_lock_irqsave(&rt
->in_lock
, flags
);
135 spin_unlock_irqrestore(&rt
->in_lock
, flags
);
138 static const struct snd_rawmidi_ops out_ops
= {
139 .open
= usb6fire_midi_out_open
,
140 .close
= usb6fire_midi_out_close
,
141 .trigger
= usb6fire_midi_out_trigger
,
142 .drain
= usb6fire_midi_out_drain
145 static const struct snd_rawmidi_ops in_ops
= {
146 .open
= usb6fire_midi_in_open
,
147 .close
= usb6fire_midi_in_close
,
148 .trigger
= usb6fire_midi_in_trigger
151 int usb6fire_midi_init(struct sfire_chip
*chip
)
154 struct midi_runtime
*rt
= kzalloc(sizeof(struct midi_runtime
),
156 struct comm_runtime
*comm_rt
= chip
->comm
;
161 rt
->out_buffer
= kzalloc(MIDI_BUFSIZE
, GFP_KERNEL
);
162 if (!rt
->out_buffer
) {
168 rt
->in_received
= usb6fire_midi_in_received
;
169 rt
->out_buffer
[0] = 0x80; /* 'send midi' command */
170 rt
->out_buffer
[1] = 0x00; /* size of data */
171 rt
->out_buffer
[2] = 0x00; /* always 0 */
172 spin_lock_init(&rt
->in_lock
);
173 spin_lock_init(&rt
->out_lock
);
175 comm_rt
->init_urb(comm_rt
, &rt
->out_urb
, rt
->out_buffer
, rt
,
176 usb6fire_midi_out_handler
);
178 ret
= snd_rawmidi_new(chip
->card
, "6FireUSB", 0, 1, 1, &rt
->instance
);
180 kfree(rt
->out_buffer
);
182 dev_err(&chip
->dev
->dev
, "unable to create midi.\n");
185 rt
->instance
->private_data
= rt
;
186 strcpy(rt
->instance
->name
, "DMX6FireUSB MIDI");
187 rt
->instance
->info_flags
= SNDRV_RAWMIDI_INFO_OUTPUT
|
188 SNDRV_RAWMIDI_INFO_INPUT
|
189 SNDRV_RAWMIDI_INFO_DUPLEX
;
190 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
192 snd_rawmidi_set_ops(rt
->instance
, SNDRV_RAWMIDI_STREAM_INPUT
,
199 void usb6fire_midi_abort(struct sfire_chip
*chip
)
201 struct midi_runtime
*rt
= chip
->midi
;
204 usb_poison_urb(&rt
->out_urb
);
207 void usb6fire_midi_destroy(struct sfire_chip
*chip
)
209 struct midi_runtime
*rt
= chip
->midi
;
211 kfree(rt
->out_buffer
);