compress_offload.c 20 KB

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