seq_memory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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/export.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 __force __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. }
  94. if (!(event->data.ext.len & SNDRV_SEQ_EXT_CHAINED))
  95. return func(private_data, event->data.ext.ptr, len);
  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 __force __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. if (snd_BUG_ON(!cell))
  167. return;
  168. pool = cell->pool;
  169. if (snd_BUG_ON(!pool))
  170. return;
  171. spin_lock_irqsave(&pool->lock, flags);
  172. free_cell(pool, cell);
  173. if (snd_seq_ev_is_variable(&cell->event)) {
  174. if (cell->event.data.ext.len & SNDRV_SEQ_EXT_CHAINED) {
  175. struct snd_seq_event_cell *curp, *nextptr;
  176. curp = cell->event.data.ext.ptr;
  177. for (; curp; curp = nextptr) {
  178. nextptr = curp->next;
  179. curp->next = pool->free;
  180. free_cell(pool, curp);
  181. }
  182. }
  183. }
  184. if (waitqueue_active(&pool->output_sleep)) {
  185. /* has enough space now? */
  186. if (snd_seq_output_ok(pool))
  187. wake_up(&pool->output_sleep);
  188. }
  189. spin_unlock_irqrestore(&pool->lock, flags);
  190. }
  191. /*
  192. * allocate an event cell.
  193. */
  194. static int snd_seq_cell_alloc(struct snd_seq_pool *pool,
  195. struct snd_seq_event_cell **cellp,
  196. int nonblock, struct file *file)
  197. {
  198. struct snd_seq_event_cell *cell;
  199. unsigned long flags;
  200. int err = -EAGAIN;
  201. wait_queue_t wait;
  202. if (pool == NULL)
  203. return -EINVAL;
  204. *cellp = NULL;
  205. init_waitqueue_entry(&wait, current);
  206. spin_lock_irqsave(&pool->lock, flags);
  207. if (pool->ptr == NULL) { /* not initialized */
  208. pr_debug("ALSA: seq: pool is not initialized\n");
  209. err = -EINVAL;
  210. goto __error;
  211. }
  212. while (pool->free == NULL && ! nonblock && ! pool->closing) {
  213. set_current_state(TASK_INTERRUPTIBLE);
  214. add_wait_queue(&pool->output_sleep, &wait);
  215. spin_unlock_irq(&pool->lock);
  216. schedule();
  217. spin_lock_irq(&pool->lock);
  218. remove_wait_queue(&pool->output_sleep, &wait);
  219. /* interrupted? */
  220. if (signal_pending(current)) {
  221. err = -ERESTARTSYS;
  222. goto __error;
  223. }
  224. }
  225. if (pool->closing) { /* closing.. */
  226. err = -ENOMEM;
  227. goto __error;
  228. }
  229. cell = pool->free;
  230. if (cell) {
  231. int used;
  232. pool->free = cell->next;
  233. atomic_inc(&pool->counter);
  234. used = atomic_read(&pool->counter);
  235. if (pool->max_used < used)
  236. pool->max_used = used;
  237. pool->event_alloc_success++;
  238. /* clear cell pointers */
  239. cell->next = NULL;
  240. err = 0;
  241. } else
  242. pool->event_alloc_failures++;
  243. *cellp = cell;
  244. __error:
  245. spin_unlock_irqrestore(&pool->lock, flags);
  246. return err;
  247. }
  248. /*
  249. * duplicate the event to a cell.
  250. * if the event has external data, the data is decomposed to additional
  251. * cells.
  252. */
  253. int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event,
  254. struct snd_seq_event_cell **cellp, int nonblock,
  255. struct file *file)
  256. {
  257. int ncells, err;
  258. unsigned int extlen;
  259. struct snd_seq_event_cell *cell;
  260. *cellp = NULL;
  261. ncells = 0;
  262. extlen = 0;
  263. if (snd_seq_ev_is_variable(event)) {
  264. extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
  265. ncells = (extlen + sizeof(struct snd_seq_event) - 1) / sizeof(struct snd_seq_event);
  266. }
  267. if (ncells >= pool->total_elements)
  268. return -ENOMEM;
  269. err = snd_seq_cell_alloc(pool, &cell, nonblock, file);
  270. if (err < 0)
  271. return err;
  272. /* copy the event */
  273. cell->event = *event;
  274. /* decompose */
  275. if (snd_seq_ev_is_variable(event)) {
  276. int len = extlen;
  277. int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED;
  278. int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR;
  279. struct snd_seq_event_cell *src, *tmp, *tail;
  280. char *buf;
  281. cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED;
  282. cell->event.data.ext.ptr = NULL;
  283. src = (struct snd_seq_event_cell *)event->data.ext.ptr;
  284. buf = (char *)event->data.ext.ptr;
  285. tail = NULL;
  286. while (ncells-- > 0) {
  287. int size = sizeof(struct snd_seq_event);
  288. if (len < size)
  289. size = len;
  290. err = snd_seq_cell_alloc(pool, &tmp, nonblock, file);
  291. if (err < 0)
  292. goto __error;
  293. if (cell->event.data.ext.ptr == NULL)
  294. cell->event.data.ext.ptr = tmp;
  295. if (tail)
  296. tail->next = tmp;
  297. tail = tmp;
  298. /* copy chunk */
  299. if (is_chained && src) {
  300. tmp->event = src->event;
  301. src = src->next;
  302. } else if (is_usrptr) {
  303. if (copy_from_user(&tmp->event, (char __force __user *)buf, size)) {
  304. err = -EFAULT;
  305. goto __error;
  306. }
  307. } else {
  308. memcpy(&tmp->event, buf, size);
  309. }
  310. buf += size;
  311. len -= size;
  312. }
  313. }
  314. *cellp = cell;
  315. return 0;
  316. __error:
  317. snd_seq_cell_free(cell);
  318. return err;
  319. }
  320. /* poll wait */
  321. int snd_seq_pool_poll_wait(struct snd_seq_pool *pool, struct file *file,
  322. poll_table *wait)
  323. {
  324. poll_wait(file, &pool->output_sleep, wait);
  325. return snd_seq_output_ok(pool);
  326. }
  327. /* allocate room specified number of events */
  328. int snd_seq_pool_init(struct snd_seq_pool *pool)
  329. {
  330. int cell;
  331. struct snd_seq_event_cell *cellptr;
  332. unsigned long flags;
  333. if (snd_BUG_ON(!pool))
  334. return -EINVAL;
  335. if (pool->ptr) /* should be atomic? */
  336. return 0;
  337. pool->ptr = vmalloc(sizeof(struct snd_seq_event_cell) * pool->size);
  338. if (!pool->ptr)
  339. return -ENOMEM;
  340. /* add new cells to the free cell list */
  341. spin_lock_irqsave(&pool->lock, flags);
  342. pool->free = NULL;
  343. for (cell = 0; cell < pool->size; cell++) {
  344. cellptr = pool->ptr + cell;
  345. cellptr->pool = pool;
  346. cellptr->next = pool->free;
  347. pool->free = cellptr;
  348. }
  349. pool->room = (pool->size + 1) / 2;
  350. /* init statistics */
  351. pool->max_used = 0;
  352. pool->total_elements = pool->size;
  353. spin_unlock_irqrestore(&pool->lock, flags);
  354. return 0;
  355. }
  356. /* remove events */
  357. int snd_seq_pool_done(struct snd_seq_pool *pool)
  358. {
  359. unsigned long flags;
  360. struct snd_seq_event_cell *ptr;
  361. int max_count = 5 * HZ;
  362. if (snd_BUG_ON(!pool))
  363. return -EINVAL;
  364. /* wait for closing all threads */
  365. spin_lock_irqsave(&pool->lock, flags);
  366. pool->closing = 1;
  367. spin_unlock_irqrestore(&pool->lock, flags);
  368. if (waitqueue_active(&pool->output_sleep))
  369. wake_up(&pool->output_sleep);
  370. while (atomic_read(&pool->counter) > 0) {
  371. if (max_count == 0) {
  372. pr_warn("ALSA: snd_seq_pool_done timeout: %d cells remain\n", atomic_read(&pool->counter));
  373. break;
  374. }
  375. schedule_timeout_uninterruptible(1);
  376. max_count--;
  377. }
  378. /* release all resources */
  379. spin_lock_irqsave(&pool->lock, flags);
  380. ptr = pool->ptr;
  381. pool->ptr = NULL;
  382. pool->free = NULL;
  383. pool->total_elements = 0;
  384. spin_unlock_irqrestore(&pool->lock, flags);
  385. vfree(ptr);
  386. spin_lock_irqsave(&pool->lock, flags);
  387. pool->closing = 0;
  388. spin_unlock_irqrestore(&pool->lock, flags);
  389. return 0;
  390. }
  391. /* init new memory pool */
  392. struct snd_seq_pool *snd_seq_pool_new(int poolsize)
  393. {
  394. struct snd_seq_pool *pool;
  395. /* create pool block */
  396. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  397. if (!pool)
  398. return NULL;
  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. }