Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * OSS compatible sequencer driver
0004  *
0005  * seq_oss_writeq.c - write queue and sync
0006  *
0007  * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
0008  */
0009 
0010 #include "seq_oss_writeq.h"
0011 #include "seq_oss_event.h"
0012 #include "seq_oss_timer.h"
0013 #include <sound/seq_oss_legacy.h>
0014 #include "../seq_lock.h"
0015 #include "../seq_clientmgr.h"
0016 #include <linux/wait.h>
0017 #include <linux/slab.h>
0018 #include <linux/sched/signal.h>
0019 
0020 
0021 /*
0022  * create a write queue record
0023  */
0024 struct seq_oss_writeq *
0025 snd_seq_oss_writeq_new(struct seq_oss_devinfo *dp, int maxlen)
0026 {
0027     struct seq_oss_writeq *q;
0028     struct snd_seq_client_pool pool;
0029 
0030     q = kzalloc(sizeof(*q), GFP_KERNEL);
0031     if (!q)
0032         return NULL;
0033     q->dp = dp;
0034     q->maxlen = maxlen;
0035     spin_lock_init(&q->sync_lock);
0036     q->sync_event_put = 0;
0037     q->sync_time = 0;
0038     init_waitqueue_head(&q->sync_sleep);
0039 
0040     memset(&pool, 0, sizeof(pool));
0041     pool.client = dp->cseq;
0042     pool.output_pool = maxlen;
0043     pool.output_room = maxlen / 2;
0044 
0045     snd_seq_oss_control(dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool);
0046 
0047     return q;
0048 }
0049 
0050 /*
0051  * delete the write queue
0052  */
0053 void
0054 snd_seq_oss_writeq_delete(struct seq_oss_writeq *q)
0055 {
0056     if (q) {
0057         snd_seq_oss_writeq_clear(q);    /* to be sure */
0058         kfree(q);
0059     }
0060 }
0061 
0062 
0063 /*
0064  * reset the write queue
0065  */
0066 void
0067 snd_seq_oss_writeq_clear(struct seq_oss_writeq *q)
0068 {
0069     struct snd_seq_remove_events reset;
0070 
0071     memset(&reset, 0, sizeof(reset));
0072     reset.remove_mode = SNDRV_SEQ_REMOVE_OUTPUT; /* remove all */
0073     snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_REMOVE_EVENTS, &reset);
0074 
0075     /* wake up sleepers if any */
0076     snd_seq_oss_writeq_wakeup(q, 0);
0077 }
0078 
0079 /*
0080  * wait until the write buffer has enough room
0081  */
0082 int
0083 snd_seq_oss_writeq_sync(struct seq_oss_writeq *q)
0084 {
0085     struct seq_oss_devinfo *dp = q->dp;
0086     abstime_t time;
0087 
0088     time = snd_seq_oss_timer_cur_tick(dp->timer);
0089     if (q->sync_time >= time)
0090         return 0; /* already finished */
0091 
0092     if (! q->sync_event_put) {
0093         struct snd_seq_event ev;
0094         union evrec *rec;
0095 
0096         /* put echoback event */
0097         memset(&ev, 0, sizeof(ev));
0098         ev.flags = 0;
0099         ev.type = SNDRV_SEQ_EVENT_ECHO;
0100         ev.time.tick = time;
0101         /* echo back to itself */
0102         snd_seq_oss_fill_addr(dp, &ev, dp->addr.client, dp->addr.port);
0103         rec = (union evrec *)&ev.data;
0104         rec->t.code = SEQ_SYNCTIMER;
0105         rec->t.time = time;
0106         q->sync_event_put = 1;
0107         snd_seq_kernel_client_enqueue(dp->cseq, &ev, NULL, true);
0108     }
0109 
0110     wait_event_interruptible_timeout(q->sync_sleep, ! q->sync_event_put, HZ);
0111     if (signal_pending(current))
0112         /* interrupted - return 0 to finish sync */
0113         q->sync_event_put = 0;
0114     if (! q->sync_event_put || q->sync_time >= time)
0115         return 0;
0116     return 1;
0117 }
0118 
0119 /*
0120  * wake up sync - echo event was catched
0121  */
0122 void
0123 snd_seq_oss_writeq_wakeup(struct seq_oss_writeq *q, abstime_t time)
0124 {
0125     unsigned long flags;
0126 
0127     spin_lock_irqsave(&q->sync_lock, flags);
0128     q->sync_time = time;
0129     q->sync_event_put = 0;
0130     wake_up(&q->sync_sleep);
0131     spin_unlock_irqrestore(&q->sync_lock, flags);
0132 }
0133 
0134 
0135 /*
0136  * return the unused pool size
0137  */
0138 int
0139 snd_seq_oss_writeq_get_free_size(struct seq_oss_writeq *q)
0140 {
0141     struct snd_seq_client_pool pool;
0142     pool.client = q->dp->cseq;
0143     snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool);
0144     return pool.output_free;
0145 }
0146 
0147 
0148 /*
0149  * set output threshold size from ioctl
0150  */
0151 void
0152 snd_seq_oss_writeq_set_output(struct seq_oss_writeq *q, int val)
0153 {
0154     struct snd_seq_client_pool pool;
0155     pool.client = q->dp->cseq;
0156     snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool);
0157     pool.output_room = val;
0158     snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool);
0159 }
0160