| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* OSS compatible sequencer driver |
| 4 |
* |
| 5 |
* seq_oss_writeq.c - write queue and sync |
| 6 |
* |
| 7 |
* Copyright (C) 1998,99 Takashi Iwai <[email protected]> |
| 8 |
*/ |
| 9 |
|
| 10 |
#include "seq_oss_writeq.h" |
| 11 |
#include "seq_oss_event.h" |
| 12 |
#include "seq_oss_timer.h" |
| 13 |
#include <sound/seq_oss_legacy.h> |
| 14 |
#include "../seq_lock.h" |
| 15 |
#include "../seq_clientmgr.h" |
| 16 |
#include <linux/wait.h> |
| 17 |
#include <linux/slab.h> |
| 18 |
#include <linux/sched/signal.h> |
| 19 |
|
| 20 |
|
| 21 |
/* |
| 22 |
* create a write queue record |
| 23 |
*/ |
| 24 |
struct seq_oss_writeq * |
| 25 |
snd_seq_oss_writeq_new(struct seq_oss_devinfo *dp, int maxlen) |
| 26 |
{ |
| 27 |
struct seq_oss_writeq *q; |
| 28 |
struct snd_seq_client_pool pool; |
| 29 |
|
| 30 |
q = kzalloc_obj(*q); |
| 31 |
if (!q) |
| 32 |
return NULL; |
| 33 |
q->dp = dp; |
| 34 |
q->maxlen = maxlen; |
| 35 |
spin_lock_init(&q->sync_lock); |
| 36 |
q->sync_event_put = 0; |
| 37 |
q->sync_time = 0; |
| 38 |
init_waitqueue_head(&q->sync_sleep); |
| 39 |
|
| 40 |
memset(&pool, 0, sizeof(pool)); |
| 41 |
pool.client = dp->cseq; |
| 42 |
pool.output_pool = maxlen; |
| 43 |
pool.output_room = maxlen / 2; |
| 44 |
|
| 45 |
snd_seq_oss_control(dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool); |
| 46 |
|
| 47 |
return q; |
| 48 |
} |
| 49 |
|
| 50 |
/* |
| 51 |
* delete the write queue |
| 52 |
*/ |
| 53 |
void |
| 54 |
snd_seq_oss_writeq_delete(struct seq_oss_writeq *q) |
| 55 |
{ |
| 56 |
if (q) { |
| 57 |
snd_seq_oss_writeq_clear(q); /* to be sure */ |
| 58 |
kfree(q); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
/* |
| 64 |
* reset the write queue |
| 65 |
*/ |
| 66 |
void |
| 67 |
snd_seq_oss_writeq_clear(struct seq_oss_writeq *q) |
| 68 |
{ |
| 69 |
struct snd_seq_remove_events reset; |
| 70 |
|
| 71 |
memset(&reset, 0, sizeof(reset)); |
| 72 |
reset.remove_mode = SNDRV_SEQ_REMOVE_OUTPUT; /* remove all */ |
| 73 |
snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_REMOVE_EVENTS, &reset); |
| 74 |
|
| 75 |
/* wake up sleepers if any */ |
| 76 |
snd_seq_oss_writeq_wakeup(q, 0); |
| 77 |
} |
| 78 |
|
| 79 |
/* |
| 80 |
* wait until the write buffer has enough room |
| 81 |
*/ |
| 82 |
int |
| 83 |
snd_seq_oss_writeq_sync(struct seq_oss_writeq *q) |
| 84 |
{ |
| 85 |
struct seq_oss_devinfo *dp = q->dp; |
| 86 |
abstime_t time; |
| 87 |
|
| 88 |
time = snd_seq_oss_timer_cur_tick(dp->timer); |
| 89 |
if (q->sync_time >= time) |
| 90 |
return 0; /* already finished */ |
| 91 |
|
| 92 |
if (! q->sync_event_put) { |
| 93 |
struct snd_seq_event ev; |
| 94 |
union evrec *rec; |
| 95 |
|
| 96 |
/* put echoback event */ |
| 97 |
memset(&ev, 0, sizeof(ev)); |
| 98 |
ev.flags = 0; |
| 99 |
ev.type = SNDRV_SEQ_EVENT_ECHO; |
| 100 |
ev.time.tick = time; |
| 101 |
/* echo back to itself */ |
| 102 |
snd_seq_oss_fill_addr(dp, &ev, dp->addr.client, dp->addr.port); |
| 103 |
rec = (union evrec *)&ev.data; |
| 104 |
rec->t.code = SEQ_SYNCTIMER; |
| 105 |
rec->t.time = time; |
| 106 |
q->sync_event_put = 1; |
| 107 |
snd_seq_kernel_client_enqueue(dp->cseq, &ev, NULL, true); |
| 108 |
} |
| 109 |
|
| 110 |
wait_event_interruptible_timeout(q->sync_sleep, ! q->sync_event_put, HZ); |
| 111 |
if (signal_pending(current)) |
| 112 |
/* interrupted - return 0 to finish sync */ |
| 113 |
q->sync_event_put = 0; |
| 114 |
if (! q->sync_event_put || q->sync_time >= time) |
| 115 |
return 0; |
| 116 |
return 1; |
| 117 |
} |
| 118 |
|
| 119 |
/* |
| 120 |
* wake up sync - echo event was catched |
| 121 |
*/ |
| 122 |
void |
| 123 |
snd_seq_oss_writeq_wakeup(struct seq_oss_writeq *q, abstime_t time) |
| 124 |
{ |
| 125 |
guard(spinlock_irqsave)(&q->sync_lock); |
| 126 |
q->sync_time = time; |
| 127 |
q->sync_event_put = 0; |
| 128 |
wake_up(&q->sync_sleep); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/* |
| 133 |
* return the unused pool size |
| 134 |
*/ |
| 135 |
int |
| 136 |
snd_seq_oss_writeq_get_free_size(struct seq_oss_writeq *q) |
| 137 |
{ |
| 138 |
struct snd_seq_client_pool pool; |
| 139 |
pool.client = q->dp->cseq; |
| 140 |
snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool); |
| 141 |
return pool.output_free; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
/* |
| 146 |
* set output threshold size from ioctl |
| 147 |
*/ |
| 148 |
void |
| 149 |
snd_seq_oss_writeq_set_output(struct seq_oss_writeq *q, int val) |
| 150 |
{ |
| 151 |
struct snd_seq_client_pool pool; |
| 152 |
pool.client = q->dp->cseq; |
| 153 |
snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_GET_CLIENT_POOL, &pool); |
| 154 |
pool.output_room = val; |
| 155 |
snd_seq_oss_control(q->dp, SNDRV_SEQ_IOCTL_SET_CLIENT_POOL, &pool); |
| 156 |
} |
| 157 |
|