2 * Line6 Linux USB driver - 0.9.1beta
4 * Copyright (C) 2004-2010 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 <linux/slab.h>
13 #include <linux/usb.h>
14 #include <sound/core.h>
15 #include <sound/rawmidi.h>
23 #define line6_rawmidi_substream_midi(substream) \
24 ((struct snd_line6_midi *)((substream)->rmidi->private_data))
26 static int send_midi_async(struct usb_line6
*line6
, unsigned char *data
,
30 Pass data received via USB to MIDI.
32 void line6_midi_receive(struct usb_line6
*line6
, unsigned char *data
,
35 if (line6
->line6midi
->substream_receive
)
36 snd_rawmidi_receive(line6
->line6midi
->substream_receive
,
41 Read data from MIDI buffer and transmit them via USB.
43 static void line6_midi_transmit(struct snd_rawmidi_substream
*substream
)
45 struct usb_line6
*line6
=
46 line6_rawmidi_substream_midi(substream
)->line6
;
47 struct snd_line6_midi
*line6midi
= line6
->line6midi
;
48 struct MidiBuffer
*mb
= &line6midi
->midibuf_out
;
50 unsigned char chunk
[line6
->max_packet_size
];
53 spin_lock_irqsave(&line6
->line6midi
->midi_transmit_lock
, flags
);
56 req
= min(line6_midibuf_bytes_free(mb
), line6
->max_packet_size
);
57 done
= snd_rawmidi_transmit_peek(substream
, chunk
, req
);
62 #ifdef CONFIG_LINE6_USB_DUMP_MIDI
63 line6_write_hexdump(line6
, 's', chunk
, done
);
65 line6_midibuf_write(mb
, chunk
, done
);
66 snd_rawmidi_transmit_ack(substream
, done
);
70 done
= line6_midibuf_read(mb
, chunk
, line6
->max_packet_size
);
75 if (line6_midibuf_skip_message
76 (mb
, line6midi
->midi_mask_transmit
))
79 send_midi_async(line6
, chunk
, done
);
82 spin_unlock_irqrestore(&line6
->line6midi
->midi_transmit_lock
, flags
);
86 Notification of completion of MIDI transmission.
88 static void midi_sent(struct urb
*urb
)
93 struct usb_line6
*line6
= (struct usb_line6
*)urb
->context
;
96 kfree(urb
->transfer_buffer
);
99 if (status
== -ESHUTDOWN
)
102 spin_lock_irqsave(&line6
->line6midi
->send_urb_lock
, flags
);
103 num
= --line6
->line6midi
->num_active_send_urbs
;
106 line6_midi_transmit(line6
->line6midi
->substream_transmit
);
107 num
= line6
->line6midi
->num_active_send_urbs
;
111 wake_up(&line6
->line6midi
->send_wait
);
113 spin_unlock_irqrestore(&line6
->line6midi
->send_urb_lock
, flags
);
117 Send an asynchronous MIDI message.
118 Assumes that line6->line6midi->send_urb_lock is held
119 (i.e., this function is serialized).
121 static int send_midi_async(struct usb_line6
*line6
, unsigned char *data
,
126 unsigned char *transfer_buffer
;
128 urb
= usb_alloc_urb(0, GFP_ATOMIC
);
131 dev_err(line6
->ifcdev
, "Out of memory\n");
134 #ifdef CONFIG_LINE6_USB_DUMP_CTRL
135 line6_write_hexdump(line6
, 'S', data
, length
);
138 transfer_buffer
= kmalloc(length
, GFP_ATOMIC
);
140 if (transfer_buffer
== NULL
) {
142 dev_err(line6
->ifcdev
, "Out of memory\n");
146 memcpy(transfer_buffer
, data
, length
);
147 usb_fill_int_urb(urb
, line6
->usbdev
,
148 usb_sndbulkpipe(line6
->usbdev
,
149 line6
->ep_control_write
),
150 transfer_buffer
, length
, midi_sent
, line6
,
152 urb
->actual_length
= 0;
153 retval
= usb_submit_urb(urb
, GFP_ATOMIC
);
156 dev_err(line6
->ifcdev
, "usb_submit_urb failed\n");
161 ++line6
->line6midi
->num_active_send_urbs
;
163 switch (line6
->usbdev
->descriptor
.idProduct
) {
164 case LINE6_DEVID_BASSPODXT
:
165 case LINE6_DEVID_BASSPODXTLIVE
:
166 case LINE6_DEVID_BASSPODXTPRO
:
167 case LINE6_DEVID_PODXT
:
168 case LINE6_DEVID_PODXTLIVE
:
169 case LINE6_DEVID_PODXTPRO
:
170 case LINE6_DEVID_POCKETPOD
:
171 line6_pod_midi_postprocess((struct usb_line6_pod
*)line6
, data
,
175 case LINE6_DEVID_VARIAX
:
185 static int line6_midi_output_open(struct snd_rawmidi_substream
*substream
)
190 static int line6_midi_output_close(struct snd_rawmidi_substream
*substream
)
195 static void line6_midi_output_trigger(struct snd_rawmidi_substream
*substream
,
199 struct usb_line6
*line6
=
200 line6_rawmidi_substream_midi(substream
)->line6
;
202 line6
->line6midi
->substream_transmit
= substream
;
203 spin_lock_irqsave(&line6
->line6midi
->send_urb_lock
, flags
);
205 if (line6
->line6midi
->num_active_send_urbs
== 0)
206 line6_midi_transmit(substream
);
208 spin_unlock_irqrestore(&line6
->line6midi
->send_urb_lock
, flags
);
211 static void line6_midi_output_drain(struct snd_rawmidi_substream
*substream
)
213 struct usb_line6
*line6
=
214 line6_rawmidi_substream_midi(substream
)->line6
;
215 struct snd_line6_midi
*midi
= line6
->line6midi
;
216 wait_event_interruptible(midi
->send_wait
,
217 midi
->num_active_send_urbs
== 0);
220 static int line6_midi_input_open(struct snd_rawmidi_substream
*substream
)
225 static int line6_midi_input_close(struct snd_rawmidi_substream
*substream
)
230 static void line6_midi_input_trigger(struct snd_rawmidi_substream
*substream
,
233 struct usb_line6
*line6
=
234 line6_rawmidi_substream_midi(substream
)->line6
;
237 line6
->line6midi
->substream_receive
= substream
;
239 line6
->line6midi
->substream_receive
= 0;
242 static struct snd_rawmidi_ops line6_midi_output_ops
= {
243 .open
= line6_midi_output_open
,
244 .close
= line6_midi_output_close
,
245 .trigger
= line6_midi_output_trigger
,
246 .drain
= line6_midi_output_drain
,
249 static struct snd_rawmidi_ops line6_midi_input_ops
= {
250 .open
= line6_midi_input_open
,
251 .close
= line6_midi_input_close
,
252 .trigger
= line6_midi_input_trigger
,
256 Cleanup the Line6 MIDI device.
258 static void line6_cleanup_midi(struct snd_rawmidi
*rmidi
)
262 /* Create a MIDI device */
263 static int snd_line6_new_midi(struct snd_line6_midi
*line6midi
)
265 struct snd_rawmidi
*rmidi
;
268 err
= snd_rawmidi_new(line6midi
->line6
->card
, "Line6 MIDI", 0, 1, 1,
273 rmidi
->private_data
= line6midi
;
274 rmidi
->private_free
= line6_cleanup_midi
;
275 strcpy(rmidi
->id
, line6midi
->line6
->properties
->id
);
276 strcpy(rmidi
->name
, line6midi
->line6
->properties
->name
);
279 SNDRV_RAWMIDI_INFO_OUTPUT
|
280 SNDRV_RAWMIDI_INFO_INPUT
| SNDRV_RAWMIDI_INFO_DUPLEX
;
282 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
283 &line6_midi_output_ops
);
284 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
285 &line6_midi_input_ops
);
290 "read" request on "midi_mask_transmit" special file.
292 static ssize_t
midi_get_midi_mask_transmit(struct device
*dev
,
293 struct device_attribute
*attr
,
296 struct usb_interface
*interface
= to_usb_interface(dev
);
297 struct usb_line6
*line6
= usb_get_intfdata(interface
);
298 return sprintf(buf
, "%d\n", line6
->line6midi
->midi_mask_transmit
);
302 "write" request on "midi_mask" special file.
304 static ssize_t
midi_set_midi_mask_transmit(struct device
*dev
,
305 struct device_attribute
*attr
,
306 const char *buf
, size_t count
)
308 struct usb_interface
*interface
= to_usb_interface(dev
);
309 struct usb_line6
*line6
= usb_get_intfdata(interface
);
313 ret
= strict_strtoul(buf
, 10, &value
);
317 line6
->line6midi
->midi_mask_transmit
= value
;
322 "read" request on "midi_mask_receive" special file.
324 static ssize_t
midi_get_midi_mask_receive(struct device
*dev
,
325 struct device_attribute
*attr
,
328 struct usb_interface
*interface
= to_usb_interface(dev
);
329 struct usb_line6
*line6
= usb_get_intfdata(interface
);
330 return sprintf(buf
, "%d\n", line6
->line6midi
->midi_mask_receive
);
334 "write" request on "midi_mask" special file.
336 static ssize_t
midi_set_midi_mask_receive(struct device
*dev
,
337 struct device_attribute
*attr
,
338 const char *buf
, size_t count
)
340 struct usb_interface
*interface
= to_usb_interface(dev
);
341 struct usb_line6
*line6
= usb_get_intfdata(interface
);
345 ret
= strict_strtoul(buf
, 10, &value
);
349 line6
->line6midi
->midi_mask_receive
= value
;
353 static DEVICE_ATTR(midi_mask_transmit
, S_IWUSR
| S_IRUGO
,
354 midi_get_midi_mask_transmit
, midi_set_midi_mask_transmit
);
355 static DEVICE_ATTR(midi_mask_receive
, S_IWUSR
| S_IRUGO
,
356 midi_get_midi_mask_receive
, midi_set_midi_mask_receive
);
358 /* MIDI device destructor */
359 static int snd_line6_midi_free(struct snd_device
*device
)
361 struct snd_line6_midi
*line6midi
= device
->device_data
;
362 device_remove_file(line6midi
->line6
->ifcdev
,
363 &dev_attr_midi_mask_transmit
);
364 device_remove_file(line6midi
->line6
->ifcdev
,
365 &dev_attr_midi_mask_receive
);
366 line6_midibuf_destroy(&line6midi
->midibuf_in
);
367 line6_midibuf_destroy(&line6midi
->midibuf_out
);
372 Initialize the Line6 MIDI subsystem.
374 int line6_init_midi(struct usb_line6
*line6
)
376 static struct snd_device_ops midi_ops
= {
377 .dev_free
= snd_line6_midi_free
,
381 struct snd_line6_midi
*line6midi
;
383 if (!(line6
->properties
->capabilities
& LINE6_BIT_CONTROL
)) {
384 /* skip MIDI initialization and report success */
388 line6midi
= kzalloc(sizeof(struct snd_line6_midi
), GFP_KERNEL
);
390 if (line6midi
== NULL
)
393 err
= line6_midibuf_init(&line6midi
->midibuf_in
, MIDI_BUFFER_SIZE
, 0);
397 err
= line6_midibuf_init(&line6midi
->midibuf_out
, MIDI_BUFFER_SIZE
, 1);
401 line6midi
->line6
= line6
;
402 line6midi
->midi_mask_transmit
= 1;
403 line6midi
->midi_mask_receive
= 4;
404 line6
->line6midi
= line6midi
;
406 err
= snd_device_new(line6
->card
, SNDRV_DEV_RAWMIDI
, line6midi
,
411 snd_card_set_dev(line6
->card
, line6
->ifcdev
);
413 err
= snd_line6_new_midi(line6midi
);
417 err
= device_create_file(line6
->ifcdev
, &dev_attr_midi_mask_transmit
);
421 err
= device_create_file(line6
->ifcdev
, &dev_attr_midi_mask_receive
);
425 init_waitqueue_head(&line6midi
->send_wait
);
426 spin_lock_init(&line6midi
->send_urb_lock
);
427 spin_lock_init(&line6midi
->midi_transmit_lock
);