seq_memory.c 12 KB

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