compress_offload.c 27 KB

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