compress_offload.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 <sound/core.h>
  41. #include <sound/initval.h>
  42. #include <sound/compress_params.h>
  43. #include <sound/compress_offload.h>
  44. #include <sound/compress_driver.h>
  45. /* TODO:
  46. * - add substream support for multiple devices in case of
  47. * SND_DYNAMIC_MINORS is not used
  48. * - Multiple node representation
  49. * driver should be able to register multiple nodes
  50. */
  51. static DEFINE_MUTEX(device_mutex);
  52. struct snd_compr_file {
  53. unsigned long caps;
  54. struct snd_compr_stream stream;
  55. };
  56. /*
  57. * a note on stream states used:
  58. * we use follwing states in the compressed core
  59. * SNDRV_PCM_STATE_OPEN: When stream has been opened.
  60. * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
  61. * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
  62. * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
  63. * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
  64. * decoding/encoding and rendering/capturing data.
  65. * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
  66. * by calling SNDRV_COMPRESS_DRAIN.
  67. * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
  68. * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
  69. * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
  70. */
  71. static int snd_compr_open(struct inode *inode, struct file *f)
  72. {
  73. struct snd_compr *compr;
  74. struct snd_compr_file *data;
  75. struct snd_compr_runtime *runtime;
  76. enum snd_compr_direction dirn;
  77. int maj = imajor(inode);
  78. int ret;
  79. if ((f->f_flags & O_ACCMODE) == O_WRONLY)
  80. dirn = SND_COMPRESS_PLAYBACK;
  81. else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
  82. dirn = SND_COMPRESS_CAPTURE;
  83. else
  84. return -EINVAL;
  85. if (maj == snd_major)
  86. compr = snd_lookup_minor_data(iminor(inode),
  87. SNDRV_DEVICE_TYPE_COMPRESS);
  88. else
  89. return -EBADFD;
  90. if (compr == NULL) {
  91. pr_err("no device data!!!\n");
  92. return -ENODEV;
  93. }
  94. if (dirn != compr->direction) {
  95. pr_err("this device doesn't support this direction\n");
  96. snd_card_unref(compr->card);
  97. return -EINVAL;
  98. }
  99. data = kzalloc(sizeof(*data), GFP_KERNEL);
  100. if (!data) {
  101. snd_card_unref(compr->card);
  102. return -ENOMEM;
  103. }
  104. data->stream.ops = compr->ops;
  105. data->stream.direction = dirn;
  106. data->stream.private_data = compr->private_data;
  107. data->stream.device = compr;
  108. runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
  109. if (!runtime) {
  110. kfree(data);
  111. snd_card_unref(compr->card);
  112. return -ENOMEM;
  113. }
  114. runtime->state = SNDRV_PCM_STATE_OPEN;
  115. init_waitqueue_head(&runtime->sleep);
  116. init_waitqueue_head(&runtime->wait);
  117. data->stream.runtime = runtime;
  118. f->private_data = (void *)data;
  119. mutex_lock(&compr->lock);
  120. ret = compr->ops->open(&data->stream);
  121. mutex_unlock(&compr->lock);
  122. if (ret) {
  123. kfree(runtime);
  124. kfree(data);
  125. }
  126. snd_card_unref(compr->card);
  127. return 0;
  128. }
  129. static int snd_compr_free(struct inode *inode, struct file *f)
  130. {
  131. struct snd_compr_file *data = f->private_data;
  132. struct snd_compr_runtime *runtime = data->stream.runtime;
  133. switch (runtime->state) {
  134. case SNDRV_PCM_STATE_RUNNING:
  135. case SNDRV_PCM_STATE_DRAINING:
  136. case SNDRV_PCM_STATE_PAUSED:
  137. data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
  138. break;
  139. default:
  140. break;
  141. }
  142. data->stream.ops->free(&data->stream);
  143. kfree(data->stream.runtime->buffer);
  144. kfree(data->stream.runtime);
  145. kfree(data);
  146. return 0;
  147. }
  148. static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
  149. struct snd_compr_tstamp *tstamp)
  150. {
  151. if (!stream->ops->pointer)
  152. return -ENOTSUPP;
  153. stream->ops->pointer(stream, tstamp);
  154. pr_debug("dsp consumed till %d total %d bytes\n",
  155. tstamp->byte_offset, tstamp->copied_total);
  156. if (stream->direction == SND_COMPRESS_PLAYBACK)
  157. stream->runtime->total_bytes_transferred = tstamp->copied_total;
  158. else
  159. stream->runtime->total_bytes_available = tstamp->copied_total;
  160. return 0;
  161. }
  162. static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
  163. struct snd_compr_avail *avail)
  164. {
  165. memset(avail, 0, sizeof(*avail));
  166. snd_compr_update_tstamp(stream, &avail->tstamp);
  167. /* Still need to return avail even if tstamp can't be filled in */
  168. if (stream->runtime->total_bytes_available == 0 &&
  169. stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
  170. stream->direction == SND_COMPRESS_PLAYBACK) {
  171. pr_debug("detected init and someone forgot to do a write\n");
  172. return stream->runtime->buffer_size;
  173. }
  174. pr_debug("app wrote %lld, DSP consumed %lld\n",
  175. stream->runtime->total_bytes_available,
  176. stream->runtime->total_bytes_transferred);
  177. if (stream->runtime->total_bytes_available ==
  178. stream->runtime->total_bytes_transferred) {
  179. if (stream->direction == SND_COMPRESS_PLAYBACK) {
  180. pr_debug("both pointers are same, returning full avail\n");
  181. return stream->runtime->buffer_size;
  182. } else {
  183. pr_debug("both pointers are same, returning no avail\n");
  184. return 0;
  185. }
  186. }
  187. avail->avail = stream->runtime->total_bytes_available -
  188. stream->runtime->total_bytes_transferred;
  189. if (stream->direction == SND_COMPRESS_PLAYBACK)
  190. avail->avail = stream->runtime->buffer_size - avail->avail;
  191. pr_debug("ret avail as %lld\n", avail->avail);
  192. return avail->avail;
  193. }
  194. static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
  195. {
  196. struct snd_compr_avail avail;
  197. return snd_compr_calc_avail(stream, &avail);
  198. }
  199. static int
  200. snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
  201. {
  202. struct snd_compr_avail ioctl_avail;
  203. size_t avail;
  204. avail = snd_compr_calc_avail(stream, &ioctl_avail);
  205. ioctl_avail.avail = avail;
  206. if (copy_to_user((__u64 __user *)arg,
  207. &ioctl_avail, sizeof(ioctl_avail)))
  208. return -EFAULT;
  209. return 0;
  210. }
  211. static int snd_compr_write_data(struct snd_compr_stream *stream,
  212. const char __user *buf, size_t count)
  213. {
  214. void *dstn;
  215. size_t copy;
  216. struct snd_compr_runtime *runtime = stream->runtime;
  217. /* 64-bit Modulus */
  218. u64 app_pointer = div64_u64(runtime->total_bytes_available,
  219. runtime->buffer_size);
  220. app_pointer = runtime->total_bytes_available -
  221. (app_pointer * runtime->buffer_size);
  222. dstn = runtime->buffer + app_pointer;
  223. pr_debug("copying %ld at %lld\n",
  224. (unsigned long)count, app_pointer);
  225. if (count < runtime->buffer_size - app_pointer) {
  226. if (copy_from_user(dstn, buf, count))
  227. return -EFAULT;
  228. } else {
  229. copy = runtime->buffer_size - app_pointer;
  230. if (copy_from_user(dstn, buf, copy))
  231. return -EFAULT;
  232. if (copy_from_user(runtime->buffer, buf + copy, count - copy))
  233. return -EFAULT;
  234. }
  235. /* if DSP cares, let it know data has been written */
  236. if (stream->ops->ack)
  237. stream->ops->ack(stream, count);
  238. return count;
  239. }
  240. static ssize_t snd_compr_write(struct file *f, const char __user *buf,
  241. size_t count, loff_t *offset)
  242. {
  243. struct snd_compr_file *data = f->private_data;
  244. struct snd_compr_stream *stream;
  245. size_t avail;
  246. int retval;
  247. if (snd_BUG_ON(!data))
  248. return -EFAULT;
  249. stream = &data->stream;
  250. mutex_lock(&stream->device->lock);
  251. /* write is allowed when stream is running or has been steup */
  252. if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
  253. stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
  254. mutex_unlock(&stream->device->lock);
  255. return -EBADFD;
  256. }
  257. avail = snd_compr_get_avail(stream);
  258. pr_debug("avail returned %ld\n", (unsigned long)avail);
  259. /* calculate how much we can write to buffer */
  260. if (avail > count)
  261. avail = count;
  262. if (stream->ops->copy) {
  263. char __user* cbuf = (char __user*)buf;
  264. retval = stream->ops->copy(stream, cbuf, avail);
  265. } else {
  266. retval = snd_compr_write_data(stream, buf, avail);
  267. }
  268. if (retval > 0)
  269. stream->runtime->total_bytes_available += retval;
  270. /* while initiating the stream, write should be called before START
  271. * call, so in setup move state */
  272. if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
  273. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  274. pr_debug("stream prepared, Houston we are good to go\n");
  275. }
  276. mutex_unlock(&stream->device->lock);
  277. return retval;
  278. }
  279. static ssize_t snd_compr_read(struct file *f, char __user *buf,
  280. size_t count, loff_t *offset)
  281. {
  282. struct snd_compr_file *data = f->private_data;
  283. struct snd_compr_stream *stream;
  284. size_t avail;
  285. int retval;
  286. if (snd_BUG_ON(!data))
  287. return -EFAULT;
  288. stream = &data->stream;
  289. mutex_lock(&stream->device->lock);
  290. /* read is allowed when stream is running, paused, draining and setup
  291. * (yes setup is state which we transition to after stop, so if user
  292. * wants to read data after stop we allow that)
  293. */
  294. switch (stream->runtime->state) {
  295. case SNDRV_PCM_STATE_OPEN:
  296. case SNDRV_PCM_STATE_PREPARED:
  297. case SNDRV_PCM_STATE_XRUN:
  298. case SNDRV_PCM_STATE_SUSPENDED:
  299. case SNDRV_PCM_STATE_DISCONNECTED:
  300. retval = -EBADFD;
  301. goto out;
  302. }
  303. avail = snd_compr_get_avail(stream);
  304. pr_debug("avail returned %ld\n", (unsigned long)avail);
  305. /* calculate how much we can read from buffer */
  306. if (avail > count)
  307. avail = count;
  308. if (stream->ops->copy) {
  309. retval = stream->ops->copy(stream, buf, avail);
  310. } else {
  311. retval = -ENXIO;
  312. goto out;
  313. }
  314. if (retval > 0)
  315. stream->runtime->total_bytes_transferred += retval;
  316. out:
  317. mutex_unlock(&stream->device->lock);
  318. return retval;
  319. }
  320. static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
  321. {
  322. return -ENXIO;
  323. }
  324. static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
  325. {
  326. if (stream->direction == SND_COMPRESS_PLAYBACK)
  327. return POLLOUT | POLLWRNORM;
  328. else
  329. return POLLIN | POLLRDNORM;
  330. }
  331. static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
  332. {
  333. struct snd_compr_file *data = f->private_data;
  334. struct snd_compr_stream *stream;
  335. size_t avail;
  336. int retval = 0;
  337. if (snd_BUG_ON(!data))
  338. return -EFAULT;
  339. stream = &data->stream;
  340. if (snd_BUG_ON(!stream))
  341. return -EFAULT;
  342. mutex_lock(&stream->device->lock);
  343. if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
  344. 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 > SIZE_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. if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000))
  447. return -EINVAL;
  448. return 0;
  449. }
  450. static int
  451. snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
  452. {
  453. struct snd_compr_params *params;
  454. int retval;
  455. if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  456. /*
  457. * we should allow parameter change only when stream has been
  458. * opened not in other cases
  459. */
  460. params = kmalloc(sizeof(*params), GFP_KERNEL);
  461. if (!params)
  462. return -ENOMEM;
  463. if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
  464. retval = -EFAULT;
  465. goto out;
  466. }
  467. retval = snd_compress_check_input(params);
  468. if (retval)
  469. goto out;
  470. retval = snd_compr_allocate_buffer(stream, params);
  471. if (retval) {
  472. retval = -ENOMEM;
  473. goto out;
  474. }
  475. retval = stream->ops->set_params(stream, params);
  476. if (retval)
  477. goto out;
  478. stream->metadata_set = false;
  479. stream->next_track = false;
  480. if (stream->direction == SND_COMPRESS_PLAYBACK)
  481. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  482. else
  483. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  484. } else {
  485. return -EPERM;
  486. }
  487. out:
  488. kfree(params);
  489. return retval;
  490. }
  491. static int
  492. snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
  493. {
  494. struct snd_codec *params;
  495. int retval;
  496. if (!stream->ops->get_params)
  497. return -EBADFD;
  498. params = kzalloc(sizeof(*params), GFP_KERNEL);
  499. if (!params)
  500. return -ENOMEM;
  501. retval = stream->ops->get_params(stream, params);
  502. if (retval)
  503. goto out;
  504. if (copy_to_user((char __user *)arg, params, sizeof(*params)))
  505. retval = -EFAULT;
  506. out:
  507. kfree(params);
  508. return retval;
  509. }
  510. static int
  511. snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
  512. {
  513. struct snd_compr_metadata metadata;
  514. int retval;
  515. if (!stream->ops->get_metadata)
  516. return -ENXIO;
  517. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  518. return -EFAULT;
  519. retval = stream->ops->get_metadata(stream, &metadata);
  520. if (retval != 0)
  521. return retval;
  522. if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
  523. return -EFAULT;
  524. return 0;
  525. }
  526. static int
  527. snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
  528. {
  529. struct snd_compr_metadata metadata;
  530. int retval;
  531. if (!stream->ops->set_metadata)
  532. return -ENXIO;
  533. /*
  534. * we should allow parameter change only when stream has been
  535. * opened not in other cases
  536. */
  537. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  538. return -EFAULT;
  539. retval = stream->ops->set_metadata(stream, &metadata);
  540. stream->metadata_set = true;
  541. return retval;
  542. }
  543. static inline int
  544. snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
  545. {
  546. struct snd_compr_tstamp tstamp = {0};
  547. int ret;
  548. ret = snd_compr_update_tstamp(stream, &tstamp);
  549. if (ret == 0)
  550. ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
  551. &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
  552. return ret;
  553. }
  554. static int snd_compr_pause(struct snd_compr_stream *stream)
  555. {
  556. int retval;
  557. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  558. return -EPERM;
  559. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  560. if (!retval)
  561. stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
  562. return retval;
  563. }
  564. static int snd_compr_resume(struct snd_compr_stream *stream)
  565. {
  566. int retval;
  567. if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
  568. return -EPERM;
  569. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  570. if (!retval)
  571. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  572. return retval;
  573. }
  574. static int snd_compr_start(struct snd_compr_stream *stream)
  575. {
  576. int retval;
  577. if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
  578. return -EPERM;
  579. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
  580. if (!retval)
  581. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  582. return retval;
  583. }
  584. static int snd_compr_stop(struct snd_compr_stream *stream)
  585. {
  586. int retval;
  587. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  588. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  589. return -EPERM;
  590. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
  591. if (!retval) {
  592. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  593. wake_up(&stream->runtime->sleep);
  594. snd_compr_drain_notify(stream);
  595. stream->runtime->total_bytes_available = 0;
  596. stream->runtime->total_bytes_transferred = 0;
  597. }
  598. return retval;
  599. }
  600. static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
  601. {
  602. /*
  603. * We are called with lock held. So drop the lock while we wait for
  604. * drain complete notfication from the driver
  605. *
  606. * It is expected that driver will notify the drain completion and then
  607. * stream will be moved to SETUP state, even if draining resulted in an
  608. * error. We can trigger next track after this.
  609. */
  610. stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
  611. mutex_unlock(&stream->device->lock);
  612. wait_event(stream->runtime->wait, stream->runtime->drain_wake);
  613. wake_up(&stream->runtime->sleep);
  614. mutex_lock(&stream->device->lock);
  615. return 0;
  616. }
  617. static int snd_compr_drain(struct snd_compr_stream *stream)
  618. {
  619. int retval;
  620. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  621. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  622. return -EPERM;
  623. stream->runtime->drain_wake = 0;
  624. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
  625. if (retval) {
  626. pr_err("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
  627. wake_up(&stream->runtime->sleep);
  628. return retval;
  629. }
  630. retval = snd_compress_wait_for_drain(stream);
  631. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  632. return retval;
  633. }
  634. static int snd_compr_next_track(struct snd_compr_stream *stream)
  635. {
  636. int retval;
  637. /* only a running stream can transition to next track */
  638. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  639. return -EPERM;
  640. /* you can signal next track isf this is intended to be a gapless stream
  641. * and current track metadata is set
  642. */
  643. if (stream->metadata_set == false)
  644. return -EPERM;
  645. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
  646. if (retval != 0)
  647. return retval;
  648. stream->metadata_set = false;
  649. stream->next_track = true;
  650. return 0;
  651. }
  652. static int snd_compr_partial_drain(struct snd_compr_stream *stream)
  653. {
  654. int retval;
  655. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  656. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  657. return -EPERM;
  658. /* stream can be drained only when next track has been signalled */
  659. if (stream->next_track == false)
  660. return -EPERM;
  661. stream->runtime->drain_wake = 0;
  662. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
  663. if (retval) {
  664. pr_err("Partial drain returned failure\n");
  665. wake_up(&stream->runtime->sleep);
  666. return retval;
  667. }
  668. stream->next_track = false;
  669. return snd_compress_wait_for_drain(stream);
  670. }
  671. static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  672. {
  673. struct snd_compr_file *data = f->private_data;
  674. struct snd_compr_stream *stream;
  675. int retval = -ENOTTY;
  676. if (snd_BUG_ON(!data))
  677. return -EFAULT;
  678. stream = &data->stream;
  679. if (snd_BUG_ON(!stream))
  680. return -EFAULT;
  681. mutex_lock(&stream->device->lock);
  682. switch (_IOC_NR(cmd)) {
  683. case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
  684. retval = put_user(SNDRV_COMPRESS_VERSION,
  685. (int __user *)arg) ? -EFAULT : 0;
  686. break;
  687. case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
  688. retval = snd_compr_get_caps(stream, arg);
  689. break;
  690. case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
  691. retval = snd_compr_get_codec_caps(stream, arg);
  692. break;
  693. case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
  694. retval = snd_compr_set_params(stream, arg);
  695. break;
  696. case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
  697. retval = snd_compr_get_params(stream, arg);
  698. break;
  699. case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
  700. retval = snd_compr_set_metadata(stream, arg);
  701. break;
  702. case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
  703. retval = snd_compr_get_metadata(stream, arg);
  704. break;
  705. case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
  706. retval = snd_compr_tstamp(stream, arg);
  707. break;
  708. case _IOC_NR(SNDRV_COMPRESS_AVAIL):
  709. retval = snd_compr_ioctl_avail(stream, arg);
  710. break;
  711. case _IOC_NR(SNDRV_COMPRESS_PAUSE):
  712. retval = snd_compr_pause(stream);
  713. break;
  714. case _IOC_NR(SNDRV_COMPRESS_RESUME):
  715. retval = snd_compr_resume(stream);
  716. break;
  717. case _IOC_NR(SNDRV_COMPRESS_START):
  718. retval = snd_compr_start(stream);
  719. break;
  720. case _IOC_NR(SNDRV_COMPRESS_STOP):
  721. retval = snd_compr_stop(stream);
  722. break;
  723. case _IOC_NR(SNDRV_COMPRESS_DRAIN):
  724. retval = snd_compr_drain(stream);
  725. break;
  726. case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
  727. retval = snd_compr_partial_drain(stream);
  728. break;
  729. case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
  730. retval = snd_compr_next_track(stream);
  731. break;
  732. }
  733. mutex_unlock(&stream->device->lock);
  734. return retval;
  735. }
  736. static const struct file_operations snd_compr_file_ops = {
  737. .owner = THIS_MODULE,
  738. .open = snd_compr_open,
  739. .release = snd_compr_free,
  740. .write = snd_compr_write,
  741. .read = snd_compr_read,
  742. .unlocked_ioctl = snd_compr_ioctl,
  743. .mmap = snd_compr_mmap,
  744. .poll = snd_compr_poll,
  745. };
  746. static int snd_compress_dev_register(struct snd_device *device)
  747. {
  748. int ret = -EINVAL;
  749. char str[16];
  750. struct snd_compr *compr;
  751. if (snd_BUG_ON(!device || !device->device_data))
  752. return -EBADFD;
  753. compr = device->device_data;
  754. sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
  755. pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
  756. compr->direction);
  757. /* register compressed device */
  758. ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
  759. compr->device, &snd_compr_file_ops, compr, str);
  760. if (ret < 0) {
  761. pr_err("snd_register_device failed\n %d", ret);
  762. return ret;
  763. }
  764. return ret;
  765. }
  766. static int snd_compress_dev_disconnect(struct snd_device *device)
  767. {
  768. struct snd_compr *compr;
  769. compr = device->device_data;
  770. snd_unregister_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
  771. compr->device);
  772. return 0;
  773. }
  774. /*
  775. * snd_compress_new: create new compress device
  776. * @card: sound card pointer
  777. * @device: device number
  778. * @dirn: device direction, should be of type enum snd_compr_direction
  779. * @compr: compress device pointer
  780. */
  781. int snd_compress_new(struct snd_card *card, int device,
  782. int dirn, struct snd_compr *compr)
  783. {
  784. static struct snd_device_ops ops = {
  785. .dev_free = NULL,
  786. .dev_register = snd_compress_dev_register,
  787. .dev_disconnect = snd_compress_dev_disconnect,
  788. };
  789. compr->card = card;
  790. compr->device = device;
  791. compr->direction = dirn;
  792. return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
  793. }
  794. EXPORT_SYMBOL_GPL(snd_compress_new);
  795. static int snd_compress_add_device(struct snd_compr *device)
  796. {
  797. int ret;
  798. if (!device->card)
  799. return -EINVAL;
  800. /* register the card */
  801. ret = snd_card_register(device->card);
  802. if (ret)
  803. goto out;
  804. return 0;
  805. out:
  806. pr_err("failed with %d\n", ret);
  807. return ret;
  808. }
  809. static int snd_compress_remove_device(struct snd_compr *device)
  810. {
  811. return snd_card_free(device->card);
  812. }
  813. /**
  814. * snd_compress_register - register compressed device
  815. *
  816. * @device: compressed device to register
  817. */
  818. int snd_compress_register(struct snd_compr *device)
  819. {
  820. int retval;
  821. if (device->name == NULL || device->dev == NULL || device->ops == NULL)
  822. return -EINVAL;
  823. pr_debug("Registering compressed device %s\n", device->name);
  824. if (snd_BUG_ON(!device->ops->open))
  825. return -EINVAL;
  826. if (snd_BUG_ON(!device->ops->free))
  827. return -EINVAL;
  828. if (snd_BUG_ON(!device->ops->set_params))
  829. return -EINVAL;
  830. if (snd_BUG_ON(!device->ops->trigger))
  831. return -EINVAL;
  832. mutex_init(&device->lock);
  833. /* register a compressed card */
  834. mutex_lock(&device_mutex);
  835. retval = snd_compress_add_device(device);
  836. mutex_unlock(&device_mutex);
  837. return retval;
  838. }
  839. EXPORT_SYMBOL_GPL(snd_compress_register);
  840. int snd_compress_deregister(struct snd_compr *device)
  841. {
  842. pr_debug("Removing compressed device %s\n", device->name);
  843. mutex_lock(&device_mutex);
  844. snd_compress_remove_device(device);
  845. mutex_unlock(&device_mutex);
  846. return 0;
  847. }
  848. EXPORT_SYMBOL_GPL(snd_compress_deregister);
  849. static int __init snd_compress_init(void)
  850. {
  851. return 0;
  852. }
  853. static void __exit snd_compress_exit(void)
  854. {
  855. }
  856. module_init(snd_compress_init);
  857. module_exit(snd_compress_exit);
  858. MODULE_DESCRIPTION("ALSA Compressed offload framework");
  859. MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
  860. MODULE_LICENSE("GPL v2");