compress_offload.c 30 KB

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