2 * Line 6 Linux USB driver
4 * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation, version 2.
12 #include <linux/slab.h>
13 #include <linux/usb.h>
14 #include <linux/export.h>
15 #include <sound/core.h>
16 #include <sound/rawmidi.h>
21 #define line6_rawmidi_substream_midi(substream) \
22 ((struct snd_line6_midi *)((substream)->rmidi->private_data))
24 static int send_midi_async(struct usb_line6
*line6
, unsigned char *data
,
28 Pass data received via USB to MIDI.
30 void line6_midi_receive(struct usb_line6
*line6
, unsigned char *data
,
33 if (line6
->line6midi
->substream_receive
)
34 snd_rawmidi_receive(line6
->line6midi
->substream_receive
,
39 Read data from MIDI buffer and transmit them via USB.
41 static void line6_midi_transmit(struct snd_rawmidi_substream
*substream
)
43 struct usb_line6
*line6
=
44 line6_rawmidi_substream_midi(substream
)->line6
;
45 struct snd_line6_midi
*line6midi
= line6
->line6midi
;
46 struct midi_buffer
*mb
= &line6midi
->midibuf_out
;
47 unsigned char chunk
[LINE6_FALLBACK_MAXPACKETSIZE
];
51 req
= min(line6_midibuf_bytes_free(mb
), line6
->max_packet_size
);
52 done
= snd_rawmidi_transmit_peek(substream
, chunk
, req
);
57 line6_midibuf_write(mb
, chunk
, done
);
58 snd_rawmidi_transmit_ack(substream
, done
);
62 done
= line6_midibuf_read(mb
, chunk
,
63 LINE6_FALLBACK_MAXPACKETSIZE
);
68 send_midi_async(line6
, chunk
, done
);
73 Notification of completion of MIDI transmission.
75 static void midi_sent(struct urb
*urb
)
80 struct usb_line6
*line6
= (struct usb_line6
*)urb
->context
;
83 kfree(urb
->transfer_buffer
);
86 if (status
== -ESHUTDOWN
)
89 spin_lock_irqsave(&line6
->line6midi
->lock
, flags
);
90 num
= --line6
->line6midi
->num_active_send_urbs
;
93 line6_midi_transmit(line6
->line6midi
->substream_transmit
);
94 num
= line6
->line6midi
->num_active_send_urbs
;
98 wake_up(&line6
->line6midi
->send_wait
);
100 spin_unlock_irqrestore(&line6
->line6midi
->lock
, flags
);
104 Send an asynchronous MIDI message.
105 Assumes that line6->line6midi->lock is held
106 (i.e., this function is serialized).
108 static int send_midi_async(struct usb_line6
*line6
, unsigned char *data
,
113 unsigned char *transfer_buffer
;
115 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
120 transfer_buffer
= kmemdup(data
, length
, GFP_ATOMIC
);
122 if (transfer_buffer
== NULL
) {
127 usb_fill_int_urb(urb
, line6
->usbdev
,
128 usb_sndbulkpipe(line6
->usbdev
,
129 line6
->properties
->ep_ctrl_w
),
130 transfer_buffer
, length
, midi_sent
, line6
,
132 urb
->actual_length
= 0;
133 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
136 dev_err(line6
->ifcdev
, "usb_submit_urb failed\n");
141 ++line6
->line6midi
->num_active_send_urbs
;
145 static int line6_midi_output_open(struct snd_rawmidi_substream
*substream
)
150 static int line6_midi_output_close(struct snd_rawmidi_substream
*substream
)
155 static void line6_midi_output_trigger(struct snd_rawmidi_substream
*substream
,
159 struct usb_line6
*line6
=
160 line6_rawmidi_substream_midi(substream
)->line6
;
162 line6
->line6midi
->substream_transmit
= substream
;
163 spin_lock_irqsave(&line6
->line6midi
->lock
, flags
);
165 if (line6
->line6midi
->num_active_send_urbs
== 0)
166 line6_midi_transmit(substream
);
168 spin_unlock_irqrestore(&line6
->line6midi
->lock
, flags
);
171 static void line6_midi_output_drain(struct snd_rawmidi_substream
*substream
)
173 struct usb_line6
*line6
=
174 line6_rawmidi_substream_midi(substream
)->line6
;
175 struct snd_line6_midi
*midi
= line6
->line6midi
;
177 wait_event_interruptible(midi
->send_wait
,
178 midi
->num_active_send_urbs
== 0);
181 static int line6_midi_input_open(struct snd_rawmidi_substream
*substream
)
186 static int line6_midi_input_close(struct snd_rawmidi_substream
*substream
)
191 static void line6_midi_input_trigger(struct snd_rawmidi_substream
*substream
,
194 struct usb_line6
*line6
=
195 line6_rawmidi_substream_midi(substream
)->line6
;
198 line6
->line6midi
->substream_receive
= substream
;
200 line6
->line6midi
->substream_receive
= NULL
;
203 static const struct snd_rawmidi_ops line6_midi_output_ops
= {
204 .open
= line6_midi_output_open
,
205 .close
= line6_midi_output_close
,
206 .trigger
= line6_midi_output_trigger
,
207 .drain
= line6_midi_output_drain
,
210 static const struct snd_rawmidi_ops line6_midi_input_ops
= {
211 .open
= line6_midi_input_open
,
212 .close
= line6_midi_input_close
,
213 .trigger
= line6_midi_input_trigger
,
216 /* Create a MIDI device */
217 static int snd_line6_new_midi(struct usb_line6
*line6
,
218 struct snd_rawmidi
**rmidi_ret
)
220 struct snd_rawmidi
*rmidi
;
223 err
= snd_rawmidi_new(line6
->card
, "Line 6 MIDI", 0, 1, 1, rmidi_ret
);
228 strcpy(rmidi
->id
, line6
->properties
->id
);
229 strcpy(rmidi
->name
, line6
->properties
->name
);
232 SNDRV_RAWMIDI_INFO_OUTPUT
|
233 SNDRV_RAWMIDI_INFO_INPUT
| SNDRV_RAWMIDI_INFO_DUPLEX
;
235 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
236 &line6_midi_output_ops
);
237 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
238 &line6_midi_input_ops
);
242 /* MIDI device destructor */
243 static void snd_line6_midi_free(struct snd_rawmidi
*rmidi
)
245 struct snd_line6_midi
*line6midi
= rmidi
->private_data
;
247 line6_midibuf_destroy(&line6midi
->midibuf_in
);
248 line6_midibuf_destroy(&line6midi
->midibuf_out
);
253 Initialize the Line 6 MIDI subsystem.
255 int line6_init_midi(struct usb_line6
*line6
)
258 struct snd_rawmidi
*rmidi
;
259 struct snd_line6_midi
*line6midi
;
261 if (!(line6
->properties
->capabilities
& LINE6_CAP_CONTROL_MIDI
)) {
262 /* skip MIDI initialization and report success */
266 err
= snd_line6_new_midi(line6
, &rmidi
);
270 line6midi
= kzalloc(sizeof(struct snd_line6_midi
), GFP_KERNEL
);
274 rmidi
->private_data
= line6midi
;
275 rmidi
->private_free
= snd_line6_midi_free
;
277 init_waitqueue_head(&line6midi
->send_wait
);
278 spin_lock_init(&line6midi
->lock
);
279 line6midi
->line6
= line6
;
281 err
= line6_midibuf_init(&line6midi
->midibuf_in
, MIDI_BUFFER_SIZE
, 0);
285 err
= line6_midibuf_init(&line6midi
->midibuf_out
, MIDI_BUFFER_SIZE
, 1);
289 line6
->line6midi
= line6midi
;
292 EXPORT_SYMBOL_GPL(line6_init_midi
);