msm-lsm-client.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (c) 2013, 2017, Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/time.h>
  17. #include <linux/wait.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/of.h>
  22. #include <linux/freezer.h>
  23. #include <sound/core.h>
  24. #include <sound/soc.h>
  25. #include <sound/soc-dapm.h>
  26. #include <sound/pcm.h>
  27. #include <sound/timer.h>
  28. #include <sound/initval.h>
  29. #include <sound/control.h>
  30. #include <sound/q6lsm.h>
  31. #include <sound/lsm_params.h>
  32. #include "msm-pcm-routing-v2.h"
  33. struct lsm_priv {
  34. struct snd_pcm_substream *substream;
  35. struct lsm_client *lsm_client;
  36. struct snd_lsm_event_status *event_status;
  37. spinlock_t event_lock;
  38. wait_queue_head_t event_wait;
  39. unsigned long event_avail;
  40. atomic_t event_wait_stop;
  41. struct mutex lsm_api_lock;
  42. };
  43. static void lsm_event_handler(uint32_t opcode, uint32_t token,
  44. void *payload, void *priv)
  45. {
  46. unsigned long flags;
  47. struct snd_lsm_event_status *event_status;
  48. struct lsm_priv *prtd = priv;
  49. struct snd_pcm_substream *substream = prtd->substream;
  50. pr_debug("%s: enter opcode 0x%x\n", __func__, opcode);
  51. switch (opcode) {
  52. case LSM_SESSION_EVENT_DETECTION_STATUS:
  53. event_status = payload;
  54. spin_lock_irqsave(&prtd->event_lock, flags);
  55. prtd->event_status = krealloc(prtd->event_status,
  56. sizeof(*event_status) +
  57. event_status->payload_size,
  58. GFP_ATOMIC);
  59. if (likely(prtd->event_status)) {
  60. memcpy(prtd->event_status, event_status,
  61. sizeof(*event_status) +
  62. event_status->payload_size);
  63. prtd->event_avail = 1;
  64. spin_unlock_irqrestore(&prtd->event_lock, flags);
  65. wake_up(&prtd->event_wait);
  66. } else {
  67. spin_unlock_irqrestore(&prtd->event_lock, flags);
  68. pr_err("%s: Couldn't allocate %d bytes of memory\n",
  69. __func__, event_status->payload_size);
  70. }
  71. if (substream->timer_running)
  72. snd_timer_interrupt(substream->timer, 1);
  73. break;
  74. default:
  75. pr_debug("%s: Unsupported Event opcode 0x%x\n", __func__,
  76. opcode);
  77. break;
  78. }
  79. }
  80. static int msm_lsm_ioctl(struct snd_pcm_substream *substream,
  81. unsigned int cmd, void *arg)
  82. {
  83. unsigned long flags;
  84. int ret;
  85. struct snd_lsm_sound_model snd_model;
  86. int rc = 0;
  87. int xchg = 0;
  88. int size = 0;
  89. struct snd_lsm_event_status *event_status = NULL;
  90. struct snd_pcm_runtime *runtime = substream->runtime;
  91. struct lsm_priv *prtd = runtime->private_data;
  92. struct snd_lsm_event_status *user = arg;
  93. mutex_lock(&prtd->lsm_api_lock);
  94. pr_debug("%s: enter cmd %x\n", __func__, cmd);
  95. switch (cmd) {
  96. case SNDRV_LSM_REG_SND_MODEL:
  97. pr_debug("%s: Registering sound model\n", __func__);
  98. if (copy_from_user(&snd_model, (void *)arg,
  99. sizeof(struct snd_lsm_sound_model))) {
  100. rc = -EFAULT;
  101. pr_err("%s: copy from user failed, size %d\n", __func__,
  102. sizeof(struct snd_lsm_sound_model));
  103. break;
  104. }
  105. rc = q6lsm_snd_model_buf_alloc(prtd->lsm_client,
  106. snd_model.data_size);
  107. if (rc) {
  108. pr_err("%s: q6lsm buffer alloc failed, size %d\n",
  109. __func__, snd_model.data_size);
  110. break;
  111. }
  112. if (copy_from_user(prtd->lsm_client->sound_model.data,
  113. snd_model.data, snd_model.data_size)) {
  114. pr_err("%s: copy from user data failed data %p size %d\n",
  115. __func__, snd_model.data, snd_model.data_size);
  116. rc = -EFAULT;
  117. break;
  118. }
  119. rc = q6lsm_register_sound_model(prtd->lsm_client,
  120. snd_model.detection_mode,
  121. snd_model.min_keyw_confidence,
  122. snd_model.min_user_confidence,
  123. snd_model.detect_failure);
  124. if (rc < 0) {
  125. pr_err("%s: q6lsm_register_sound_model failed =%d\n",
  126. __func__, rc);
  127. q6lsm_snd_model_buf_free(prtd->lsm_client);
  128. }
  129. break;
  130. case SNDRV_LSM_DEREG_SND_MODEL:
  131. pr_debug("%s: Deregistering sound model\n", __func__);
  132. rc = q6lsm_deregister_sound_model(prtd->lsm_client);
  133. break;
  134. case SNDRV_LSM_EVENT_STATUS:
  135. pr_debug("%s: Get event status\n", __func__);
  136. atomic_set(&prtd->event_wait_stop, 0);
  137. /*
  138. * Release the api lock before wait to allow
  139. * other IOCTLs to be invoked while waiting
  140. * for event
  141. */
  142. mutex_unlock(&prtd->lsm_api_lock);
  143. rc = wait_event_freezable(prtd->event_wait,
  144. (cmpxchg(&prtd->event_avail, 1, 0) ||
  145. (xchg = atomic_cmpxchg(&prtd->event_wait_stop,
  146. 1, 0))));
  147. mutex_lock(&prtd->lsm_api_lock);
  148. pr_debug("%s: wait_event_freezable %d event_wait_stop %d\n",
  149. __func__, rc, xchg);
  150. if (!rc && !xchg) {
  151. pr_debug("%s: New event available %ld\n", __func__,
  152. prtd->event_avail);
  153. spin_lock_irqsave(&prtd->event_lock, flags);
  154. if (prtd->event_status) {
  155. size = sizeof(*event_status) +
  156. prtd->event_status->payload_size;
  157. event_status = kmemdup(prtd->event_status, size,
  158. GFP_ATOMIC);
  159. }
  160. spin_unlock_irqrestore(&prtd->event_lock, flags);
  161. if (!event_status) {
  162. pr_err("%s: Couldn't allocate %d bytes\n",
  163. __func__, size);
  164. /*
  165. * Don't use -ENOMEM as userspace will check
  166. * it for increasing buffer
  167. */
  168. rc = -EFAULT;
  169. } else {
  170. if (!access_ok(VERIFY_READ, user,
  171. sizeof(struct snd_lsm_event_status)))
  172. rc = -EFAULT;
  173. if (user->payload_size <
  174. event_status->payload_size) {
  175. pr_debug("%s: provided %dbytes isn't enough, needs %dbytes\n",
  176. __func__, user->payload_size,
  177. size);
  178. rc = -ENOMEM;
  179. } else if (!access_ok(VERIFY_WRITE, arg,
  180. size)) {
  181. rc = -EFAULT;
  182. } else {
  183. rc = copy_to_user(arg, event_status,
  184. size);
  185. if (rc)
  186. pr_err("%s: copy to user failed %d\n",
  187. __func__, rc);
  188. }
  189. kfree(event_status);
  190. }
  191. } else if (xchg) {
  192. pr_debug("%s: Wait aborted\n", __func__);
  193. rc = 0;
  194. }
  195. break;
  196. case SNDRV_LSM_ABORT_EVENT:
  197. pr_debug("%s: Aborting event status wait\n", __func__);
  198. atomic_set(&prtd->event_wait_stop, 1);
  199. wake_up(&prtd->event_wait);
  200. break;
  201. case SNDRV_LSM_START:
  202. pr_debug("%s: Starting LSM client session\n", __func__);
  203. if (!prtd->lsm_client->started) {
  204. ret = q6lsm_start(prtd->lsm_client, true);
  205. if (!ret) {
  206. prtd->lsm_client->started = true;
  207. pr_debug("%s: LSM client session started\n",
  208. __func__);
  209. }
  210. }
  211. break;
  212. case SNDRV_LSM_STOP:
  213. pr_debug("%s: Stopping LSM client session\n", __func__);
  214. if (prtd->lsm_client->started) {
  215. ret = q6lsm_stop(prtd->lsm_client, true);
  216. if (!ret)
  217. pr_debug("%s: LSM client session stopped %d\n",
  218. __func__, ret);
  219. prtd->lsm_client->started = false;
  220. }
  221. break;
  222. default:
  223. pr_debug("%s: Falling into default snd_lib_ioctl cmd 0x%x\n",
  224. __func__, cmd);
  225. rc = snd_pcm_lib_ioctl(substream, cmd, arg);
  226. break;
  227. }
  228. if (!rc)
  229. pr_debug("%s: leave (%d)\n", __func__, rc);
  230. else
  231. pr_err("%s: cmd 0x%x failed %d\n", __func__, cmd, rc);
  232. mutex_unlock(&prtd->lsm_api_lock);
  233. return rc;
  234. }
  235. static int msm_lsm_open(struct snd_pcm_substream *substream)
  236. {
  237. struct snd_pcm_runtime *runtime = substream->runtime;
  238. struct lsm_priv *prtd;
  239. int ret = 0;
  240. pr_debug("%s\n", __func__);
  241. prtd = kzalloc(sizeof(struct lsm_priv), GFP_KERNEL);
  242. if (!prtd) {
  243. pr_err("%s: Failed to allocate memory for lsm_priv\n",
  244. __func__);
  245. return -ENOMEM;
  246. }
  247. prtd->substream = substream;
  248. prtd->lsm_client = q6lsm_client_alloc(
  249. (lsm_app_cb)lsm_event_handler, prtd);
  250. if (!prtd->lsm_client) {
  251. pr_err("%s: Could not allocate memory\n", __func__);
  252. kfree(prtd);
  253. return -ENOMEM;
  254. }
  255. prtd->lsm_client->opened = false;
  256. ret = q6lsm_open(prtd->lsm_client);
  257. if (ret < 0) {
  258. pr_err("%s: lsm open failed, %d\n", __func__, ret);
  259. return ret;
  260. }
  261. prtd->lsm_client->opened = true;
  262. pr_debug("%s: Session ID %d\n", __func__, prtd->lsm_client->session);
  263. prtd->lsm_client->started = false;
  264. mutex_init(&prtd->lsm_api_lock);
  265. spin_lock_init(&prtd->event_lock);
  266. init_waitqueue_head(&prtd->event_wait);
  267. runtime->private_data = prtd;
  268. return 0;
  269. }
  270. static int msm_lsm_close(struct snd_pcm_substream *substream)
  271. {
  272. unsigned long flags;
  273. struct snd_pcm_runtime *runtime = substream->runtime;
  274. struct lsm_priv *prtd = runtime->private_data;
  275. int ret = 0;
  276. pr_debug("%s\n", __func__);
  277. if (prtd->lsm_client->started) {
  278. ret = q6lsm_stop(prtd->lsm_client, true);
  279. if (ret)
  280. pr_err("%s: session stop failed, err = %d\n",
  281. __func__, ret);
  282. else
  283. pr_debug("%s: LSM client session stopped %d\n",
  284. __func__, ret);
  285. /*
  286. * Go Ahead and try de-register sound model,
  287. * even if stop failed
  288. */
  289. prtd->lsm_client->started = false;
  290. ret = q6lsm_deregister_sound_model(prtd->lsm_client);
  291. if (ret)
  292. pr_err("%s: dereg_snd_model failed, err = %d\n",
  293. __func__, ret);
  294. else
  295. pr_debug("%s: dereg_snd_model succesful\n",
  296. __func__);
  297. }
  298. if (prtd->lsm_client->opened) {
  299. q6lsm_close(prtd->lsm_client);
  300. prtd->lsm_client->opened = false;
  301. }
  302. q6lsm_client_free(prtd->lsm_client);
  303. spin_lock_irqsave(&prtd->event_lock, flags);
  304. kfree(prtd->event_status);
  305. prtd->event_status = NULL;
  306. spin_unlock_irqrestore(&prtd->event_lock, flags);
  307. mutex_destroy(&prtd->lsm_api_lock);
  308. kfree(prtd);
  309. runtime->private_data = NULL;
  310. return 0;
  311. }
  312. static struct snd_pcm_ops msm_lsm_ops = {
  313. .open = msm_lsm_open,
  314. .close = msm_lsm_close,
  315. .ioctl = msm_lsm_ioctl,
  316. };
  317. static int msm_asoc_lsm_new(struct snd_soc_pcm_runtime *rtd)
  318. {
  319. struct snd_card *card = rtd->card->snd_card;
  320. if (!card->dev->coherent_dma_mask)
  321. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  322. return 0;
  323. }
  324. static int msm_asoc_lsm_probe(struct snd_soc_platform *platform)
  325. {
  326. pr_debug("enter %s\n", __func__);
  327. return 0;
  328. }
  329. static struct snd_soc_platform_driver msm_soc_platform = {
  330. .ops = &msm_lsm_ops,
  331. .pcm_new = msm_asoc_lsm_new,
  332. .probe = msm_asoc_lsm_probe,
  333. };
  334. static __devinit int msm_lsm_probe(struct platform_device *pdev)
  335. {
  336. if (pdev->dev.of_node)
  337. dev_set_name(&pdev->dev, "%s", "msm-lsm-client");
  338. return snd_soc_register_platform(&pdev->dev, &msm_soc_platform);
  339. }
  340. static int msm_lsm_remove(struct platform_device *pdev)
  341. {
  342. snd_soc_unregister_platform(&pdev->dev);
  343. return 0;
  344. }
  345. static const struct of_device_id msm_lsm_client_dt_match[] = {
  346. {.compatible = "qcom,msm-lsm-client" },
  347. { }
  348. };
  349. static struct platform_driver msm_lsm_driver = {
  350. .driver = {
  351. .name = "msm-lsm-client",
  352. .owner = THIS_MODULE,
  353. .of_match_table = of_match_ptr(msm_lsm_client_dt_match),
  354. },
  355. .probe = msm_lsm_probe,
  356. .remove = __devexit_p(msm_lsm_remove),
  357. };
  358. static int __init msm_soc_platform_init(void)
  359. {
  360. return platform_driver_register(&msm_lsm_driver);
  361. }
  362. module_init(msm_soc_platform_init);
  363. static void __exit msm_soc_platform_exit(void)
  364. {
  365. platform_driver_unregister(&msm_lsm_driver);
  366. }
  367. module_exit(msm_soc_platform_exit);
  368. MODULE_DESCRIPTION("LSM client platform driver");
  369. MODULE_DEVICE_TABLE(of, msm_lsm_client_dt_match);
  370. MODULE_LICENSE("GPL v2");