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. while (!signal_pending(current) && midi_devs[dev]->buffer_status(dev))
  72. interruptible_sleep_on_timeout(&midi_sleeper[dev],
  73. 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. while (!signal_pending(current) && DATA_AVAIL(midi_out_buf[dev]))
  194. interruptible_sleep_on(&midi_sleeper[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. interruptible_sleep_on(&midi_sleeper[dev]);
  231. if (signal_pending(current))
  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. interruptible_sleep_on_timeout(&input_sleeper[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);