Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006,2007 Daniel Mack |
| 3 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 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 |
| 17 | */ |
| 18 | |
| 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> |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 26 | #include <sound/core.h> |
| 27 | #include <sound/rawmidi.h> |
| 28 | #include <sound/pcm.h> |
| 29 | |
| 30 | #include "caiaq-device.h" |
| 31 | #include "caiaq-midi.h" |
| 32 | |
| 33 | |
| 34 | static int snd_usb_caiaq_midi_input_open(struct snd_rawmidi_substream *substream) |
| 35 | { |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | static int snd_usb_caiaq_midi_input_close(struct snd_rawmidi_substream *substream) |
| 40 | { |
| 41 | return 0; |
| 42 | } |
| 43 | |
| 44 | static void snd_usb_caiaq_midi_input_trigger(struct snd_rawmidi_substream *substream, int up) |
| 45 | { |
| 46 | struct snd_usb_caiaqdev *dev = substream->rmidi->private_data; |
| 47 | |
| 48 | if (!dev) |
| 49 | return; |
| 50 | |
| 51 | dev->midi_receive_substream = up ? substream : NULL; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | static int snd_usb_caiaq_midi_output_open(struct snd_rawmidi_substream *substream) |
| 56 | { |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int snd_usb_caiaq_midi_output_close(struct snd_rawmidi_substream *substream) |
| 61 | { |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame^] | 62 | struct snd_usb_caiaqdev *dev = substream->rmidi->private_data; |
| 63 | if (dev->midi_out_active) { |
| 64 | usb_kill_urb(&dev->midi_out_urb); |
| 65 | dev->midi_out_active = 0; |
| 66 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static void snd_usb_caiaq_midi_send(struct snd_usb_caiaqdev *dev, |
| 71 | struct snd_rawmidi_substream *substream) |
| 72 | { |
| 73 | int len, ret; |
| 74 | |
| 75 | dev->midi_out_buf[0] = EP1_CMD_MIDI_WRITE; |
| 76 | dev->midi_out_buf[1] = 0; /* port */ |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame^] | 77 | len = snd_rawmidi_transmit(substream, dev->midi_out_buf + 3, |
| 78 | EP1_BUFSIZE - 3); |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 79 | |
| 80 | if (len <= 0) |
| 81 | return; |
| 82 | |
| 83 | dev->midi_out_buf[2] = len; |
| 84 | dev->midi_out_urb.transfer_buffer_length = len+3; |
| 85 | |
| 86 | ret = usb_submit_urb(&dev->midi_out_urb, GFP_ATOMIC); |
| 87 | if (ret < 0) |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame^] | 88 | log("snd_usb_caiaq_midi_send(%p): usb_submit_urb() failed," |
| 89 | "ret=%d, len=%d\n", |
| 90 | substream, ret, len); |
| 91 | else |
| 92 | dev->midi_out_active = 1; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static void snd_usb_caiaq_midi_output_trigger(struct snd_rawmidi_substream *substream, int up) |
| 96 | { |
| 97 | struct snd_usb_caiaqdev *dev = substream->rmidi->private_data; |
| 98 | |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame^] | 99 | if (up) { |
| 100 | dev->midi_out_substream = substream; |
| 101 | if (!dev->midi_out_active) |
| 102 | snd_usb_caiaq_midi_send(dev, substream); |
| 103 | } else { |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 104 | dev->midi_out_substream = NULL; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 105 | } |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
| 109 | static struct snd_rawmidi_ops snd_usb_caiaq_midi_output = |
| 110 | { |
| 111 | .open = snd_usb_caiaq_midi_output_open, |
| 112 | .close = snd_usb_caiaq_midi_output_close, |
| 113 | .trigger = snd_usb_caiaq_midi_output_trigger, |
| 114 | }; |
| 115 | |
| 116 | static struct snd_rawmidi_ops snd_usb_caiaq_midi_input = |
| 117 | { |
| 118 | .open = snd_usb_caiaq_midi_input_open, |
| 119 | .close = snd_usb_caiaq_midi_input_close, |
| 120 | .trigger = snd_usb_caiaq_midi_input_trigger, |
| 121 | }; |
| 122 | |
| 123 | void snd_usb_caiaq_midi_handle_input(struct snd_usb_caiaqdev *dev, |
| 124 | int port, const char *buf, int len) |
| 125 | { |
| 126 | if (!dev->midi_receive_substream) |
| 127 | return; |
| 128 | |
| 129 | snd_rawmidi_receive(dev->midi_receive_substream, buf, len); |
| 130 | } |
| 131 | |
Randy Dunlap | 599c3e7 | 2008-01-16 14:56:04 +0100 | [diff] [blame] | 132 | int snd_usb_caiaq_midi_init(struct snd_usb_caiaqdev *device) |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 133 | { |
| 134 | int ret; |
| 135 | struct snd_rawmidi *rmidi; |
| 136 | |
| 137 | ret = snd_rawmidi_new(device->chip.card, device->product_name, 0, |
| 138 | device->spec.num_midi_out, |
| 139 | device->spec.num_midi_in, |
| 140 | &rmidi); |
| 141 | |
| 142 | if (ret < 0) |
| 143 | return ret; |
| 144 | |
| 145 | strcpy(rmidi->name, device->product_name); |
| 146 | |
| 147 | rmidi->info_flags = SNDRV_RAWMIDI_INFO_DUPLEX; |
| 148 | rmidi->private_data = device; |
| 149 | |
| 150 | if (device->spec.num_midi_out > 0) { |
| 151 | rmidi->info_flags |= SNDRV_RAWMIDI_INFO_OUTPUT; |
| 152 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, |
| 153 | &snd_usb_caiaq_midi_output); |
| 154 | } |
| 155 | |
| 156 | if (device->spec.num_midi_in > 0) { |
| 157 | rmidi->info_flags |= SNDRV_RAWMIDI_INFO_INPUT; |
| 158 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, |
| 159 | &snd_usb_caiaq_midi_input); |
| 160 | } |
| 161 | |
| 162 | device->rmidi = rmidi; |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | void snd_usb_caiaq_midi_output_done(struct urb* urb) |
| 168 | { |
| 169 | struct snd_usb_caiaqdev *dev = urb->context; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 170 | |
Takashi Iwai | f3f80a9 | 2009-01-08 15:32:56 +0100 | [diff] [blame^] | 171 | dev->midi_out_active = 0; |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 172 | if (urb->status != 0) |
| 173 | return; |
| 174 | |
| 175 | if (!dev->midi_out_substream) |
| 176 | return; |
| 177 | |
Daniel Mack | 523f1dc | 2007-03-26 19:11:24 +0200 | [diff] [blame] | 178 | snd_usb_caiaq_midi_send(dev, dev->midi_out_substream); |
| 179 | } |
| 180 | |