compress_offload.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116
  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 <linux/compat.h>
  41. #include <sound/core.h>
  42. #include <sound/initval.h>
  43. #include <sound/info.h>
  44. #include <sound/compress_params.h>
  45. #include <sound/compress_offload.h>
  46. #include <sound/compress_driver.h>
  47. /* struct snd_compr_codec_caps overflows the ioctl bit size for some
  48. * architectures, so we need to disable the relevant ioctls.
  49. */
  50. #if _IOC_SIZEBITS < 14
  51. #define COMPR_CODEC_CAPS_OVERFLOW
  52. #endif
  53. /* TODO:
  54. * - add substream support for multiple devices in case of
  55. * SND_DYNAMIC_MINORS is not used
  56. * - Multiple node representation
  57. * driver should be able to register multiple nodes
  58. */
  59. static DEFINE_MUTEX(device_mutex);
  60. struct snd_compr_file {
  61. unsigned long caps;
  62. struct snd_compr_stream stream;
  63. };
  64. /*
  65. * a note on stream states used:
  66. * we use follwing states in the compressed core
  67. * SNDRV_PCM_STATE_OPEN: When stream has been opened.
  68. * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
  69. * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
  70. * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
  71. * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
  72. * decoding/encoding and rendering/capturing data.
  73. * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
  74. * by calling SNDRV_COMPRESS_DRAIN.
  75. * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
  76. * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
  77. * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
  78. */
  79. static int snd_compr_open(struct inode *inode, struct file *f)
  80. {
  81. struct snd_compr *compr;
  82. struct snd_compr_file *data;
  83. struct snd_compr_runtime *runtime;
  84. enum snd_compr_direction dirn;
  85. int maj = imajor(inode);
  86. int ret;
  87. if ((f->f_flags & O_ACCMODE) == O_WRONLY)
  88. dirn = SND_COMPRESS_PLAYBACK;
  89. else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
  90. dirn = SND_COMPRESS_CAPTURE;
  91. else
  92. return -EINVAL;
  93. if (maj == snd_major)
  94. compr = snd_lookup_minor_data(iminor(inode),
  95. SNDRV_DEVICE_TYPE_COMPRESS);
  96. else
  97. return -EBADFD;
  98. if (compr == NULL) {
  99. pr_err("no device data!!!\n");
  100. return -ENODEV;
  101. }
  102. if (dirn != compr->direction) {
  103. pr_err("this device doesn't support this direction\n");
  104. snd_card_unref(compr->card);
  105. return -EINVAL;
  106. }
  107. data = kzalloc(sizeof(*data), GFP_KERNEL);
  108. if (!data) {
  109. snd_card_unref(compr->card);
  110. return -ENOMEM;
  111. }
  112. data->stream.ops = compr->ops;
  113. data->stream.direction = dirn;
  114. data->stream.private_data = compr->private_data;
  115. data->stream.device = compr;
  116. runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
  117. if (!runtime) {
  118. kfree(data);
  119. snd_card_unref(compr->card);
  120. return -ENOMEM;
  121. }
  122. runtime->state = SNDRV_PCM_STATE_OPEN;
  123. init_waitqueue_head(&runtime->sleep);
  124. data->stream.runtime = runtime;
  125. f->private_data = (void *)data;
  126. mutex_lock(&compr->lock);
  127. ret = compr->ops->open(&data->stream);
  128. mutex_unlock(&compr->lock);
  129. if (ret) {
  130. kfree(runtime);
  131. kfree(data);
  132. }
  133. snd_card_unref(compr->card);
  134. return ret;
  135. }
  136. static int snd_compr_free(struct inode *inode, struct file *f)
  137. {
  138. struct snd_compr_file *data = f->private_data;
  139. struct snd_compr_runtime *runtime = data->stream.runtime;
  140. switch (runtime->state) {
  141. case SNDRV_PCM_STATE_RUNNING:
  142. case SNDRV_PCM_STATE_DRAINING:
  143. case SNDRV_PCM_STATE_PAUSED:
  144. data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
  145. break;
  146. default:
  147. break;
  148. }
  149. data->stream.ops->free(&data->stream);
  150. kfree(data->stream.runtime->buffer);
  151. kfree(data->stream.runtime);
  152. kfree(data);
  153. return 0;
  154. }
  155. static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
  156. struct snd_compr_tstamp *tstamp)
  157. {
  158. if (!stream->ops->pointer)
  159. return -ENOTSUPP;
  160. stream->ops->pointer(stream, tstamp);
  161. pr_debug("dsp consumed till %d total %d bytes\n",
  162. tstamp->byte_offset, tstamp->copied_total);
  163. if (stream->direction == SND_COMPRESS_PLAYBACK)
  164. stream->runtime->total_bytes_transferred = tstamp->copied_total;
  165. else
  166. stream->runtime->total_bytes_available = tstamp->copied_total;
  167. return 0;
  168. }
  169. static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
  170. struct snd_compr_avail *avail)
  171. {
  172. memset(avail, 0, sizeof(*avail));
  173. snd_compr_update_tstamp(stream, &avail->tstamp);
  174. /* Still need to return avail even if tstamp can't be filled in */
  175. if (stream->runtime->total_bytes_available == 0 &&
  176. stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
  177. stream->direction == SND_COMPRESS_PLAYBACK) {
  178. pr_debug("detected init and someone forgot to do a write\n");
  179. return stream->runtime->buffer_size;
  180. }
  181. pr_debug("app wrote %lld, DSP consumed %lld\n",
  182. stream->runtime->total_bytes_available,
  183. stream->runtime->total_bytes_transferred);
  184. if (stream->runtime->total_bytes_available ==
  185. stream->runtime->total_bytes_transferred) {
  186. if (stream->direction == SND_COMPRESS_PLAYBACK) {
  187. pr_debug("both pointers are same, returning full avail\n");
  188. return stream->runtime->buffer_size;
  189. } else {
  190. pr_debug("both pointers are same, returning no avail\n");
  191. return 0;
  192. }
  193. }
  194. avail->avail = stream->runtime->total_bytes_available -
  195. stream->runtime->total_bytes_transferred;
  196. if (stream->direction == SND_COMPRESS_PLAYBACK)
  197. avail->avail = stream->runtime->buffer_size - avail->avail;
  198. pr_debug("ret avail as %lld\n", avail->avail);
  199. return avail->avail;
  200. }
  201. static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
  202. {
  203. struct snd_compr_avail avail;
  204. return snd_compr_calc_avail(stream, &avail);
  205. }
  206. static int
  207. snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
  208. {
  209. struct snd_compr_avail ioctl_avail;
  210. size_t avail;
  211. avail = snd_compr_calc_avail(stream, &ioctl_avail);
  212. ioctl_avail.avail = avail;
  213. if (copy_to_user((__u64 __user *)arg,
  214. &ioctl_avail, sizeof(ioctl_avail)))
  215. return -EFAULT;
  216. return 0;
  217. }
  218. static int snd_compr_write_data(struct snd_compr_stream *stream,
  219. const char __user *buf, size_t count)
  220. {
  221. void *dstn;
  222. size_t copy;
  223. struct snd_compr_runtime *runtime = stream->runtime;
  224. /* 64-bit Modulus */
  225. u64 app_pointer = div64_u64(runtime->total_bytes_available,
  226. runtime->buffer_size);
  227. app_pointer = runtime->total_bytes_available -
  228. (app_pointer * runtime->buffer_size);
  229. dstn = runtime->buffer + app_pointer;
  230. pr_debug("copying %ld at %lld\n",
  231. (unsigned long)count, app_pointer);
  232. if (count < runtime->buffer_size - app_pointer) {
  233. if (copy_from_user(dstn, buf, count))
  234. return -EFAULT;
  235. } else {
  236. copy = runtime->buffer_size - app_pointer;
  237. if (copy_from_user(dstn, buf, copy))
  238. return -EFAULT;
  239. if (copy_from_user(runtime->buffer, buf + copy, count - copy))
  240. return -EFAULT;
  241. }
  242. /* if DSP cares, let it know data has been written */
  243. if (stream->ops->ack)
  244. stream->ops->ack(stream, count);
  245. return count;
  246. }
  247. static ssize_t snd_compr_write(struct file *f, const char __user *buf,
  248. size_t count, loff_t *offset)
  249. {
  250. struct snd_compr_file *data = f->private_data;
  251. struct snd_compr_stream *stream;
  252. size_t avail;
  253. int retval;
  254. if (snd_BUG_ON(!data))
  255. return -EFAULT;
  256. stream = &data->stream;
  257. mutex_lock(&stream->device->lock);
  258. /* write is allowed when stream is running or has been steup */
  259. if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
  260. stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
  261. mutex_unlock(&stream->device->lock);
  262. return -EBADFD;
  263. }
  264. avail = snd_compr_get_avail(stream);
  265. pr_debug("avail returned %ld\n", (unsigned long)avail);
  266. /* calculate how much we can write to buffer */
  267. if (avail > count)
  268. avail = count;
  269. if (stream->ops->copy) {
  270. char __user* cbuf = (char __user*)buf;
  271. retval = stream->ops->copy(stream, cbuf, avail);
  272. } else {
  273. retval = snd_compr_write_data(stream, buf, avail);
  274. }
  275. if (retval > 0)
  276. stream->runtime->total_bytes_available += retval;
  277. /* while initiating the stream, write should be called before START
  278. * call, so in setup move state */
  279. if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
  280. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  281. pr_debug("stream prepared, Houston we are good to go\n");
  282. }
  283. mutex_unlock(&stream->device->lock);
  284. return retval;
  285. }
  286. static ssize_t snd_compr_read(struct file *f, char __user *buf,
  287. size_t count, loff_t *offset)
  288. {
  289. struct snd_compr_file *data = f->private_data;
  290. struct snd_compr_stream *stream;
  291. size_t avail;
  292. int retval;
  293. if (snd_BUG_ON(!data))
  294. return -EFAULT;
  295. stream = &data->stream;
  296. mutex_lock(&stream->device->lock);
  297. /* read is allowed when stream is running, paused, draining and setup
  298. * (yes setup is state which we transition to after stop, so if user
  299. * wants to read data after stop we allow that)
  300. */
  301. switch (stream->runtime->state) {
  302. case SNDRV_PCM_STATE_OPEN:
  303. case SNDRV_PCM_STATE_PREPARED:
  304. case SNDRV_PCM_STATE_XRUN:
  305. case SNDRV_PCM_STATE_SUSPENDED:
  306. case SNDRV_PCM_STATE_DISCONNECTED:
  307. retval = -EBADFD;
  308. goto out;
  309. }
  310. avail = snd_compr_get_avail(stream);
  311. pr_debug("avail returned %ld\n", (unsigned long)avail);
  312. /* calculate how much we can read from buffer */
  313. if (avail > count)
  314. avail = count;
  315. if (stream->ops->copy) {
  316. retval = stream->ops->copy(stream, buf, avail);
  317. } else {
  318. retval = -ENXIO;
  319. goto out;
  320. }
  321. if (retval > 0)
  322. stream->runtime->total_bytes_transferred += retval;
  323. out:
  324. mutex_unlock(&stream->device->lock);
  325. return retval;
  326. }
  327. static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
  328. {
  329. return -ENXIO;
  330. }
  331. static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
  332. {
  333. if (stream->direction == SND_COMPRESS_PLAYBACK)
  334. return POLLOUT | POLLWRNORM;
  335. else
  336. return POLLIN | POLLRDNORM;
  337. }
  338. static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
  339. {
  340. struct snd_compr_file *data = f->private_data;
  341. struct snd_compr_stream *stream;
  342. size_t avail;
  343. int retval = 0;
  344. if (snd_BUG_ON(!data))
  345. return -EFAULT;
  346. stream = &data->stream;
  347. if (snd_BUG_ON(!stream))
  348. return -EFAULT;
  349. mutex_lock(&stream->device->lock);
  350. if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  351. retval = -EBADFD;
  352. goto out;
  353. }
  354. poll_wait(f, &stream->runtime->sleep, wait);
  355. avail = snd_compr_get_avail(stream);
  356. pr_debug("avail is %ld\n", (unsigned long)avail);
  357. /* check if we have at least one fragment to fill */
  358. switch (stream->runtime->state) {
  359. case SNDRV_PCM_STATE_DRAINING:
  360. /* stream has been woken up after drain is complete
  361. * draining done so set stream state to stopped
  362. */
  363. retval = snd_compr_get_poll(stream);
  364. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  365. break;
  366. case SNDRV_PCM_STATE_RUNNING:
  367. case SNDRV_PCM_STATE_PREPARED:
  368. case SNDRV_PCM_STATE_PAUSED:
  369. if (avail >= stream->runtime->fragment_size)
  370. retval = snd_compr_get_poll(stream);
  371. break;
  372. default:
  373. if (stream->direction == SND_COMPRESS_PLAYBACK)
  374. retval = POLLOUT | POLLWRNORM | POLLERR;
  375. else
  376. retval = POLLIN | POLLRDNORM | POLLERR;
  377. break;
  378. }
  379. out:
  380. mutex_unlock(&stream->device->lock);
  381. return retval;
  382. }
  383. static int
  384. snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
  385. {
  386. int retval;
  387. struct snd_compr_caps caps;
  388. if (!stream->ops->get_caps)
  389. return -ENXIO;
  390. memset(&caps, 0, sizeof(caps));
  391. retval = stream->ops->get_caps(stream, &caps);
  392. if (retval)
  393. goto out;
  394. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  395. retval = -EFAULT;
  396. out:
  397. return retval;
  398. }
  399. #ifndef COMPR_CODEC_CAPS_OVERFLOW
  400. static int
  401. snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
  402. {
  403. int retval;
  404. struct snd_compr_codec_caps *caps;
  405. if (!stream->ops->get_codec_caps)
  406. return -ENXIO;
  407. caps = kzalloc(sizeof(*caps), GFP_KERNEL);
  408. if (!caps)
  409. return -ENOMEM;
  410. retval = stream->ops->get_codec_caps(stream, caps);
  411. if (retval)
  412. goto out;
  413. if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
  414. retval = -EFAULT;
  415. out:
  416. kfree(caps);
  417. return retval;
  418. }
  419. #endif /* !COMPR_CODEC_CAPS_OVERFLOW */
  420. /* revisit this with snd_pcm_preallocate_xxx */
  421. static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
  422. struct snd_compr_params *params)
  423. {
  424. unsigned int buffer_size;
  425. void *buffer;
  426. buffer_size = params->buffer.fragment_size * params->buffer.fragments;
  427. if (stream->ops->copy) {
  428. buffer = NULL;
  429. /* if copy is defined the driver will be required to copy
  430. * the data from core
  431. */
  432. } else {
  433. buffer = kmalloc(buffer_size, GFP_KERNEL);
  434. if (!buffer)
  435. return -ENOMEM;
  436. }
  437. stream->runtime->fragment_size = params->buffer.fragment_size;
  438. stream->runtime->fragments = params->buffer.fragments;
  439. stream->runtime->buffer = buffer;
  440. stream->runtime->buffer_size = buffer_size;
  441. return 0;
  442. }
  443. static int snd_compress_check_input(struct snd_compr_params *params)
  444. {
  445. /* first let's check the buffer parameter's */
  446. if (params->buffer.fragment_size == 0 ||
  447. params->buffer.fragments > INT_MAX / params->buffer.fragment_size)
  448. return -EINVAL;
  449. /* now codec parameters */
  450. if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
  451. return -EINVAL;
  452. if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
  453. return -EINVAL;
  454. return 0;
  455. }
  456. static int
  457. snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
  458. {
  459. struct snd_compr_params *params;
  460. int retval;
  461. if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  462. /*
  463. * we should allow parameter change only when stream has been
  464. * opened not in other cases
  465. */
  466. params = kmalloc(sizeof(*params), GFP_KERNEL);
  467. if (!params)
  468. return -ENOMEM;
  469. if (copy_from_user(params, (void __user *)arg, sizeof(*params))) {
  470. retval = -EFAULT;
  471. goto out;
  472. }
  473. retval = snd_compress_check_input(params);
  474. if (retval)
  475. goto out;
  476. retval = snd_compr_allocate_buffer(stream, params);
  477. if (retval) {
  478. retval = -ENOMEM;
  479. goto out;
  480. }
  481. retval = stream->ops->set_params(stream, params);
  482. if (retval)
  483. goto out;
  484. stream->metadata_set = false;
  485. stream->next_track = false;
  486. if (stream->direction == SND_COMPRESS_PLAYBACK)
  487. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  488. else
  489. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  490. } else {
  491. return -EPERM;
  492. }
  493. out:
  494. kfree(params);
  495. return retval;
  496. }
  497. static int
  498. snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
  499. {
  500. struct snd_codec *params;
  501. int retval;
  502. if (!stream->ops->get_params)
  503. return -EBADFD;
  504. params = kzalloc(sizeof(*params), GFP_KERNEL);
  505. if (!params)
  506. return -ENOMEM;
  507. retval = stream->ops->get_params(stream, params);
  508. if (retval)
  509. goto out;
  510. if (copy_to_user((char __user *)arg, params, sizeof(*params)))
  511. retval = -EFAULT;
  512. out:
  513. kfree(params);
  514. return retval;
  515. }
  516. static int
  517. snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
  518. {
  519. struct snd_compr_metadata metadata;
  520. int retval;
  521. if (!stream->ops->get_metadata)
  522. return -ENXIO;
  523. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  524. return -EFAULT;
  525. retval = stream->ops->get_metadata(stream, &metadata);
  526. if (retval != 0)
  527. return retval;
  528. if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
  529. return -EFAULT;
  530. return 0;
  531. }
  532. static int
  533. snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
  534. {
  535. struct snd_compr_metadata metadata;
  536. int retval;
  537. if (!stream->ops->set_metadata)
  538. return -ENXIO;
  539. /*
  540. * we should allow parameter change only when stream has been
  541. * opened not in other cases
  542. */
  543. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  544. return -EFAULT;
  545. retval = stream->ops->set_metadata(stream, &metadata);
  546. stream->metadata_set = true;
  547. return retval;
  548. }
  549. static inline int
  550. snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
  551. {
  552. struct snd_compr_tstamp tstamp = {0};
  553. int ret;
  554. ret = snd_compr_update_tstamp(stream, &tstamp);
  555. if (ret == 0)
  556. ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
  557. &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
  558. return ret;
  559. }
  560. static int snd_compr_pause(struct snd_compr_stream *stream)
  561. {
  562. int retval;
  563. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  564. return -EPERM;
  565. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  566. if (!retval)
  567. stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
  568. return retval;
  569. }
  570. static int snd_compr_resume(struct snd_compr_stream *stream)
  571. {
  572. int retval;
  573. if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
  574. return -EPERM;
  575. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  576. if (!retval)
  577. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  578. return retval;
  579. }
  580. static int snd_compr_start(struct snd_compr_stream *stream)
  581. {
  582. int retval;
  583. if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
  584. return -EPERM;
  585. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
  586. if (!retval)
  587. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  588. return retval;
  589. }
  590. static int snd_compr_stop(struct snd_compr_stream *stream)
  591. {
  592. int retval;
  593. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  594. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  595. return -EPERM;
  596. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
  597. if (!retval) {
  598. snd_compr_drain_notify(stream);
  599. stream->runtime->total_bytes_available = 0;
  600. stream->runtime->total_bytes_transferred = 0;
  601. }
  602. return retval;
  603. }
  604. static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
  605. {
  606. int ret;
  607. /*
  608. * We are called with lock held. So drop the lock while we wait for
  609. * drain complete notfication from the driver
  610. *
  611. * It is expected that driver will notify the drain completion and then
  612. * stream will be moved to SETUP state, even if draining resulted in an
  613. * error. We can trigger next track after this.
  614. */
  615. stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
  616. mutex_unlock(&stream->device->lock);
  617. /* we wait for drain to complete here, drain can return when
  618. * interruption occurred, wait returned error or success.
  619. * For the first two cases we don't do anything different here and
  620. * return after waking up
  621. */
  622. ret = wait_event_interruptible(stream->runtime->sleep,
  623. (stream->runtime->state != SNDRV_PCM_STATE_DRAINING));
  624. if (ret == -ERESTARTSYS)
  625. pr_debug("wait aborted by a signal");
  626. else if (ret)
  627. pr_debug("wait for drain failed with %d\n", ret);
  628. wake_up(&stream->runtime->sleep);
  629. mutex_lock(&stream->device->lock);
  630. return ret;
  631. }
  632. static int snd_compr_drain(struct snd_compr_stream *stream)
  633. {
  634. int retval;
  635. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  636. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  637. return -EPERM;
  638. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
  639. if (retval) {
  640. pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
  641. wake_up(&stream->runtime->sleep);
  642. return retval;
  643. }
  644. return snd_compress_wait_for_drain(stream);
  645. }
  646. static int snd_compr_next_track(struct snd_compr_stream *stream)
  647. {
  648. int retval;
  649. /* only a running stream can transition to next track */
  650. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  651. return -EPERM;
  652. /* you can signal next track isf this is intended to be a gapless stream
  653. * and current track metadata is set
  654. */
  655. if (stream->metadata_set == false)
  656. return -EPERM;
  657. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
  658. if (retval != 0)
  659. return retval;
  660. stream->metadata_set = false;
  661. stream->next_track = true;
  662. return 0;
  663. }
  664. static int snd_compr_partial_drain(struct snd_compr_stream *stream)
  665. {
  666. int retval;
  667. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  668. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  669. return -EPERM;
  670. /* stream can be drained only when next track has been signalled */
  671. if (stream->next_track == false)
  672. return -EPERM;
  673. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
  674. if (retval) {
  675. pr_debug("Partial drain returned failure\n");
  676. wake_up(&stream->runtime->sleep);
  677. return retval;
  678. }
  679. stream->next_track = false;
  680. return snd_compress_wait_for_drain(stream);
  681. }
  682. static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  683. {
  684. struct snd_compr_file *data = f->private_data;
  685. struct snd_compr_stream *stream;
  686. int retval = -ENOTTY;
  687. if (snd_BUG_ON(!data))
  688. return -EFAULT;
  689. stream = &data->stream;
  690. if (snd_BUG_ON(!stream))
  691. return -EFAULT;
  692. mutex_lock(&stream->device->lock);
  693. switch (_IOC_NR(cmd)) {
  694. case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
  695. retval = put_user(SNDRV_COMPRESS_VERSION,
  696. (int __user *)arg) ? -EFAULT : 0;
  697. break;
  698. case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
  699. retval = snd_compr_get_caps(stream, arg);
  700. break;
  701. #ifndef COMPR_CODEC_CAPS_OVERFLOW
  702. case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
  703. retval = snd_compr_get_codec_caps(stream, arg);
  704. break;
  705. #endif
  706. case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
  707. retval = snd_compr_set_params(stream, arg);
  708. break;
  709. case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
  710. retval = snd_compr_get_params(stream, arg);
  711. break;
  712. case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
  713. retval = snd_compr_set_metadata(stream, arg);
  714. break;
  715. case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
  716. retval = snd_compr_get_metadata(stream, arg);
  717. break;
  718. case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
  719. retval = snd_compr_tstamp(stream, arg);
  720. break;
  721. case _IOC_NR(SNDRV_COMPRESS_AVAIL):
  722. retval = snd_compr_ioctl_avail(stream, arg);
  723. break;
  724. case _IOC_NR(SNDRV_COMPRESS_PAUSE):
  725. retval = snd_compr_pause(stream);
  726. break;
  727. case _IOC_NR(SNDRV_COMPRESS_RESUME):
  728. retval = snd_compr_resume(stream);
  729. break;
  730. case _IOC_NR(SNDRV_COMPRESS_START):
  731. retval = snd_compr_start(stream);
  732. break;
  733. case _IOC_NR(SNDRV_COMPRESS_STOP):
  734. retval = snd_compr_stop(stream);
  735. break;
  736. case _IOC_NR(SNDRV_COMPRESS_DRAIN):
  737. retval = snd_compr_drain(stream);
  738. break;
  739. case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
  740. retval = snd_compr_partial_drain(stream);
  741. break;
  742. case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
  743. retval = snd_compr_next_track(stream);
  744. break;
  745. }
  746. mutex_unlock(&stream->device->lock);
  747. return retval;
  748. }
  749. /* support of 32bit userspace on 64bit platforms */
  750. #ifdef CONFIG_COMPAT
  751. static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
  752. unsigned long arg)
  753. {
  754. return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  755. }
  756. #endif
  757. static const struct file_operations snd_compr_file_ops = {
  758. .owner = THIS_MODULE,
  759. .open = snd_compr_open,
  760. .release = snd_compr_free,
  761. .write = snd_compr_write,
  762. .read = snd_compr_read,
  763. .unlocked_ioctl = snd_compr_ioctl,
  764. #ifdef CONFIG_COMPAT
  765. .compat_ioctl = snd_compr_ioctl_compat,
  766. #endif
  767. .mmap = snd_compr_mmap,
  768. .poll = snd_compr_poll,
  769. };
  770. static int snd_compress_dev_register(struct snd_device *device)
  771. {
  772. int ret = -EINVAL;
  773. char str[16];
  774. struct snd_compr *compr;
  775. if (snd_BUG_ON(!device || !device->device_data))
  776. return -EBADFD;
  777. compr = device->device_data;
  778. pr_debug("reg %s for device %s, direction %d\n", str, compr->name,
  779. compr->direction);
  780. /* register compressed device */
  781. ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
  782. compr->card, compr->device,
  783. &snd_compr_file_ops, compr, &compr->dev);
  784. if (ret < 0) {
  785. pr_err("snd_register_device failed\n %d", ret);
  786. return ret;
  787. }
  788. return ret;
  789. }
  790. static int snd_compress_dev_disconnect(struct snd_device *device)
  791. {
  792. struct snd_compr *compr;
  793. compr = device->device_data;
  794. snd_unregister_device(&compr->dev);
  795. return 0;
  796. }
  797. #ifdef CONFIG_SND_VERBOSE_PROCFS
  798. static void snd_compress_proc_info_read(struct snd_info_entry *entry,
  799. struct snd_info_buffer *buffer)
  800. {
  801. struct snd_compr *compr = (struct snd_compr *)entry->private_data;
  802. snd_iprintf(buffer, "card: %d\n", compr->card->number);
  803. snd_iprintf(buffer, "device: %d\n", compr->device);
  804. snd_iprintf(buffer, "stream: %s\n",
  805. compr->direction == SND_COMPRESS_PLAYBACK
  806. ? "PLAYBACK" : "CAPTURE");
  807. snd_iprintf(buffer, "id: %s\n", compr->id);
  808. }
  809. static int snd_compress_proc_init(struct snd_compr *compr)
  810. {
  811. struct snd_info_entry *entry;
  812. char name[16];
  813. sprintf(name, "compr%i", compr->device);
  814. entry = snd_info_create_card_entry(compr->card, name,
  815. compr->card->proc_root);
  816. if (!entry)
  817. return -ENOMEM;
  818. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  819. if (snd_info_register(entry) < 0) {
  820. snd_info_free_entry(entry);
  821. return -ENOMEM;
  822. }
  823. compr->proc_root = entry;
  824. entry = snd_info_create_card_entry(compr->card, "info",
  825. compr->proc_root);
  826. if (entry) {
  827. snd_info_set_text_ops(entry, compr,
  828. snd_compress_proc_info_read);
  829. if (snd_info_register(entry) < 0) {
  830. snd_info_free_entry(entry);
  831. entry = NULL;
  832. }
  833. }
  834. compr->proc_info_entry = entry;
  835. return 0;
  836. }
  837. static void snd_compress_proc_done(struct snd_compr *compr)
  838. {
  839. snd_info_free_entry(compr->proc_info_entry);
  840. compr->proc_info_entry = NULL;
  841. snd_info_free_entry(compr->proc_root);
  842. compr->proc_root = NULL;
  843. }
  844. static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
  845. {
  846. strlcpy(compr->id, id, sizeof(compr->id));
  847. }
  848. #else
  849. static inline int snd_compress_proc_init(struct snd_compr *compr)
  850. {
  851. return 0;
  852. }
  853. static inline void snd_compress_proc_done(struct snd_compr *compr)
  854. {
  855. }
  856. static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
  857. {
  858. }
  859. #endif
  860. static int snd_compress_dev_free(struct snd_device *device)
  861. {
  862. struct snd_compr *compr;
  863. compr = device->device_data;
  864. snd_compress_proc_done(compr);
  865. put_device(&compr->dev);
  866. return 0;
  867. }
  868. /*
  869. * snd_compress_new: create new compress device
  870. * @card: sound card pointer
  871. * @device: device number
  872. * @dirn: device direction, should be of type enum snd_compr_direction
  873. * @compr: compress device pointer
  874. */
  875. int snd_compress_new(struct snd_card *card, int device,
  876. int dirn, const char *id, struct snd_compr *compr)
  877. {
  878. static struct snd_device_ops ops = {
  879. .dev_free = snd_compress_dev_free,
  880. .dev_register = snd_compress_dev_register,
  881. .dev_disconnect = snd_compress_dev_disconnect,
  882. };
  883. int ret;
  884. compr->card = card;
  885. compr->device = device;
  886. compr->direction = dirn;
  887. snd_compress_set_id(compr, id);
  888. snd_device_initialize(&compr->dev, card);
  889. dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
  890. ret = snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
  891. if (ret == 0)
  892. snd_compress_proc_init(compr);
  893. return ret;
  894. }
  895. EXPORT_SYMBOL_GPL(snd_compress_new);
  896. static int snd_compress_add_device(struct snd_compr *device)
  897. {
  898. int ret;
  899. if (!device->card)
  900. return -EINVAL;
  901. /* register the card */
  902. ret = snd_card_register(device->card);
  903. if (ret)
  904. goto out;
  905. return 0;
  906. out:
  907. pr_err("failed with %d\n", ret);
  908. return ret;
  909. }
  910. static int snd_compress_remove_device(struct snd_compr *device)
  911. {
  912. return snd_card_free(device->card);
  913. }
  914. /**
  915. * snd_compress_register - register compressed device
  916. *
  917. * @device: compressed device to register
  918. */
  919. int snd_compress_register(struct snd_compr *device)
  920. {
  921. int retval;
  922. if (device->name == NULL || device->ops == NULL)
  923. return -EINVAL;
  924. pr_debug("Registering compressed device %s\n", device->name);
  925. if (snd_BUG_ON(!device->ops->open))
  926. return -EINVAL;
  927. if (snd_BUG_ON(!device->ops->free))
  928. return -EINVAL;
  929. if (snd_BUG_ON(!device->ops->set_params))
  930. return -EINVAL;
  931. if (snd_BUG_ON(!device->ops->trigger))
  932. return -EINVAL;
  933. mutex_init(&device->lock);
  934. /* register a compressed card */
  935. mutex_lock(&device_mutex);
  936. retval = snd_compress_add_device(device);
  937. mutex_unlock(&device_mutex);
  938. return retval;
  939. }
  940. EXPORT_SYMBOL_GPL(snd_compress_register);
  941. int snd_compress_deregister(struct snd_compr *device)
  942. {
  943. pr_debug("Removing compressed device %s\n", device->name);
  944. mutex_lock(&device_mutex);
  945. snd_compress_remove_device(device);
  946. mutex_unlock(&device_mutex);
  947. return 0;
  948. }
  949. EXPORT_SYMBOL_GPL(snd_compress_deregister);
  950. static int __init snd_compress_init(void)
  951. {
  952. return 0;
  953. }
  954. static void __exit snd_compress_exit(void)
  955. {
  956. }
  957. module_init(snd_compress_init);
  958. module_exit(snd_compress_exit);
  959. MODULE_DESCRIPTION("ALSA Compressed offload framework");
  960. MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
  961. MODULE_LICENSE("GPL v2");