#include <linux/time.h>
#include <linux/wait.h>
#include <linux/moduleparam.h>
+#include <linux/delay.h>
+#include <linux/wait.h>
#include <sound/rawmidi.h>
#include <sound/info.h>
#include <sound/control.h>
(!substream->append || runtime->avail >= count);
}
-static int snd_rawmidi_init(snd_rawmidi_substream_t *substream)
+static void snd_rawmidi_input_event_tasklet(unsigned long data)
{
- snd_rawmidi_runtime_t *runtime = substream->runtime;
+ snd_rawmidi_substream_t *substream = (snd_rawmidi_substream_t *)data;
+ substream->runtime->event(substream);
+}
+
+static void snd_rawmidi_output_trigger_tasklet(unsigned long data)
+{
+ snd_rawmidi_substream_t *substream = (snd_rawmidi_substream_t *)data;
+ substream->ops->trigger(substream, 1);
+}
+
+static int snd_rawmidi_runtime_create(snd_rawmidi_substream_t * substream)
+{
+ snd_rawmidi_runtime_t *runtime;
+
+ if ((runtime = kcalloc(1, sizeof(*runtime), GFP_KERNEL)) == NULL)
+ return -ENOMEM;
spin_lock_init(&runtime->lock);
init_waitqueue_head(&runtime->sleep);
+ if (substream->stream == SNDRV_RAWMIDI_STREAM_INPUT)
+ tasklet_init(&runtime->tasklet,
+ snd_rawmidi_input_event_tasklet,
+ (unsigned long)substream);
+ else
+ tasklet_init(&runtime->tasklet,
+ snd_rawmidi_output_trigger_tasklet,
+ (unsigned long)substream);
runtime->event = NULL;
runtime->buffer_size = PAGE_SIZE;
runtime->avail_min = 1;
runtime->avail = 0;
else
runtime->avail = runtime->buffer_size;
- if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL)
+ if ((runtime->buffer = kmalloc(runtime->buffer_size, GFP_KERNEL)) == NULL) {
+ kfree(runtime);
return -ENOMEM;
+ }
runtime->appl_ptr = runtime->hw_ptr = 0;
+ substream->runtime = runtime;
return 0;
}
-static int snd_rawmidi_done_buffer(snd_rawmidi_runtime_t *runtime)
+static int snd_rawmidi_runtime_free(snd_rawmidi_substream_t * substream)
{
- if (runtime->buffer) {
- kfree(runtime->buffer);
- runtime->buffer = NULL;
- }
+ snd_rawmidi_runtime_t *runtime = substream->runtime;
+
+ kfree(runtime->buffer);
+ kfree(runtime);
+ substream->runtime = NULL;
return 0;
}
+static inline void snd_rawmidi_output_trigger(snd_rawmidi_substream_t * substream, int up)
+{
+ if (up) {
+ tasklet_hi_schedule(&substream->runtime->tasklet);
+ } else {
+ tasklet_kill(&substream->runtime->tasklet);
+ substream->ops->trigger(substream, 0);
+ }
+}
+
+static void snd_rawmidi_input_trigger(snd_rawmidi_substream_t * substream, int up)
+{
+ substream->ops->trigger(substream, up);
+ if (!up && substream->runtime->event)
+ tasklet_kill(&substream->runtime->tasklet);
+}
+
int snd_rawmidi_drop_output(snd_rawmidi_substream_t * substream)
{
+ unsigned long flags;
snd_rawmidi_runtime_t *runtime = substream->runtime;
- substream->ops->trigger(substream, 0);
- runtime->trigger = 0;
+ snd_rawmidi_output_trigger(substream, 0);
runtime->drain = 0;
- /* interrupts are not enabled at this moment,
- so spinlock is not required */
+ spin_lock_irqsave(&runtime->lock, flags);
runtime->appl_ptr = runtime->hw_ptr = 0;
runtime->avail = runtime->buffer_size;
+ spin_unlock_irqrestore(&runtime->lock, flags);
return 0;
}
err = 0;
runtime->drain = 1;
- while (runtime->avail < runtime->buffer_size) {
- timeout = interruptible_sleep_on_timeout(&runtime->sleep, 10 * HZ);
- if (signal_pending(current)) {
- err = -ERESTARTSYS;
- break;
- }
- if (runtime->avail < runtime->buffer_size && !timeout) {
- snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
- err = -EIO;
- break;
- }
+ timeout = wait_event_interruptible_timeout(runtime->sleep,
+ (runtime->avail >= runtime->buffer_size),
+ 10*HZ);
+ if (signal_pending(current))
+ err = -ERESTARTSYS;
+ if (runtime->avail < runtime->buffer_size && !timeout) {
+ snd_printk(KERN_WARNING "rawmidi drain error (avail = %li, buffer_size = %li)\n", (long)runtime->avail, (long)runtime->buffer_size);
+ err = -EIO;
}
runtime->drain = 0;
if (err != -ERESTARTSYS) {
/* we need wait a while to make sure that Tx FIFOs are empty */
if (substream->ops->drain)
substream->ops->drain(substream);
- else {
- set_current_state(TASK_UNINTERRUPTIBLE);
- schedule_timeout(HZ / 20);
- }
+ else
+ msleep(50);
snd_rawmidi_drop_output(substream);
}
return err;
int snd_rawmidi_drain_input(snd_rawmidi_substream_t * substream)
{
+ unsigned long flags;
snd_rawmidi_runtime_t *runtime = substream->runtime;
- substream->ops->trigger(substream, 0);
- runtime->trigger = 0;
+ snd_rawmidi_input_trigger(substream, 0);
runtime->drain = 0;
- /* interrupts aren't enabled at this moment, so spinlock isn't needed */
+ spin_lock_irqsave(&runtime->lock, flags);
runtime->appl_ptr = runtime->hw_ptr = 0;
runtime->avail = 0;
+ spin_unlock_irqrestore(&runtime->lock, flags);
return 0;
}
{
snd_rawmidi_t *rmidi;
struct list_head *list1, *list2;
- snd_rawmidi_substream_t *sinput, *soutput;
+ snd_rawmidi_substream_t *sinput = NULL, *soutput = NULL;
snd_rawmidi_runtime_t *input = NULL, *output = NULL;
int err;
list2 = list2->next;
}
if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
- input = kcalloc(1, sizeof(*input), GFP_KERNEL);
- if (input == NULL) {
- err = -ENOMEM;
- goto __error;
- }
- sinput->runtime = input;
- if (snd_rawmidi_init(sinput) < 0) {
- err = -ENOMEM;
+ if ((err = snd_rawmidi_runtime_create(sinput)) < 0)
goto __error;
- }
- if ((err = sinput->ops->open(sinput)) < 0) {
- sinput->runtime = NULL;
+ input = sinput->runtime;
+ if ((err = sinput->ops->open(sinput)) < 0)
goto __error;
- }
sinput->opened = 1;
rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened++;
} else {
if (mode & SNDRV_RAWMIDI_LFLG_OUTPUT) {
if (soutput->opened)
goto __skip_output;
- output = kcalloc(1, sizeof(*output), GFP_KERNEL);
- if (output == NULL) {
- err = -ENOMEM;
- goto __error;
- }
- soutput->runtime = output;
- if (snd_rawmidi_init(soutput) < 0) {
- if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
+ if ((err = snd_rawmidi_runtime_create(soutput)) < 0) {
+ if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
sinput->ops->close(sinput);
- sinput->runtime = NULL;
- }
- err = -ENOMEM;
goto __error;
}
+ output = soutput->runtime;
if ((err = soutput->ops->open(soutput)) < 0) {
- if (mode & SNDRV_RAWMIDI_LFLG_INPUT) {
+ if (mode & SNDRV_RAWMIDI_LFLG_INPUT)
sinput->ops->close(sinput);
- sinput->runtime = NULL;
- }
- soutput->runtime = NULL;
goto __error;
}
__skip_output:
return 0;
__error:
- if (input != NULL) {
- snd_rawmidi_done_buffer(input);
- kfree(input);
- }
- if (output != NULL) {
- snd_rawmidi_done_buffer(output);
- kfree(output);
- }
+ if (input != NULL)
+ snd_rawmidi_runtime_free(sinput);
+ if (output != NULL)
+ snd_rawmidi_runtime_free(soutput);
module_put(rmidi->card->module);
if (!(mode & SNDRV_RAWMIDI_LFLG_NOOPENLOCK))
up(&rmidi->open_mutex);
substream = rfile->input;
rfile->input = NULL;
runtime = substream->runtime;
- runtime->trigger = 0;
- substream->ops->trigger(substream, 0);
+ snd_rawmidi_input_trigger(substream, 0);
substream->ops->close(substream);
- snd_rawmidi_done_buffer(runtime);
if (runtime->private_free != NULL)
runtime->private_free(substream);
- kfree(runtime);
- substream->runtime = NULL;
+ snd_rawmidi_runtime_free(substream);
substream->opened = 0;
rmidi->streams[SNDRV_RAWMIDI_STREAM_INPUT].substream_opened--;
}
snd_rawmidi_kernel_write(substream, &buf, 1);
}
if (snd_rawmidi_drain_output(substream) == -ERESTARTSYS)
- substream->ops->trigger(substream, 0);
+ snd_rawmidi_output_trigger(substream, 0);
substream->ops->close(substream);
- snd_rawmidi_done_buffer(runtime);
if (runtime->private_free != NULL)
runtime->private_free(substream);
- kfree(runtime);
- substream->runtime = NULL;
+ snd_rawmidi_runtime_free(substream);
substream->opened = 0;
substream->append = 0;
}
return 0;
}
-static inline int _snd_rawmidi_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
+static long snd_rawmidi_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
snd_rawmidi_file_t *rfile;
void __user *argp = (void __user *)arg;
return -ENOTTY;
}
-/* FIXME: need to unlock BKL to allow preemption */
-static int snd_rawmidi_ioctl(struct inode *inode, struct file *file,
- unsigned int cmd, unsigned long arg)
-{
- int err;
- unlock_kernel();
- err = _snd_rawmidi_ioctl(inode, file, cmd, arg);
- lock_kernel();
- return err;
-}
-
static int snd_rawmidi_control_ioctl(snd_card_t * card,
snd_ctl_file_t * control,
unsigned int cmd,
count1 = count;
if (count1 > (int)(runtime->buffer_size - runtime->avail)) {
count1 = runtime->buffer_size - runtime->avail;
- runtime->xruns = count - count1;
+ runtime->xruns += count - count1;
}
if (count1 > 0) {
memcpy(runtime->buffer, buffer, count1);
}
}
}
- if (result > 0 && runtime->event == NULL) {
- if (snd_rawmidi_ready(substream))
+ if (result > 0) {
+ if (runtime->event)
+ tasklet_hi_schedule(&runtime->tasklet);
+ else if (snd_rawmidi_ready(substream))
wake_up(&runtime->sleep);
}
spin_unlock_irqrestore(&runtime->lock, flags);
- if (result > 0 && runtime->event)
- runtime->event(substream);
return result;
}
memcpy(buf + result, runtime->buffer + runtime->appl_ptr, count1);
} else {
spin_unlock_irqrestore(&runtime->lock, flags);
- if (copy_to_user(buf + result, runtime->buffer + runtime->appl_ptr, count1)) {
+ if (copy_to_user((char __user *)buf + result,
+ runtime->buffer + runtime->appl_ptr, count1)) {
return result > 0 ? result : -EFAULT;
}
spin_lock_irqsave(&runtime->lock, flags);
long snd_rawmidi_kernel_read(snd_rawmidi_substream_t *substream, unsigned char *buf, long count)
{
- substream->runtime->trigger = 1;
- substream->ops->trigger(substream, 1);
+ snd_rawmidi_input_trigger(substream, 1);
return snd_rawmidi_kernel_read1(substream, buf, count, 1);
}
if (substream == NULL)
return -EIO;
runtime = substream->runtime;
- runtime->trigger = 1;
- substream->ops->trigger(substream, 1);
+ snd_rawmidi_input_trigger(substream, 1);
result = 0;
while (count > 0) {
spin_lock_irq(&runtime->lock);
spin_lock_irq(&runtime->lock);
}
spin_unlock_irq(&runtime->lock);
- count1 = snd_rawmidi_kernel_read1(substream, buf, count, 0);
+ count1 = snd_rawmidi_kernel_read1(substream, (unsigned char *)buf, count, 0);
if (count1 < 0)
return result > 0 ? result : count1;
result += count1;
}
spin_lock_irqsave(&runtime->lock, flags);
result = runtime->avail >= runtime->buffer_size;
- if (result)
- runtime->trigger = 1;
spin_unlock_irqrestore(&runtime->lock, flags);
return result;
}
spin_lock_irqsave(&runtime->lock, flags);
if (runtime->avail >= runtime->buffer_size) {
/* warning: lowlevel layer MUST trigger down the hardware */
- runtime->trigger = 0;
goto __skip;
}
if (count == 1) { /* special case, faster code */
memcpy(buffer, runtime->buffer + runtime->hw_ptr, count1);
count -= count1;
result += count1;
- if (count > 0)
+ if (count > 0) {
+ if (count > (int)(runtime->buffer_size - runtime->avail - count1))
+ count = runtime->buffer_size - runtime->avail - count1;
memcpy(buffer + count1, runtime->buffer, count);
+ result += count;
+ }
}
__skip:
spin_unlock_irqrestore(&runtime->lock, flags);
runtime->hw_ptr %= runtime->buffer_size;
runtime->avail += count;
substream->bytes += count;
- if (runtime->drain)
- wake_up(&runtime->sleep);
- else
- if (count > 0 && runtime->event == NULL)
- if (snd_rawmidi_ready(substream))
- wake_up(&runtime->sleep);
+ if (count > 0) {
+ if (runtime->drain || snd_rawmidi_ready(substream))
+ wake_up(&runtime->sleep);
+ }
spin_unlock_irqrestore(&runtime->lock, flags);
- if (count > 0 && runtime->event)
- runtime->event(substream);
return count;
}
memcpy(runtime->buffer + runtime->appl_ptr, buf, count1);
} else {
spin_unlock_irqrestore(&runtime->lock, flags);
- if (copy_from_user(runtime->buffer + runtime->appl_ptr, buf, count1)) {
+ if (copy_from_user(runtime->buffer + runtime->appl_ptr,
+ (char __user *)buf, count1)) {
spin_lock_irqsave(&runtime->lock, flags);
result = result > 0 ? result : -EFAULT;
goto __end;
count -= count1;
}
__end:
- if (result > 0)
- runtime->trigger = 1;
count1 = runtime->avail < runtime->buffer_size;
spin_unlock_irqrestore(&runtime->lock, flags);
if (count1)
- substream->ops->trigger(substream, 1);
+ snd_rawmidi_output_trigger(substream, 1);
return result;
}
spin_lock_irq(&runtime->lock);
}
spin_unlock_irq(&runtime->lock);
- count1 = snd_rawmidi_kernel_write1(substream, buf, count, 0);
+ count1 = snd_rawmidi_kernel_write1(substream, (unsigned char *)buf, count, 0);
if (count1 < 0)
return result > 0 ? result : count1;
result += count1;
rfile = file->private_data;
if (rfile->input != NULL) {
runtime = rfile->input->runtime;
- runtime->trigger = 1;
- rfile->input->ops->trigger(rfile->input, 1);
+ snd_rawmidi_input_trigger(rfile->input, 1);
poll_wait(file, &runtime->sleep, wait);
}
if (rfile->output != NULL) {
return mask;
}
+/*
+ */
+#ifdef CONFIG_COMPAT
+#include "rawmidi_compat.c"
+#else
+#define snd_rawmidi_ioctl_compat NULL
+#endif
+
/*
*/
.open = snd_rawmidi_open,
.release = snd_rawmidi_release,
.poll = snd_rawmidi_poll,
- .ioctl = snd_rawmidi_ioctl,
+ .unlocked_ioctl = snd_rawmidi_ioctl,
+ .compat_ioctl = snd_rawmidi_ioctl_compat,
};
static snd_minor_t snd_rawmidi_reg =
{
snd_ctl_register_ioctl(snd_rawmidi_control_ioctl);
+ snd_ctl_register_ioctl_compat(snd_rawmidi_control_ioctl);
#ifdef CONFIG_SND_OSSEMUL
{ int i;
/* check device map table */
static void __exit alsa_rawmidi_exit(void)
{
snd_ctl_unregister_ioctl(snd_rawmidi_control_ioctl);
+ snd_ctl_unregister_ioctl_compat(snd_rawmidi_control_ioctl);
}
module_init(alsa_rawmidi_init)