compress_offload.c 26 KB

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