Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * usbmidi.c - ALSA USB MIDI driver |
| 3 | * |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 4 | * Copyright (c) 2002-2009 Clemens Ladisch |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * All rights reserved. |
| 6 | * |
| 7 | * Based on the OSS usb-midi driver by NAGANO Daisuke, |
| 8 | * NetBSD's umidi driver by Takuya SHIOZAKI, |
| 9 | * the "USB Device Class Definition for MIDI Devices" by Roland |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions |
| 13 | * are met: |
| 14 | * 1. Redistributions of source code must retain the above copyright |
| 15 | * notice, this list of conditions, and the following disclaimer, |
| 16 | * without modification. |
| 17 | * 2. The name of the author may not be used to endorse or promote products |
| 18 | * derived from this software without specific prior written permission. |
| 19 | * |
| 20 | * Alternatively, this software may be distributed and/or modified under the |
| 21 | * terms of the GNU General Public License as published by the Free Software |
| 22 | * Foundation; either version 2 of the License, or (at your option) any later |
| 23 | * version. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR |
| 29 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 35 | * SUCH DAMAGE. |
| 36 | */ |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/kernel.h> |
| 39 | #include <linux/types.h> |
| 40 | #include <linux/bitops.h> |
| 41 | #include <linux/interrupt.h> |
| 42 | #include <linux/spinlock.h> |
| 43 | #include <linux/string.h> |
| 44 | #include <linux/init.h> |
| 45 | #include <linux/slab.h> |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 46 | #include <linux/timer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/usb.h> |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 48 | #include <linux/wait.h> |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 49 | #include <linux/usb/audio.h> |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <sound/core.h> |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 52 | #include <sound/control.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <sound/rawmidi.h> |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 54 | #include <sound/asequencer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include "usbaudio.h" |
Daniel Mack | e577999 | 2010-03-04 19:46:13 +0100 | [diff] [blame] | 56 | #include "midi.h" |
| 57 | #include "helper.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | /* |
| 60 | * define this to log all USB packets |
| 61 | */ |
| 62 | /* #define DUMP_PACKETS */ |
| 63 | |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 64 | /* |
| 65 | * how long to wait after some USB errors, so that khubd can disconnect() us |
| 66 | * without too many spurious errors |
| 67 | */ |
| 68 | #define ERROR_DELAY_JIFFIES (HZ / 10) |
| 69 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 70 | #define OUTPUT_URBS 7 |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 71 | #define INPUT_URBS 7 |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>"); |
| 75 | MODULE_DESCRIPTION("USB Audio/MIDI helper module"); |
| 76 | MODULE_LICENSE("Dual BSD/GPL"); |
| 77 | |
| 78 | |
| 79 | struct usb_ms_header_descriptor { |
| 80 | __u8 bLength; |
| 81 | __u8 bDescriptorType; |
| 82 | __u8 bDescriptorSubtype; |
| 83 | __u8 bcdMSC[2]; |
| 84 | __le16 wTotalLength; |
| 85 | } __attribute__ ((packed)); |
| 86 | |
| 87 | struct usb_ms_endpoint_descriptor { |
| 88 | __u8 bLength; |
| 89 | __u8 bDescriptorType; |
| 90 | __u8 bDescriptorSubtype; |
| 91 | __u8 bNumEmbMIDIJack; |
| 92 | __u8 baAssocJackID[0]; |
| 93 | } __attribute__ ((packed)); |
| 94 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 95 | struct snd_usb_midi_in_endpoint; |
| 96 | struct snd_usb_midi_out_endpoint; |
| 97 | struct snd_usb_midi_endpoint; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | struct usb_protocol_ops { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 100 | void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int); |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 101 | void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t); |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 103 | void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint*); |
| 104 | void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint*); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct snd_usb_midi { |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 108 | struct usb_device *dev; |
| 109 | struct snd_card *card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | struct usb_interface *iface; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 111 | const struct snd_usb_audio_quirk *quirk; |
| 112 | struct snd_rawmidi *rmidi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | struct usb_protocol_ops* usb_protocol_ops; |
| 114 | struct list_head list; |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 115 | struct timer_list error_timer; |
Takashi Iwai | c0792e0 | 2008-02-22 18:34:44 +0100 | [diff] [blame] | 116 | spinlock_t disc_lock; |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 117 | struct mutex mutex; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 118 | u32 usb_id; |
| 119 | int next_midi_device; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
| 121 | struct snd_usb_midi_endpoint { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 122 | struct snd_usb_midi_out_endpoint *out; |
| 123 | struct snd_usb_midi_in_endpoint *in; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } endpoints[MIDI_MAX_ENDPOINTS]; |
| 125 | unsigned long input_triggered; |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 126 | unsigned int opened; |
Takashi Iwai | c0792e0 | 2008-02-22 18:34:44 +0100 | [diff] [blame] | 127 | unsigned char disconnected; |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 128 | |
| 129 | struct snd_kcontrol *roland_load_ctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | struct snd_usb_midi_out_endpoint { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 133 | struct snd_usb_midi* umidi; |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 134 | struct out_urb_context { |
| 135 | struct urb *urb; |
| 136 | struct snd_usb_midi_out_endpoint *ep; |
| 137 | } urbs[OUTPUT_URBS]; |
| 138 | unsigned int active_urbs; |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 139 | unsigned int drain_urbs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | int max_transfer; /* size of urb buffer */ |
| 141 | struct tasklet_struct tasklet; |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 142 | unsigned int next_urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | spinlock_t buffer_lock; |
| 144 | |
| 145 | struct usbmidi_out_port { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 146 | struct snd_usb_midi_out_endpoint* ep; |
| 147 | struct snd_rawmidi_substream *substream; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | int active; |
| 149 | uint8_t cable; /* cable number << 4 */ |
| 150 | uint8_t state; |
| 151 | #define STATE_UNKNOWN 0 |
| 152 | #define STATE_1PARAM 1 |
| 153 | #define STATE_2PARAM_1 2 |
| 154 | #define STATE_2PARAM_2 3 |
| 155 | #define STATE_SYSEX_0 4 |
| 156 | #define STATE_SYSEX_1 5 |
| 157 | #define STATE_SYSEX_2 6 |
| 158 | uint8_t data[2]; |
| 159 | } ports[0x10]; |
| 160 | int current_port; |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 161 | |
| 162 | wait_queue_head_t drain_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | struct snd_usb_midi_in_endpoint { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 166 | struct snd_usb_midi* umidi; |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 167 | struct urb* urbs[INPUT_URBS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | struct usbmidi_in_port { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 169 | struct snd_rawmidi_substream *substream; |
Clemens Ladisch | d05cc1043 | 2007-05-07 09:28:53 +0200 | [diff] [blame] | 170 | u8 running_status_length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | } ports[0x10]; |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 172 | u8 seen_f5; |
| 173 | u8 error_resubmit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | int current_port; |
| 175 | }; |
| 176 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 177 | static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
| 179 | static const uint8_t snd_usbmidi_cin_length[] = { |
| 180 | 0, 0, 2, 3, 3, 1, 2, 3, 3, 3, 3, 3, 2, 2, 3, 1 |
| 181 | }; |
| 182 | |
| 183 | /* |
| 184 | * Submits the URB, with error handling. |
| 185 | */ |
Al Viro | 55016f1 | 2005-10-21 03:21:58 -0400 | [diff] [blame] | 186 | static int snd_usbmidi_submit_urb(struct urb* urb, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | { |
| 188 | int err = usb_submit_urb(urb, flags); |
| 189 | if (err < 0 && err != -ENODEV) |
| 190 | snd_printk(KERN_ERR "usb_submit_urb: %d\n", err); |
| 191 | return err; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Error handling for URB completion functions. |
| 196 | */ |
| 197 | static int snd_usbmidi_urb_error(int status) |
| 198 | { |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 199 | switch (status) { |
| 200 | /* manually unlinked, or device gone */ |
| 201 | case -ENOENT: |
| 202 | case -ECONNRESET: |
| 203 | case -ESHUTDOWN: |
| 204 | case -ENODEV: |
| 205 | return -ENODEV; |
| 206 | /* errors that might occur during unplugging */ |
Pete Zaitcev | 38e2bfc | 2006-09-18 22:49:02 -0700 | [diff] [blame] | 207 | case -EPROTO: |
| 208 | case -ETIME: |
| 209 | case -EILSEQ: |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 210 | return -EIO; |
| 211 | default: |
| 212 | snd_printk(KERN_ERR "urb status %d\n", status); |
| 213 | return 0; /* continue */ |
| 214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* |
| 218 | * Receives a chunk of MIDI data. |
| 219 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 220 | static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint* ep, int portidx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | uint8_t* data, int length) |
| 222 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 223 | struct usbmidi_in_port* port = &ep->ports[portidx]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | if (!port->substream) { |
| 226 | snd_printd("unexpected port %d!\n", portidx); |
| 227 | return; |
| 228 | } |
| 229 | if (!test_bit(port->substream->number, &ep->umidi->input_triggered)) |
| 230 | return; |
| 231 | snd_rawmidi_receive(port->substream, data, length); |
| 232 | } |
| 233 | |
| 234 | #ifdef DUMP_PACKETS |
| 235 | static void dump_urb(const char *type, const u8 *data, int length) |
| 236 | { |
| 237 | snd_printk(KERN_DEBUG "%s packet: [", type); |
| 238 | for (; length > 0; ++data, --length) |
| 239 | printk(" %02x", *data); |
| 240 | printk(" ]\n"); |
| 241 | } |
| 242 | #else |
| 243 | #define dump_urb(type, data, length) /* nothing */ |
| 244 | #endif |
| 245 | |
| 246 | /* |
| 247 | * Processes the data read from the device. |
| 248 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 249 | static void snd_usbmidi_in_urb_complete(struct urb* urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 251 | struct snd_usb_midi_in_endpoint* ep = urb->context; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | if (urb->status == 0) { |
| 254 | dump_urb("received", urb->transfer_buffer, urb->actual_length); |
| 255 | ep->umidi->usb_protocol_ops->input(ep, urb->transfer_buffer, |
| 256 | urb->actual_length); |
| 257 | } else { |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 258 | int err = snd_usbmidi_urb_error(urb->status); |
| 259 | if (err < 0) { |
| 260 | if (err != -ENODEV) { |
| 261 | ep->error_resubmit = 1; |
| 262 | mod_timer(&ep->umidi->error_timer, |
| 263 | jiffies + ERROR_DELAY_JIFFIES); |
| 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | return; |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 266 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 269 | urb->dev = ep->umidi->dev; |
Clemens Ladisch | 3cfc1eb | 2005-09-26 10:01:12 +0200 | [diff] [blame] | 270 | snd_usbmidi_submit_urb(urb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 273 | static void snd_usbmidi_out_urb_complete(struct urb* urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 275 | struct out_urb_context *context = urb->context; |
| 276 | struct snd_usb_midi_out_endpoint* ep = context->ep; |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 277 | unsigned int urb_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | spin_lock(&ep->buffer_lock); |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 280 | urb_index = context - ep->urbs; |
| 281 | ep->active_urbs &= ~(1 << urb_index); |
| 282 | if (unlikely(ep->drain_urbs)) { |
| 283 | ep->drain_urbs &= ~(1 << urb_index); |
| 284 | wake_up(&ep->drain_wait); |
| 285 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | spin_unlock(&ep->buffer_lock); |
| 287 | if (urb->status < 0) { |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 288 | int err = snd_usbmidi_urb_error(urb->status); |
| 289 | if (err < 0) { |
| 290 | if (err != -ENODEV) |
| 291 | mod_timer(&ep->umidi->error_timer, |
| 292 | jiffies + ERROR_DELAY_JIFFIES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | return; |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 294 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | } |
| 296 | snd_usbmidi_do_output(ep); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * This is called when some data should be transferred to the device |
| 301 | * (from one or more substreams). |
| 302 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 303 | static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint* ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 305 | unsigned int urb_index; |
| 306 | struct urb* urb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | unsigned long flags; |
| 308 | |
| 309 | spin_lock_irqsave(&ep->buffer_lock, flags); |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 310 | if (ep->umidi->disconnected) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | spin_unlock_irqrestore(&ep->buffer_lock, flags); |
| 312 | return; |
| 313 | } |
| 314 | |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 315 | urb_index = ep->next_urb; |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 316 | for (;;) { |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 317 | if (!(ep->active_urbs & (1 << urb_index))) { |
| 318 | urb = ep->urbs[urb_index].urb; |
| 319 | urb->transfer_buffer_length = 0; |
| 320 | ep->umidi->usb_protocol_ops->output(ep, urb); |
| 321 | if (urb->transfer_buffer_length == 0) |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 322 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 324 | dump_urb("sending", urb->transfer_buffer, |
| 325 | urb->transfer_buffer_length); |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 326 | urb->dev = ep->umidi->dev; |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 327 | if (snd_usbmidi_submit_urb(urb, GFP_ATOMIC) < 0) |
| 328 | break; |
| 329 | ep->active_urbs |= 1 << urb_index; |
| 330 | } |
| 331 | if (++urb_index >= OUTPUT_URBS) |
| 332 | urb_index = 0; |
| 333 | if (urb_index == ep->next_urb) |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 334 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | } |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 336 | ep->next_urb = urb_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | spin_unlock_irqrestore(&ep->buffer_lock, flags); |
| 338 | } |
| 339 | |
| 340 | static void snd_usbmidi_out_tasklet(unsigned long data) |
| 341 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 342 | struct snd_usb_midi_out_endpoint* ep = (struct snd_usb_midi_out_endpoint *) data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | |
| 344 | snd_usbmidi_do_output(ep); |
| 345 | } |
| 346 | |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 347 | /* called after transfers had been interrupted due to some USB error */ |
| 348 | static void snd_usbmidi_error_timer(unsigned long data) |
| 349 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 350 | struct snd_usb_midi *umidi = (struct snd_usb_midi *)data; |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 351 | unsigned int i, j; |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 352 | |
Takashi Iwai | c0792e0 | 2008-02-22 18:34:44 +0100 | [diff] [blame] | 353 | spin_lock(&umidi->disc_lock); |
| 354 | if (umidi->disconnected) { |
| 355 | spin_unlock(&umidi->disc_lock); |
| 356 | return; |
| 357 | } |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 358 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 359 | struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in; |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 360 | if (in && in->error_resubmit) { |
| 361 | in->error_resubmit = 0; |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 362 | for (j = 0; j < INPUT_URBS; ++j) { |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 363 | in->urbs[j]->dev = umidi->dev; |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 364 | snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC); |
| 365 | } |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 366 | } |
| 367 | if (umidi->endpoints[i].out) |
| 368 | snd_usbmidi_do_output(umidi->endpoints[i].out); |
| 369 | } |
Takashi Iwai | c0792e0 | 2008-02-22 18:34:44 +0100 | [diff] [blame] | 370 | spin_unlock(&umidi->disc_lock); |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | /* helper function to send static data that may not DMA-able */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 374 | static int send_bulk_static_data(struct snd_usb_midi_out_endpoint* ep, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | const void *data, int len) |
| 376 | { |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 377 | int err = 0; |
Alexey Dobriyan | 52978be | 2006-09-30 23:27:21 -0700 | [diff] [blame] | 378 | void *buf = kmemdup(data, len, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | if (!buf) |
| 380 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | dump_urb("sending", buf, len); |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 382 | if (ep->urbs[0].urb) |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 383 | err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe, |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 384 | buf, len, NULL, 250); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | kfree(buf); |
| 386 | return err; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Standard USB MIDI protocol: see the spec. |
| 391 | * Midiman protocol: like the standard protocol, but the control byte is the |
| 392 | * fourth byte in each packet, and uses length instead of CIN. |
| 393 | */ |
| 394 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 395 | static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint* ep, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | uint8_t* buffer, int buffer_length) |
| 397 | { |
| 398 | int i; |
| 399 | |
| 400 | for (i = 0; i + 3 < buffer_length; i += 4) |
| 401 | if (buffer[i] != 0) { |
| 402 | int cable = buffer[i] >> 4; |
| 403 | int length = snd_usbmidi_cin_length[buffer[i] & 0x0f]; |
| 404 | snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length); |
| 405 | } |
| 406 | } |
| 407 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 408 | static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint* ep, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | uint8_t* buffer, int buffer_length) |
| 410 | { |
| 411 | int i; |
| 412 | |
| 413 | for (i = 0; i + 3 < buffer_length; i += 4) |
| 414 | if (buffer[i + 3] != 0) { |
| 415 | int port = buffer[i + 3] >> 4; |
| 416 | int length = buffer[i + 3] & 3; |
| 417 | snd_usbmidi_input_data(ep, port, &buffer[i], length); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /* |
Clemens Ladisch | d05cc1043 | 2007-05-07 09:28:53 +0200 | [diff] [blame] | 422 | * Buggy M-Audio device: running status on input results in a packet that has |
| 423 | * the data bytes but not the status byte and that is marked with CIN 4. |
| 424 | */ |
| 425 | static void snd_usbmidi_maudio_broken_running_status_input( |
| 426 | struct snd_usb_midi_in_endpoint* ep, |
| 427 | uint8_t* buffer, int buffer_length) |
| 428 | { |
| 429 | int i; |
| 430 | |
| 431 | for (i = 0; i + 3 < buffer_length; i += 4) |
| 432 | if (buffer[i] != 0) { |
| 433 | int cable = buffer[i] >> 4; |
| 434 | u8 cin = buffer[i] & 0x0f; |
| 435 | struct usbmidi_in_port *port = &ep->ports[cable]; |
| 436 | int length; |
| 437 | |
| 438 | length = snd_usbmidi_cin_length[cin]; |
| 439 | if (cin == 0xf && buffer[i + 1] >= 0xf8) |
| 440 | ; /* realtime msg: no running status change */ |
| 441 | else if (cin >= 0x8 && cin <= 0xe) |
| 442 | /* channel msg */ |
| 443 | port->running_status_length = length - 1; |
| 444 | else if (cin == 0x4 && |
| 445 | port->running_status_length != 0 && |
| 446 | buffer[i + 1] < 0x80) |
| 447 | /* CIN 4 that is not a SysEx */ |
| 448 | length = port->running_status_length; |
| 449 | else |
| 450 | /* |
| 451 | * All other msgs cannot begin running status. |
| 452 | * (A channel msg sent as two or three CIN 0xF |
| 453 | * packets could in theory, but this device |
| 454 | * doesn't use this format.) |
| 455 | */ |
| 456 | port->running_status_length = 0; |
| 457 | snd_usbmidi_input_data(ep, cable, &buffer[i + 1], length); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /* |
Clemens Ladisch | 61870ae | 2007-08-16 08:44:51 +0200 | [diff] [blame] | 462 | * CME protocol: like the standard protocol, but SysEx commands are sent as a |
| 463 | * single USB packet preceded by a 0x0F byte. |
| 464 | */ |
| 465 | static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep, |
| 466 | uint8_t *buffer, int buffer_length) |
| 467 | { |
| 468 | if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f) |
| 469 | snd_usbmidi_standard_input(ep, buffer, buffer_length); |
| 470 | else |
| 471 | snd_usbmidi_input_data(ep, buffer[0] >> 4, |
| 472 | &buffer[1], buffer_length - 1); |
| 473 | } |
| 474 | |
| 475 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | * Adds one USB MIDI packet to the output buffer. |
| 477 | */ |
| 478 | static void snd_usbmidi_output_standard_packet(struct urb* urb, uint8_t p0, |
| 479 | uint8_t p1, uint8_t p2, uint8_t p3) |
| 480 | { |
| 481 | |
| 482 | uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length; |
| 483 | buf[0] = p0; |
| 484 | buf[1] = p1; |
| 485 | buf[2] = p2; |
| 486 | buf[3] = p3; |
| 487 | urb->transfer_buffer_length += 4; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Adds one Midiman packet to the output buffer. |
| 492 | */ |
| 493 | static void snd_usbmidi_output_midiman_packet(struct urb* urb, uint8_t p0, |
| 494 | uint8_t p1, uint8_t p2, uint8_t p3) |
| 495 | { |
| 496 | |
| 497 | uint8_t* buf = (uint8_t*)urb->transfer_buffer + urb->transfer_buffer_length; |
| 498 | buf[0] = p1; |
| 499 | buf[1] = p2; |
| 500 | buf[2] = p3; |
| 501 | buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f]; |
| 502 | urb->transfer_buffer_length += 4; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * Converts MIDI commands to USB MIDI packets. |
| 507 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 508 | static void snd_usbmidi_transmit_byte(struct usbmidi_out_port* port, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | uint8_t b, struct urb* urb) |
| 510 | { |
| 511 | uint8_t p0 = port->cable; |
| 512 | void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) = |
| 513 | port->ep->umidi->usb_protocol_ops->output_packet; |
| 514 | |
| 515 | if (b >= 0xf8) { |
| 516 | output_packet(urb, p0 | 0x0f, b, 0, 0); |
| 517 | } else if (b >= 0xf0) { |
| 518 | switch (b) { |
| 519 | case 0xf0: |
| 520 | port->data[0] = b; |
| 521 | port->state = STATE_SYSEX_1; |
| 522 | break; |
| 523 | case 0xf1: |
| 524 | case 0xf3: |
| 525 | port->data[0] = b; |
| 526 | port->state = STATE_1PARAM; |
| 527 | break; |
| 528 | case 0xf2: |
| 529 | port->data[0] = b; |
| 530 | port->state = STATE_2PARAM_1; |
| 531 | break; |
| 532 | case 0xf4: |
| 533 | case 0xf5: |
| 534 | port->state = STATE_UNKNOWN; |
| 535 | break; |
| 536 | case 0xf6: |
| 537 | output_packet(urb, p0 | 0x05, 0xf6, 0, 0); |
| 538 | port->state = STATE_UNKNOWN; |
| 539 | break; |
| 540 | case 0xf7: |
| 541 | switch (port->state) { |
| 542 | case STATE_SYSEX_0: |
| 543 | output_packet(urb, p0 | 0x05, 0xf7, 0, 0); |
| 544 | break; |
| 545 | case STATE_SYSEX_1: |
| 546 | output_packet(urb, p0 | 0x06, port->data[0], 0xf7, 0); |
| 547 | break; |
| 548 | case STATE_SYSEX_2: |
| 549 | output_packet(urb, p0 | 0x07, port->data[0], port->data[1], 0xf7); |
| 550 | break; |
| 551 | } |
| 552 | port->state = STATE_UNKNOWN; |
| 553 | break; |
| 554 | } |
| 555 | } else if (b >= 0x80) { |
| 556 | port->data[0] = b; |
| 557 | if (b >= 0xc0 && b <= 0xdf) |
| 558 | port->state = STATE_1PARAM; |
| 559 | else |
| 560 | port->state = STATE_2PARAM_1; |
| 561 | } else { /* b < 0x80 */ |
| 562 | switch (port->state) { |
| 563 | case STATE_1PARAM: |
| 564 | if (port->data[0] < 0xf0) { |
| 565 | p0 |= port->data[0] >> 4; |
| 566 | } else { |
| 567 | p0 |= 0x02; |
| 568 | port->state = STATE_UNKNOWN; |
| 569 | } |
| 570 | output_packet(urb, p0, port->data[0], b, 0); |
| 571 | break; |
| 572 | case STATE_2PARAM_1: |
| 573 | port->data[1] = b; |
| 574 | port->state = STATE_2PARAM_2; |
| 575 | break; |
| 576 | case STATE_2PARAM_2: |
| 577 | if (port->data[0] < 0xf0) { |
| 578 | p0 |= port->data[0] >> 4; |
| 579 | port->state = STATE_2PARAM_1; |
| 580 | } else { |
| 581 | p0 |= 0x03; |
| 582 | port->state = STATE_UNKNOWN; |
| 583 | } |
| 584 | output_packet(urb, p0, port->data[0], port->data[1], b); |
| 585 | break; |
| 586 | case STATE_SYSEX_0: |
| 587 | port->data[0] = b; |
| 588 | port->state = STATE_SYSEX_1; |
| 589 | break; |
| 590 | case STATE_SYSEX_1: |
| 591 | port->data[1] = b; |
| 592 | port->state = STATE_SYSEX_2; |
| 593 | break; |
| 594 | case STATE_SYSEX_2: |
| 595 | output_packet(urb, p0 | 0x04, port->data[0], port->data[1], b); |
| 596 | port->state = STATE_SYSEX_0; |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | } |
| 601 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 602 | static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint* ep, |
| 603 | struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | int p; |
| 606 | |
| 607 | /* FIXME: lower-numbered ports can starve higher-numbered ports */ |
| 608 | for (p = 0; p < 0x10; ++p) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 609 | struct usbmidi_out_port* port = &ep->ports[p]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if (!port->active) |
| 611 | continue; |
| 612 | while (urb->transfer_buffer_length + 3 < ep->max_transfer) { |
| 613 | uint8_t b; |
| 614 | if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) { |
| 615 | port->active = 0; |
| 616 | break; |
| 617 | } |
| 618 | snd_usbmidi_transmit_byte(port, b, urb); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | static struct usb_protocol_ops snd_usbmidi_standard_ops = { |
| 624 | .input = snd_usbmidi_standard_input, |
| 625 | .output = snd_usbmidi_standard_output, |
| 626 | .output_packet = snd_usbmidi_output_standard_packet, |
| 627 | }; |
| 628 | |
| 629 | static struct usb_protocol_ops snd_usbmidi_midiman_ops = { |
| 630 | .input = snd_usbmidi_midiman_input, |
| 631 | .output = snd_usbmidi_standard_output, |
| 632 | .output_packet = snd_usbmidi_output_midiman_packet, |
| 633 | }; |
| 634 | |
Clemens Ladisch | d05cc1043 | 2007-05-07 09:28:53 +0200 | [diff] [blame] | 635 | static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = { |
| 636 | .input = snd_usbmidi_maudio_broken_running_status_input, |
| 637 | .output = snd_usbmidi_standard_output, |
| 638 | .output_packet = snd_usbmidi_output_standard_packet, |
| 639 | }; |
| 640 | |
Clemens Ladisch | 61870ae | 2007-08-16 08:44:51 +0200 | [diff] [blame] | 641 | static struct usb_protocol_ops snd_usbmidi_cme_ops = { |
| 642 | .input = snd_usbmidi_cme_input, |
| 643 | .output = snd_usbmidi_standard_output, |
| 644 | .output_packet = snd_usbmidi_output_standard_packet, |
| 645 | }; |
| 646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | /* |
Krzysztof Foltman | 4434ade | 2010-05-20 20:31:10 +0100 | [diff] [blame] | 648 | * AKAI MPD16 protocol: |
| 649 | * |
| 650 | * For control port (endpoint 1): |
| 651 | * ============================== |
| 652 | * One or more chunks consisting of first byte of (0x10 | msg_len) and then a |
| 653 | * SysEx message (msg_len=9 bytes long). |
| 654 | * |
| 655 | * For data port (endpoint 2): |
| 656 | * =========================== |
| 657 | * One or more chunks consisting of first byte of (0x20 | msg_len) and then a |
| 658 | * MIDI message (msg_len bytes long) |
| 659 | * |
| 660 | * Messages sent: Active Sense, Note On, Poly Pressure, Control Change. |
| 661 | */ |
| 662 | static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep, |
| 663 | uint8_t *buffer, int buffer_length) |
| 664 | { |
| 665 | unsigned int pos = 0; |
| 666 | unsigned int len = (unsigned int)buffer_length; |
| 667 | while (pos < len) { |
| 668 | unsigned int port = (buffer[pos] >> 4) - 1; |
| 669 | unsigned int msg_len = buffer[pos] & 0x0f; |
| 670 | pos++; |
| 671 | if (pos + msg_len <= len && port < 2) |
| 672 | snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len); |
| 673 | pos += msg_len; |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | #define MAX_AKAI_SYSEX_LEN 9 |
| 678 | |
| 679 | static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep, |
| 680 | struct urb *urb) |
| 681 | { |
| 682 | uint8_t *msg; |
| 683 | int pos, end, count, buf_end; |
| 684 | uint8_t tmp[MAX_AKAI_SYSEX_LEN]; |
| 685 | struct snd_rawmidi_substream *substream = ep->ports[0].substream; |
| 686 | |
| 687 | if (!ep->ports[0].active) |
| 688 | return; |
| 689 | |
| 690 | msg = urb->transfer_buffer + urb->transfer_buffer_length; |
| 691 | buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1; |
| 692 | |
| 693 | /* only try adding more data when there's space for at least 1 SysEx */ |
| 694 | while (urb->transfer_buffer_length < buf_end) { |
| 695 | count = snd_rawmidi_transmit_peek(substream, |
| 696 | tmp, MAX_AKAI_SYSEX_LEN); |
| 697 | if (!count) { |
| 698 | ep->ports[0].active = 0; |
| 699 | return; |
| 700 | } |
| 701 | /* try to skip non-SysEx data */ |
| 702 | for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++) |
| 703 | ; |
| 704 | |
| 705 | if (pos > 0) { |
| 706 | snd_rawmidi_transmit_ack(substream, pos); |
| 707 | continue; |
| 708 | } |
| 709 | |
| 710 | /* look for the start or end marker */ |
| 711 | for (end = 1; end < count && tmp[end] < 0xF0; end++) |
| 712 | ; |
| 713 | |
| 714 | /* next SysEx started before the end of current one */ |
| 715 | if (end < count && tmp[end] == 0xF0) { |
| 716 | /* it's incomplete - drop it */ |
| 717 | snd_rawmidi_transmit_ack(substream, end); |
| 718 | continue; |
| 719 | } |
| 720 | /* SysEx complete */ |
| 721 | if (end < count && tmp[end] == 0xF7) { |
| 722 | /* queue it, ack it, and get the next one */ |
| 723 | count = end + 1; |
| 724 | msg[0] = 0x10 | count; |
| 725 | memcpy(&msg[1], tmp, count); |
| 726 | snd_rawmidi_transmit_ack(substream, count); |
| 727 | urb->transfer_buffer_length += count + 1; |
| 728 | msg += count + 1; |
| 729 | continue; |
| 730 | } |
| 731 | /* less than 9 bytes and no end byte - wait for more */ |
| 732 | if (count < MAX_AKAI_SYSEX_LEN) { |
| 733 | ep->ports[0].active = 0; |
| 734 | return; |
| 735 | } |
| 736 | /* 9 bytes and no end marker in sight - malformed, skip it */ |
| 737 | snd_rawmidi_transmit_ack(substream, count); |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | static struct usb_protocol_ops snd_usbmidi_akai_ops = { |
| 742 | .input = snd_usbmidi_akai_input, |
| 743 | .output = snd_usbmidi_akai_output, |
| 744 | }; |
| 745 | |
| 746 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | * Novation USB MIDI protocol: number of data bytes is in the first byte |
| 748 | * (when receiving) (+1!) or in the second byte (when sending); data begins |
| 749 | * at the third byte. |
| 750 | */ |
| 751 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 752 | static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint* ep, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | uint8_t* buffer, int buffer_length) |
| 754 | { |
| 755 | if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1) |
| 756 | return; |
| 757 | snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1); |
| 758 | } |
| 759 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 760 | static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint* ep, |
| 761 | struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
| 763 | uint8_t* transfer_buffer; |
| 764 | int count; |
| 765 | |
| 766 | if (!ep->ports[0].active) |
| 767 | return; |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 768 | transfer_buffer = urb->transfer_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | count = snd_rawmidi_transmit(ep->ports[0].substream, |
| 770 | &transfer_buffer[2], |
| 771 | ep->max_transfer - 2); |
| 772 | if (count < 1) { |
| 773 | ep->ports[0].active = 0; |
| 774 | return; |
| 775 | } |
| 776 | transfer_buffer[0] = 0; |
| 777 | transfer_buffer[1] = count; |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 778 | urb->transfer_buffer_length = 2 + count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | static struct usb_protocol_ops snd_usbmidi_novation_ops = { |
| 782 | .input = snd_usbmidi_novation_input, |
| 783 | .output = snd_usbmidi_novation_output, |
| 784 | }; |
| 785 | |
| 786 | /* |
Clemens Ladisch | 6155aff | 2005-07-04 09:20:42 +0200 | [diff] [blame] | 787 | * "raw" protocol: used by the MOTU FastLane. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | */ |
| 789 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 790 | static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep, |
Clemens Ladisch | 6155aff | 2005-07-04 09:20:42 +0200 | [diff] [blame] | 791 | uint8_t* buffer, int buffer_length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | { |
| 793 | snd_usbmidi_input_data(ep, 0, buffer, buffer_length); |
| 794 | } |
| 795 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 796 | static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint* ep, |
| 797 | struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | { |
| 799 | int count; |
| 800 | |
| 801 | if (!ep->ports[0].active) |
| 802 | return; |
| 803 | count = snd_rawmidi_transmit(ep->ports[0].substream, |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 804 | urb->transfer_buffer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | ep->max_transfer); |
| 806 | if (count < 1) { |
| 807 | ep->ports[0].active = 0; |
| 808 | return; |
| 809 | } |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 810 | urb->transfer_buffer_length = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Clemens Ladisch | 6155aff | 2005-07-04 09:20:42 +0200 | [diff] [blame] | 813 | static struct usb_protocol_ops snd_usbmidi_raw_ops = { |
| 814 | .input = snd_usbmidi_raw_input, |
| 815 | .output = snd_usbmidi_raw_output, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | }; |
| 817 | |
Karsten Wiese | 030a07e | 2008-07-30 15:13:29 +0200 | [diff] [blame] | 818 | static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep, |
| 819 | uint8_t *buffer, int buffer_length) |
| 820 | { |
| 821 | if (buffer_length != 9) |
| 822 | return; |
| 823 | buffer_length = 8; |
| 824 | while (buffer_length && buffer[buffer_length - 1] == 0xFD) |
| 825 | buffer_length--; |
| 826 | if (buffer_length) |
| 827 | snd_usbmidi_input_data(ep, 0, buffer, buffer_length); |
| 828 | } |
| 829 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 830 | static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep, |
| 831 | struct urb *urb) |
Karsten Wiese | 030a07e | 2008-07-30 15:13:29 +0200 | [diff] [blame] | 832 | { |
| 833 | int count; |
| 834 | |
| 835 | if (!ep->ports[0].active) |
| 836 | return; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 837 | count = snd_usb_get_speed(ep->umidi->dev) == USB_SPEED_HIGH ? 1 : 2; |
Karsten Wiese | 030a07e | 2008-07-30 15:13:29 +0200 | [diff] [blame] | 838 | count = snd_rawmidi_transmit(ep->ports[0].substream, |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 839 | urb->transfer_buffer, |
Karsten Wiese | 030a07e | 2008-07-30 15:13:29 +0200 | [diff] [blame] | 840 | count); |
| 841 | if (count < 1) { |
| 842 | ep->ports[0].active = 0; |
| 843 | return; |
| 844 | } |
| 845 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 846 | memset(urb->transfer_buffer + count, 0xFD, 9 - count); |
| 847 | urb->transfer_buffer_length = count; |
Karsten Wiese | 030a07e | 2008-07-30 15:13:29 +0200 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | static struct usb_protocol_ops snd_usbmidi_122l_ops = { |
| 851 | .input = snd_usbmidi_us122l_input, |
| 852 | .output = snd_usbmidi_us122l_output, |
| 853 | }; |
| 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | /* |
| 856 | * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching. |
| 857 | */ |
| 858 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 859 | static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint* ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
| 861 | static const u8 init_data[] = { |
| 862 | /* initialization magic: "get version" */ |
| 863 | 0xf0, |
| 864 | 0x00, 0x20, 0x31, /* Emagic */ |
| 865 | 0x64, /* Unitor8 */ |
| 866 | 0x0b, /* version number request */ |
| 867 | 0x00, /* command version */ |
| 868 | 0x00, /* EEPROM, box 0 */ |
| 869 | 0xf7 |
| 870 | }; |
| 871 | send_bulk_static_data(ep, init_data, sizeof(init_data)); |
| 872 | /* while we're at it, pour on more magic */ |
| 873 | send_bulk_static_data(ep, init_data, sizeof(init_data)); |
| 874 | } |
| 875 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 876 | static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint* ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
| 878 | static const u8 finish_data[] = { |
| 879 | /* switch to patch mode with last preset */ |
| 880 | 0xf0, |
| 881 | 0x00, 0x20, 0x31, /* Emagic */ |
| 882 | 0x64, /* Unitor8 */ |
| 883 | 0x10, /* patch switch command */ |
| 884 | 0x00, /* command version */ |
| 885 | 0x7f, /* to all boxes */ |
| 886 | 0x40, /* last preset in EEPROM */ |
| 887 | 0xf7 |
| 888 | }; |
| 889 | send_bulk_static_data(ep, finish_data, sizeof(finish_data)); |
| 890 | } |
| 891 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 892 | static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint* ep, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | uint8_t* buffer, int buffer_length) |
| 894 | { |
Clemens Ladisch | c347e9f | 2005-08-25 11:10:05 +0200 | [diff] [blame] | 895 | int i; |
| 896 | |
| 897 | /* FF indicates end of valid data */ |
| 898 | for (i = 0; i < buffer_length; ++i) |
| 899 | if (buffer[i] == 0xff) { |
| 900 | buffer_length = i; |
| 901 | break; |
| 902 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
| 904 | /* handle F5 at end of last buffer */ |
| 905 | if (ep->seen_f5) |
| 906 | goto switch_port; |
| 907 | |
| 908 | while (buffer_length > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | /* determine size of data until next F5 */ |
| 910 | for (i = 0; i < buffer_length; ++i) |
| 911 | if (buffer[i] == 0xf5) |
| 912 | break; |
| 913 | snd_usbmidi_input_data(ep, ep->current_port, buffer, i); |
| 914 | buffer += i; |
| 915 | buffer_length -= i; |
| 916 | |
| 917 | if (buffer_length <= 0) |
| 918 | break; |
| 919 | /* assert(buffer[0] == 0xf5); */ |
| 920 | ep->seen_f5 = 1; |
| 921 | ++buffer; |
| 922 | --buffer_length; |
| 923 | |
| 924 | switch_port: |
| 925 | if (buffer_length <= 0) |
| 926 | break; |
| 927 | if (buffer[0] < 0x80) { |
| 928 | ep->current_port = (buffer[0] - 1) & 15; |
| 929 | ++buffer; |
| 930 | --buffer_length; |
| 931 | } |
| 932 | ep->seen_f5 = 0; |
| 933 | } |
| 934 | } |
| 935 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 936 | static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint* ep, |
| 937 | struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | { |
| 939 | int port0 = ep->current_port; |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 940 | uint8_t* buf = urb->transfer_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | int buf_free = ep->max_transfer; |
| 942 | int length, i; |
| 943 | |
| 944 | for (i = 0; i < 0x10; ++i) { |
| 945 | /* round-robin, starting at the last current port */ |
| 946 | int portnum = (port0 + i) & 15; |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 947 | struct usbmidi_out_port* port = &ep->ports[portnum]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
| 949 | if (!port->active) |
| 950 | continue; |
| 951 | if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) { |
| 952 | port->active = 0; |
| 953 | continue; |
| 954 | } |
| 955 | |
| 956 | if (portnum != ep->current_port) { |
| 957 | if (buf_free < 2) |
| 958 | break; |
| 959 | ep->current_port = portnum; |
| 960 | buf[0] = 0xf5; |
| 961 | buf[1] = (portnum + 1) & 15; |
| 962 | buf += 2; |
| 963 | buf_free -= 2; |
| 964 | } |
| 965 | |
| 966 | if (buf_free < 1) |
| 967 | break; |
| 968 | length = snd_rawmidi_transmit(port->substream, buf, buf_free); |
| 969 | if (length > 0) { |
| 970 | buf += length; |
| 971 | buf_free -= length; |
| 972 | if (buf_free < 1) |
| 973 | break; |
| 974 | } |
| 975 | } |
Clemens Ladisch | c347e9f | 2005-08-25 11:10:05 +0200 | [diff] [blame] | 976 | if (buf_free < ep->max_transfer && buf_free > 0) { |
| 977 | *buf = 0xff; |
| 978 | --buf_free; |
| 979 | } |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 980 | urb->transfer_buffer_length = ep->max_transfer - buf_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | static struct usb_protocol_ops snd_usbmidi_emagic_ops = { |
| 984 | .input = snd_usbmidi_emagic_input, |
| 985 | .output = snd_usbmidi_emagic_output, |
| 986 | .init_out_endpoint = snd_usbmidi_emagic_init_out, |
| 987 | .finish_out_endpoint = snd_usbmidi_emagic_finish_out, |
| 988 | }; |
| 989 | |
| 990 | |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 991 | static void update_roland_altsetting(struct snd_usb_midi* umidi) |
| 992 | { |
| 993 | struct usb_interface *intf; |
| 994 | struct usb_host_interface *hostif; |
| 995 | struct usb_interface_descriptor *intfd; |
| 996 | int is_light_load; |
| 997 | |
| 998 | intf = umidi->iface; |
| 999 | is_light_load = intf->cur_altsetting != intf->altsetting; |
| 1000 | if (umidi->roland_load_ctl->private_value == is_light_load) |
| 1001 | return; |
| 1002 | hostif = &intf->altsetting[umidi->roland_load_ctl->private_value]; |
| 1003 | intfd = get_iface_desc(hostif); |
| 1004 | snd_usbmidi_input_stop(&umidi->list); |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1005 | usb_set_interface(umidi->dev, intfd->bInterfaceNumber, |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1006 | intfd->bAlternateSetting); |
| 1007 | snd_usbmidi_input_start(&umidi->list); |
| 1008 | } |
| 1009 | |
| 1010 | static void substream_open(struct snd_rawmidi_substream *substream, int open) |
| 1011 | { |
| 1012 | struct snd_usb_midi* umidi = substream->rmidi->private_data; |
| 1013 | struct snd_kcontrol *ctl; |
| 1014 | |
| 1015 | mutex_lock(&umidi->mutex); |
| 1016 | if (open) { |
| 1017 | if (umidi->opened++ == 0 && umidi->roland_load_ctl) { |
| 1018 | ctl = umidi->roland_load_ctl; |
| 1019 | ctl->vd[0].access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1020 | snd_ctl_notify(umidi->card, |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1021 | SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); |
| 1022 | update_roland_altsetting(umidi); |
| 1023 | } |
| 1024 | } else { |
| 1025 | if (--umidi->opened == 0 && umidi->roland_load_ctl) { |
| 1026 | ctl = umidi->roland_load_ctl; |
| 1027 | ctl->vd[0].access &= ~SNDRV_CTL_ELEM_ACCESS_INACTIVE; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1028 | snd_ctl_notify(umidi->card, |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1029 | SNDRV_CTL_EVENT_MASK_INFO, &ctl->id); |
| 1030 | } |
| 1031 | } |
| 1032 | mutex_unlock(&umidi->mutex); |
| 1033 | } |
| 1034 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1035 | static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1037 | struct snd_usb_midi* umidi = substream->rmidi->private_data; |
| 1038 | struct usbmidi_out_port* port = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | int i, j; |
| 1040 | |
| 1041 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) |
| 1042 | if (umidi->endpoints[i].out) |
| 1043 | for (j = 0; j < 0x10; ++j) |
| 1044 | if (umidi->endpoints[i].out->ports[j].substream == substream) { |
| 1045 | port = &umidi->endpoints[i].out->ports[j]; |
| 1046 | break; |
| 1047 | } |
| 1048 | if (!port) { |
| 1049 | snd_BUG(); |
| 1050 | return -ENXIO; |
| 1051 | } |
| 1052 | substream->runtime->private_data = port; |
| 1053 | port->state = STATE_UNKNOWN; |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1054 | substream_open(substream, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | return 0; |
| 1056 | } |
| 1057 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1058 | static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | { |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1060 | substream_open(substream, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | return 0; |
| 1062 | } |
| 1063 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1064 | static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream, int up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1066 | struct usbmidi_out_port* port = (struct usbmidi_out_port*)substream->runtime->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
| 1068 | port->active = up; |
| 1069 | if (up) { |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1070 | if (port->ep->umidi->disconnected) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | /* gobble up remaining bytes to prevent wait in |
| 1072 | * snd_rawmidi_drain_output */ |
| 1073 | while (!snd_rawmidi_transmit_empty(substream)) |
| 1074 | snd_rawmidi_transmit_ack(substream, 1); |
| 1075 | return; |
| 1076 | } |
Takashi Iwai | 1f04128 | 2008-12-18 12:17:55 +0100 | [diff] [blame] | 1077 | tasklet_schedule(&port->ep->tasklet); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 1081 | static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream) |
| 1082 | { |
| 1083 | struct usbmidi_out_port* port = substream->runtime->private_data; |
| 1084 | struct snd_usb_midi_out_endpoint *ep = port->ep; |
| 1085 | unsigned int drain_urbs; |
| 1086 | DEFINE_WAIT(wait); |
| 1087 | long timeout = msecs_to_jiffies(50); |
| 1088 | |
Takashi Iwai | 29aac00 | 2010-04-10 21:27:23 +0200 | [diff] [blame] | 1089 | if (ep->umidi->disconnected) |
| 1090 | return; |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 1091 | /* |
| 1092 | * The substream buffer is empty, but some data might still be in the |
| 1093 | * currently active URBs, so we have to wait for those to complete. |
| 1094 | */ |
| 1095 | spin_lock_irq(&ep->buffer_lock); |
| 1096 | drain_urbs = ep->active_urbs; |
| 1097 | if (drain_urbs) { |
| 1098 | ep->drain_urbs |= drain_urbs; |
| 1099 | do { |
| 1100 | prepare_to_wait(&ep->drain_wait, &wait, |
| 1101 | TASK_UNINTERRUPTIBLE); |
| 1102 | spin_unlock_irq(&ep->buffer_lock); |
| 1103 | timeout = schedule_timeout(timeout); |
| 1104 | spin_lock_irq(&ep->buffer_lock); |
| 1105 | drain_urbs &= ep->drain_urbs; |
| 1106 | } while (drain_urbs && timeout); |
| 1107 | finish_wait(&ep->drain_wait, &wait); |
| 1108 | } |
| 1109 | spin_unlock_irq(&ep->buffer_lock); |
| 1110 | } |
| 1111 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1112 | static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | { |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1114 | substream_open(substream, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1118 | static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | { |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1120 | substream_open(substream, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1124 | static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream, int up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1126 | struct snd_usb_midi* umidi = substream->rmidi->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | |
| 1128 | if (up) |
| 1129 | set_bit(substream->number, &umidi->input_triggered); |
| 1130 | else |
| 1131 | clear_bit(substream->number, &umidi->input_triggered); |
| 1132 | } |
| 1133 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1134 | static struct snd_rawmidi_ops snd_usbmidi_output_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | .open = snd_usbmidi_output_open, |
| 1136 | .close = snd_usbmidi_output_close, |
| 1137 | .trigger = snd_usbmidi_output_trigger, |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 1138 | .drain = snd_usbmidi_output_drain, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | }; |
| 1140 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1141 | static struct snd_rawmidi_ops snd_usbmidi_input_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | .open = snd_usbmidi_input_open, |
| 1143 | .close = snd_usbmidi_input_close, |
| 1144 | .trigger = snd_usbmidi_input_trigger |
| 1145 | }; |
| 1146 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1147 | static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb, |
| 1148 | unsigned int buffer_length) |
| 1149 | { |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 1150 | usb_free_coherent(umidi->dev, buffer_length, |
| 1151 | urb->transfer_buffer, urb->transfer_dma); |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1152 | usb_free_urb(urb); |
| 1153 | } |
| 1154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | /* |
| 1156 | * Frees an input endpoint. |
| 1157 | * May be called when ep hasn't been initialized completely. |
| 1158 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1159 | static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint* ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | { |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 1161 | unsigned int i; |
| 1162 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1163 | for (i = 0; i < INPUT_URBS; ++i) |
| 1164 | if (ep->urbs[i]) |
| 1165 | free_urb_and_buffer(ep->umidi, ep->urbs[i], |
| 1166 | ep->urbs[i]->transfer_buffer_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | kfree(ep); |
| 1168 | } |
| 1169 | |
| 1170 | /* |
| 1171 | * Creates an input endpoint. |
| 1172 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1173 | static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi, |
| 1174 | struct snd_usb_midi_endpoint_info* ep_info, |
| 1175 | struct snd_usb_midi_endpoint* rep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1177 | struct snd_usb_midi_in_endpoint* ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | void* buffer; |
| 1179 | unsigned int pipe; |
| 1180 | int length; |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 1181 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | |
| 1183 | rep->in = NULL; |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1184 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | if (!ep) |
| 1186 | return -ENOMEM; |
| 1187 | ep->umidi = umidi; |
| 1188 | |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 1189 | for (i = 0; i < INPUT_URBS; ++i) { |
| 1190 | ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL); |
| 1191 | if (!ep->urbs[i]) { |
| 1192 | snd_usbmidi_in_endpoint_delete(ep); |
| 1193 | return -ENOMEM; |
| 1194 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | if (ep_info->in_interval) |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1197 | pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | else |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1199 | pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep); |
| 1200 | length = usb_maxpacket(umidi->dev, pipe, 0); |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 1201 | for (i = 0; i < INPUT_URBS; ++i) { |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 1202 | buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL, |
| 1203 | &ep->urbs[i]->transfer_dma); |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 1204 | if (!buffer) { |
| 1205 | snd_usbmidi_in_endpoint_delete(ep); |
| 1206 | return -ENOMEM; |
| 1207 | } |
| 1208 | if (ep_info->in_interval) |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1209 | usb_fill_int_urb(ep->urbs[i], umidi->dev, |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 1210 | pipe, buffer, length, |
| 1211 | snd_usbmidi_in_urb_complete, |
| 1212 | ep, ep_info->in_interval); |
| 1213 | else |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1214 | usb_fill_bulk_urb(ep->urbs[i], umidi->dev, |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 1215 | pipe, buffer, length, |
| 1216 | snd_usbmidi_in_urb_complete, ep); |
| 1217 | ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
| 1220 | rep->in = ep; |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | /* |
| 1225 | * Frees an output endpoint. |
| 1226 | * May be called when ep hasn't been initialized completely. |
| 1227 | */ |
Takashi Iwai | 29aac00 | 2010-04-10 21:27:23 +0200 | [diff] [blame] | 1228 | static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | { |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1230 | unsigned int i; |
| 1231 | |
| 1232 | for (i = 0; i < OUTPUT_URBS; ++i) |
Takashi Iwai | 29aac00 | 2010-04-10 21:27:23 +0200 | [diff] [blame] | 1233 | if (ep->urbs[i].urb) { |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1234 | free_urb_and_buffer(ep->umidi, ep->urbs[i].urb, |
| 1235 | ep->max_transfer); |
Takashi Iwai | 29aac00 | 2010-04-10 21:27:23 +0200 | [diff] [blame] | 1236 | ep->urbs[i].urb = NULL; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep) |
| 1241 | { |
| 1242 | snd_usbmidi_out_endpoint_clear(ep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | kfree(ep); |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * Creates an output endpoint, and initializes output ports. |
| 1248 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1249 | static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi, |
| 1250 | struct snd_usb_midi_endpoint_info* ep_info, |
| 1251 | struct snd_usb_midi_endpoint* rep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1253 | struct snd_usb_midi_out_endpoint* ep; |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1254 | unsigned int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | unsigned int pipe; |
| 1256 | void* buffer; |
| 1257 | |
| 1258 | rep->out = NULL; |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 1259 | ep = kzalloc(sizeof(*ep), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | if (!ep) |
| 1261 | return -ENOMEM; |
| 1262 | ep->umidi = umidi; |
| 1263 | |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1264 | for (i = 0; i < OUTPUT_URBS; ++i) { |
| 1265 | ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1266 | if (!ep->urbs[i].urb) { |
| 1267 | snd_usbmidi_out_endpoint_delete(ep); |
| 1268 | return -ENOMEM; |
| 1269 | } |
| 1270 | ep->urbs[i].ep = ep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | } |
Clemens Ladisch | a6a712a | 2007-08-21 08:56:08 +0200 | [diff] [blame] | 1272 | if (ep_info->out_interval) |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1273 | pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep); |
Clemens Ladisch | a6a712a | 2007-08-21 08:56:08 +0200 | [diff] [blame] | 1274 | else |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1275 | pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep); |
Clemens Ladisch | f167e1d07 | 2010-02-15 08:55:28 +0100 | [diff] [blame] | 1276 | switch (umidi->usb_id) { |
| 1277 | default: |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1278 | ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1); |
Clemens Ladisch | f167e1d07 | 2010-02-15 08:55:28 +0100 | [diff] [blame] | 1279 | break; |
| 1280 | /* |
| 1281 | * Various chips declare a packet size larger than 4 bytes, but |
| 1282 | * do not actually work with larger packets: |
| 1283 | */ |
| 1284 | case USB_ID(0x0a92, 0x1020): /* ESI M4U */ |
| 1285 | case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */ |
| 1286 | case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */ |
| 1287 | case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */ |
| 1288 | case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */ |
| 1289 | ep->max_transfer = 4; |
| 1290 | break; |
| 1291 | } |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1292 | for (i = 0; i < OUTPUT_URBS; ++i) { |
Daniel Mack | 997ea58 | 2010-04-12 13:17:25 +0200 | [diff] [blame] | 1293 | buffer = usb_alloc_coherent(umidi->dev, |
| 1294 | ep->max_transfer, GFP_KERNEL, |
| 1295 | &ep->urbs[i].urb->transfer_dma); |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1296 | if (!buffer) { |
| 1297 | snd_usbmidi_out_endpoint_delete(ep); |
| 1298 | return -ENOMEM; |
| 1299 | } |
| 1300 | if (ep_info->out_interval) |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1301 | usb_fill_int_urb(ep->urbs[i].urb, umidi->dev, |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1302 | pipe, buffer, ep->max_transfer, |
| 1303 | snd_usbmidi_out_urb_complete, |
| 1304 | &ep->urbs[i], ep_info->out_interval); |
| 1305 | else |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1306 | usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev, |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1307 | pipe, buffer, ep->max_transfer, |
| 1308 | snd_usbmidi_out_urb_complete, |
| 1309 | &ep->urbs[i]); |
| 1310 | ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
| 1313 | spin_lock_init(&ep->buffer_lock); |
| 1314 | tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep); |
Clemens Ladisch | a65dd99 | 2009-07-13 13:48:36 +0200 | [diff] [blame] | 1315 | init_waitqueue_head(&ep->drain_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | |
| 1317 | for (i = 0; i < 0x10; ++i) |
| 1318 | if (ep_info->out_cables & (1 << i)) { |
| 1319 | ep->ports[i].ep = ep; |
| 1320 | ep->ports[i].cable = i << 4; |
| 1321 | } |
| 1322 | |
| 1323 | if (umidi->usb_protocol_ops->init_out_endpoint) |
| 1324 | umidi->usb_protocol_ops->init_out_endpoint(ep); |
| 1325 | |
| 1326 | rep->out = ep; |
| 1327 | return 0; |
| 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | * Frees everything. |
| 1332 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1333 | static void snd_usbmidi_free(struct snd_usb_midi* umidi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | { |
| 1335 | int i; |
| 1336 | |
| 1337 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1338 | struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | if (ep->out) |
| 1340 | snd_usbmidi_out_endpoint_delete(ep->out); |
| 1341 | if (ep->in) |
| 1342 | snd_usbmidi_in_endpoint_delete(ep->in); |
| 1343 | } |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1344 | mutex_destroy(&umidi->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | kfree(umidi); |
| 1346 | } |
| 1347 | |
| 1348 | /* |
| 1349 | * Unlinks all URBs (must be done before the usb_device is deleted). |
| 1350 | */ |
Clemens Ladisch | ee73339 | 2005-04-25 10:34:13 +0200 | [diff] [blame] | 1351 | void snd_usbmidi_disconnect(struct list_head* p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1353 | struct snd_usb_midi* umidi; |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 1354 | unsigned int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1356 | umidi = list_entry(p, struct snd_usb_midi, list); |
Takashi Iwai | c0792e0 | 2008-02-22 18:34:44 +0100 | [diff] [blame] | 1357 | /* |
| 1358 | * an URB's completion handler may start the timer and |
| 1359 | * a timer may submit an URB. To reliably break the cycle |
| 1360 | * a flag under lock must be used |
| 1361 | */ |
| 1362 | spin_lock_irq(&umidi->disc_lock); |
| 1363 | umidi->disconnected = 1; |
| 1364 | spin_unlock_irq(&umidi->disc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1365 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1366 | struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 1367 | if (ep->out) |
| 1368 | tasklet_kill(&ep->out->tasklet); |
Clemens Ladisch | ed4affa | 2009-07-13 13:43:59 +0200 | [diff] [blame] | 1369 | if (ep->out) { |
| 1370 | for (j = 0; j < OUTPUT_URBS; ++j) |
| 1371 | usb_kill_urb(ep->out->urbs[j].urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | if (umidi->usb_protocol_ops->finish_out_endpoint) |
| 1373 | umidi->usb_protocol_ops->finish_out_endpoint(ep->out); |
Takashi Iwai | 29aac00 | 2010-04-10 21:27:23 +0200 | [diff] [blame] | 1374 | ep->out->active_urbs = 0; |
| 1375 | if (ep->out->drain_urbs) { |
| 1376 | ep->out->drain_urbs = 0; |
| 1377 | wake_up(&ep->out->drain_wait); |
| 1378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | } |
Mariusz Kozlowski | f5e135a | 2006-11-08 15:37:00 +0100 | [diff] [blame] | 1380 | if (ep->in) |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 1381 | for (j = 0; j < INPUT_URBS; ++j) |
| 1382 | usb_kill_urb(ep->in->urbs[j]); |
Takashi Iwai | 7a17daa | 2008-10-02 14:50:22 +0200 | [diff] [blame] | 1383 | /* free endpoints here; later call can result in Oops */ |
Takashi Iwai | 29aac00 | 2010-04-10 21:27:23 +0200 | [diff] [blame] | 1384 | if (ep->out) |
| 1385 | snd_usbmidi_out_endpoint_clear(ep->out); |
Takashi Iwai | 7a17daa | 2008-10-02 14:50:22 +0200 | [diff] [blame] | 1386 | if (ep->in) { |
| 1387 | snd_usbmidi_in_endpoint_delete(ep->in); |
| 1388 | ep->in = NULL; |
| 1389 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1390 | } |
Takashi Iwai | c0792e0 | 2008-02-22 18:34:44 +0100 | [diff] [blame] | 1391 | del_timer_sync(&umidi->error_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1394 | static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1396 | struct snd_usb_midi* umidi = rmidi->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | snd_usbmidi_free(umidi); |
| 1398 | } |
| 1399 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1400 | static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | int stream, int number) |
| 1402 | { |
| 1403 | struct list_head* list; |
| 1404 | |
| 1405 | list_for_each(list, &umidi->rmidi->streams[stream].substreams) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1406 | struct snd_rawmidi_substream *substream = list_entry(list, struct snd_rawmidi_substream, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | if (substream->number == number) |
| 1408 | return substream; |
| 1409 | } |
| 1410 | return NULL; |
| 1411 | } |
| 1412 | |
| 1413 | /* |
| 1414 | * This list specifies names for ports that do not fit into the standard |
| 1415 | * "(product) MIDI (n)" schema because they aren't external MIDI ports, |
| 1416 | * such as internal control or synthesizer ports. |
| 1417 | */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1418 | static struct port_info { |
Clemens Ladisch | 27d10f5 | 2005-05-02 08:51:26 +0200 | [diff] [blame] | 1419 | u32 id; |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1420 | short int port; |
| 1421 | short int voices; |
| 1422 | const char *name; |
| 1423 | unsigned int seq_flags; |
| 1424 | } snd_usbmidi_port_info[] = { |
| 1425 | #define PORT_INFO(vendor, product, num, name_, voices_, flags) \ |
| 1426 | { .id = USB_ID(vendor, product), \ |
| 1427 | .port = num, .voices = voices_, \ |
| 1428 | .name = name_, .seq_flags = flags } |
| 1429 | #define EXTERNAL_PORT(vendor, product, num, name) \ |
| 1430 | PORT_INFO(vendor, product, num, name, 0, \ |
| 1431 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ |
| 1432 | SNDRV_SEQ_PORT_TYPE_HARDWARE | \ |
| 1433 | SNDRV_SEQ_PORT_TYPE_PORT) |
| 1434 | #define CONTROL_PORT(vendor, product, num, name) \ |
| 1435 | PORT_INFO(vendor, product, num, name, 0, \ |
| 1436 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ |
| 1437 | SNDRV_SEQ_PORT_TYPE_HARDWARE) |
| 1438 | #define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \ |
| 1439 | PORT_INFO(vendor, product, num, name, voices, \ |
| 1440 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ |
| 1441 | SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ |
| 1442 | SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ |
| 1443 | SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ |
| 1444 | SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ |
| 1445 | SNDRV_SEQ_PORT_TYPE_HARDWARE | \ |
| 1446 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) |
| 1447 | #define SOUNDCANVAS_PORT(vendor, product, num, name, voices) \ |
| 1448 | PORT_INFO(vendor, product, num, name, voices, \ |
| 1449 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \ |
| 1450 | SNDRV_SEQ_PORT_TYPE_MIDI_GM | \ |
| 1451 | SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \ |
| 1452 | SNDRV_SEQ_PORT_TYPE_MIDI_GS | \ |
| 1453 | SNDRV_SEQ_PORT_TYPE_MIDI_XG | \ |
| 1454 | SNDRV_SEQ_PORT_TYPE_MIDI_MT32 | \ |
| 1455 | SNDRV_SEQ_PORT_TYPE_HARDWARE | \ |
| 1456 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | /* Roland UA-100 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1458 | CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | /* Roland SC-8850 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1460 | SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128), |
| 1461 | SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128), |
| 1462 | SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128), |
| 1463 | SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128), |
| 1464 | EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"), |
| 1465 | EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | /* Roland U-8 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1467 | EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"), |
| 1468 | CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | /* Roland SC-8820 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1470 | SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64), |
| 1471 | SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64), |
| 1472 | EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | /* Roland SK-500 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1474 | SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64), |
| 1475 | SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64), |
| 1476 | EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | /* Roland SC-D70 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1478 | SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64), |
| 1479 | SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64), |
| 1480 | EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | /* Edirol UM-880 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1482 | CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | /* Edirol SD-90 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1484 | ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128), |
| 1485 | ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128), |
| 1486 | EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"), |
| 1487 | EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | /* Edirol UM-550 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1489 | CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | /* Edirol SD-20 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1491 | ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64), |
| 1492 | ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64), |
| 1493 | EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1494 | /* Edirol SD-80 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1495 | ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128), |
| 1496 | ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128), |
| 1497 | EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"), |
| 1498 | EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | /* Edirol UA-700 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1500 | EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"), |
| 1501 | CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | /* Roland VariOS */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1503 | EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"), |
| 1504 | EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"), |
| 1505 | EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | /* Edirol PCR */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1507 | EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"), |
| 1508 | EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"), |
| 1509 | EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | /* BOSS GS-10 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1511 | EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"), |
| 1512 | CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | /* Edirol UA-1000 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1514 | EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"), |
| 1515 | CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | /* Edirol UR-80 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1517 | EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"), |
| 1518 | EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"), |
| 1519 | EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | /* Edirol PCR-A */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1521 | EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"), |
| 1522 | EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"), |
| 1523 | EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"), |
Clemens Ladisch | 7c79b76 | 2006-01-10 18:56:23 +0100 | [diff] [blame] | 1524 | /* Edirol UM-3EX */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1525 | CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | /* M-Audio MidiSport 8x8 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1527 | CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"), |
| 1528 | CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | /* MOTU Fastlane */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1530 | EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"), |
| 1531 | EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | /* Emagic Unitor8/AMT8/MT4 */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1533 | EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"), |
| 1534 | EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"), |
| 1535 | EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"), |
Krzysztof Foltman | 4434ade | 2010-05-20 20:31:10 +0100 | [diff] [blame] | 1536 | /* Akai MPD16 */ |
| 1537 | CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"), |
| 1538 | PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0, |
| 1539 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | |
| 1540 | SNDRV_SEQ_PORT_TYPE_HARDWARE), |
Sebastien Alaiwan | d39e82d | 2010-02-16 08:55:08 +0100 | [diff] [blame] | 1541 | /* Access Music Virus TI */ |
| 1542 | EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"), |
| 1543 | PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0, |
| 1544 | SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | |
| 1545 | SNDRV_SEQ_PORT_TYPE_HARDWARE | |
| 1546 | SNDRV_SEQ_PORT_TYPE_SYNTHESIZER), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | }; |
| 1548 | |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1549 | static struct port_info *find_port_info(struct snd_usb_midi* umidi, int number) |
| 1550 | { |
| 1551 | int i; |
| 1552 | |
| 1553 | for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) { |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1554 | if (snd_usbmidi_port_info[i].id == umidi->usb_id && |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1555 | snd_usbmidi_port_info[i].port == number) |
| 1556 | return &snd_usbmidi_port_info[i]; |
| 1557 | } |
| 1558 | return NULL; |
| 1559 | } |
| 1560 | |
| 1561 | static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number, |
| 1562 | struct snd_seq_port_info *seq_port_info) |
| 1563 | { |
| 1564 | struct snd_usb_midi *umidi = rmidi->private_data; |
| 1565 | struct port_info *port_info; |
| 1566 | |
| 1567 | /* TODO: read port flags from descriptors */ |
| 1568 | port_info = find_port_info(umidi, number); |
| 1569 | if (port_info) { |
| 1570 | seq_port_info->type = port_info->seq_flags; |
| 1571 | seq_port_info->midi_voices = port_info->voices; |
| 1572 | } |
| 1573 | } |
| 1574 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1575 | static void snd_usbmidi_init_substream(struct snd_usb_midi* umidi, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | int stream, int number, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1577 | struct snd_rawmidi_substream ** rsubstream) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | { |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1579 | struct port_info *port_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | const char *name_format; |
| 1581 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1582 | struct snd_rawmidi_substream *substream = snd_usbmidi_find_substream(umidi, stream, number); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | if (!substream) { |
| 1584 | snd_printd(KERN_ERR "substream %d:%d not found\n", stream, number); |
| 1585 | return; |
| 1586 | } |
| 1587 | |
| 1588 | /* TODO: read port name from jack descriptor */ |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1589 | port_info = find_port_info(umidi, number); |
| 1590 | name_format = port_info ? port_info->name : "%s MIDI %d"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | snprintf(substream->name, sizeof(substream->name), |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1592 | name_format, umidi->card->shortname, number + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | |
| 1594 | *rsubstream = substream; |
| 1595 | } |
| 1596 | |
| 1597 | /* |
| 1598 | * Creates the endpoints and their ports. |
| 1599 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1600 | static int snd_usbmidi_create_endpoints(struct snd_usb_midi* umidi, |
| 1601 | struct snd_usb_midi_endpoint_info* endpoints) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | { |
| 1603 | int i, j, err; |
| 1604 | int out_ports = 0, in_ports = 0; |
| 1605 | |
| 1606 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
| 1607 | if (endpoints[i].out_cables) { |
| 1608 | err = snd_usbmidi_out_endpoint_create(umidi, &endpoints[i], |
| 1609 | &umidi->endpoints[i]); |
| 1610 | if (err < 0) |
| 1611 | return err; |
| 1612 | } |
| 1613 | if (endpoints[i].in_cables) { |
| 1614 | err = snd_usbmidi_in_endpoint_create(umidi, &endpoints[i], |
| 1615 | &umidi->endpoints[i]); |
| 1616 | if (err < 0) |
| 1617 | return err; |
| 1618 | } |
| 1619 | |
| 1620 | for (j = 0; j < 0x10; ++j) { |
| 1621 | if (endpoints[i].out_cables & (1 << j)) { |
| 1622 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, out_ports, |
| 1623 | &umidi->endpoints[i].out->ports[j].substream); |
| 1624 | ++out_ports; |
| 1625 | } |
| 1626 | if (endpoints[i].in_cables & (1 << j)) { |
| 1627 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, in_ports, |
| 1628 | &umidi->endpoints[i].in->ports[j].substream); |
| 1629 | ++in_ports; |
| 1630 | } |
| 1631 | } |
| 1632 | } |
| 1633 | snd_printdd(KERN_INFO "created %d output and %d input ports\n", |
| 1634 | out_ports, in_ports); |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
| 1638 | /* |
| 1639 | * Returns MIDIStreaming device capabilities. |
| 1640 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1641 | static int snd_usbmidi_get_ms_info(struct snd_usb_midi* umidi, |
| 1642 | struct snd_usb_midi_endpoint_info* endpoints) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | { |
| 1644 | struct usb_interface* intf; |
| 1645 | struct usb_host_interface *hostif; |
| 1646 | struct usb_interface_descriptor* intfd; |
| 1647 | struct usb_ms_header_descriptor* ms_header; |
| 1648 | struct usb_host_endpoint *hostep; |
| 1649 | struct usb_endpoint_descriptor* ep; |
| 1650 | struct usb_ms_endpoint_descriptor* ms_ep; |
| 1651 | int i, epidx; |
| 1652 | |
| 1653 | intf = umidi->iface; |
| 1654 | if (!intf) |
| 1655 | return -ENXIO; |
| 1656 | hostif = &intf->altsetting[0]; |
| 1657 | intfd = get_iface_desc(hostif); |
| 1658 | ms_header = (struct usb_ms_header_descriptor*)hostif->extra; |
| 1659 | if (hostif->extralen >= 7 && |
| 1660 | ms_header->bLength >= 7 && |
| 1661 | ms_header->bDescriptorType == USB_DT_CS_INTERFACE && |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1662 | ms_header->bDescriptorSubtype == UAC_HEADER) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | snd_printdd(KERN_INFO "MIDIStreaming version %02x.%02x\n", |
| 1664 | ms_header->bcdMSC[1], ms_header->bcdMSC[0]); |
| 1665 | else |
| 1666 | snd_printk(KERN_WARNING "MIDIStreaming interface descriptor not found\n"); |
| 1667 | |
| 1668 | epidx = 0; |
| 1669 | for (i = 0; i < intfd->bNumEndpoints; ++i) { |
| 1670 | hostep = &hostif->endpoint[i]; |
| 1671 | ep = get_ep_desc(hostep); |
Julia Lawall | 913ae5a | 2009-01-03 17:54:53 +0100 | [diff] [blame] | 1672 | if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1673 | continue; |
| 1674 | ms_ep = (struct usb_ms_endpoint_descriptor*)hostep->extra; |
| 1675 | if (hostep->extralen < 4 || |
| 1676 | ms_ep->bLength < 4 || |
| 1677 | ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT || |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1678 | ms_ep->bDescriptorSubtype != UAC_MS_GENERAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | continue; |
Julia Lawall | 42a6e66 | 2008-12-29 11:23:02 +0100 | [diff] [blame] | 1680 | if (usb_endpoint_dir_out(ep)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | if (endpoints[epidx].out_ep) { |
| 1682 | if (++epidx >= MIDI_MAX_ENDPOINTS) { |
| 1683 | snd_printk(KERN_WARNING "too many endpoints\n"); |
| 1684 | break; |
| 1685 | } |
| 1686 | } |
Julia Lawall | 42a6e66 | 2008-12-29 11:23:02 +0100 | [diff] [blame] | 1687 | endpoints[epidx].out_ep = usb_endpoint_num(ep); |
| 1688 | if (usb_endpoint_xfer_int(ep)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | endpoints[epidx].out_interval = ep->bInterval; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1690 | else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) |
Clemens Ladisch | 56162aa | 2007-08-21 08:57:34 +0200 | [diff] [blame] | 1691 | /* |
| 1692 | * Low speed bulk transfers don't exist, so |
| 1693 | * force interrupt transfers for devices like |
| 1694 | * ESI MIDI Mate that try to use them anyway. |
| 1695 | */ |
| 1696 | endpoints[epidx].out_interval = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | endpoints[epidx].out_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; |
| 1698 | snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", |
| 1699 | ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); |
| 1700 | } else { |
| 1701 | if (endpoints[epidx].in_ep) { |
| 1702 | if (++epidx >= MIDI_MAX_ENDPOINTS) { |
| 1703 | snd_printk(KERN_WARNING "too many endpoints\n"); |
| 1704 | break; |
| 1705 | } |
| 1706 | } |
Julia Lawall | 42a6e66 | 2008-12-29 11:23:02 +0100 | [diff] [blame] | 1707 | endpoints[epidx].in_ep = usb_endpoint_num(ep); |
| 1708 | if (usb_endpoint_xfer_int(ep)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1709 | endpoints[epidx].in_interval = ep->bInterval; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1710 | else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW) |
Clemens Ladisch | 56162aa | 2007-08-21 08:57:34 +0200 | [diff] [blame] | 1711 | endpoints[epidx].in_interval = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | endpoints[epidx].in_cables = (1 << ms_ep->bNumEmbMIDIJack) - 1; |
| 1713 | snd_printdd(KERN_INFO "EP %02X: %d jack(s)\n", |
| 1714 | ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack); |
| 1715 | } |
| 1716 | } |
| 1717 | return 0; |
| 1718 | } |
| 1719 | |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1720 | static int roland_load_info(struct snd_kcontrol *kcontrol, |
| 1721 | struct snd_ctl_elem_info *info) |
| 1722 | { |
| 1723 | static const char *const names[] = { "High Load", "Light Load" }; |
| 1724 | |
| 1725 | info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1726 | info->count = 1; |
| 1727 | info->value.enumerated.items = 2; |
| 1728 | if (info->value.enumerated.item > 1) |
| 1729 | info->value.enumerated.item = 1; |
| 1730 | strcpy(info->value.enumerated.name, names[info->value.enumerated.item]); |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | static int roland_load_get(struct snd_kcontrol *kcontrol, |
| 1735 | struct snd_ctl_elem_value *value) |
| 1736 | { |
| 1737 | value->value.enumerated.item[0] = kcontrol->private_value; |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
| 1741 | static int roland_load_put(struct snd_kcontrol *kcontrol, |
| 1742 | struct snd_ctl_elem_value *value) |
| 1743 | { |
| 1744 | struct snd_usb_midi* umidi = kcontrol->private_data; |
| 1745 | int changed; |
| 1746 | |
| 1747 | if (value->value.enumerated.item[0] > 1) |
| 1748 | return -EINVAL; |
| 1749 | mutex_lock(&umidi->mutex); |
| 1750 | changed = value->value.enumerated.item[0] != kcontrol->private_value; |
| 1751 | if (changed) |
| 1752 | kcontrol->private_value = value->value.enumerated.item[0]; |
| 1753 | mutex_unlock(&umidi->mutex); |
| 1754 | return changed; |
| 1755 | } |
| 1756 | |
| 1757 | static struct snd_kcontrol_new roland_load_ctl = { |
| 1758 | .iface = SNDRV_CTL_ELEM_IFACE_MIXER, |
| 1759 | .name = "MIDI Input Mode", |
| 1760 | .info = roland_load_info, |
| 1761 | .get = roland_load_get, |
| 1762 | .put = roland_load_put, |
| 1763 | .private_value = 1, |
| 1764 | }; |
| 1765 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | /* |
| 1767 | * On Roland devices, use the second alternate setting to be able to use |
| 1768 | * the interrupt input endpoint. |
| 1769 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1770 | static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi* umidi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | { |
| 1772 | struct usb_interface* intf; |
| 1773 | struct usb_host_interface *hostif; |
| 1774 | struct usb_interface_descriptor* intfd; |
| 1775 | |
| 1776 | intf = umidi->iface; |
| 1777 | if (!intf || intf->num_altsetting != 2) |
| 1778 | return; |
| 1779 | |
| 1780 | hostif = &intf->altsetting[1]; |
| 1781 | intfd = get_iface_desc(hostif); |
| 1782 | if (intfd->bNumEndpoints != 2 || |
| 1783 | (get_endpoint(hostif, 0)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK || |
| 1784 | (get_endpoint(hostif, 1)->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT) |
| 1785 | return; |
| 1786 | |
| 1787 | snd_printdd(KERN_INFO "switching to altsetting %d with int ep\n", |
| 1788 | intfd->bAlternateSetting); |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1789 | usb_set_interface(umidi->dev, intfd->bInterfaceNumber, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | intfd->bAlternateSetting); |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1791 | |
| 1792 | umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi); |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1793 | if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0) |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 1794 | umidi->roland_load_ctl = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | /* |
| 1798 | * Try to find any usable endpoints in the interface. |
| 1799 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1800 | static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi, |
| 1801 | struct snd_usb_midi_endpoint_info* endpoint, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | int max_endpoints) |
| 1803 | { |
| 1804 | struct usb_interface* intf; |
| 1805 | struct usb_host_interface *hostif; |
| 1806 | struct usb_interface_descriptor* intfd; |
| 1807 | struct usb_endpoint_descriptor* epd; |
| 1808 | int i, out_eps = 0, in_eps = 0; |
| 1809 | |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1810 | if (USB_ID_VENDOR(umidi->usb_id) == 0x0582) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1811 | snd_usbmidi_switch_roland_altsetting(umidi); |
| 1812 | |
Clemens Ladisch | c1ab5d5 | 2005-03-30 16:22:01 +0200 | [diff] [blame] | 1813 | if (endpoint[0].out_ep || endpoint[0].in_ep) |
| 1814 | return 0; |
| 1815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | intf = umidi->iface; |
| 1817 | if (!intf || intf->num_altsetting < 1) |
| 1818 | return -ENOENT; |
| 1819 | hostif = intf->cur_altsetting; |
| 1820 | intfd = get_iface_desc(hostif); |
| 1821 | |
| 1822 | for (i = 0; i < intfd->bNumEndpoints; ++i) { |
| 1823 | epd = get_endpoint(hostif, i); |
Julia Lawall | 913ae5a | 2009-01-03 17:54:53 +0100 | [diff] [blame] | 1824 | if (!usb_endpoint_xfer_bulk(epd) && |
| 1825 | !usb_endpoint_xfer_int(epd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | continue; |
| 1827 | if (out_eps < max_endpoints && |
Julia Lawall | 42a6e66 | 2008-12-29 11:23:02 +0100 | [diff] [blame] | 1828 | usb_endpoint_dir_out(epd)) { |
| 1829 | endpoint[out_eps].out_ep = usb_endpoint_num(epd); |
| 1830 | if (usb_endpoint_xfer_int(epd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | endpoint[out_eps].out_interval = epd->bInterval; |
| 1832 | ++out_eps; |
| 1833 | } |
| 1834 | if (in_eps < max_endpoints && |
Julia Lawall | 42a6e66 | 2008-12-29 11:23:02 +0100 | [diff] [blame] | 1835 | usb_endpoint_dir_in(epd)) { |
| 1836 | endpoint[in_eps].in_ep = usb_endpoint_num(epd); |
| 1837 | if (usb_endpoint_xfer_int(epd)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1838 | endpoint[in_eps].in_interval = epd->bInterval; |
| 1839 | ++in_eps; |
| 1840 | } |
| 1841 | } |
| 1842 | return (out_eps || in_eps) ? 0 : -ENOENT; |
| 1843 | } |
| 1844 | |
| 1845 | /* |
| 1846 | * Detects the endpoints for one-port-per-endpoint protocols. |
| 1847 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1848 | static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi, |
| 1849 | struct snd_usb_midi_endpoint_info* endpoints) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | { |
| 1851 | int err, i; |
| 1852 | |
| 1853 | err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS); |
| 1854 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
| 1855 | if (endpoints[i].out_ep) |
| 1856 | endpoints[i].out_cables = 0x0001; |
| 1857 | if (endpoints[i].in_ep) |
| 1858 | endpoints[i].in_cables = 0x0001; |
| 1859 | } |
| 1860 | return err; |
| 1861 | } |
| 1862 | |
| 1863 | /* |
| 1864 | * Detects the endpoints and ports of Yamaha devices. |
| 1865 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1866 | static int snd_usbmidi_detect_yamaha(struct snd_usb_midi* umidi, |
| 1867 | struct snd_usb_midi_endpoint_info* endpoint) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | { |
| 1869 | struct usb_interface* intf; |
| 1870 | struct usb_host_interface *hostif; |
| 1871 | struct usb_interface_descriptor* intfd; |
| 1872 | uint8_t* cs_desc; |
| 1873 | |
| 1874 | intf = umidi->iface; |
| 1875 | if (!intf) |
| 1876 | return -ENOENT; |
| 1877 | hostif = intf->altsetting; |
| 1878 | intfd = get_iface_desc(hostif); |
| 1879 | if (intfd->bNumEndpoints < 1) |
| 1880 | return -ENOENT; |
| 1881 | |
| 1882 | /* |
| 1883 | * For each port there is one MIDI_IN/OUT_JACK descriptor, not |
| 1884 | * necessarily with any useful contents. So simply count 'em. |
| 1885 | */ |
| 1886 | for (cs_desc = hostif->extra; |
| 1887 | cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2; |
| 1888 | cs_desc += cs_desc[0]) { |
Ben Williamson | c4a87ef | 2006-06-19 17:20:09 +0200 | [diff] [blame] | 1889 | if (cs_desc[1] == USB_DT_CS_INTERFACE) { |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1890 | if (cs_desc[2] == UAC_MIDI_IN_JACK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1891 | endpoint->in_cables = (endpoint->in_cables << 1) | 1; |
Daniel Mack | de48c7b | 2010-02-22 23:49:13 +0100 | [diff] [blame] | 1892 | else if (cs_desc[2] == UAC_MIDI_OUT_JACK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | endpoint->out_cables = (endpoint->out_cables << 1) | 1; |
| 1894 | } |
| 1895 | } |
| 1896 | if (!endpoint->in_cables && !endpoint->out_cables) |
| 1897 | return -ENOENT; |
| 1898 | |
| 1899 | return snd_usbmidi_detect_endpoints(umidi, endpoint, 1); |
| 1900 | } |
| 1901 | |
| 1902 | /* |
| 1903 | * Creates the endpoints and their ports for Midiman devices. |
| 1904 | */ |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1905 | static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi* umidi, |
| 1906 | struct snd_usb_midi_endpoint_info* endpoint) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1908 | struct snd_usb_midi_endpoint_info ep_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | struct usb_interface* intf; |
| 1910 | struct usb_host_interface *hostif; |
| 1911 | struct usb_interface_descriptor* intfd; |
| 1912 | struct usb_endpoint_descriptor* epd; |
| 1913 | int cable, err; |
| 1914 | |
| 1915 | intf = umidi->iface; |
| 1916 | if (!intf) |
| 1917 | return -ENOENT; |
| 1918 | hostif = intf->altsetting; |
| 1919 | intfd = get_iface_desc(hostif); |
| 1920 | /* |
| 1921 | * The various MidiSport devices have more or less random endpoint |
| 1922 | * numbers, so we have to identify the endpoints by their index in |
| 1923 | * the descriptor array, like the driver for that other OS does. |
| 1924 | * |
| 1925 | * There is one interrupt input endpoint for all input ports, one |
| 1926 | * bulk output endpoint for even-numbered ports, and one for odd- |
| 1927 | * numbered ports. Both bulk output endpoints have corresponding |
| 1928 | * input bulk endpoints (at indices 1 and 3) which aren't used. |
| 1929 | */ |
| 1930 | if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) { |
| 1931 | snd_printdd(KERN_ERR "not enough endpoints\n"); |
| 1932 | return -ENOENT; |
| 1933 | } |
| 1934 | |
| 1935 | epd = get_endpoint(hostif, 0); |
Julia Lawall | 913ae5a | 2009-01-03 17:54:53 +0100 | [diff] [blame] | 1936 | if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | snd_printdd(KERN_ERR "endpoint[0] isn't interrupt\n"); |
| 1938 | return -ENXIO; |
| 1939 | } |
| 1940 | epd = get_endpoint(hostif, 2); |
Julia Lawall | 913ae5a | 2009-01-03 17:54:53 +0100 | [diff] [blame] | 1941 | if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | snd_printdd(KERN_ERR "endpoint[2] isn't bulk output\n"); |
| 1943 | return -ENXIO; |
| 1944 | } |
| 1945 | if (endpoint->out_cables > 0x0001) { |
| 1946 | epd = get_endpoint(hostif, 4); |
Julia Lawall | 913ae5a | 2009-01-03 17:54:53 +0100 | [diff] [blame] | 1947 | if (!usb_endpoint_dir_out(epd) || |
| 1948 | !usb_endpoint_xfer_bulk(epd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | snd_printdd(KERN_ERR "endpoint[4] isn't bulk output\n"); |
| 1950 | return -ENXIO; |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
Clemens Ladisch | e156ac4 | 2009-02-16 15:22:39 +0100 | [diff] [blame] | 1955 | ep_info.out_interval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | ep_info.out_cables = endpoint->out_cables & 0x5555; |
| 1957 | err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); |
| 1958 | if (err < 0) |
| 1959 | return err; |
| 1960 | |
| 1961 | ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 1962 | ep_info.in_interval = get_endpoint(hostif, 0)->bInterval; |
| 1963 | ep_info.in_cables = endpoint->in_cables; |
| 1964 | err = snd_usbmidi_in_endpoint_create(umidi, &ep_info, &umidi->endpoints[0]); |
| 1965 | if (err < 0) |
| 1966 | return err; |
| 1967 | |
| 1968 | if (endpoint->out_cables > 0x0001) { |
| 1969 | ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK; |
| 1970 | ep_info.out_cables = endpoint->out_cables & 0xaaaa; |
| 1971 | err = snd_usbmidi_out_endpoint_create(umidi, &ep_info, &umidi->endpoints[1]); |
| 1972 | if (err < 0) |
| 1973 | return err; |
| 1974 | } |
| 1975 | |
| 1976 | for (cable = 0; cable < 0x10; ++cable) { |
| 1977 | if (endpoint->out_cables & (1 << cable)) |
| 1978 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_OUTPUT, cable, |
| 1979 | &umidi->endpoints[cable & 1].out->ports[cable].substream); |
| 1980 | if (endpoint->in_cables & (1 << cable)) |
| 1981 | snd_usbmidi_init_substream(umidi, SNDRV_RAWMIDI_STREAM_INPUT, cable, |
| 1982 | &umidi->endpoints[0].in->ports[cable].substream); |
| 1983 | } |
| 1984 | return 0; |
| 1985 | } |
| 1986 | |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 1987 | static struct snd_rawmidi_global_ops snd_usbmidi_ops = { |
| 1988 | .get_port_info = snd_usbmidi_get_port_info, |
| 1989 | }; |
| 1990 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1991 | static int snd_usbmidi_create_rawmidi(struct snd_usb_midi* umidi, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | int out_ports, int in_ports) |
| 1993 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 1994 | struct snd_rawmidi *rmidi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | int err; |
| 1996 | |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 1997 | err = snd_rawmidi_new(umidi->card, "USB MIDI", |
| 1998 | umidi->next_midi_device++, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | out_ports, in_ports, &rmidi); |
| 2000 | if (err < 0) |
| 2001 | return err; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2002 | strcpy(rmidi->name, umidi->card->shortname); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT | |
| 2004 | SNDRV_RAWMIDI_INFO_INPUT | |
| 2005 | SNDRV_RAWMIDI_INFO_DUPLEX; |
Clemens Ladisch | a7b928a | 2006-05-02 16:22:12 +0200 | [diff] [blame] | 2006 | rmidi->ops = &snd_usbmidi_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | rmidi->private_data = umidi; |
| 2008 | rmidi->private_free = snd_usbmidi_rawmidi_free; |
| 2009 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT, &snd_usbmidi_output_ops); |
| 2010 | snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT, &snd_usbmidi_input_ops); |
| 2011 | |
| 2012 | umidi->rmidi = rmidi; |
| 2013 | return 0; |
| 2014 | } |
| 2015 | |
| 2016 | /* |
| 2017 | * Temporarily stop input. |
| 2018 | */ |
| 2019 | void snd_usbmidi_input_stop(struct list_head* p) |
| 2020 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2021 | struct snd_usb_midi* umidi; |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 2022 | unsigned int i, j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2024 | umidi = list_entry(p, struct snd_usb_midi, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2026 | struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | if (ep->in) |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 2028 | for (j = 0; j < INPUT_URBS; ++j) |
| 2029 | usb_kill_urb(ep->in->urbs[j]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | } |
| 2031 | } |
| 2032 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2033 | static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint* ep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2034 | { |
Clemens Ladisch | 4773d1f | 2009-07-13 13:40:36 +0200 | [diff] [blame] | 2035 | unsigned int i; |
| 2036 | |
| 2037 | if (!ep) |
| 2038 | return; |
| 2039 | for (i = 0; i < INPUT_URBS; ++i) { |
| 2040 | struct urb* urb = ep->urbs[i]; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2041 | urb->dev = ep->umidi->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | snd_usbmidi_submit_urb(urb, GFP_KERNEL); |
| 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | /* |
| 2047 | * Resume input after a call to snd_usbmidi_input_stop(). |
| 2048 | */ |
| 2049 | void snd_usbmidi_input_start(struct list_head* p) |
| 2050 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2051 | struct snd_usb_midi* umidi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | int i; |
| 2053 | |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2054 | umidi = list_entry(p, struct snd_usb_midi, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) |
| 2056 | snd_usbmidi_input_start_ep(umidi->endpoints[i].in); |
| 2057 | } |
| 2058 | |
| 2059 | /* |
| 2060 | * Creates and registers everything needed for a MIDI streaming interface. |
| 2061 | */ |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2062 | int snd_usbmidi_create(struct snd_card *card, |
| 2063 | struct usb_interface* iface, |
| 2064 | struct list_head *midi_list, |
| 2065 | const struct snd_usb_audio_quirk* quirk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | { |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2067 | struct snd_usb_midi* umidi; |
| 2068 | struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | int out_ports, in_ports; |
| 2070 | int i, err; |
| 2071 | |
Takashi Iwai | 561b220 | 2005-09-09 14:22:34 +0200 | [diff] [blame] | 2072 | umidi = kzalloc(sizeof(*umidi), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | if (!umidi) |
| 2074 | return -ENOMEM; |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2075 | umidi->dev = interface_to_usbdev(iface); |
| 2076 | umidi->card = card; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | umidi->iface = iface; |
| 2078 | umidi->quirk = quirk; |
| 2079 | umidi->usb_protocol_ops = &snd_usbmidi_standard_ops; |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 2080 | init_timer(&umidi->error_timer); |
Takashi Iwai | c0792e0 | 2008-02-22 18:34:44 +0100 | [diff] [blame] | 2081 | spin_lock_init(&umidi->disc_lock); |
Clemens Ladisch | 96f61d9 | 2009-10-22 09:06:19 +0200 | [diff] [blame] | 2082 | mutex_init(&umidi->mutex); |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2083 | umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor), |
| 2084 | le16_to_cpu(umidi->dev->descriptor.idProduct)); |
Clemens Ladisch | c884697 | 2005-08-02 15:26:52 +0200 | [diff] [blame] | 2085 | umidi->error_timer.function = snd_usbmidi_error_timer; |
| 2086 | umidi->error_timer.data = (unsigned long)umidi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | |
| 2088 | /* detect the endpoint(s) to use */ |
| 2089 | memset(endpoints, 0, sizeof(endpoints)); |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2090 | switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) { |
| 2091 | case QUIRK_MIDI_STANDARD_INTERFACE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | err = snd_usbmidi_get_ms_info(umidi, endpoints); |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2093 | if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */ |
Clemens Ladisch | d05cc1043 | 2007-05-07 09:28:53 +0200 | [diff] [blame] | 2094 | umidi->usb_protocol_ops = |
| 2095 | &snd_usbmidi_maudio_broken_running_status_ops; |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2096 | break; |
Karsten Wiese | 030a07e | 2008-07-30 15:13:29 +0200 | [diff] [blame] | 2097 | case QUIRK_MIDI_US122L: |
| 2098 | umidi->usb_protocol_ops = &snd_usbmidi_122l_ops; |
| 2099 | /* fall through */ |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2100 | case QUIRK_MIDI_FIXED_ENDPOINT: |
| 2101 | memcpy(&endpoints[0], quirk->data, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2102 | sizeof(struct snd_usb_midi_endpoint_info)); |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2103 | err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); |
| 2104 | break; |
| 2105 | case QUIRK_MIDI_YAMAHA: |
| 2106 | err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]); |
| 2107 | break; |
| 2108 | case QUIRK_MIDI_MIDIMAN: |
| 2109 | umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops; |
| 2110 | memcpy(&endpoints[0], quirk->data, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2111 | sizeof(struct snd_usb_midi_endpoint_info)); |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2112 | err = 0; |
| 2113 | break; |
| 2114 | case QUIRK_MIDI_NOVATION: |
| 2115 | umidi->usb_protocol_ops = &snd_usbmidi_novation_ops; |
| 2116 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); |
| 2117 | break; |
Clemens Ladisch | 55de5ef | 2009-05-27 10:49:30 +0200 | [diff] [blame] | 2118 | case QUIRK_MIDI_FASTLANE: |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2119 | umidi->usb_protocol_ops = &snd_usbmidi_raw_ops; |
Clemens Ladisch | 55de5ef | 2009-05-27 10:49:30 +0200 | [diff] [blame] | 2120 | /* |
| 2121 | * Interface 1 contains isochronous endpoints, but with the same |
| 2122 | * numbers as in interface 0. Since it is interface 1 that the |
| 2123 | * USB core has most recently seen, these descriptors are now |
| 2124 | * associated with the endpoint numbers. This will foul up our |
| 2125 | * attempts to submit bulk/interrupt URBs to the endpoints in |
| 2126 | * interface 0, so we have to make sure that the USB core looks |
| 2127 | * again at interface 0 by calling usb_set_interface() on it. |
| 2128 | */ |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2129 | usb_set_interface(umidi->dev, 0, 0); |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2130 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); |
| 2131 | break; |
| 2132 | case QUIRK_MIDI_EMAGIC: |
| 2133 | umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops; |
| 2134 | memcpy(&endpoints[0], quirk->data, |
Takashi Iwai | 86e07d3 | 2005-11-17 15:08:02 +0100 | [diff] [blame] | 2135 | sizeof(struct snd_usb_midi_endpoint_info)); |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2136 | err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1); |
| 2137 | break; |
Clemens Ladisch | cc7a59b | 2006-02-07 17:11:06 +0100 | [diff] [blame] | 2138 | case QUIRK_MIDI_CME: |
Clemens Ladisch | 61870ae | 2007-08-16 08:44:51 +0200 | [diff] [blame] | 2139 | umidi->usb_protocol_ops = &snd_usbmidi_cme_ops; |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2140 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); |
| 2141 | break; |
Krzysztof Foltman | 4434ade | 2010-05-20 20:31:10 +0100 | [diff] [blame] | 2142 | case QUIRK_MIDI_AKAI: |
| 2143 | umidi->usb_protocol_ops = &snd_usbmidi_akai_ops; |
| 2144 | err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints); |
| 2145 | /* endpoint 1 is input-only */ |
| 2146 | endpoints[1].out_cables = 0; |
| 2147 | break; |
Clemens Ladisch | d1bda04 | 2005-09-14 08:36:03 +0200 | [diff] [blame] | 2148 | default: |
| 2149 | snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type); |
| 2150 | err = -ENXIO; |
| 2151 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | } |
| 2153 | if (err < 0) { |
| 2154 | kfree(umidi); |
| 2155 | return err; |
| 2156 | } |
| 2157 | |
| 2158 | /* create rawmidi device */ |
| 2159 | out_ports = 0; |
| 2160 | in_ports = 0; |
| 2161 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) { |
Akinobu Mita | fbc5439 | 2009-11-20 14:56:52 +0900 | [diff] [blame] | 2162 | out_ports += hweight16(endpoints[i].out_cables); |
| 2163 | in_ports += hweight16(endpoints[i].in_cables); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | } |
| 2165 | err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports); |
| 2166 | if (err < 0) { |
| 2167 | kfree(umidi); |
| 2168 | return err; |
| 2169 | } |
| 2170 | |
| 2171 | /* create endpoint/port structures */ |
| 2172 | if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN) |
| 2173 | err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]); |
| 2174 | else |
| 2175 | err = snd_usbmidi_create_endpoints(umidi, endpoints); |
| 2176 | if (err < 0) { |
| 2177 | snd_usbmidi_free(umidi); |
| 2178 | return err; |
| 2179 | } |
| 2180 | |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2181 | list_add_tail(&umidi->list, midi_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | |
| 2183 | for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) |
| 2184 | snd_usbmidi_input_start_ep(umidi->endpoints[i].in); |
| 2185 | return 0; |
| 2186 | } |
| 2187 | |
Clemens Ladisch | d82af9f | 2009-11-16 12:23:46 +0100 | [diff] [blame] | 2188 | EXPORT_SYMBOL(snd_usbmidi_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | EXPORT_SYMBOL(snd_usbmidi_input_stop); |
| 2190 | EXPORT_SYMBOL(snd_usbmidi_input_start); |
| 2191 | EXPORT_SYMBOL(snd_usbmidi_disconnect); |