Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * OSS compatible sequencer driver
0004  *
0005  * read/write/select interface to device file
0006  *
0007  * Copyright (C) 1998,99 Takashi Iwai <tiwai@suse.de>
0008  */
0009 
0010 #include "seq_oss_device.h"
0011 #include "seq_oss_readq.h"
0012 #include "seq_oss_writeq.h"
0013 #include "seq_oss_synth.h"
0014 #include <sound/seq_oss_legacy.h>
0015 #include "seq_oss_event.h"
0016 #include "seq_oss_timer.h"
0017 #include "../seq_clientmgr.h"
0018 
0019 
0020 /*
0021  * protoypes
0022  */
0023 static int insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt);
0024 
0025 
0026 /*
0027  * read interface
0028  */
0029 
0030 int
0031 snd_seq_oss_read(struct seq_oss_devinfo *dp, char __user *buf, int count)
0032 {
0033     struct seq_oss_readq *readq = dp->readq;
0034     int result = 0, err = 0;
0035     int ev_len;
0036     union evrec rec;
0037     unsigned long flags;
0038 
0039     if (readq == NULL || ! is_read_mode(dp->file_mode))
0040         return -ENXIO;
0041 
0042     while (count >= SHORT_EVENT_SIZE) {
0043         snd_seq_oss_readq_lock(readq, flags);
0044         err = snd_seq_oss_readq_pick(readq, &rec);
0045         if (err == -EAGAIN &&
0046             !is_nonblock_mode(dp->file_mode) && result == 0) {
0047             snd_seq_oss_readq_unlock(readq, flags);
0048             snd_seq_oss_readq_wait(readq);
0049             snd_seq_oss_readq_lock(readq, flags);
0050             if (signal_pending(current))
0051                 err = -ERESTARTSYS;
0052             else
0053                 err = snd_seq_oss_readq_pick(readq, &rec);
0054         }
0055         if (err < 0) {
0056             snd_seq_oss_readq_unlock(readq, flags);
0057             break;
0058         }
0059         ev_len = ev_length(&rec);
0060         if (ev_len < count) {
0061             snd_seq_oss_readq_unlock(readq, flags);
0062             break;
0063         }
0064         snd_seq_oss_readq_free(readq);
0065         snd_seq_oss_readq_unlock(readq, flags);
0066         if (copy_to_user(buf, &rec, ev_len)) {
0067             err = -EFAULT;
0068             break;
0069         }
0070         result += ev_len;
0071         buf += ev_len;
0072         count -= ev_len;
0073     }
0074     return result > 0 ? result : err;
0075 }
0076 
0077 
0078 /*
0079  * write interface
0080  */
0081 
0082 int
0083 snd_seq_oss_write(struct seq_oss_devinfo *dp, const char __user *buf, int count, struct file *opt)
0084 {
0085     int result = 0, err = 0;
0086     int ev_size, fmt;
0087     union evrec rec;
0088 
0089     if (! is_write_mode(dp->file_mode) || dp->writeq == NULL)
0090         return -ENXIO;
0091 
0092     while (count >= SHORT_EVENT_SIZE) {
0093         if (copy_from_user(&rec, buf, SHORT_EVENT_SIZE)) {
0094             err = -EFAULT;
0095             break;
0096         }
0097         if (rec.s.code == SEQ_FULLSIZE) {
0098             /* load patch */
0099             if (result > 0) {
0100                 err = -EINVAL;
0101                 break;
0102             }
0103             fmt = (*(unsigned short *)rec.c) & 0xffff;
0104             /* FIXME the return value isn't correct */
0105             return snd_seq_oss_synth_load_patch(dp, rec.s.dev,
0106                                 fmt, buf, 0, count);
0107         }
0108         if (ev_is_long(&rec)) {
0109             /* extended code */
0110             if (rec.s.code == SEQ_EXTENDED &&
0111                 dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
0112                 err = -EINVAL;
0113                 break;
0114             }
0115             ev_size = LONG_EVENT_SIZE;
0116             if (count < ev_size)
0117                 break;
0118             /* copy the reset 4 bytes */
0119             if (copy_from_user(rec.c + SHORT_EVENT_SIZE,
0120                        buf + SHORT_EVENT_SIZE,
0121                        LONG_EVENT_SIZE - SHORT_EVENT_SIZE)) {
0122                 err = -EFAULT;
0123                 break;
0124             }
0125         } else {
0126             /* old-type code */
0127             if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
0128                 err = -EINVAL;
0129                 break;
0130             }
0131             ev_size = SHORT_EVENT_SIZE;
0132         }
0133 
0134         /* insert queue */
0135         err = insert_queue(dp, &rec, opt);
0136         if (err < 0)
0137             break;
0138 
0139         result += ev_size;
0140         buf += ev_size;
0141         count -= ev_size;
0142     }
0143     return result > 0 ? result : err;
0144 }
0145 
0146 
0147 /*
0148  * insert event record to write queue
0149  * return: 0 = OK, non-zero = NG
0150  */
0151 static int
0152 insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt)
0153 {
0154     int rc = 0;
0155     struct snd_seq_event event;
0156 
0157     /* if this is a timing event, process the current time */
0158     if (snd_seq_oss_process_timer_event(dp->timer, rec))
0159         return 0; /* no need to insert queue */
0160 
0161     /* parse this event */
0162     memset(&event, 0, sizeof(event));
0163     /* set dummy -- to be sure */
0164     event.type = SNDRV_SEQ_EVENT_NOTEOFF;
0165     snd_seq_oss_fill_addr(dp, &event, dp->addr.client, dp->addr.port);
0166 
0167     if (snd_seq_oss_process_event(dp, rec, &event))
0168         return 0; /* invalid event - no need to insert queue */
0169 
0170     event.time.tick = snd_seq_oss_timer_cur_tick(dp->timer);
0171     if (dp->timer->realtime || !dp->timer->running)
0172         snd_seq_oss_dispatch(dp, &event, 0, 0);
0173     else
0174         rc = snd_seq_kernel_client_enqueue(dp->cseq, &event, opt,
0175                            !is_nonblock_mode(dp->file_mode));
0176     return rc;
0177 }
0178         
0179 
0180 /*
0181  * select / poll
0182  */
0183   
0184 __poll_t
0185 snd_seq_oss_poll(struct seq_oss_devinfo *dp, struct file *file, poll_table * wait)
0186 {
0187     __poll_t mask = 0;
0188 
0189     /* input */
0190     if (dp->readq && is_read_mode(dp->file_mode)) {
0191         if (snd_seq_oss_readq_poll(dp->readq, file, wait))
0192             mask |= EPOLLIN | EPOLLRDNORM;
0193     }
0194 
0195     /* output */
0196     if (dp->writeq && is_write_mode(dp->file_mode)) {
0197         if (snd_seq_kernel_client_write_poll(dp->cseq, file, wait))
0198             mask |= EPOLLOUT | EPOLLWRNORM;
0199     }
0200     return mask;
0201 }