compress_offload.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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 void snd_compr_update_tstamp(struct snd_compr_stream *stream,
  136. struct snd_compr_tstamp *tstamp)
  137. {
  138. if (!stream->ops->pointer)
  139. return;
  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. stream->runtime->total_bytes_transferred = tstamp->copied_total;
  145. }
  146. static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
  147. struct snd_compr_avail *avail)
  148. {
  149. long avail_calc; /*this needs to be signed variable */
  150. snd_compr_update_tstamp(stream, &avail->tstamp);
  151. /* FIXME: This needs to be different for capture stream,
  152. available is # of compressed data, for playback it's
  153. remainder of buffer */
  154. if (stream->runtime->total_bytes_available == 0 &&
  155. stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
  156. pr_debug("detected init and someone forgot to do a write\n");
  157. return stream->runtime->buffer_size;
  158. }
  159. pr_debug("app wrote %lld, DSP consumed %lld\n",
  160. stream->runtime->total_bytes_available,
  161. stream->runtime->total_bytes_transferred);
  162. if (stream->runtime->total_bytes_available ==
  163. stream->runtime->total_bytes_transferred) {
  164. pr_debug("both pointers are same, returning full avail\n");
  165. return stream->runtime->buffer_size;
  166. }
  167. /* FIXME: this routine isn't consistent, in one test we use
  168. * cumulative values and in the other byte offsets. Do we
  169. * really need the byte offsets if the cumulative values have
  170. * been updated? In the PCM interface app_ptr and hw_ptr are
  171. * already cumulative */
  172. avail_calc = stream->runtime->buffer_size -
  173. (stream->runtime->app_pointer - stream->runtime->hw_pointer);
  174. pr_debug("calc avail as %ld, app_ptr %lld, hw+ptr %lld\n", avail_calc,
  175. stream->runtime->app_pointer,
  176. stream->runtime->hw_pointer);
  177. if (avail_calc >= stream->runtime->buffer_size)
  178. avail_calc -= stream->runtime->buffer_size;
  179. pr_debug("ret avail as %ld\n", avail_calc);
  180. avail->avail = avail_calc;
  181. return avail_calc;
  182. }
  183. static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
  184. {
  185. struct snd_compr_avail avail;
  186. return snd_compr_calc_avail(stream, &avail);
  187. }
  188. static int
  189. snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
  190. {
  191. struct snd_compr_avail ioctl_avail;
  192. size_t avail;
  193. avail = snd_compr_calc_avail(stream, &ioctl_avail);
  194. ioctl_avail.avail = avail;
  195. if (copy_to_user((__u64 __user *)arg,
  196. &ioctl_avail, sizeof(ioctl_avail)))
  197. return -EFAULT;
  198. return 0;
  199. }
  200. static int snd_compr_write_data(struct snd_compr_stream *stream,
  201. const char __user *buf, size_t count)
  202. {
  203. void *dstn;
  204. size_t copy;
  205. struct snd_compr_runtime *runtime = stream->runtime;
  206. dstn = runtime->buffer + runtime->app_pointer;
  207. pr_debug("copying %ld at %lld\n",
  208. (unsigned long)count, runtime->app_pointer);
  209. if (count < runtime->buffer_size - runtime->app_pointer) {
  210. if (copy_from_user(dstn, buf, count))
  211. return -EFAULT;
  212. runtime->app_pointer += count;
  213. } else {
  214. copy = runtime->buffer_size - runtime->app_pointer;
  215. if (copy_from_user(dstn, buf, copy))
  216. return -EFAULT;
  217. if (copy_from_user(runtime->buffer, buf + copy, count - copy))
  218. return -EFAULT;
  219. runtime->app_pointer = count - copy;
  220. }
  221. /* if DSP cares, let it know data has been written */
  222. if (stream->ops->ack)
  223. stream->ops->ack(stream, count);
  224. return count;
  225. }
  226. static ssize_t snd_compr_write(struct file *f, const char __user *buf,
  227. size_t count, loff_t *offset)
  228. {
  229. struct snd_compr_file *data = f->private_data;
  230. struct snd_compr_stream *stream;
  231. size_t avail;
  232. int retval;
  233. if (snd_BUG_ON(!data))
  234. return -EFAULT;
  235. stream = &data->stream;
  236. mutex_lock(&stream->device->lock);
  237. /* write is allowed when stream is running or has been steup */
  238. if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
  239. stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
  240. mutex_unlock(&stream->device->lock);
  241. return -EBADFD;
  242. }
  243. avail = snd_compr_get_avail(stream);
  244. pr_debug("avail returned %ld\n", (unsigned long)avail);
  245. /* calculate how much we can write to buffer */
  246. if (avail > count)
  247. avail = count;
  248. if (stream->ops->copy)
  249. retval = stream->ops->copy(stream, buf, avail);
  250. else
  251. retval = snd_compr_write_data(stream, buf, avail);
  252. if (retval > 0)
  253. stream->runtime->total_bytes_available += retval;
  254. /* while initiating the stream, write should be called before START
  255. * call, so in setup move state */
  256. if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
  257. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  258. pr_debug("stream prepared, Houston we are good to go\n");
  259. }
  260. mutex_unlock(&stream->device->lock);
  261. return retval;
  262. }
  263. static ssize_t snd_compr_read(struct file *f, char __user *buf,
  264. size_t count, loff_t *offset)
  265. {
  266. return -ENXIO;
  267. }
  268. static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
  269. {
  270. return -ENXIO;
  271. }
  272. static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
  273. {
  274. if (stream->direction == SND_COMPRESS_PLAYBACK)
  275. return POLLOUT | POLLWRNORM;
  276. else
  277. return POLLIN | POLLRDNORM;
  278. }
  279. static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
  280. {
  281. struct snd_compr_file *data = f->private_data;
  282. struct snd_compr_stream *stream;
  283. size_t avail;
  284. int retval = 0;
  285. if (snd_BUG_ON(!data))
  286. return -EFAULT;
  287. stream = &data->stream;
  288. if (snd_BUG_ON(!stream))
  289. return -EFAULT;
  290. mutex_lock(&stream->device->lock);
  291. if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
  292. stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  293. retval = -EBADFD;
  294. goto out;
  295. }
  296. poll_wait(f, &stream->runtime->sleep, wait);
  297. avail = snd_compr_get_avail(stream);
  298. pr_debug("avail is %ld\n", (unsigned long)avail);
  299. /* check if we have at least one fragment to fill */
  300. switch (stream->runtime->state) {
  301. case SNDRV_PCM_STATE_DRAINING:
  302. /* stream has been woken up after drain is complete
  303. * draining done so set stream state to stopped
  304. */
  305. retval = snd_compr_get_poll(stream);
  306. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  307. break;
  308. case SNDRV_PCM_STATE_RUNNING:
  309. case SNDRV_PCM_STATE_PREPARED:
  310. case SNDRV_PCM_STATE_PAUSED:
  311. if (avail >= stream->runtime->fragment_size)
  312. retval = snd_compr_get_poll(stream);
  313. break;
  314. default:
  315. if (stream->direction == SND_COMPRESS_PLAYBACK)
  316. retval = POLLOUT | POLLWRNORM | POLLERR;
  317. else
  318. retval = POLLIN | POLLRDNORM | POLLERR;
  319. break;
  320. }
  321. out:
  322. mutex_unlock(&stream->device->lock);
  323. return retval;
  324. }
  325. static int
  326. snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
  327. {
  328. int retval;
  329. struct snd_compr_caps caps;
  330. if (!stream->ops->get_caps)
  331. return -ENXIO;
  332. retval = stream->ops->get_caps(stream, &caps);
  333. if (retval)
  334. goto out;
  335. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  336. retval = -EFAULT;
  337. out:
  338. return retval;
  339. }
  340. static int
  341. snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
  342. {
  343. int retval;
  344. struct snd_compr_codec_caps *caps;
  345. if (!stream->ops->get_codec_caps)
  346. return -ENXIO;
  347. caps = kmalloc(sizeof(*caps), GFP_KERNEL);
  348. if (!caps)
  349. return -ENOMEM;
  350. retval = stream->ops->get_codec_caps(stream, caps);
  351. if (retval)
  352. goto out;
  353. if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
  354. retval = -EFAULT;
  355. out:
  356. kfree(caps);
  357. return retval;
  358. }
  359. /* revisit this with snd_pcm_preallocate_xxx */
  360. static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
  361. struct snd_compr_params *params)
  362. {
  363. unsigned int buffer_size;
  364. void *buffer;
  365. buffer_size = params->buffer.fragment_size * params->buffer.fragments;
  366. if (stream->ops->copy) {
  367. buffer = NULL;
  368. /* if copy is defined the driver will be required to copy
  369. * the data from core
  370. */
  371. } else {
  372. buffer = kmalloc(buffer_size, GFP_KERNEL);
  373. if (!buffer)
  374. return -ENOMEM;
  375. }
  376. stream->runtime->fragment_size = params->buffer.fragment_size;
  377. stream->runtime->fragments = params->buffer.fragments;
  378. stream->runtime->buffer = buffer;
  379. stream->runtime->buffer_size = buffer_size;
  380. return 0;
  381. }
  382. static int snd_compress_check_input(struct snd_compr_params *params)
  383. {
  384. /* first let's check the buffer parameter's */
  385. if (params->buffer.fragment_size == 0 ||
  386. params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size)
  387. return -EINVAL;
  388. /* now codec parameters */
  389. if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
  390. return -EINVAL;
  391. if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
  392. return -EINVAL;
  393. if (!(params->codec.sample_rate & SNDRV_PCM_RATE_8000_192000))
  394. return -EINVAL;
  395. return 0;
  396. }
  397. static int
  398. snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
  399. {
  400. struct snd_compr_params *params;
  401. int retval;
  402. if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  403. /*
  404. * we should allow parameter change only when stream has been
  405. * opened not in other cases
  406. */
  407. params = kmalloc(sizeof(*params), GFP_KERNEL);
  408. if (!params)
  409. return -ENOMEM;
  410. if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
  411. retval = -EFAULT;
  412. goto out;
  413. }
  414. retval = snd_compress_check_input(params);
  415. if (retval)
  416. goto out;
  417. retval = snd_compr_allocate_buffer(stream, params);
  418. if (retval) {
  419. retval = -ENOMEM;
  420. goto out;
  421. }
  422. retval = stream->ops->set_params(stream, params);
  423. if (retval)
  424. goto out;
  425. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  426. } else {
  427. return -EPERM;
  428. }
  429. out:
  430. kfree(params);
  431. return retval;
  432. }
  433. static int
  434. snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
  435. {
  436. struct snd_codec *params;
  437. int retval;
  438. if (!stream->ops->get_params)
  439. return -EBADFD;
  440. params = kmalloc(sizeof(*params), GFP_KERNEL);
  441. if (!params)
  442. return -ENOMEM;
  443. retval = stream->ops->get_params(stream, params);
  444. if (retval)
  445. goto out;
  446. if (copy_to_user((char __user *)arg, params, sizeof(*params)))
  447. retval = -EFAULT;
  448. out:
  449. kfree(params);
  450. return retval;
  451. }
  452. static inline int
  453. snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
  454. {
  455. struct snd_compr_tstamp tstamp;
  456. snd_compr_update_tstamp(stream, &tstamp);
  457. return copy_to_user((struct snd_compr_tstamp __user *)arg,
  458. &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
  459. }
  460. static int snd_compr_pause(struct snd_compr_stream *stream)
  461. {
  462. int retval;
  463. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  464. return -EPERM;
  465. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  466. if (!retval)
  467. stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
  468. return retval;
  469. }
  470. static int snd_compr_resume(struct snd_compr_stream *stream)
  471. {
  472. int retval;
  473. if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
  474. return -EPERM;
  475. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  476. if (!retval)
  477. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  478. return retval;
  479. }
  480. static int snd_compr_start(struct snd_compr_stream *stream)
  481. {
  482. int retval;
  483. if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
  484. return -EPERM;
  485. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
  486. if (!retval)
  487. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  488. return retval;
  489. }
  490. static int snd_compr_stop(struct snd_compr_stream *stream)
  491. {
  492. int retval;
  493. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  494. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  495. return -EPERM;
  496. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
  497. if (!retval) {
  498. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  499. wake_up(&stream->runtime->sleep);
  500. stream->runtime->hw_pointer = 0;
  501. stream->runtime->app_pointer = 0;
  502. stream->runtime->total_bytes_available = 0;
  503. stream->runtime->total_bytes_transferred = 0;
  504. }
  505. return retval;
  506. }
  507. static int snd_compr_drain(struct snd_compr_stream *stream)
  508. {
  509. int retval;
  510. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  511. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  512. return -EPERM;
  513. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
  514. if (!retval) {
  515. stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
  516. wake_up(&stream->runtime->sleep);
  517. }
  518. return retval;
  519. }
  520. static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  521. {
  522. struct snd_compr_file *data = f->private_data;
  523. struct snd_compr_stream *stream;
  524. int retval = -ENOTTY;
  525. if (snd_BUG_ON(!data))
  526. return -EFAULT;
  527. stream = &data->stream;
  528. if (snd_BUG_ON(!stream))
  529. return -EFAULT;
  530. mutex_lock(&stream->device->lock);
  531. switch (_IOC_NR(cmd)) {
  532. case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
  533. put_user(SNDRV_COMPRESS_VERSION,
  534. (int __user *)arg) ? -EFAULT : 0;
  535. break;
  536. case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
  537. retval = snd_compr_get_caps(stream, arg);
  538. break;
  539. case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
  540. retval = snd_compr_get_codec_caps(stream, arg);
  541. break;
  542. case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
  543. retval = snd_compr_set_params(stream, arg);
  544. break;
  545. case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
  546. retval = snd_compr_get_params(stream, arg);
  547. break;
  548. case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
  549. retval = snd_compr_tstamp(stream, arg);
  550. break;
  551. case _IOC_NR(SNDRV_COMPRESS_AVAIL):
  552. retval = snd_compr_ioctl_avail(stream, arg);
  553. break;
  554. case _IOC_NR(SNDRV_COMPRESS_PAUSE):
  555. retval = snd_compr_pause(stream);
  556. break;
  557. case _IOC_NR(SNDRV_COMPRESS_RESUME):
  558. retval = snd_compr_resume(stream);
  559. break;
  560. case _IOC_NR(SNDRV_COMPRESS_START):
  561. retval = snd_compr_start(stream);
  562. break;
  563. case _IOC_NR(SNDRV_COMPRESS_STOP):
  564. retval = snd_compr_stop(stream);
  565. break;
  566. case _IOC_NR(SNDRV_COMPRESS_DRAIN):
  567. retval = snd_compr_drain(stream);
  568. break;
  569. }
  570. mutex_unlock(&stream->device->lock);
  571. return retval;
  572. }
  573. static const struct file_operations snd_compr_file_ops = {
  574. .owner = THIS_MODULE,
  575. .open = snd_compr_open,
  576. .release = snd_compr_free,
  577. .write = snd_compr_write,
  578. .read = snd_compr_read,
  579. .unlocked_ioctl = snd_compr_ioctl,
  580. .mmap = snd_compr_mmap,
  581. .poll = snd_compr_poll,
  582. };
  583. static int snd_compress_dev_register(struct snd_device *device)
  584. {
  585. int ret = -EINVAL;
  586. char str[16];
  587. struct snd_compr *compr;
  588. if (snd_BUG_ON(!device || !device->device_data))
  589. return -EBADFD;
  590. compr = device->device_data;
  591. sprintf(str, "comprC%iD%i", compr->card->number, compr->device);
  592. pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
  593. compr->direction);
  594. /* register compressed device */
  595. ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS, compr->card,
  596. compr->device, &snd_compr_file_ops, compr, str);
  597. if (ret < 0) {
  598. pr_err("snd_register_device failed\n %d", ret);
  599. return ret;
  600. }
  601. return ret;
  602. }
  603. static int snd_compress_dev_disconnect(struct snd_device *device)
  604. {
  605. struct snd_compr *compr;
  606. compr = device->device_data;
  607. snd_unregister_device(compr->direction, compr->card, compr->device);
  608. return 0;
  609. }
  610. /*
  611. * snd_compress_new: create new compress device
  612. * @card: sound card pointer
  613. * @device: device number
  614. * @dirn: device direction, should be of type enum snd_compr_direction
  615. * @compr: compress device pointer
  616. */
  617. int snd_compress_new(struct snd_card *card, int device,
  618. int dirn, struct snd_compr *compr)
  619. {
  620. static struct snd_device_ops ops = {
  621. .dev_free = NULL,
  622. .dev_register = snd_compress_dev_register,
  623. .dev_disconnect = snd_compress_dev_disconnect,
  624. };
  625. compr->card = card;
  626. compr->device = device;
  627. compr->direction = dirn;
  628. return snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
  629. }
  630. EXPORT_SYMBOL_GPL(snd_compress_new);
  631. static int snd_compress_add_device(struct snd_compr *device)
  632. {
  633. int ret;
  634. if (!device->card)
  635. return -EINVAL;
  636. /* register the card */
  637. ret = snd_card_register(device->card);
  638. if (ret)
  639. goto out;
  640. return 0;
  641. out:
  642. pr_err("failed with %d\n", ret);
  643. return ret;
  644. }
  645. static int snd_compress_remove_device(struct snd_compr *device)
  646. {
  647. return snd_card_free(device->card);
  648. }
  649. /**
  650. * snd_compress_register - register compressed device
  651. *
  652. * @device: compressed device to register
  653. */
  654. int snd_compress_register(struct snd_compr *device)
  655. {
  656. int retval;
  657. if (device->name == NULL || device->dev == NULL || device->ops == NULL)
  658. return -EINVAL;
  659. pr_debug("Registering compressed device %s\n", device->name);
  660. if (snd_BUG_ON(!device->ops->open))
  661. return -EINVAL;
  662. if (snd_BUG_ON(!device->ops->free))
  663. return -EINVAL;
  664. if (snd_BUG_ON(!device->ops->set_params))
  665. return -EINVAL;
  666. if (snd_BUG_ON(!device->ops->trigger))
  667. return -EINVAL;
  668. mutex_init(&device->lock);
  669. /* register a compressed card */
  670. mutex_lock(&device_mutex);
  671. retval = snd_compress_add_device(device);
  672. mutex_unlock(&device_mutex);
  673. return retval;
  674. }
  675. EXPORT_SYMBOL_GPL(snd_compress_register);
  676. int snd_compress_deregister(struct snd_compr *device)
  677. {
  678. pr_debug("Removing compressed device %s\n", device->name);
  679. mutex_lock(&device_mutex);
  680. snd_compress_remove_device(device);
  681. mutex_unlock(&device_mutex);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL_GPL(snd_compress_deregister);
  685. static int __init snd_compress_init(void)
  686. {
  687. return 0;
  688. }
  689. static void __exit snd_compress_exit(void)
  690. {
  691. }
  692. module_init(snd_compress_init);
  693. module_exit(snd_compress_exit);
  694. MODULE_DESCRIPTION("ALSA Compressed offload framework");
  695. MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
  696. MODULE_LICENSE("GPL v2");