midibuf.c 8.6 KB

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