Merge branch 'for-2.6.30' of git://linux-nfs.org/~bfields/linux
[linux-2.6] / drivers / staging / line6 / midi.c
1 /*
2  * Line6 Linux USB driver - 0.8.0
3  *
4  * Copyright (C) 2004-2009 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 "driver.h"
13
14 #include <linux/usb.h>
15
16 #include <sound/core.h>
17 #include <sound/rawmidi.h>
18
19 #include "audio.h"
20 #include "midi.h"
21 #include "pod.h"
22 #include "usbdefs.h"
23
24
25 #define USE_MIDIBUF      1
26 #define OUTPUT_DUMP_ONLY 0
27
28
29 #define line6_rawmidi_substream_midi(substream) \
30         ((struct snd_line6_midi *)((substream)->rmidi->private_data))
31
32
33 static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
34                            int length);
35
36
37 /*
38         Pass data received via USB to MIDI.
39 */
40 void line6_midi_receive(struct usb_line6 *line6, unsigned char *data,
41                         int length)
42 {
43         if (line6->line6midi->substream_receive)
44                 snd_rawmidi_receive(line6->line6midi->substream_receive,
45                                     data, length);
46 }
47
48 /*
49         Read data from MIDI buffer and transmit them via USB.
50 */
51 static void line6_midi_transmit(struct snd_rawmidi_substream *substream)
52 {
53         struct usb_line6 *line6 = line6_rawmidi_substream_midi(substream)->line6;
54         struct snd_line6_midi *line6midi = line6->line6midi;
55         struct MidiBuffer *mb = &line6midi->midibuf_out;
56         unsigned long flags;
57         unsigned char chunk[line6->max_packet_size];
58         int req, done;
59
60         spin_lock_irqsave(&line6->line6midi->midi_transmit_lock, flags);
61
62         for (;;) {
63                 req = min(midibuf_bytes_free(mb), line6->max_packet_size);
64                 done = snd_rawmidi_transmit_peek(substream, chunk, req);
65
66                 if (done == 0)
67                         break;
68
69 #if DO_DUMP_MIDI_SEND
70                 line6_write_hexdump(line6, 's', chunk, done);
71 #endif
72                 midibuf_write(mb, chunk, done);
73                 snd_rawmidi_transmit_ack(substream, done);
74         }
75
76         for (;;) {
77                 done = midibuf_read(mb, chunk, line6->max_packet_size);
78
79                 if (done == 0)
80                         break;
81
82                 if (midibuf_skip_message(mb, line6midi->midi_mask_transmit))
83                         continue;
84
85                 send_midi_async(line6, chunk, done);
86         }
87
88         spin_unlock_irqrestore(&line6->line6midi->midi_transmit_lock, flags);
89 }
90
91 /*
92         Notification of completion of MIDI transmission.
93 */
94 static void midi_sent(struct urb *urb)
95 {
96         unsigned long flags;
97         int status;
98         int num;
99         struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
100
101         status = urb->status;
102         kfree(urb->transfer_buffer);
103         usb_free_urb(urb);
104
105         if (status == -ESHUTDOWN)
106                 return;
107
108         spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags);
109         num = --line6->line6midi->num_active_send_urbs;
110
111         if (num == 0) {
112                 line6_midi_transmit(line6->line6midi->substream_transmit);
113                 num = line6->line6midi->num_active_send_urbs;
114         }
115
116         if (num == 0)
117                 wake_up_interruptible(&line6->line6midi->send_wait);
118
119         spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags);
120 }
121
122 /*
123         Send an asynchronous MIDI message.
124         Assumes that line6->line6midi->send_urb_lock is held
125         (i.e., this function is serialized).
126 */
127 static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
128                            int length)
129 {
130         struct urb *urb;
131         int retval;
132         unsigned char *transfer_buffer;
133
134         urb = usb_alloc_urb(0, GFP_ATOMIC);
135
136         if (urb == 0) {
137                 dev_err(line6->ifcdev, "Out of memory\n");
138                 return -ENOMEM;
139         }
140
141 #if DO_DUMP_URB_SEND
142         line6_write_hexdump(line6, 'S', data, length);
143 #endif
144
145         transfer_buffer = kmalloc(length, GFP_ATOMIC);
146
147         if (transfer_buffer == 0) {
148                 usb_free_urb(urb);
149                 dev_err(line6->ifcdev, "Out of memory\n");
150                 return -ENOMEM;
151         }
152
153         memcpy(transfer_buffer, data, length);
154         usb_fill_int_urb(urb, line6->usbdev,
155                          usb_sndbulkpipe(line6->usbdev,
156                                          line6->ep_control_write),
157                          transfer_buffer, length, midi_sent, line6,
158                          line6->interval);
159         urb->actual_length = 0;
160         retval = usb_submit_urb(urb, GFP_ATOMIC);
161
162         if (retval < 0) {
163                 dev_err(line6->ifcdev, "usb_submit_urb failed\n");
164                 usb_free_urb(urb);
165                 return -EINVAL;
166         }
167
168         ++line6->line6midi->num_active_send_urbs;
169
170         switch (line6->usbdev->descriptor.idProduct) {
171         case LINE6_DEVID_BASSPODXT:
172         case LINE6_DEVID_BASSPODXTLIVE:
173         case LINE6_DEVID_BASSPODXTPRO:
174         case LINE6_DEVID_PODXT:
175         case LINE6_DEVID_PODXTLIVE:
176         case LINE6_DEVID_PODXTPRO:
177         case LINE6_DEVID_POCKETPOD:
178                 pod_midi_postprocess((struct usb_line6_pod *)line6, data,
179                                      length);
180                 break;
181
182         default:
183                 MISSING_CASE;
184         }
185
186         return 0;
187 }
188
189 static int line6_midi_output_open(struct snd_rawmidi_substream *substream)
190 {
191         return 0;
192 }
193
194 static int line6_midi_output_close(struct snd_rawmidi_substream *substream)
195 {
196         return 0;
197 }
198
199 static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream,
200                                       int up)
201 {
202         unsigned long flags;
203         struct usb_line6 *line6 = line6_rawmidi_substream_midi(substream)->line6;
204
205         line6->line6midi->substream_transmit = substream;
206         spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags);
207
208         if (line6->line6midi->num_active_send_urbs == 0)
209                 line6_midi_transmit(substream);
210
211         spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags);
212 }
213
214 static void line6_midi_output_drain(struct snd_rawmidi_substream *substream)
215 {
216         struct usb_line6 *line6 = line6_rawmidi_substream_midi(substream)->line6;
217         wait_queue_head_t *head = &line6->line6midi->send_wait;
218         DECLARE_WAITQUEUE(wait, current);
219         add_wait_queue(head, &wait);
220         current->state = TASK_INTERRUPTIBLE;
221
222         while (line6->line6midi->num_active_send_urbs > 0)
223                 if (signal_pending(current))
224                         break;
225                 else
226                         schedule();
227
228         current->state = TASK_RUNNING;
229         remove_wait_queue(head, &wait);
230 }
231
232 static int line6_midi_input_open(struct snd_rawmidi_substream *substream)
233 {
234         return 0;
235 }
236
237 static int line6_midi_input_close(struct snd_rawmidi_substream *substream)
238 {
239         return 0;
240 }
241
242 static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream,
243                                      int up)
244 {
245         struct usb_line6 *line6 = line6_rawmidi_substream_midi(substream)->line6;
246
247         if (up)
248                 line6->line6midi->substream_receive = substream;
249         else
250                 line6->line6midi->substream_receive = 0;
251 }
252
253 static struct snd_rawmidi_ops line6_midi_output_ops = {
254         .open = line6_midi_output_open,
255         .close = line6_midi_output_close,
256         .trigger = line6_midi_output_trigger,
257         .drain = line6_midi_output_drain,
258 };
259
260 static struct snd_rawmidi_ops line6_midi_input_ops = {
261         .open = line6_midi_input_open,
262         .close = line6_midi_input_close,
263         .trigger = line6_midi_input_trigger,
264 };
265
266 /*
267         Cleanup the Line6 MIDI device.
268 */
269 static void line6_cleanup_midi(struct snd_rawmidi *rmidi)
270 {
271 }
272
273 /* Create a MIDI device */
274 static int snd_line6_new_midi(struct snd_line6_midi *line6midi)
275 {
276         struct snd_rawmidi *rmidi;
277         int err;
278
279         err = snd_rawmidi_new(line6midi->line6->card, "Line6 MIDI", 0, 1, 1,
280                               &rmidi);
281         if (err < 0)
282                 return err;
283
284         rmidi->private_data = line6midi;
285         rmidi->private_free = line6_cleanup_midi;
286         strcpy(rmidi->name, line6midi->line6->properties->name);
287
288         rmidi->info_flags =
289                 SNDRV_RAWMIDI_INFO_OUTPUT |
290                 SNDRV_RAWMIDI_INFO_INPUT |
291                 SNDRV_RAWMIDI_INFO_DUPLEX;
292
293         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
294                             &line6_midi_output_ops);
295         snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
296                             &line6_midi_input_ops);
297         return 0;
298 }
299
300 /*
301         "read" request on "midi_mask_transmit" special file.
302 */
303 static ssize_t midi_get_midi_mask_transmit(struct device *dev,
304                                            struct device_attribute *attr,
305                                            char *buf)
306 {
307         struct usb_interface *interface = to_usb_interface(dev);
308         struct usb_line6 *line6 = usb_get_intfdata(interface);
309         return sprintf(buf, "%d\n", line6->line6midi->midi_mask_transmit);
310 }
311
312 /*
313         "write" request on "midi_mask" special file.
314 */
315 static ssize_t midi_set_midi_mask_transmit(struct device *dev,
316                                            struct device_attribute *attr,
317                                            const char *buf, size_t count)
318 {
319         struct usb_interface *interface = to_usb_interface(dev);
320         struct usb_line6 *line6 = usb_get_intfdata(interface);
321         int value = simple_strtoul(buf, NULL, 10);
322         line6->line6midi->midi_mask_transmit = value;
323         return count;
324 }
325
326 /*
327         "read" request on "midi_mask_receive" special file.
328 */
329 static ssize_t midi_get_midi_mask_receive(struct device *dev,
330                                           struct device_attribute *attr,
331                                           char *buf)
332 {
333         struct usb_interface *interface = to_usb_interface(dev);
334         struct usb_line6 *line6 = usb_get_intfdata(interface);
335         return sprintf(buf, "%d\n", line6->line6midi->midi_mask_receive);
336 }
337
338 /*
339         "write" request on "midi_mask" special file.
340 */
341 static ssize_t midi_set_midi_mask_receive(struct device *dev,
342                                           struct device_attribute *attr,
343                                           const char *buf, size_t count)
344 {
345         struct usb_interface *interface = to_usb_interface(dev);
346         struct usb_line6 *line6 = usb_get_intfdata(interface);
347         int value = simple_strtoul(buf, NULL, 10);
348         line6->line6midi->midi_mask_receive = value;
349         return count;
350 }
351
352 static DEVICE_ATTR(midi_mask_transmit, S_IWUGO | S_IRUGO, midi_get_midi_mask_transmit, midi_set_midi_mask_transmit);
353 static DEVICE_ATTR(midi_mask_receive, S_IWUGO | S_IRUGO, midi_get_midi_mask_receive, midi_set_midi_mask_receive);
354
355 /* MIDI device destructor */
356 static int snd_line6_midi_free(struct snd_device *device)
357 {
358         struct snd_line6_midi *line6midi = device->device_data;
359         device_remove_file(line6midi->line6->ifcdev, &dev_attr_midi_mask_transmit);
360         device_remove_file(line6midi->line6->ifcdev, &dev_attr_midi_mask_receive);
361         midibuf_destroy(&line6midi->midibuf_in);
362         midibuf_destroy(&line6midi->midibuf_out);
363         return 0;
364 }
365
366 /*
367         Initialize the Line6 MIDI subsystem.
368 */
369 int line6_init_midi(struct usb_line6 *line6)
370 {
371         static struct snd_device_ops midi_ops = {
372                 .dev_free = snd_line6_midi_free,
373         };
374
375         int err;
376         struct snd_line6_midi *line6midi;
377
378         if (!(line6->properties->capabilities & LINE6_BIT_CONTROL))
379                 return 0;  /* skip MIDI initialization and report success */
380
381         line6midi = kzalloc(sizeof(struct snd_line6_midi), GFP_KERNEL);
382
383         if (line6midi == NULL)
384                 return -ENOMEM;
385
386         err = midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0);
387         if (err < 0)
388                 return err;
389
390         err = midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1);
391         if (err < 0)
392                 return err;
393
394         line6midi->line6 = line6;
395         line6midi->midi_mask_transmit = 1;
396         line6midi->midi_mask_receive = 4;
397         line6->line6midi = line6midi;
398
399         err = snd_device_new(line6->card, SNDRV_DEV_RAWMIDI, line6midi,
400                              &midi_ops);
401         if (err < 0)
402                 return err;
403
404         snd_card_set_dev(line6->card, line6->ifcdev);
405
406         err = snd_line6_new_midi(line6midi);
407         if (err < 0)
408                 return err;
409
410         err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_transmit);
411         if (err < 0)
412                 return err;
413
414         err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_receive);
415         if (err < 0)
416                 return err;
417
418         init_waitqueue_head(&line6midi->send_wait);
419         spin_lock_init(&line6midi->send_urb_lock);
420         spin_lock_init(&line6midi->midi_transmit_lock);
421         return 0;
422 }