seq_memory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. EXPORT_SYMBOL(snd_seq_dump_var_event);
  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 struct snd_seq_event *event, int count, char *buf,
  127. int in_kernel, int size_aligned)
  128. {
  129. int len, newlen;
  130. int err;
  131. if ((len = get_var_len(event)) < 0)
  132. return len;
  133. newlen = len;
  134. if (size_aligned > 0)
  135. newlen = roundup(len, size_aligned);
  136. if (count < newlen)
  137. return -EAGAIN;
  138. if (event->data.ext.len & SNDRV_SEQ_EXT_USRPTR) {
  139. if (! in_kernel)
  140. return -EINVAL;
  141. if (copy_from_user(buf, (void __user *)event->data.ext.ptr, len))
  142. return -EFAULT;
  143. return newlen;
  144. }
  145. err = snd_seq_dump_var_event(event,
  146. in_kernel ? (snd_seq_dump_func_t)seq_copy_in_kernel :
  147. (snd_seq_dump_func_t)seq_copy_in_user,
  148. &buf);
  149. return err < 0 ? err : newlen;
  150. }
  151. EXPORT_SYMBOL(snd_seq_expand_var_event);
  152. /*
  153. * release this cell, free extended data if available
  154. */
  155. static inline void free_cell(struct snd_seq_pool *pool,
  156. struct snd_seq_event_cell *cell)
  157. {
  158. cell->next = pool->free;
  159. pool->free = cell;
  160. atomic_dec(&pool->counter);
  161. }
  162. void snd_seq_cell_free(struct snd_seq_event_cell * cell)
  163. {
  164. unsigned long flags;
  165. struct snd_seq_pool *pool;
  166. snd_assert(cell != NULL, return);
  167. pool = cell->pool;
  168. snd_assert(pool != NULL, return);
  169. spin_lock_irqsave(&pool->lock, flags);
  170. free_cell(pool, cell);
  171. if (snd_seq_ev_is_variable(&cell->event)) {
  172. if (cell->event.data.ext.len & SNDRV_SEQ_EXT_CHAINED) {
  173. struct snd_seq_event_cell *curp, *nextptr;
  174. curp = cell->event.data.ext.ptr;
  175. for (; curp; curp = nextptr) {
  176. nextptr = curp->next;
  177. curp->next = pool->free;
  178. free_cell(pool, curp);
  179. }
  180. }
  181. }
  182. if (waitqueue_active(&pool->output_sleep)) {
  183. /* has enough space now? */
  184. if (snd_seq_output_ok(pool))
  185. wake_up(&pool->output_sleep);
  186. }
  187. spin_unlock_irqrestore(&pool->lock, flags);
  188. }
  189. /*
  190. * allocate an event cell.
  191. */
  192. static int snd_seq_cell_alloc(struct snd_seq_pool *pool,
  193. struct snd_seq_event_cell **cellp,
  194. int nonblock, struct file *file)
  195. {
  196. struct snd_seq_event_cell *cell;
  197. unsigned long flags;
  198. int err = -EAGAIN;
  199. wait_queue_t wait;
  200. if (pool == NULL)
  201. return -EINVAL;
  202. *cellp = NULL;
  203. init_waitqueue_entry(&wait, current);
  204. spin_lock_irqsave(&pool->lock, flags);
  205. if (pool->ptr == NULL) { /* not initialized */
  206. snd_printd("seq: pool is not initialized\n");
  207. err = -EINVAL;
  208. goto __error;
  209. }
  210. while (pool->free == NULL && ! nonblock && ! pool->closing) {
  211. set_current_state(TASK_INTERRUPTIBLE);
  212. add_wait_queue(&pool->output_sleep, &wait);
  213. spin_unlock_irq(&pool->lock);
  214. schedule();
  215. spin_lock_irq(&pool->lock);
  216. remove_wait_queue(&pool->output_sleep, &wait);
  217. /* interrupted? */
  218. if (signal_pending(current)) {
  219. err = -ERESTARTSYS;
  220. goto __error;
  221. }
  222. }
  223. if (pool->closing) { /* closing.. */
  224. err = -ENOMEM;
  225. goto __error;
  226. }
  227. cell = pool->free;
  228. if (cell) {
  229. int used;
  230. pool->free = cell->next;
  231. atomic_inc(&pool->counter);
  232. used = atomic_read(&pool->counter);
  233. if (pool->max_used < used)
  234. pool->max_used = used;
  235. pool->event_alloc_success++;
  236. /* clear cell pointers */
  237. cell->next = NULL;
  238. err = 0;
  239. } else
  240. pool->event_alloc_failures++;
  241. *cellp = cell;
  242. __error:
  243. spin_unlock_irqrestore(&pool->lock, flags);
  244. return err;
  245. }
  246. /*
  247. * duplicate the event to a cell.
  248. * if the event has external data, the data is decomposed to additional
  249. * cells.
  250. */
  251. int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event,
  252. struct snd_seq_event_cell **cellp, int nonblock,
  253. struct file *file)
  254. {
  255. int ncells, err;
  256. unsigned int extlen;
  257. struct snd_seq_event_cell *cell;
  258. *cellp = NULL;
  259. ncells = 0;
  260. extlen = 0;
  261. if (snd_seq_ev_is_variable(event)) {
  262. extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
  263. ncells = (extlen + sizeof(struct snd_seq_event) - 1) / sizeof(struct snd_seq_event);
  264. }
  265. if (ncells >= pool->total_elements)
  266. return -ENOMEM;
  267. err = snd_seq_cell_alloc(pool, &cell, nonblock, file);
  268. if (err < 0)
  269. return err;
  270. /* copy the event */
  271. cell->event = *event;
  272. /* decompose */
  273. if (snd_seq_ev_is_variable(event)) {
  274. int len = extlen;
  275. int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED;
  276. int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR;
  277. struct snd_seq_event_cell *src, *tmp, *tail;
  278. char *buf;
  279. cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED;
  280. cell->event.data.ext.ptr = NULL;
  281. src = (struct snd_seq_event_cell *)event->data.ext.ptr;
  282. buf = (char *)event->data.ext.ptr;
  283. tail = NULL;
  284. while (ncells-- > 0) {
  285. int size = sizeof(struct snd_seq_event);
  286. if (len < size)
  287. size = len;
  288. err = snd_seq_cell_alloc(pool, &tmp, nonblock, file);
  289. if (err < 0)
  290. goto __error;
  291. if (cell->event.data.ext.ptr == NULL)
  292. cell->event.data.ext.ptr = tmp;
  293. if (tail)
  294. tail->next = tmp;
  295. tail = tmp;
  296. /* copy chunk */
  297. if (is_chained && src) {
  298. tmp->event = src->event;
  299. src = src->next;
  300. } else if (is_usrptr) {
  301. if (copy_from_user(&tmp->event, (char __user *)buf, size)) {
  302. err = -EFAULT;
  303. goto __error;
  304. }
  305. } else {
  306. memcpy(&tmp->event, buf, size);
  307. }
  308. buf += size;
  309. len -= size;
  310. }
  311. }
  312. *cellp = cell;
  313. return 0;
  314. __error:
  315. snd_seq_cell_free(cell);
  316. return err;
  317. }
  318. /* poll wait */
  319. int snd_seq_pool_poll_wait(struct snd_seq_pool *pool, struct file *file,
  320. poll_table *wait)
  321. {
  322. poll_wait(file, &pool->output_sleep, wait);
  323. return snd_seq_output_ok(pool);
  324. }
  325. /* allocate room specified number of events */
  326. int snd_seq_pool_init(struct snd_seq_pool *pool)
  327. {
  328. int cell;
  329. struct snd_seq_event_cell *cellptr;
  330. unsigned long flags;
  331. snd_assert(pool != NULL, return -EINVAL);
  332. if (pool->ptr) /* should be atomic? */
  333. return 0;
  334. pool->ptr = vmalloc(sizeof(struct snd_seq_event_cell) * pool->size);
  335. if (pool->ptr == NULL) {
  336. snd_printd("seq: malloc for sequencer events failed\n");
  337. return -ENOMEM;
  338. }
  339. /* add new cells to the free cell list */
  340. spin_lock_irqsave(&pool->lock, flags);
  341. pool->free = NULL;
  342. for (cell = 0; cell < pool->size; cell++) {
  343. cellptr = pool->ptr + cell;
  344. cellptr->pool = pool;
  345. cellptr->next = pool->free;
  346. pool->free = cellptr;
  347. }
  348. pool->room = (pool->size + 1) / 2;
  349. /* init statistics */
  350. pool->max_used = 0;
  351. pool->total_elements = pool->size;
  352. spin_unlock_irqrestore(&pool->lock, flags);
  353. return 0;
  354. }
  355. /* remove events */
  356. int snd_seq_pool_done(struct snd_seq_pool *pool)
  357. {
  358. unsigned long flags;
  359. struct snd_seq_event_cell *ptr;
  360. int max_count = 5 * HZ;
  361. snd_assert(pool != NULL, return -EINVAL);
  362. /* wait for closing all threads */
  363. spin_lock_irqsave(&pool->lock, flags);
  364. pool->closing = 1;
  365. spin_unlock_irqrestore(&pool->lock, flags);
  366. if (waitqueue_active(&pool->output_sleep))
  367. wake_up(&pool->output_sleep);
  368. while (atomic_read(&pool->counter) > 0) {
  369. if (max_count == 0) {
  370. snd_printk(KERN_WARNING "snd_seq_pool_done timeout: %d cells remain\n", atomic_read(&pool->counter));
  371. break;
  372. }
  373. schedule_timeout_uninterruptible(1);
  374. max_count--;
  375. }
  376. /* release all resources */
  377. spin_lock_irqsave(&pool->lock, flags);
  378. ptr = pool->ptr;
  379. pool->ptr = NULL;
  380. pool->free = NULL;
  381. pool->total_elements = 0;
  382. spin_unlock_irqrestore(&pool->lock, flags);
  383. vfree(ptr);
  384. spin_lock_irqsave(&pool->lock, flags);
  385. pool->closing = 0;
  386. spin_unlock_irqrestore(&pool->lock, flags);
  387. return 0;
  388. }
  389. /* init new memory pool */
  390. struct snd_seq_pool *snd_seq_pool_new(int poolsize)
  391. {
  392. struct snd_seq_pool *pool;
  393. /* create pool block */
  394. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  395. if (pool == NULL) {
  396. snd_printd("seq: malloc failed for pool\n");
  397. return NULL;
  398. }
  399. spin_lock_init(&pool->lock);
  400. pool->ptr = NULL;
  401. pool->free = NULL;
  402. pool->total_elements = 0;
  403. atomic_set(&pool->counter, 0);
  404. pool->closing = 0;
  405. init_waitqueue_head(&pool->output_sleep);
  406. pool->size = poolsize;
  407. /* init statistics */
  408. pool->max_used = 0;
  409. return pool;
  410. }
  411. /* remove memory pool */
  412. int snd_seq_pool_delete(struct snd_seq_pool **ppool)
  413. {
  414. struct snd_seq_pool *pool = *ppool;
  415. *ppool = NULL;
  416. if (pool == NULL)
  417. return 0;
  418. snd_seq_pool_done(pool);
  419. kfree(pool);
  420. return 0;
  421. }
  422. /* initialize sequencer memory */
  423. int __init snd_sequencer_memory_init(void)
  424. {
  425. return 0;
  426. }
  427. /* release sequencer memory */
  428. void __exit snd_sequencer_memory_done(void)
  429. {
  430. }
  431. /* exported to seq_clientmgr.c */
  432. void snd_seq_info_pool(struct snd_info_buffer *buffer,
  433. struct snd_seq_pool *pool, char *space)
  434. {
  435. if (pool == NULL)
  436. return;
  437. snd_iprintf(buffer, "%sPool size : %d\n", space, pool->total_elements);
  438. snd_iprintf(buffer, "%sCells in use : %d\n", space, atomic_read(&pool->counter));
  439. snd_iprintf(buffer, "%sPeak cells in use : %d\n", space, pool->max_used);
  440. snd_iprintf(buffer, "%sAlloc success : %d\n", space, pool->event_alloc_success);
  441. snd_iprintf(buffer, "%sAlloc failures : %d\n", space, pool->event_alloc_failures);
  442. }