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