sound: seq_midi_event: fix decoding of (N)RPN events
[pandora-kernel.git] / sound / core / seq / seq_midi_event.c
index 603b637..b5d6ea4 100644 (file)
@@ -2,7 +2,7 @@
  *  MIDI byte <-> sequencer event coder
  *
  *  Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>,
- *                        Jaroslav Kysela <perex@suse.cz>
+ *                        Jaroslav Kysela <perex@perex.cz>
  *
  *   This program is free software; you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
@@ -19,7 +19,6 @@
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
 
-#include <sound/driver.h>
 #include <linux/slab.h>
 #include <linux/errno.h>
 #include <linux/string.h>
 #include <sound/seq_midi_event.h>
 #include <sound/asoundef.h>
 
-MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@suse.cz>");
+MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>, Jaroslav Kysela <perex@perex.cz>");
 MODULE_DESCRIPTION("MIDI byte <-> sequencer event coder");
 MODULE_LICENSE("GPL");
 
-/* queue type */
-/* from 0 to 7 are normal commands (note off, on, etc.) */
-#define ST_NOTEOFF     0
-#define ST_NOTEON      1
+/* event type, index into status_event[] */
+/* from 0 to 6 are normal commands (note off, on, etc.) for 0x9?-0xe? */
+#define ST_INVALID     7
 #define ST_SPECIAL     8
 #define ST_SYSEX       ST_SPECIAL
 /* from 8 to 15 are events for 0xf0-0xf7 */
 
 
-/* status event types */
-typedef void (*event_encode_t)(snd_midi_event_t *dev, snd_seq_event_t *ev);
-typedef void (*event_decode_t)(snd_seq_event_t *ev, unsigned char *buf);
-
 /*
  * prototypes
  */
-static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
-static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
-static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
-static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
-static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
-static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev);
-static void note_decode(snd_seq_event_t *ev, unsigned char *buf);
-static void one_param_decode(snd_seq_event_t *ev, unsigned char *buf);
-static void pitchbend_decode(snd_seq_event_t *ev, unsigned char *buf);
-static void two_param_decode(snd_seq_event_t *ev, unsigned char *buf);
-static void songpos_decode(snd_seq_event_t *ev, unsigned char *buf);
+static void note_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
+static void one_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
+static void pitchbend_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
+static void two_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
+static void one_param_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
+static void songpos_event(struct snd_midi_event *dev, struct snd_seq_event *ev);
+static void note_decode(struct snd_seq_event *ev, unsigned char *buf);
+static void one_param_decode(struct snd_seq_event *ev, unsigned char *buf);
+static void pitchbend_decode(struct snd_seq_event *ev, unsigned char *buf);
+static void two_param_decode(struct snd_seq_event *ev, unsigned char *buf);
+static void songpos_decode(struct snd_seq_event *ev, unsigned char *buf);
 
 /*
  * event list
  */
-static struct status_event_list_t {
+static struct status_event_list {
        int event;
        int qlen;
-       event_encode_t encode;
-       event_decode_t decode;
+       void (*encode)(struct snd_midi_event *dev, struct snd_seq_event *ev);
+       void (*decode)(struct snd_seq_event *ev, unsigned char *buf);
 } status_event[] = {
-       /* 0x80 - 0xf0 */
-       {SNDRV_SEQ_EVENT_NOTEOFF,       2, note_event, note_decode},
-       {SNDRV_SEQ_EVENT_NOTEON,        2, note_event, note_decode},
-       {SNDRV_SEQ_EVENT_KEYPRESS,      2, note_event, note_decode},
-       {SNDRV_SEQ_EVENT_CONTROLLER,    2, two_param_ctrl_event, two_param_decode},
-       {SNDRV_SEQ_EVENT_PGMCHANGE,     1, one_param_ctrl_event, one_param_decode},
-       {SNDRV_SEQ_EVENT_CHANPRESS,     1, one_param_ctrl_event, one_param_decode},
-       {SNDRV_SEQ_EVENT_PITCHBEND,     2, pitchbend_ctrl_event, pitchbend_decode},
-       {SNDRV_SEQ_EVENT_NONE,          0, NULL, NULL}, /* 0xf0 */
+       /* 0x80 - 0xef */
+       {SNDRV_SEQ_EVENT_NOTEOFF,        2, note_event, note_decode},
+       {SNDRV_SEQ_EVENT_NOTEON,         2, note_event, note_decode},
+       {SNDRV_SEQ_EVENT_KEYPRESS,       2, note_event, note_decode},
+       {SNDRV_SEQ_EVENT_CONTROLLER,     2, two_param_ctrl_event, two_param_decode},
+       {SNDRV_SEQ_EVENT_PGMCHANGE,      1, one_param_ctrl_event, one_param_decode},
+       {SNDRV_SEQ_EVENT_CHANPRESS,      1, one_param_ctrl_event, one_param_decode},
+       {SNDRV_SEQ_EVENT_PITCHBEND,      2, pitchbend_ctrl_event, pitchbend_decode},
+       /* invalid */
+       {SNDRV_SEQ_EVENT_NONE,          -1, NULL, NULL},
        /* 0xf0 - 0xff */
-       {SNDRV_SEQ_EVENT_SYSEX,         1, NULL, NULL}, /* sysex: 0xf0 */
-       {SNDRV_SEQ_EVENT_QFRAME,        1, one_param_event, one_param_decode}, /* 0xf1 */
-       {SNDRV_SEQ_EVENT_SONGPOS,       2, songpos_event, songpos_decode}, /* 0xf2 */
-       {SNDRV_SEQ_EVENT_SONGSEL,       1, one_param_event, one_param_decode}, /* 0xf3 */
-       {SNDRV_SEQ_EVENT_NONE,          0, NULL, NULL}, /* 0xf4 */
-       {SNDRV_SEQ_EVENT_NONE,          0, NULL, NULL}, /* 0xf5 */
-       {SNDRV_SEQ_EVENT_TUNE_REQUEST,  0, NULL, NULL}, /* 0xf6 */
-       {SNDRV_SEQ_EVENT_NONE,          0, NULL, NULL}, /* 0xf7 */
-       {SNDRV_SEQ_EVENT_CLOCK,         0, NULL, NULL}, /* 0xf8 */
-       {SNDRV_SEQ_EVENT_NONE,          0, NULL, NULL}, /* 0xf9 */
-       {SNDRV_SEQ_EVENT_START,         0, NULL, NULL}, /* 0xfa */
-       {SNDRV_SEQ_EVENT_CONTINUE,      0, NULL, NULL}, /* 0xfb */
-       {SNDRV_SEQ_EVENT_STOP,          0, NULL, NULL}, /* 0xfc */
-       {SNDRV_SEQ_EVENT_NONE,          0, NULL, NULL}, /* 0xfd */
-       {SNDRV_SEQ_EVENT_SENSING,       0, NULL, NULL}, /* 0xfe */
-       {SNDRV_SEQ_EVENT_RESET,         0, NULL, NULL}, /* 0xff */
+       {SNDRV_SEQ_EVENT_SYSEX,          1, NULL, NULL}, /* sysex: 0xf0 */
+       {SNDRV_SEQ_EVENT_QFRAME,         1, one_param_event, one_param_decode}, /* 0xf1 */
+       {SNDRV_SEQ_EVENT_SONGPOS,        2, songpos_event, songpos_decode}, /* 0xf2 */
+       {SNDRV_SEQ_EVENT_SONGSEL,        1, one_param_event, one_param_decode}, /* 0xf3 */
+       {SNDRV_SEQ_EVENT_NONE,          -1, NULL, NULL}, /* 0xf4 */
+       {SNDRV_SEQ_EVENT_NONE,          -1, NULL, NULL}, /* 0xf5 */
+       {SNDRV_SEQ_EVENT_TUNE_REQUEST,   0, NULL, NULL}, /* 0xf6 */
+       {SNDRV_SEQ_EVENT_NONE,          -1, NULL, NULL}, /* 0xf7 */
+       {SNDRV_SEQ_EVENT_CLOCK,          0, NULL, NULL}, /* 0xf8 */
+       {SNDRV_SEQ_EVENT_NONE,          -1, NULL, NULL}, /* 0xf9 */
+       {SNDRV_SEQ_EVENT_START,          0, NULL, NULL}, /* 0xfa */
+       {SNDRV_SEQ_EVENT_CONTINUE,       0, NULL, NULL}, /* 0xfb */
+       {SNDRV_SEQ_EVENT_STOP,           0, NULL, NULL}, /* 0xfc */
+       {SNDRV_SEQ_EVENT_NONE,          -1, NULL, NULL}, /* 0xfd */
+       {SNDRV_SEQ_EVENT_SENSING,        0, NULL, NULL}, /* 0xfe */
+       {SNDRV_SEQ_EVENT_RESET,          0, NULL, NULL}, /* 0xff */
 };
 
-static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int len, snd_seq_event_t *ev);
-static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, snd_seq_event_t *ev);
+static int extra_decode_ctrl14(struct snd_midi_event *dev, unsigned char *buf, int len,
+                              struct snd_seq_event *ev);
+static int extra_decode_xrpn(struct snd_midi_event *dev, unsigned char *buf, int count,
+                            struct snd_seq_event *ev);
 
-static struct extra_event_list_t {
+static struct extra_event_list {
        int event;
-       int (*decode)(snd_midi_event_t *dev, unsigned char *buf, int len, snd_seq_event_t *ev);
+       int (*decode)(struct snd_midi_event *dev, unsigned char *buf, int len,
+                     struct snd_seq_event *ev);
 } extra_event[] = {
        {SNDRV_SEQ_EVENT_CONTROL14, extra_decode_ctrl14},
        {SNDRV_SEQ_EVENT_NONREGPARAM, extra_decode_xrpn},
@@ -113,12 +111,12 @@ static struct extra_event_list_t {
  *  new/delete record
  */
 
-int snd_midi_event_new(int bufsize, snd_midi_event_t **rdev)
+int snd_midi_event_new(int bufsize, struct snd_midi_event **rdev)
 {
-       snd_midi_event_t *dev;
+       struct snd_midi_event *dev;
 
        *rdev = NULL;
-       dev = kcalloc(1, sizeof(*dev), GFP_KERNEL);
+       dev = kzalloc(sizeof(*dev), GFP_KERNEL);
        if (dev == NULL)
                return -ENOMEM;
        if (bufsize > 0) {
@@ -130,12 +128,13 @@ int snd_midi_event_new(int bufsize, snd_midi_event_t **rdev)
        }
        dev->bufsize = bufsize;
        dev->lastcmd = 0xff;
+       dev->type = ST_INVALID;
        spin_lock_init(&dev->lock);
        *rdev = dev;
        return 0;
 }
 
-void snd_midi_event_free(snd_midi_event_t *dev)
+void snd_midi_event_free(struct snd_midi_event *dev)
 {
        if (dev != NULL) {
                kfree(dev->buf);
@@ -146,14 +145,14 @@ void snd_midi_event_free(snd_midi_event_t *dev)
 /*
  * initialize record
  */
-static inline void reset_encode(snd_midi_event_t *dev)
+static inline void reset_encode(struct snd_midi_event *dev)
 {
        dev->read = 0;
        dev->qlen = 0;
-       dev->type = 0;
+       dev->type = ST_INVALID;
 }
 
-void snd_midi_event_reset_encode(snd_midi_event_t *dev)
+void snd_midi_event_reset_encode(struct snd_midi_event *dev)
 {
        unsigned long flags;
 
@@ -162,7 +161,7 @@ void snd_midi_event_reset_encode(snd_midi_event_t *dev)
        spin_unlock_irqrestore(&dev->lock, flags);
 }
 
-void snd_midi_event_reset_decode(snd_midi_event_t *dev)
+void snd_midi_event_reset_decode(struct snd_midi_event *dev)
 {
        unsigned long flags;
 
@@ -172,14 +171,14 @@ void snd_midi_event_reset_decode(snd_midi_event_t *dev)
 }
 
 #if 0
-void snd_midi_event_init(snd_midi_event_t *dev)
+void snd_midi_event_init(struct snd_midi_event *dev)
 {
        snd_midi_event_reset_encode(dev);
        snd_midi_event_reset_decode(dev);
 }
 #endif  /*  0  */
 
-void snd_midi_event_no_status(snd_midi_event_t *dev, int on)
+void snd_midi_event_no_status(struct snd_midi_event *dev, int on)
 {
        dev->nostat = on ? 1 : 0;
 }
@@ -188,7 +187,7 @@ void snd_midi_event_no_status(snd_midi_event_t *dev, int on)
  * resize buffer
  */
 #if 0
-int snd_midi_event_resize_buffer(snd_midi_event_t *dev, int bufsize)
+int snd_midi_event_resize_buffer(struct snd_midi_event *dev, int bufsize)
 {
        unsigned char *new_buf, *old_buf;
        unsigned long flags;
@@ -213,7 +212,8 @@ int snd_midi_event_resize_buffer(snd_midi_event_t *dev, int bufsize)
  *  read bytes and encode to sequencer event if finished
  *  return the size of encoded bytes
  */
-long snd_midi_event_encode(snd_midi_event_t *dev, unsigned char *buf, long count, snd_seq_event_t *ev)
+long snd_midi_event_encode(struct snd_midi_event *dev, unsigned char *buf, long count,
+                          struct snd_seq_event *ev)
 {
        long result = 0;
        int rc;
@@ -238,7 +238,8 @@ long snd_midi_event_encode(snd_midi_event_t *dev, unsigned char *buf, long count
  *         0 data is not finished
  *         negative for error
  */
-int snd_midi_event_encode_byte(snd_midi_event_t *dev, int c, snd_seq_event_t *ev)
+int snd_midi_event_encode_byte(struct snd_midi_event *dev, int c,
+                              struct snd_seq_event *ev)
 {
        int rc = 0;
        unsigned long flags;
@@ -250,29 +251,31 @@ int snd_midi_event_encode_byte(snd_midi_event_t *dev, int c, snd_seq_event_t *ev
                ev->type = status_event[ST_SPECIAL + c - 0xf0].event;
                ev->flags &= ~SNDRV_SEQ_EVENT_LENGTH_MASK;
                ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
-               return 1;
+               return ev->type != SNDRV_SEQ_EVENT_NONE;
        }
 
        spin_lock_irqsave(&dev->lock, flags);
-       if (dev->qlen > 0) {
-               /* rest of command */
-               dev->buf[dev->read++] = c;
-               if (dev->type != ST_SYSEX)
-                       dev->qlen--;
-       } else {
+       if ((c & 0x80) &&
+           (c != MIDI_CMD_COMMON_SYSEX_END || dev->type != ST_SYSEX)) {
                /* new command */
+               dev->buf[0] = c;
+               if ((c & 0xf0) == 0xf0) /* system messages */
+                       dev->type = (c & 0x0f) + ST_SPECIAL;
+               else
+                       dev->type = (c >> 4) & 0x07;
                dev->read = 1;
-               if (c & 0x80) {
-                       dev->buf[0] = c;
-                       if ((c & 0xf0) == 0xf0) /* special events */
-                               dev->type = (c & 0x0f) + ST_SPECIAL;
-                       else
-                               dev->type = (c >> 4) & 0x07;
-                       dev->qlen = status_event[dev->type].qlen;
-               } else {
-                       /* process this byte as argument */
+               dev->qlen = status_event[dev->type].qlen;
+       } else {
+               if (dev->qlen > 0) {
+                       /* rest of command */
                        dev->buf[dev->read++] = c;
+                       if (dev->type != ST_SYSEX)
+                               dev->qlen--;
+               } else {
+                       /* running status */
+                       dev->buf[1] = c;
                        dev->qlen = status_event[dev->type].qlen - 1;
+                       dev->read = 2;
                }
        }
        if (dev->qlen == 0) {
@@ -281,6 +284,8 @@ int snd_midi_event_encode_byte(snd_midi_event_t *dev, int c, snd_seq_event_t *ev
                ev->flags |= SNDRV_SEQ_EVENT_LENGTH_FIXED;
                if (status_event[dev->type].encode) /* set data values */
                        status_event[dev->type].encode(dev, ev);
+               if (dev->type >= ST_SPECIAL)
+                       dev->type = ST_INVALID;
                rc = 1;
        } else  if (dev->type == ST_SYSEX) {
                if (c == MIDI_CMD_COMMON_SYSEX_END ||
@@ -303,7 +308,7 @@ int snd_midi_event_encode_byte(snd_midi_event_t *dev, int c, snd_seq_event_t *ev
 }
 
 /* encode note event */
-static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
+static void note_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
 {
        ev->data.note.channel = dev->buf[0] & 0x0f;
        ev->data.note.note = dev->buf[1];
@@ -311,21 +316,21 @@ static void note_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
 }
 
 /* encode one parameter controls */
-static void one_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
+static void one_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
 {
        ev->data.control.channel = dev->buf[0] & 0x0f;
        ev->data.control.value = dev->buf[1];
 }
 
 /* encode pitch wheel change */
-static void pitchbend_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
+static void pitchbend_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
 {
        ev->data.control.channel = dev->buf[0] & 0x0f;
        ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1] - 8192;
 }
 
 /* encode midi control change */
-static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
+static void two_param_ctrl_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
 {
        ev->data.control.channel = dev->buf[0] & 0x0f;
        ev->data.control.param = dev->buf[1];
@@ -333,13 +338,13 @@ static void two_param_ctrl_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
 }
 
 /* encode one parameter value*/
-static void one_param_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
+static void one_param_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
 {
        ev->data.control.value = dev->buf[1];
 }
 
 /* encode song position */
-static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
+static void songpos_event(struct snd_midi_event *dev, struct snd_seq_event *ev)
 {
        ev->data.control.value = (int)dev->buf[2] * 128 + (int)dev->buf[1];
 }
@@ -348,7 +353,8 @@ static void songpos_event(snd_midi_event_t *dev, snd_seq_event_t *ev)
  * decode from a sequencer event to midi bytes
  * return the size of decoded midi events
  */
-long snd_midi_event_decode(snd_midi_event_t *dev, unsigned char *buf, long count, snd_seq_event_t *ev)
+long snd_midi_event_decode(struct snd_midi_event *dev, unsigned char *buf, long count,
+                          struct snd_seq_event *ev)
 {
        unsigned int cmd, type;
 
@@ -404,20 +410,20 @@ long snd_midi_event_decode(snd_midi_event_t *dev, unsigned char *buf, long count
 
 
 /* decode note event */
-static void note_decode(snd_seq_event_t *ev, unsigned char *buf)
+static void note_decode(struct snd_seq_event *ev, unsigned char *buf)
 {
        buf[0] = ev->data.note.note & 0x7f;
        buf[1] = ev->data.note.velocity & 0x7f;
 }
 
 /* decode one parameter controls */
-static void one_param_decode(snd_seq_event_t *ev, unsigned char *buf)
+static void one_param_decode(struct snd_seq_event *ev, unsigned char *buf)
 {
        buf[0] = ev->data.control.value & 0x7f;
 }
 
 /* decode pitch wheel change */
-static void pitchbend_decode(snd_seq_event_t *ev, unsigned char *buf)
+static void pitchbend_decode(struct snd_seq_event *ev, unsigned char *buf)
 {
        int value = ev->data.control.value + 8192;
        buf[0] = value & 0x7f;
@@ -425,21 +431,22 @@ static void pitchbend_decode(snd_seq_event_t *ev, unsigned char *buf)
 }
 
 /* decode midi control change */
-static void two_param_decode(snd_seq_event_t *ev, unsigned char *buf)
+static void two_param_decode(struct snd_seq_event *ev, unsigned char *buf)
 {
        buf[0] = ev->data.control.param & 0x7f;
        buf[1] = ev->data.control.value & 0x7f;
 }
 
 /* decode song position */
-static void songpos_decode(snd_seq_event_t *ev, unsigned char *buf)
+static void songpos_decode(struct snd_seq_event *ev, unsigned char *buf)
 {
        buf[0] = ev->data.control.value & 0x7f;
        buf[1] = (ev->data.control.value >> 7) & 0x7f;
 }
 
 /* decode 14bit control */
-static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int count, snd_seq_event_t *ev)
+static int extra_decode_ctrl14(struct snd_midi_event *dev, unsigned char *buf,
+                              int count, struct snd_seq_event *ev)
 {
        unsigned char cmd;
        int idx = 0;
@@ -476,7 +483,8 @@ static int extra_decode_ctrl14(snd_midi_event_t *dev, unsigned char *buf, int co
 }
 
 /* decode reg/nonreg param */
-static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int count, snd_seq_event_t *ev)
+static int extra_decode_xrpn(struct snd_midi_event *dev, unsigned char *buf,
+                            int count, struct snd_seq_event *ev)
 {
        unsigned char cmd;
        char *cbytes;
@@ -496,10 +504,10 @@ static int extra_decode_xrpn(snd_midi_event_t *dev, unsigned char *buf, int coun
        if (dev->nostat && count < 12)
                return -ENOMEM;
        cmd = MIDI_CMD_CONTROL|(ev->data.control.channel & 0x0f);
-       bytes[0] = ev->data.control.param & 0x007f;
-       bytes[1] = (ev->data.control.param & 0x3f80) >> 7;
-       bytes[2] = ev->data.control.value & 0x007f;
-       bytes[3] = (ev->data.control.value & 0x3f80) >> 7;
+       bytes[0] = (ev->data.control.param & 0x3f80) >> 7;
+       bytes[1] = ev->data.control.param & 0x007f;
+       bytes[2] = (ev->data.control.value & 0x3f80) >> 7;
+       bytes[3] = ev->data.control.value & 0x007f;
        if (cmd != dev->lastcmd && !dev->nostat) {
                if (count < 9)
                        return -ENOMEM;