midibuf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * sound/oss/midibuf.c
  3. *
  4. * Device file manager for /dev/midi#
  5. */
  6. /*
  7. * Copyright (C) by Hannu Savolainen 1993-1997
  8. *
  9. * OSS/Free for Linux is distributed under the GNU GENERAL PUBLIC LICENSE (GPL)
  10. * Version 2 (June 1991). See the "COPYING" file distributed with this software
  11. * for more info.
  12. */
  13. /*
  14. * Thomas Sailer : ioctl code reworked (vmalloc/vfree removed)
  15. */
  16. #include <linux/stddef.h>
  17. #include <linux/kmod.h>
  18. #include <linux/spinlock.h>
  19. #define MIDIBUF_C
  20. #include "sound_config.h"
  21. /*
  22. * Don't make MAX_QUEUE_SIZE larger than 4000
  23. */
  24. #define MAX_QUEUE_SIZE 4000
  25. static wait_queue_head_t midi_sleeper[MAX_MIDI_DEV];
  26. static wait_queue_head_t input_sleeper[MAX_MIDI_DEV];
  27. struct midi_buf
  28. {
  29. int len, head, tail;
  30. unsigned char queue[MAX_QUEUE_SIZE];
  31. };
  32. struct midi_parms
  33. {
  34. long prech_timeout; /*
  35. * Timeout before the first ch
  36. */
  37. };
  38. static struct midi_buf *midi_out_buf[MAX_MIDI_DEV] = {NULL};
  39. static struct midi_buf *midi_in_buf[MAX_MIDI_DEV] = {NULL};
  40. static struct midi_parms parms[MAX_MIDI_DEV];
  41. static void midi_poll(unsigned long dummy);
  42. static DEFINE_TIMER(poll_timer, midi_poll, 0, 0);
  43. static volatile int open_devs;
  44. static DEFINE_SPINLOCK(lock);
  45. #define DATA_AVAIL(q) (q->len)
  46. #define SPACE_AVAIL(q) (MAX_QUEUE_SIZE - q->len)
  47. #define QUEUE_BYTE(q, data) \
  48. if (SPACE_AVAIL(q)) \
  49. { \
  50. unsigned long flags; \
  51. spin_lock_irqsave(&lock, flags); \
  52. q->queue[q->tail] = (data); \
  53. q->len++; q->tail = (q->tail+1) % MAX_QUEUE_SIZE; \
  54. spin_unlock_irqrestore(&lock, flags); \
  55. }
  56. #define REMOVE_BYTE(q, data) \
  57. if (DATA_AVAIL(q)) \
  58. { \
  59. unsigned long flags; \
  60. spin_lock_irqsave(&lock, flags); \
  61. data = q->queue[q->head]; \
  62. q->len--; q->head = (q->head+1) % MAX_QUEUE_SIZE; \
  63. spin_unlock_irqrestore(&lock, flags); \
  64. }
  65. static void drain_midi_queue(int dev)
  66. {
  67. /*
  68. * Give the Midi driver time to drain its output queues
  69. */
  70. if (midi_devs[dev]->buffer_status != NULL)
  71. wait_event_interruptible_timeout(midi_sleeper[dev],
  72. !midi_devs[dev]->buffer_status(dev), HZ/10);
  73. }
  74. static void midi_input_intr(int dev, unsigned char data)
  75. {
  76. if (midi_in_buf[dev] == NULL)
  77. return;
  78. if (data == 0xfe) /*
  79. * Active sensing
  80. */
  81. return; /*
  82. * Ignore
  83. */
  84. if (SPACE_AVAIL(midi_in_buf[dev])) {
  85. QUEUE_BYTE(midi_in_buf[dev], data);
  86. wake_up(&input_sleeper[dev]);
  87. }
  88. }
  89. static void midi_output_intr(int dev)
  90. {
  91. /*
  92. * Currently NOP
  93. */
  94. }
  95. static void midi_poll(unsigned long dummy)
  96. {
  97. unsigned long flags;
  98. int dev;
  99. spin_lock_irqsave(&lock, flags);
  100. if (open_devs)
  101. {
  102. for (dev = 0; dev < num_midis; dev++)
  103. if (midi_devs[dev] != NULL && midi_out_buf[dev] != NULL)
  104. {
  105. while (DATA_AVAIL(midi_out_buf[dev]))
  106. {
  107. int ok;
  108. int c = midi_out_buf[dev]->queue[midi_out_buf[dev]->head];
  109. spin_unlock_irqrestore(&lock,flags);/* Give some time to others */
  110. ok = midi_devs[dev]->outputc(dev, c);
  111. spin_lock_irqsave(&lock, flags);
  112. if (!ok)
  113. break;
  114. midi_out_buf[dev]->head = (midi_out_buf[dev]->head + 1) % MAX_QUEUE_SIZE;
  115. midi_out_buf[dev]->len--;
  116. }
  117. if (DATA_AVAIL(midi_out_buf[dev]) < 100)
  118. wake_up(&midi_sleeper[dev]);
  119. }
  120. poll_timer.expires = (1) + jiffies;
  121. add_timer(&poll_timer);
  122. /*
  123. * Come back later
  124. */
  125. }
  126. spin_unlock_irqrestore(&lock, flags);
  127. }
  128. int MIDIbuf_open(int dev, struct file *file)
  129. {
  130. int mode, err;
  131. dev = dev >> 4;
  132. mode = translate_mode(file);
  133. if (num_midis > MAX_MIDI_DEV)
  134. {
  135. printk(KERN_ERR "midi: Too many midi interfaces\n");
  136. num_midis = MAX_MIDI_DEV;
  137. }
  138. if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
  139. return -ENXIO;
  140. /*
  141. * Interrupts disabled. Be careful
  142. */
  143. module_put(midi_devs[dev]->owner);
  144. if ((err = midi_devs[dev]->open(dev, mode,
  145. midi_input_intr, midi_output_intr)) < 0)
  146. return err;
  147. parms[dev].prech_timeout = MAX_SCHEDULE_TIMEOUT;
  148. midi_in_buf[dev] = vmalloc(sizeof(struct midi_buf));
  149. if (midi_in_buf[dev] == NULL)
  150. {
  151. printk(KERN_WARNING "midi: Can't allocate buffer\n");
  152. midi_devs[dev]->close(dev);
  153. return -EIO;
  154. }
  155. midi_in_buf[dev]->len = midi_in_buf[dev]->head = midi_in_buf[dev]->tail = 0;
  156. midi_out_buf[dev] = vmalloc(sizeof(struct midi_buf));
  157. if (midi_out_buf[dev] == NULL)
  158. {
  159. printk(KERN_WARNING "midi: Can't allocate buffer\n");
  160. midi_devs[dev]->close(dev);
  161. vfree(midi_in_buf[dev]);
  162. midi_in_buf[dev] = NULL;
  163. return -EIO;
  164. }
  165. midi_out_buf[dev]->len = midi_out_buf[dev]->head = midi_out_buf[dev]->tail = 0;
  166. open_devs++;
  167. init_waitqueue_head(&midi_sleeper[dev]);
  168. init_waitqueue_head(&input_sleeper[dev]);
  169. if (open_devs < 2) /* This was first open */
  170. {
  171. poll_timer.expires = 1 + jiffies;
  172. add_timer(&poll_timer); /* Start polling */
  173. }
  174. return err;
  175. }
  176. void MIDIbuf_release(int dev, struct file *file)
  177. {
  178. int mode;
  179. dev = dev >> 4;
  180. mode = translate_mode(file);
  181. if (dev < 0 || dev >= num_midis || midi_devs[dev] == NULL)
  182. return;
  183. /*
  184. * Wait until the queue is empty
  185. */
  186. if (mode != OPEN_READ)
  187. {
  188. midi_devs[dev]->outputc(dev, 0xfe); /*
  189. * Active sensing to shut the
  190. * devices
  191. */
  192. wait_event_interruptible(midi_sleeper[dev],
  193. !DATA_AVAIL(midi_out_buf[dev]));
  194. /*
  195. * Sync
  196. */
  197. drain_midi_queue(dev); /*
  198. * Ensure the output queues are empty
  199. */
  200. }
  201. midi_devs[dev]->close(dev);
  202. open_devs--;
  203. if (open_devs == 0)
  204. del_timer_sync(&poll_timer);
  205. vfree(midi_in_buf[dev]);
  206. vfree(midi_out_buf[dev]);
  207. midi_in_buf[dev] = NULL;
  208. midi_out_buf[dev] = NULL;
  209. module_put(midi_devs[dev]->owner);
  210. }
  211. int MIDIbuf_write(int dev, struct file *file, const char __user *buf, int count)
  212. {
  213. int c, n, i;
  214. unsigned char tmp_data;
  215. dev = dev >> 4;
  216. if (!count)
  217. return 0;
  218. c = 0;
  219. while (c < count)
  220. {
  221. n = SPACE_AVAIL(midi_out_buf[dev]);
  222. if (n == 0) { /*
  223. * No space just now.
  224. */
  225. if (file->f_flags & O_NONBLOCK) {
  226. c = -EAGAIN;
  227. goto out;
  228. }
  229. if (wait_event_interruptible(midi_sleeper[dev],
  230. SPACE_AVAIL(midi_out_buf[dev])))
  231. {
  232. c = -EINTR;
  233. goto out;
  234. }
  235. n = SPACE_AVAIL(midi_out_buf[dev]);
  236. }
  237. if (n > (count - c))
  238. n = count - c;
  239. for (i = 0; i < n; i++)
  240. {
  241. /* BROKE BROKE BROKE - CAN'T DO THIS WITH CLI !! */
  242. /* yes, think the same, so I removed the cli() brackets
  243. QUEUE_BYTE is protected against interrupts */
  244. if (copy_from_user((char *) &tmp_data, &(buf)[c], 1)) {
  245. c = -EFAULT;
  246. goto out;
  247. }
  248. QUEUE_BYTE(midi_out_buf[dev], tmp_data);
  249. c++;
  250. }
  251. }
  252. out:
  253. return c;
  254. }
  255. int MIDIbuf_read(int dev, struct file *file, char __user *buf, int count)
  256. {
  257. int n, c = 0;
  258. unsigned char tmp_data;
  259. dev = dev >> 4;
  260. if (!DATA_AVAIL(midi_in_buf[dev])) { /*
  261. * No data yet, wait
  262. */
  263. if (file->f_flags & O_NONBLOCK) {
  264. c = -EAGAIN;
  265. goto out;
  266. }
  267. wait_event_interruptible_timeout(input_sleeper[dev],
  268. DATA_AVAIL(midi_in_buf[dev]),
  269. parms[dev].prech_timeout);
  270. if (signal_pending(current))
  271. c = -EINTR; /* The user is getting restless */
  272. }
  273. if (c == 0 && DATA_AVAIL(midi_in_buf[dev])) /*
  274. * Got some bytes
  275. */
  276. {
  277. n = DATA_AVAIL(midi_in_buf[dev]);
  278. if (n > count)
  279. n = count;
  280. c = 0;
  281. while (c < n)
  282. {
  283. char *fixit;
  284. REMOVE_BYTE(midi_in_buf[dev], tmp_data);
  285. fixit = (char *) &tmp_data;
  286. /* BROKE BROKE BROKE */
  287. /* yes removed the cli() brackets again
  288. should q->len,tail&head be atomic_t? */
  289. if (copy_to_user(&(buf)[c], fixit, 1)) {
  290. c = -EFAULT;
  291. goto out;
  292. }
  293. c++;
  294. }
  295. }
  296. out:
  297. return c;
  298. }
  299. int MIDIbuf_ioctl(int dev, struct file *file,
  300. unsigned int cmd, void __user *arg)
  301. {
  302. int val;
  303. dev = dev >> 4;
  304. if (((cmd >> 8) & 0xff) == 'C')
  305. {
  306. if (midi_devs[dev]->coproc) /* Coprocessor ioctl */
  307. return midi_devs[dev]->coproc->ioctl(midi_devs[dev]->coproc->devc, cmd, arg, 0);
  308. /* printk("/dev/midi%d: No coprocessor for this device\n", dev);*/
  309. return -ENXIO;
  310. }
  311. else
  312. {
  313. switch (cmd)
  314. {
  315. case SNDCTL_MIDI_PRETIME:
  316. if (get_user(val, (int __user *)arg))
  317. return -EFAULT;
  318. if (val < 0)
  319. val = 0;
  320. val = (HZ * val) / 10;
  321. parms[dev].prech_timeout = val;
  322. return put_user(val, (int __user *)arg);
  323. default:
  324. if (!midi_devs[dev]->ioctl)
  325. return -EINVAL;
  326. return midi_devs[dev]->ioctl(dev, cmd, arg);
  327. }
  328. }
  329. }
  330. /* No kernel lock - fine */
  331. unsigned int MIDIbuf_poll(int dev, struct file *file, poll_table * wait)
  332. {
  333. unsigned int mask = 0;
  334. dev = dev >> 4;
  335. /* input */
  336. poll_wait(file, &input_sleeper[dev], wait);
  337. if (DATA_AVAIL(midi_in_buf[dev]))
  338. mask |= POLLIN | POLLRDNORM;
  339. /* output */
  340. poll_wait(file, &midi_sleeper[dev], wait);
  341. if (!SPACE_AVAIL(midi_out_buf[dev]))
  342. mask |= POLLOUT | POLLWRNORM;
  343. return mask;
  344. }
  345. int MIDIbuf_avail(int dev)
  346. {
  347. if (midi_in_buf[dev])
  348. return DATA_AVAIL (midi_in_buf[dev]);
  349. return 0;
  350. }
  351. EXPORT_SYMBOL(MIDIbuf_avail);