xref: /openbmc/linux/sound/usb/line6/midi.c (revision 2209fda3)
1 /*
2  * Line 6 Linux USB driver
3  *
4  * Copyright (C) 2004-2010 Markus Grabner (grabner@icg.tugraz.at)
5  *
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.
9  *
10  */
11 
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>
17 
18 #include "driver.h"
19 #include "midi.h"
20 
21 #define line6_rawmidi_substream_midi(substream) \
22 	((struct snd_line6_midi *)((substream)->rmidi->private_data))
23 
24 static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
25 			   int length);
26 
27 /*
28 	Pass data received via USB to MIDI.
29 */
30 void line6_midi_receive(struct usb_line6 *line6, unsigned char *data,
31 			int length)
32 {
33 	if (line6->line6midi->substream_receive)
34 		snd_rawmidi_receive(line6->line6midi->substream_receive,
35 				    data, length);
36 }
37 
38 /*
39 	Read data from MIDI buffer and transmit them via USB.
40 */
41 static void line6_midi_transmit(struct snd_rawmidi_substream *substream)
42 {
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];
48 	int req, done;
49 
50 	for (;;) {
51 		req = min(line6_midibuf_bytes_free(mb), line6->max_packet_size);
52 		done = snd_rawmidi_transmit_peek(substream, chunk, req);
53 
54 		if (done == 0)
55 			break;
56 
57 		line6_midibuf_write(mb, chunk, done);
58 		snd_rawmidi_transmit_ack(substream, done);
59 	}
60 
61 	for (;;) {
62 		done = line6_midibuf_read(mb, chunk,
63 					  LINE6_FALLBACK_MAXPACKETSIZE);
64 
65 		if (done == 0)
66 			break;
67 
68 		send_midi_async(line6, chunk, done);
69 	}
70 }
71 
72 /*
73 	Notification of completion of MIDI transmission.
74 */
75 static void midi_sent(struct urb *urb)
76 {
77 	unsigned long flags;
78 	int status;
79 	int num;
80 	struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
81 
82 	status = urb->status;
83 	kfree(urb->transfer_buffer);
84 	usb_free_urb(urb);
85 
86 	if (status == -ESHUTDOWN)
87 		return;
88 
89 	spin_lock_irqsave(&line6->line6midi->lock, flags);
90 	num = --line6->line6midi->num_active_send_urbs;
91 
92 	if (num == 0) {
93 		line6_midi_transmit(line6->line6midi->substream_transmit);
94 		num = line6->line6midi->num_active_send_urbs;
95 	}
96 
97 	if (num == 0)
98 		wake_up(&line6->line6midi->send_wait);
99 
100 	spin_unlock_irqrestore(&line6->line6midi->lock, flags);
101 }
102 
103 /*
104 	Send an asynchronous MIDI message.
105 	Assumes that line6->line6midi->lock is held
106 	(i.e., this function is serialized).
107 */
108 static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
109 			   int length)
110 {
111 	struct urb *urb;
112 	int retval;
113 	unsigned char *transfer_buffer;
114 
115 	urb = usb_alloc_urb(0, GFP_ATOMIC);
116 
117 	if (urb == NULL)
118 		return -ENOMEM;
119 
120 	transfer_buffer = kmemdup(data, length, GFP_ATOMIC);
121 
122 	if (transfer_buffer == NULL) {
123 		usb_free_urb(urb);
124 		return -ENOMEM;
125 	}
126 
127 	usb_fill_int_urb(urb, line6->usbdev,
128 			 usb_sndintpipe(line6->usbdev,
129 					 line6->properties->ep_ctrl_w),
130 			 transfer_buffer, length, midi_sent, line6,
131 			 line6->interval);
132 	urb->actual_length = 0;
133 	retval = usb_urb_ep_type_check(urb);
134 	if (retval < 0)
135 		goto error;
136 
137 	retval = usb_submit_urb(urb, GFP_ATOMIC);
138 	if (retval < 0)
139 		goto error;
140 
141 	++line6->line6midi->num_active_send_urbs;
142 	return 0;
143 
144  error:
145 	dev_err(line6->ifcdev, "usb_submit_urb failed\n");
146 	usb_free_urb(urb);
147 	return retval;
148 }
149 
150 static int line6_midi_output_open(struct snd_rawmidi_substream *substream)
151 {
152 	return 0;
153 }
154 
155 static int line6_midi_output_close(struct snd_rawmidi_substream *substream)
156 {
157 	return 0;
158 }
159 
160 static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream,
161 				      int up)
162 {
163 	unsigned long flags;
164 	struct usb_line6 *line6 =
165 	    line6_rawmidi_substream_midi(substream)->line6;
166 
167 	line6->line6midi->substream_transmit = substream;
168 	spin_lock_irqsave(&line6->line6midi->lock, flags);
169 
170 	if (line6->line6midi->num_active_send_urbs == 0)
171 		line6_midi_transmit(substream);
172 
173 	spin_unlock_irqrestore(&line6->line6midi->lock, flags);
174 }
175 
176 static void line6_midi_output_drain(struct snd_rawmidi_substream *substream)
177 {
178 	struct usb_line6 *line6 =
179 	    line6_rawmidi_substream_midi(substream)->line6;
180 	struct snd_line6_midi *midi = line6->line6midi;
181 
182 	wait_event_interruptible(midi->send_wait,
183 				 midi->num_active_send_urbs == 0);
184 }
185 
186 static int line6_midi_input_open(struct snd_rawmidi_substream *substream)
187 {
188 	return 0;
189 }
190 
191 static int line6_midi_input_close(struct snd_rawmidi_substream *substream)
192 {
193 	return 0;
194 }
195 
196 static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream,
197 				     int up)
198 {
199 	struct usb_line6 *line6 =
200 	    line6_rawmidi_substream_midi(substream)->line6;
201 
202 	if (up)
203 		line6->line6midi->substream_receive = substream;
204 	else
205 		line6->line6midi->substream_receive = NULL;
206 }
207 
208 static const struct snd_rawmidi_ops line6_midi_output_ops = {
209 	.open = line6_midi_output_open,
210 	.close = line6_midi_output_close,
211 	.trigger = line6_midi_output_trigger,
212 	.drain = line6_midi_output_drain,
213 };
214 
215 static const struct snd_rawmidi_ops line6_midi_input_ops = {
216 	.open = line6_midi_input_open,
217 	.close = line6_midi_input_close,
218 	.trigger = line6_midi_input_trigger,
219 };
220 
221 /* Create a MIDI device */
222 static int snd_line6_new_midi(struct usb_line6 *line6,
223 			      struct snd_rawmidi **rmidi_ret)
224 {
225 	struct snd_rawmidi *rmidi;
226 	int err;
227 
228 	err = snd_rawmidi_new(line6->card, "Line 6 MIDI", 0, 1, 1, rmidi_ret);
229 	if (err < 0)
230 		return err;
231 
232 	rmidi = *rmidi_ret;
233 	strcpy(rmidi->id, line6->properties->id);
234 	strcpy(rmidi->name, line6->properties->name);
235 
236 	rmidi->info_flags =
237 	    SNDRV_RAWMIDI_INFO_OUTPUT |
238 	    SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
239 
240 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
241 			    &line6_midi_output_ops);
242 	snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
243 			    &line6_midi_input_ops);
244 	return 0;
245 }
246 
247 /* MIDI device destructor */
248 static void snd_line6_midi_free(struct snd_rawmidi *rmidi)
249 {
250 	struct snd_line6_midi *line6midi = rmidi->private_data;
251 
252 	line6_midibuf_destroy(&line6midi->midibuf_in);
253 	line6_midibuf_destroy(&line6midi->midibuf_out);
254 	kfree(line6midi);
255 }
256 
257 /*
258 	Initialize the Line 6 MIDI subsystem.
259 */
260 int line6_init_midi(struct usb_line6 *line6)
261 {
262 	int err;
263 	struct snd_rawmidi *rmidi;
264 	struct snd_line6_midi *line6midi;
265 
266 	if (!(line6->properties->capabilities & LINE6_CAP_CONTROL_MIDI)) {
267 		/* skip MIDI initialization and report success */
268 		return 0;
269 	}
270 
271 	err = snd_line6_new_midi(line6, &rmidi);
272 	if (err < 0)
273 		return err;
274 
275 	line6midi = kzalloc(sizeof(struct snd_line6_midi), GFP_KERNEL);
276 	if (!line6midi)
277 		return -ENOMEM;
278 
279 	rmidi->private_data = line6midi;
280 	rmidi->private_free = snd_line6_midi_free;
281 
282 	init_waitqueue_head(&line6midi->send_wait);
283 	spin_lock_init(&line6midi->lock);
284 	line6midi->line6 = line6;
285 
286 	err = line6_midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0);
287 	if (err < 0)
288 		return err;
289 
290 	err = line6_midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1);
291 	if (err < 0)
292 		return err;
293 
294 	line6->line6midi = line6midi;
295 	return 0;
296 }
297 EXPORT_SYMBOL_GPL(line6_init_midi);
298