audio_utils_aio.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* Copyright (C) 2008 Google, Inc.
  2. * Copyright (C) 2008 HTC Corporation
  3. * Copyright (c) 2009-2017, The Linux Foundation. All rights reserved.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/fs.h>
  16. #include <linux/module.h>
  17. #include <linux/miscdevice.h>
  18. #include <linux/mutex.h>
  19. #include <linux/sched.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/wait.h>
  22. #include <linux/wakelock.h>
  23. #include <linux/msm_audio.h>
  24. #include <linux/debugfs.h>
  25. #include <linux/list.h>
  26. #include <linux/slab.h>
  27. #include <linux/msm_ion.h>
  28. #include <asm/ioctls.h>
  29. #include <linux/atomic.h>
  30. #include "q6audio_common.h"
  31. #define TUNNEL_MODE 0x0000
  32. #define NON_TUNNEL_MODE 0x0001
  33. #define ADRV_STATUS_AIO_INTF 0x00000001 /* AIO interface */
  34. #define ADRV_STATUS_FSYNC 0x00000008
  35. #define ADRV_STATUS_PAUSE 0x00000010
  36. #define AUDIO_DEC_EOS_SET 0x00000001
  37. #define AUDIO_DEC_EOF_SET 0x00000010
  38. #define AUDIO_EVENT_NUM 10
  39. #define __CONTAINS(r, v, l) ({ \
  40. typeof(r) __r = r; \
  41. typeof(v) __v = v; \
  42. typeof(v) __e = __v + l; \
  43. int res = ((__v >= __r->vaddr) && \
  44. (__e <= __r->vaddr + __r->len)); \
  45. res; \
  46. })
  47. #define CONTAINS(r1, r2) ({ \
  48. typeof(r2) __r2 = r2; \
  49. __CONTAINS(r1, __r2->vaddr, __r2->len); \
  50. })
  51. #define IN_RANGE(r, v) ({ \
  52. typeof(r) __r = r; \
  53. typeof(v) __vv = v; \
  54. int res = ((__vv >= __r->vaddr) && \
  55. (__vv < (__r->vaddr + __r->len))); \
  56. res; \
  57. })
  58. #define OVERLAPS(r1, r2) ({ \
  59. typeof(r1) __r1 = r1; \
  60. typeof(r2) __r2 = r2; \
  61. typeof(__r2->vaddr) __v = __r2->vaddr; \
  62. typeof(__v) __e = __v + __r2->len - 1; \
  63. int res = (IN_RANGE(__r1, __v) || IN_RANGE(__r1, __e)); \
  64. res; \
  65. })
  66. struct timestamp {
  67. u32 lowpart;
  68. u32 highpart;
  69. } __packed;
  70. struct meta_out_dsp {
  71. u32 offset_to_frame;
  72. u32 frame_size;
  73. u32 encoded_pcm_samples;
  74. u32 msw_ts;
  75. u32 lsw_ts;
  76. u32 nflags;
  77. } __packed;
  78. struct dec_meta_in {
  79. unsigned char reserved[18];
  80. unsigned short offset;
  81. struct timestamp ntimestamp;
  82. unsigned int nflags;
  83. } __packed;
  84. struct dec_meta_out {
  85. unsigned int reserved[7];
  86. unsigned int num_of_frames;
  87. struct meta_out_dsp meta_out_dsp[];
  88. } __packed;
  89. /* General meta field to store meta info locally */
  90. union meta_data {
  91. struct dec_meta_out meta_out;
  92. struct dec_meta_in meta_in;
  93. } __packed;
  94. /* per device wakeup source manager */
  95. struct ws_mgr {
  96. struct mutex ws_lock;
  97. uint32_t ref_cnt;
  98. };
  99. #define PCM_BUF_COUNT (2)
  100. /* Buffer with meta */
  101. #define PCM_BUFSZ_MIN ((4*1024) + sizeof(struct dec_meta_out))
  102. /* FRAME_NUM must be a power of two */
  103. #define FRAME_NUM (2)
  104. #define FRAME_SIZE ((4*1536) + sizeof(struct dec_meta_in))
  105. struct audio_aio_ion_region {
  106. struct list_head list;
  107. struct ion_handle *handle;
  108. int fd;
  109. void *vaddr;
  110. phys_addr_t paddr;
  111. void *kvaddr;
  112. unsigned long len;
  113. unsigned int ref_cnt;
  114. };
  115. struct audio_aio_event {
  116. struct list_head list;
  117. int event_type;
  118. union msm_audio_event_payload payload;
  119. };
  120. struct audio_aio_buffer_node {
  121. struct list_head list;
  122. struct msm_audio_aio_buf buf;
  123. unsigned long paddr;
  124. uint32_t token;
  125. void *kvaddr;
  126. union meta_data meta_info;
  127. };
  128. struct q6audio_aio;
  129. struct audio_aio_drv_operations {
  130. void (*out_flush)(struct q6audio_aio *);
  131. void (*in_flush)(struct q6audio_aio *);
  132. };
  133. struct q6audio_aio {
  134. atomic_t in_bytes;
  135. atomic_t in_samples;
  136. struct msm_audio_stream_config str_cfg;
  137. struct msm_audio_buf_cfg buf_cfg;
  138. struct msm_audio_config pcm_cfg;
  139. void *codec_cfg;
  140. struct audio_client *ac;
  141. struct mutex lock;
  142. struct mutex read_lock;
  143. struct mutex write_lock;
  144. struct mutex get_event_lock;
  145. wait_queue_head_t cmd_wait;
  146. wait_queue_head_t write_wait;
  147. wait_queue_head_t event_wait;
  148. spinlock_t dsp_lock;
  149. spinlock_t event_queue_lock;
  150. struct miscdevice *miscdevice;
  151. uint32_t wakelock_voted;
  152. struct ws_mgr *audio_ws_mgr;
  153. #ifdef CONFIG_DEBUG_FS
  154. struct dentry *dentry;
  155. #endif
  156. struct list_head out_queue; /* queue to retain output buffers */
  157. struct list_head in_queue; /* queue to retain input buffers */
  158. struct list_head free_event_queue;
  159. struct list_head event_queue;
  160. struct list_head ion_region_queue; /* protected by lock */
  161. struct ion_client *client;
  162. struct audio_aio_drv_operations drv_ops;
  163. union msm_audio_event_payload eos_write_payload;
  164. uint32_t device_events;
  165. uint16_t volume;
  166. uint32_t drv_status;
  167. int event_abort;
  168. int eos_rsp;
  169. int eos_flag;
  170. int opened;
  171. int enabled;
  172. int stopped;
  173. int feedback;
  174. int rflush; /* Read flush */
  175. int wflush; /* Write flush */
  176. bool reset_event;
  177. long (*codec_ioctl)(struct file *, unsigned int, unsigned long);
  178. long (*codec_compat_ioctl)(struct file *, unsigned int, unsigned long);
  179. };
  180. void audio_aio_async_write_ack(struct q6audio_aio *audio, uint32_t token,
  181. uint32_t *payload);
  182. void audio_aio_async_read_ack(struct q6audio_aio *audio, uint32_t token,
  183. uint32_t *payload);
  184. int insert_eos_buf(struct q6audio_aio *audio,
  185. struct audio_aio_buffer_node *buf_node);
  186. void extract_meta_out_info(struct q6audio_aio *audio,
  187. struct audio_aio_buffer_node *buf_node, int dir);
  188. int audio_aio_open(struct q6audio_aio *audio, struct file *file);
  189. int audio_aio_enable(struct q6audio_aio *audio);
  190. void audio_aio_post_event(struct q6audio_aio *audio, int type,
  191. union msm_audio_event_payload payload);
  192. int audio_aio_release(struct inode *inode, struct file *file);
  193. int audio_aio_fsync(struct file *file, loff_t start, loff_t end, int datasync);
  194. void audio_aio_async_out_flush(struct q6audio_aio *audio);
  195. void audio_aio_async_in_flush(struct q6audio_aio *audio);
  196. void audio_aio_ioport_reset(struct q6audio_aio *audio);
  197. int enable_volume_ramp(struct q6audio_aio *audio);
  198. #ifdef CONFIG_DEBUG_FS
  199. int audio_aio_debug_open(struct inode *inode, struct file *file);
  200. ssize_t audio_aio_debug_read(struct file *file, char __user *buf,
  201. size_t count, loff_t *ppos);
  202. #endif