compress_offload.c 30 KB

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