compress_offload.c 26 KB

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