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