2 * Copyright (c) 2006,2007 Daniel Mack
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include <linux/device.h>
20 #include <linux/usb.h>
21 #include <linux/gfp.h>
22 #include <sound/rawmidi.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
29 static int snd_usb_caiaq_midi_input_open(struct snd_rawmidi_substream
*substream
)
34 static int snd_usb_caiaq_midi_input_close(struct snd_rawmidi_substream
*substream
)
39 static void snd_usb_caiaq_midi_input_trigger(struct snd_rawmidi_substream
*substream
, int up
)
41 struct snd_usb_caiaqdev
*cdev
= substream
->rmidi
->private_data
;
46 cdev
->midi_receive_substream
= up
? substream
: NULL
;
50 static int snd_usb_caiaq_midi_output_open(struct snd_rawmidi_substream
*substream
)
55 static int snd_usb_caiaq_midi_output_close(struct snd_rawmidi_substream
*substream
)
57 struct snd_usb_caiaqdev
*cdev
= substream
->rmidi
->private_data
;
58 if (cdev
->midi_out_active
) {
59 usb_kill_urb(&cdev
->midi_out_urb
);
60 cdev
->midi_out_active
= 0;
65 static void snd_usb_caiaq_midi_send(struct snd_usb_caiaqdev
*cdev
,
66 struct snd_rawmidi_substream
*substream
)
69 struct device
*dev
= caiaqdev_to_dev(cdev
);
71 cdev
->midi_out_buf
[0] = EP1_CMD_MIDI_WRITE
;
72 cdev
->midi_out_buf
[1] = 0; /* port */
73 len
= snd_rawmidi_transmit(substream
, cdev
->midi_out_buf
+ 3,
79 cdev
->midi_out_buf
[2] = len
;
80 cdev
->midi_out_urb
.transfer_buffer_length
= len
+3;
82 ret
= usb_submit_urb(&cdev
->midi_out_urb
, GFP_ATOMIC
);
85 "snd_usb_caiaq_midi_send(%p): usb_submit_urb() failed,"
86 "ret=%d, len=%d\n", substream
, ret
, len
);
88 cdev
->midi_out_active
= 1;
91 static void snd_usb_caiaq_midi_output_trigger(struct snd_rawmidi_substream
*substream
, int up
)
93 struct snd_usb_caiaqdev
*cdev
= substream
->rmidi
->private_data
;
96 cdev
->midi_out_substream
= substream
;
97 if (!cdev
->midi_out_active
)
98 snd_usb_caiaq_midi_send(cdev
, substream
);
100 cdev
->midi_out_substream
= NULL
;
105 static struct snd_rawmidi_ops snd_usb_caiaq_midi_output
=
107 .open
= snd_usb_caiaq_midi_output_open
,
108 .close
= snd_usb_caiaq_midi_output_close
,
109 .trigger
= snd_usb_caiaq_midi_output_trigger
,
112 static struct snd_rawmidi_ops snd_usb_caiaq_midi_input
=
114 .open
= snd_usb_caiaq_midi_input_open
,
115 .close
= snd_usb_caiaq_midi_input_close
,
116 .trigger
= snd_usb_caiaq_midi_input_trigger
,
119 void snd_usb_caiaq_midi_handle_input(struct snd_usb_caiaqdev
*cdev
,
120 int port
, const char *buf
, int len
)
122 if (!cdev
->midi_receive_substream
)
125 snd_rawmidi_receive(cdev
->midi_receive_substream
, buf
, len
);
128 int snd_usb_caiaq_midi_init(struct snd_usb_caiaqdev
*device
)
131 struct snd_rawmidi
*rmidi
;
133 ret
= snd_rawmidi_new(device
->chip
.card
, device
->product_name
, 0,
134 device
->spec
.num_midi_out
,
135 device
->spec
.num_midi_in
,
141 strlcpy(rmidi
->name
, device
->product_name
, sizeof(rmidi
->name
));
143 rmidi
->info_flags
= SNDRV_RAWMIDI_INFO_DUPLEX
;
144 rmidi
->private_data
= device
;
146 if (device
->spec
.num_midi_out
> 0) {
147 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_OUTPUT
;
148 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_OUTPUT
,
149 &snd_usb_caiaq_midi_output
);
152 if (device
->spec
.num_midi_in
> 0) {
153 rmidi
->info_flags
|= SNDRV_RAWMIDI_INFO_INPUT
;
154 snd_rawmidi_set_ops(rmidi
, SNDRV_RAWMIDI_STREAM_INPUT
,
155 &snd_usb_caiaq_midi_input
);
158 device
->rmidi
= rmidi
;
163 void snd_usb_caiaq_midi_output_done(struct urb
* urb
)
165 struct snd_usb_caiaqdev
*cdev
= urb
->context
;
167 cdev
->midi_out_active
= 0;
168 if (urb
->status
!= 0)
171 if (!cdev
->midi_out_substream
)
174 snd_usb_caiaq_midi_send(cdev
, cdev
->midi_out_substream
);