WIP FPC-III support
[linux/fpc-iii.git] / sound / core / seq / oss / seq_oss_rw.c
blob537d5f423e206b075743129cee071cdeee938645
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * OSS compatible sequencer driver
5 * read/write/select interface to device file
7 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
8 */
10 #include "seq_oss_device.h"
11 #include "seq_oss_readq.h"
12 #include "seq_oss_writeq.h"
13 #include "seq_oss_synth.h"
14 #include <sound/seq_oss_legacy.h>
15 #include "seq_oss_event.h"
16 #include "seq_oss_timer.h"
17 #include "../seq_clientmgr.h"
21 * protoypes
23 static int insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt);
27 * read interface
30 int
31 snd_seq_oss_read(struct seq_oss_devinfo *dp, char __user *buf, int count)
33 struct seq_oss_readq *readq = dp->readq;
34 int result = 0, err = 0;
35 int ev_len;
36 union evrec rec;
37 unsigned long flags;
39 if (readq == NULL || ! is_read_mode(dp->file_mode))
40 return -ENXIO;
42 while (count >= SHORT_EVENT_SIZE) {
43 snd_seq_oss_readq_lock(readq, flags);
44 err = snd_seq_oss_readq_pick(readq, &rec);
45 if (err == -EAGAIN &&
46 !is_nonblock_mode(dp->file_mode) && result == 0) {
47 snd_seq_oss_readq_unlock(readq, flags);
48 snd_seq_oss_readq_wait(readq);
49 snd_seq_oss_readq_lock(readq, flags);
50 if (signal_pending(current))
51 err = -ERESTARTSYS;
52 else
53 err = snd_seq_oss_readq_pick(readq, &rec);
55 if (err < 0) {
56 snd_seq_oss_readq_unlock(readq, flags);
57 break;
59 ev_len = ev_length(&rec);
60 if (ev_len < count) {
61 snd_seq_oss_readq_unlock(readq, flags);
62 break;
64 snd_seq_oss_readq_free(readq);
65 snd_seq_oss_readq_unlock(readq, flags);
66 if (copy_to_user(buf, &rec, ev_len)) {
67 err = -EFAULT;
68 break;
70 result += ev_len;
71 buf += ev_len;
72 count -= ev_len;
74 return result > 0 ? result : err;
79 * write interface
82 int
83 snd_seq_oss_write(struct seq_oss_devinfo *dp, const char __user *buf, int count, struct file *opt)
85 int result = 0, err = 0;
86 int ev_size, fmt;
87 union evrec rec;
89 if (! is_write_mode(dp->file_mode) || dp->writeq == NULL)
90 return -ENXIO;
92 while (count >= SHORT_EVENT_SIZE) {
93 if (copy_from_user(&rec, buf, SHORT_EVENT_SIZE)) {
94 err = -EFAULT;
95 break;
97 if (rec.s.code == SEQ_FULLSIZE) {
98 /* load patch */
99 if (result > 0) {
100 err = -EINVAL;
101 break;
103 fmt = (*(unsigned short *)rec.c) & 0xffff;
104 /* FIXME the return value isn't correct */
105 return snd_seq_oss_synth_load_patch(dp, rec.s.dev,
106 fmt, buf, 0, count);
108 if (ev_is_long(&rec)) {
109 /* extended code */
110 if (rec.s.code == SEQ_EXTENDED &&
111 dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
112 err = -EINVAL;
113 break;
115 ev_size = LONG_EVENT_SIZE;
116 if (count < ev_size)
117 break;
118 /* copy the reset 4 bytes */
119 if (copy_from_user(rec.c + SHORT_EVENT_SIZE,
120 buf + SHORT_EVENT_SIZE,
121 LONG_EVENT_SIZE - SHORT_EVENT_SIZE)) {
122 err = -EFAULT;
123 break;
125 } else {
126 /* old-type code */
127 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
128 err = -EINVAL;
129 break;
131 ev_size = SHORT_EVENT_SIZE;
134 /* insert queue */
135 if ((err = insert_queue(dp, &rec, opt)) < 0)
136 break;
138 result += ev_size;
139 buf += ev_size;
140 count -= ev_size;
142 return result > 0 ? result : err;
147 * insert event record to write queue
148 * return: 0 = OK, non-zero = NG
150 static int
151 insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt)
153 int rc = 0;
154 struct snd_seq_event event;
156 /* if this is a timing event, process the current time */
157 if (snd_seq_oss_process_timer_event(dp->timer, rec))
158 return 0; /* no need to insert queue */
160 /* parse this event */
161 memset(&event, 0, sizeof(event));
162 /* set dummy -- to be sure */
163 event.type = SNDRV_SEQ_EVENT_NOTEOFF;
164 snd_seq_oss_fill_addr(dp, &event, dp->addr.client, dp->addr.port);
166 if (snd_seq_oss_process_event(dp, rec, &event))
167 return 0; /* invalid event - no need to insert queue */
169 event.time.tick = snd_seq_oss_timer_cur_tick(dp->timer);
170 if (dp->timer->realtime || !dp->timer->running)
171 snd_seq_oss_dispatch(dp, &event, 0, 0);
172 else
173 rc = snd_seq_kernel_client_enqueue(dp->cseq, &event, opt,
174 !is_nonblock_mode(dp->file_mode));
175 return rc;
180 * select / poll
183 __poll_t
184 snd_seq_oss_poll(struct seq_oss_devinfo *dp, struct file *file, poll_table * wait)
186 __poll_t mask = 0;
188 /* input */
189 if (dp->readq && is_read_mode(dp->file_mode)) {
190 if (snd_seq_oss_readq_poll(dp->readq, file, wait))
191 mask |= EPOLLIN | EPOLLRDNORM;
194 /* output */
195 if (dp->writeq && is_write_mode(dp->file_mode)) {
196 if (snd_seq_kernel_client_write_poll(dp->cseq, file, wait))
197 mask |= EPOLLOUT | EPOLLWRNORM;
199 return mask;