seq_memory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * ALSA sequencer Memory Manager
  3. * Copyright (c) 1998 by Frank van de Pol <fvdpol@coil.demon.nl>
  4. * Jaroslav Kysela <perex@suse.cz>
  5. * 2000 by Takashi Iwai <tiwai@suse.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <sound/driver.h>
  23. #include <linux/init.h>
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <sound/core.h>
  27. #include <sound/seq_kernel.h>
  28. #include "seq_memory.h"
  29. #include "seq_queue.h"
  30. #include "seq_info.h"
  31. #include "seq_lock.h"
  32. /* semaphore in struct file record */
  33. #define semaphore_of(fp) ((fp)->f_dentry->d_inode->i_sem)
  34. inline static int snd_seq_pool_available(pool_t *pool)
  35. {
  36. return pool->total_elements - atomic_read(&pool->counter);
  37. }
  38. inline static int snd_seq_output_ok(pool_t *pool)
  39. {
  40. return snd_seq_pool_available(pool) >= pool->room;
  41. }
  42. /*
  43. * Variable length event:
  44. * The event like sysex uses variable length type.
  45. * The external data may be stored in three different formats.
  46. * 1) kernel space
  47. * This is the normal case.
  48. * ext.data.len = length
  49. * ext.data.ptr = buffer pointer
  50. * 2) user space
  51. * When an event is generated via read(), the external data is
  52. * kept in user space until expanded.
  53. * ext.data.len = length | SNDRV_SEQ_EXT_USRPTR
  54. * ext.data.ptr = userspace pointer
  55. * 3) chained cells
  56. * When the variable length event is enqueued (in prioq or fifo),
  57. * the external data is decomposed to several cells.
  58. * ext.data.len = length | SNDRV_SEQ_EXT_CHAINED
  59. * ext.data.ptr = the additiona cell head
  60. * -> cell.next -> cell.next -> ..
  61. */
  62. /*
  63. * exported:
  64. * call dump function to expand external data.
  65. */
  66. static int get_var_len(const snd_seq_event_t *event)
  67. {
  68. if ((event->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
  69. return -EINVAL;
  70. return event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
  71. }
  72. int snd_seq_dump_var_event(const snd_seq_event_t *event, snd_seq_dump_func_t func, void *private_data)
  73. {
  74. int len, err;
  75. snd_seq_event_cell_t *cell;
  76. if ((len = get_var_len(event)) <= 0)
  77. return len;
  78. if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
  79. char buf[32];
  80. char __user *curptr = (char __user *)event->data.ext.ptr;
  81. while (len > 0) {
  82. int size = sizeof(buf);
  83. if (len < size)
  84. size = len;
  85. if (copy_from_user(buf, curptr, size))
  86. return -EFAULT;
  87. err = func(private_data, buf, size);
  88. if (err < 0)
  89. return err;
  90. curptr += size;
  91. len -= size;
  92. }
  93. return 0;
  94. } if (! (event->data.ext.len & SNDRV_SEQ_EXT_CHAINED)) {
  95. return func(private_data, event->data.ext.ptr, len);
  96. }
  97. cell = (snd_seq_event_cell_t*)event->data.ext.ptr;
  98. for (; len > 0 && cell; cell = cell->next) {
  99. int size = sizeof(snd_seq_event_t);
  100. if (len < size)
  101. size = len;
  102. err = func(private_data, &cell->event, size);
  103. if (err < 0)
  104. return err;
  105. len -= size;
  106. }
  107. return 0;
  108. }
  109. /*
  110. * exported:
  111. * expand the variable length event to linear buffer space.
  112. */
  113. static int seq_copy_in_kernel(char **bufptr, const void *src, int size)
  114. {
  115. memcpy(*bufptr, src, size);
  116. *bufptr += size;
  117. return 0;
  118. }
  119. static int seq_copy_in_user(char __user **bufptr, const void *src, int size)
  120. {
  121. if (copy_to_user(*bufptr, src, size))
  122. return -EFAULT;
  123. *bufptr += size;
  124. return 0;
  125. }
  126. int snd_seq_expand_var_event(const snd_seq_event_t *event, int count, char *buf, int in_kernel, int size_aligned)
  127. {
  128. int len, newlen;
  129. int err;
  130. if ((len = get_var_len(event)) < 0)
  131. return len;
  132. newlen = len;
  133. if (size_aligned > 0)
  134. newlen = ((len + size_aligned - 1) / size_aligned) * size_aligned;
  135. if (count < newlen)
  136. return -EAGAIN;
  137. if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
  138. if (! in_kernel)
  139. return -EINVAL;
  140. if (copy_from_user(buf, (void __user *)event->data.ext.ptr, len))
  141. return -EFAULT;
  142. return newlen;
  143. }
  144. err = snd_seq_dump_var_event(event,
  145. in_kernel ? (snd_seq_dump_func_t)seq_copy_in_kernel :
  146. (snd_seq_dump_func_t)seq_copy_in_user,
  147. &buf);
  148. return err < 0 ? err : newlen;
  149. }
  150. /*
  151. * release this cell, free extended data if available
  152. */
  153. static inline void free_cell(pool_t *pool, snd_seq_event_cell_t *cell)
  154. {
  155. cell->next = pool->free;
  156. pool->free = cell;
  157. atomic_dec(&pool->counter);
  158. }
  159. void snd_seq_cell_free(snd_seq_event_cell_t * cell)
  160. {
  161. unsigned long flags;
  162. pool_t *pool;
  163. snd_assert(cell != NULL, return);
  164. pool = cell->pool;
  165. snd_assert(pool != NULL, return);
  166. spin_lock_irqsave(&pool->lock, flags);
  167. free_cell(pool, cell);
  168. if (snd_seq_ev_is_variable(&cell->event)) {
  169. if (cell->event.data.ext.len & SNDRV_SEQ_EXT_CHAINED) {
  170. snd_seq_event_cell_t *curp, *nextptr;
  171. curp = cell->event.data.ext.ptr;
  172. for (; curp; curp = nextptr) {
  173. nextptr = curp->next;
  174. curp->next = pool->free;
  175. free_cell(pool, curp);
  176. }
  177. }
  178. }
  179. if (waitqueue_active(&pool->output_sleep)) {
  180. /* has enough space now? */
  181. if (snd_seq_output_ok(pool))
  182. wake_up(&pool->output_sleep);
  183. }
  184. spin_unlock_irqrestore(&pool->lock, flags);
  185. }
  186. /*
  187. * allocate an event cell.
  188. */
  189. static int snd_seq_cell_alloc(pool_t *pool, snd_seq_event_cell_t **cellp, int nonblock, struct file *file)
  190. {
  191. snd_seq_event_cell_t *cell;
  192. unsigned long flags;
  193. int err = -EAGAIN;
  194. wait_queue_t wait;
  195. if (pool == NULL)
  196. return -EINVAL;
  197. *cellp = NULL;
  198. init_waitqueue_entry(&wait, current);
  199. spin_lock_irqsave(&pool->lock, flags);
  200. if (pool->ptr == NULL) { /* not initialized */
  201. snd_printd("seq: pool is not initialized\n");
  202. err = -EINVAL;
  203. goto __error;
  204. }
  205. while (pool->free == NULL && ! nonblock && ! pool->closing) {
  206. set_current_state(TASK_INTERRUPTIBLE);
  207. add_wait_queue(&pool->output_sleep, &wait);
  208. spin_unlock_irq(&pool->lock);
  209. schedule();
  210. spin_lock_irq(&pool->lock);
  211. remove_wait_queue(&pool->output_sleep, &wait);
  212. /* interrupted? */
  213. if (signal_pending(current)) {
  214. err = -ERESTARTSYS;
  215. goto __error;
  216. }
  217. }
  218. if (pool->closing) { /* closing.. */
  219. err = -ENOMEM;
  220. goto __error;
  221. }
  222. cell = pool->free;
  223. if (cell) {
  224. int used;
  225. pool->free = cell->next;
  226. atomic_inc(&pool->counter);
  227. used = atomic_read(&pool->counter);
  228. if (pool->max_used < used)
  229. pool->max_used = used;
  230. pool->event_alloc_success++;
  231. /* clear cell pointers */
  232. cell->next = NULL;
  233. err = 0;
  234. } else
  235. pool->event_alloc_failures++;
  236. *cellp = cell;
  237. __error:
  238. spin_unlock_irqrestore(&pool->lock, flags);
  239. return err;
  240. }
  241. /*
  242. * duplicate the event to a cell.
  243. * if the event has external data, the data is decomposed to additional
  244. * cells.
  245. */
  246. int snd_seq_event_dup(pool_t *pool, snd_seq_event_t *event, snd_seq_event_cell_t **cellp, int nonblock, struct file *file)
  247. {
  248. int ncells, err;
  249. unsigned int extlen;
  250. snd_seq_event_cell_t *cell;
  251. *cellp = NULL;
  252. ncells = 0;
  253. extlen = 0;
  254. if (snd_seq_ev_is_variable(event)) {
  255. extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
  256. ncells = (extlen + sizeof(snd_seq_event_t) - 1) / sizeof(snd_seq_event_t);
  257. }
  258. if (ncells >= pool->total_elements)
  259. return -ENOMEM;
  260. err = snd_seq_cell_alloc(pool, &cell, nonblock, file);
  261. if (err < 0)
  262. return err;
  263. /* copy the event */
  264. cell->event = *event;
  265. /* decompose */
  266. if (snd_seq_ev_is_variable(event)) {
  267. int len = extlen;
  268. int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED;
  269. int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR;
  270. snd_seq_event_cell_t *src, *tmp, *tail;
  271. char *buf;
  272. cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED;
  273. cell->event.data.ext.ptr = NULL;
  274. src = (snd_seq_event_cell_t*)event->data.ext.ptr;
  275. buf = (char *)event->data.ext.ptr;
  276. tail = NULL;
  277. while (ncells-- > 0) {
  278. int size = sizeof(snd_seq_event_t);
  279. if (len < size)
  280. size = len;
  281. err = snd_seq_cell_alloc(pool, &tmp, nonblock, file);
  282. if (err < 0)
  283. goto __error;
  284. if (cell->event.data.ext.ptr == NULL)
  285. cell->event.data.ext.ptr = tmp;
  286. if (tail)
  287. tail->next = tmp;
  288. tail = tmp;
  289. /* copy chunk */
  290. if (is_chained && src) {
  291. tmp->event = src->event;
  292. src = src->next;
  293. } else if (is_usrptr) {
  294. if (copy_from_user(&tmp->event, (char __user *)buf, size)) {
  295. err = -EFAULT;
  296. goto __error;
  297. }
  298. } else {
  299. memcpy(&tmp->event, buf, size);
  300. }
  301. buf += size;
  302. len -= size;
  303. }
  304. }
  305. *cellp = cell;
  306. return 0;
  307. __error:
  308. snd_seq_cell_free(cell);
  309. return err;
  310. }
  311. /* poll wait */
  312. int snd_seq_pool_poll_wait(pool_t *pool, struct file *file, poll_table *wait)
  313. {
  314. poll_wait(file, &pool->output_sleep, wait);
  315. return snd_seq_output_ok(pool);
  316. }
  317. /* allocate room specified number of events */
  318. int snd_seq_pool_init(pool_t *pool)
  319. {
  320. int cell;
  321. snd_seq_event_cell_t *cellptr;
  322. unsigned long flags;
  323. snd_assert(pool != NULL, return -EINVAL);
  324. if (pool->ptr) /* should be atomic? */
  325. return 0;
  326. pool->ptr = vmalloc(sizeof(snd_seq_event_cell_t) * pool->size);
  327. if (pool->ptr == NULL) {
  328. snd_printd("seq: malloc for sequencer events failed\n");
  329. return -ENOMEM;
  330. }
  331. /* add new cells to the free cell list */
  332. spin_lock_irqsave(&pool->lock, flags);
  333. pool->free = NULL;
  334. for (cell = 0; cell < pool->size; cell++) {
  335. cellptr = pool->ptr + cell;
  336. cellptr->pool = pool;
  337. cellptr->next = pool->free;
  338. pool->free = cellptr;
  339. }
  340. pool->room = (pool->size + 1) / 2;
  341. /* init statistics */
  342. pool->max_used = 0;
  343. pool->total_elements = pool->size;
  344. spin_unlock_irqrestore(&pool->lock, flags);
  345. return 0;
  346. }
  347. /* remove events */
  348. int snd_seq_pool_done(pool_t *pool)
  349. {
  350. unsigned long flags;
  351. snd_seq_event_cell_t *ptr;
  352. int max_count = 5 * HZ;
  353. snd_assert(pool != NULL, return -EINVAL);
  354. /* wait for closing all threads */
  355. spin_lock_irqsave(&pool->lock, flags);
  356. pool->closing = 1;
  357. spin_unlock_irqrestore(&pool->lock, flags);
  358. if (waitqueue_active(&pool->output_sleep))
  359. wake_up(&pool->output_sleep);
  360. while (atomic_read(&pool->counter) > 0) {
  361. if (max_count == 0) {
  362. snd_printk(KERN_WARNING "snd_seq_pool_done timeout: %d cells remain\n", atomic_read(&pool->counter));
  363. break;
  364. }
  365. set_current_state(TASK_UNINTERRUPTIBLE);
  366. schedule_timeout(1);
  367. max_count--;
  368. }
  369. /* release all resources */
  370. spin_lock_irqsave(&pool->lock, flags);
  371. ptr = pool->ptr;
  372. pool->ptr = NULL;
  373. pool->free = NULL;
  374. pool->total_elements = 0;
  375. spin_unlock_irqrestore(&pool->lock, flags);
  376. vfree(ptr);
  377. spin_lock_irqsave(&pool->lock, flags);
  378. pool->closing = 0;
  379. spin_unlock_irqrestore(&pool->lock, flags);
  380. return 0;
  381. }
  382. /* init new memory pool */
  383. pool_t *snd_seq_pool_new(int poolsize)
  384. {
  385. pool_t *pool;
  386. /* create pool block */
  387. pool = kcalloc(1, sizeof(*pool), GFP_KERNEL);
  388. if (pool == NULL) {
  389. snd_printd("seq: malloc failed for pool\n");
  390. return NULL;
  391. }
  392. spin_lock_init(&pool->lock);
  393. pool->ptr = NULL;
  394. pool->free = NULL;
  395. pool->total_elements = 0;
  396. atomic_set(&pool->counter, 0);
  397. pool->closing = 0;
  398. init_waitqueue_head(&pool->output_sleep);
  399. pool->size = poolsize;
  400. /* init statistics */
  401. pool->max_used = 0;
  402. return pool;
  403. }
  404. /* remove memory pool */
  405. int snd_seq_pool_delete(pool_t **ppool)
  406. {
  407. pool_t *pool = *ppool;
  408. *ppool = NULL;
  409. if (pool == NULL)
  410. return 0;
  411. snd_seq_pool_done(pool);
  412. kfree(pool);
  413. return 0;
  414. }
  415. /* initialize sequencer memory */
  416. int __init snd_sequencer_memory_init(void)
  417. {
  418. return 0;
  419. }
  420. /* release sequencer memory */
  421. void __exit snd_sequencer_memory_done(void)
  422. {
  423. }
  424. /* exported to seq_clientmgr.c */
  425. void snd_seq_info_pool(snd_info_buffer_t * buffer, pool_t *pool, char *space)
  426. {
  427. if (pool == NULL)
  428. return;
  429. snd_iprintf(buffer, "%sPool size : %d\n", space, pool->total_elements);
  430. snd_iprintf(buffer, "%sCells in use : %d\n", space, atomic_read(&pool->counter));
  431. snd_iprintf(buffer, "%sPeak cells in use : %d\n", space, pool->max_used);
  432. snd_iprintf(buffer, "%sAlloc success : %d\n", space, pool->event_alloc_success);
  433. snd_iprintf(buffer, "%sAlloc failures : %d\n", space, pool->event_alloc_failures);
  434. }