cfg80211/mac80211: fix BSS leaks when abandoning assoc attempts
[linux/fpc-iii.git] / sound / usb / 6fire / midi.c
blob3d410969553e2da2748b0587416fb5a8e8b41aab
1 /*
2 * Linux driver for TerraTec DMX 6Fire USB
4 * Rawmidi driver
6 * Author: Torsten Schenk <torsten.schenk@zoho.com>
7 * Created: Jan 01, 2011
8 * Copyright: (C) Torsten Schenk
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <sound/rawmidi.h>
18 #include "midi.h"
19 #include "chip.h"
20 #include "comm.h"
22 enum {
23 MIDI_BUFSIZE = 64
26 static void usb6fire_midi_out_handler(struct urb *urb)
28 struct midi_runtime *rt = urb->context;
29 int ret;
30 unsigned long flags;
32 spin_lock_irqsave(&rt->out_lock, flags);
34 if (rt->out) {
35 ret = snd_rawmidi_transmit(rt->out, rt->out_buffer + 4,
36 MIDI_BUFSIZE - 4);
37 if (ret > 0) { /* more data available, send next packet */
38 rt->out_buffer[1] = ret + 2;
39 rt->out_buffer[3] = rt->out_serial++;
40 urb->transfer_buffer_length = ret + 4;
42 ret = usb_submit_urb(urb, GFP_ATOMIC);
43 if (ret < 0)
44 dev_err(&urb->dev->dev,
45 "midi out urb submit failed: %d\n",
46 ret);
47 } else /* no more data to transmit */
48 rt->out = NULL;
50 spin_unlock_irqrestore(&rt->out_lock, flags);
53 static void usb6fire_midi_in_received(
54 struct midi_runtime *rt, u8 *data, int length)
56 unsigned long flags;
58 spin_lock_irqsave(&rt->in_lock, flags);
59 if (rt->in)
60 snd_rawmidi_receive(rt->in, data, length);
61 spin_unlock_irqrestore(&rt->in_lock, flags);
64 static int usb6fire_midi_out_open(struct snd_rawmidi_substream *alsa_sub)
66 return 0;
69 static int usb6fire_midi_out_close(struct snd_rawmidi_substream *alsa_sub)
71 return 0;
74 static void usb6fire_midi_out_trigger(
75 struct snd_rawmidi_substream *alsa_sub, int up)
77 struct midi_runtime *rt = alsa_sub->rmidi->private_data;
78 struct urb *urb = &rt->out_urb;
79 __s8 ret;
80 unsigned long flags;
82 spin_lock_irqsave(&rt->out_lock, flags);
83 if (up) { /* start transfer */
84 if (rt->out) { /* we are already transmitting so just return */
85 spin_unlock_irqrestore(&rt->out_lock, flags);
86 return;
89 ret = snd_rawmidi_transmit(alsa_sub, rt->out_buffer + 4,
90 MIDI_BUFSIZE - 4);
91 if (ret > 0) {
92 rt->out_buffer[1] = ret + 2;
93 rt->out_buffer[3] = rt->out_serial++;
94 urb->transfer_buffer_length = ret + 4;
96 ret = usb_submit_urb(urb, GFP_ATOMIC);
97 if (ret < 0)
98 dev_err(&urb->dev->dev,
99 "midi out urb submit failed: %d\n",
100 ret);
101 else
102 rt->out = alsa_sub;
104 } else if (rt->out == alsa_sub)
105 rt->out = NULL;
106 spin_unlock_irqrestore(&rt->out_lock, flags);
109 static void usb6fire_midi_out_drain(struct snd_rawmidi_substream *alsa_sub)
111 struct midi_runtime *rt = alsa_sub->rmidi->private_data;
112 int retry = 0;
114 while (rt->out && retry++ < 100)
115 msleep(10);
118 static int usb6fire_midi_in_open(struct snd_rawmidi_substream *alsa_sub)
120 return 0;
123 static int usb6fire_midi_in_close(struct snd_rawmidi_substream *alsa_sub)
125 return 0;
128 static void usb6fire_midi_in_trigger(
129 struct snd_rawmidi_substream *alsa_sub, int up)
131 struct midi_runtime *rt = alsa_sub->rmidi->private_data;
132 unsigned long flags;
134 spin_lock_irqsave(&rt->in_lock, flags);
135 if (up)
136 rt->in = alsa_sub;
137 else
138 rt->in = NULL;
139 spin_unlock_irqrestore(&rt->in_lock, flags);
142 static struct snd_rawmidi_ops out_ops = {
143 .open = usb6fire_midi_out_open,
144 .close = usb6fire_midi_out_close,
145 .trigger = usb6fire_midi_out_trigger,
146 .drain = usb6fire_midi_out_drain
149 static struct snd_rawmidi_ops in_ops = {
150 .open = usb6fire_midi_in_open,
151 .close = usb6fire_midi_in_close,
152 .trigger = usb6fire_midi_in_trigger
155 int usb6fire_midi_init(struct sfire_chip *chip)
157 int ret;
158 struct midi_runtime *rt = kzalloc(sizeof(struct midi_runtime),
159 GFP_KERNEL);
160 struct comm_runtime *comm_rt = chip->comm;
162 if (!rt)
163 return -ENOMEM;
165 rt->out_buffer = kzalloc(MIDI_BUFSIZE, GFP_KERNEL);
166 if (!rt->out_buffer) {
167 kfree(rt);
168 return -ENOMEM;
171 rt->chip = chip;
172 rt->in_received = usb6fire_midi_in_received;
173 rt->out_buffer[0] = 0x80; /* 'send midi' command */
174 rt->out_buffer[1] = 0x00; /* size of data */
175 rt->out_buffer[2] = 0x00; /* always 0 */
176 spin_lock_init(&rt->in_lock);
177 spin_lock_init(&rt->out_lock);
179 comm_rt->init_urb(comm_rt, &rt->out_urb, rt->out_buffer, rt,
180 usb6fire_midi_out_handler);
182 ret = snd_rawmidi_new(chip->card, "6FireUSB", 0, 1, 1, &rt->instance);
183 if (ret < 0) {
184 kfree(rt->out_buffer);
185 kfree(rt);
186 dev_err(&chip->dev->dev, "unable to create midi.\n");
187 return ret;
189 rt->instance->private_data = rt;
190 strcpy(rt->instance->name, "DMX6FireUSB MIDI");
191 rt->instance->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
192 SNDRV_RAWMIDI_INFO_INPUT |
193 SNDRV_RAWMIDI_INFO_DUPLEX;
194 snd_rawmidi_set_ops(rt->instance, SNDRV_RAWMIDI_STREAM_OUTPUT,
195 &out_ops);
196 snd_rawmidi_set_ops(rt->instance, SNDRV_RAWMIDI_STREAM_INPUT,
197 &in_ops);
199 chip->midi = rt;
200 return 0;
203 void usb6fire_midi_abort(struct sfire_chip *chip)
205 struct midi_runtime *rt = chip->midi;
207 if (rt)
208 usb_poison_urb(&rt->out_urb);
211 void usb6fire_midi_destroy(struct sfire_chip *chip)
213 struct midi_runtime *rt = chip->midi;
215 kfree(rt->out_buffer);
216 kfree(rt);
217 chip->midi = NULL;