usb: dwc3: keystone: drop dma_mask configuration
[linux/fpc-iii.git] / drivers / staging / line6 / midi.c
blob1ac343b649c12fece6e21b90f250d9f8f508260a
1 /*
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>
17 #include "audio.h"
18 #include "driver.h"
19 #include "midi.h"
20 #include "pod.h"
21 #include "usbdefs.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,
27 int length);
30 Pass data received via USB to MIDI.
32 void line6_midi_receive(struct usb_line6 *line6, unsigned char *data,
33 int length)
35 if (line6->line6midi->substream_receive)
36 snd_rawmidi_receive(line6->line6midi->substream_receive,
37 data, length);
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;
49 unsigned long flags;
50 unsigned char chunk[LINE6_FALLBACK_MAXPACKETSIZE];
51 int req, done;
53 spin_lock_irqsave(&line6->line6midi->midi_transmit_lock, flags);
55 for (;;) {
56 req = min(line6_midibuf_bytes_free(mb), line6->max_packet_size);
57 done = snd_rawmidi_transmit_peek(substream, chunk, req);
59 if (done == 0)
60 break;
62 line6_midibuf_write(mb, chunk, done);
63 snd_rawmidi_transmit_ack(substream, done);
66 for (;;) {
67 done = line6_midibuf_read(mb, chunk,
68 LINE6_FALLBACK_MAXPACKETSIZE);
70 if (done == 0)
71 break;
73 send_midi_async(line6, chunk, done);
76 spin_unlock_irqrestore(&line6->line6midi->midi_transmit_lock, flags);
80 Notification of completion of MIDI transmission.
82 static void midi_sent(struct urb *urb)
84 unsigned long flags;
85 int status;
86 int num;
87 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
89 status = urb->status;
90 kfree(urb->transfer_buffer);
91 usb_free_urb(urb);
93 if (status == -ESHUTDOWN)
94 return;
96 spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags);
97 num = --line6->line6midi->num_active_send_urbs;
99 if (num == 0) {
100 line6_midi_transmit(line6->line6midi->substream_transmit);
101 num = line6->line6midi->num_active_send_urbs;
104 if (num == 0)
105 wake_up(&line6->line6midi->send_wait);
107 spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags);
111 Send an asynchronous MIDI message.
112 Assumes that line6->line6midi->send_urb_lock is held
113 (i.e., this function is serialized).
115 static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
116 int length)
118 struct urb *urb;
119 int retval;
120 unsigned char *transfer_buffer;
122 urb = usb_alloc_urb(0, GFP_ATOMIC);
124 if (urb == NULL) {
125 dev_err(line6->ifcdev, "Out of memory\n");
126 return -ENOMEM;
129 transfer_buffer = kmemdup(data, length, GFP_ATOMIC);
131 if (transfer_buffer == NULL) {
132 usb_free_urb(urb);
133 dev_err(line6->ifcdev, "Out of memory\n");
134 return -ENOMEM;
137 usb_fill_int_urb(urb, line6->usbdev,
138 usb_sndbulkpipe(line6->usbdev,
139 line6->ep_control_write),
140 transfer_buffer, length, midi_sent, line6,
141 line6->interval);
142 urb->actual_length = 0;
143 retval = usb_submit_urb(urb, GFP_ATOMIC);
145 if (retval < 0) {
146 dev_err(line6->ifcdev, "usb_submit_urb failed\n");
147 usb_free_urb(urb);
148 return retval;
151 ++line6->line6midi->num_active_send_urbs;
152 return 0;
155 static int line6_midi_output_open(struct snd_rawmidi_substream *substream)
157 return 0;
160 static int line6_midi_output_close(struct snd_rawmidi_substream *substream)
162 return 0;
165 static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream,
166 int up)
168 unsigned long flags;
169 struct usb_line6 *line6 =
170 line6_rawmidi_substream_midi(substream)->line6;
172 line6->line6midi->substream_transmit = substream;
173 spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags);
175 if (line6->line6midi->num_active_send_urbs == 0)
176 line6_midi_transmit(substream);
178 spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags);
181 static void line6_midi_output_drain(struct snd_rawmidi_substream *substream)
183 struct usb_line6 *line6 =
184 line6_rawmidi_substream_midi(substream)->line6;
185 struct snd_line6_midi *midi = line6->line6midi;
187 wait_event_interruptible(midi->send_wait,
188 midi->num_active_send_urbs == 0);
191 static int line6_midi_input_open(struct snd_rawmidi_substream *substream)
193 return 0;
196 static int line6_midi_input_close(struct snd_rawmidi_substream *substream)
198 return 0;
201 static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream,
202 int up)
204 struct usb_line6 *line6 =
205 line6_rawmidi_substream_midi(substream)->line6;
207 if (up)
208 line6->line6midi->substream_receive = substream;
209 else
210 line6->line6midi->substream_receive = NULL;
213 static struct snd_rawmidi_ops line6_midi_output_ops = {
214 .open = line6_midi_output_open,
215 .close = line6_midi_output_close,
216 .trigger = line6_midi_output_trigger,
217 .drain = line6_midi_output_drain,
220 static struct snd_rawmidi_ops line6_midi_input_ops = {
221 .open = line6_midi_input_open,
222 .close = line6_midi_input_close,
223 .trigger = line6_midi_input_trigger,
227 Cleanup the Line6 MIDI device.
229 static void line6_cleanup_midi(struct snd_rawmidi *rmidi)
233 /* Create a MIDI device */
234 static int snd_line6_new_midi(struct snd_line6_midi *line6midi)
236 struct snd_rawmidi *rmidi;
237 int err;
239 err = snd_rawmidi_new(line6midi->line6->card, "Line6 MIDI", 0, 1, 1,
240 &rmidi);
241 if (err < 0)
242 return err;
244 rmidi->private_data = line6midi;
245 rmidi->private_free = line6_cleanup_midi;
246 strcpy(rmidi->id, line6midi->line6->properties->id);
247 strcpy(rmidi->name, line6midi->line6->properties->name);
249 rmidi->info_flags =
250 SNDRV_RAWMIDI_INFO_OUTPUT |
251 SNDRV_RAWMIDI_INFO_INPUT | SNDRV_RAWMIDI_INFO_DUPLEX;
253 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
254 &line6_midi_output_ops);
255 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
256 &line6_midi_input_ops);
257 return 0;
260 /* MIDI device destructor */
261 static int snd_line6_midi_free(struct snd_device *device)
263 struct snd_line6_midi *line6midi = device->device_data;
265 line6_midibuf_destroy(&line6midi->midibuf_in);
266 line6_midibuf_destroy(&line6midi->midibuf_out);
267 return 0;
271 Initialize the Line6 MIDI subsystem.
273 int line6_init_midi(struct usb_line6 *line6)
275 static struct snd_device_ops midi_ops = {
276 .dev_free = snd_line6_midi_free,
279 int err;
280 struct snd_line6_midi *line6midi;
282 if (!(line6->properties->capabilities & LINE6_BIT_CONTROL)) {
283 /* skip MIDI initialization and report success */
284 return 0;
287 line6midi = kzalloc(sizeof(struct snd_line6_midi), GFP_KERNEL);
289 if (line6midi == NULL)
290 return -ENOMEM;
292 err = line6_midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0);
293 if (err < 0) {
294 kfree(line6midi);
295 return err;
298 err = line6_midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1);
299 if (err < 0) {
300 kfree(line6midi->midibuf_in.buf);
301 kfree(line6midi);
302 return err;
305 line6midi->line6 = line6;
306 line6->line6midi = line6midi;
308 err = snd_device_new(line6->card, SNDRV_DEV_RAWMIDI, line6midi,
309 &midi_ops);
310 if (err < 0)
311 return err;
313 err = snd_line6_new_midi(line6midi);
314 if (err < 0)
315 return err;
317 init_waitqueue_head(&line6midi->send_wait);
318 spin_lock_init(&line6midi->send_urb_lock);
319 spin_lock_init(&line6midi->midi_transmit_lock);
320 return 0;