compress_offload.c 30 KB

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