seq_memory.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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. struct mutex *mutexp)
  198. {
  199. struct snd_seq_event_cell *cell;
  200. unsigned long flags;
  201. int err = -EAGAIN;
  202. wait_queue_t wait;
  203. if (pool == NULL)
  204. return -EINVAL;
  205. *cellp = NULL;
  206. init_waitqueue_entry(&wait, current);
  207. spin_lock_irqsave(&pool->lock, flags);
  208. if (pool->ptr == NULL) { /* not initialized */
  209. pr_debug("ALSA: seq: pool is not initialized\n");
  210. err = -EINVAL;
  211. goto __error;
  212. }
  213. while (pool->free == NULL && ! nonblock && ! pool->closing) {
  214. set_current_state(TASK_INTERRUPTIBLE);
  215. add_wait_queue(&pool->output_sleep, &wait);
  216. spin_unlock_irq(&pool->lock);
  217. if (mutexp)
  218. mutex_unlock(mutexp);
  219. schedule();
  220. if (mutexp)
  221. mutex_lock(mutexp);
  222. spin_lock_irq(&pool->lock);
  223. remove_wait_queue(&pool->output_sleep, &wait);
  224. /* interrupted? */
  225. if (signal_pending(current)) {
  226. err = -ERESTARTSYS;
  227. goto __error;
  228. }
  229. }
  230. if (pool->closing) { /* closing.. */
  231. err = -ENOMEM;
  232. goto __error;
  233. }
  234. cell = pool->free;
  235. if (cell) {
  236. int used;
  237. pool->free = cell->next;
  238. atomic_inc(&pool->counter);
  239. used = atomic_read(&pool->counter);
  240. if (pool->max_used < used)
  241. pool->max_used = used;
  242. pool->event_alloc_success++;
  243. /* clear cell pointers */
  244. cell->next = NULL;
  245. err = 0;
  246. } else
  247. pool->event_alloc_failures++;
  248. *cellp = cell;
  249. __error:
  250. spin_unlock_irqrestore(&pool->lock, flags);
  251. return err;
  252. }
  253. /*
  254. * duplicate the event to a cell.
  255. * if the event has external data, the data is decomposed to additional
  256. * cells.
  257. */
  258. int snd_seq_event_dup(struct snd_seq_pool *pool, struct snd_seq_event *event,
  259. struct snd_seq_event_cell **cellp, int nonblock,
  260. struct file *file, struct mutex *mutexp)
  261. {
  262. int ncells, err;
  263. unsigned int extlen;
  264. struct snd_seq_event_cell *cell;
  265. *cellp = NULL;
  266. ncells = 0;
  267. extlen = 0;
  268. if (snd_seq_ev_is_variable(event)) {
  269. extlen = event->data.ext.len & ~SNDRV_SEQ_EXT_MASK;
  270. ncells = (extlen + sizeof(struct snd_seq_event) - 1) / sizeof(struct snd_seq_event);
  271. }
  272. if (ncells >= pool->total_elements)
  273. return -ENOMEM;
  274. err = snd_seq_cell_alloc(pool, &cell, nonblock, file, mutexp);
  275. if (err < 0)
  276. return err;
  277. /* copy the event */
  278. cell->event = *event;
  279. /* decompose */
  280. if (snd_seq_ev_is_variable(event)) {
  281. int len = extlen;
  282. int is_chained = event->data.ext.len & SNDRV_SEQ_EXT_CHAINED;
  283. int is_usrptr = event->data.ext.len & SNDRV_SEQ_EXT_USRPTR;
  284. struct snd_seq_event_cell *src, *tmp, *tail;
  285. char *buf;
  286. cell->event.data.ext.len = extlen | SNDRV_SEQ_EXT_CHAINED;
  287. cell->event.data.ext.ptr = NULL;
  288. src = (struct snd_seq_event_cell *)event->data.ext.ptr;
  289. buf = (char *)event->data.ext.ptr;
  290. tail = NULL;
  291. while (ncells-- > 0) {
  292. int size = sizeof(struct snd_seq_event);
  293. if (len < size)
  294. size = len;
  295. err = snd_seq_cell_alloc(pool, &tmp, nonblock, file,
  296. mutexp);
  297. if (err < 0)
  298. goto __error;
  299. if (cell->event.data.ext.ptr == NULL)
  300. cell->event.data.ext.ptr = tmp;
  301. if (tail)
  302. tail->next = tmp;
  303. tail = tmp;
  304. /* copy chunk */
  305. if (is_chained && src) {
  306. tmp->event = src->event;
  307. src = src->next;
  308. } else if (is_usrptr) {
  309. if (copy_from_user(&tmp->event, (char __force __user *)buf, size)) {
  310. err = -EFAULT;
  311. goto __error;
  312. }
  313. } else {
  314. memcpy(&tmp->event, buf, size);
  315. }
  316. buf += size;
  317. len -= size;
  318. }
  319. }
  320. *cellp = cell;
  321. return 0;
  322. __error:
  323. snd_seq_cell_free(cell);
  324. return err;
  325. }
  326. /* poll wait */
  327. int snd_seq_pool_poll_wait(struct snd_seq_pool *pool, struct file *file,
  328. poll_table *wait)
  329. {
  330. poll_wait(file, &pool->output_sleep, wait);
  331. return snd_seq_output_ok(pool);
  332. }
  333. /* allocate room specified number of events */
  334. int snd_seq_pool_init(struct snd_seq_pool *pool)
  335. {
  336. int cell;
  337. struct snd_seq_event_cell *cellptr;
  338. unsigned long flags;
  339. if (snd_BUG_ON(!pool))
  340. return -EINVAL;
  341. cellptr = vmalloc(sizeof(struct snd_seq_event_cell) * pool->size);
  342. if (!cellptr)
  343. return -ENOMEM;
  344. /* add new cells to the free cell list */
  345. spin_lock_irqsave(&pool->lock, flags);
  346. if (pool->ptr) {
  347. spin_unlock_irqrestore(&pool->lock, flags);
  348. vfree(cellptr);
  349. return 0;
  350. }
  351. pool->ptr = cellptr;
  352. pool->free = NULL;
  353. for (cell = 0; cell < pool->size; cell++) {
  354. cellptr = pool->ptr + cell;
  355. cellptr->pool = pool;
  356. cellptr->next = pool->free;
  357. pool->free = cellptr;
  358. }
  359. pool->room = (pool->size + 1) / 2;
  360. /* init statistics */
  361. pool->max_used = 0;
  362. pool->total_elements = pool->size;
  363. spin_unlock_irqrestore(&pool->lock, flags);
  364. return 0;
  365. }
  366. /* refuse the further insertion to the pool */
  367. void snd_seq_pool_mark_closing(struct snd_seq_pool *pool)
  368. {
  369. unsigned long flags;
  370. if (snd_BUG_ON(!pool))
  371. return;
  372. spin_lock_irqsave(&pool->lock, flags);
  373. pool->closing = 1;
  374. spin_unlock_irqrestore(&pool->lock, flags);
  375. }
  376. /* remove events */
  377. int snd_seq_pool_done(struct snd_seq_pool *pool)
  378. {
  379. unsigned long flags;
  380. struct snd_seq_event_cell *ptr;
  381. if (snd_BUG_ON(!pool))
  382. return -EINVAL;
  383. /* wait for closing all threads */
  384. if (waitqueue_active(&pool->output_sleep))
  385. wake_up(&pool->output_sleep);
  386. while (atomic_read(&pool->counter) > 0)
  387. schedule_timeout_uninterruptible(1);
  388. /* release all resources */
  389. spin_lock_irqsave(&pool->lock, flags);
  390. ptr = pool->ptr;
  391. pool->ptr = NULL;
  392. pool->free = NULL;
  393. pool->total_elements = 0;
  394. spin_unlock_irqrestore(&pool->lock, flags);
  395. vfree(ptr);
  396. spin_lock_irqsave(&pool->lock, flags);
  397. pool->closing = 0;
  398. spin_unlock_irqrestore(&pool->lock, flags);
  399. return 0;
  400. }
  401. /* init new memory pool */
  402. struct snd_seq_pool *snd_seq_pool_new(int poolsize)
  403. {
  404. struct snd_seq_pool *pool;
  405. /* create pool block */
  406. pool = kzalloc(sizeof(*pool), GFP_KERNEL);
  407. if (!pool)
  408. return NULL;
  409. spin_lock_init(&pool->lock);
  410. pool->ptr = NULL;
  411. pool->free = NULL;
  412. pool->total_elements = 0;
  413. atomic_set(&pool->counter, 0);
  414. pool->closing = 0;
  415. init_waitqueue_head(&pool->output_sleep);
  416. pool->size = poolsize;
  417. /* init statistics */
  418. pool->max_used = 0;
  419. return pool;
  420. }
  421. /* remove memory pool */
  422. int snd_seq_pool_delete(struct snd_seq_pool **ppool)
  423. {
  424. struct snd_seq_pool *pool = *ppool;
  425. *ppool = NULL;
  426. if (pool == NULL)
  427. return 0;
  428. snd_seq_pool_mark_closing(pool);
  429. snd_seq_pool_done(pool);
  430. kfree(pool);
  431. return 0;
  432. }
  433. /* initialize sequencer memory */
  434. int __init snd_sequencer_memory_init(void)
  435. {
  436. return 0;
  437. }
  438. /* release sequencer memory */
  439. void __exit snd_sequencer_memory_done(void)
  440. {
  441. }
  442. /* exported to seq_clientmgr.c */
  443. void snd_seq_info_pool(struct snd_info_buffer *buffer,
  444. struct snd_seq_pool *pool, char *space)
  445. {
  446. if (pool == NULL)
  447. return;
  448. snd_iprintf(buffer, "%sPool size : %d\n", space, pool->total_elements);
  449. snd_iprintf(buffer, "%sCells in use : %d\n", space, atomic_read(&pool->counter));
  450. snd_iprintf(buffer, "%sPeak cells in use : %d\n", space, pool->max_used);
  451. snd_iprintf(buffer, "%sAlloc success : %d\n", space, pool->event_alloc_success);
  452. snd_iprintf(buffer, "%sAlloc failures : %d\n", space, pool->event_alloc_failures);
  453. }