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