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