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