2 * Line6 Linux USB driver - 0.9.1beta
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 <sound/core.h>
15 #include <sound/rawmidi.h>
23 #define line6_rawmidi_substream_midi(substream) \
24 ((struct snd_line6_midi *)((substream)->rmidi->private_data))
26 static int send_midi_async(struct usb_line6
*line6
, unsigned char *data
,
30 Pass data received via USB to MIDI.
32 void line6_midi_receive(struct usb_line6
*line6
, unsigned char *data
,
35 if (line6
->line6midi
->substream_receive
)
36 snd_rawmidi_receive(line6
->line6midi
->substream_receive
,
41 Read data from MIDI buffer and transmit them via USB.
43 static void line6_midi_transmit(struct snd_rawmidi_substream
*substream
)
45 struct usb_line6
*line6
=
46 line6_rawmidi_substream_midi(substream
)->line6
;
47 struct snd_line6_midi
*line6midi
= line6
->line6midi
;
48 struct midi_buffer
*mb
= &line6midi
->midibuf_out
;
50 unsigned char chunk
[line6
->max_packet_size
];
53 spin_lock_irqsave(&line6
->line6midi
->midi_transmit_lock
, flags
);
56 req
= min(line6_midibuf_bytes_free(mb
), line6
->max_packet_size
);
57 done
= snd_rawmidi_transmit_peek(substream
, chunk
, req
);
62 line6_midibuf_write(mb
, chunk
, done
);
63 snd_rawmidi_transmit_ack(substream
, done
);
67 done
= line6_midibuf_read(mb
, chunk
, line6
->max_packet_size
);
72 send_midi_async(line6
, chunk
, done
);
75 spin_unlock_irqrestore(&line6
->line6midi
->midi_transmit_lock
, flags
);
79 Notification of completion of MIDI transmission.
81 static void midi_sent(struct urb
*urb
)
86 struct usb_line6
*line6
= (struct usb_line6
*)urb
->context
;
89 kfree(urb
->transfer_buffer
);
92 if (status
== -ESHUTDOWN
)
95 spin_lock_irqsave(&line6
->line6midi
->send_urb_lock
, flags
);
96 num
= --line6
->line6midi
->num_active_send_urbs
;
99 line6_midi_transmit(line6
->line6midi
->substream_transmit
);
100 num
= line6
->line6midi
->num_active_send_urbs
;
104 wake_up(&line6
->line6midi
->send_wait
);
106 spin_unlock_irqrestore(&line6
->line6midi
->send_urb_lock
, flags
);
110 Send an asynchronous MIDI message.
111 Assumes that line6->line6midi->send_urb_lock is held
112 (i.e., this function is serialized).
114 static int send_midi_async(struct usb_line6
*line6
, unsigned char *data
,
119 unsigned char *transfer_buffer
;
121 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
124 dev_err(line6
->ifcdev
, "Out of memory\n");
128 transfer_buffer
= kmemdup(data
, length
, GFP_ATOMIC
);
130 if (transfer_buffer
== NULL
) {
132 dev_err(line6
->ifcdev
, "Out of memory\n");
136 usb_fill_int_urb(urb
, line6
->usbdev
,
137 usb_sndbulkpipe(line6
->usbdev
,
138 line6
->ep_control_write
),
139 transfer_buffer
, length
, midi_sent
, line6
,
141 urb
->actual_length
= 0;
142 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
145 dev_err(line6
->ifcdev
, "usb_submit_urb failed\n");
150 ++line6
->line6midi
->num_active_send_urbs
;
154 static int line6_midi_output_open(struct snd_rawmidi_substream
*substream
)
159 static int line6_midi_output_close(struct snd_rawmidi_substream
*substream
)
164 static void line6_midi_output_trigger(struct snd_rawmidi_substream
*substream
,
168 struct usb_line6
*line6
=
169 line6_rawmidi_substream_midi(substream
)->line6
;
171 line6
->line6midi
->substream_transmit
= substream
;
172 spin_lock_irqsave(&line6
->line6midi
->send_urb_lock
, flags
);
174 if (line6
->line6midi
->num_active_send_urbs
== 0)
175 line6_midi_transmit(substream
);
177 spin_unlock_irqrestore(&line6
->line6midi
->send_urb_lock
, flags
);
180 static void line6_midi_output_drain(struct snd_rawmidi_substream
*substream
)
182 struct usb_line6
*line6
=
183 line6_rawmidi_substream_midi(substream
)->line6
;
184 struct snd_line6_midi
*midi
= line6
->line6midi
;
185 wait_event_interruptible(midi
->send_wait
,
186 midi
->num_active_send_urbs
== 0);
189 static int line6_midi_input_open(struct snd_rawmidi_substream
*substream
)
194 static int line6_midi_input_close(struct snd_rawmidi_substream
*substream
)
199 static void line6_midi_input_trigger(struct snd_rawmidi_substream
*substream
,
202 struct usb_line6
*line6
=
203 line6_rawmidi_substream_midi(substream
)->line6
;
206 line6
->line6midi
->substream_receive
= substream
;
208 line6
->line6midi
->substream_receive
= 0;
211 static struct snd_rawmidi_ops line6_midi_output_ops
= {
212 .open
= line6_midi_output_open
,
213 .close
= line6_midi_output_close
,
214 .trigger
= line6_midi_output_trigger
,
215 .drain
= line6_midi_output_drain
,
218 static struct snd_rawmidi_ops line6_midi_input_ops
= {
219 .open
= line6_midi_input_open
,
220 .close
= line6_midi_input_close
,
221 .trigger
= line6_midi_input_trigger
,
225 Cleanup the Line6 MIDI device.
227 static void line6_cleanup_midi(struct snd_rawmidi
*rmidi
)
231 /* Create a MIDI device */
232 static int snd_line6_new_midi(struct snd_line6_midi
*line6midi
)
234 struct snd_rawmidi
*rmidi
;
237 err
= snd_rawmidi_new(line6midi
->line6
->card
, "Line6 MIDI", 0, 1, 1,
242 rmidi
->private_data
= line6midi
;
243 rmidi
->private_free
= line6_cleanup_midi
;
244 strcpy(rmidi
->id
, line6midi
->line6
->properties
->id
);
245 strcpy(rmidi
->name
, line6midi
->line6
->properties
->name
);
248 SNDRV_RAWMIDI_INFO_OUTPUT
|
249 SNDRV_RAWMIDI_INFO_INPUT
| SNDRV_RAWMIDI_INFO_DUPLEX
;
251 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
252 &line6_midi_output_ops
);
253 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
254 &line6_midi_input_ops
);
258 /* MIDI device destructor */
259 static int snd_line6_midi_free(struct snd_device
*device
)
261 struct snd_line6_midi
*line6midi
= device
->device_data
;
262 line6_midibuf_destroy(&line6midi
->midibuf_in
);
263 line6_midibuf_destroy(&line6midi
->midibuf_out
);
268 Initialize the Line6 MIDI subsystem.
270 int line6_init_midi(struct usb_line6
*line6
)
272 static struct snd_device_ops midi_ops
= {
273 .dev_free
= snd_line6_midi_free
,
277 struct snd_line6_midi
*line6midi
;
279 if (!(line6
->properties
->capabilities
& LINE6_BIT_CONTROL
)) {
280 /* skip MIDI initialization and report success */
284 line6midi
= kzalloc(sizeof(struct snd_line6_midi
), GFP_KERNEL
);
286 if (line6midi
== NULL
)
289 err
= line6_midibuf_init(&line6midi
->midibuf_in
, MIDI_BUFFER_SIZE
, 0);
295 err
= line6_midibuf_init(&line6midi
->midibuf_out
, MIDI_BUFFER_SIZE
, 1);
297 kfree(line6midi
->midibuf_in
.buf
);
302 line6midi
->line6
= line6
;
303 line6
->line6midi
= line6midi
;
305 err
= snd_device_new(line6
->card
, SNDRV_DEV_RAWMIDI
, line6midi
,
310 snd_card_set_dev(line6
->card
, line6
->ifcdev
);
312 err
= snd_line6_new_midi(line6midi
);
316 init_waitqueue_head(&line6midi
->send_wait
);
317 spin_lock_init(&line6midi
->send_urb_lock
);
318 spin_lock_init(&line6midi
->midi_transmit_lock
);