compress_offload.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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 > U32_MAX / params->buffer.fragment_size ||
  467. params->buffer.fragments == 0)
  468. return -EINVAL;
  469. /* now codec parameters */
  470. if (params->codec.id == 0 || params->codec.id > SND_AUDIOCODEC_MAX)
  471. return -EINVAL;
  472. if (params->codec.ch_in == 0 || params->codec.ch_out == 0)
  473. return -EINVAL;
  474. return 0;
  475. }
  476. static int
  477. snd_compr_set_params(struct snd_compr_stream *stream, unsigned long arg)
  478. {
  479. struct snd_compr_params *params;
  480. int retval;
  481. if (stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  482. /*
  483. * we should allow parameter change only when stream has been
  484. * opened not in other cases
  485. */
  486. params = memdup_user((void __user *)arg, sizeof(*params));
  487. if (IS_ERR(params))
  488. return PTR_ERR(params);
  489. retval = snd_compress_check_input(params);
  490. if (retval)
  491. goto out;
  492. retval = snd_compr_allocate_buffer(stream, params);
  493. if (retval) {
  494. retval = -ENOMEM;
  495. goto out;
  496. }
  497. retval = stream->ops->set_params(stream, params);
  498. if (retval)
  499. goto out;
  500. stream->metadata_set = false;
  501. stream->next_track = false;
  502. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  503. } else {
  504. return -EPERM;
  505. }
  506. out:
  507. kfree(params);
  508. return retval;
  509. }
  510. static int
  511. snd_compr_get_params(struct snd_compr_stream *stream, unsigned long arg)
  512. {
  513. struct snd_codec *params;
  514. int retval;
  515. if (!stream->ops->get_params)
  516. return -EBADFD;
  517. params = kzalloc(sizeof(*params), GFP_KERNEL);
  518. if (!params)
  519. return -ENOMEM;
  520. retval = stream->ops->get_params(stream, params);
  521. if (retval)
  522. goto out;
  523. if (copy_to_user((char __user *)arg, params, sizeof(*params)))
  524. retval = -EFAULT;
  525. out:
  526. kfree(params);
  527. return retval;
  528. }
  529. static int
  530. snd_compr_get_metadata(struct snd_compr_stream *stream, unsigned long arg)
  531. {
  532. struct snd_compr_metadata metadata;
  533. int retval;
  534. if (!stream->ops->get_metadata)
  535. return -ENXIO;
  536. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  537. return -EFAULT;
  538. retval = stream->ops->get_metadata(stream, &metadata);
  539. if (retval != 0)
  540. return retval;
  541. if (copy_to_user((void __user *)arg, &metadata, sizeof(metadata)))
  542. return -EFAULT;
  543. return 0;
  544. }
  545. static int
  546. snd_compr_set_metadata(struct snd_compr_stream *stream, unsigned long arg)
  547. {
  548. struct snd_compr_metadata metadata;
  549. int retval;
  550. if (!stream->ops->set_metadata)
  551. return -ENXIO;
  552. /*
  553. * we should allow parameter change only when stream has been
  554. * opened not in other cases
  555. */
  556. if (copy_from_user(&metadata, (void __user *)arg, sizeof(metadata)))
  557. return -EFAULT;
  558. retval = stream->ops->set_metadata(stream, &metadata);
  559. stream->metadata_set = true;
  560. return retval;
  561. }
  562. static inline int
  563. snd_compr_tstamp(struct snd_compr_stream *stream, unsigned long arg)
  564. {
  565. struct snd_compr_tstamp tstamp = {0};
  566. int ret;
  567. ret = snd_compr_update_tstamp(stream, &tstamp);
  568. if (ret == 0)
  569. ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
  570. &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
  571. return ret;
  572. }
  573. static int snd_compr_pause(struct snd_compr_stream *stream)
  574. {
  575. int retval;
  576. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  577. return -EPERM;
  578. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  579. if (!retval)
  580. stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
  581. return retval;
  582. }
  583. static int snd_compr_resume(struct snd_compr_stream *stream)
  584. {
  585. int retval;
  586. if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
  587. return -EPERM;
  588. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  589. if (!retval)
  590. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  591. return retval;
  592. }
  593. static int snd_compr_start(struct snd_compr_stream *stream)
  594. {
  595. int retval;
  596. switch (stream->runtime->state) {
  597. case SNDRV_PCM_STATE_SETUP:
  598. if (stream->direction != SND_COMPRESS_CAPTURE)
  599. return -EPERM;
  600. break;
  601. case SNDRV_PCM_STATE_PREPARED:
  602. break;
  603. default:
  604. return -EPERM;
  605. }
  606. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
  607. if (!retval)
  608. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  609. return retval;
  610. }
  611. static int snd_compr_stop(struct snd_compr_stream *stream)
  612. {
  613. int retval;
  614. switch (stream->runtime->state) {
  615. case SNDRV_PCM_STATE_OPEN:
  616. case SNDRV_PCM_STATE_SETUP:
  617. case SNDRV_PCM_STATE_PREPARED:
  618. return -EPERM;
  619. default:
  620. break;
  621. }
  622. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
  623. if (!retval) {
  624. /* clear flags and stop any drain wait */
  625. stream->partial_drain = false;
  626. stream->metadata_set = false;
  627. snd_compr_drain_notify(stream);
  628. stream->runtime->total_bytes_available = 0;
  629. stream->runtime->total_bytes_transferred = 0;
  630. }
  631. return retval;
  632. }
  633. static void error_delayed_work(struct work_struct *work)
  634. {
  635. struct snd_compr_stream *stream;
  636. stream = container_of(work, struct snd_compr_stream, error_work.work);
  637. mutex_lock(&stream->device->lock);
  638. stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
  639. wake_up(&stream->runtime->sleep);
  640. mutex_unlock(&stream->device->lock);
  641. }
  642. /*
  643. * snd_compr_stop_error: Report a fatal error on a stream
  644. * @stream: pointer to stream
  645. * @state: state to transition the stream to
  646. *
  647. * Stop the stream and set its state.
  648. *
  649. * Should be called with compressed device lock held.
  650. */
  651. int snd_compr_stop_error(struct snd_compr_stream *stream,
  652. snd_pcm_state_t state)
  653. {
  654. if (stream->runtime->state == state)
  655. return 0;
  656. stream->runtime->state = state;
  657. pr_debug("Changing state to: %d\n", state);
  658. queue_delayed_work(system_power_efficient_wq, &stream->error_work, 0);
  659. return 0;
  660. }
  661. EXPORT_SYMBOL_GPL(snd_compr_stop_error);
  662. static int snd_compress_wait_for_drain(struct snd_compr_stream *stream)
  663. {
  664. int ret;
  665. /*
  666. * We are called with lock held. So drop the lock while we wait for
  667. * drain complete notification from the driver
  668. *
  669. * It is expected that driver will notify the drain completion and then
  670. * stream will be moved to SETUP state, even if draining resulted in an
  671. * error. We can trigger next track after this.
  672. */
  673. stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
  674. mutex_unlock(&stream->device->lock);
  675. /* we wait for drain to complete here, drain can return when
  676. * interruption occurred, wait returned error or success.
  677. * For the first two cases we don't do anything different here and
  678. * return after waking up
  679. */
  680. ret = wait_event_interruptible(stream->runtime->sleep,
  681. (stream->runtime->state != SNDRV_PCM_STATE_DRAINING));
  682. if (ret == -ERESTARTSYS)
  683. pr_debug("wait aborted by a signal\n");
  684. else if (ret)
  685. pr_debug("wait for drain failed with %d\n", ret);
  686. wake_up(&stream->runtime->sleep);
  687. mutex_lock(&stream->device->lock);
  688. return ret;
  689. }
  690. static int snd_compr_drain(struct snd_compr_stream *stream)
  691. {
  692. int retval;
  693. switch (stream->runtime->state) {
  694. case SNDRV_PCM_STATE_OPEN:
  695. case SNDRV_PCM_STATE_SETUP:
  696. case SNDRV_PCM_STATE_PREPARED:
  697. case SNDRV_PCM_STATE_PAUSED:
  698. return -EPERM;
  699. case SNDRV_PCM_STATE_XRUN:
  700. return -EPIPE;
  701. default:
  702. break;
  703. }
  704. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
  705. if (retval) {
  706. pr_debug("SND_COMPR_TRIGGER_DRAIN failed %d\n", retval);
  707. wake_up(&stream->runtime->sleep);
  708. return retval;
  709. }
  710. return snd_compress_wait_for_drain(stream);
  711. }
  712. static int snd_compr_next_track(struct snd_compr_stream *stream)
  713. {
  714. int retval;
  715. /* only a running stream can transition to next track */
  716. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  717. return -EPERM;
  718. /* next track doesn't have any meaning for capture streams */
  719. if (stream->direction == SND_COMPRESS_CAPTURE)
  720. return -EPERM;
  721. /* you can signal next track if this is intended to be a gapless stream
  722. * and current track metadata is set
  723. */
  724. if (stream->metadata_set == false)
  725. return -EPERM;
  726. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
  727. if (retval != 0)
  728. return retval;
  729. stream->metadata_set = false;
  730. stream->next_track = true;
  731. return 0;
  732. }
  733. static int snd_compr_partial_drain(struct snd_compr_stream *stream)
  734. {
  735. int retval;
  736. switch (stream->runtime->state) {
  737. case SNDRV_PCM_STATE_OPEN:
  738. case SNDRV_PCM_STATE_SETUP:
  739. case SNDRV_PCM_STATE_PREPARED:
  740. case SNDRV_PCM_STATE_PAUSED:
  741. return -EPERM;
  742. case SNDRV_PCM_STATE_XRUN:
  743. return -EPIPE;
  744. default:
  745. break;
  746. }
  747. /* partial drain doesn't have any meaning for capture streams */
  748. if (stream->direction == SND_COMPRESS_CAPTURE)
  749. return -EPERM;
  750. /* stream can be drained only when next track has been signalled */
  751. if (stream->next_track == false)
  752. return -EPERM;
  753. stream->partial_drain = true;
  754. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
  755. if (retval) {
  756. pr_debug("Partial drain returned failure\n");
  757. wake_up(&stream->runtime->sleep);
  758. return retval;
  759. }
  760. stream->next_track = false;
  761. return snd_compress_wait_for_drain(stream);
  762. }
  763. static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  764. {
  765. struct snd_compr_file *data = f->private_data;
  766. struct snd_compr_stream *stream;
  767. int retval = -ENOTTY;
  768. if (snd_BUG_ON(!data))
  769. return -EFAULT;
  770. stream = &data->stream;
  771. mutex_lock(&stream->device->lock);
  772. switch (_IOC_NR(cmd)) {
  773. case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
  774. retval = put_user(SNDRV_COMPRESS_VERSION,
  775. (int __user *)arg) ? -EFAULT : 0;
  776. break;
  777. case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
  778. retval = snd_compr_get_caps(stream, arg);
  779. break;
  780. #ifndef COMPR_CODEC_CAPS_OVERFLOW
  781. case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
  782. retval = snd_compr_get_codec_caps(stream, arg);
  783. break;
  784. #endif
  785. case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
  786. retval = snd_compr_set_params(stream, arg);
  787. break;
  788. case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
  789. retval = snd_compr_get_params(stream, arg);
  790. break;
  791. case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
  792. retval = snd_compr_set_metadata(stream, arg);
  793. break;
  794. case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
  795. retval = snd_compr_get_metadata(stream, arg);
  796. break;
  797. case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
  798. retval = snd_compr_tstamp(stream, arg);
  799. break;
  800. case _IOC_NR(SNDRV_COMPRESS_AVAIL):
  801. retval = snd_compr_ioctl_avail(stream, arg);
  802. break;
  803. case _IOC_NR(SNDRV_COMPRESS_PAUSE):
  804. retval = snd_compr_pause(stream);
  805. break;
  806. case _IOC_NR(SNDRV_COMPRESS_RESUME):
  807. retval = snd_compr_resume(stream);
  808. break;
  809. case _IOC_NR(SNDRV_COMPRESS_START):
  810. retval = snd_compr_start(stream);
  811. break;
  812. case _IOC_NR(SNDRV_COMPRESS_STOP):
  813. retval = snd_compr_stop(stream);
  814. break;
  815. case _IOC_NR(SNDRV_COMPRESS_DRAIN):
  816. retval = snd_compr_drain(stream);
  817. break;
  818. case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
  819. retval = snd_compr_partial_drain(stream);
  820. break;
  821. case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
  822. retval = snd_compr_next_track(stream);
  823. break;
  824. }
  825. mutex_unlock(&stream->device->lock);
  826. return retval;
  827. }
  828. /* support of 32bit userspace on 64bit platforms */
  829. #ifdef CONFIG_COMPAT
  830. static long snd_compr_ioctl_compat(struct file *file, unsigned int cmd,
  831. unsigned long arg)
  832. {
  833. return snd_compr_ioctl(file, cmd, (unsigned long)compat_ptr(arg));
  834. }
  835. #endif
  836. static const struct file_operations snd_compr_file_ops = {
  837. .owner = THIS_MODULE,
  838. .open = snd_compr_open,
  839. .release = snd_compr_free,
  840. .write = snd_compr_write,
  841. .read = snd_compr_read,
  842. .unlocked_ioctl = snd_compr_ioctl,
  843. #ifdef CONFIG_COMPAT
  844. .compat_ioctl = snd_compr_ioctl_compat,
  845. #endif
  846. .mmap = snd_compr_mmap,
  847. .poll = snd_compr_poll,
  848. };
  849. static int snd_compress_dev_register(struct snd_device *device)
  850. {
  851. int ret = -EINVAL;
  852. struct snd_compr *compr;
  853. if (snd_BUG_ON(!device || !device->device_data))
  854. return -EBADFD;
  855. compr = device->device_data;
  856. pr_debug("reg device %s, direction %d\n", compr->name,
  857. compr->direction);
  858. /* register compressed device */
  859. ret = snd_register_device(SNDRV_DEVICE_TYPE_COMPRESS,
  860. compr->card, compr->device,
  861. &snd_compr_file_ops, compr, &compr->dev);
  862. if (ret < 0) {
  863. pr_err("snd_register_device failed %d\n", ret);
  864. return ret;
  865. }
  866. return ret;
  867. }
  868. static int snd_compress_dev_disconnect(struct snd_device *device)
  869. {
  870. struct snd_compr *compr;
  871. compr = device->device_data;
  872. snd_unregister_device(&compr->dev);
  873. return 0;
  874. }
  875. #ifdef CONFIG_SND_VERBOSE_PROCFS
  876. static void snd_compress_proc_info_read(struct snd_info_entry *entry,
  877. struct snd_info_buffer *buffer)
  878. {
  879. struct snd_compr *compr = (struct snd_compr *)entry->private_data;
  880. snd_iprintf(buffer, "card: %d\n", compr->card->number);
  881. snd_iprintf(buffer, "device: %d\n", compr->device);
  882. snd_iprintf(buffer, "stream: %s\n",
  883. compr->direction == SND_COMPRESS_PLAYBACK
  884. ? "PLAYBACK" : "CAPTURE");
  885. snd_iprintf(buffer, "id: %s\n", compr->id);
  886. }
  887. static int snd_compress_proc_init(struct snd_compr *compr)
  888. {
  889. struct snd_info_entry *entry;
  890. char name[16];
  891. sprintf(name, "compr%i", compr->device);
  892. entry = snd_info_create_card_entry(compr->card, name,
  893. compr->card->proc_root);
  894. if (!entry)
  895. return -ENOMEM;
  896. entry->mode = S_IFDIR | S_IRUGO | S_IXUGO;
  897. if (snd_info_register(entry) < 0) {
  898. snd_info_free_entry(entry);
  899. return -ENOMEM;
  900. }
  901. compr->proc_root = entry;
  902. entry = snd_info_create_card_entry(compr->card, "info",
  903. compr->proc_root);
  904. if (entry) {
  905. snd_info_set_text_ops(entry, compr,
  906. snd_compress_proc_info_read);
  907. if (snd_info_register(entry) < 0) {
  908. snd_info_free_entry(entry);
  909. entry = NULL;
  910. }
  911. }
  912. compr->proc_info_entry = entry;
  913. return 0;
  914. }
  915. static void snd_compress_proc_done(struct snd_compr *compr)
  916. {
  917. snd_info_free_entry(compr->proc_info_entry);
  918. compr->proc_info_entry = NULL;
  919. snd_info_free_entry(compr->proc_root);
  920. compr->proc_root = NULL;
  921. }
  922. static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
  923. {
  924. strlcpy(compr->id, id, sizeof(compr->id));
  925. }
  926. #else
  927. static inline int snd_compress_proc_init(struct snd_compr *compr)
  928. {
  929. return 0;
  930. }
  931. static inline void snd_compress_proc_done(struct snd_compr *compr)
  932. {
  933. }
  934. static inline void snd_compress_set_id(struct snd_compr *compr, const char *id)
  935. {
  936. }
  937. #endif
  938. static int snd_compress_dev_free(struct snd_device *device)
  939. {
  940. struct snd_compr *compr;
  941. compr = device->device_data;
  942. snd_compress_proc_done(compr);
  943. put_device(&compr->dev);
  944. return 0;
  945. }
  946. /*
  947. * snd_compress_new: create new compress device
  948. * @card: sound card pointer
  949. * @device: device number
  950. * @dirn: device direction, should be of type enum snd_compr_direction
  951. * @compr: compress device pointer
  952. */
  953. int snd_compress_new(struct snd_card *card, int device,
  954. int dirn, const char *id, struct snd_compr *compr)
  955. {
  956. static struct snd_device_ops ops = {
  957. .dev_free = snd_compress_dev_free,
  958. .dev_register = snd_compress_dev_register,
  959. .dev_disconnect = snd_compress_dev_disconnect,
  960. };
  961. int ret;
  962. compr->card = card;
  963. compr->device = device;
  964. compr->direction = dirn;
  965. snd_compress_set_id(compr, id);
  966. snd_device_initialize(&compr->dev, card);
  967. dev_set_name(&compr->dev, "comprC%iD%i", card->number, device);
  968. ret = snd_device_new(card, SNDRV_DEV_COMPRESS, compr, &ops);
  969. if (ret == 0)
  970. snd_compress_proc_init(compr);
  971. return ret;
  972. }
  973. EXPORT_SYMBOL_GPL(snd_compress_new);
  974. static int snd_compress_add_device(struct snd_compr *device)
  975. {
  976. int ret;
  977. if (!device->card)
  978. return -EINVAL;
  979. /* register the card */
  980. ret = snd_card_register(device->card);
  981. if (ret)
  982. goto out;
  983. return 0;
  984. out:
  985. pr_err("failed with %d\n", ret);
  986. return ret;
  987. }
  988. static int snd_compress_remove_device(struct snd_compr *device)
  989. {
  990. return snd_card_free(device->card);
  991. }
  992. /**
  993. * snd_compress_register - register compressed device
  994. *
  995. * @device: compressed device to register
  996. */
  997. int snd_compress_register(struct snd_compr *device)
  998. {
  999. int retval;
  1000. if (device->name == NULL || device->ops == NULL)
  1001. return -EINVAL;
  1002. pr_debug("Registering compressed device %s\n", device->name);
  1003. if (snd_BUG_ON(!device->ops->open))
  1004. return -EINVAL;
  1005. if (snd_BUG_ON(!device->ops->free))
  1006. return -EINVAL;
  1007. if (snd_BUG_ON(!device->ops->set_params))
  1008. return -EINVAL;
  1009. if (snd_BUG_ON(!device->ops->trigger))
  1010. return -EINVAL;
  1011. mutex_init(&device->lock);
  1012. /* register a compressed card */
  1013. mutex_lock(&device_mutex);
  1014. retval = snd_compress_add_device(device);
  1015. mutex_unlock(&device_mutex);
  1016. return retval;
  1017. }
  1018. EXPORT_SYMBOL_GPL(snd_compress_register);
  1019. int snd_compress_deregister(struct snd_compr *device)
  1020. {
  1021. pr_debug("Removing compressed device %s\n", device->name);
  1022. mutex_lock(&device_mutex);
  1023. snd_compress_remove_device(device);
  1024. mutex_unlock(&device_mutex);
  1025. return 0;
  1026. }
  1027. EXPORT_SYMBOL_GPL(snd_compress_deregister);
  1028. static int __init snd_compress_init(void)
  1029. {
  1030. return 0;
  1031. }
  1032. static void __exit snd_compress_exit(void)
  1033. {
  1034. }
  1035. module_init(snd_compress_init);
  1036. module_exit(snd_compress_exit);
  1037. MODULE_DESCRIPTION("ALSA Compressed offload framework");
  1038. MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
  1039. MODULE_LICENSE("GPL v2");