compress_offload.c 25 KB

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