seq_oss_rw.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include "seq_oss_device.h"
  23. #include "seq_oss_readq.h"
  24. #include "seq_oss_writeq.h"
  25. #include "seq_oss_synth.h"
  26. #include <sound/seq_oss_legacy.h>
  27. #include "seq_oss_event.h"
  28. #include "seq_oss_timer.h"
  29. #include "../seq_clientmgr.h"
  30. /*
  31. * protoypes
  32. */
  33. static int insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt);
  34. /*
  35. * read interface
  36. */
  37. int
  38. snd_seq_oss_read(struct seq_oss_devinfo *dp, char __user *buf, int count)
  39. {
  40. struct seq_oss_readq *readq = dp->readq;
  41. int result = 0, err = 0;
  42. int ev_len;
  43. union evrec rec;
  44. unsigned long flags;
  45. if (readq == NULL || ! is_read_mode(dp->file_mode))
  46. return -ENXIO;
  47. while (count >= SHORT_EVENT_SIZE) {
  48. snd_seq_oss_readq_lock(readq, flags);
  49. err = snd_seq_oss_readq_pick(readq, &rec);
  50. if (err == -EAGAIN &&
  51. !is_nonblock_mode(dp->file_mode) && result == 0) {
  52. snd_seq_oss_readq_unlock(readq, flags);
  53. snd_seq_oss_readq_wait(readq);
  54. snd_seq_oss_readq_lock(readq, flags);
  55. if (signal_pending(current))
  56. err = -ERESTARTSYS;
  57. else
  58. err = snd_seq_oss_readq_pick(readq, &rec);
  59. }
  60. if (err < 0) {
  61. snd_seq_oss_readq_unlock(readq, flags);
  62. break;
  63. }
  64. ev_len = ev_length(&rec);
  65. if (ev_len < count) {
  66. snd_seq_oss_readq_unlock(readq, flags);
  67. break;
  68. }
  69. snd_seq_oss_readq_free(readq);
  70. snd_seq_oss_readq_unlock(readq, flags);
  71. if (copy_to_user(buf, &rec, ev_len)) {
  72. err = -EFAULT;
  73. break;
  74. }
  75. result += ev_len;
  76. buf += ev_len;
  77. count -= ev_len;
  78. }
  79. return result > 0 ? result : err;
  80. }
  81. /*
  82. * write interface
  83. */
  84. int
  85. snd_seq_oss_write(struct seq_oss_devinfo *dp, const char __user *buf, int count, struct file *opt)
  86. {
  87. int result = 0, err = 0;
  88. int ev_size, fmt;
  89. union evrec rec;
  90. if (! is_write_mode(dp->file_mode) || dp->writeq == NULL)
  91. return -ENXIO;
  92. while (count >= SHORT_EVENT_SIZE) {
  93. if (copy_from_user(&rec, buf, SHORT_EVENT_SIZE)) {
  94. err = -EFAULT;
  95. break;
  96. }
  97. if (rec.s.code == SEQ_FULLSIZE) {
  98. /* load patch */
  99. if (result > 0) {
  100. err = -EINVAL;
  101. break;
  102. }
  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);
  107. }
  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;
  114. }
  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;
  124. }
  125. } else {
  126. /* old-type code */
  127. if (dp->seq_mode == SNDRV_SEQ_OSS_MODE_MUSIC) {
  128. err = -EINVAL;
  129. break;
  130. }
  131. ev_size = SHORT_EVENT_SIZE;
  132. }
  133. /* insert queue */
  134. if ((err = insert_queue(dp, &rec, opt)) < 0)
  135. break;
  136. result += ev_size;
  137. buf += ev_size;
  138. count -= ev_size;
  139. }
  140. return result > 0 ? result : err;
  141. }
  142. /*
  143. * insert event record to write queue
  144. * return: 0 = OK, non-zero = NG
  145. */
  146. static int
  147. insert_queue(struct seq_oss_devinfo *dp, union evrec *rec, struct file *opt)
  148. {
  149. int rc = 0;
  150. struct snd_seq_event event;
  151. /* if this is a timing event, process the current time */
  152. if (snd_seq_oss_process_timer_event(dp->timer, rec))
  153. return 0; /* no need to insert queue */
  154. /* parse this event */
  155. memset(&event, 0, sizeof(event));
  156. /* set dummy -- to be sure */
  157. event.type = SNDRV_SEQ_EVENT_NOTEOFF;
  158. snd_seq_oss_fill_addr(dp, &event, dp->addr.port, dp->addr.client);
  159. if (snd_seq_oss_process_event(dp, rec, &event))
  160. return 0; /* invalid event - no need to insert queue */
  161. event.time.tick = snd_seq_oss_timer_cur_tick(dp->timer);
  162. if (dp->timer->realtime || !dp->timer->running) {
  163. snd_seq_oss_dispatch(dp, &event, 0, 0);
  164. } else {
  165. if (is_nonblock_mode(dp->file_mode))
  166. rc = snd_seq_kernel_client_enqueue(dp->cseq, &event, 0, 0);
  167. else
  168. rc = snd_seq_kernel_client_enqueue_blocking(dp->cseq, &event, opt, 0, 0);
  169. }
  170. return rc;
  171. }
  172. /*
  173. * select / poll
  174. */
  175. unsigned int
  176. snd_seq_oss_poll(struct seq_oss_devinfo *dp, struct file *file, poll_table * wait)
  177. {
  178. unsigned int mask = 0;
  179. /* input */
  180. if (dp->readq && is_read_mode(dp->file_mode)) {
  181. if (snd_seq_oss_readq_poll(dp->readq, file, wait))
  182. mask |= POLLIN | POLLRDNORM;
  183. }
  184. /* output */
  185. if (dp->writeq && is_write_mode(dp->file_mode)) {
  186. if (snd_seq_kernel_client_write_poll(dp->cseq, file, wait))
  187. mask |= POLLOUT | POLLWRNORM;
  188. }
  189. return mask;
  190. }