seq_memory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. static inline int snd_seq_pool_available(struct snd_seq_pool *pool)
  33. {
  34. return pool->total_elements - atomic_read(&pool->counter);
  35. }
  36. static inline int snd_seq_output_ok(struct snd_seq_pool *pool)
  37. {
  38. return snd_seq_pool_available(pool) >= pool->room;
  39. }
  40. /*
  41. * Variable length event:
  42. * The event like sysex uses variable length type.
  43. * The external data may be stored in three different formats.
  44. * 1) kernel space
  45. * This is the normal case.
  46. * ext.data.len = length
  47. * ext.data.ptr = buffer pointer
  48. * 2) user space
  49. * When an event is generated via read(), the external data is
  50. * kept in user space until expanded.
  51. * ext.data.len = length | SNDRV_SEQ_EXT_USRPTR
  52. * ext.data.ptr = userspace pointer
  53. * 3) chained cells
  54. * When the variable length event is enqueued (in prioq or fifo),
  55. * the external data is decomposed to several cells.
  56. * ext.data.len = length | SNDRV_SEQ_EXT_CHAINED
  57. * ext.data.ptr = the additiona cell head
  58. * -> cell.next -> cell.next -> ..
  59. */
  60. /*
  61. * exported:
  62. * call dump function to expand external data.
  63. */
  64. static int get_var_len(const struct snd_seq_event *event)
  65. {
  66. if ((event->flags & SNDRV_SEQ_EVENT_LENGTH_MASK) != SNDRV_SEQ_EVENT_LENGTH_VARIABLE)
  67. return -EINVAL;
  68. return event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
  69. }
  70. int snd_seq_dump_var_event(const struct snd_seq_event *event,
  71. snd_seq_dump_func_t func, void *private_data)
  72. {
  73. int len, err;
  74. struct snd_seq_event_cell *cell;
  75. if ((len = get_var_len(event)) <= 0)
  76. return len;
  77. if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
  78. char buf[32];
  79. char __user *curptr = (char __user *)event->data.ext.ptr;
  80. while (len > 0) {
  81. int size = sizeof(buf);
  82. if (len < size)
  83. size = len;
  84. if (copy_from_user(buf, curptr, size))
  85. return -EFAULT;
  86. err = func(private_data, buf, size);
  87. if (err < 0)
  88. return err;
  89. curptr += size;
  90. len -= size;
  91. }
  92. return 0;
  93. } if (! (event->data.ext.len & SNDRV_SEQ_EXT_CHAINED)) {
  94. return func(private_data, event->data.ext.ptr, len);
  95. }
  96. cell = (struct snd_seq_event_cell *)event->data.ext.ptr;
  97. for (; len > 0 && cell; cell = cell->next) {
  98. int size = sizeof(struct snd_seq_event);
  99. if (len < size)
  100. size = len;
  101. err = func(private_data, &cell->event, size);
  102. if (err < 0)
  103. return err;
  104. len -= size;
  105. }
  106. return 0;
  107. }
  108. /*
  109. * exported:
  110. * expand the variable length event to linear buffer space.
  111. */
  112. static int seq_copy_in_kernel(char **bufptr, const void *src, int size)
  113. {
  114. memcpy(*bufptr, src, size);
  115. *bufptr += size;
  116. return 0;
  117. }
  118. static int seq_copy_in_user(char __user **bufptr, const void *src, int size)
  119. {
  120. if (copy_to_user(*bufptr, src, size))
  121. return -EFAULT;
  122. *bufptr += size;
  123. return 0;
  124. }
  125. int snd_seq_expand_var_event(const struct snd_seq_event *event, int count, char *buf,
  126. 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(struct snd_seq_pool *pool,
  154. struct snd_seq_event_cell *cell)
  155. {
  156. cell->next = pool->free;
  157. pool->free = cell;
  158. atomic_dec(&pool->counter);
  159. }
  160. void snd_seq_cell_free(struct snd_seq_event_cell * cell)
  161. {
  162. unsigned long flags;
  163. struct snd_seq_pool *pool;
  164. snd_assert(cell != NULL, return);
  165. pool = cell->pool;
  166. snd_assert(pool != NULL, return);
  167. spin_lock_irqsave(&pool->lock, flags);
  168. free_cell(pool, cell);
  169. if (snd_seq_ev_is_variable(&cell->event)) {
  170. if (cell->event.data.ext.len & SNDRV_SEQ_EXT_CHAINED) {
  171. struct snd_seq_event_cell *curp, *nextptr;
  172. curp = cell->event.data.ext.ptr;
  173. for (; curp; curp = nextptr) {
  174. nextptr = curp->next;
  175. curp->next = pool->free;
  176. free_cell(pool, curp);
  177. }
  178. }
  179. }
  180. if (waitqueue_active(&pool->output_sleep)) {
  181. /* has enough space now? */
  182. if (snd_seq_output_ok(pool))
  183. wake_up(&pool->output_sleep);
  184. }
  185. spin_unlock_irqrestore(&pool->lock, flags);
  186. }
  187. /*
  188. * allocate an event cell.
  189. */
  190. static int snd_seq_cell_alloc(struct snd_seq_pool *pool,
  191. struct snd_seq_event_cell **cellp,
  192. int nonblock, struct file *file)
  193. {
  194. struct snd_seq_event_cell *cell;
  195. unsigned long flags;
  196. int err = -EAGAIN;
  197. wait_queue_t wait;
  198. if (pool == NULL)
  199. return -EINVAL;
  200. *cellp = NULL;
  201. init_waitqueue_entry(&wait, current);
  202. spin_lock_irqsave(&pool->lock, flags);
  203. if (pool->ptr == NULL) { /* not initialized */
  204. snd_printd("seq: pool is not initialized\n");
  205. err = -EINVAL;
  206. goto __error;
  207. }
  208. while (pool->free == NULL && ! nonblock && ! pool->closing) {
  209. set_current_state(TASK_INTERRUPTIBLE);
  210. add_wait_queue(&pool->output_sleep, &wait);
  211. spin_unlock_irq(&pool->lock);
  212. schedule();
  213. spin_lock_irq(&pool->lock);
  214. remove_wait_queue(&pool->output_sleep, &wait);
  215. /* interrupted? */
  216. if (signal_pending(current)) {
  217. err = -ERESTARTSYS;
  218. goto __error;
  219. }
  220. }
  221. if (pool->closing) { /* closing.. */
  222. err = -ENOMEM;
  223. goto __error;
  224. }
  225. cell = pool->free;
  226. if (cell) {
  227. int used;
  228. pool->free = cell->next;
  229. atomic_inc(&pool->counter);
  230. used = atomic_read(&pool->counter);
  231. if (pool->max_used < used)
  232. pool->max_used = used;
  233. pool->event_alloc_success++;
  234. /* clear cell pointers */
  235. cell->next = NULL;
  236. err = 0;
  237. } else
  238. pool->event_alloc_failures++;
  239. *cellp = cell;
  240. __error:
  241. spin_unlock_irqrestore(&pool->lock, flags);
  242. return err;
  243. }
  244. /*
  245. * duplicate the event to a cell.
  246. * if the event has external data, the data is decomposed to additional
  247. * cells.
  248. */
  249. int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event,
  250. struct snd_seq_event_cell **cellp, int nonblock,
  251. struct file *file)
  252. {
  253. int ncells, err;
  254. unsigned int extlen;
  255. struct snd_seq_event_cell *cell;
  256. *cellp = NULL;
  257. ncells = 0;
  258. extlen = 0;
  259. if (snd_seq_ev_is_variable(event)) {
  260. extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
  261. ncells = (extlen + sizeof(struct snd_seq_event) - 1) / sizeof(struct snd_seq_event);
  262. }
  263. if (ncells >= pool->total_elements)
  264. return -ENOMEM;
  265. err = snd_seq_cell_alloc(pool, &cell, nonblock, file);
  266. if (err < 0)
  267. return err;
  268. /* copy the event */
  269. cell->event = *event;
  270. /* decompose */
  271. if (snd_seq_ev_is_variable(event)) {
  272. int len = extlen;
  273. int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED;
  274. int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR;
  275. struct snd_seq_event_cell *src, *tmp, *tail;
  276. char *buf;
  277. cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED;
  278. cell->event.data.ext.ptr = NULL;
  279. src = (struct snd_seq_event_cell *)event->data.ext.ptr;
  280. buf = (char *)event->data.ext.ptr;
  281. tail = NULL;
  282. while (ncells-- > 0) {
  283. int size = sizeof(struct snd_seq_event);
  284. if (len < size)
  285. size = len;
  286. err = snd_seq_cell_alloc(pool, &tmp, nonblock, file);
  287. if (err < 0)
  288. goto __error;
  289. if (cell->event.data.ext.ptr == NULL)
  290. cell->event.data.ext.ptr = tmp;
  291. if (tail)
  292. tail->next = tmp;
  293. tail = tmp;
  294. /* copy chunk */
  295. if (is_chained && src) {
  296. tmp->event = src->event;
  297. src = src->next;
  298. } else if (is_usrptr) {
  299. if (copy_from_user(&tmp->event, (char __user *)buf, size)) {
  300. err = -EFAULT;
  301. goto __error;
  302. }
  303. } else {
  304. memcpy(&tmp->event, buf, size);
  305. }
  306. buf += size;
  307. len -= size;
  308. }
  309. }
  310. *cellp = cell;
  311. return 0;
  312. __error:
  313. snd_seq_cell_free(cell);
  314. return err;
  315. }
  316. /* poll wait */
  317. int snd_seq_pool_poll_wait(struct snd_seq_pool *pool, struct file *file,
  318. poll_table *wait)
  319. {
  320. poll_wait(file, &pool->output_sleep, wait);
  321. return snd_seq_output_ok(pool);
  322. }
  323. /* allocate room specified number of events */
  324. int snd_seq_pool_init(struct snd_seq_pool *pool)
  325. {
  326. int cell;
  327. struct snd_seq_event_cell *cellptr;
  328. unsigned long flags;
  329. snd_assert(pool != NULL, return -EINVAL);
  330. if (pool->ptr) /* should be atomic? */
  331. return 0;
  332. pool->ptr = vmalloc(sizeof(struct snd_seq_event_cell) * pool->size);
  333. if (pool->ptr == NULL) {
  334. snd_printd("seq: malloc for sequencer events failed\n");
  335. return -ENOMEM;
  336. }
  337. /* add new cells to the free cell list */
  338. spin_lock_irqsave(&pool->lock, flags);
  339. pool->free = NULL;
  340. for (cell = 0; cell < pool->size; cell++) {
  341. cellptr = pool->ptr + cell;
  342. cellptr->pool = pool;
  343. cellptr->next = pool->free;
  344. pool->free = cellptr;
  345. }
  346. pool->room = (pool->size + 1) / 2;
  347. /* init statistics */
  348. pool->max_used = 0;
  349. pool->total_elements = pool->size;
  350. spin_unlock_irqrestore(&pool->lock, flags);
  351. return 0;
  352. }
  353. /* remove events */
  354. int snd_seq_pool_done(struct snd_seq_pool *pool)
  355. {
  356. unsigned long flags;
  357. struct snd_seq_event_cell *ptr;
  358. int max_count = 5 * HZ;
  359. snd_assert(pool != NULL, return -EINVAL);
  360. /* wait for closing all threads */
  361. spin_lock_irqsave(&pool->lock, flags);
  362. pool->closing = 1;
  363. spin_unlock_irqrestore(&pool->lock, flags);
  364. if (waitqueue_active(&pool->output_sleep))
  365. wake_up(&pool->output_sleep);
  366. while (atomic_read(&pool->counter) > 0) {
  367. if (max_count == 0) {
  368. snd_printk(KERN_WARNING "snd_seq_pool_done timeout: %d cells remain\n", atomic_read(&pool->counter));
  369. break;
  370. }
  371. schedule_timeout_uninterruptible(1);
  372. max_count--;
  373. }
  374. /* release all resources */
  375. spin_lock_irqsave(&pool->lock, flags);
  376. ptr = pool->ptr;
  377. pool->ptr = NULL;
  378. pool->free = NULL;
  379. pool->total_elements = 0;
  380. spin_unlock_irqrestore(&pool->lock, flags);
  381. vfree(ptr);
  382. spin_lock_irqsave(&pool->lock, flags);
  383. pool->closing = 0;
  384. spin_unlock_irqrestore(&pool->lock, flags);
  385. return 0;
  386. }
  387. /* init new memory pool */
  388. struct snd_seq_pool *snd_seq_pool_new(int poolsize)
  389. {
  390. struct snd_seq_pool *pool;
  391. /* create pool block */
  392. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  393. if (pool == NULL) {
  394. snd_printd("seq: malloc failed for pool\n");
  395. return NULL;
  396. }
  397. spin_lock_init(&pool->lock);
  398. pool->ptr = NULL;
  399. pool->free = NULL;
  400. pool->total_elements = 0;
  401. atomic_set(&pool->counter, 0);
  402. pool->closing = 0;
  403. init_waitqueue_head(&pool->output_sleep);
  404. pool->size = poolsize;
  405. /* init statistics */
  406. pool->max_used = 0;
  407. return pool;
  408. }
  409. /* remove memory pool */
  410. int snd_seq_pool_delete(struct snd_seq_pool **ppool)
  411. {
  412. struct snd_seq_pool *pool = *ppool;
  413. *ppool = NULL;
  414. if (pool == NULL)
  415. return 0;
  416. snd_seq_pool_done(pool);
  417. kfree(pool);
  418. return 0;
  419. }
  420. /* initialize sequencer memory */
  421. int __init snd_sequencer_memory_init(void)
  422. {
  423. return 0;
  424. }
  425. /* release sequencer memory */
  426. void __exit snd_sequencer_memory_done(void)
  427. {
  428. }
  429. /* exported to seq_clientmgr.c */
  430. void snd_seq_info_pool(struct snd_info_buffer *buffer,
  431. struct snd_seq_pool *pool, char *space)
  432. {
  433. if (pool == NULL)
  434. return;
  435. snd_iprintf(buffer, "%sPool size : %d\n", space, pool->total_elements);
  436. snd_iprintf(buffer, "%sCells in use : %d\n", space, atomic_read(&pool->counter));
  437. snd_iprintf(buffer, "%sPeak cells in use : %d\n", space, pool->max_used);
  438. snd_iprintf(buffer, "%sAlloc success : %d\n", space, pool->event_alloc_success);
  439. snd_iprintf(buffer, "%sAlloc failures : %d\n", space, pool->event_alloc_failures);
  440. }