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>
15 #include <linux/slab.h>
17 #include <sound/core.h>
18 #include <sound/rawmidi.h>
27 #define OUTPUT_DUMP_ONLY 0
30 #define line6_rawmidi_substream_midi(substream) \
31 ((struct snd_line6_midi *)((substream)->rmidi->private_data))
34 static int send_midi_async(struct usb_line6
*line6
, unsigned char *data
,
39 Pass data received via USB to MIDI.
41 void line6_midi_receive(struct usb_line6
*line6
, unsigned char *data
,
44 if (line6
->line6midi
->substream_receive
)
45 snd_rawmidi_receive(line6
->line6midi
->substream_receive
,
50 Read data from MIDI buffer and transmit them via USB.
52 static void line6_midi_transmit(struct snd_rawmidi_substream
*substream
)
54 struct usb_line6
*line6
= line6_rawmidi_substream_midi(substream
)->line6
;
55 struct snd_line6_midi
*line6midi
= line6
->line6midi
;
56 struct MidiBuffer
*mb
= &line6midi
->midibuf_out
;
58 unsigned char chunk
[line6
->max_packet_size
];
61 spin_lock_irqsave(&line6
->line6midi
->midi_transmit_lock
, flags
);
64 req
= min(midibuf_bytes_free(mb
), line6
->max_packet_size
);
65 done
= snd_rawmidi_transmit_peek(substream
, chunk
, req
);
71 line6_write_hexdump(line6
, 's', chunk
, done
);
73 midibuf_write(mb
, chunk
, done
);
74 snd_rawmidi_transmit_ack(substream
, done
);
78 done
= midibuf_read(mb
, chunk
, line6
->max_packet_size
);
83 if (midibuf_skip_message(mb
, line6midi
->midi_mask_transmit
))
86 send_midi_async(line6
, chunk
, done
);
89 spin_unlock_irqrestore(&line6
->line6midi
->midi_transmit_lock
, flags
);
93 Notification of completion of MIDI transmission.
95 static void midi_sent(struct urb
*urb
)
100 struct usb_line6
*line6
= (struct usb_line6
*)urb
->context
;
102 status
= urb
->status
;
103 kfree(urb
->transfer_buffer
);
106 if (status
== -ESHUTDOWN
)
109 spin_lock_irqsave(&line6
->line6midi
->send_urb_lock
, flags
);
110 num
= --line6
->line6midi
->num_active_send_urbs
;
113 line6_midi_transmit(line6
->line6midi
->substream_transmit
);
114 num
= line6
->line6midi
->num_active_send_urbs
;
118 wake_up_interruptible(&line6
->line6midi
->send_wait
);
120 spin_unlock_irqrestore(&line6
->line6midi
->send_urb_lock
, flags
);
124 Send an asynchronous MIDI message.
125 Assumes that line6->line6midi->send_urb_lock is held
126 (i.e., this function is serialized).
128 static int send_midi_async(struct usb_line6
*line6
, unsigned char *data
,
133 unsigned char *transfer_buffer
;
135 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
138 dev_err(line6
->ifcdev
, "Out of memory\n");
143 line6_write_hexdump(line6
, 'S', data
, length
);
146 transfer_buffer
= kmalloc(length
, GFP_ATOMIC
);
148 if (transfer_buffer
== 0) {
150 dev_err(line6
->ifcdev
, "Out of memory\n");
154 memcpy(transfer_buffer
, data
, length
);
155 usb_fill_int_urb(urb
, line6
->usbdev
,
156 usb_sndbulkpipe(line6
->usbdev
,
157 line6
->ep_control_write
),
158 transfer_buffer
, length
, midi_sent
, line6
,
160 urb
->actual_length
= 0;
161 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
164 dev_err(line6
->ifcdev
, "usb_submit_urb failed\n");
169 ++line6
->line6midi
->num_active_send_urbs
;
171 switch (line6
->usbdev
->descriptor
.idProduct
) {
172 case LINE6_DEVID_BASSPODXT
:
173 case LINE6_DEVID_BASSPODXTLIVE
:
174 case LINE6_DEVID_BASSPODXTPRO
:
175 case LINE6_DEVID_PODXT
:
176 case LINE6_DEVID_PODXTLIVE
:
177 case LINE6_DEVID_PODXTPRO
:
178 case LINE6_DEVID_POCKETPOD
:
179 pod_midi_postprocess((struct usb_line6_pod
*)line6
, data
,
190 static int line6_midi_output_open(struct snd_rawmidi_substream
*substream
)
195 static int line6_midi_output_close(struct snd_rawmidi_substream
*substream
)
200 static void line6_midi_output_trigger(struct snd_rawmidi_substream
*substream
,
204 struct usb_line6
*line6
= line6_rawmidi_substream_midi(substream
)->line6
;
206 line6
->line6midi
->substream_transmit
= substream
;
207 spin_lock_irqsave(&line6
->line6midi
->send_urb_lock
, flags
);
209 if (line6
->line6midi
->num_active_send_urbs
== 0)
210 line6_midi_transmit(substream
);
212 spin_unlock_irqrestore(&line6
->line6midi
->send_urb_lock
, flags
);
215 static void line6_midi_output_drain(struct snd_rawmidi_substream
*substream
)
217 struct usb_line6
*line6
= line6_rawmidi_substream_midi(substream
)->line6
;
218 wait_queue_head_t
*head
= &line6
->line6midi
->send_wait
;
219 DECLARE_WAITQUEUE(wait
, current
);
220 add_wait_queue(head
, &wait
);
221 current
->state
= TASK_INTERRUPTIBLE
;
223 while (line6
->line6midi
->num_active_send_urbs
> 0)
224 if (signal_pending(current
))
229 current
->state
= TASK_RUNNING
;
230 remove_wait_queue(head
, &wait
);
233 static int line6_midi_input_open(struct snd_rawmidi_substream
*substream
)
238 static int line6_midi_input_close(struct snd_rawmidi_substream
*substream
)
243 static void line6_midi_input_trigger(struct snd_rawmidi_substream
*substream
,
246 struct usb_line6
*line6
= line6_rawmidi_substream_midi(substream
)->line6
;
249 line6
->line6midi
->substream_receive
= substream
;
251 line6
->line6midi
->substream_receive
= 0;
254 static struct snd_rawmidi_ops line6_midi_output_ops
= {
255 .open
= line6_midi_output_open
,
256 .close
= line6_midi_output_close
,
257 .trigger
= line6_midi_output_trigger
,
258 .drain
= line6_midi_output_drain
,
261 static struct snd_rawmidi_ops line6_midi_input_ops
= {
262 .open
= line6_midi_input_open
,
263 .close
= line6_midi_input_close
,
264 .trigger
= line6_midi_input_trigger
,
268 Cleanup the Line6 MIDI device.
270 static void line6_cleanup_midi(struct snd_rawmidi
*rmidi
)
274 /* Create a MIDI device */
275 static int snd_line6_new_midi(struct snd_line6_midi
*line6midi
)
277 struct snd_rawmidi
*rmidi
;
280 err
= snd_rawmidi_new(line6midi
->line6
->card
, "Line6 MIDI", 0, 1, 1,
285 rmidi
->private_data
= line6midi
;
286 rmidi
->private_free
= line6_cleanup_midi
;
287 strcpy(rmidi
->name
, line6midi
->line6
->properties
->name
);
290 SNDRV_RAWMIDI_INFO_OUTPUT
|
291 SNDRV_RAWMIDI_INFO_INPUT
|
292 SNDRV_RAWMIDI_INFO_DUPLEX
;
294 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
295 &line6_midi_output_ops
);
296 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
297 &line6_midi_input_ops
);
302 "read" request on "midi_mask_transmit" special file.
304 static ssize_t
midi_get_midi_mask_transmit(struct device
*dev
,
305 struct device_attribute
*attr
,
308 struct usb_interface
*interface
= to_usb_interface(dev
);
309 struct usb_line6
*line6
= usb_get_intfdata(interface
);
310 return sprintf(buf
, "%d\n", line6
->line6midi
->midi_mask_transmit
);
314 "write" request on "midi_mask" special file.
316 static ssize_t
midi_set_midi_mask_transmit(struct device
*dev
,
317 struct device_attribute
*attr
,
318 const char *buf
, size_t count
)
320 struct usb_interface
*interface
= to_usb_interface(dev
);
321 struct usb_line6
*line6
= usb_get_intfdata(interface
);
325 ret
= strict_strtoul(buf
, 10, &value
);
329 line6
->line6midi
->midi_mask_transmit
= value
;
334 "read" request on "midi_mask_receive" special file.
336 static ssize_t
midi_get_midi_mask_receive(struct device
*dev
,
337 struct device_attribute
*attr
,
340 struct usb_interface
*interface
= to_usb_interface(dev
);
341 struct usb_line6
*line6
= usb_get_intfdata(interface
);
342 return sprintf(buf
, "%d\n", line6
->line6midi
->midi_mask_receive
);
346 "write" request on "midi_mask" special file.
348 static ssize_t
midi_set_midi_mask_receive(struct device
*dev
,
349 struct device_attribute
*attr
,
350 const char *buf
, size_t count
)
352 struct usb_interface
*interface
= to_usb_interface(dev
);
353 struct usb_line6
*line6
= usb_get_intfdata(interface
);
357 ret
= strict_strtoul(buf
, 10, &value
);
361 line6
->line6midi
->midi_mask_receive
= value
;
365 static DEVICE_ATTR(midi_mask_transmit
, S_IWUGO
| S_IRUGO
, midi_get_midi_mask_transmit
, midi_set_midi_mask_transmit
);
366 static DEVICE_ATTR(midi_mask_receive
, S_IWUGO
| S_IRUGO
, midi_get_midi_mask_receive
, midi_set_midi_mask_receive
);
368 /* MIDI device destructor */
369 static int snd_line6_midi_free(struct snd_device
*device
)
371 struct snd_line6_midi
*line6midi
= device
->device_data
;
372 device_remove_file(line6midi
->line6
->ifcdev
, &dev_attr_midi_mask_transmit
);
373 device_remove_file(line6midi
->line6
->ifcdev
, &dev_attr_midi_mask_receive
);
374 midibuf_destroy(&line6midi
->midibuf_in
);
375 midibuf_destroy(&line6midi
->midibuf_out
);
380 Initialize the Line6 MIDI subsystem.
382 int line6_init_midi(struct usb_line6
*line6
)
384 static struct snd_device_ops midi_ops
= {
385 .dev_free
= snd_line6_midi_free
,
389 struct snd_line6_midi
*line6midi
;
391 if (!(line6
->properties
->capabilities
& LINE6_BIT_CONTROL
))
392 return 0; /* skip MIDI initialization and report success */
394 line6midi
= kzalloc(sizeof(struct snd_line6_midi
), GFP_KERNEL
);
396 if (line6midi
== NULL
)
399 err
= midibuf_init(&line6midi
->midibuf_in
, MIDI_BUFFER_SIZE
, 0);
403 err
= midibuf_init(&line6midi
->midibuf_out
, MIDI_BUFFER_SIZE
, 1);
407 line6midi
->line6
= line6
;
408 line6midi
->midi_mask_transmit
= 1;
409 line6midi
->midi_mask_receive
= 4;
410 line6
->line6midi
= line6midi
;
412 err
= snd_device_new(line6
->card
, SNDRV_DEV_RAWMIDI
, line6midi
,
417 snd_card_set_dev(line6
->card
, line6
->ifcdev
);
419 err
= snd_line6_new_midi(line6midi
);
423 err
= device_create_file(line6
->ifcdev
, &dev_attr_midi_mask_transmit
);
427 err
= device_create_file(line6
->ifcdev
, &dev_attr_midi_mask_receive
);
431 init_waitqueue_head(&line6midi
->send_wait
);
432 spin_lock_init(&line6midi
->send_urb_lock
);
433 spin_lock_init(&line6midi
->midi_transmit_lock
);