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