compress_offload.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * compress_core.c - compress offload core
  3. *
  4. * Copyright (C) 2011 Intel Corporation
  5. * Authors: Vinod Koul <vinod.koul@linux.intel.com>
  6. * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. */
  25. #define FORMAT(fmt) "%s: %d: " fmt, __func__, __LINE__
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " FORMAT(fmt)
  27. #include <linux/file.h>
  28. #include <linux/fs.h>
  29. #include <linux/list.h>
  30. #include <linux/math64.h>
  31. #include <linux/mm.h>
  32. #include <linux/mutex.h>
  33. #include <linux/poll.h>
  34. #include <linux/slab.h>
  35. #include <linux/sched.h>
  36. #include <linux/types.h>
  37. #include <linux/uio.h>
  38. #include <linux/uaccess.h>
  39. #include <linux/module.h>
  40. #include <linux/compat.h>
  41. #include <sound/core.h>
  42. #include <sound/initval.h>
  43. #include <sound/info.h>
  44. #include <sound/compress_params.h>
  45. #include <sound/compress_offload.h>
  46. #include <sound/compress_driver.h>
  47. /* TODO:
  48. * - add substream support for multiple devices in case of
  49. * SND_DYNAMIC_MINORS is not used
  50. * - Multiple node representation
  51. * driver should be able to register multiple nodes
  52. */
  53. static DEFINE_MUTEX(device_mutex);
  54. struct snd_compr_file {
  55. unsigned long caps;
  56. struct snd_compr_stream stream;
  57. };
  58. /*
  59. * a note on stream states used:
  60. * we use follwing states in the compressed core
  61. * SNDRV_PCM_STATE_OPEN: When stream has been opened.
  62. * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
  63. * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
  64. * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
  65. * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
  66. * decoding/encoding and rendering/capturing data.
  67. * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
  68. * by calling SNDRV_COMPRESS_DRAIN.
  69. * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
  70. * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
  71. * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
  72. */
  73. static int snd_compr_open(struct inode *inode, struct file *f)
  74. {
  75. struct snd_compr *compr;
  76. struct snd_compr_file *data;
  77. struct snd_compr_runtime *runtime;
  78. enum snd_compr_direction dirn;
  79. int maj = imajor(inode);
  80. int ret;
  81. if ((f->f_flags & O_ACCMODE) == O_WRONLY)
  82. dirn = SND_COMPRESS_PLAYBACK;
  83. else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
  84. dirn = SND_COMPRESS_CAPTURE;
  85. else
  86. return -EINVAL;
  87. if (maj == snd_major)
  88. compr = snd_lookup_minor_data(iminor(inode),
  89. SNDRV_DEVICE_TYPE_COMPRESS);
  90. else
  91. return -EBADFD;
  92. if (compr == NULL) {
  93. pr_err("no device data!!!\n");
  94. return -ENODEV;
  95. }
  96. if (dirn != compr->direction) {
  97. pr_err("this device doesn't support this direction\n");
  98. snd_card_unref(compr->card);
  99. return -EINVAL;
  100. }
  101. data = kzalloc(sizeof(*data), GFP_KERNEL);
  102. if (!data) {
  103. snd_card_unref(compr->card);
  104. return -ENOMEM;
  105. }
  106. data->stream.ops = compr->ops;
  107. data->stream.direction = dirn;
  108. data->stream.private_data = compr->private_data;
  109. data->stream.device = compr;
  110. runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
  111. if (!runtime) {
  112. kfree(data);
  113. snd_card_unref(compr->card);
  114. return -ENOMEM;
  115. }
  116. runtime->state = SNDRV_PCM_STATE_OPEN;
  117. init_waitqueue_head(&runtime->sleep);
  118. data->stream.runtime = runtime;
  119. f->private_data = (void *)data;
  120. mutex_lock(&compr->lock);
  121. ret = compr->ops->open(&data->stream);
  122. mutex_unlock(&compr->lock);
  123. if (ret) {
  124. kfree(runtime);
  125. kfree(data);
  126. }
  127. snd_card_unref(compr->card);
  128. return ret;
  129. }
  130. static int snd_compr_free(struct inode *inode, struct file *f)
  131. {
  132. struct snd_compr_file *data = f->private_data;
  133. struct snd_compr_runtime *runtime = data->stream.runtime;
  134. switch (runtime->state) {
  135. case SNDRV_PCM_STATE_RUNNING:
  136. case SNDRV_PCM_STATE_DRAINING:
  137. case SNDRV_PCM_STATE_PAUSED:
  138. data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
  139. break;
  140. default:
  141. break;
  142. }
  143. data->stream.ops->free(&data->stream);
  144. kfree(data->stream.runtime->buffer);
  145. kfree(data->stream.runtime);
  146. kfree(data);
  147. return 0;
  148. }
  149. static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
  150. struct snd_compr_tstamp *tstamp)
  151. {
  152. if (!stream->ops->pointer)
  153. return -ENOTSUPP;
  154. stream->ops->pointer(stream, tstamp);
  155. pr_debug("dsp consumed till %d total %d bytes\n",
  156. tstamp->byte_offset, tstamp->copied_total);
  157. if (stream->direction == SND_COMPRESS_PLAYBACK)
  158. stream->runtime->total_bytes_transferred = tstamp->copied_total;
  159. else
  160. stream->runtime->total_bytes_available = tstamp->copied_total;
  161. return 0;
  162. }
  163. static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
  164. struct snd_compr_avail *avail)
  165. {
  166. memset(avail, 0, sizeof(*avail));
  167. snd_compr_update_tstamp(stream, &avail->tstamp);
  168. /* Still need to return avail even if tstamp can't be filled in */
  169. if (stream->runtime->total_bytes_available == 0 &&
  170. stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
  171. stream->direction == SND_COMPRESS_PLAYBACK) {
  172. pr_debug("detected init and someone forgot to do a write\n");
  173. return stream->runtime->buffer_size;
  174. }
  175. pr_debug("app wrote %lld, DSP consumed %lld\n",
  176. stream->runtime->total_bytes_available,
  177. stream->runtime->total_bytes_transferred);
  178. if (stream->runtime->total_bytes_available ==
  179. stream->runtime->total_bytes_transferred) {
  180. if (stream->direction == SND_COMPRESS_PLAYBACK) {
  181. pr_debug("both pointers are same, returning full avail\n");
  182. return stream->runtime->buffer_size;
  183. } else {
  184. pr_debug("both pointers are same, returning no avail\n");
  185. return 0;
  186. }
  187. }
  188. avail->avail = stream->runtime->total_bytes_available -
  189. stream->runtime->total_bytes_transferred;
  190. if (stream->direction == SND_COMPRESS_PLAYBACK)
  191. avail->avail = stream->runtime->buffer_size - avail->avail;
  192. pr_debug("ret avail as %lld\n", avail->avail);
  193. return avail->avail;
  194. }
  195. static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
  196. {
  197. struct snd_compr_avail avail;
  198. return snd_compr_calc_avail(stream, &avail);
  199. }
  200. static int
  201. snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
  202. {
  203. struct snd_compr_avail ioctl_avail;
  204. size_t avail;
  205. avail = snd_compr_calc_avail(stream, &ioctl_avail);
  206. ioctl_avail.avail = avail;
  207. if (copy_to_user((__u64 __user *)arg,
  208. &ioctl_avail, sizeof(ioctl_avail)))
  209. return -EFAULT;
  210. return 0;
  211. }
  212. static int snd_compr_write_data(struct snd_compr_stream *stream,
  213. const char __user *buf, size_t count)
  214. {
  215. void *dstn;
  216. size_t copy;
  217. struct snd_compr_runtime *runtime = stream->runtime;
  218. /* 64-bit Modulus */
  219. u64 app_pointer = div64_u64(runtime->total_bytes_available,
  220. runtime->buffer_size);
  221. app_pointer = runtime->total_bytes_available -
  222. (app_pointer * runtime->buffer_size);
  223. dstn = runtime->buffer + app_pointer;
  224. pr_debug("copying %ld at %lld\n",
  225. (unsigned long)count, app_pointer);
  226. if (count < runtime->buffer_size - app_pointer) {
  227. if (copy_from_user(dstn, buf, count))
  228. return -EFAULT;
  229. } else {
  230. copy = runtime->buffer_size - app_pointer;
  231. if (copy_from_user(dstn, buf, copy))
  232. return -EFAULT;
  233. if (copy_from_user(runtime->buffer, buf + copy, count - copy))
  234. return -EFAULT;
  235. }
  236. /* if DSP cares, let it know data has been written */
  237. if (stream->ops->ack)
  238. stream->ops->ack(stream, count);
  239. return count;
  240. }
  241. static ssize_t snd_compr_write(struct file *f, const char __user *buf,
  242. size_t count, loff_t *offset)
  243. {
  244. struct snd_compr_file *data = f->private_data;
  245. struct snd_compr_stream *stream;
  246. size_t avail;
  247. int retval;
  248. if (snd_BUG_ON(!data))
  249. return -EFAULT;
  250. stream = &data->stream;
  251. mutex_lock(&stream->device->lock);
  252. /* write is allowed when stream is running or has been steup */
  253. if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
  254. stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
  255. mutex_unlock(&stream->device->lock);
  256. return -EBADFD;
  257. }
  258. avail = snd_compr_get_avail(stream);
  259. pr_debug("avail returned %ld\n", (unsigned long)avail);
  260. /* calculate how much we can write to buffer */
  261. if (avail > count)
  262. avail = count;
  263. if (stream->ops->copy) {
  264. char __user* cbuf = (char __user*)buf;
  265. retval = stream->ops->copy(stream, cbuf, avail);
  266. } else {
  267. retval = snd_compr_write_data(stream, buf, avail);
  268. }
  269. if (retval > 0)
  270. stream->runtime->total_bytes_available += retval;
  271. /* while initiating the stream, write should be called before START
  272. * call, so in setup move state */
  273. if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
  274. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  275. pr_debug("stream prepared, Houston we are good to go\n");
  276. }
  277. mutex_unlock(&stream->device->lock);
  278. return retval;
  279. }
  280. static ssize_t snd_compr_read(struct file *f, char __user *buf,
  281. size_t count, loff_t *offset)
  282. {
  283. struct snd_compr_file *data = f->private_data;
  284. struct snd_compr_stream *stream;
  285. size_t avail;
  286. int retval;
  287. if (snd_BUG_ON(!data))
  288. return -EFAULT;
  289. stream = &data->stream;
  290. mutex_lock(&stream->device->lock);
  291. /* read is allowed when stream is running, paused, draining and setup
  292. * (yes setup is state which we transition to after stop, so if user
  293. * wants to read data after stop we allow that)
  294. */
  295. switch (stream->runtime->state) {
  296. case SNDRV_PCM_STATE_OPEN:
  297. case SNDRV_PCM_STATE_PREPARED:
  298. case SNDRV_PCM_STATE_XRUN:
  299. case SNDRV_PCM_STATE_SUSPENDED:
  300. case SNDRV_PCM_STATE_DISCONNECTED:
  301. retval = -EBADFD;
  302. goto out;
  303. }
  304. avail = snd_compr_get_avail(stream);
  305. pr_debug("avail returned %ld\n", (unsigned long)avail);
  306. /* calculate how much we can read from buffer */
  307. if (avail > count)
  308. avail = count;
  309. if (stream->ops->copy) {
  310. retval = stream->ops->copy(stream, buf, avail);
  311. } else {
  312. retval = -ENXIO;
  313. goto out;
  314. }
  315. if (retval > 0)
  316. stream->runtime->total_bytes_transferred += retval;
  317. out:
  318. mutex_unlock(&stream->device->lock);
  319. return retval;
  320. }
  321. static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
  322. {
  323. return -ENXIO;
  324. }
  325. static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
  326. {
  327. if (stream->direction == SND_COMPRESS_PLAYBACK)
  328. return POLLOUT | POLLWRNORM;
  329. else
  330. return POLLIN | POLLRDNORM;
  331. }
  332. static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
  333. {
  334. struct snd_compr_file *data = f->private_data;
  335. struct snd_compr_stream *stream;
  336. size_t avail;
  337. int retval = 0;
  338. if (snd_BUG_ON(!data))
  339. return -EFAULT;
  340. stream = &data->stream;
  341. if (snd_BUG_ON(!stream))
  342. return -EFAULT;
  343. mutex_lock(&stream->device->lock);
  344. if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  345. retval = -EBADFD;
  346. goto out;
  347. }
  348. poll_wait(f, &stream->runtime->sleep, wait);
  349. avail = snd_compr_get_avail(stream);
  350. pr_debug("avail is %ld\n", (unsigned long)avail);
  351. /* check if we have at least one fragment to fill */
  352. switch (stream->runtime->state) {
  353. case SNDRV_PCM_STATE_DRAINING:
  354. /* stream has been woken up after drain is complete
  355. * draining done so set stream state to stopped
  356. */
  357. retval = snd_compr_get_poll(stream);
  358. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  359. break;
  360. case SNDRV_PCM_STATE_RUNNING:
  361. case SNDRV_PCM_STATE_PREPARED:
  362. case SNDRV_PCM_STATE_PAUSED:
  363. if (avail >= stream->runtime->fragment_size)
  364. retval = snd_compr_get_poll(stream);
  365. break;
  366. default:
  367. if (stream->direction == SND_COMPRESS_PLAYBACK)
  368. retval = POLLOUT | POLLWRNORM | POLLERR;
  369. else
  370. retval = POLLIN | POLLRDNORM | POLLERR;
  371. break;
  372. }
  373. out:
  374. mutex_unlock(&stream->device->lock);
  375. return retval;
  376. }
  377. static int
  378. snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
  379. {
  380. int retval;
  381. struct snd_compr_caps caps;
  382. if (!stream->ops->get_caps)
  383. return -ENXIO;
  384. memset(&caps, 0, sizeof(caps));
  385. retval = stream->ops->get_caps(stream, &caps);
  386. if (retval)
  387. goto out;
  388. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  389. retval = -EFAULT;
  390. out:
  391. return retval;
  392. }
  393. static int
  394. snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
  395. {
  396. int retval;
  397. struct snd_compr_codec_caps *caps;
  398. if (!stream->ops->get_codec_caps)
  399. return -ENXIO;
  400. caps = kzalloc(sizeof(*caps), GFP_KERNEL);
  401. if (!caps)
  402. return -ENOMEM;
  403. retval = stream->ops->get_codec_caps(stream, caps);
  404. if (retval)
  405. goto out;
  406. if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
  407. retval = -EFAULT;
  408. out:
  409. kfree(caps);
  410. return retval;
  411. }
  412. /* revisit this with snd_pcm_preallocate_xxx */
  413. static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
  414. struct snd_compr_params *params)
  415. {
  416. unsigned int buffer_size;
  417. void *buffer;
  418. buffer_size = params->buffer.fragment_size * params->buffer.fragments;
  419. if (stream->ops->copy) {
  420. buffer = NULL;
  421. /* if copy is defined the driver will be required to copy
  422. * the data from core
  423. */
  424. } else {
  425. buffer = kmalloc(buffer_size, GFP_KERNEL);
  426. if (!buffer)
  427. return -ENOMEM;
  428. }
  429. stream->runtime->fragment_size = params->buffer.fragment_size;
  430. stream->runtime->fragments = params->buffer.fragments;
  431. stream->runtime->buffer = buffer;
  432. stream->runtime->buffer_size = buffer_size;
  433. return 0;
  434. }
  435. static int snd_compress_check_input(struct snd_compr_params *params)
  436. {
  437. /* first let's check the buffer parameter's */
  438. if (params->buffer.fragment_size == 0 ||
  439. params->buffer.fragments > INT_MAX / params->buffer.fragment_size)
  440. return -EINVAL;
  441. /* now codec parameters */
  442. if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
  443. return -EINVAL;
  444. if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
  445. return -EINVAL;
  446. return 0;
  447. }
  448. static int
  449. snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
  450. {
  451. struct snd_compr_params *params;
  452. int retval;
  453. if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  454. /*
  455. * we should allow parameter change only when stream has been
  456. * opened not in other cases
  457. */
  458. params = kmalloc(sizeof(*params), GFP_KERNEL);
  459. if (!params)
  460. return -ENOMEM;
  461. if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
  462. retval = -EFAULT;
  463. goto out;
  464. }
  465. retval = snd_compress_check_input(params);
  466. if (retval)
  467. goto out;
  468. retval = snd_compr_allocate_buffer(stream, params);
  469. if (retval) {
  470. retval = -ENOMEM;
  471. goto out;
  472. }
  473. retval = stream->ops->set_params(stream, params);
  474. if (retval)
  475. goto out;
  476. stream->metadata_set = false;
  477. stream->next_track = false;
  478. if (stream->direction == SND_COMPRESS_PLAYBACK)
  479. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  480. else
  481. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  482. } else {
  483. return -EPERM;
  484. }
  485. out:
  486. kfree(params);
  487. return retval;
  488. }
  489. static int
  490. snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
  491. {
  492. struct snd_codec *params;
  493. int retval;
  494. if (!stream->ops->get_params)
  495. return -EBADFD;
  496. params = kzalloc(sizeof(*params), GFP_KERNEL);
  497. if (!params)
  498. return -ENOMEM;
  499. retval = stream->ops->get_params(stream, params);
  500. if (retval)
  501. goto out;
  502. if (copy_to_user((char __user *)arg, params, sizeof(*params)))
  503. retval = -EFAULT;
  504. out:
  505. kfree(params);
  506. return retval;
  507. }
  508. static int
  509. snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
  510. {
  511. struct snd_compr_metadata metadata;
  512. int retval;
  513. if (!stream->ops->get_metadata)
  514. return -ENXIO;
  515. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  516. return -EFAULT;
  517. retval = stream->ops->get_metadata(stream, &metadata);
  518. if (retval != 0)
  519. return retval;
  520. if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
  521. return -EFAULT;
  522. return 0;
  523. }
  524. static int
  525. snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
  526. {
  527. struct snd_compr_metadata metadata;
  528. int retval;
  529. if (!stream->ops->set_metadata)
  530. return -ENXIO;
  531. /*
  532. * we should allow parameter change only when stream has been
  533. * opened not in other cases
  534. */
  535. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  536. return -EFAULT;
  537. retval = stream->ops->set_metadata(stream, &metadata);
  538. stream->metadata_set = true;
  539. return retval;
  540. }
  541. static inline int
  542. snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
  543. {
  544. struct snd_compr_tstamp tstamp = {0};
  545. int ret;
  546. ret = snd_compr_update_tstamp(stream, &tstamp);
  547. if (ret == 0)
  548. ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
  549. &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
  550. return ret;
  551. }
  552. static int snd_compr_pause(struct snd_compr_stream *stream)
  553. {
  554. int retval;
  555. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  556. return -EPERM;
  557. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  558. if (!retval)
  559. stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
  560. return retval;
  561. }
  562. static int snd_compr_resume(struct snd_compr_stream *stream)
  563. {
  564. int retval;
  565. if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
  566. return -EPERM;
  567. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  568. if (!retval)
  569. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  570. return retval;
  571. }
  572. static int snd_compr_start(struct snd_compr_stream *stream)
  573. {
  574. int retval;
  575. if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
  576. return -EPERM;
  577. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
  578. if (!retval)
  579. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  580. return retval;
  581. }
  582. static int snd_compr_stop(struct snd_compr_stream *stream)
  583. {
  584. int retval;
  585. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  586. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  587. return -EPERM;
  588. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
  589. if (!retval) {
  590. snd_compr_drain_notify(stream);
  591. stream->runtime->total_bytes_available = 0;
  592. stream->runtime->total_bytes_transferred = 0;
  593. }
  594. return retval;
  595. }
  596. static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
  597. {
  598. int ret;
  599. /*
  600. * We are called with lock held. So drop the lock while we wait for
  601. * drain complete notfication from the driver
  602. *
  603. * It is expected that driver will notify the drain completion and then
  604. * stream will be moved to SETUP state, even if draining resulted in an
  605. * error. We can trigger next track after this.
  606. */
  607. stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
  608. mutex_unlock(&stream->device->lock);
  609. /* we wait for drain to complete here, drain can return when
  610. * interruption occurred, wait returned error or success.
  611. * For the first two cases we don't do anything different here and
  612. * return after waking up
  613. */
  614. ret = wait_event_interruptible(stream->runtime->sleep,
  615. (stream->runtime->state != SNDRV_PCM_STATE_DRAINING));
  616. if (ret == -ERESTARTSYS)
  617. pr_debug("wait aborted by a signal");
  618. else if (ret)
  619. pr_debug("wait for drain failed with %d\n", ret);
  620. wake_up(&stream->runtime->sleep);
  621. mutex_lock(&stream->device->lock);
  622. return ret;
  623. }
  624. static int snd_compr_drain(struct snd_compr_stream *stream)
  625. {
  626. int retval;
  627. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  628. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  629. return -EPERM;
  630. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
  631. if (retval) {
  632. pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
  633. wake_up(&stream->runtime->sleep);
  634. return retval;
  635. }
  636. return snd_compress_wait_for_drain(stream);
  637. }
  638. static int snd_compr_next_track(struct snd_compr_stream *stream)
  639. {
  640. int retval;
  641. /* only a running stream can transition to next track */
  642. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  643. return -EPERM;
  644. /* you can signal next track isf this is intended to be a gapless stream
  645. * and current track metadata is set
  646. */
  647. if (stream->metadata_set == false)
  648. return -EPERM;
  649. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
  650. if (retval != 0)
  651. return retval;
  652. stream->metadata_set = false;
  653. stream->next_track = true;
  654. return 0;
  655. }
  656. static int snd_compr_partial_drain(struct snd_compr_stream *stream)
  657. {
  658. int retval;
  659. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  660. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  661. return -EPERM;
  662. /* stream can be drained only when next track has been signalled */
  663. if (stream->next_track == false)
  664. return -EPERM;
  665. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
  666. if (retval) {
  667. pr_debug("Partial drain returned failure\n");
  668. wake_up(&stream->runtime->sleep);
  669. return retval;
  670. }
  671. stream->next_track = false;
  672. return snd_compress_wait_for_drain(stream);
  673. }
  674. static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  675. {
  676. struct snd_compr_file *data = f->private_data;
  677. struct snd_compr_stream *stream;
  678. int retval = -ENOTTY;
  679. if (snd_BUG_ON(!data))
  680. return -EFAULT;
  681. stream = &data->stream;
  682. if (snd_BUG_ON(!stream))
  683. return -EFAULT;
  684. mutex_lock(&stream->device->lock);
  685. switch (_IOC_NR(cmd)) {
  686. case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
  687. retval = put_user(SNDRV_COMPRESS_VERSION,
  688. (int __user *)arg) ? -EFAULT : 0;
  689. break;
  690. case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
  691. retval = snd_compr_get_caps(stream, arg);
  692. break;
  693. case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
  694. retval = snd_compr_get_codec_caps(stream, arg);
  695. break;
  696. case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
  697. retval = snd_compr_set_params(stream, arg);
  698. break;
  699. case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
  700. retval = snd_compr_get_params(stream, arg);
  701. break;
  702. case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
  703. retval = snd_compr_set_metadata(stream, arg);
  704. break;
  705. case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
  706. retval = snd_compr_get_metadata(stream, arg);
  707. break;
  708. case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
  709. retval = snd_compr_tstamp(stream, arg);
  710. break;
  711. case _IOC_NR(SNDRV_COMPRESS_AVAIL):
  712. retval = snd_compr_ioctl_avail(stream, arg);
  713. break;
  714. case _IOC_NR(SNDRV_COMPRESS_PAUSE):
  715. retval = snd_compr_pause(stream);
  716. break;
  717. case _IOC_NR(SNDRV_COMPRESS_RESUME):
  718. retval = snd_compr_resume(stream);
  719. break;
  720. case _IOC_NR(SNDRV_COMPRESS_START):
  721. retval = snd_compr_start(stream);
  722. break;
  723. case _IOC_NR(SNDRV_COMPRESS_STOP):
  724. retval = snd_compr_stop(stream);
  725. break;
  726. case _IOC_NR(SNDRV_COMPRESS_DRAIN):
  727. retval = snd_compr_drain(stream);
  728. break;
  729. case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
  730. retval = snd_compr_partial_drain(stream);
  731. break;
  732. case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
  733. retval = snd_compr_next_track(stream);
  734. break;
  735. }
  736. mutex_unlock(&stream->device->lock);
  737. return retval;
  738. }
  739. /* support of 32bit userspace on 64bit platforms */
  740. #ifdef CONFIG_COMPAT
  741. static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
  742. unsigned long arg)
  743. {
  744. return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  745. }
  746. #endif
  747. static const struct file_operations snd_compr_file_ops = {
  748. .owner = THIS_MODULE,
  749. .open = snd_compr_open,
  750. .release = snd_compr_free,
  751. .write = snd_compr_write,
  752. .read = snd_compr_read,
  753. .unlocked_ioctl = snd_compr_ioctl,
  754. #ifdef CONFIG_COMPAT
  755. .compat_ioctl = snd_compr_ioctl_compat,
  756. #endif
  757. .mmap = snd_compr_mmap,
  758. .poll = snd_compr_poll,
  759. };
  760. static int snd_compress_dev_register(struct snd_device *device)
  761. {
  762. int ret = -EINVAL;
  763. char str[16];
  764. struct snd_compr *compr;
  765. if (snd_BUG_ON(!device || !device->device_data))
  766. return -EBADFD;
  767. compr = device->device_data;
  768. pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
  769. compr->direction);
  770. /* register compressed device */
  771. ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
  772. compr->card, compr->device,
  773. &snd_compr_file_ops, compr, &compr->dev);
  774. if (ret < 0) {
  775. pr_err("snd_register_device failed\n %d", ret);
  776. return ret;
  777. }
  778. return ret;
  779. }
  780. static int snd_compress_dev_disconnect(struct snd_device *device)
  781. {
  782. struct snd_compr *compr;
  783. compr = device->device_data;
  784. snd_unregister_device(&compr->dev);
  785. return 0;
  786. }
  787. #ifdef CONFIG_SND_VERBOSE_PROCFS
  788. static void snd_compress_proc_info_read(struct snd_info_entry *entry,
  789. struct snd_info_buffer *buffer)
  790. {
  791. struct snd_compr *compr = (struct snd_compr *)entry->private_data;
  792. snd_iprintf(buffer, "card: %d\n", compr->card->number);
  793. snd_iprintf(buffer, "device: %d\n", compr->device);
  794. snd_iprintf(buffer, "stream: %s\n",
  795. compr->direction == SND_COMPRESS_PLAYBACK
  796. ? "PLAYBACK" : "CAPTURE");
  797. snd_iprintf(buffer, "id: %s\n", compr->id);
  798. }
  799. static int snd_compress_proc_init(struct snd_compr *compr)
  800. {
  801. struct snd_info_entry *entry;
  802. char name[16];
  803. sprintf(name, "compr%i", compr->device);
  804. entry = snd_info_create_card_entry(compr->card, name,
  805. compr->card->proc_root);
  806. if (!entry)
  807. return -ENOMEM;
  808. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  809. if (snd_info_register(entry) < 0) {
  810. snd_info_free_entry(entry);
  811. return -ENOMEM;
  812. }
  813. compr->proc_root = entry;
  814. entry = snd_info_create_card_entry(compr->card, "info",
  815. compr->proc_root);
  816. if (entry) {
  817. snd_info_set_text_ops(entry, compr,
  818. snd_compress_proc_info_read);
  819. if (snd_info_register(entry) < 0) {
  820. snd_info_free_entry(entry);
  821. entry = NULL;
  822. }
  823. }
  824. compr->proc_info_entry = entry;
  825. return 0;
  826. }
  827. static void snd_compress_proc_done(struct snd_compr *compr)
  828. {
  829. snd_info_free_entry(compr->proc_info_entry);
  830. compr->proc_info_entry = NULL;
  831. snd_info_free_entry(compr->proc_root);
  832. compr->proc_root = NULL;
  833. }
  834. static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
  835. {
  836. strlcpy(compr->id, id, sizeof(compr->id));
  837. }
  838. #else
  839. static inline int snd_compress_proc_init(struct snd_compr *compr)
  840. {
  841. return 0;
  842. }
  843. static inline void snd_compress_proc_done(struct snd_compr *compr)
  844. {
  845. }
  846. static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
  847. {
  848. }
  849. #endif
  850. static int snd_compress_dev_free(struct snd_device *device)
  851. {
  852. struct snd_compr *compr;
  853. compr = device->device_data;
  854. snd_compress_proc_done(compr);
  855. put_device(&compr->dev);
  856. return 0;
  857. }
  858. /*
  859. * snd_compress_new: create new compress device
  860. * @card: sound card pointer
  861. * @device: device number
  862. * @dirn: device direction, should be of type enum snd_compr_direction
  863. * @compr: compress device pointer
  864. */
  865. int snd_compress_new(struct snd_card *card, int device,
  866. int dirn, const char *id, struct snd_compr *compr)
  867. {
  868. static struct snd_device_ops ops = {
  869. .dev_free = snd_compress_dev_free,
  870. .dev_register = snd_compress_dev_register,
  871. .dev_disconnect = snd_compress_dev_disconnect,
  872. };
  873. int ret;
  874. compr->card = card;
  875. compr->device = device;
  876. compr->direction = dirn;
  877. snd_compress_set_id(compr, id);
  878. snd_device_initialize(&compr->dev, card);
  879. dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
  880. ret = snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
  881. if (ret == 0)
  882. snd_compress_proc_init(compr);
  883. return ret;
  884. }
  885. EXPORT_SYMBOL_GPL(snd_compress_new);
  886. static int snd_compress_add_device(struct snd_compr *device)
  887. {
  888. int ret;
  889. if (!device->card)
  890. return -EINVAL;
  891. /* register the card */
  892. ret = snd_card_register(device->card);
  893. if (ret)
  894. goto out;
  895. return 0;
  896. out:
  897. pr_err("failed with %d\n", ret);
  898. return ret;
  899. }
  900. static int snd_compress_remove_device(struct snd_compr *device)
  901. {
  902. return snd_card_free(device->card);
  903. }
  904. /**
  905. * snd_compress_register - register compressed device
  906. *
  907. * @device: compressed device to register
  908. */
  909. int snd_compress_register(struct snd_compr *device)
  910. {
  911. int retval;
  912. if (device->name == NULL || device->ops == NULL)
  913. return -EINVAL;
  914. pr_debug("Registering compressed device %s\n", device->name);
  915. if (snd_BUG_ON(!device->ops->open))
  916. return -EINVAL;
  917. if (snd_BUG_ON(!device->ops->free))
  918. return -EINVAL;
  919. if (snd_BUG_ON(!device->ops->set_params))
  920. return -EINVAL;
  921. if (snd_BUG_ON(!device->ops->trigger))
  922. return -EINVAL;
  923. mutex_init(&device->lock);
  924. /* register a compressed card */
  925. mutex_lock(&device_mutex);
  926. retval = snd_compress_add_device(device);
  927. mutex_unlock(&device_mutex);
  928. return retval;
  929. }
  930. EXPORT_SYMBOL_GPL(snd_compress_register);
  931. int snd_compress_deregister(struct snd_compr *device)
  932. {
  933. pr_debug("Removing compressed device %s\n", device->name);
  934. mutex_lock(&device_mutex);
  935. snd_compress_remove_device(device);
  936. mutex_unlock(&device_mutex);
  937. return 0;
  938. }
  939. EXPORT_SYMBOL_GPL(snd_compress_deregister);
  940. static int __init snd_compress_init(void)
  941. {
  942. return 0;
  943. }
  944. static void __exit snd_compress_exit(void)
  945. {
  946. }
  947. module_init(snd_compress_init);
  948. module_exit(snd_compress_exit);
  949. MODULE_DESCRIPTION("ALSA Compressed offload framework");
  950. MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
  951. MODULE_LICENSE("GPL v2");