compress_offload.c 26 KB

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