ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / sound / core / seq / oss / seq_oss_rw.c
1 /*
2  * OSS compatible sequencer driver
3  *
4  * read/write/select interface to device file
5  *
6  * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
7  *
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.
12  *
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.
17  *
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
21  */
22
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"
31
32
33 /*
34  * protoypes
35  */
36 static int insert_queue(seq_oss_devinfo_t *dp, evrec_t *rec, struct file *opt);
37
38
39 /*
40  * read interface
41  */
42
43 int
44 snd_seq_oss_read(seq_oss_devinfo_t *dp, char __user *buf, int count)
45 {
46         seq_oss_readq_t *readq = dp->readq;
47         int cnt, pos;
48         evrec_t *q;
49         unsigned long flags;
50
51         if (readq == NULL || ! is_read_mode(dp->file_mode))
52                 return -EIO;
53
54         /* copy queued events to read buffer */
55         cnt = count;
56         pos = 0;
57         q = snd_seq_oss_readq_pick(readq, !is_nonblock_mode(dp->file_mode), &flags);
58         if (q == NULL)
59                 return 0;
60         do {
61                 int ev_len;
62                 /* tansfer the data */
63                 ev_len = ev_length(q);
64                 if (copy_to_user(buf + pos, q, ev_len)) {
65                         snd_seq_oss_readq_unlock(readq, flags);
66                         break;
67                 }
68                 snd_seq_oss_readq_free(readq, flags);
69                 pos += ev_len;
70                 cnt -= ev_len;
71                 if (cnt < ev_len)
72                         break;
73         } while ((q = snd_seq_oss_readq_pick(readq, 0, &flags)) != NULL);
74
75         return count - cnt;
76 }
77
78
79 /*
80  * write interface
81  */
82
83 int
84 snd_seq_oss_write(seq_oss_devinfo_t *dp, const char __user *buf, int count, struct file *opt)
85 {
86         int rc, c, p, ev_size;
87         evrec_t rec;
88
89         if (! is_write_mode(dp->file_mode) || dp->writeq == NULL)
90                 return -EIO;
91
92         c = count;
93         p = 0;
94         while (c >= SHORT_EVENT_SIZE) {
95                 if (copy_from_user(rec.c, buf + p, SHORT_EVENT_SIZE))
96                         break;
97                 p += SHORT_EVENT_SIZE;
98
99                 if (rec.s.code == SEQ_FULLSIZE) {
100                         /* load patch */
101                         int fmt = (*(unsigned short *)rec.c) & 0xffff;
102                         return snd_seq_oss_synth_load_patch(dp, rec.s.dev, fmt, buf, p, c);
103
104                 }
105                 if (ev_is_long(&rec)) {
106                         /* extended code */
107                         if (rec.s.code == SEQ_EXTENDED &&
108                             dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
109                                 return -EINVAL;
110                         ev_size = LONG_EVENT_SIZE;
111                         if (c < ev_size)
112                                 break;
113                         /* copy the reset 4 bytes */
114                         if (copy_from_user(rec.c + SHORT_EVENT_SIZE, buf + p,
115                                            LONG_EVENT_SIZE - SHORT_EVENT_SIZE))
116                                 break;
117                         p += LONG_EVENT_SIZE - SHORT_EVENT_SIZE;
118
119                 } else {
120                         /* old-type code */
121                         if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC)
122                                 return -EINVAL;
123                         ev_size = SHORT_EVENT_SIZE;
124                 }
125
126                 /* insert queue */
127                 if ((rc = insert_queue(dp, &rec, opt)) < 0)
128                         break;
129
130                 c -= ev_size;
131         }
132
133         if (count == c && is_nonblock_mode(dp->file_mode))
134                 return -EAGAIN;
135         return count - c;
136 }
137
138
139 /*
140  * insert event record to write queue
141  * return: 0 = OK, non-zero = NG
142  */
143 static int
144 insert_queue(seq_oss_devinfo_t *dp, evrec_t *rec, struct file *opt)
145 {
146         int rc = 0;
147         snd_seq_event_t event;
148
149         /* if this is a timing event, process the current time */
150         if (snd_seq_oss_process_timer_event(dp->timer, rec))
151                 return 0; /* no need to insert queue */
152
153         /* parse this event */
154         memset(&event, 0, sizeof(event));
155         /* set dummy -- to be sure */
156         event.type = SNDRV_SEQ_EVENT_NOTEOFF;
157         snd_seq_oss_fill_addr(dp, &event, dp->addr.port, dp->addr.client);
158
159         if (snd_seq_oss_process_event(dp, rec, &event))
160                 return 0; /* invalid event - no need to insert queue */
161
162         event.time.tick = snd_seq_oss_timer_cur_tick(dp->timer);
163         if (dp->timer->realtime || !dp->timer->running) {
164                 snd_seq_oss_dispatch(dp, &event, 0, 0);
165         } else {
166                 if (is_nonblock_mode(dp->file_mode))
167                         rc = snd_seq_kernel_client_enqueue(dp->cseq, &event, 0, 0);
168                 else
169                         rc = snd_seq_kernel_client_enqueue_blocking(dp->cseq, &event, opt, 0, 0);
170         }
171         return rc;
172 }
173                 
174
175 /*
176  * select / poll
177  */
178   
179 unsigned int
180 snd_seq_oss_poll(seq_oss_devinfo_t *dp, struct file *file, poll_table * wait)
181 {
182         unsigned int mask = 0;
183
184         /* input */
185         if (dp->readq && is_read_mode(dp->file_mode)) {
186                 if (snd_seq_oss_readq_poll(dp->readq, file, wait))
187                         mask |= POLLIN | POLLRDNORM;
188         }
189
190         /* output */
191         if (dp->writeq && is_write_mode(dp->file_mode)) {
192                 if (snd_seq_kernel_client_write_poll(dp->cseq, file, wait))
193                         mask |= POLLOUT | POLLWRNORM;
194         }
195         return mask;
196 }