compress_offload.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. #define U32_MAX ((u32)~0U)
  46. /* TODO:
  47. * - add substream support for multiple devices in case of
  48. * SND_DYNAMIC_MINORS is not used
  49. * - Multiple node representation
  50. * driver should be able to register multiple nodes
  51. */
  52. static DEFINE_MUTEX(device_mutex);
  53. struct snd_compr_file {
  54. unsigned long caps;
  55. struct snd_compr_stream stream;
  56. };
  57. /*
  58. * a note on stream states used:
  59. * we use follwing states in the compressed core
  60. * SNDRV_PCM_STATE_OPEN: When stream has been opened.
  61. * SNDRV_PCM_STATE_SETUP: When stream has been initialized. This is done by
  62. * calling SNDRV_COMPRESS_SET_PARAMS. running streams will come to this
  63. * state at stop by calling SNDRV_COMPRESS_STOP, or at end of drain.
  64. * SNDRV_PCM_STATE_RUNNING: When stream has been started and is
  65. * decoding/encoding and rendering/capturing data.
  66. * SNDRV_PCM_STATE_DRAINING: When stream is draining current data. This is done
  67. * by calling SNDRV_COMPRESS_DRAIN.
  68. * SNDRV_PCM_STATE_PAUSED: When stream is paused. This is done by calling
  69. * SNDRV_COMPRESS_PAUSE. It can be stopped or resumed by calling
  70. * SNDRV_COMPRESS_STOP or SNDRV_COMPRESS_RESUME respectively.
  71. */
  72. static int snd_compr_open(struct inode *inode, struct file *f)
  73. {
  74. struct snd_compr *compr;
  75. struct snd_compr_file *data;
  76. struct snd_compr_runtime *runtime;
  77. enum snd_compr_direction dirn;
  78. int maj = imajor(inode);
  79. int ret;
  80. if ((f->f_flags & O_ACCMODE) == O_WRONLY)
  81. dirn = SND_COMPRESS_PLAYBACK;
  82. else if ((f->f_flags & O_ACCMODE) == O_RDONLY)
  83. dirn = SND_COMPRESS_CAPTURE;
  84. else
  85. return -EINVAL;
  86. if (maj == snd_major)
  87. compr = snd_lookup_minor_data(iminor(inode),
  88. SNDRV_DEVICE_TYPE_COMPRESS);
  89. else
  90. return -EBADFD;
  91. if (compr == NULL) {
  92. pr_err("no device data!!!\n");
  93. return -ENODEV;
  94. }
  95. if (dirn != compr->direction) {
  96. pr_err("this device doesn't support this direction\n");
  97. snd_card_unref(compr->card);
  98. return -EINVAL;
  99. }
  100. data = kzalloc(sizeof(*data), GFP_KERNEL);
  101. if (!data) {
  102. snd_card_unref(compr->card);
  103. return -ENOMEM;
  104. }
  105. data->stream.ops = compr->ops;
  106. data->stream.direction = dirn;
  107. data->stream.private_data = compr->private_data;
  108. data->stream.device = compr;
  109. runtime = kzalloc(sizeof(*runtime), GFP_KERNEL);
  110. if (!runtime) {
  111. kfree(data);
  112. snd_card_unref(compr->card);
  113. return -ENOMEM;
  114. }
  115. runtime->state = SNDRV_PCM_STATE_OPEN;
  116. init_waitqueue_head(&runtime->sleep);
  117. data->stream.runtime = runtime;
  118. f->private_data = (void *)data;
  119. mutex_lock(&compr->lock);
  120. ret = compr->ops->open(&data->stream);
  121. mutex_unlock(&compr->lock);
  122. if (ret) {
  123. kfree(runtime);
  124. kfree(data);
  125. }
  126. snd_card_unref(compr->card);
  127. return ret;
  128. }
  129. static int snd_compr_free(struct inode *inode, struct file *f)
  130. {
  131. struct snd_compr_file *data = f->private_data;
  132. struct snd_compr_runtime *runtime = data->stream.runtime;
  133. switch (runtime->state) {
  134. case SNDRV_PCM_STATE_RUNNING:
  135. case SNDRV_PCM_STATE_DRAINING:
  136. case SNDRV_PCM_STATE_PAUSED:
  137. data->stream.ops->trigger(&data->stream, SNDRV_PCM_TRIGGER_STOP);
  138. break;
  139. default:
  140. break;
  141. }
  142. data->stream.ops->free(&data->stream);
  143. kfree(data->stream.runtime->buffer);
  144. kfree(data->stream.runtime);
  145. kfree(data);
  146. return 0;
  147. }
  148. static int snd_compr_update_tstamp(struct snd_compr_stream *stream,
  149. struct snd_compr_tstamp *tstamp)
  150. {
  151. int err = 0;
  152. if (!stream->ops->pointer)
  153. return -ENOTSUPP;
  154. err = stream->ops->pointer(stream, tstamp);
  155. if (err)
  156. return err;
  157. pr_debug("dsp consumed till %d total %d bytes\n",
  158. tstamp->byte_offset, tstamp->copied_total);
  159. if (stream->direction == SND_COMPRESS_PLAYBACK)
  160. stream->runtime->total_bytes_transferred = tstamp->copied_total;
  161. else
  162. stream->runtime->total_bytes_available = tstamp->copied_total;
  163. return 0;
  164. }
  165. static size_t snd_compr_calc_avail(struct snd_compr_stream *stream,
  166. struct snd_compr_avail *avail)
  167. {
  168. memset(avail, 0, sizeof(*avail));
  169. snd_compr_update_tstamp(stream, &avail->tstamp);
  170. /* Still need to return avail even if tstamp can't be filled in */
  171. if (stream->runtime->total_bytes_available == 0 &&
  172. stream->runtime->state == SNDRV_PCM_STATE_SETUP &&
  173. stream->direction == SND_COMPRESS_PLAYBACK) {
  174. pr_debug("detected init and someone forgot to do a write\n");
  175. return stream->runtime->buffer_size;
  176. }
  177. pr_debug("app wrote %lld, DSP consumed %lld\n",
  178. stream->runtime->total_bytes_available,
  179. stream->runtime->total_bytes_transferred);
  180. if (stream->runtime->total_bytes_available ==
  181. stream->runtime->total_bytes_transferred) {
  182. if (stream->direction == SND_COMPRESS_PLAYBACK) {
  183. pr_debug("both pointers are same, returning full avail\n");
  184. return stream->runtime->buffer_size;
  185. } else {
  186. pr_debug("both pointers are same, returning no avail\n");
  187. return 0;
  188. }
  189. }
  190. avail->avail = stream->runtime->total_bytes_available -
  191. stream->runtime->total_bytes_transferred;
  192. if (stream->direction == SND_COMPRESS_PLAYBACK)
  193. avail->avail = stream->runtime->buffer_size - avail->avail;
  194. pr_debug("ret avail as %lld\n", avail->avail);
  195. return avail->avail;
  196. }
  197. static inline size_t snd_compr_get_avail(struct snd_compr_stream *stream)
  198. {
  199. struct snd_compr_avail avail;
  200. return snd_compr_calc_avail(stream, &avail);
  201. }
  202. static int
  203. snd_compr_ioctl_avail(struct snd_compr_stream *stream, unsigned long arg)
  204. {
  205. struct snd_compr_avail ioctl_avail;
  206. size_t avail;
  207. avail = snd_compr_calc_avail(stream, &ioctl_avail);
  208. ioctl_avail.avail = avail;
  209. if (copy_to_user((__u64 __user *)arg,
  210. &ioctl_avail, sizeof(ioctl_avail)))
  211. return -EFAULT;
  212. return 0;
  213. }
  214. static int snd_compr_write_data(struct snd_compr_stream *stream,
  215. const char __user *buf, size_t count)
  216. {
  217. void *dstn;
  218. size_t copy;
  219. struct snd_compr_runtime *runtime = stream->runtime;
  220. /* 64-bit Modulus */
  221. u64 app_pointer = div64_u64(runtime->total_bytes_available,
  222. runtime->buffer_size);
  223. app_pointer = runtime->total_bytes_available -
  224. (app_pointer * runtime->buffer_size);
  225. dstn = runtime->buffer + app_pointer;
  226. pr_debug("copying %ld at %lld\n",
  227. (unsigned long)count, app_pointer);
  228. if (count < runtime->buffer_size - app_pointer) {
  229. if (copy_from_user(dstn, buf, count))
  230. return -EFAULT;
  231. } else {
  232. copy = runtime->buffer_size - app_pointer;
  233. if (copy_from_user(dstn, buf, copy))
  234. return -EFAULT;
  235. if (copy_from_user(runtime->buffer, buf + copy, count - copy))
  236. return -EFAULT;
  237. }
  238. /* if DSP cares, let it know data has been written */
  239. if (stream->ops->ack)
  240. stream->ops->ack(stream, count);
  241. return count;
  242. }
  243. static ssize_t snd_compr_write(struct file *f, const char __user *buf,
  244. size_t count, loff_t *offset)
  245. {
  246. struct snd_compr_file *data = f->private_data;
  247. struct snd_compr_stream *stream;
  248. size_t avail;
  249. int retval;
  250. if (snd_BUG_ON(!data))
  251. return -EFAULT;
  252. stream = &data->stream;
  253. mutex_lock(&stream->device->lock);
  254. /* write is allowed when stream is running or has been steup */
  255. if (stream->runtime->state != SNDRV_PCM_STATE_SETUP &&
  256. stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
  257. mutex_unlock(&stream->device->lock);
  258. return -EBADFD;
  259. }
  260. avail = snd_compr_get_avail(stream);
  261. pr_debug("avail returned %ld\n", (unsigned long)avail);
  262. /* calculate how much we can write to buffer */
  263. if (avail > count)
  264. avail = count;
  265. if (stream->ops->copy) {
  266. char __user* cbuf = (char __user*)buf;
  267. retval = stream->ops->copy(stream, cbuf, avail);
  268. } else {
  269. retval = snd_compr_write_data(stream, buf, avail);
  270. }
  271. if (retval > 0)
  272. stream->runtime->total_bytes_available += retval;
  273. /* while initiating the stream, write should be called before START
  274. * call, so in setup move state */
  275. if (stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
  276. stream->runtime->state = SNDRV_PCM_STATE_PREPARED;
  277. pr_debug("stream prepared, Houston we are good to go\n");
  278. }
  279. mutex_unlock(&stream->device->lock);
  280. return retval;
  281. }
  282. static ssize_t snd_compr_read(struct file *f, char __user *buf,
  283. size_t count, loff_t *offset)
  284. {
  285. struct snd_compr_file *data = f->private_data;
  286. struct snd_compr_stream *stream;
  287. size_t avail;
  288. int retval;
  289. if (snd_BUG_ON(!data))
  290. return -EFAULT;
  291. stream = &data->stream;
  292. mutex_lock(&stream->device->lock);
  293. /* read is allowed when stream is running */
  294. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING) {
  295. retval = -EBADFD;
  296. goto out;
  297. }
  298. avail = snd_compr_get_avail(stream);
  299. pr_debug("avail returned %ld\n", (unsigned long)avail);
  300. /* calculate how much we can read from buffer */
  301. if (avail > count)
  302. avail = count;
  303. if (stream->ops->copy) {
  304. retval = stream->ops->copy(stream, buf, avail);
  305. } else {
  306. retval = -ENXIO;
  307. goto out;
  308. }
  309. if (retval > 0)
  310. stream->runtime->total_bytes_transferred += retval;
  311. out:
  312. mutex_unlock(&stream->device->lock);
  313. return retval;
  314. }
  315. static int snd_compr_mmap(struct file *f, struct vm_area_struct *vma)
  316. {
  317. return -ENXIO;
  318. }
  319. static inline int snd_compr_get_poll(struct snd_compr_stream *stream)
  320. {
  321. if (stream->direction == SND_COMPRESS_PLAYBACK)
  322. return POLLOUT | POLLWRNORM;
  323. else
  324. return POLLIN | POLLRDNORM;
  325. }
  326. static unsigned int snd_compr_poll(struct file *f, poll_table *wait)
  327. {
  328. struct snd_compr_file *data = f->private_data;
  329. struct snd_compr_stream *stream;
  330. size_t avail;
  331. int retval = 0;
  332. if (snd_BUG_ON(!data))
  333. return -EFAULT;
  334. stream = &data->stream;
  335. if (snd_BUG_ON(!stream))
  336. return -EFAULT;
  337. mutex_lock(&stream->device->lock);
  338. if (stream->runtime->state == SNDRV_PCM_STATE_PAUSED ||
  339. stream->runtime->state == SNDRV_PCM_STATE_OPEN) {
  340. retval = -EBADFD;
  341. goto out;
  342. }
  343. poll_wait(f, &stream->runtime->sleep, wait);
  344. avail = snd_compr_get_avail(stream);
  345. pr_debug("avail is %ld\n", (unsigned long)avail);
  346. /* check if we have at least one fragment to fill */
  347. switch (stream->runtime->state) {
  348. case SNDRV_PCM_STATE_DRAINING:
  349. /* stream has been woken up after drain is complete
  350. * draining done so set stream state to stopped
  351. */
  352. retval = snd_compr_get_poll(stream);
  353. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  354. break;
  355. case SNDRV_PCM_STATE_RUNNING:
  356. case SNDRV_PCM_STATE_PREPARED:
  357. case SNDRV_PCM_STATE_PAUSED:
  358. if (avail >= stream->runtime->fragment_size)
  359. retval = snd_compr_get_poll(stream);
  360. break;
  361. default:
  362. if (stream->direction == SND_COMPRESS_PLAYBACK)
  363. retval = POLLOUT | POLLWRNORM | POLLERR;
  364. else
  365. retval = POLLIN | POLLRDNORM | POLLERR;
  366. break;
  367. }
  368. out:
  369. mutex_unlock(&stream->device->lock);
  370. return retval;
  371. }
  372. static int
  373. snd_compr_get_caps(struct snd_compr_stream *stream, unsigned long arg)
  374. {
  375. int retval;
  376. struct snd_compr_caps caps;
  377. if (!stream->ops->get_caps)
  378. return -ENXIO;
  379. memset(&caps, 0, sizeof(caps));
  380. retval = stream->ops->get_caps(stream, &caps);
  381. if (retval)
  382. goto out;
  383. if (copy_to_user((void __user *)arg, &caps, sizeof(caps)))
  384. retval = -EFAULT;
  385. out:
  386. return retval;
  387. }
  388. static int
  389. snd_compr_get_codec_caps(struct snd_compr_stream *stream, unsigned long arg)
  390. {
  391. int retval;
  392. struct snd_compr_codec_caps *caps;
  393. if (!stream->ops->get_codec_caps)
  394. return -ENXIO;
  395. caps = kzalloc(sizeof(*caps), GFP_KERNEL);
  396. if (!caps)
  397. return -ENOMEM;
  398. retval = stream->ops->get_codec_caps(stream, caps);
  399. if (retval)
  400. goto out;
  401. if (copy_to_user((void __user *)arg, caps, sizeof(*caps)))
  402. retval = -EFAULT;
  403. out:
  404. kfree(caps);
  405. return retval;
  406. }
  407. /* revisit this with snd_pcm_preallocate_xxx */
  408. static int snd_compr_allocate_buffer(struct snd_compr_stream *stream,
  409. struct snd_compr_params *params)
  410. {
  411. unsigned int buffer_size;
  412. void *buffer;
  413. if (params->buffer.fragment_size == 0 ||
  414. params->buffer.fragments > SIZE_MAX / params->buffer.fragment_size)
  415. return -EINVAL;
  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 > U32_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;
  545. int ret;
  546. memset(&tstamp, 0, sizeof(tstamp));
  547. ret = snd_compr_update_tstamp(stream, &tstamp);
  548. if (ret == 0)
  549. ret = copy_to_user((struct snd_compr_tstamp __user *)arg,
  550. &tstamp, sizeof(tstamp)) ? -EFAULT : 0;
  551. return ret;
  552. }
  553. static int snd_compr_pause(struct snd_compr_stream *stream)
  554. {
  555. int retval;
  556. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  557. return -EPERM;
  558. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_PUSH);
  559. if (!retval)
  560. stream->runtime->state = SNDRV_PCM_STATE_PAUSED;
  561. return retval;
  562. }
  563. static int snd_compr_resume(struct snd_compr_stream *stream)
  564. {
  565. int retval;
  566. if (stream->runtime->state != SNDRV_PCM_STATE_PAUSED)
  567. return -EPERM;
  568. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_PAUSE_RELEASE);
  569. if (!retval)
  570. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  571. return retval;
  572. }
  573. static int snd_compr_start(struct snd_compr_stream *stream)
  574. {
  575. int retval;
  576. if (stream->runtime->state != SNDRV_PCM_STATE_PREPARED)
  577. return -EPERM;
  578. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_START);
  579. if (!retval)
  580. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  581. return retval;
  582. }
  583. static int snd_compr_stop(struct snd_compr_stream *stream)
  584. {
  585. int retval;
  586. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  587. stream->runtime->state == SNDRV_PCM_STATE_SETUP)
  588. return -EPERM;
  589. retval = stream->ops->trigger(stream, SNDRV_PCM_TRIGGER_STOP);
  590. if (!retval) {
  591. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  592. wake_up(&stream->runtime->sleep);
  593. stream->runtime->total_bytes_available = 0;
  594. stream->runtime->total_bytes_transferred = 0;
  595. }
  596. return retval;
  597. }
  598. /* this fn is called without lock being held and we change stream states here
  599. * so while using the stream state auquire the lock but relase before invoking
  600. * DSP as the call will possibly take a while
  601. */
  602. static int snd_compr_drain(struct snd_compr_stream *stream)
  603. {
  604. int retval;
  605. mutex_lock(&stream->device->lock);
  606. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  607. stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
  608. retval = -EPERM;
  609. goto ret;
  610. }
  611. mutex_unlock(&stream->device->lock);
  612. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_DRAIN);
  613. mutex_lock(&stream->device->lock);
  614. if (!retval) {
  615. stream->runtime->state = SNDRV_PCM_STATE_DRAINING;
  616. wake_up(&stream->runtime->sleep);
  617. }
  618. ret:
  619. mutex_unlock(&stream->device->lock);
  620. return retval;
  621. }
  622. static int snd_compr_next_track(struct snd_compr_stream *stream)
  623. {
  624. int retval;
  625. /* only a running stream can transition to next track */
  626. if (stream->runtime->state != SNDRV_PCM_STATE_RUNNING)
  627. return -EPERM;
  628. /* you can signal next track isf this is intended to be a gapless stream
  629. * and current track metadata is set
  630. */
  631. if (stream->metadata_set == false)
  632. return -EPERM;
  633. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_NEXT_TRACK);
  634. if (retval != 0)
  635. return retval;
  636. stream->metadata_set = false;
  637. stream->next_track = true;
  638. return 0;
  639. }
  640. static int snd_compr_partial_drain(struct snd_compr_stream *stream)
  641. {
  642. int retval;
  643. mutex_lock(&stream->device->lock);
  644. if (stream->runtime->state == SNDRV_PCM_STATE_PREPARED ||
  645. stream->runtime->state == SNDRV_PCM_STATE_SETUP) {
  646. mutex_unlock(&stream->device->lock);
  647. return -EPERM;
  648. }
  649. mutex_unlock(&stream->device->lock);
  650. /* stream can be drained only when next track has been signalled */
  651. if (stream->next_track == false)
  652. return -EPERM;
  653. retval = stream->ops->trigger(stream, SND_COMPR_TRIGGER_PARTIAL_DRAIN);
  654. stream->next_track = false;
  655. return retval;
  656. }
  657. static int snd_compress_simple_ioctls(struct file *file,
  658. struct snd_compr_stream *stream,
  659. unsigned int cmd, unsigned long arg)
  660. {
  661. int retval = -ENOTTY;
  662. switch (_IOC_NR(cmd)) {
  663. case _IOC_NR(SNDRV_COMPRESS_IOCTL_VERSION):
  664. retval = put_user(SNDRV_COMPRESS_VERSION,
  665. (int __user *)arg) ? -EFAULT : 0;
  666. break;
  667. case _IOC_NR(SNDRV_COMPRESS_GET_CAPS):
  668. retval = snd_compr_get_caps(stream, arg);
  669. break;
  670. case _IOC_NR(SNDRV_COMPRESS_GET_CODEC_CAPS):
  671. retval = snd_compr_get_codec_caps(stream, arg);
  672. break;
  673. case _IOC_NR(SNDRV_COMPRESS_TSTAMP):
  674. retval = snd_compr_tstamp(stream, arg);
  675. break;
  676. case _IOC_NR(SNDRV_COMPRESS_AVAIL):
  677. retval = snd_compr_ioctl_avail(stream, arg);
  678. break;
  679. /* drain and partial drain need special handling
  680. * we need to drop the locks here as the streams would get blocked on the
  681. * dsp to get drained. The locking would be handled in respective
  682. * function here
  683. */
  684. case _IOC_NR(SNDRV_COMPRESS_DRAIN):
  685. retval = snd_compr_drain(stream);
  686. break;
  687. case _IOC_NR(SNDRV_COMPRESS_PARTIAL_DRAIN):
  688. retval = snd_compr_partial_drain(stream);
  689. break;
  690. }
  691. return retval;
  692. }
  693. static long snd_compr_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
  694. {
  695. struct snd_compr_file *data = f->private_data;
  696. struct snd_compr_stream *stream;
  697. int retval = -ENOTTY;
  698. if (snd_BUG_ON(!data))
  699. return -EFAULT;
  700. stream = &data->stream;
  701. if (snd_BUG_ON(!stream))
  702. return -EFAULT;
  703. mutex_lock(&stream->device->lock);
  704. switch (_IOC_NR(cmd)) {
  705. case _IOC_NR(SNDRV_COMPRESS_SET_PARAMS):
  706. retval = snd_compr_set_params(stream, arg);
  707. break;
  708. case _IOC_NR(SNDRV_COMPRESS_GET_PARAMS):
  709. retval = snd_compr_get_params(stream, arg);
  710. break;
  711. case _IOC_NR(SNDRV_COMPRESS_SET_METADATA):
  712. retval = snd_compr_set_metadata(stream, arg);
  713. break;
  714. case _IOC_NR(SNDRV_COMPRESS_GET_METADATA):
  715. retval = snd_compr_get_metadata(stream, arg);
  716. break;
  717. case _IOC_NR(SNDRV_COMPRESS_PAUSE):
  718. retval = snd_compr_pause(stream);
  719. break;
  720. case _IOC_NR(SNDRV_COMPRESS_RESUME):
  721. retval = snd_compr_resume(stream);
  722. break;
  723. case _IOC_NR(SNDRV_COMPRESS_START):
  724. retval = snd_compr_start(stream);
  725. break;
  726. case _IOC_NR(SNDRV_COMPRESS_STOP):
  727. retval = snd_compr_stop(stream);
  728. break;
  729. case _IOC_NR(SNDRV_COMPRESS_NEXT_TRACK):
  730. retval = snd_compr_next_track(stream);
  731. break;
  732. default:
  733. mutex_unlock(&stream->device->lock);
  734. return snd_compress_simple_ioctls(f, stream, cmd, arg);
  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. /*
  799. * snd_compress_free: free compress device
  800. * @card: sound card pointer
  801. * @compr: compress device pointer
  802. */
  803. void snd_compress_free(struct snd_card *card, struct snd_compr *compr)
  804. {
  805. snd_device_free(card, compr);
  806. }
  807. EXPORT_SYMBOL_GPL(snd_compress_free);
  808. static int snd_compress_add_device(struct snd_compr *device)
  809. {
  810. int ret;
  811. if (!device->card)
  812. return -EINVAL;
  813. /* register the card */
  814. ret = snd_card_register(device->card);
  815. if (ret)
  816. goto out;
  817. return 0;
  818. out:
  819. pr_err("failed with %d\n", ret);
  820. return ret;
  821. }
  822. static int snd_compress_remove_device(struct snd_compr *device)
  823. {
  824. return snd_card_free(device->card);
  825. }
  826. /**
  827. * snd_compress_register - register compressed device
  828. *
  829. * @device: compressed device to register
  830. */
  831. int snd_compress_register(struct snd_compr *device)
  832. {
  833. int retval;
  834. if (device->name == NULL || device->dev == NULL || device->ops == NULL)
  835. return -EINVAL;
  836. pr_debug("Registering compressed device %s\n", device->name);
  837. if (snd_BUG_ON(!device->ops->open))
  838. return -EINVAL;
  839. if (snd_BUG_ON(!device->ops->free))
  840. return -EINVAL;
  841. if (snd_BUG_ON(!device->ops->set_params))
  842. return -EINVAL;
  843. if (snd_BUG_ON(!device->ops->trigger))
  844. return -EINVAL;
  845. mutex_init(&device->lock);
  846. /* register a compressed card */
  847. mutex_lock(&device_mutex);
  848. retval = snd_compress_add_device(device);
  849. mutex_unlock(&device_mutex);
  850. return retval;
  851. }
  852. EXPORT_SYMBOL_GPL(snd_compress_register);
  853. int snd_compress_deregister(struct snd_compr *device)
  854. {
  855. pr_debug("Removing compressed device %s\n", device->name);
  856. mutex_lock(&device_mutex);
  857. snd_compress_remove_device(device);
  858. mutex_unlock(&device_mutex);
  859. return 0;
  860. }
  861. EXPORT_SYMBOL_GPL(snd_compress_deregister);
  862. static int __init snd_compress_init(void)
  863. {
  864. return 0;
  865. }
  866. static void __exit snd_compress_exit(void)
  867. {
  868. }
  869. module_init(snd_compress_init);
  870. module_exit(snd_compress_exit);
  871. MODULE_DESCRIPTION("ALSA Compressed offload framework");
  872. MODULE_AUTHOR("Vinod Koul <vinod.koul@linux.intel.com>");
  873. MODULE_LICENSE("GPL v2");