ALSA: usb-audio: Avoid autopm calls after disconnection
[linux-2.6.git] / sound / usb / midi.c
index 2c1558c..c0054ee 100644 (file)
@@ -47,6 +47,7 @@
 #include <linux/usb.h>
 #include <linux/wait.h>
 #include <linux/usb/audio.h>
+#include <linux/module.h>
 
 #include <sound/core.h>
 #include <sound/control.h>
@@ -54,6 +55,7 @@
 #include <sound/asequencer.h>
 #include "usbaudio.h"
 #include "midi.h"
+#include "power.h"
 #include "helper.h"
 
 /*
@@ -114,6 +116,7 @@ struct snd_usb_midi {
        struct list_head list;
        struct timer_list error_timer;
        spinlock_t disc_lock;
+       struct rw_semaphore disc_rwsem;
        struct mutex mutex;
        u32 usb_id;
        int next_midi_device;
@@ -146,6 +149,7 @@ struct snd_usb_midi_out_endpoint {
                struct snd_usb_midi_out_endpoint* ep;
                struct snd_rawmidi_substream *substream;
                int active;
+               bool autopm_reference;
                uint8_t cable;          /* cable number << 4 */
                uint8_t state;
 #define STATE_UNKNOWN  0
@@ -434,7 +438,7 @@ static void snd_usbmidi_maudio_broken_running_status_input(
                        u8 cin = buffer[i] & 0x0f;
                        struct usbmidi_in_port *port = &ep->ports[cable];
                        int length;
-                       
+
                        length = snd_usbmidi_cin_length[cin];
                        if (cin == 0xf && buffer[i + 1] >= 0xf8)
                                ; /* realtime msg: no running status change */
@@ -628,13 +632,13 @@ static struct usb_protocol_ops snd_usbmidi_standard_ops = {
 
 static struct usb_protocol_ops snd_usbmidi_midiman_ops = {
        .input = snd_usbmidi_midiman_input,
-       .output = snd_usbmidi_standard_output, 
+       .output = snd_usbmidi_standard_output,
        .output_packet = snd_usbmidi_output_midiman_packet,
 };
 
 static struct usb_protocol_ops snd_usbmidi_maudio_broken_running_status_ops = {
        .input = snd_usbmidi_maudio_broken_running_status_input,
-       .output = snd_usbmidi_standard_output, 
+       .output = snd_usbmidi_standard_output,
        .output_packet = snd_usbmidi_output_standard_packet,
 };
 
@@ -645,6 +649,105 @@ static struct usb_protocol_ops snd_usbmidi_cme_ops = {
 };
 
 /*
+ * AKAI MPD16 protocol:
+ *
+ * For control port (endpoint 1):
+ * ==============================
+ * One or more chunks consisting of first byte of (0x10 | msg_len) and then a
+ * SysEx message (msg_len=9 bytes long).
+ *
+ * For data port (endpoint 2):
+ * ===========================
+ * One or more chunks consisting of first byte of (0x20 | msg_len) and then a
+ * MIDI message (msg_len bytes long)
+ *
+ * Messages sent: Active Sense, Note On, Poly Pressure, Control Change.
+ */
+static void snd_usbmidi_akai_input(struct snd_usb_midi_in_endpoint *ep,
+                                  uint8_t *buffer, int buffer_length)
+{
+       unsigned int pos = 0;
+       unsigned int len = (unsigned int)buffer_length;
+       while (pos < len) {
+               unsigned int port = (buffer[pos] >> 4) - 1;
+               unsigned int msg_len = buffer[pos] & 0x0f;
+               pos++;
+               if (pos + msg_len <= len && port < 2)
+                       snd_usbmidi_input_data(ep, 0, &buffer[pos], msg_len);
+               pos += msg_len;
+       }
+}
+
+#define MAX_AKAI_SYSEX_LEN 9
+
+static void snd_usbmidi_akai_output(struct snd_usb_midi_out_endpoint *ep,
+                                   struct urb *urb)
+{
+       uint8_t *msg;
+       int pos, end, count, buf_end;
+       uint8_t tmp[MAX_AKAI_SYSEX_LEN];
+       struct snd_rawmidi_substream *substream = ep->ports[0].substream;
+
+       if (!ep->ports[0].active)
+               return;
+
+       msg = urb->transfer_buffer + urb->transfer_buffer_length;
+       buf_end = ep->max_transfer - MAX_AKAI_SYSEX_LEN - 1;
+
+       /* only try adding more data when there's space for at least 1 SysEx */
+       while (urb->transfer_buffer_length < buf_end) {
+               count = snd_rawmidi_transmit_peek(substream,
+                                                 tmp, MAX_AKAI_SYSEX_LEN);
+               if (!count) {
+                       ep->ports[0].active = 0;
+                       return;
+               }
+               /* try to skip non-SysEx data */
+               for (pos = 0; pos < count && tmp[pos] != 0xF0; pos++)
+                       ;
+
+               if (pos > 0) {
+                       snd_rawmidi_transmit_ack(substream, pos);
+                       continue;
+               }
+
+               /* look for the start or end marker */
+               for (end = 1; end < count && tmp[end] < 0xF0; end++)
+                       ;
+
+               /* next SysEx started before the end of current one */
+               if (end < count && tmp[end] == 0xF0) {
+                       /* it's incomplete - drop it */
+                       snd_rawmidi_transmit_ack(substream, end);
+                       continue;
+               }
+               /* SysEx complete */
+               if (end < count && tmp[end] == 0xF7) {
+                       /* queue it, ack it, and get the next one */
+                       count = end + 1;
+                       msg[0] = 0x10 | count;
+                       memcpy(&msg[1], tmp, count);
+                       snd_rawmidi_transmit_ack(substream, count);
+                       urb->transfer_buffer_length += count + 1;
+                       msg += count + 1;
+                       continue;
+               }
+               /* less than 9 bytes and no end byte - wait for more */
+               if (count < MAX_AKAI_SYSEX_LEN) {
+                       ep->ports[0].active = 0;
+                       return;
+               }
+               /* 9 bytes and no end marker in sight - malformed, skip it */
+               snd_rawmidi_transmit_ack(substream, count);
+       }
+}
+
+static struct usb_protocol_ops snd_usbmidi_akai_ops = {
+       .input = snd_usbmidi_akai_input,
+       .output = snd_usbmidi_akai_output,
+};
+
+/*
  * Novation USB MIDI protocol: number of data bytes is in the first byte
  * (when receiving) (+1!) or in the second byte (when sending); data begins
  * at the third byte.
@@ -685,7 +788,7 @@ static struct usb_protocol_ops snd_usbmidi_novation_ops = {
 };
 
 /*
- * "raw" protocol: used by the MOTU FastLane.
+ * "raw" protocol: just move raw MIDI bytes from/to the endpoint
  */
 
 static void snd_usbmidi_raw_input(struct snd_usb_midi_in_endpoint* ep,
@@ -716,6 +819,22 @@ static struct usb_protocol_ops snd_usbmidi_raw_ops = {
        .output = snd_usbmidi_raw_output,
 };
 
+/*
+ * FTDI protocol: raw MIDI bytes, but input packets have two modem status bytes.
+ */
+
+static void snd_usbmidi_ftdi_input(struct snd_usb_midi_in_endpoint* ep,
+                                  uint8_t* buffer, int buffer_length)
+{
+       if (buffer_length > 2)
+               snd_usbmidi_input_data(ep, 0, buffer + 2, buffer_length - 2);
+}
+
+static struct usb_protocol_ops snd_usbmidi_ftdi_ops = {
+       .input = snd_usbmidi_ftdi_input,
+       .output = snd_usbmidi_raw_output,
+};
+
 static void snd_usbmidi_us122l_input(struct snd_usb_midi_in_endpoint *ep,
                                     uint8_t *buffer, int buffer_length)
 {
@@ -735,7 +854,14 @@ static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
 
        if (!ep->ports[0].active)
                return;
-       count = snd_usb_get_speed(ep->umidi->dev) == USB_SPEED_HIGH ? 1 : 2;
+       switch (snd_usb_get_speed(ep->umidi->dev)) {
+       case USB_SPEED_HIGH:
+       case USB_SPEED_SUPER:
+               count = 1;
+               break;
+       default:
+               count = 2;
+       }
        count = snd_rawmidi_transmit(ep->ports[0].substream,
                                     urb->transfer_buffer,
                                     count);
@@ -744,8 +870,8 @@ static void snd_usbmidi_us122l_output(struct snd_usb_midi_out_endpoint *ep,
                return;
        }
 
-       memset(urb->transfer_buffer + count, 0xFD, 9 - count);
-       urb->transfer_buffer_length = count;
+       memset(urb->transfer_buffer + count, 0xFD, ep->max_transfer - count);
+       urb->transfer_buffer_length = ep->max_transfer;
 }
 
 static struct usb_protocol_ops snd_usbmidi_122l_ops = {
@@ -913,6 +1039,12 @@ static void substream_open(struct snd_rawmidi_substream *substream, int open)
        struct snd_usb_midi* umidi = substream->rmidi->private_data;
        struct snd_kcontrol *ctl;
 
+       down_read(&umidi->disc_rwsem);
+       if (umidi->disconnected) {
+               up_read(&umidi->disc_rwsem);
+               return;
+       }
+
        mutex_lock(&umidi->mutex);
        if (open) {
                if (umidi->opened++ == 0 && umidi->roland_load_ctl) {
@@ -931,6 +1063,7 @@ static void substream_open(struct snd_rawmidi_substream *substream, int open)
                }
        }
        mutex_unlock(&umidi->mutex);
+       up_read(&umidi->disc_rwsem);
 }
 
 static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
@@ -938,6 +1071,7 @@ static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
        struct snd_usb_midi* umidi = substream->rmidi->private_data;
        struct usbmidi_out_port* port = NULL;
        int i, j;
+       int err;
 
        for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i)
                if (umidi->endpoints[i].out)
@@ -950,6 +1084,17 @@ static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
                snd_BUG();
                return -ENXIO;
        }
+
+       down_read(&umidi->disc_rwsem);
+       if (umidi->disconnected) {
+               up_read(&umidi->disc_rwsem);
+               return -ENODEV;
+       }
+       err = usb_autopm_get_interface(umidi->iface);
+       port->autopm_reference = err >= 0;
+       up_read(&umidi->disc_rwsem);
+       if (err < 0 && err != -EACCES)
+               return -EIO;
        substream->runtime->private_data = port;
        port->state = STATE_UNKNOWN;
        substream_open(substream, 1);
@@ -958,7 +1103,14 @@ static int snd_usbmidi_output_open(struct snd_rawmidi_substream *substream)
 
 static int snd_usbmidi_output_close(struct snd_rawmidi_substream *substream)
 {
+       struct snd_usb_midi* umidi = substream->rmidi->private_data;
+       struct usbmidi_out_port *port = substream->runtime->private_data;
+
        substream_open(substream, 0);
+       down_read(&umidi->disc_rwsem);
+       if (!umidi->disconnected && port->autopm_reference)
+               usb_autopm_put_interface(umidi->iface);
+       up_read(&umidi->disc_rwsem);
        return 0;
 }
 
@@ -1048,8 +1200,8 @@ static struct snd_rawmidi_ops snd_usbmidi_input_ops = {
 static void free_urb_and_buffer(struct snd_usb_midi *umidi, struct urb *urb,
                                unsigned int buffer_length)
 {
-       usb_buffer_free(umidi->dev, buffer_length,
-                       urb->transfer_buffer, urb->transfer_dma);
+       usb_free_coherent(umidi->dev, buffer_length,
+                         urb->transfer_buffer, urb->transfer_dma);
        usb_free_urb(urb);
 }
 
@@ -1100,8 +1252,8 @@ static int snd_usbmidi_in_endpoint_create(struct snd_usb_midi* umidi,
                pipe = usb_rcvbulkpipe(umidi->dev, ep_info->in_ep);
        length = usb_maxpacket(umidi->dev, pipe, 0);
        for (i = 0; i < INPUT_URBS; ++i) {
-               buffer = usb_buffer_alloc(umidi->dev, length, GFP_KERNEL,
-                                         &ep->urbs[i]->transfer_dma);
+               buffer = usb_alloc_coherent(umidi->dev, length, GFP_KERNEL,
+                                           &ep->urbs[i]->transfer_dma);
                if (!buffer) {
                        snd_usbmidi_in_endpoint_delete(ep);
                        return -ENOMEM;
@@ -1149,7 +1301,7 @@ static void snd_usbmidi_out_endpoint_delete(struct snd_usb_midi_out_endpoint *ep
  */
 static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
                                           struct snd_usb_midi_endpoint_info* ep_info,
-                                          struct snd_usb_midi_endpoint* rep)
+                                          struct snd_usb_midi_endpoint* rep)
 {
        struct snd_usb_midi_out_endpoint* ep;
        unsigned int i;
@@ -1187,13 +1339,21 @@ static int snd_usbmidi_out_endpoint_create(struct snd_usb_midi* umidi,
        case USB_ID(0x15ca, 0x0101): /* Textech USB Midi Cable */
        case USB_ID(0x15ca, 0x1806): /* Textech USB Midi Cable */
        case USB_ID(0x1a86, 0x752d): /* QinHeng CH345 "USB2.0-MIDI" */
+       case USB_ID(0xfc08, 0x0101): /* Unknown vendor Cable */
                ep->max_transfer = 4;
                break;
+               /*
+                * Some devices only work with 9 bytes packet size:
+                */
+       case USB_ID(0x0644, 0x800E): /* Tascam US-122L */
+       case USB_ID(0x0644, 0x800F): /* Tascam US-144 */
+               ep->max_transfer = 9;
+               break;
        }
        for (i = 0; i < OUTPUT_URBS; ++i) {
-               buffer = usb_buffer_alloc(umidi->dev,
-                                         ep->max_transfer, GFP_KERNEL,
-                                         &ep->urbs[i].urb->transfer_dma);
+               buffer = usb_alloc_coherent(umidi->dev,
+                                           ep->max_transfer, GFP_KERNEL,
+                                           &ep->urbs[i].urb->transfer_dma);
                if (!buffer) {
                        snd_usbmidi_out_endpoint_delete(ep);
                        return -ENOMEM;
@@ -1260,9 +1420,12 @@ void snd_usbmidi_disconnect(struct list_head* p)
         * a timer may submit an URB. To reliably break the cycle
         * a flag under lock must be used
         */
+       down_write(&umidi->disc_rwsem);
        spin_lock_irq(&umidi->disc_lock);
        umidi->disconnected = 1;
        spin_unlock_irq(&umidi->disc_lock);
+       up_write(&umidi->disc_rwsem);
+
        for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
                struct snd_usb_midi_endpoint* ep = &umidi->endpoints[i];
                if (ep->out)
@@ -1299,7 +1462,7 @@ static void snd_usbmidi_rawmidi_free(struct snd_rawmidi *rmidi)
 }
 
 static struct snd_rawmidi_substream *snd_usbmidi_find_substream(struct snd_usb_midi* umidi,
-                                                          int stream, int number)
+                                                               int stream, int number)
 {
        struct list_head* list;
 
@@ -1434,6 +1597,11 @@ static struct port_info {
        EXTERNAL_PORT(0x086a, 0x0001, 8, "%s Broadcast"),
        EXTERNAL_PORT(0x086a, 0x0002, 8, "%s Broadcast"),
        EXTERNAL_PORT(0x086a, 0x0003, 4, "%s Broadcast"),
+       /* Akai MPD16 */
+       CONTROL_PORT(0x09e8, 0x0062, 0, "%s Control"),
+       PORT_INFO(0x09e8, 0x0062, 1, "%s MIDI", 0,
+               SNDRV_SEQ_PORT_TYPE_MIDI_GENERIC |
+               SNDRV_SEQ_PORT_TYPE_HARDWARE),
        /* Access Music Virus TI */
        EXTERNAL_PORT(0x133e, 0x0815, 0, "%s MIDI"),
        PORT_INFO(0x133e, 0x0815, 1, "%s Synth", 0,
@@ -1618,13 +1786,7 @@ static int roland_load_info(struct snd_kcontrol *kcontrol,
 {
        static const char *const names[] = { "High Load", "Light Load" };
 
-       info->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
-       info->count = 1;
-       info->value.enumerated.items = 2;
-       if (info->value.enumerated.item > 1)
-               info->value.enumerated.item = 1;
-       strcpy(info->value.enumerated.name, names[info->value.enumerated.item]);
-       return 0;
+       return snd_ctl_enum_info(info, 1, 2, names);
 }
 
 static int roland_load_get(struct snd_kcontrol *kcontrol,
@@ -1707,7 +1869,7 @@ static int snd_usbmidi_detect_endpoints(struct snd_usb_midi* umidi,
                snd_usbmidi_switch_roland_altsetting(umidi);
 
        if (endpoint[0].out_ep || endpoint[0].in_ep)
-               return 0;       
+               return 0;
 
        intf = umidi->iface;
        if (!intf || intf->num_altsetting < 1)
@@ -1745,7 +1907,7 @@ static int snd_usbmidi_detect_per_port_endpoints(struct snd_usb_midi* umidi,
                                                 struct snd_usb_midi_endpoint_info* endpoints)
 {
        int err, i;
-       
+
        err = snd_usbmidi_detect_endpoints(umidi, endpoints, MIDI_MAX_ENDPOINTS);
        for (i = 0; i < MIDI_MAX_ENDPOINTS; ++i) {
                if (endpoints[i].out_ep)
@@ -1975,6 +2137,7 @@ int snd_usbmidi_create(struct snd_card *card,
        umidi->usb_protocol_ops = &snd_usbmidi_standard_ops;
        init_timer(&umidi->error_timer);
        spin_lock_init(&umidi->disc_lock);
+       init_rwsem(&umidi->disc_rwsem);
        mutex_init(&umidi->mutex);
        umidi->usb_id = USB_ID(le16_to_cpu(umidi->dev->descriptor.idVendor),
                               le16_to_cpu(umidi->dev->descriptor.idProduct));
@@ -2011,7 +2174,7 @@ int snd_usbmidi_create(struct snd_card *card,
                umidi->usb_protocol_ops = &snd_usbmidi_novation_ops;
                err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
                break;
-       case QUIRK_MIDI_FASTLANE:
+       case QUIRK_MIDI_RAW_BYTES:
                umidi->usb_protocol_ops = &snd_usbmidi_raw_ops;
                /*
                 * Interface 1 contains isochronous endpoints, but with the same
@@ -2022,7 +2185,8 @@ int snd_usbmidi_create(struct snd_card *card,
                 * interface 0, so we have to make sure that the USB core looks
                 * again at interface 0 by calling usb_set_interface() on it.
                 */
-               usb_set_interface(umidi->dev, 0, 0);
+               if (umidi->usb_id == USB_ID(0x07fd, 0x0001)) /* MOTU Fastlane */
+                       usb_set_interface(umidi->dev, 0, 0);
                err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
                break;
        case QUIRK_MIDI_EMAGIC:
@@ -2035,6 +2199,23 @@ int snd_usbmidi_create(struct snd_card *card,
                umidi->usb_protocol_ops = &snd_usbmidi_cme_ops;
                err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
                break;
+       case QUIRK_MIDI_AKAI:
+               umidi->usb_protocol_ops = &snd_usbmidi_akai_ops;
+               err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
+               /* endpoint 1 is input-only */
+               endpoints[1].out_cables = 0;
+               break;
+       case QUIRK_MIDI_FTDI:
+               umidi->usb_protocol_ops = &snd_usbmidi_ftdi_ops;
+
+               /* set baud rate to 31250 (48 MHz / 16 / 96) */
+               err = usb_control_msg(umidi->dev, usb_sndctrlpipe(umidi->dev, 0),
+                                     3, 0x40, 0x60, 0, NULL, 0, 1000);
+               if (err < 0)
+                       break;
+
+               err = snd_usbmidi_detect_per_port_endpoints(umidi, endpoints);
+               break;
        default:
                snd_printd(KERN_ERR "invalid quirk type %d\n", quirk->type);
                err = -ENXIO;