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.
14 #include <linux/usb.h>
16 #include <sound/core.h>
17 #include <sound/rawmidi.h>
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,
38 Pass data received via USB to MIDI.
40 void line6_midi_receive(struct usb_line6 *line6, unsigned char *data,
43 if (line6->line6midi->substream_receive)
44 snd_rawmidi_receive(line6->line6midi->substream_receive,
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;
57 unsigned char chunk[line6->max_packet_size];
60 spin_lock_irqsave(&line6->line6midi->midi_transmit_lock, flags);
63 req = min(midibuf_bytes_free(mb), line6->max_packet_size);
64 done = snd_rawmidi_transmit_peek(substream, chunk, req);
70 line6_write_hexdump(line6, 's', chunk, done);
72 midibuf_write(mb, chunk, done);
73 snd_rawmidi_transmit_ack(substream, done);
77 done = midibuf_read(mb, chunk, line6->max_packet_size);
82 if (midibuf_skip_message(mb, line6midi->midi_mask_transmit))
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)
99 struct usb_line6 *line6 = (struct usb_line6 *)urb->context;
101 status = urb->status;
102 kfree(urb->transfer_buffer);
105 if (status == -ESHUTDOWN)
108 spin_lock_irqsave(&line6->line6midi->send_urb_lock, flags);
109 num = --line6->line6midi->num_active_send_urbs;
112 line6_midi_transmit(line6->line6midi->substream_transmit);
113 num = line6->line6midi->num_active_send_urbs;
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,
132 unsigned char *transfer_buffer;
134 urb = usb_alloc_urb(0, GFP_ATOMIC);
137 dev_err(line6->ifcdev, "Out of memory\n");
142 line6_write_hexdump(line6, 'S', data, length);
145 transfer_buffer = kmalloc(length, GFP_ATOMIC);
147 if (transfer_buffer == 0) {
149 dev_err(line6->ifcdev, "Out of memory\n");
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,
159 urb->actual_length = 0;
160 retval = usb_submit_urb(urb, GFP_ATOMIC);
163 dev_err(line6->ifcdev, "usb_submit_urb failed\n");
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,
189 static int line6_midi_output_open(struct snd_rawmidi_substream *substream)
194 static int line6_midi_output_close(struct snd_rawmidi_substream *substream)
199 static void line6_midi_output_trigger(struct snd_rawmidi_substream *substream,
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))
228 current->state = TASK_RUNNING;
229 remove_wait_queue(head, &wait);
232 static int line6_midi_input_open(struct snd_rawmidi_substream *substream)
237 static int line6_midi_input_close(struct snd_rawmidi_substream *substream)
242 static void line6_midi_input_trigger(struct snd_rawmidi_substream *substream,
245 struct usb_line6 *line6 = line6_rawmidi_substream_midi(substream)->line6;
248 line6->line6midi->substream_receive = substream;
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;
279 err = snd_rawmidi_new(line6midi->line6->card, "Line6 MIDI", 0, 1, 1,
284 rmidi->private_data = line6midi;
285 rmidi->private_free = line6_cleanup_midi;
286 strcpy(rmidi->name, line6midi->line6->properties->name);
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);
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,
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;
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,
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;
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);
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,
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)
386 err = midibuf_init(&line6midi->midibuf_in, MIDI_BUFFER_SIZE, 0);
390 err = midibuf_init(&line6midi->midibuf_out, MIDI_BUFFER_SIZE, 1);
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,
404 snd_card_set_dev(line6->card, line6->ifcdev);
406 err = snd_line6_new_midi(line6midi);
410 err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_transmit);
414 err = device_create_file(line6->ifcdev, &dev_attr_midi_mask_receive);
418 init_waitqueue_head(&line6midi->send_wait);
419 spin_lock_init(&line6midi->send_urb_lock);
420 spin_lock_init(&line6midi->midi_transmit_lock);