compress_offload.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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/mm.h>
  31. #include <linux/mutex.h>
  32. #include <linux/poll.h>
  33. #include <linux/slab.h>
  34. #include <linux/sched.h>
  35. #include <linux/uio.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/module.h>
  38. #include <sound/core.h>
  39. #include <sound/initval.h>
  40. #include <sound/compress_params.h>
  41. #include <sound/compress_offload.h>
  42. #include <sound/compress_driver.h>
  43. /* TODO:
  44. * - add substream support for multiple devices in case of
  45. * SND_DYNAMIC_MINORS is not used
  46. * - Multiple node representation
  47. * driver should be able to register multiple nodes
  48. */
  49. static DEFINE_MUTEX(device_mutex);
  50. struct snd_compr_file {
  51. unsigned long caps;
  52. struct snd_compr_stream stream;
  53. };
  54. /*
  55. * a note on stream states used:
  56. * we use follwing states in the compressed core
  57. * SNDRV_PCM_STATE_OPEN: When stream has been opened.
  58. * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
  59. * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
  60. * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
  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. data->stream.ops = compr->ops;
  103. data->stream.direction = dirn;
  104. data->stream.private_data = compr->private_data;
  105. data->stream.device = compr;
  106. runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
  107. if (!runtime) {
  108. kfree(data);
  109. snd_card_unref(compr->card);
  110. return -ENOMEM;
  111. }
  112. runtime->state = SNDRV_PCM_STATE_OPEN;
  113. init_waitqueue_head(&runtime->sleep);
  114. data->stream.runtime = runtime;
  115. f->private_data = (void *)data;
  116. mutex_lock(&compr->lock);
  117. ret = compr->ops->open(&data->stream);
  118. mutex_unlock(&compr->lock);
  119. if (ret) {
  120. kfree(runtime);
  121. kfree(data);
  122. }
  123. snd_card_unref(compr->card);
  124. return 0;
  125. }
  126. static int snd_compr_free(struct inode *inode, struct file *f)
  127. {
  128. struct snd_compr_file *data = f->private_data;
  129. data->stream.ops->free(&data->stream);
  130. kfree(data->stream.runtime->buffer);
  131. kfree(data->stream.runtime);
  132. kfree(data);
  133. return 0;
  134. }
  135. static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
  136. struct snd_compr_tstamp *tstamp)
  137. {
  138. if (!stream->ops->pointer)
  139. return -ENOTSUPP;
  140. stream->ops->pointer(stream, tstamp);
  141. pr_debug("dsp consumed till %d total %d bytes\n",
  142. tstamp->byte_offset, tstamp->copied_total);
  143. if (stream->direction == SND_COMPRESS_PLAYBACK)
  144. stream->runtime->total_bytes_transferred = tstamp->copied_total;
  145. else
  146. stream->runtime->total_bytes_available = tstamp->copied_total;
  147. return 0;
  148. }
  149. static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
  150. struct snd_compr_avail *avail)
  151. {
  152. memset(avail, 0, sizeof(*avail));
  153. snd_compr_update_tstamp(stream, &avail->tstamp);
  154. /* Still need to return avail even if tstamp can't be filled in */
  155. if (stream->runtime->total_bytes_available == 0 &&
  156. stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
  157. stream->direction == SND_COMPRESS_PLAYBACK) {
  158. pr_debug("detected init and someone forgot to do a write\n");
  159. return stream->runtime->buffer_size;
  160. }
  161. pr_debug("app wrote %lld, DSP consumed %lld\n",
  162. stream->runtime->total_bytes_available,
  163. stream->runtime->total_bytes_transferred);
  164. if (stream->runtime->total_bytes_available ==
  165. stream->runtime->total_bytes_transferred) {
  166. if (stream->direction == SND_COMPRESS_PLAYBACK) {
  167. pr_debug("both pointers are same, returning full avail\n");
  168. return stream->runtime->buffer_size;
  169. } else {
  170. pr_debug("both pointers are same, returning no avail\n");
  171. return 0;
  172. }
  173. }
  174. avail->avail = stream->runtime->total_bytes_available -
  175. stream->runtime->total_bytes_transferred;
  176. if (stream->direction == SND_COMPRESS_PLAYBACK)
  177. avail->avail = stream->runtime->buffer_size - avail->avail;
  178. pr_debug("ret avail as %lld\n", avail->avail);
  179. return avail->avail;
  180. }
  181. static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
  182. {
  183. struct snd_compr_avail avail;
  184. return snd_compr_calc_avail(stream, &avail);
  185. }
  186. static int
  187. snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
  188. {
  189. struct snd_compr_avail ioctl_avail;
  190. size_t avail;
  191. avail = snd_compr_calc_avail(stream, &ioctl_avail);
  192. ioctl_avail.avail = avail;
  193. if (copy_to_user((__u64 __user *)arg,
  194. &ioctl_avail, sizeof(ioctl_avail)))
  195. return -EFAULT;
  196. return 0;
  197. }
  198. static int snd_compr_write_data(struct snd_compr_stream *stream,
  199. const char __user *buf, size_t count)
  200. {
  201. void *dstn;
  202. size_t copy;
  203. struct snd_compr_runtime *runtime = stream->runtime;
  204. dstn = runtime->buffer + runtime->app_pointer;
  205. pr_debug("copying %ld at %lld\n",
  206. (unsigned long)count, runtime->app_pointer);
  207. if (count < runtime->buffer_size - runtime->app_pointer) {
  208. if (copy_from_user(dstn, buf, count))
  209. return -EFAULT;
  210. runtime->app_pointer += count;
  211. } else {
  212. copy = runtime->buffer_size - runtime->app_pointer;
  213. if (copy_from_user(dstn, buf, copy))
  214. return -EFAULT;
  215. if (copy_from_user(runtime->buffer, buf + copy, count - copy))
  216. return -EFAULT;
  217. runtime->app_pointer = count - copy;
  218. }
  219. /* if DSP cares, let it know data has been written */
  220. if (stream->ops->ack)
  221. stream->ops->ack(stream, count);
  222. return count;
  223. }
  224. static ssize_t snd_compr_write(struct file *f, const char __user *buf,
  225. size_t count, loff_t *offset)
  226. {
  227. struct snd_compr_file *data = f->private_data;
  228. struct snd_compr_stream *stream;
  229. size_t avail;
  230. int retval;
  231. if (snd_BUG_ON(!data))
  232. return -EFAULT;
  233. stream = &data->stream;
  234. mutex_lock(&stream->device->lock);
  235. /* write is allowed when stream is running or has been steup */
  236. if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
  237. stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
  238. mutex_unlock(&stream->device->lock);
  239. return -EBADFD;
  240. }
  241. avail = snd_compr_get_avail(stream);
  242. pr_debug("avail returned %ld\n", (unsigned long)avail);
  243. /* calculate how much we can write to buffer */
  244. if (avail > count)
  245. avail = count;
  246. if (stream->ops->copy) {
  247. char __user* cbuf = (char __user*)buf;
  248. retval = stream->ops->copy(stream, cbuf, avail);
  249. } else {
  250. retval = snd_compr_write_data(stream, buf, avail);
  251. }
  252. if (retval > 0)
  253. stream->runtime->total_bytes_available += retval;
  254. /* while initiating the stream, write should be called before START
  255. * call, so in setup move state */
  256. if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
  257. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  258. pr_debug("stream prepared, Houston we are good to go\n");
  259. }
  260. mutex_unlock(&stream->device->lock);
  261. return retval;
  262. }
  263. static ssize_t snd_compr_read(struct file *f, char __user *buf,
  264. size_t count, loff_t *offset)
  265. {
  266. struct snd_compr_file *data = f->private_data;
  267. struct snd_compr_stream *stream;
  268. size_t avail;
  269. int retval;
  270. if (snd_BUG_ON(!data))
  271. return -EFAULT;
  272. stream = &data->stream;
  273. mutex_lock(&stream->device->lock);
  274. /* read is allowed when stream is running */
  275. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
  276. retval = -EBADFD;
  277. goto out;
  278. }
  279. avail = snd_compr_get_avail(stream);
  280. pr_debug("avail returned %ld\n", (unsigned long)avail);
  281. /* calculate how much we can read from buffer */
  282. if (avail > count)
  283. avail = count;
  284. if (stream->ops->copy) {
  285. retval = stream->ops->copy(stream, buf, avail);
  286. } else {
  287. retval = -ENXIO;
  288. goto out;
  289. }
  290. if (retval > 0)
  291. stream->runtime->total_bytes_transferred += retval;
  292. out:
  293. mutex_unlock(&stream->device->lock);
  294. return retval;
  295. }
  296. static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
  297. {
  298. return -ENXIO;
  299. }
  300. static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
  301. {
  302. if (stream->direction == SND_COMPRESS_PLAYBACK)
  303. return POLLOUT | POLLWRNORM;
  304. else
  305. return POLLIN | POLLRDNORM;
  306. }
  307. static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
  308. {
  309. struct snd_compr_file *data = f->private_data;
  310. struct snd_compr_stream *stream;
  311. size_t avail;
  312. int retval = 0;
  313. if (snd_BUG_ON(!data))
  314. return -EFAULT;
  315. stream = &data->stream;
  316. if (snd_BUG_ON(!stream))
  317. return -EFAULT;
  318. mutex_lock(&stream->device->lock);
  319. if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
  320. stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  321. retval = -EBADFD;
  322. goto out;
  323. }
  324. poll_wait(f, &stream->runtime->sleep, wait);
  325. avail = snd_compr_get_avail(stream);
  326. pr_debug("avail is %ld\n", (unsigned long)avail);
  327. /* check if we have at least one fragment to fill */
  328. switch (stream->runtime->state) {
  329. case SNDRV_PCM_STATE_DRAINING:
  330. /* stream has been woken up after drain is complete
  331. * draining done so set stream state to stopped
  332. */
  333. retval = snd_compr_get_poll(stream);
  334. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  335. break;
  336. case SNDRV_PCM_STATE_RUNNING:
  337. case SNDRV_PCM_STATE_PREPARED:
  338. case SNDRV_PCM_STATE_PAUSED:
  339. if (avail >= stream->runtime->fragment_size)
  340. retval = snd_compr_get_poll(stream);
  341. break;
  342. default:
  343. if (stream->direction == SND_COMPRESS_PLAYBACK)
  344. retval = POLLOUT | POLLWRNORM | POLLERR;
  345. else
  346. retval = POLLIN | POLLRDNORM | POLLERR;
  347. break;
  348. }
  349. out:
  350. mutex_unlock(&stream->device->lock);
  351. return retval;
  352. }
  353. static int
  354. snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
  355. {
  356. int retval;
  357. struct snd_compr_caps caps;
  358. if (!stream->ops->get_caps)
  359. return -ENXIO;
  360. retval = stream->ops->get_caps(stream, &caps);
  361. if (retval)
  362. goto out;
  363. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  364. retval = -EFAULT;
  365. out:
  366. return retval;
  367. }
  368. static int
  369. snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
  370. {
  371. int retval;
  372. struct snd_compr_codec_caps *caps;
  373. if (!stream->ops->get_codec_caps)
  374. return -ENXIO;
  375. caps = kmalloc(sizeof(*caps), GFP_KERNEL);
  376. if (!caps)
  377. return -ENOMEM;
  378. retval = stream->ops->get_codec_caps(stream, caps);
  379. if (retval)
  380. goto out;
  381. if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
  382. retval = -EFAULT;
  383. out:
  384. kfree(caps);
  385. return retval;
  386. }
  387. /* revisit this with snd_pcm_preallocate_xxx */
  388. static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
  389. struct snd_compr_params *params)
  390. {
  391. unsigned int buffer_size;
  392. void *buffer;
  393. buffer_size = params->buffer.fragment_size * params->buffer.fragments;
  394. if (stream->ops->copy) {
  395. buffer = NULL;
  396. /* if copy is defined the driver will be required to copy
  397. * the data from core
  398. */
  399. } else {
  400. buffer = kmalloc(buffer_size, GFP_KERNEL);
  401. if (!buffer)
  402. return -ENOMEM;
  403. }
  404. stream->runtime->fragment_size = params->buffer.fragment_size;
  405. stream->runtime->fragments = params->buffer.fragments;
  406. stream->runtime->buffer = buffer;
  407. stream->runtime->buffer_size = buffer_size;
  408. return 0;
  409. }
  410. static int snd_compress_check_input(struct snd_compr_params *params)
  411. {
  412. /* first let's check the buffer parameter's */
  413. if (params->buffer.fragment_size == 0 ||
  414. params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size)
  415. return -EINVAL;
  416. /* now codec parameters */
  417. if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
  418. return -EINVAL;
  419. if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
  420. return -EINVAL;
  421. if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000))
  422. return -EINVAL;
  423. return 0;
  424. }
  425. static int
  426. snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
  427. {
  428. struct snd_compr_params *params;
  429. int retval;
  430. if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  431. /*
  432. * we should allow parameter change only when stream has been
  433. * opened not in other cases
  434. */
  435. params = kmalloc(sizeof(*params), GFP_KERNEL);
  436. if (!params)
  437. return -ENOMEM;
  438. if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
  439. retval = -EFAULT;
  440. goto out;
  441. }
  442. retval = snd_compress_check_input(params);
  443. if (retval)
  444. goto out;
  445. retval = snd_compr_allocate_buffer(stream, params);
  446. if (retval) {
  447. retval = -ENOMEM;
  448. goto out;
  449. }
  450. retval = stream->ops->set_params(stream, params);
  451. if (retval)
  452. goto out;
  453. stream->metadata_set = false;
  454. stream->next_track = false;
  455. if (stream->direction == SND_COMPRESS_PLAYBACK)
  456. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  457. else
  458. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  459. } else {
  460. return -EPERM;
  461. }
  462. out:
  463. kfree(params);
  464. return retval;
  465. }
  466. static int
  467. snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
  468. {
  469. struct snd_codec *params;
  470. int retval;
  471. if (!stream->ops->get_params)
  472. return -EBADFD;
  473. params = kmalloc(sizeof(*params), GFP_KERNEL);
  474. if (!params)
  475. return -ENOMEM;
  476. retval = stream->ops->get_params(stream, params);
  477. if (retval)
  478. goto out;
  479. if (copy_to_user((char __user *)arg, params, sizeof(*params)))
  480. retval = -EFAULT;
  481. out:
  482. kfree(params);
  483. return retval;
  484. }
  485. static int
  486. snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
  487. {
  488. struct snd_compr_metadata metadata;
  489. int retval;
  490. if (!stream->ops->get_metadata)
  491. return -ENXIO;
  492. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  493. return -EFAULT;
  494. retval = stream->ops->get_metadata(stream, &metadata);
  495. if (retval != 0)
  496. return retval;
  497. if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
  498. return -EFAULT;
  499. return 0;
  500. }
  501. static int
  502. snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
  503. {
  504. struct snd_compr_metadata metadata;
  505. int retval;
  506. if (!stream->ops->set_metadata)
  507. return -ENXIO;
  508. /*
  509. * we should allow parameter change only when stream has been
  510. * opened not in other cases
  511. */
  512. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  513. return -EFAULT;
  514. retval = stream->ops->set_metadata(stream, &metadata);
  515. stream->metadata_set = true;
  516. return retval;
  517. }
  518. static inline int
  519. snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
  520. {
  521. struct snd_compr_tstamp tstamp = {0};
  522. int ret;
  523. ret = snd_compr_update_tstamp(stream, &tstamp);
  524. if (ret == 0)
  525. ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
  526. &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
  527. return ret;
  528. }
  529. static int snd_compr_pause(struct snd_compr_stream *stream)
  530. {
  531. int retval;
  532. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  533. return -EPERM;
  534. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  535. if (!retval)
  536. stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
  537. return retval;
  538. }
  539. static int snd_compr_resume(struct snd_compr_stream *stream)
  540. {
  541. int retval;
  542. if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
  543. return -EPERM;
  544. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  545. if (!retval)
  546. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  547. return retval;
  548. }
  549. static int snd_compr_start(struct snd_compr_stream *stream)
  550. {
  551. int retval;
  552. if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
  553. return -EPERM;
  554. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
  555. if (!retval)
  556. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  557. return retval;
  558. }
  559. static int snd_compr_stop(struct snd_compr_stream *stream)
  560. {
  561. int retval;
  562. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  563. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  564. return -EPERM;
  565. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
  566. if (!retval) {
  567. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  568. wake_up(&stream->runtime->sleep);
  569. stream->runtime->app_pointer = 0;
  570. stream->runtime->total_bytes_available = 0;
  571. stream->runtime->total_bytes_transferred = 0;
  572. }
  573. return retval;
  574. }
  575. static int snd_compr_drain(struct snd_compr_stream *stream)
  576. {
  577. int retval;
  578. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  579. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  580. return -EPERM;
  581. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
  582. if (!retval) {
  583. stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
  584. wake_up(&stream->runtime->sleep);
  585. }
  586. return retval;
  587. }
  588. static int snd_compr_next_track(struct snd_compr_stream *stream)
  589. {
  590. int retval;
  591. /* only a running stream can transition to next track */
  592. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  593. return -EPERM;
  594. /* you can signal next track isf this is intended to be a gapless stream
  595. * and current track metadata is set
  596. */
  597. if (stream->metadata_set == false)
  598. return -EPERM;
  599. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
  600. if (retval != 0)
  601. return retval;
  602. stream->metadata_set = false;
  603. stream->next_track = true;
  604. return 0;
  605. }
  606. static int snd_compr_partial_drain(struct snd_compr_stream *stream)
  607. {
  608. int retval;
  609. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  610. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  611. return -EPERM;
  612. /* stream can be drained only when next track has been signalled */
  613. if (stream->next_track == false)
  614. return -EPERM;
  615. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
  616. stream->next_track = false;
  617. return retval;
  618. }
  619. static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  620. {
  621. struct snd_compr_file *data = f->private_data;
  622. struct snd_compr_stream *stream;
  623. int retval = -ENOTTY;
  624. if (snd_BUG_ON(!data))
  625. return -EFAULT;
  626. stream = &data->stream;
  627. if (snd_BUG_ON(!stream))
  628. return -EFAULT;
  629. mutex_lock(&stream->device->lock);
  630. switch (_IOC_NR(cmd)) {
  631. case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
  632. put_user(SNDRV_COMPRESS_VERSION,
  633. (int __user *)arg) ? -EFAULT : 0;
  634. break;
  635. case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
  636. retval = snd_compr_get_caps(stream, arg);
  637. break;
  638. case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
  639. retval = snd_compr_get_codec_caps(stream, arg);
  640. break;
  641. case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
  642. retval = snd_compr_set_params(stream, arg);
  643. break;
  644. case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
  645. retval = snd_compr_get_params(stream, arg);
  646. break;
  647. case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
  648. retval = snd_compr_set_metadata(stream, arg);
  649. break;
  650. case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
  651. retval = snd_compr_get_metadata(stream, arg);
  652. break;
  653. case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
  654. retval = snd_compr_tstamp(stream, arg);
  655. break;
  656. case _IOC_NR(SNDRV_COMPRESS_AVAIL):
  657. retval = snd_compr_ioctl_avail(stream, arg);
  658. break;
  659. case _IOC_NR(SNDRV_COMPRESS_PAUSE):
  660. retval = snd_compr_pause(stream);
  661. break;
  662. case _IOC_NR(SNDRV_COMPRESS_RESUME):
  663. retval = snd_compr_resume(stream);
  664. break;
  665. case _IOC_NR(SNDRV_COMPRESS_START):
  666. retval = snd_compr_start(stream);
  667. break;
  668. case _IOC_NR(SNDRV_COMPRESS_STOP):
  669. retval = snd_compr_stop(stream);
  670. break;
  671. case _IOC_NR(SNDRV_COMPRESS_DRAIN):
  672. retval = snd_compr_drain(stream);
  673. break;
  674. case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
  675. retval = snd_compr_partial_drain(stream);
  676. break;
  677. case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
  678. retval = snd_compr_next_track(stream);
  679. break;
  680. }
  681. mutex_unlock(&stream->device->lock);
  682. return retval;
  683. }
  684. static const struct file_operations snd_compr_file_ops = {
  685. .owner = THIS_MODULE,
  686. .open = snd_compr_open,
  687. .release = snd_compr_free,
  688. .write = snd_compr_write,
  689. .read = snd_compr_read,
  690. .unlocked_ioctl = snd_compr_ioctl,
  691. .mmap = snd_compr_mmap,
  692. .poll = snd_compr_poll,
  693. };
  694. static int snd_compress_dev_register(struct snd_device *device)
  695. {
  696. int ret = -EINVAL;
  697. char str[16];
  698. struct snd_compr *compr;
  699. if (snd_BUG_ON(!device || !device->device_data))
  700. return -EBADFD;
  701. compr = device->device_data;
  702. sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
  703. pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
  704. compr->direction);
  705. /* register compressed device */
  706. ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
  707. compr->device, &snd_compr_file_ops, compr, str);
  708. if (ret < 0) {
  709. pr_err("snd_register_device failed\n %d", ret);
  710. return ret;
  711. }
  712. return ret;
  713. }
  714. static int snd_compress_dev_disconnect(struct snd_device *device)
  715. {
  716. struct snd_compr *compr;
  717. compr = device->device_data;
  718. snd_unregister_device(compr->direction, compr->card, compr->device);
  719. return 0;
  720. }
  721. /*
  722. * snd_compress_new: create new compress device
  723. * @card: sound card pointer
  724. * @device: device number
  725. * @dirn: device direction, should be of type enum snd_compr_direction
  726. * @compr: compress device pointer
  727. */
  728. int snd_compress_new(struct snd_card *card, int device,
  729. int dirn, struct snd_compr *compr)
  730. {
  731. static struct snd_device_ops ops = {
  732. .dev_free = NULL,
  733. .dev_register = snd_compress_dev_register,
  734. .dev_disconnect = snd_compress_dev_disconnect,
  735. };
  736. compr->card = card;
  737. compr->device = device;
  738. compr->direction = dirn;
  739. return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
  740. }
  741. EXPORT_SYMBOL_GPL(snd_compress_new);
  742. static int snd_compress_add_device(struct snd_compr *device)
  743. {
  744. int ret;
  745. if (!device->card)
  746. return -EINVAL;
  747. /* register the card */
  748. ret = snd_card_register(device->card);
  749. if (ret)
  750. goto out;
  751. return 0;
  752. out:
  753. pr_err("failed with %d\n", ret);
  754. return ret;
  755. }
  756. static int snd_compress_remove_device(struct snd_compr *device)
  757. {
  758. return snd_card_free(device->card);
  759. }
  760. /**
  761. * snd_compress_register - register compressed device
  762. *
  763. * @device: compressed device to register
  764. */
  765. int snd_compress_register(struct snd_compr *device)
  766. {
  767. int retval;
  768. if (device->name == NULL || device->dev == NULL || device->ops == NULL)
  769. return -EINVAL;
  770. pr_debug("Registering compressed device %s\n", device->name);
  771. if (snd_BUG_ON(!device->ops->open))
  772. return -EINVAL;
  773. if (snd_BUG_ON(!device->ops->free))
  774. return -EINVAL;
  775. if (snd_BUG_ON(!device->ops->set_params))
  776. return -EINVAL;
  777. if (snd_BUG_ON(!device->ops->trigger))
  778. return -EINVAL;
  779. mutex_init(&device->lock);
  780. /* register a compressed card */
  781. mutex_lock(&device_mutex);
  782. retval = snd_compress_add_device(device);
  783. mutex_unlock(&device_mutex);
  784. return retval;
  785. }
  786. EXPORT_SYMBOL_GPL(snd_compress_register);
  787. int snd_compress_deregister(struct snd_compr *device)
  788. {
  789. pr_debug("Removing compressed device %s\n", device->name);
  790. mutex_lock(&device_mutex);
  791. snd_compress_remove_device(device);
  792. mutex_unlock(&device_mutex);
  793. return 0;
  794. }
  795. EXPORT_SYMBOL_GPL(snd_compress_deregister);
  796. static int __init snd_compress_init(void)
  797. {
  798. return 0;
  799. }
  800. static void __exit snd_compress_exit(void)
  801. {
  802. }
  803. module_init(snd_compress_init);
  804. module_exit(snd_compress_exit);
  805. MODULE_DESCRIPTION("ALSA Compressed offload framework");
  806. MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
  807. MODULE_LICENSE("GPL v2");