compress_offload.c 30 KB

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