compress_offload.c 23 KB

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