RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / sound / core / seq / oss / seq_oss_rw.c
blob6fc0d0806ba9594cad2a9bb2fad536dca0b73d34
1 /*
2 * OSS compatible sequencer driver
4 * read/write/select interface to device file
6 * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "seq_oss_device.h"
24 #include "seq_oss_readq.h"
25 #include "seq_oss_writeq.h"
26 #include "seq_oss_synth.h"
27 #include <sound/seq_oss_legacy.h>
28 #include "seq_oss_event.h"
29 #include "seq_oss_timer.h"
30 #include "../seq_clientmgr.h"
34 * protoypes
36 static int insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt);
40 * read interface
43 int
44 snd_seq_oss_read(struct seq_oss_devinfo *dp, char __user *buf, int count)
46 struct seq_oss_readq *readq = dp->readq;
47 int result = 0, err = 0;
48 int ev_len;
49 union evrec rec;
50 unsigned long flags;
52 if (readq == NULL || ! is_read_mode(dp->file_mode))
53 return -ENXIO;
55 while (count >= SHORT_EVENT_SIZE) {
56 snd_seq_oss_readq_lock(readq, flags);
57 err = snd_seq_oss_readq_pick(readq, &rec);
58 if (err == -EAGAIN &&
59 !is_nonblock_mode(dp->file_mode) && result == 0) {
60 snd_seq_oss_readq_unlock(readq, flags);
61 snd_seq_oss_readq_wait(readq);
62 snd_seq_oss_readq_lock(readq, flags);
63 if (signal_pending(current))
64 err = -ERESTARTSYS;
65 else
66 err = snd_seq_oss_readq_pick(readq, &rec);
68 if (err < 0) {
69 snd_seq_oss_readq_unlock(readq, flags);
70 break;
72 ev_len = ev_length(&rec);
73 if (ev_len < count) {
74 snd_seq_oss_readq_unlock(readq, flags);
75 break;
77 snd_seq_oss_readq_free(readq);
78 snd_seq_oss_readq_unlock(readq, flags);
79 if (copy_to_user(buf, &rec, ev_len)) {
80 err = -EFAULT;
81 break;
83 result += ev_len;
84 buf += ev_len;
85 count -= ev_len;
87 return result > 0 ? result : err;
92 * write interface
95 int
96 snd_seq_oss_write(struct seq_oss_devinfo *dp, const char __user *buf, int count, struct file *opt)
98 int result = 0, err = 0;
99 int ev_size, fmt;
100 union evrec rec;
102 if (! is_write_mode(dp->file_mode) || dp->writeq == NULL)
103 return -ENXIO;
105 while (count >= SHORT_EVENT_SIZE) {
106 if (copy_from_user(&rec, buf, SHORT_EVENT_SIZE)) {
107 err = -EFAULT;
108 break;
110 if (rec.s.code == SEQ_FULLSIZE) {
111 /* load patch */
112 if (result > 0) {
113 err = -EINVAL;
114 break;
116 fmt = (*(unsigned short *)rec.c) & 0xffff;
117 return snd_seq_oss_synth_load_patch(dp, rec.s.dev,
118 fmt, buf, 0, count);
120 if (ev_is_long(&rec)) {
121 /* extended code */
122 if (rec.s.code == SEQ_EXTENDED &&
123 dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
124 err = -EINVAL;
125 break;
127 ev_size = LONG_EVENT_SIZE;
128 if (count < ev_size)
129 break;
130 /* copy the reset 4 bytes */
131 if (copy_from_user(rec.c + SHORT_EVENT_SIZE,
132 buf + SHORT_EVENT_SIZE,
133 LONG_EVENT_SIZE - SHORT_EVENT_SIZE)) {
134 err = -EFAULT;
135 break;
137 } else {
138 /* old-type code */
139 if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
140 err = -EINVAL;
141 break;
143 ev_size = SHORT_EVENT_SIZE;
146 /* insert queue */
147 if ((err = insert_queue(dp, &rec, opt)) < 0)
148 break;
150 result += ev_size;
151 buf += ev_size;
152 count -= ev_size;
154 return result > 0 ? result : err;
159 * insert event record to write queue
160 * return: 0 = OK, non-zero = NG
162 static int
163 insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt)
165 int rc = 0;
166 struct snd_seq_event event;
168 /* if this is a timing event, process the current time */
169 if (snd_seq_oss_process_timer_event(dp->timer, rec))
170 return 0; /* no need to insert queue */
172 /* parse this event */
173 memset(&event, 0, sizeof(event));
174 /* set dummy -- to be sure */
175 event.type = SNDRV_SEQ_EVENT_NOTEOFF;
176 snd_seq_oss_fill_addr(dp, &event, dp->addr.port, dp->addr.client);
178 if (snd_seq_oss_process_event(dp, rec, &event))
179 return 0; /* invalid event - no need to insert queue */
181 event.time.tick = snd_seq_oss_timer_cur_tick(dp->timer);
182 if (dp->timer->realtime || !dp->timer->running) {
183 snd_seq_oss_dispatch(dp, &event, 0, 0);
184 } else {
185 if (is_nonblock_mode(dp->file_mode))
186 rc = snd_seq_kernel_client_enqueue(dp->cseq, &event, 0, 0);
187 else
188 rc = snd_seq_kernel_client_enqueue_blocking(dp->cseq, &event, opt, 0, 0);
190 return rc;
195 * select / poll
198 unsigned int
199 snd_seq_oss_poll(struct seq_oss_devinfo *dp, struct file *file, poll_table * wait)
201 unsigned int mask = 0;
203 /* input */
204 if (dp->readq && is_read_mode(dp->file_mode)) {
205 if (snd_seq_oss_readq_poll(dp->readq, file, wait))
206 mask |= POLLIN | POLLRDNORM;
209 /* output */
210 if (dp->writeq && is_write_mode(dp->file_mode)) {
211 if (snd_seq_kernel_client_write_poll(dp->cseq, file, wait))
212 mask |= POLLOUT | POLLWRNORM;
214 return mask;