Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / sound / usb / caiaq / caiaq-midi.c
blob30b57f97c6e411221517f9a8c3d0a5e1a42036b0
1 /*
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/init.h>
20 #include <linux/module.h>
21 #include <linux/moduleparam.h>
22 #include <linux/interrupt.h>
23 #include <linux/usb.h>
24 #include <linux/input.h>
25 #include <linux/spinlock.h>
26 #include <sound/core.h>
27 #include <sound/rawmidi.h>
28 #include <sound/pcm.h>
30 #include "caiaq-device.h"
31 #include "caiaq-midi.h"
34 static int snd_usb_caiaq_midi_input_open(struct snd_rawmidi_substream *substream)
36 return 0;
39 static int snd_usb_caiaq_midi_input_close(struct snd_rawmidi_substream *substream)
41 return 0;
44 static void snd_usb_caiaq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up)
46 struct snd_usb_caiaqdev *dev = substream->rmidi->private_data;
48 if (!dev)
49 return;
51 dev->midi_receive_substream = up ? substream : NULL;
55 static int snd_usb_caiaq_midi_output_open(struct snd_rawmidi_substream *substream)
57 return 0;
60 static int snd_usb_caiaq_midi_output_close(struct snd_rawmidi_substream *substream)
62 return 0;
65 static void snd_usb_caiaq_midi_send(struct snd_usb_caiaqdev *dev,
66 struct snd_rawmidi_substream *substream)
68 int len, ret;
70 dev->midi_out_buf[0] = EP1_CMD_MIDI_WRITE;
71 dev->midi_out_buf[1] = 0; /* port */
72 len = snd_rawmidi_transmit_peek(substream, dev->midi_out_buf+3, EP1_BUFSIZE-3);
74 if (len <= 0)
75 return;
77 dev->midi_out_buf[2] = len;
78 dev->midi_out_urb.transfer_buffer_length = len+3;
80 ret = usb_submit_urb(&dev->midi_out_urb, GFP_ATOMIC);
81 if (ret < 0)
82 log("snd_usb_caiaq_midi_send(%p): usb_submit_urb() failed, %d\n",
83 substream, ret);
86 static void snd_usb_caiaq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up)
88 struct snd_usb_caiaqdev *dev = substream->rmidi->private_data;
90 if (dev->midi_out_substream != NULL)
91 return;
93 if (!up) {
94 dev->midi_out_substream = NULL;
95 return;
98 dev->midi_out_substream = substream;
99 snd_usb_caiaq_midi_send(dev, substream);
103 static struct snd_rawmidi_ops snd_usb_caiaq_midi_output =
105 .open = snd_usb_caiaq_midi_output_open,
106 .close = snd_usb_caiaq_midi_output_close,
107 .trigger = snd_usb_caiaq_midi_output_trigger,
110 static struct snd_rawmidi_ops snd_usb_caiaq_midi_input =
112 .open = snd_usb_caiaq_midi_input_open,
113 .close = snd_usb_caiaq_midi_input_close,
114 .trigger = snd_usb_caiaq_midi_input_trigger,
117 void snd_usb_caiaq_midi_handle_input(struct snd_usb_caiaqdev *dev,
118 int port, const char *buf, int len)
120 if (!dev->midi_receive_substream)
121 return;
123 snd_rawmidi_receive(dev->midi_receive_substream, buf, len);
126 int snd_usb_caiaq_midi_init(struct snd_usb_caiaqdev *device)
128 int ret;
129 struct snd_rawmidi *rmidi;
131 ret = snd_rawmidi_new(device->chip.card, device->product_name, 0,
132 device->spec.num_midi_out,
133 device->spec.num_midi_in,
134 &rmidi);
136 if (ret < 0)
137 return ret;
139 strcpy(rmidi->name, device->product_name);
141 rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX;
142 rmidi->private_data = device;
144 if (device->spec.num_midi_out > 0) {
145 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT;
146 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
147 &snd_usb_caiaq_midi_output);
150 if (device->spec.num_midi_in > 0) {
151 rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT;
152 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
153 &snd_usb_caiaq_midi_input);
156 device->rmidi = rmidi;
158 return 0;
161 void snd_usb_caiaq_midi_output_done(struct urb* urb)
163 struct snd_usb_caiaqdev *dev = urb->context;
164 char *buf = urb->transfer_buffer;
166 if (urb->status != 0)
167 return;
169 if (!dev->midi_out_substream)
170 return;
172 snd_rawmidi_transmit_ack(dev->midi_out_substream, buf[2]);
173 dev->midi_out_substream = NULL;
174 snd_usb_caiaq_midi_send(dev, dev->midi_out_substream);