[ARM] pxa: Gumstix Verdex PCMCIA support
[linux-2.6/verdex.git] / drivers / staging / line6 / midi.c
blob89a2b17e9cafc78744752ae41edcc062184db98b
1 /*
2 * Line6 Linux USB driver - 0.8.0
4 * Copyright (C) 2004-2009 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 "driver.h"
14 #include <linux/usb.h>
16 #include <sound/core.h>
17 #include <sound/rawmidi.h>
19 #include "audio.h"
20 #include "midi.h"
21 #include "pod.h"
22 #include "usbdefs.h"
25 #define USE_MIDIBUF 1
26 #define OUTPUT_DUMP_ONLY 0
29 #define line6_rawmidi_substream_midi(substream) \
30 ((struct snd_line6_midi *)((substream)->rmidi->private_data))
33 static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
34 int length);
38 Pass data received via USB to MIDI.
40 void line6_midi_receive(struct usb_line6 *line6, unsigned char *data,
41 int length)
43 if (line6->line6midi->substream_receive)
44 snd_rawmidi_receive(line6->line6midi->substream_receive,
45 data, length);
49 Read data from MIDI buffer and transmit them via USB.
51 static void line6_midi_transmit(struct snd_rawmidi_substream *substream)
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;
60 spin_lock_irqsave(&line6->line6midi->midi_transmit_lock, flags);
62 for (;;) {
63 req = min(midibuf_bytes_free(mb), line6->max_packet_size);
64 done = snd_rawmidi_transmit_peek(substream, chunk, req);
66 if (done == 0)
67 break;
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);
76 for (;;) {
77 done = midibuf_read(mb, chunk, line6->max_packet_size);
79 if (done == 0)
80 break;
82 if (midibuf_skip_message(mb, line6midi->midi_mask_transmit))
83 continue;
85 send_midi_async(line6, chunk, done);
88 spin_unlock_irqrestore(&line6->line6midi->midi_transmit_lock, flags);
92 Notification of completion of MIDI transmission.
94 static void midi_sent(struct urb *urb)
96 unsigned long flags;
97 int status;
98 int num;
99 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
101 status = urb->status;
102 kfree(urb->transfer_buffer);
103 usb_free_urb(urb);
105 if (status == -ESHUTDOWN)
106 return;
108 spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags);
109 num = --line6->line6midi->num_active_send_urbs;
111 if (num == 0) {
112 line6_midi_transmit(line6->line6midi->substream_transmit);
113 num = line6->line6midi->num_active_send_urbs;
116 if (num == 0)
117 wake_up_interruptible(&line6->line6midi->send_wait);
119 spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags);
123 Send an asynchronous MIDI message.
124 Assumes that line6->line6midi->send_urb_lock is held
125 (i.e., this function is serialized).
127 static int send_midi_async(struct usb_line6 *line6, unsigned char *data,
128 int length)
130 struct urb *urb;
131 int retval;
132 unsigned char *transfer_buffer;
134 urb = usb_alloc_urb(0, GFP_ATOMIC);
136 if (urb == 0) {
137 dev_err(line6->ifcdev, "Out of memory\n");
138 return -ENOMEM;
141 #if DO_DUMP_URB_SEND
142 line6_write_hexdump(line6, 'S', data, length);
143 #endif
145 transfer_buffer = kmalloc(length, GFP_ATOMIC);
147 if (transfer_buffer == 0) {
148 usb_free_urb(urb);
149 dev_err(line6->ifcdev, "Out of memory\n");
150 return -ENOMEM;
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);
162 if (retval < 0) {
163 dev_err(line6->ifcdev, "usb_submit_urb failed\n");
164 usb_free_urb(urb);
165 return -EINVAL;
168 ++line6->line6midi->num_active_send_urbs;
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;
182 default:
183 MISSING_CASE;
186 return 0;
189 static int line6_midi_output_open(struct snd_rawmidi_substream *substream)
191 return 0;
194 static int line6_midi_output_close(struct snd_rawmidi_substream *substream)
196 return 0;
199 static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream,
200 int up)
202 unsigned long flags;
203 struct usb_line6 *line6 = line6_rawmidi_substream_midi(substream)->line6;
205 line6->line6midi->substream_transmit = substream;
206 spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags);
208 if (line6->line6midi->num_active_send_urbs == 0)
209 line6_midi_transmit(substream);
211 spin_unlock_irqrestore(&line6->line6midi->send_urb_lock, flags);
214 static void line6_midi_output_drain(struct snd_rawmidi_substream *substream)
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;
222 while (line6->line6midi->num_active_send_urbs > 0)
223 if (signal_pending(current))
224 break;
225 else
226 schedule();
228 current->state = TASK_RUNNING;
229 remove_wait_queue(head, &wait);
232 static int line6_midi_input_open(struct snd_rawmidi_substream *substream)
234 return 0;
237 static int line6_midi_input_close(struct snd_rawmidi_substream *substream)
239 return 0;
242 static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream,
243 int up)
245 struct usb_line6 *line6 = line6_rawmidi_substream_midi(substream)->line6;
247 if (up)
248 line6->line6midi->substream_receive = substream;
249 else
250 line6->line6midi->substream_receive = 0;
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,
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,
267 Cleanup the Line6 MIDI device.
269 static void line6_cleanup_midi(struct snd_rawmidi *rmidi)
273 /* Create a MIDI device */
274 static int snd_line6_new_midi(struct snd_line6_midi *line6midi)
276 struct snd_rawmidi *rmidi;
277 int err;
279 err = snd_rawmidi_new(line6midi->line6->card, "Line6 MIDI", 0, 1, 1,
280 &rmidi);
281 if (err < 0)
282 return err;
284 rmidi->private_data = line6midi;
285 rmidi->private_free = line6_cleanup_midi;
286 strcpy(rmidi->name, line6midi->line6->properties->name);
288 rmidi->info_flags =
289 SNDRV_RAWMIDI_INFO_OUTPUT |
290 SNDRV_RAWMIDI_INFO_INPUT |
291 SNDRV_RAWMIDI_INFO_DUPLEX;
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;
301 "read" request on "midi_mask_transmit" special file.
303 static ssize_t midi_get_midi_mask_transmit(struct device *dev,
304 struct device_attribute *attr,
305 char *buf)
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);
313 "write" request on "midi_mask" special file.
315 static ssize_t midi_set_midi_mask_transmit(struct device *dev,
316 struct device_attribute *attr,
317 const char *buf, size_t count)
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;
327 "read" request on "midi_mask_receive" special file.
329 static ssize_t midi_get_midi_mask_receive(struct device *dev,
330 struct device_attribute *attr,
331 char *buf)
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);
339 "write" request on "midi_mask" special file.
341 static ssize_t midi_set_midi_mask_receive(struct device *dev,
342 struct device_attribute *attr,
343 const char *buf, size_t count)
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;
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);
355 /* MIDI device destructor */
356 static int snd_line6_midi_free(struct snd_device *device)
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;
367 Initialize the Line6 MIDI subsystem.
369 int line6_init_midi(struct usb_line6 *line6)
371 static struct snd_device_ops midi_ops = {
372 .dev_free = snd_line6_midi_free,
375 int err;
376 struct snd_line6_midi *line6midi;
378 if (!(line6->properties->capabilities & LINE6_BIT_CONTROL))
379 return 0; /* skip MIDI initialization and report success */
381 line6midi = kzalloc(sizeof(struct snd_line6_midi), GFP_KERNEL);
383 if (line6midi == NULL)
384 return -ENOMEM;
386 err = midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0);
387 if (err < 0)
388 return err;
390 err = midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1);
391 if (err < 0)
392 return err;
394 line6midi->line6 = line6;
395 line6midi->midi_mask_transmit = 1;
396 line6midi->midi_mask_receive = 4;
397 line6->line6midi = line6midi;
399 err = snd_device_new(line6->card, SNDRV_DEV_RAWMIDI, line6midi,
400 &midi_ops);
401 if (err < 0)
402 return err;
404 snd_card_set_dev(line6->card, line6->ifcdev);
406 err = snd_line6_new_midi(line6midi);
407 if (err < 0)
408 return err;
410 err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_transmit);
411 if (err < 0)
412 return err;
414 err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_receive);
415 if (err < 0)
416 return err;
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;