compress_offload.c 24 KB

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