blob: 7b166c2be0f77a80a822d4597acf09f19659bf4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * usbmidi.c - ALSA USB MIDI driver
3 *
Clemens Ladisch96f61d92009-10-22 09:06:19 +02004 * Copyright (c) 2002-2009 Clemens Ladisch
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 Torvalds1da177e2005-04-16 15:20:36 -070038#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 Ladischc8846972005-08-02 15:26:52 +020046#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/usb.h>
Clemens Ladischa65dd992009-07-13 13:48:36 +020048#include <linux/wait.h>
Daniel Mackde48c7b2010-02-22 23:49:13 +010049#include <linux/usb/audio.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040050#include <linux/module.h>
Daniel Mackde48c7b2010-02-22 23:49:13 +010051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <sound/core.h>
Clemens Ladisch96f61d92009-10-22 09:06:19 +020053#include <sound/control.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <sound/rawmidi.h>
Clemens Ladischa7b928a2006-05-02 16:22:12 +020055#include <sound/asequencer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "usbaudio.h"
Daniel Macke5779992010-03-04 19:46:13 +010057#include "midi.h"
Oliver Neukum88a85162011-03-11 14:51:12 +010058#include "power.h"
Daniel Macke5779992010-03-04 19:46:13 +010059#include "helper.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
62 * define this to log all USB packets
63 */
64/* #define DUMP_PACKETS */
65
Clemens Ladischc8846972005-08-02 15:26:52 +020066/*
67 * how long to wait after some USB errors, so that khubd can disconnect() us
68 * without too many spurious errors
69 */
70#define ERROR_DELAY_JIFFIES (HZ / 10)
71
Clemens Ladisched4affa2009-07-13 13:43:59 +020072#define OUTPUT_URBS 7
Clemens Ladisch4773d1f2009-07-13 13:40:36 +020073#define INPUT_URBS 7
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76MODULE_AUTHOR("Clemens Ladisch <clemens@ladisch.de>");
77MODULE_DESCRIPTION("USB Audio/MIDI helper module");
78MODULE_LICENSE("Dual BSD/GPL");
79
80
81struct usb_ms_header_descriptor {
82 __u8 bLength;
83 __u8 bDescriptorType;
84 __u8 bDescriptorSubtype;
85 __u8 bcdMSC[2];
86 __le16 wTotalLength;
87} __attribute__ ((packed));
88
89struct 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 Iwai86e07d32005-11-17 15:08:02 +010097struct snd_usb_midi_in_endpoint;
98struct snd_usb_midi_out_endpoint;
99struct snd_usb_midi_endpoint;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101struct usb_protocol_ops {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100102 void (*input)(struct snd_usb_midi_in_endpoint*, uint8_t*, int);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200103 void (*output)(struct snd_usb_midi_out_endpoint *ep, struct urb *urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t);
Adam Goodea5095742014-08-05 12:44:51 -0400105 void (*init_out_endpoint)(struct snd_usb_midi_out_endpoint *);
106 void (*finish_out_endpoint)(struct snd_usb_midi_out_endpoint *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109struct snd_usb_midi {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100110 struct usb_device *dev;
111 struct snd_card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 struct usb_interface *iface;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100113 const struct snd_usb_audio_quirk *quirk;
114 struct snd_rawmidi *rmidi;
Adam Goodea5095742014-08-05 12:44:51 -0400115 struct usb_protocol_ops *usb_protocol_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct list_head list;
Clemens Ladischc8846972005-08-02 15:26:52 +0200117 struct timer_list error_timer;
Takashi Iwaic0792e02008-02-22 18:34:44 +0100118 spinlock_t disc_lock;
Takashi Iwai59866da2012-12-03 11:12:46 +0100119 struct rw_semaphore disc_rwsem;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200120 struct mutex mutex;
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100121 u32 usb_id;
122 int next_midi_device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 struct snd_usb_midi_endpoint {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100125 struct snd_usb_midi_out_endpoint *out;
126 struct snd_usb_midi_in_endpoint *in;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 } endpoints[MIDI_MAX_ENDPOINTS];
128 unsigned long input_triggered;
Takashi Iwaif5f16542012-12-03 11:30:50 +0100129 unsigned int opened[2];
Takashi Iwaic0792e02008-02-22 18:34:44 +0100130 unsigned char disconnected;
Takashi Iwaif5f16542012-12-03 11:30:50 +0100131 unsigned char input_running;
Clemens Ladisch96f61d92009-10-22 09:06:19 +0200132
133 struct snd_kcontrol *roland_load_ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134};
135
136struct snd_usb_midi_out_endpoint {
Adam Goodea5095742014-08-05 12:44:51 -0400137 struct snd_usb_midi *umidi;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200138 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 Ladischa65dd992009-07-13 13:48:36 +0200143 unsigned int drain_urbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int max_transfer; /* size of urb buffer */
145 struct tasklet_struct tasklet;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200146 unsigned int next_urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 spinlock_t buffer_lock;
148
149 struct usbmidi_out_port {
Adam Goodea5095742014-08-05 12:44:51 -0400150 struct snd_usb_midi_out_endpoint *ep;
Takashi Iwai86e07d32005-11-17 15:08:02 +0100151 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 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 Ladischa65dd992009-07-13 13:48:36 +0200165
166 wait_queue_head_t drain_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167};
168
169struct snd_usb_midi_in_endpoint {
Adam Goodea5095742014-08-05 12:44:51 -0400170 struct snd_usb_midi *umidi;
171 struct urb *urbs[INPUT_URBS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct usbmidi_in_port {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100173 struct snd_rawmidi_substream *substream;
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200174 u8 running_status_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 } ports[0x10];
Clemens Ladischc8846972005-08-02 15:26:52 +0200176 u8 seen_f5;
177 u8 error_resubmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 int current_port;
179};
180
Adam Goodea5095742014-08-05 12:44:51 -0400181static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183static 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 Goodea5095742014-08-05 12:44:51 -0400190static int snd_usbmidi_submit_urb(struct urb *urb, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 int err = usb_submit_urb(urb, flags);
193 if (err < 0 && err != -ENODEV)
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100194 dev_err(&urb->dev->dev, "usb_submit_urb: %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 return err;
196}
197
198/*
199 * Error handling for URB completion functions.
200 */
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100201static int snd_usbmidi_urb_error(const struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100203 switch (urb->status) {
Clemens Ladischc8846972005-08-02 15:26:52 +0200204 /* 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 Zaitcev38e2bfc2006-09-18 22:49:02 -0700211 case -EPROTO:
212 case -ETIME:
213 case -EILSEQ:
Clemens Ladischc8846972005-08-02 15:26:52 +0200214 return -EIO;
215 default:
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100216 dev_err(&urb->dev->dev, "urb status %d\n", urb->status);
Clemens Ladischc8846972005-08-02 15:26:52 +0200217 return 0; /* continue */
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
221/*
222 * Receives a chunk of MIDI data.
223 */
Adam Goodea5095742014-08-05 12:44:51 -0400224static void snd_usbmidi_input_data(struct snd_usb_midi_in_endpoint *ep,
225 int portidx, uint8_t *data, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Adam Goodea5095742014-08-05 12:44:51 -0400227 struct usbmidi_in_port *port = &ep->ports[portidx];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (!port->substream) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100230 dev_dbg(&ep->umidi->dev->dev, "unexpected port %d!\n", portidx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 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
239static 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 Goodea5095742014-08-05 12:44:51 -0400253static void snd_usbmidi_in_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Adam Goodea5095742014-08-05 12:44:51 -0400255 struct snd_usb_midi_in_endpoint *ep = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
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 Iwai0ba41d92014-02-26 13:02:17 +0100262 int err = snd_usbmidi_urb_error(urb);
Clemens Ladischc8846972005-08-02 15:26:52 +0200263 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 Torvalds1da177e2005-04-16 15:20:36 -0700269 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100273 urb->dev = ep->umidi->dev;
Clemens Ladisch3cfc1eb2005-09-26 10:01:12 +0200274 snd_usbmidi_submit_urb(urb, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Adam Goodea5095742014-08-05 12:44:51 -0400277static void snd_usbmidi_out_urb_complete(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200279 struct out_urb_context *context = urb->context;
Adam Goodea5095742014-08-05 12:44:51 -0400280 struct snd_usb_midi_out_endpoint *ep = context->ep;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200281 unsigned int urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 spin_lock(&ep->buffer_lock);
Clemens Ladischa65dd992009-07-13 13:48:36 +0200284 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 Torvalds1da177e2005-04-16 15:20:36 -0700290 spin_unlock(&ep->buffer_lock);
291 if (urb->status < 0) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +0100292 int err = snd_usbmidi_urb_error(urb);
Clemens Ladischc8846972005-08-02 15:26:52 +0200293 if (err < 0) {
294 if (err != -ENODEV)
295 mod_timer(&ep->umidi->error_timer,
296 jiffies + ERROR_DELAY_JIFFIES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 return;
Clemens Ladischc8846972005-08-02 15:26:52 +0200298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
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 Goodea5095742014-08-05 12:44:51 -0400307static void snd_usbmidi_do_output(struct snd_usb_midi_out_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200309 unsigned int urb_index;
Adam Goodea5095742014-08-05 12:44:51 -0400310 struct urb *urb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 unsigned long flags;
312
313 spin_lock_irqsave(&ep->buffer_lock, flags);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100314 if (ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 spin_unlock_irqrestore(&ep->buffer_lock, flags);
316 return;
317 }
318
Clemens Ladischa65dd992009-07-13 13:48:36 +0200319 urb_index = ep->next_urb;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200320 for (;;) {
Clemens Ladischa65dd992009-07-13 13:48:36 +0200321 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 Ladisched4affa2009-07-13 13:43:59 +0200326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Clemens Ladischa65dd992009-07-13 13:48:36 +0200328 dump_urb("sending", urb->transfer_buffer,
329 urb->transfer_buffer_length);
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100330 urb->dev = ep->umidi->dev;
Clemens Ladischa65dd992009-07-13 13:48:36 +0200331 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 Ladisched4affa2009-07-13 13:43:59 +0200338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Clemens Ladischa65dd992009-07-13 13:48:36 +0200340 ep->next_urb = urb_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 spin_unlock_irqrestore(&ep->buffer_lock, flags);
342}
343
344static void snd_usbmidi_out_tasklet(unsigned long data)
345{
Adam Goodea5095742014-08-05 12:44:51 -0400346 struct snd_usb_midi_out_endpoint *ep =
347 (struct snd_usb_midi_out_endpoint *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 snd_usbmidi_do_output(ep);
350}
351
Clemens Ladischc8846972005-08-02 15:26:52 +0200352/* called after transfers had been interrupted due to some USB error */
353static void snd_usbmidi_error_timer(unsigned long data)
354{
Takashi Iwai86e07d32005-11-17 15:08:02 +0100355 struct snd_usb_midi *umidi = (struct snd_usb_midi *)data;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200356 unsigned int i, j;
Clemens Ladischc8846972005-08-02 15:26:52 +0200357
Takashi Iwaic0792e02008-02-22 18:34:44 +0100358 spin_lock(&umidi->disc_lock);
359 if (umidi->disconnected) {
360 spin_unlock(&umidi->disc_lock);
361 return;
362 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200363 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Takashi Iwai86e07d32005-11-17 15:08:02 +0100364 struct snd_usb_midi_in_endpoint *in = umidi->endpoints[i].in;
Clemens Ladischc8846972005-08-02 15:26:52 +0200365 if (in && in->error_resubmit) {
366 in->error_resubmit = 0;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200367 for (j = 0; j < INPUT_URBS; ++j) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100368 in->urbs[j]->dev = umidi->dev;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +0200369 snd_usbmidi_submit_urb(in->urbs[j], GFP_ATOMIC);
370 }
Clemens Ladischc8846972005-08-02 15:26:52 +0200371 }
372 if (umidi->endpoints[i].out)
373 snd_usbmidi_do_output(umidi->endpoints[i].out);
374 }
Takashi Iwaic0792e02008-02-22 18:34:44 +0100375 spin_unlock(&umidi->disc_lock);
Clemens Ladischc8846972005-08-02 15:26:52 +0200376}
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378/* helper function to send static data that may not DMA-able */
Adam Goodea5095742014-08-05 12:44:51 -0400379static int send_bulk_static_data(struct snd_usb_midi_out_endpoint *ep,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 const void *data, int len)
381{
Clemens Ladisched4affa2009-07-13 13:43:59 +0200382 int err = 0;
Alexey Dobriyan52978be2006-09-30 23:27:21 -0700383 void *buf = kmemdup(data, len, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (!buf)
385 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 dump_urb("sending", buf, len);
Clemens Ladisched4affa2009-07-13 13:43:59 +0200387 if (ep->urbs[0].urb)
Clemens Ladischd82af9f2009-11-16 12:23:46 +0100388 err = usb_bulk_msg(ep->umidi->dev, ep->urbs[0].urb->pipe,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200389 buf, len, NULL, 250);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 kfree(buf);
391 return err;
392}
393
394/*
395 * Standard USB MIDI protocol: see the spec.
396 * Midiman protocol: like the standard protocol, but the control byte is the
397 * fourth byte in each packet, and uses length instead of CIN.
398 */
399
Adam Goodea5095742014-08-05 12:44:51 -0400400static void snd_usbmidi_standard_input(struct snd_usb_midi_in_endpoint *ep,
401 uint8_t *buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 int i;
404
405 for (i = 0; i + 3 < buffer_length; i += 4)
406 if (buffer[i] != 0) {
407 int cable = buffer[i] >> 4;
408 int length = snd_usbmidi_cin_length[buffer[i] & 0x0f];
Adam Goodea5095742014-08-05 12:44:51 -0400409 snd_usbmidi_input_data(ep, cable, &buffer[i + 1],
410 length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412}
413
Adam Goodea5095742014-08-05 12:44:51 -0400414static void snd_usbmidi_midiman_input(struct snd_usb_midi_in_endpoint *ep,
415 uint8_t *buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 int i;
418
419 for (i = 0; i + 3 < buffer_length; i += 4)
420 if (buffer[i + 3] != 0) {
421 int port = buffer[i + 3] >> 4;
422 int length = buffer[i + 3] & 3;
423 snd_usbmidi_input_data(ep, port, &buffer[i], length);
424 }
425}
426
427/*
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200428 * Buggy M-Audio device: running status on input results in a packet that has
429 * the data bytes but not the status byte and that is marked with CIN 4.
430 */
431static void snd_usbmidi_maudio_broken_running_status_input(
Adam Goodea5095742014-08-05 12:44:51 -0400432 struct snd_usb_midi_in_endpoint *ep,
433 uint8_t *buffer, int buffer_length)
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200434{
435 int i;
436
437 for (i = 0; i + 3 < buffer_length; i += 4)
438 if (buffer[i] != 0) {
439 int cable = buffer[i] >> 4;
440 u8 cin = buffer[i] & 0x0f;
441 struct usbmidi_in_port *port = &ep->ports[cable];
442 int length;
Daniel Mack21af7d82010-06-16 17:57:29 +0200443
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200444 length = snd_usbmidi_cin_length[cin];
445 if (cin == 0xf && buffer[i + 1] >= 0xf8)
446 ; /* realtime msg: no running status change */
447 else if (cin >= 0x8 && cin <= 0xe)
448 /* channel msg */
449 port->running_status_length = length - 1;
450 else if (cin == 0x4 &&
451 port->running_status_length != 0 &&
452 buffer[i + 1] < 0x80)
453 /* CIN 4 that is not a SysEx */
454 length = port->running_status_length;
455 else
456 /*
457 * All other msgs cannot begin running status.
458 * (A channel msg sent as two or three CIN 0xF
459 * packets could in theory, but this device
460 * doesn't use this format.)
461 */
462 port->running_status_length = 0;
Adam Goodea5095742014-08-05 12:44:51 -0400463 snd_usbmidi_input_data(ep, cable, &buffer[i + 1],
464 length);
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200465 }
466}
467
468/*
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200469 * CME protocol: like the standard protocol, but SysEx commands are sent as a
470 * single USB packet preceded by a 0x0F byte.
471 */
472static void snd_usbmidi_cme_input(struct snd_usb_midi_in_endpoint *ep,
473 uint8_t *buffer, int buffer_length)
474{
475 if (buffer_length < 2 || (buffer[0] & 0x0f) != 0x0f)
476 snd_usbmidi_standard_input(ep, buffer, buffer_length);
477 else
478 snd_usbmidi_input_data(ep, buffer[0] >> 4,
479 &buffer[1], buffer_length - 1);
480}
481
482/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 * Adds one USB MIDI packet to the output buffer.
484 */
Adam Goodea5095742014-08-05 12:44:51 -0400485static void snd_usbmidi_output_standard_packet(struct urb *urb, uint8_t p0,
486 uint8_t p1, uint8_t p2,
487 uint8_t p3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489
Adam Goodea5095742014-08-05 12:44:51 -0400490 uint8_t *buf =
491 (uint8_t *)urb->transfer_buffer + urb->transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 buf[0] = p0;
493 buf[1] = p1;
494 buf[2] = p2;
495 buf[3] = p3;
496 urb->transfer_buffer_length += 4;
497}
498
499/*
500 * Adds one Midiman packet to the output buffer.
501 */
Adam Goodea5095742014-08-05 12:44:51 -0400502static void snd_usbmidi_output_midiman_packet(struct urb *urb, uint8_t p0,
503 uint8_t p1, uint8_t p2,
504 uint8_t p3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506
Adam Goodea5095742014-08-05 12:44:51 -0400507 uint8_t *buf =
508 (uint8_t *)urb->transfer_buffer + urb->transfer_buffer_length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 buf[0] = p1;
510 buf[1] = p2;
511 buf[2] = p3;
512 buf[3] = (p0 & 0xf0) | snd_usbmidi_cin_length[p0 & 0x0f];
513 urb->transfer_buffer_length += 4;
514}
515
516/*
517 * Converts MIDI commands to USB MIDI packets.
518 */
Adam Goodea5095742014-08-05 12:44:51 -0400519static void snd_usbmidi_transmit_byte(struct usbmidi_out_port *port,
520 uint8_t b, struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 uint8_t p0 = port->cable;
523 void (*output_packet)(struct urb*, uint8_t, uint8_t, uint8_t, uint8_t) =
524 port->ep->umidi->usb_protocol_ops->output_packet;
525
526 if (b >= 0xf8) {
527 output_packet(urb, p0 | 0x0f, b, 0, 0);
528 } else if (b >= 0xf0) {
529 switch (b) {
530 case 0xf0:
531 port->data[0] = b;
532 port->state = STATE_SYSEX_1;
533 break;
534 case 0xf1:
535 case 0xf3:
536 port->data[0] = b;
537 port->state = STATE_1PARAM;
538 break;
539 case 0xf2:
540 port->data[0] = b;
541 port->state = STATE_2PARAM_1;
542 break;
543 case 0xf4:
544 case 0xf5:
545 port->state = STATE_UNKNOWN;
546 break;
547 case 0xf6:
548 output_packet(urb, p0 | 0x05, 0xf6, 0, 0);
549 port->state = STATE_UNKNOWN;
550 break;
551 case 0xf7:
552 switch (port->state) {
553 case STATE_SYSEX_0:
554 output_packet(urb, p0 | 0x05, 0xf7, 0, 0);
555 break;
556 case STATE_SYSEX_1:
Adam Goodea5095742014-08-05 12:44:51 -0400557 output_packet(urb, p0 | 0x06, port->data[0],
558 0xf7, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560 case STATE_SYSEX_2:
Adam Goodea5095742014-08-05 12:44:51 -0400561 output_packet(urb, p0 | 0x07, port->data[0],
562 port->data[1], 0xf7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
564 }
565 port->state = STATE_UNKNOWN;
566 break;
567 }
568 } else if (b >= 0x80) {
569 port->data[0] = b;
570 if (b >= 0xc0 && b <= 0xdf)
571 port->state = STATE_1PARAM;
572 else
573 port->state = STATE_2PARAM_1;
574 } else { /* b < 0x80 */
575 switch (port->state) {
576 case STATE_1PARAM:
577 if (port->data[0] < 0xf0) {
578 p0 |= port->data[0] >> 4;
579 } else {
580 p0 |= 0x02;
581 port->state = STATE_UNKNOWN;
582 }
583 output_packet(urb, p0, port->data[0], b, 0);
584 break;
585 case STATE_2PARAM_1:
586 port->data[1] = b;
587 port->state = STATE_2PARAM_2;
588 break;
589 case STATE_2PARAM_2:
590 if (port->data[0] < 0xf0) {
591 p0 |= port->data[0] >> 4;
592 port->state = STATE_2PARAM_1;
593 } else {
594 p0 |= 0x03;
595 port->state = STATE_UNKNOWN;
596 }
597 output_packet(urb, p0, port->data[0], port->data[1], b);
598 break;
599 case STATE_SYSEX_0:
600 port->data[0] = b;
601 port->state = STATE_SYSEX_1;
602 break;
603 case STATE_SYSEX_1:
604 port->data[1] = b;
605 port->state = STATE_SYSEX_2;
606 break;
607 case STATE_SYSEX_2:
Adam Goodea5095742014-08-05 12:44:51 -0400608 output_packet(urb, p0 | 0x04, port->data[0],
609 port->data[1], b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 port->state = STATE_SYSEX_0;
611 break;
612 }
613 }
614}
615
Adam Goodea5095742014-08-05 12:44:51 -0400616static void snd_usbmidi_standard_output(struct snd_usb_midi_out_endpoint *ep,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200617 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int p;
620
621 /* FIXME: lower-numbered ports can starve higher-numbered ports */
622 for (p = 0; p < 0x10; ++p) {
Adam Goodea5095742014-08-05 12:44:51 -0400623 struct usbmidi_out_port *port = &ep->ports[p];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!port->active)
625 continue;
626 while (urb->transfer_buffer_length + 3 < ep->max_transfer) {
627 uint8_t b;
628 if (snd_rawmidi_transmit(port->substream, &b, 1) != 1) {
629 port->active = 0;
630 break;
631 }
632 snd_usbmidi_transmit_byte(port, b, urb);
633 }
634 }
635}
636
637static struct usb_protocol_ops snd_usbmidi_standard_ops = {
638 .input = snd_usbmidi_standard_input,
639 .output = snd_usbmidi_standard_output,
640 .output_packet = snd_usbmidi_output_standard_packet,
641};
642
643static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
644 .input = snd_usbmidi_midiman_input,
Daniel Mack21af7d82010-06-16 17:57:29 +0200645 .output = snd_usbmidi_standard_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 .output_packet = snd_usbmidi_output_midiman_packet,
647};
648
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200649static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
650 .input = snd_usbmidi_maudio_broken_running_status_input,
Daniel Mack21af7d82010-06-16 17:57:29 +0200651 .output = snd_usbmidi_standard_output,
Clemens Ladischd05cc10432007-05-07 09:28:53 +0200652 .output_packet = snd_usbmidi_output_standard_packet,
653};
654
Clemens Ladisch61870ae2007-08-16 08:44:51 +0200655static struct usb_protocol_ops snd_usbmidi_cme_ops = {
656 .input = snd_usbmidi_cme_input,
657 .output = snd_usbmidi_standard_output,
658 .output_packet = snd_usbmidi_output_standard_packet,
659};
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661/*
Krzysztof Foltman4434ade2010-05-20 20:31:10 +0100662 * AKAI MPD16 protocol:
663 *
664 * For control port (endpoint 1):
665 * ==============================
666 * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
667 * SysEx message (msg_len=9 bytes long).
668 *
669 * For data port (endpoint 2):
670 * ===========================
671 * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
672 * MIDI message (msg_len bytes long)
673 *
674 * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
675 */
676static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
677 uint8_t *buffer, int buffer_length)
678{
679 unsigned int pos = 0;
680 unsigned int len = (unsigned int)buffer_length;
681 while (pos < len) {
682 unsigned int port = (buffer[pos] >> 4) - 1;
683 unsigned int msg_len = buffer[pos] & 0x0f;
684 pos++;
685 if (pos + msg_len <= len && port < 2)
686 snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
687 pos += msg_len;
688 }
689}
690
691#define MAX_AKAI_SYSEX_LEN 9
692
693static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
694 struct urb *urb)
695{
696 uint8_t *msg;
697 int pos, end, count, buf_end;
698 uint8_t tmp[MAX_AKAI_SYSEX_LEN];
699 struct snd_rawmidi_substream *substream = ep->ports[0].substream;
700
701 if (!ep->ports[0].active)
702 return;
703
704 msg = urb->transfer_buffer + urb->transfer_buffer_length;
705 buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
706
707 /* only try adding more data when there's space for at least 1 SysEx */
708 while (urb->transfer_buffer_length < buf_end) {
709 count = snd_rawmidi_transmit_peek(substream,
710 tmp, MAX_AKAI_SYSEX_LEN);
711 if (!count) {
712 ep->ports[0].active = 0;
713 return;
714 }
715 /* try to skip non-SysEx data */
716 for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
717 ;
718
719 if (pos > 0) {
720 snd_rawmidi_transmit_ack(substream, pos);
721 continue;
722 }
723
724 /* look for the start or end marker */
725 for (end = 1; end < count && tmp[end] < 0xF0; end++)
726 ;
727
728 /* next SysEx started before the end of current one */
729 if (end < count && tmp[end] == 0xF0) {
730 /* it's incomplete - drop it */
731 snd_rawmidi_transmit_ack(substream, end);
732 continue;
733 }
734 /* SysEx complete */
735 if (end < count && tmp[end] == 0xF7) {
736 /* queue it, ack it, and get the next one */
737 count = end + 1;
738 msg[0] = 0x10 | count;
739 memcpy(&msg[1], tmp, count);
740 snd_rawmidi_transmit_ack(substream, count);
741 urb->transfer_buffer_length += count + 1;
742 msg += count + 1;
743 continue;
744 }
745 /* less than 9 bytes and no end byte - wait for more */
746 if (count < MAX_AKAI_SYSEX_LEN) {
747 ep->ports[0].active = 0;
748 return;
749 }
750 /* 9 bytes and no end marker in sight - malformed, skip it */
751 snd_rawmidi_transmit_ack(substream, count);
752 }
753}
754
755static struct usb_protocol_ops snd_usbmidi_akai_ops = {
756 .input = snd_usbmidi_akai_input,
757 .output = snd_usbmidi_akai_output,
758};
759
760/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 * Novation USB MIDI protocol: number of data bytes is in the first byte
762 * (when receiving) (+1!) or in the second byte (when sending); data begins
763 * at the third byte.
764 */
765
Adam Goodea5095742014-08-05 12:44:51 -0400766static void snd_usbmidi_novation_input(struct snd_usb_midi_in_endpoint *ep,
767 uint8_t *buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
769 if (buffer_length < 2 || !buffer[0] || buffer_length < buffer[0] + 1)
770 return;
771 snd_usbmidi_input_data(ep, 0, &buffer[2], buffer[0] - 1);
772}
773
Adam Goodea5095742014-08-05 12:44:51 -0400774static void snd_usbmidi_novation_output(struct snd_usb_midi_out_endpoint *ep,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200775 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Adam Goodea5095742014-08-05 12:44:51 -0400777 uint8_t *transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 int count;
779
780 if (!ep->ports[0].active)
781 return;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200782 transfer_buffer = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 count = snd_rawmidi_transmit(ep->ports[0].substream,
784 &transfer_buffer[2],
785 ep->max_transfer - 2);
786 if (count < 1) {
787 ep->ports[0].active = 0;
788 return;
789 }
790 transfer_buffer[0] = 0;
791 transfer_buffer[1] = count;
Clemens Ladisched4affa2009-07-13 13:43:59 +0200792 urb->transfer_buffer_length = 2 + count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
795static struct usb_protocol_ops snd_usbmidi_novation_ops = {
796 .input = snd_usbmidi_novation_input,
797 .output = snd_usbmidi_novation_output,
798};
799
800/*
Clemens Ladischc7f57212010-10-22 18:20:48 +0200801 * "raw" protocol: just move raw MIDI bytes from/to the endpoint
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 */
803
Adam Goodea5095742014-08-05 12:44:51 -0400804static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint *ep,
805 uint8_t *buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
808}
809
Adam Goodea5095742014-08-05 12:44:51 -0400810static void snd_usbmidi_raw_output(struct snd_usb_midi_out_endpoint *ep,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200811 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 int count;
814
815 if (!ep->ports[0].active)
816 return;
817 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200818 urb->transfer_buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 ep->max_transfer);
820 if (count < 1) {
821 ep->ports[0].active = 0;
822 return;
823 }
Clemens Ladisched4affa2009-07-13 13:43:59 +0200824 urb->transfer_buffer_length = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Clemens Ladisch6155aff2005-07-04 09:20:42 +0200827static struct usb_protocol_ops snd_usbmidi_raw_ops = {
828 .input = snd_usbmidi_raw_input,
829 .output = snd_usbmidi_raw_output,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830};
831
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +0200832/*
833 * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
834 */
835
Adam Goodea5095742014-08-05 12:44:51 -0400836static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint *ep,
837 uint8_t *buffer, int buffer_length)
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +0200838{
839 if (buffer_length > 2)
840 snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
841}
842
843static struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
844 .input = snd_usbmidi_ftdi_input,
845 .output = snd_usbmidi_raw_output,
846};
847
Karsten Wiese030a07e2008-07-30 15:13:29 +0200848static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
849 uint8_t *buffer, int buffer_length)
850{
851 if (buffer_length != 9)
852 return;
853 buffer_length = 8;
854 while (buffer_length && buffer[buffer_length - 1] == 0xFD)
855 buffer_length--;
856 if (buffer_length)
857 snd_usbmidi_input_data(ep, 0, buffer, buffer_length);
858}
859
Clemens Ladisched4affa2009-07-13 13:43:59 +0200860static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
861 struct urb *urb)
Karsten Wiese030a07e2008-07-30 15:13:29 +0200862{
863 int count;
864
865 if (!ep->ports[0].active)
866 return;
Paul Zimmerman4f4e8f62010-08-13 12:42:07 -0700867 switch (snd_usb_get_speed(ep->umidi->dev)) {
868 case USB_SPEED_HIGH:
869 case USB_SPEED_SUPER:
870 count = 1;
871 break;
872 default:
873 count = 2;
874 }
Karsten Wiese030a07e2008-07-30 15:13:29 +0200875 count = snd_rawmidi_transmit(ep->ports[0].substream,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200876 urb->transfer_buffer,
Karsten Wiese030a07e2008-07-30 15:13:29 +0200877 count);
878 if (count < 1) {
879 ep->ports[0].active = 0;
880 return;
881 }
882
Karsten Wiese921eebd2011-01-03 02:41:58 +0100883 memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
884 urb->transfer_buffer_length = ep->max_transfer;
Karsten Wiese030a07e2008-07-30 15:13:29 +0200885}
886
887static struct usb_protocol_ops snd_usbmidi_122l_ops = {
888 .input = snd_usbmidi_us122l_input,
889 .output = snd_usbmidi_us122l_output,
890};
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
893 * Emagic USB MIDI protocol: raw MIDI with "F5 xx" port switching.
894 */
895
Adam Goodea5095742014-08-05 12:44:51 -0400896static void snd_usbmidi_emagic_init_out(struct snd_usb_midi_out_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 static const u8 init_data[] = {
899 /* initialization magic: "get version" */
900 0xf0,
901 0x00, 0x20, 0x31, /* Emagic */
902 0x64, /* Unitor8 */
903 0x0b, /* version number request */
904 0x00, /* command version */
905 0x00, /* EEPROM, box 0 */
906 0xf7
907 };
908 send_bulk_static_data(ep, init_data, sizeof(init_data));
909 /* while we're at it, pour on more magic */
910 send_bulk_static_data(ep, init_data, sizeof(init_data));
911}
912
Adam Goodea5095742014-08-05 12:44:51 -0400913static void snd_usbmidi_emagic_finish_out(struct snd_usb_midi_out_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914{
915 static const u8 finish_data[] = {
916 /* switch to patch mode with last preset */
917 0xf0,
918 0x00, 0x20, 0x31, /* Emagic */
919 0x64, /* Unitor8 */
920 0x10, /* patch switch command */
921 0x00, /* command version */
922 0x7f, /* to all boxes */
923 0x40, /* last preset in EEPROM */
924 0xf7
925 };
926 send_bulk_static_data(ep, finish_data, sizeof(finish_data));
927}
928
Adam Goodea5095742014-08-05 12:44:51 -0400929static void snd_usbmidi_emagic_input(struct snd_usb_midi_in_endpoint *ep,
930 uint8_t *buffer, int buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Clemens Ladischc347e9f2005-08-25 11:10:05 +0200932 int i;
933
934 /* FF indicates end of valid data */
935 for (i = 0; i < buffer_length; ++i)
936 if (buffer[i] == 0xff) {
937 buffer_length = i;
938 break;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 /* handle F5 at end of last buffer */
942 if (ep->seen_f5)
943 goto switch_port;
944
945 while (buffer_length > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 /* determine size of data until next F5 */
947 for (i = 0; i < buffer_length; ++i)
948 if (buffer[i] == 0xf5)
949 break;
950 snd_usbmidi_input_data(ep, ep->current_port, buffer, i);
951 buffer += i;
952 buffer_length -= i;
953
954 if (buffer_length <= 0)
955 break;
956 /* assert(buffer[0] == 0xf5); */
957 ep->seen_f5 = 1;
958 ++buffer;
959 --buffer_length;
960
961 switch_port:
962 if (buffer_length <= 0)
963 break;
964 if (buffer[0] < 0x80) {
965 ep->current_port = (buffer[0] - 1) & 15;
966 ++buffer;
967 --buffer_length;
968 }
969 ep->seen_f5 = 0;
970 }
971}
972
Adam Goodea5095742014-08-05 12:44:51 -0400973static void snd_usbmidi_emagic_output(struct snd_usb_midi_out_endpoint *ep,
Clemens Ladisched4affa2009-07-13 13:43:59 +0200974 struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975{
976 int port0 = ep->current_port;
Adam Goodea5095742014-08-05 12:44:51 -0400977 uint8_t *buf = urb->transfer_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 int buf_free = ep->max_transfer;
979 int length, i;
980
981 for (i = 0; i < 0x10; ++i) {
982 /* round-robin, starting at the last current port */
983 int portnum = (port0 + i) & 15;
Adam Goodea5095742014-08-05 12:44:51 -0400984 struct usbmidi_out_port *port = &ep->ports[portnum];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (!port->active)
987 continue;
988 if (snd_rawmidi_transmit_peek(port->substream, buf, 1) != 1) {
989 port->active = 0;
990 continue;
991 }
992
993 if (portnum != ep->current_port) {
994 if (buf_free < 2)
995 break;
996 ep->current_port = portnum;
997 buf[0] = 0xf5;
998 buf[1] = (portnum + 1) & 15;
999 buf += 2;
1000 buf_free -= 2;
1001 }
1002
1003 if (buf_free < 1)
1004 break;
1005 length = snd_rawmidi_transmit(port->substream, buf, buf_free);
1006 if (length > 0) {
1007 buf += length;
1008 buf_free -= length;
1009 if (buf_free < 1)
1010 break;
1011 }
1012 }
Clemens Ladischc347e9f2005-08-25 11:10:05 +02001013 if (buf_free < ep->max_transfer && buf_free > 0) {
1014 *buf = 0xff;
1015 --buf_free;
1016 }
Clemens Ladisched4affa2009-07-13 13:43:59 +02001017 urb->transfer_buffer_length = ep->max_transfer - buf_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018}
1019
1020static struct usb_protocol_ops snd_usbmidi_emagic_ops = {
1021 .input = snd_usbmidi_emagic_input,
1022 .output = snd_usbmidi_emagic_output,
1023 .init_out_endpoint = snd_usbmidi_emagic_init_out,
1024 .finish_out_endpoint = snd_usbmidi_emagic_finish_out,
1025};
1026
1027
Adam Goodea5095742014-08-05 12:44:51 -04001028static void update_roland_altsetting(struct snd_usb_midi *umidi)
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001029{
1030 struct usb_interface *intf;
1031 struct usb_host_interface *hostif;
1032 struct usb_interface_descriptor *intfd;
1033 int is_light_load;
1034
1035 intf = umidi->iface;
1036 is_light_load = intf->cur_altsetting != intf->altsetting;
1037 if (umidi->roland_load_ctl->private_value == is_light_load)
1038 return;
1039 hostif = &intf->altsetting[umidi->roland_load_ctl->private_value];
1040 intfd = get_iface_desc(hostif);
1041 snd_usbmidi_input_stop(&umidi->list);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001042 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001043 intfd->bAlternateSetting);
1044 snd_usbmidi_input_start(&umidi->list);
1045}
1046
Takashi Iwaif5f16542012-12-03 11:30:50 +01001047static int substream_open(struct snd_rawmidi_substream *substream, int dir,
1048 int open)
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001049{
Adam Goodea5095742014-08-05 12:44:51 -04001050 struct snd_usb_midi *umidi = substream->rmidi->private_data;
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001051 struct snd_kcontrol *ctl;
1052
Takashi Iwai59866da2012-12-03 11:12:46 +01001053 down_read(&umidi->disc_rwsem);
1054 if (umidi->disconnected) {
1055 up_read(&umidi->disc_rwsem);
Takashi Iwaif5f16542012-12-03 11:30:50 +01001056 return open ? -ENODEV : 0;
Takashi Iwai59866da2012-12-03 11:12:46 +01001057 }
1058
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001059 mutex_lock(&umidi->mutex);
1060 if (open) {
Takashi Iwaif5f16542012-12-03 11:30:50 +01001061 if (!umidi->opened[0] && !umidi->opened[1]) {
Takashi Iwaif5f16542012-12-03 11:30:50 +01001062 if (umidi->roland_load_ctl) {
1063 ctl = umidi->roland_load_ctl;
Adam Goodea5095742014-08-05 12:44:51 -04001064 ctl->vd[0].access |=
1065 SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Takashi Iwaif5f16542012-12-03 11:30:50 +01001066 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001067 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
Takashi Iwaif5f16542012-12-03 11:30:50 +01001068 update_roland_altsetting(umidi);
1069 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001070 }
Takashi Iwaif5f16542012-12-03 11:30:50 +01001071 umidi->opened[dir]++;
1072 if (umidi->opened[1])
1073 snd_usbmidi_input_start(&umidi->list);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001074 } else {
Takashi Iwaif5f16542012-12-03 11:30:50 +01001075 umidi->opened[dir]--;
1076 if (!umidi->opened[1])
1077 snd_usbmidi_input_stop(&umidi->list);
1078 if (!umidi->opened[0] && !umidi->opened[1]) {
1079 if (umidi->roland_load_ctl) {
1080 ctl = umidi->roland_load_ctl;
Adam Goodea5095742014-08-05 12:44:51 -04001081 ctl->vd[0].access &=
1082 ~SNDRV_CTL_ELEM_ACCESS_INACTIVE;
Takashi Iwaif5f16542012-12-03 11:30:50 +01001083 snd_ctl_notify(umidi->card,
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001084 SNDRV_CTL_EVENT_MASK_INFO, &ctl->id);
Takashi Iwaif5f16542012-12-03 11:30:50 +01001085 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001086 }
1087 }
1088 mutex_unlock(&umidi->mutex);
Takashi Iwai59866da2012-12-03 11:12:46 +01001089 up_read(&umidi->disc_rwsem);
Takashi Iwaif5f16542012-12-03 11:30:50 +01001090 return 0;
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001091}
1092
Takashi Iwai86e07d32005-11-17 15:08:02 +01001093static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
Adam Goodea5095742014-08-05 12:44:51 -04001095 struct snd_usb_midi *umidi = substream->rmidi->private_data;
1096 struct usbmidi_out_port *port = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int i, j;
1098
1099 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
1100 if (umidi->endpoints[i].out)
1101 for (j = 0; j < 0x10; ++j)
1102 if (umidi->endpoints[i].out->ports[j].substream == substream) {
1103 port = &umidi->endpoints[i].out->ports[j];
1104 break;
1105 }
1106 if (!port) {
1107 snd_BUG();
1108 return -ENXIO;
1109 }
Takashi Iwai59866da2012-12-03 11:12:46 +01001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 substream->runtime->private_data = port;
1112 port->state = STATE_UNKNOWN;
Takashi Iwaif5f16542012-12-03 11:30:50 +01001113 return substream_open(substream, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114}
1115
Takashi Iwai86e07d32005-11-17 15:08:02 +01001116static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117{
Takashi Iwaif5f16542012-12-03 11:30:50 +01001118 return substream_open(substream, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
Adam Goodea5095742014-08-05 12:44:51 -04001121static void snd_usbmidi_output_trigger(struct snd_rawmidi_substream *substream,
1122 int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Adam Goodea5095742014-08-05 12:44:51 -04001124 struct usbmidi_out_port *port =
1125 (struct usbmidi_out_port *)substream->runtime->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 port->active = up;
1128 if (up) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001129 if (port->ep->umidi->disconnected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /* gobble up remaining bytes to prevent wait in
1131 * snd_rawmidi_drain_output */
1132 while (!snd_rawmidi_transmit_empty(substream))
1133 snd_rawmidi_transmit_ack(substream, 1);
1134 return;
1135 }
Takashi Iwai1f041282008-12-18 12:17:55 +01001136 tasklet_schedule(&port->ep->tasklet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138}
1139
Clemens Ladischa65dd992009-07-13 13:48:36 +02001140static void snd_usbmidi_output_drain(struct snd_rawmidi_substream *substream)
1141{
Adam Goodea5095742014-08-05 12:44:51 -04001142 struct usbmidi_out_port *port = substream->runtime->private_data;
Clemens Ladischa65dd992009-07-13 13:48:36 +02001143 struct snd_usb_midi_out_endpoint *ep = port->ep;
1144 unsigned int drain_urbs;
1145 DEFINE_WAIT(wait);
1146 long timeout = msecs_to_jiffies(50);
1147
Takashi Iwai29aac002010-04-10 21:27:23 +02001148 if (ep->umidi->disconnected)
1149 return;
Clemens Ladischa65dd992009-07-13 13:48:36 +02001150 /*
1151 * The substream buffer is empty, but some data might still be in the
1152 * currently active URBs, so we have to wait for those to complete.
1153 */
1154 spin_lock_irq(&ep->buffer_lock);
1155 drain_urbs = ep->active_urbs;
1156 if (drain_urbs) {
1157 ep->drain_urbs |= drain_urbs;
1158 do {
1159 prepare_to_wait(&ep->drain_wait, &wait,
1160 TASK_UNINTERRUPTIBLE);
1161 spin_unlock_irq(&ep->buffer_lock);
1162 timeout = schedule_timeout(timeout);
1163 spin_lock_irq(&ep->buffer_lock);
1164 drain_urbs &= ep->drain_urbs;
1165 } while (drain_urbs && timeout);
1166 finish_wait(&ep->drain_wait, &wait);
1167 }
1168 spin_unlock_irq(&ep->buffer_lock);
1169}
1170
Takashi Iwai86e07d32005-11-17 15:08:02 +01001171static int snd_usbmidi_input_open(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
Takashi Iwaif5f16542012-12-03 11:30:50 +01001173 return substream_open(substream, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Takashi Iwai86e07d32005-11-17 15:08:02 +01001176static int snd_usbmidi_input_close(struct snd_rawmidi_substream *substream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Takashi Iwaif5f16542012-12-03 11:30:50 +01001178 return substream_open(substream, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Adam Goodea5095742014-08-05 12:44:51 -04001181static void snd_usbmidi_input_trigger(struct snd_rawmidi_substream *substream,
1182 int up)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Adam Goodea5095742014-08-05 12:44:51 -04001184 struct snd_usb_midi *umidi = substream->rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 if (up)
1187 set_bit(substream->number, &umidi->input_triggered);
1188 else
1189 clear_bit(substream->number, &umidi->input_triggered);
1190}
1191
Takashi Iwai86e07d32005-11-17 15:08:02 +01001192static struct snd_rawmidi_ops snd_usbmidi_output_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 .open = snd_usbmidi_output_open,
1194 .close = snd_usbmidi_output_close,
1195 .trigger = snd_usbmidi_output_trigger,
Clemens Ladischa65dd992009-07-13 13:48:36 +02001196 .drain = snd_usbmidi_output_drain,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197};
1198
Takashi Iwai86e07d32005-11-17 15:08:02 +01001199static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 .open = snd_usbmidi_input_open,
1201 .close = snd_usbmidi_input_close,
1202 .trigger = snd_usbmidi_input_trigger
1203};
1204
Clemens Ladisched4affa2009-07-13 13:43:59 +02001205static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
1206 unsigned int buffer_length)
1207{
Daniel Mack997ea582010-04-12 13:17:25 +02001208 usb_free_coherent(umidi->dev, buffer_length,
1209 urb->transfer_buffer, urb->transfer_dma);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001210 usb_free_urb(urb);
1211}
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/*
1214 * Frees an input endpoint.
1215 * May be called when ep hasn't been initialized completely.
1216 */
Adam Goodea5095742014-08-05 12:44:51 -04001217static void snd_usbmidi_in_endpoint_delete(struct snd_usb_midi_in_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001219 unsigned int i;
1220
Clemens Ladisched4affa2009-07-13 13:43:59 +02001221 for (i = 0; i < INPUT_URBS; ++i)
1222 if (ep->urbs[i])
1223 free_urb_and_buffer(ep->umidi, ep->urbs[i],
1224 ep->urbs[i]->transfer_buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 kfree(ep);
1226}
1227
1228/*
1229 * Creates an input endpoint.
1230 */
Adam Goodea5095742014-08-05 12:44:51 -04001231static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi *umidi,
1232 struct snd_usb_midi_endpoint_info *ep_info,
1233 struct snd_usb_midi_endpoint *rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Adam Goodea5095742014-08-05 12:44:51 -04001235 struct snd_usb_midi_in_endpoint *ep;
1236 void *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 unsigned int pipe;
1238 int length;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001239 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 rep->in = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001242 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 if (!ep)
1244 return -ENOMEM;
1245 ep->umidi = umidi;
1246
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001247 for (i = 0; i < INPUT_URBS; ++i) {
1248 ep->urbs[i] = usb_alloc_urb(0, GFP_KERNEL);
1249 if (!ep->urbs[i]) {
1250 snd_usbmidi_in_endpoint_delete(ep);
1251 return -ENOMEM;
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001255 pipe = usb_rcvintpipe(umidi->dev, ep_info->in_ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001257 pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
1258 length = usb_maxpacket(umidi->dev, pipe, 0);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001259 for (i = 0; i < INPUT_URBS; ++i) {
Daniel Mack997ea582010-04-12 13:17:25 +02001260 buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
1261 &ep->urbs[i]->transfer_dma);
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001262 if (!buffer) {
1263 snd_usbmidi_in_endpoint_delete(ep);
1264 return -ENOMEM;
1265 }
1266 if (ep_info->in_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001267 usb_fill_int_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001268 pipe, buffer, length,
1269 snd_usbmidi_in_urb_complete,
1270 ep, ep_info->in_interval);
1271 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001272 usb_fill_bulk_urb(ep->urbs[i], umidi->dev,
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001273 pipe, buffer, length,
1274 snd_usbmidi_in_urb_complete, ep);
1275 ep->urbs[i]->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 rep->in = ep;
1279 return 0;
1280}
1281
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282/*
1283 * Frees an output endpoint.
1284 * May be called when ep hasn't been initialized completely.
1285 */
Takashi Iwai29aac002010-04-10 21:27:23 +02001286static void snd_usbmidi_out_endpoint_clear(struct snd_usb_midi_out_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
Clemens Ladisched4affa2009-07-13 13:43:59 +02001288 unsigned int i;
1289
1290 for (i = 0; i < OUTPUT_URBS; ++i)
Takashi Iwai29aac002010-04-10 21:27:23 +02001291 if (ep->urbs[i].urb) {
Clemens Ladisched4affa2009-07-13 13:43:59 +02001292 free_urb_and_buffer(ep->umidi, ep->urbs[i].urb,
1293 ep->max_transfer);
Takashi Iwai29aac002010-04-10 21:27:23 +02001294 ep->urbs[i].urb = NULL;
1295 }
1296}
1297
1298static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep)
1299{
1300 snd_usbmidi_out_endpoint_clear(ep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 kfree(ep);
1302}
1303
1304/*
1305 * Creates an output endpoint, and initializes output ports.
1306 */
Adam Goodea5095742014-08-05 12:44:51 -04001307static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi *umidi,
1308 struct snd_usb_midi_endpoint_info *ep_info,
1309 struct snd_usb_midi_endpoint *rep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Adam Goodea5095742014-08-05 12:44:51 -04001311 struct snd_usb_midi_out_endpoint *ep;
Clemens Ladisched4affa2009-07-13 13:43:59 +02001312 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 unsigned int pipe;
Adam Goodea5095742014-08-05 12:44:51 -04001314 void *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 rep->out = NULL;
Takashi Iwai561b2202005-09-09 14:22:34 +02001317 ep = kzalloc(sizeof(*ep), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (!ep)
1319 return -ENOMEM;
1320 ep->umidi = umidi;
1321
Clemens Ladisched4affa2009-07-13 13:43:59 +02001322 for (i = 0; i < OUTPUT_URBS; ++i) {
1323 ep->urbs[i].urb = usb_alloc_urb(0, GFP_KERNEL);
1324 if (!ep->urbs[i].urb) {
1325 snd_usbmidi_out_endpoint_delete(ep);
1326 return -ENOMEM;
1327 }
1328 ep->urbs[i].ep = ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001330 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001331 pipe = usb_sndintpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischa6a712a2007-08-21 08:56:08 +02001332 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001333 pipe = usb_sndbulkpipe(umidi->dev, ep_info->out_ep);
Clemens Ladischf167e1d072010-02-15 08:55:28 +01001334 switch (umidi->usb_id) {
1335 default:
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001336 ep->max_transfer = usb_maxpacket(umidi->dev, pipe, 1);
Clemens Ladischf167e1d072010-02-15 08:55:28 +01001337 break;
1338 /*
1339 * Various chips declare a packet size larger than 4 bytes, but
1340 * do not actually work with larger packets:
1341 */
1342 case USB_ID(0x0a92, 0x1020): /* ESI M4U */
1343 case USB_ID(0x1430, 0x474b): /* RedOctane GH MIDI INTERFACE */
1344 case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
1345 case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
1346 case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
Tarek Soliman49c039f2011-04-04 09:23:53 -05001347 case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
Clemens Ladischf167e1d072010-02-15 08:55:28 +01001348 ep->max_transfer = 4;
1349 break;
Karsten Wiese921eebd2011-01-03 02:41:58 +01001350 /*
1351 * Some devices only work with 9 bytes packet size:
1352 */
1353 case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
1354 case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
1355 ep->max_transfer = 9;
1356 break;
Clemens Ladischf167e1d072010-02-15 08:55:28 +01001357 }
Clemens Ladisched4affa2009-07-13 13:43:59 +02001358 for (i = 0; i < OUTPUT_URBS; ++i) {
Daniel Mack997ea582010-04-12 13:17:25 +02001359 buffer = usb_alloc_coherent(umidi->dev,
1360 ep->max_transfer, GFP_KERNEL,
1361 &ep->urbs[i].urb->transfer_dma);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001362 if (!buffer) {
1363 snd_usbmidi_out_endpoint_delete(ep);
1364 return -ENOMEM;
1365 }
1366 if (ep_info->out_interval)
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001367 usb_fill_int_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001368 pipe, buffer, ep->max_transfer,
1369 snd_usbmidi_out_urb_complete,
1370 &ep->urbs[i], ep_info->out_interval);
1371 else
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001372 usb_fill_bulk_urb(ep->urbs[i].urb, umidi->dev,
Clemens Ladisched4affa2009-07-13 13:43:59 +02001373 pipe, buffer, ep->max_transfer,
1374 snd_usbmidi_out_urb_complete,
1375 &ep->urbs[i]);
1376 ep->urbs[i].urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 spin_lock_init(&ep->buffer_lock);
1380 tasklet_init(&ep->tasklet, snd_usbmidi_out_tasklet, (unsigned long)ep);
Clemens Ladischa65dd992009-07-13 13:48:36 +02001381 init_waitqueue_head(&ep->drain_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 for (i = 0; i < 0x10; ++i)
1384 if (ep_info->out_cables & (1 << i)) {
1385 ep->ports[i].ep = ep;
1386 ep->ports[i].cable = i << 4;
1387 }
1388
1389 if (umidi->usb_protocol_ops->init_out_endpoint)
1390 umidi->usb_protocol_ops->init_out_endpoint(ep);
1391
1392 rep->out = ep;
1393 return 0;
1394}
1395
1396/*
1397 * Frees everything.
1398 */
Adam Goodea5095742014-08-05 12:44:51 -04001399static void snd_usbmidi_free(struct snd_usb_midi *umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
1401 int i;
1402
1403 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Adam Goodea5095742014-08-05 12:44:51 -04001404 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 if (ep->out)
1406 snd_usbmidi_out_endpoint_delete(ep->out);
1407 if (ep->in)
1408 snd_usbmidi_in_endpoint_delete(ep->in);
1409 }
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001410 mutex_destroy(&umidi->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 kfree(umidi);
1412}
1413
1414/*
1415 * Unlinks all URBs (must be done before the usb_device is deleted).
1416 */
Adam Goodea5095742014-08-05 12:44:51 -04001417void snd_usbmidi_disconnect(struct list_head *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418{
Adam Goodea5095742014-08-05 12:44:51 -04001419 struct snd_usb_midi *umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001420 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Takashi Iwai86e07d32005-11-17 15:08:02 +01001422 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001423 /*
1424 * an URB's completion handler may start the timer and
1425 * a timer may submit an URB. To reliably break the cycle
1426 * a flag under lock must be used
1427 */
Takashi Iwai59866da2012-12-03 11:12:46 +01001428 down_write(&umidi->disc_rwsem);
Takashi Iwaic0792e02008-02-22 18:34:44 +01001429 spin_lock_irq(&umidi->disc_lock);
1430 umidi->disconnected = 1;
1431 spin_unlock_irq(&umidi->disc_lock);
Takashi Iwai59866da2012-12-03 11:12:46 +01001432 up_write(&umidi->disc_rwsem);
1433
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Adam Goodea5095742014-08-05 12:44:51 -04001435 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
Clemens Ladischc8846972005-08-02 15:26:52 +02001436 if (ep->out)
1437 tasklet_kill(&ep->out->tasklet);
Clemens Ladisched4affa2009-07-13 13:43:59 +02001438 if (ep->out) {
1439 for (j = 0; j < OUTPUT_URBS; ++j)
1440 usb_kill_urb(ep->out->urbs[j].urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (umidi->usb_protocol_ops->finish_out_endpoint)
1442 umidi->usb_protocol_ops->finish_out_endpoint(ep->out);
Takashi Iwai29aac002010-04-10 21:27:23 +02001443 ep->out->active_urbs = 0;
1444 if (ep->out->drain_urbs) {
1445 ep->out->drain_urbs = 0;
1446 wake_up(&ep->out->drain_wait);
1447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
Mariusz Kozlowskif5e135a2006-11-08 15:37:00 +01001449 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02001450 for (j = 0; j < INPUT_URBS; ++j)
1451 usb_kill_urb(ep->in->urbs[j]);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001452 /* free endpoints here; later call can result in Oops */
Takashi Iwai29aac002010-04-10 21:27:23 +02001453 if (ep->out)
1454 snd_usbmidi_out_endpoint_clear(ep->out);
Takashi Iwai7a17daa2008-10-02 14:50:22 +02001455 if (ep->in) {
1456 snd_usbmidi_in_endpoint_delete(ep->in);
1457 ep->in = NULL;
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Takashi Iwaic0792e02008-02-22 18:34:44 +01001460 del_timer_sync(&umidi->error_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461}
Eldad Zacked136ac2013-04-03 23:18:51 +02001462EXPORT_SYMBOL(snd_usbmidi_disconnect);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Takashi Iwai86e07d32005-11-17 15:08:02 +01001464static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Adam Goodea5095742014-08-05 12:44:51 -04001466 struct snd_usb_midi *umidi = rmidi->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 snd_usbmidi_free(umidi);
1468}
1469
Adam Goodea5095742014-08-05 12:44:51 -04001470static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi *umidi,
1471 int stream,
1472 int number)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Eldad Zack88766f02013-04-03 23:18:49 +02001474 struct snd_rawmidi_substream *substream;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Adam Goodea5095742014-08-05 12:44:51 -04001476 list_for_each_entry(substream, &umidi->rmidi->streams[stream].substreams,
1477 list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (substream->number == number)
1479 return substream;
1480 }
1481 return NULL;
1482}
1483
1484/*
1485 * This list specifies names for ports that do not fit into the standard
1486 * "(product) MIDI (n)" schema because they aren't external MIDI ports,
1487 * such as internal control or synthesizer ports.
1488 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001489static struct port_info {
Clemens Ladisch27d10f52005-05-02 08:51:26 +02001490 u32 id;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001491 short int port;
1492 short int voices;
1493 const char *name;
1494 unsigned int seq_flags;
1495} snd_usbmidi_port_info[] = {
1496#define PORT_INFO(vendor, product, num, name_, voices_, flags) \
1497 { .id = USB_ID(vendor, product), \
1498 .port = num, .voices = voices_, \
1499 .name = name_, .seq_flags = flags }
1500#define EXTERNAL_PORT(vendor, product, num, name) \
1501 PORT_INFO(vendor, product, num, name, 0, \
1502 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1503 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1504 SNDRV_SEQ_PORT_TYPE_PORT)
1505#define CONTROL_PORT(vendor, product, num, name) \
1506 PORT_INFO(vendor, product, num, name, 0, \
1507 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1508 SNDRV_SEQ_PORT_TYPE_HARDWARE)
1509#define ROLAND_SYNTH_PORT(vendor, product, num, name, voices) \
1510 PORT_INFO(vendor, product, num, name, voices, \
1511 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC | \
1512 SNDRV_SEQ_PORT_TYPE_MIDI_GM | \
1513 SNDRV_SEQ_PORT_TYPE_MIDI_GM2 | \
1514 SNDRV_SEQ_PORT_TYPE_MIDI_GS | \
1515 SNDRV_SEQ_PORT_TYPE_MIDI_XG | \
1516 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1517 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
1518#define SOUNDCANVAS_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_MIDI_MT32 | \
1526 SNDRV_SEQ_PORT_TYPE_HARDWARE | \
1527 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 /* Roland UA-100 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001529 CONTROL_PORT(0x0582, 0x0000, 2, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /* Roland SC-8850 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001531 SOUNDCANVAS_PORT(0x0582, 0x0003, 0, "%s Part A", 128),
1532 SOUNDCANVAS_PORT(0x0582, 0x0003, 1, "%s Part B", 128),
1533 SOUNDCANVAS_PORT(0x0582, 0x0003, 2, "%s Part C", 128),
1534 SOUNDCANVAS_PORT(0x0582, 0x0003, 3, "%s Part D", 128),
1535 EXTERNAL_PORT(0x0582, 0x0003, 4, "%s MIDI 1"),
1536 EXTERNAL_PORT(0x0582, 0x0003, 5, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 /* Roland U-8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001538 EXTERNAL_PORT(0x0582, 0x0004, 0, "%s MIDI"),
1539 CONTROL_PORT(0x0582, 0x0004, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 /* Roland SC-8820 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001541 SOUNDCANVAS_PORT(0x0582, 0x0007, 0, "%s Part A", 64),
1542 SOUNDCANVAS_PORT(0x0582, 0x0007, 1, "%s Part B", 64),
1543 EXTERNAL_PORT(0x0582, 0x0007, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 /* Roland SK-500 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001545 SOUNDCANVAS_PORT(0x0582, 0x000b, 0, "%s Part A", 64),
1546 SOUNDCANVAS_PORT(0x0582, 0x000b, 1, "%s Part B", 64),
1547 EXTERNAL_PORT(0x0582, 0x000b, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 /* Roland SC-D70 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001549 SOUNDCANVAS_PORT(0x0582, 0x000c, 0, "%s Part A", 64),
1550 SOUNDCANVAS_PORT(0x0582, 0x000c, 1, "%s Part B", 64),
1551 EXTERNAL_PORT(0x0582, 0x000c, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 /* Edirol UM-880 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001553 CONTROL_PORT(0x0582, 0x0014, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 /* Edirol SD-90 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001555 ROLAND_SYNTH_PORT(0x0582, 0x0016, 0, "%s Part A", 128),
1556 ROLAND_SYNTH_PORT(0x0582, 0x0016, 1, "%s Part B", 128),
1557 EXTERNAL_PORT(0x0582, 0x0016, 2, "%s MIDI 1"),
1558 EXTERNAL_PORT(0x0582, 0x0016, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 /* Edirol UM-550 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001560 CONTROL_PORT(0x0582, 0x0023, 5, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* Edirol SD-20 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001562 ROLAND_SYNTH_PORT(0x0582, 0x0027, 0, "%s Part A", 64),
1563 ROLAND_SYNTH_PORT(0x0582, 0x0027, 1, "%s Part B", 64),
1564 EXTERNAL_PORT(0x0582, 0x0027, 2, "%s MIDI"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 /* Edirol SD-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001566 ROLAND_SYNTH_PORT(0x0582, 0x0029, 0, "%s Part A", 128),
1567 ROLAND_SYNTH_PORT(0x0582, 0x0029, 1, "%s Part B", 128),
1568 EXTERNAL_PORT(0x0582, 0x0029, 2, "%s MIDI 1"),
1569 EXTERNAL_PORT(0x0582, 0x0029, 3, "%s MIDI 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 /* Edirol UA-700 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001571 EXTERNAL_PORT(0x0582, 0x002b, 0, "%s MIDI"),
1572 CONTROL_PORT(0x0582, 0x002b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /* Roland VariOS */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001574 EXTERNAL_PORT(0x0582, 0x002f, 0, "%s MIDI"),
1575 EXTERNAL_PORT(0x0582, 0x002f, 1, "%s External MIDI"),
1576 EXTERNAL_PORT(0x0582, 0x002f, 2, "%s Sync"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* Edirol PCR */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001578 EXTERNAL_PORT(0x0582, 0x0033, 0, "%s MIDI"),
1579 EXTERNAL_PORT(0x0582, 0x0033, 1, "%s 1"),
1580 EXTERNAL_PORT(0x0582, 0x0033, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 /* BOSS GS-10 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001582 EXTERNAL_PORT(0x0582, 0x003b, 0, "%s MIDI"),
1583 CONTROL_PORT(0x0582, 0x003b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /* Edirol UA-1000 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001585 EXTERNAL_PORT(0x0582, 0x0044, 0, "%s MIDI"),
1586 CONTROL_PORT(0x0582, 0x0044, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /* Edirol UR-80 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001588 EXTERNAL_PORT(0x0582, 0x0048, 0, "%s MIDI"),
1589 EXTERNAL_PORT(0x0582, 0x0048, 1, "%s 1"),
1590 EXTERNAL_PORT(0x0582, 0x0048, 2, "%s 2"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /* Edirol PCR-A */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001592 EXTERNAL_PORT(0x0582, 0x004d, 0, "%s MIDI"),
1593 EXTERNAL_PORT(0x0582, 0x004d, 1, "%s 1"),
1594 EXTERNAL_PORT(0x0582, 0x004d, 2, "%s 2"),
Clemens Ladischa9687822013-02-09 10:05:20 +01001595 /* BOSS GT-PRO */
1596 CONTROL_PORT(0x0582, 0x0089, 0, "%s Control"),
Clemens Ladisch7c79b762006-01-10 18:56:23 +01001597 /* Edirol UM-3EX */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001598 CONTROL_PORT(0x0582, 0x009a, 3, "%s Control"),
Clemens Ladischa9687822013-02-09 10:05:20 +01001599 /* Roland VG-99 */
1600 CONTROL_PORT(0x0582, 0x00b2, 0, "%s Control"),
1601 EXTERNAL_PORT(0x0582, 0x00b2, 1, "%s MIDI"),
1602 /* Cakewalk Sonar V-Studio 100 */
1603 EXTERNAL_PORT(0x0582, 0x00eb, 0, "%s MIDI"),
1604 CONTROL_PORT(0x0582, 0x00eb, 1, "%s Control"),
1605 /* Roland VB-99 */
1606 CONTROL_PORT(0x0582, 0x0102, 0, "%s Control"),
1607 EXTERNAL_PORT(0x0582, 0x0102, 1, "%s MIDI"),
1608 /* Roland A-PRO */
1609 EXTERNAL_PORT(0x0582, 0x010f, 0, "%s MIDI"),
1610 CONTROL_PORT(0x0582, 0x010f, 1, "%s 1"),
1611 CONTROL_PORT(0x0582, 0x010f, 2, "%s 2"),
1612 /* Roland SD-50 */
1613 ROLAND_SYNTH_PORT(0x0582, 0x0114, 0, "%s Synth", 128),
1614 EXTERNAL_PORT(0x0582, 0x0114, 1, "%s MIDI"),
1615 CONTROL_PORT(0x0582, 0x0114, 2, "%s Control"),
1616 /* Roland OCTA-CAPTURE */
1617 EXTERNAL_PORT(0x0582, 0x0120, 0, "%s MIDI"),
1618 CONTROL_PORT(0x0582, 0x0120, 1, "%s Control"),
1619 EXTERNAL_PORT(0x0582, 0x0121, 0, "%s MIDI"),
1620 CONTROL_PORT(0x0582, 0x0121, 1, "%s Control"),
1621 /* Roland SPD-SX */
1622 CONTROL_PORT(0x0582, 0x0145, 0, "%s Control"),
1623 EXTERNAL_PORT(0x0582, 0x0145, 1, "%s MIDI"),
1624 /* Roland A-Series */
1625 CONTROL_PORT(0x0582, 0x0156, 0, "%s Keyboard"),
1626 EXTERNAL_PORT(0x0582, 0x0156, 1, "%s MIDI"),
1627 /* Roland INTEGRA-7 */
1628 ROLAND_SYNTH_PORT(0x0582, 0x015b, 0, "%s Synth", 128),
1629 CONTROL_PORT(0x0582, 0x015b, 1, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /* M-Audio MidiSport 8x8 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001631 CONTROL_PORT(0x0763, 0x1031, 8, "%s Control"),
1632 CONTROL_PORT(0x0763, 0x1033, 8, "%s Control"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* MOTU Fastlane */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001634 EXTERNAL_PORT(0x07fd, 0x0001, 0, "%s MIDI A"),
1635 EXTERNAL_PORT(0x07fd, 0x0001, 1, "%s MIDI B"),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 /* Emagic Unitor8/AMT8/MT4 */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001637 EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
1638 EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
1639 EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
Krzysztof Foltman4434ade2010-05-20 20:31:10 +01001640 /* Akai MPD16 */
1641 CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
1642 PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
1643 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1644 SNDRV_SEQ_PORT_TYPE_HARDWARE),
Sebastien Alaiwand39e82d2010-02-16 08:55:08 +01001645 /* Access Music Virus TI */
1646 EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
1647 PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
1648 SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
1649 SNDRV_SEQ_PORT_TYPE_HARDWARE |
1650 SNDRV_SEQ_PORT_TYPE_SYNTHESIZER),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651};
1652
Adam Goodea5095742014-08-05 12:44:51 -04001653static struct port_info *find_port_info(struct snd_usb_midi *umidi, int number)
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001654{
1655 int i;
1656
1657 for (i = 0; i < ARRAY_SIZE(snd_usbmidi_port_info); ++i) {
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001658 if (snd_usbmidi_port_info[i].id == umidi->usb_id &&
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001659 snd_usbmidi_port_info[i].port == number)
1660 return &snd_usbmidi_port_info[i];
1661 }
1662 return NULL;
1663}
1664
1665static void snd_usbmidi_get_port_info(struct snd_rawmidi *rmidi, int number,
1666 struct snd_seq_port_info *seq_port_info)
1667{
1668 struct snd_usb_midi *umidi = rmidi->private_data;
1669 struct port_info *port_info;
1670
1671 /* TODO: read port flags from descriptors */
1672 port_info = find_port_info(umidi, number);
1673 if (port_info) {
1674 seq_port_info->type = port_info->seq_flags;
1675 seq_port_info->midi_voices = port_info->voices;
1676 }
1677}
1678
Adam Goodea5095742014-08-05 12:44:51 -04001679static void snd_usbmidi_init_substream(struct snd_usb_midi *umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int stream, int number,
Adam Goodea5095742014-08-05 12:44:51 -04001681 struct snd_rawmidi_substream **rsubstream)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001683 struct port_info *port_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 const char *name_format;
1685
Adam Goodea5095742014-08-05 12:44:51 -04001686 struct snd_rawmidi_substream *substream =
1687 snd_usbmidi_find_substream(umidi, stream, number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 if (!substream) {
Adam Goodea5095742014-08-05 12:44:51 -04001689 dev_err(&umidi->dev->dev, "substream %d:%d not found\n", stream,
1690 number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return;
1692 }
1693
1694 /* TODO: read port name from jack descriptor */
Clemens Ladischa7b928a2006-05-02 16:22:12 +02001695 port_info = find_port_info(umidi, number);
1696 name_format = port_info ? port_info->name : "%s MIDI %d";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 snprintf(substream->name, sizeof(substream->name),
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001698 name_format, umidi->card->shortname, number + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 *rsubstream = substream;
1701}
1702
1703/*
1704 * Creates the endpoints and their ports.
1705 */
Adam Goodea5095742014-08-05 12:44:51 -04001706static int snd_usbmidi_create_endpoints(struct snd_usb_midi *umidi,
1707 struct snd_usb_midi_endpoint_info *endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708{
1709 int i, j, err;
1710 int out_ports = 0, in_ports = 0;
1711
1712 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1713 if (endpoints[i].out_cables) {
Adam Goodea5095742014-08-05 12:44:51 -04001714 err = snd_usbmidi_out_endpoint_create(umidi,
1715 &endpoints[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 &umidi->endpoints[i]);
1717 if (err < 0)
1718 return err;
1719 }
1720 if (endpoints[i].in_cables) {
Adam Goodea5095742014-08-05 12:44:51 -04001721 err = snd_usbmidi_in_endpoint_create(umidi,
1722 &endpoints[i],
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 &umidi->endpoints[i]);
1724 if (err < 0)
1725 return err;
1726 }
1727
1728 for (j = 0; j < 0x10; ++j) {
1729 if (endpoints[i].out_cables & (1 << j)) {
Adam Goodea5095742014-08-05 12:44:51 -04001730 snd_usbmidi_init_substream(umidi,
1731 SNDRV_RAWMIDI_STREAM_OUTPUT,
1732 out_ports,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 &umidi->endpoints[i].out->ports[j].substream);
1734 ++out_ports;
1735 }
1736 if (endpoints[i].in_cables & (1 << j)) {
Adam Goodea5095742014-08-05 12:44:51 -04001737 snd_usbmidi_init_substream(umidi,
1738 SNDRV_RAWMIDI_STREAM_INPUT,
1739 in_ports,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 &umidi->endpoints[i].in->ports[j].substream);
1741 ++in_ports;
1742 }
1743 }
1744 }
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001745 dev_dbg(&umidi->dev->dev, "created %d output and %d input ports\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 out_ports, in_ports);
1747 return 0;
1748}
1749
1750/*
1751 * Returns MIDIStreaming device capabilities.
1752 */
Adam Goodea5095742014-08-05 12:44:51 -04001753static int snd_usbmidi_get_ms_info(struct snd_usb_midi *umidi,
1754 struct snd_usb_midi_endpoint_info *endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Adam Goodea5095742014-08-05 12:44:51 -04001756 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 struct usb_host_interface *hostif;
Adam Goodea5095742014-08-05 12:44:51 -04001758 struct usb_interface_descriptor *intfd;
1759 struct usb_ms_header_descriptor *ms_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 struct usb_host_endpoint *hostep;
Adam Goodea5095742014-08-05 12:44:51 -04001761 struct usb_endpoint_descriptor *ep;
1762 struct usb_ms_endpoint_descriptor *ms_ep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 int i, epidx;
1764
1765 intf = umidi->iface;
1766 if (!intf)
1767 return -ENXIO;
1768 hostif = &intf->altsetting[0];
1769 intfd = get_iface_desc(hostif);
Adam Goodea5095742014-08-05 12:44:51 -04001770 ms_header = (struct usb_ms_header_descriptor *)hostif->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 if (hostif->extralen >= 7 &&
1772 ms_header->bLength >= 7 &&
1773 ms_header->bDescriptorType == USB_DT_CS_INTERFACE &&
Daniel Mackde48c7b2010-02-22 23:49:13 +01001774 ms_header->bDescriptorSubtype == UAC_HEADER)
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001775 dev_dbg(&umidi->dev->dev, "MIDIStreaming version %02x.%02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 ms_header->bcdMSC[1], ms_header->bcdMSC[0]);
1777 else
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001778 dev_warn(&umidi->dev->dev,
1779 "MIDIStreaming interface descriptor not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 epidx = 0;
1782 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1783 hostep = &hostif->endpoint[i];
1784 ep = get_ep_desc(hostep);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001785 if (!usb_endpoint_xfer_bulk(ep) && !usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 continue;
Adam Goodea5095742014-08-05 12:44:51 -04001787 ms_ep = (struct usb_ms_endpoint_descriptor *)hostep->extra;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (hostep->extralen < 4 ||
1789 ms_ep->bLength < 4 ||
1790 ms_ep->bDescriptorType != USB_DT_CS_ENDPOINT ||
Daniel Mackde48c7b2010-02-22 23:49:13 +01001791 ms_ep->bDescriptorSubtype != UAC_MS_GENERAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 continue;
Julia Lawall42a6e662008-12-29 11:23:02 +01001793 if (usb_endpoint_dir_out(ep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (endpoints[epidx].out_ep) {
1795 if (++epidx >= MIDI_MAX_ENDPOINTS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001796 dev_warn(&umidi->dev->dev,
1797 "too many endpoints\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 break;
1799 }
1800 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001801 endpoints[epidx].out_ep = usb_endpoint_num(ep);
1802 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 endpoints[epidx].out_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001804 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001805 /*
1806 * Low speed bulk transfers don't exist, so
1807 * force interrupt transfers for devices like
1808 * ESI MIDI Mate that try to use them anyway.
1809 */
1810 endpoints[epidx].out_interval = 1;
Adam Goodea5095742014-08-05 12:44:51 -04001811 endpoints[epidx].out_cables =
1812 (1 << ms_ep->bNumEmbMIDIJack) - 1;
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001813 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
Adam Goodea5095742014-08-05 12:44:51 -04001814 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 } else {
1816 if (endpoints[epidx].in_ep) {
1817 if (++epidx >= MIDI_MAX_ENDPOINTS) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001818 dev_warn(&umidi->dev->dev,
1819 "too many endpoints\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 break;
1821 }
1822 }
Julia Lawall42a6e662008-12-29 11:23:02 +01001823 endpoints[epidx].in_ep = usb_endpoint_num(ep);
1824 if (usb_endpoint_xfer_int(ep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 endpoints[epidx].in_interval = ep->bInterval;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001826 else if (snd_usb_get_speed(umidi->dev) == USB_SPEED_LOW)
Clemens Ladisch56162aa2007-08-21 08:57:34 +02001827 endpoints[epidx].in_interval = 1;
Adam Goodea5095742014-08-05 12:44:51 -04001828 endpoints[epidx].in_cables =
1829 (1 << ms_ep->bNumEmbMIDIJack) - 1;
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001830 dev_dbg(&umidi->dev->dev, "EP %02X: %d jack(s)\n",
Adam Goodea5095742014-08-05 12:44:51 -04001831 ep->bEndpointAddress, ms_ep->bNumEmbMIDIJack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833 }
1834 return 0;
1835}
1836
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001837static int roland_load_info(struct snd_kcontrol *kcontrol,
1838 struct snd_ctl_elem_info *info)
1839{
1840 static const char *const names[] = { "High Load", "Light Load" };
1841
Clemens Ladisch2a1803a2011-01-10 16:30:13 +01001842 return snd_ctl_enum_info(info, 1, 2, names);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001843}
1844
1845static int roland_load_get(struct snd_kcontrol *kcontrol,
1846 struct snd_ctl_elem_value *value)
1847{
1848 value->value.enumerated.item[0] = kcontrol->private_value;
1849 return 0;
1850}
1851
1852static int roland_load_put(struct snd_kcontrol *kcontrol,
1853 struct snd_ctl_elem_value *value)
1854{
Adam Goodea5095742014-08-05 12:44:51 -04001855 struct snd_usb_midi *umidi = kcontrol->private_data;
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001856 int changed;
1857
1858 if (value->value.enumerated.item[0] > 1)
1859 return -EINVAL;
1860 mutex_lock(&umidi->mutex);
1861 changed = value->value.enumerated.item[0] != kcontrol->private_value;
1862 if (changed)
1863 kcontrol->private_value = value->value.enumerated.item[0];
1864 mutex_unlock(&umidi->mutex);
1865 return changed;
1866}
1867
1868static struct snd_kcontrol_new roland_load_ctl = {
1869 .iface = SNDRV_CTL_ELEM_IFACE_MIXER,
1870 .name = "MIDI Input Mode",
1871 .info = roland_load_info,
1872 .get = roland_load_get,
1873 .put = roland_load_put,
1874 .private_value = 1,
1875};
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877/*
1878 * On Roland devices, use the second alternate setting to be able to use
1879 * the interrupt input endpoint.
1880 */
Adam Goodea5095742014-08-05 12:44:51 -04001881static void snd_usbmidi_switch_roland_altsetting(struct snd_usb_midi *umidi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Adam Goodea5095742014-08-05 12:44:51 -04001883 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 struct usb_host_interface *hostif;
Adam Goodea5095742014-08-05 12:44:51 -04001885 struct usb_interface_descriptor *intfd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 intf = umidi->iface;
1888 if (!intf || intf->num_altsetting != 2)
1889 return;
1890
1891 hostif = &intf->altsetting[1];
1892 intfd = get_iface_desc(hostif);
1893 if (intfd->bNumEndpoints != 2 ||
Adam Goodea5095742014-08-05 12:44:51 -04001894 (get_endpoint(hostif, 0)->bmAttributes &
1895 USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_BULK ||
1896 (get_endpoint(hostif, 1)->bmAttributes &
1897 USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_INT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 return;
1899
Takashi Iwai0ba41d92014-02-26 13:02:17 +01001900 dev_dbg(&umidi->dev->dev, "switching to altsetting %d with int ep\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 intfd->bAlternateSetting);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001902 usb_set_interface(umidi->dev, intfd->bInterfaceNumber,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 intfd->bAlternateSetting);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001904
1905 umidi->roland_load_ctl = snd_ctl_new1(&roland_load_ctl, umidi);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001906 if (snd_ctl_add(umidi->card, umidi->roland_load_ctl) < 0)
Clemens Ladisch96f61d92009-10-22 09:06:19 +02001907 umidi->roland_load_ctl = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
1910/*
1911 * Try to find any usable endpoints in the interface.
1912 */
Adam Goodea5095742014-08-05 12:44:51 -04001913static int snd_usbmidi_detect_endpoints(struct snd_usb_midi *umidi,
1914 struct snd_usb_midi_endpoint_info *endpoint,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 int max_endpoints)
1916{
Adam Goodea5095742014-08-05 12:44:51 -04001917 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct usb_host_interface *hostif;
Adam Goodea5095742014-08-05 12:44:51 -04001919 struct usb_interface_descriptor *intfd;
1920 struct usb_endpoint_descriptor *epd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 int i, out_eps = 0, in_eps = 0;
1922
Clemens Ladischd82af9f2009-11-16 12:23:46 +01001923 if (USB_ID_VENDOR(umidi->usb_id) == 0x0582)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 snd_usbmidi_switch_roland_altsetting(umidi);
1925
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001926 if (endpoint[0].out_ep || endpoint[0].in_ep)
Daniel Mack21af7d82010-06-16 17:57:29 +02001927 return 0;
Clemens Ladischc1ab5d52005-03-30 16:22:01 +02001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 intf = umidi->iface;
1930 if (!intf || intf->num_altsetting < 1)
1931 return -ENOENT;
1932 hostif = intf->cur_altsetting;
1933 intfd = get_iface_desc(hostif);
1934
1935 for (i = 0; i < intfd->bNumEndpoints; ++i) {
1936 epd = get_endpoint(hostif, i);
Julia Lawall913ae5a2009-01-03 17:54:53 +01001937 if (!usb_endpoint_xfer_bulk(epd) &&
1938 !usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 continue;
1940 if (out_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001941 usb_endpoint_dir_out(epd)) {
1942 endpoint[out_eps].out_ep = usb_endpoint_num(epd);
1943 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 endpoint[out_eps].out_interval = epd->bInterval;
1945 ++out_eps;
1946 }
1947 if (in_eps < max_endpoints &&
Julia Lawall42a6e662008-12-29 11:23:02 +01001948 usb_endpoint_dir_in(epd)) {
1949 endpoint[in_eps].in_ep = usb_endpoint_num(epd);
1950 if (usb_endpoint_xfer_int(epd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 endpoint[in_eps].in_interval = epd->bInterval;
1952 ++in_eps;
1953 }
1954 }
1955 return (out_eps || in_eps) ? 0 : -ENOENT;
1956}
1957
1958/*
1959 * Detects the endpoints for one-port-per-endpoint protocols.
1960 */
Adam Goodea5095742014-08-05 12:44:51 -04001961static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi *umidi,
1962 struct snd_usb_midi_endpoint_info *endpoints)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963{
1964 int err, i;
Daniel Mack21af7d82010-06-16 17:57:29 +02001965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
1967 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
1968 if (endpoints[i].out_ep)
1969 endpoints[i].out_cables = 0x0001;
1970 if (endpoints[i].in_ep)
1971 endpoints[i].in_cables = 0x0001;
1972 }
1973 return err;
1974}
1975
1976/*
1977 * Detects the endpoints and ports of Yamaha devices.
1978 */
Adam Goodea5095742014-08-05 12:44:51 -04001979static int snd_usbmidi_detect_yamaha(struct snd_usb_midi *umidi,
1980 struct snd_usb_midi_endpoint_info *endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981{
Adam Goodea5095742014-08-05 12:44:51 -04001982 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct usb_host_interface *hostif;
Adam Goodea5095742014-08-05 12:44:51 -04001984 struct usb_interface_descriptor *intfd;
1985 uint8_t *cs_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 intf = umidi->iface;
1988 if (!intf)
1989 return -ENOENT;
1990 hostif = intf->altsetting;
1991 intfd = get_iface_desc(hostif);
1992 if (intfd->bNumEndpoints < 1)
1993 return -ENOENT;
1994
1995 /*
1996 * For each port there is one MIDI_IN/OUT_JACK descriptor, not
1997 * necessarily with any useful contents. So simply count 'em.
1998 */
1999 for (cs_desc = hostif->extra;
2000 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
2001 cs_desc += cs_desc[0]) {
Ben Williamsonc4a87ef2006-06-19 17:20:09 +02002002 if (cs_desc[1] == USB_DT_CS_INTERFACE) {
Daniel Mackde48c7b2010-02-22 23:49:13 +01002003 if (cs_desc[2] == UAC_MIDI_IN_JACK)
Adam Goodea5095742014-08-05 12:44:51 -04002004 endpoint->in_cables =
2005 (endpoint->in_cables << 1) | 1;
Daniel Mackde48c7b2010-02-22 23:49:13 +01002006 else if (cs_desc[2] == UAC_MIDI_OUT_JACK)
Adam Goodea5095742014-08-05 12:44:51 -04002007 endpoint->out_cables =
2008 (endpoint->out_cables << 1) | 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010 }
2011 if (!endpoint->in_cables && !endpoint->out_cables)
2012 return -ENOENT;
2013
2014 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
2015}
2016
2017/*
Clemens Ladischaafe77c2013-03-31 23:43:12 +02002018 * Detects the endpoints and ports of Roland devices.
2019 */
Adam Goodea5095742014-08-05 12:44:51 -04002020static int snd_usbmidi_detect_roland(struct snd_usb_midi *umidi,
2021 struct snd_usb_midi_endpoint_info *endpoint)
Clemens Ladischaafe77c2013-03-31 23:43:12 +02002022{
Adam Goodea5095742014-08-05 12:44:51 -04002023 struct usb_interface *intf;
Clemens Ladischaafe77c2013-03-31 23:43:12 +02002024 struct usb_host_interface *hostif;
Adam Goodea5095742014-08-05 12:44:51 -04002025 u8 *cs_desc;
Clemens Ladischaafe77c2013-03-31 23:43:12 +02002026
2027 intf = umidi->iface;
2028 if (!intf)
2029 return -ENOENT;
2030 hostif = intf->altsetting;
2031 /*
2032 * Some devices have a descriptor <06 24 F1 02 <inputs> <outputs>>,
2033 * some have standard class descriptors, or both kinds, or neither.
2034 */
2035 for (cs_desc = hostif->extra;
2036 cs_desc < hostif->extra + hostif->extralen && cs_desc[0] >= 2;
2037 cs_desc += cs_desc[0]) {
2038 if (cs_desc[0] >= 6 &&
2039 cs_desc[1] == USB_DT_CS_INTERFACE &&
2040 cs_desc[2] == 0xf1 &&
2041 cs_desc[3] == 0x02) {
2042 endpoint->in_cables = (1 << cs_desc[4]) - 1;
2043 endpoint->out_cables = (1 << cs_desc[5]) - 1;
2044 return snd_usbmidi_detect_endpoints(umidi, endpoint, 1);
2045 } else if (cs_desc[0] >= 7 &&
2046 cs_desc[1] == USB_DT_CS_INTERFACE &&
2047 cs_desc[2] == UAC_HEADER) {
2048 return snd_usbmidi_get_ms_info(umidi, endpoint);
2049 }
2050 }
2051
2052 return -ENODEV;
2053}
2054
2055/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 * Creates the endpoints and their ports for Midiman devices.
2057 */
Adam Goodea5095742014-08-05 12:44:51 -04002058static int snd_usbmidi_create_endpoints_midiman(struct snd_usb_midi *umidi,
2059 struct snd_usb_midi_endpoint_info *endpoint)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002061 struct snd_usb_midi_endpoint_info ep_info;
Adam Goodea5095742014-08-05 12:44:51 -04002062 struct usb_interface *intf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 struct usb_host_interface *hostif;
Adam Goodea5095742014-08-05 12:44:51 -04002064 struct usb_interface_descriptor *intfd;
2065 struct usb_endpoint_descriptor *epd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 int cable, err;
2067
2068 intf = umidi->iface;
2069 if (!intf)
2070 return -ENOENT;
2071 hostif = intf->altsetting;
2072 intfd = get_iface_desc(hostif);
2073 /*
2074 * The various MidiSport devices have more or less random endpoint
2075 * numbers, so we have to identify the endpoints by their index in
2076 * the descriptor array, like the driver for that other OS does.
2077 *
2078 * There is one interrupt input endpoint for all input ports, one
2079 * bulk output endpoint for even-numbered ports, and one for odd-
2080 * numbered ports. Both bulk output endpoints have corresponding
2081 * input bulk endpoints (at indices 1 and 3) which aren't used.
2082 */
2083 if (intfd->bNumEndpoints < (endpoint->out_cables > 0x0001 ? 5 : 3)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002084 dev_dbg(&umidi->dev->dev, "not enough endpoints\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 return -ENOENT;
2086 }
2087
2088 epd = get_endpoint(hostif, 0);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002089 if (!usb_endpoint_dir_in(epd) || !usb_endpoint_xfer_int(epd)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002090 dev_dbg(&umidi->dev->dev, "endpoint[0] isn't interrupt\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 return -ENXIO;
2092 }
2093 epd = get_endpoint(hostif, 2);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002094 if (!usb_endpoint_dir_out(epd) || !usb_endpoint_xfer_bulk(epd)) {
Takashi Iwai0ba41d92014-02-26 13:02:17 +01002095 dev_dbg(&umidi->dev->dev, "endpoint[2] isn't bulk output\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 return -ENXIO;
2097 }
2098 if (endpoint->out_cables > 0x0001) {
2099 epd = get_endpoint(hostif, 4);
Julia Lawall913ae5a2009-01-03 17:54:53 +01002100 if (!usb_endpoint_dir_out(epd) ||
2101 !usb_endpoint_xfer_bulk(epd)) {
Adam Goodea5095742014-08-05 12:44:51 -04002102 dev_dbg(&umidi->dev->dev,
2103 "endpoint[4] isn't bulk output\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 return -ENXIO;
2105 }
2106 }
2107
Adam Goodea5095742014-08-05 12:44:51 -04002108 ep_info.out_ep = get_endpoint(hostif, 2)->bEndpointAddress &
2109 USB_ENDPOINT_NUMBER_MASK;
Clemens Ladische156ac42009-02-16 15:22:39 +01002110 ep_info.out_interval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 ep_info.out_cables = endpoint->out_cables & 0x5555;
Adam Goodea5095742014-08-05 12:44:51 -04002112 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
2113 &umidi->endpoints[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (err < 0)
2115 return err;
2116
Adam Goodea5095742014-08-05 12:44:51 -04002117 ep_info.in_ep = get_endpoint(hostif, 0)->bEndpointAddress &
2118 USB_ENDPOINT_NUMBER_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 ep_info.in_interval = get_endpoint(hostif, 0)->bInterval;
2120 ep_info.in_cables = endpoint->in_cables;
Adam Goodea5095742014-08-05 12:44:51 -04002121 err = snd_usbmidi_in_endpoint_create(umidi, &ep_info,
2122 &umidi->endpoints[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (err < 0)
2124 return err;
2125
2126 if (endpoint->out_cables > 0x0001) {
Adam Goodea5095742014-08-05 12:44:51 -04002127 ep_info.out_ep = get_endpoint(hostif, 4)->bEndpointAddress &
2128 USB_ENDPOINT_NUMBER_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 ep_info.out_cables = endpoint->out_cables & 0xaaaa;
Adam Goodea5095742014-08-05 12:44:51 -04002130 err = snd_usbmidi_out_endpoint_create(umidi, &ep_info,
2131 &umidi->endpoints[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (err < 0)
2133 return err;
2134 }
2135
2136 for (cable = 0; cable < 0x10; ++cable) {
2137 if (endpoint->out_cables & (1 << cable))
Adam Goodea5095742014-08-05 12:44:51 -04002138 snd_usbmidi_init_substream(umidi,
2139 SNDRV_RAWMIDI_STREAM_OUTPUT,
2140 cable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 &umidi->endpoints[cable & 1].out->ports[cable].substream);
2142 if (endpoint->in_cables & (1 << cable))
Adam Goodea5095742014-08-05 12:44:51 -04002143 snd_usbmidi_init_substream(umidi,
2144 SNDRV_RAWMIDI_STREAM_INPUT,
2145 cable,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 &umidi->endpoints[0].in->ports[cable].substream);
2147 }
2148 return 0;
2149}
2150
Clemens Ladischa7b928a2006-05-02 16:22:12 +02002151static struct snd_rawmidi_global_ops snd_usbmidi_ops = {
2152 .get_port_info = snd_usbmidi_get_port_info,
2153};
2154
Adam Goodea5095742014-08-05 12:44:51 -04002155static int snd_usbmidi_create_rawmidi(struct snd_usb_midi *umidi,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 int out_ports, int in_ports)
2157{
Takashi Iwai86e07d32005-11-17 15:08:02 +01002158 struct snd_rawmidi *rmidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 int err;
2160
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002161 err = snd_rawmidi_new(umidi->card, "USB MIDI",
2162 umidi->next_midi_device++,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 out_ports, in_ports, &rmidi);
2164 if (err < 0)
2165 return err;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002166 strcpy(rmidi->name, umidi->card->shortname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 rmidi->info_flags = SNDRV_RAWMIDI_INFO_OUTPUT |
2168 SNDRV_RAWMIDI_INFO_INPUT |
2169 SNDRV_RAWMIDI_INFO_DUPLEX;
Clemens Ladischa7b928a2006-05-02 16:22:12 +02002170 rmidi->ops = &snd_usbmidi_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 rmidi->private_data = umidi;
2172 rmidi->private_free = snd_usbmidi_rawmidi_free;
Adam Goodea5095742014-08-05 12:44:51 -04002173 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_OUTPUT,
2174 &snd_usbmidi_output_ops);
2175 snd_rawmidi_set_ops(rmidi, SNDRV_RAWMIDI_STREAM_INPUT,
2176 &snd_usbmidi_input_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 umidi->rmidi = rmidi;
2179 return 0;
2180}
2181
2182/*
2183 * Temporarily stop input.
2184 */
Adam Goodea5095742014-08-05 12:44:51 -04002185void snd_usbmidi_input_stop(struct list_head *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Adam Goodea5095742014-08-05 12:44:51 -04002187 struct snd_usb_midi *umidi;
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02002188 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Takashi Iwai86e07d32005-11-17 15:08:02 +01002190 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaif5f16542012-12-03 11:30:50 +01002191 if (!umidi->input_running)
2192 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Adam Goodea5095742014-08-05 12:44:51 -04002194 struct snd_usb_midi_endpoint *ep = &umidi->endpoints[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (ep->in)
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02002196 for (j = 0; j < INPUT_URBS; ++j)
2197 usb_kill_urb(ep->in->urbs[j]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Takashi Iwaif5f16542012-12-03 11:30:50 +01002199 umidi->input_running = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
Eldad Zacked136ac2013-04-03 23:18:51 +02002201EXPORT_SYMBOL(snd_usbmidi_input_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Adam Goodea5095742014-08-05 12:44:51 -04002203static void snd_usbmidi_input_start_ep(struct snd_usb_midi_in_endpoint *ep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Clemens Ladisch4773d1f2009-07-13 13:40:36 +02002205 unsigned int i;
2206
2207 if (!ep)
2208 return;
2209 for (i = 0; i < INPUT_URBS; ++i) {
Adam Goodea5095742014-08-05 12:44:51 -04002210 struct urb *urb = ep->urbs[i];
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002211 urb->dev = ep->umidi->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 snd_usbmidi_submit_urb(urb, GFP_KERNEL);
2213 }
2214}
2215
2216/*
2217 * Resume input after a call to snd_usbmidi_input_stop().
2218 */
Adam Goodea5095742014-08-05 12:44:51 -04002219void snd_usbmidi_input_start(struct list_head *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220{
Adam Goodea5095742014-08-05 12:44:51 -04002221 struct snd_usb_midi *umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 int i;
2223
Takashi Iwai86e07d32005-11-17 15:08:02 +01002224 umidi = list_entry(p, struct snd_usb_midi, list);
Takashi Iwaif5f16542012-12-03 11:30:50 +01002225 if (umidi->input_running || !umidi->opened[1])
2226 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
2228 snd_usbmidi_input_start_ep(umidi->endpoints[i].in);
Takashi Iwaif5f16542012-12-03 11:30:50 +01002229 umidi->input_running = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
Eldad Zacked136ac2013-04-03 23:18:51 +02002231EXPORT_SYMBOL(snd_usbmidi_input_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233/*
Adam Goodef7881e52014-08-05 12:44:50 -04002234 * Prepare for suspend. Typically called from the USB suspend callback.
2235 */
2236void snd_usbmidi_suspend(struct list_head *p)
2237{
2238 struct snd_usb_midi *umidi;
2239
2240 umidi = list_entry(p, struct snd_usb_midi, list);
2241 mutex_lock(&umidi->mutex);
2242 snd_usbmidi_input_stop(p);
2243 mutex_unlock(&umidi->mutex);
2244}
2245EXPORT_SYMBOL(snd_usbmidi_suspend);
2246
2247/*
2248 * Resume. Typically called from the USB resume callback.
2249 */
2250void snd_usbmidi_resume(struct list_head *p)
2251{
2252 struct snd_usb_midi *umidi;
2253
2254 umidi = list_entry(p, struct snd_usb_midi, list);
2255 mutex_lock(&umidi->mutex);
2256 snd_usbmidi_input_start(p);
2257 mutex_unlock(&umidi->mutex);
2258}
2259EXPORT_SYMBOL(snd_usbmidi_resume);
2260
2261/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 * Creates and registers everything needed for a MIDI streaming interface.
2263 */
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002264int snd_usbmidi_create(struct snd_card *card,
Adam Goodea5095742014-08-05 12:44:51 -04002265 struct usb_interface *iface,
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002266 struct list_head *midi_list,
Adam Goodea5095742014-08-05 12:44:51 -04002267 const struct snd_usb_audio_quirk *quirk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Adam Goodea5095742014-08-05 12:44:51 -04002269 struct snd_usb_midi *umidi;
Takashi Iwai86e07d32005-11-17 15:08:02 +01002270 struct snd_usb_midi_endpoint_info endpoints[MIDI_MAX_ENDPOINTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 int out_ports, in_ports;
2272 int i, err;
2273
Takashi Iwai561b2202005-09-09 14:22:34 +02002274 umidi = kzalloc(sizeof(*umidi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if (!umidi)
2276 return -ENOMEM;
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002277 umidi->dev = interface_to_usbdev(iface);
2278 umidi->card = card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 umidi->iface = iface;
2280 umidi->quirk = quirk;
2281 umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
Clemens Ladischc8846972005-08-02 15:26:52 +02002282 init_timer(&umidi->error_timer);
Takashi Iwaic0792e02008-02-22 18:34:44 +01002283 spin_lock_init(&umidi->disc_lock);
Takashi Iwai59866da2012-12-03 11:12:46 +01002284 init_rwsem(&umidi->disc_rwsem);
Clemens Ladisch96f61d92009-10-22 09:06:19 +02002285 mutex_init(&umidi->mutex);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002286 umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
2287 le16_to_cpu(umidi->dev->descriptor.idProduct));
Clemens Ladischc8846972005-08-02 15:26:52 +02002288 umidi->error_timer.function = snd_usbmidi_error_timer;
2289 umidi->error_timer.data = (unsigned long)umidi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
2291 /* detect the endpoint(s) to use */
2292 memset(endpoints, 0, sizeof(endpoints));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002293 switch (quirk ? quirk->type : QUIRK_MIDI_STANDARD_INTERFACE) {
2294 case QUIRK_MIDI_STANDARD_INTERFACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 err = snd_usbmidi_get_ms_info(umidi, endpoints);
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002296 if (umidi->usb_id == USB_ID(0x0763, 0x0150)) /* M-Audio Uno */
Clemens Ladischd05cc10432007-05-07 09:28:53 +02002297 umidi->usb_protocol_ops =
2298 &snd_usbmidi_maudio_broken_running_status_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002299 break;
Karsten Wiese030a07e2008-07-30 15:13:29 +02002300 case QUIRK_MIDI_US122L:
2301 umidi->usb_protocol_ops = &snd_usbmidi_122l_ops;
2302 /* fall through */
Clemens Ladischd1bda042005-09-14 08:36:03 +02002303 case QUIRK_MIDI_FIXED_ENDPOINT:
2304 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002305 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002306 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2307 break;
2308 case QUIRK_MIDI_YAMAHA:
2309 err = snd_usbmidi_detect_yamaha(umidi, &endpoints[0]);
2310 break;
Clemens Ladischaafe77c2013-03-31 23:43:12 +02002311 case QUIRK_MIDI_ROLAND:
2312 err = snd_usbmidi_detect_roland(umidi, &endpoints[0]);
2313 break;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002314 case QUIRK_MIDI_MIDIMAN:
2315 umidi->usb_protocol_ops = &snd_usbmidi_midiman_ops;
2316 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002317 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002318 err = 0;
2319 break;
2320 case QUIRK_MIDI_NOVATION:
2321 umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
2322 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2323 break;
Clemens Ladischc7f57212010-10-22 18:20:48 +02002324 case QUIRK_MIDI_RAW_BYTES:
Clemens Ladischd1bda042005-09-14 08:36:03 +02002325 umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
Clemens Ladisch55de5ef2009-05-27 10:49:30 +02002326 /*
2327 * Interface 1 contains isochronous endpoints, but with the same
2328 * numbers as in interface 0. Since it is interface 1 that the
2329 * USB core has most recently seen, these descriptors are now
2330 * associated with the endpoint numbers. This will foul up our
2331 * attempts to submit bulk/interrupt URBs to the endpoints in
2332 * interface 0, so we have to make sure that the USB core looks
2333 * again at interface 0 by calling usb_set_interface() on it.
2334 */
Clemens Ladischc7f57212010-10-22 18:20:48 +02002335 if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
2336 usb_set_interface(umidi->dev, 0, 0);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002337 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2338 break;
2339 case QUIRK_MIDI_EMAGIC:
2340 umidi->usb_protocol_ops = &snd_usbmidi_emagic_ops;
2341 memcpy(&endpoints[0], quirk->data,
Takashi Iwai86e07d32005-11-17 15:08:02 +01002342 sizeof(struct snd_usb_midi_endpoint_info));
Clemens Ladischd1bda042005-09-14 08:36:03 +02002343 err = snd_usbmidi_detect_endpoints(umidi, &endpoints[0], 1);
2344 break;
Clemens Ladischcc7a59b2006-02-07 17:11:06 +01002345 case QUIRK_MIDI_CME:
Clemens Ladisch61870ae2007-08-16 08:44:51 +02002346 umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002347 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2348 break;
Krzysztof Foltman4434ade2010-05-20 20:31:10 +01002349 case QUIRK_MIDI_AKAI:
2350 umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
2351 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2352 /* endpoint 1 is input-only */
2353 endpoints[1].out_cables = 0;
2354 break;
Kristian Amlie1ef0e0a2011-08-26 13:19:49 +02002355 case QUIRK_MIDI_FTDI:
2356 umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
2357
2358 /* set baud rate to 31250 (48 MHz / 16 / 96) */
2359 err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
2360 3, 0x40, 0x60, 0, NULL, 0, 1000);
2361 if (err < 0)
2362 break;
2363
2364 err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
2365 break;
Clemens Ladischd1bda042005-09-14 08:36:03 +02002366 default:
Adam Goodea5095742014-08-05 12:44:51 -04002367 dev_err(&umidi->dev->dev, "invalid quirk type %d\n",
2368 quirk->type);
Clemens Ladischd1bda042005-09-14 08:36:03 +02002369 err = -ENXIO;
2370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
2372 if (err < 0) {
2373 kfree(umidi);
2374 return err;
2375 }
2376
2377 /* create rawmidi device */
2378 out_ports = 0;
2379 in_ports = 0;
2380 for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
Akinobu Mitafbc54392009-11-20 14:56:52 +09002381 out_ports += hweight16(endpoints[i].out_cables);
2382 in_ports += hweight16(endpoints[i].in_cables);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
2384 err = snd_usbmidi_create_rawmidi(umidi, out_ports, in_ports);
2385 if (err < 0) {
2386 kfree(umidi);
2387 return err;
2388 }
2389
2390 /* create endpoint/port structures */
2391 if (quirk && quirk->type == QUIRK_MIDI_MIDIMAN)
2392 err = snd_usbmidi_create_endpoints_midiman(umidi, &endpoints[0]);
2393 else
2394 err = snd_usbmidi_create_endpoints(umidi, endpoints);
2395 if (err < 0) {
2396 snd_usbmidi_free(umidi);
2397 return err;
2398 }
2399
Clemens Ladischcbc200b2013-04-15 15:59:51 +02002400 usb_autopm_get_interface_no_resume(umidi->iface);
2401
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002402 list_add_tail(&umidi->list, midi_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 return 0;
2404}
Clemens Ladischd82af9f2009-11-16 12:23:46 +01002405EXPORT_SYMBOL(snd_usbmidi_create);