snd_event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018, 2020 The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include <linux/platform_device.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/of_device.h>
  10. #include <soc/snd_event.h>
  11. struct snd_event_client {
  12. struct list_head node;
  13. struct device *dev;
  14. const struct snd_event_ops *ops;
  15. void *data;
  16. bool attached;
  17. bool state;
  18. };
  19. struct snd_event_client_array {
  20. struct device *dev;
  21. struct snd_event_client *clnt;
  22. void *data;
  23. int (*compare)(struct device *, void *);
  24. };
  25. struct snd_event_clients {
  26. size_t num_clients;
  27. struct snd_event_client_array *cl_arr;
  28. };
  29. struct snd_master {
  30. struct device *dev;
  31. const struct snd_event_ops *ops;
  32. void *data;
  33. bool state;
  34. bool fwk_state;
  35. bool clients_found;
  36. struct snd_event_clients *clients;
  37. };
  38. static DEFINE_MUTEX(snd_event_mutex);
  39. static LIST_HEAD(snd_event_client_list);
  40. static struct snd_master *master;
  41. static struct snd_event_client *find_snd_event_client(struct device *dev)
  42. {
  43. struct snd_event_client *c;
  44. list_for_each_entry(c, &snd_event_client_list, node)
  45. if ((c->dev == dev) && c->ops)
  46. return c;
  47. return NULL;
  48. }
  49. static int check_and_update_fwk_state(void)
  50. {
  51. bool new_fwk_state = true;
  52. struct snd_event_client *c;
  53. int ret = 0;
  54. int i = 0;
  55. for (i = 0; i < master->clients->num_clients; i++) {
  56. c = master->clients->cl_arr[i].clnt;
  57. new_fwk_state &= c->state;
  58. }
  59. new_fwk_state &= master->state;
  60. if (master->fwk_state ^ new_fwk_state) {
  61. if (new_fwk_state) {
  62. for (i = 0; i < master->clients->num_clients; i++) {
  63. c = master->clients->cl_arr[i].clnt;
  64. if (c->ops->enable) {
  65. ret = c->ops->enable(c->dev, c->data);
  66. if (ret) {
  67. dev_err_ratelimited(c->dev,
  68. "%s: enable failed\n",
  69. __func__);
  70. goto dev_en_failed;
  71. }
  72. }
  73. }
  74. if (master->ops->enable) {
  75. ret = master->ops->enable(master->dev,
  76. master->data);
  77. if (ret) {
  78. dev_err_ratelimited(master->dev,
  79. "%s: enable failed\n",
  80. __func__);
  81. goto mstr_en_failed;
  82. }
  83. }
  84. } else {
  85. if (master->ops->disable)
  86. master->ops->disable(master->dev,
  87. master->data);
  88. for (i = 0; i < master->clients->num_clients; i++) {
  89. c = master->clients->cl_arr[i].clnt;
  90. if (c->ops->disable)
  91. c->ops->disable(c->dev, c->data);
  92. }
  93. }
  94. master->fwk_state = new_fwk_state;
  95. }
  96. goto exit;
  97. mstr_en_failed:
  98. i = master->clients->num_clients;
  99. dev_en_failed:
  100. for (; i > 0; i--) {
  101. c = master->clients->cl_arr[i - 1].clnt;
  102. if (c->ops->disable)
  103. c->ops->disable(c->dev, c->data);
  104. }
  105. exit:
  106. return ret;
  107. }
  108. static int snd_event_find_clients(struct snd_master *master)
  109. {
  110. struct snd_event_clients *clients = master->clients;
  111. int i = 0;
  112. int ret = 0;
  113. for (i = 0; i < clients->num_clients; i++) {
  114. struct snd_event_client_array *c_arr = &clients->cl_arr[i];
  115. struct snd_event_client *c;
  116. if (c_arr->dev) {
  117. pr_err_ratelimited("%s: client already present dev=%pK\n",
  118. __func__, c_arr->dev);
  119. continue;
  120. }
  121. list_for_each_entry(c, &snd_event_client_list, node) {
  122. if (c->attached)
  123. continue;
  124. if (c_arr->compare(c->dev, c_arr->data)) {
  125. dev_dbg(master->dev,
  126. "%s: found client, dev=%pK\n",
  127. __func__, c->dev);
  128. c_arr->dev = c->dev;
  129. c_arr->clnt = c;
  130. c->attached = true;
  131. break;
  132. }
  133. }
  134. if (!c_arr->dev) {
  135. dev_dbg(master->dev,
  136. "%s: failed to find some client\n",
  137. __func__);
  138. ret = -ENXIO;
  139. break;
  140. }
  141. }
  142. return ret;
  143. }
  144. /*
  145. * snd_event_client_register - Register a client with the SND event FW
  146. *
  147. * @dev: Pointer to the "struct device" associated with the client
  148. * @snd_ev_ops: Pointer to the snd_event_ops struct for the client containing
  149. * callback functions
  150. * @data: Pointer to any additional data that the caller wants to get back
  151. * with callback functions
  152. *
  153. * Returns 0 on success or error on failure.
  154. */
  155. int snd_event_client_register(struct device *dev,
  156. const struct snd_event_ops *snd_ev_ops,
  157. void *data)
  158. {
  159. struct snd_event_client *c;
  160. if (!dev) {
  161. pr_err_ratelimited("%s: dev is NULL\n", __func__);
  162. return -EINVAL;
  163. }
  164. c = kzalloc(sizeof(*c), GFP_KERNEL);
  165. if (!c)
  166. return -ENOMEM;
  167. c->dev = dev;
  168. c->ops = snd_ev_ops;
  169. c->data = data;
  170. dev_dbg(dev, "%s: adding client to SND event FW (ops %pK)\n",
  171. __func__, snd_ev_ops);
  172. mutex_lock(&snd_event_mutex);
  173. list_add_tail(&c->node, &snd_event_client_list);
  174. if (master && !master->clients_found) {
  175. if (snd_event_find_clients(master)) {
  176. dev_dbg(dev, "%s: Failed to find all clients\n",
  177. __func__);
  178. goto exit;
  179. }
  180. master->clients_found = true;
  181. }
  182. exit:
  183. mutex_unlock(&snd_event_mutex);
  184. return 0;
  185. }
  186. EXPORT_SYMBOL(snd_event_client_register);
  187. /*
  188. * snd_event_client_deregister - Remove a client from the SND event FW
  189. *
  190. * @dev: Pointer to the "struct device" associated with the client
  191. *
  192. * Returns 0 on success or error on failure.
  193. */
  194. int snd_event_client_deregister(struct device *dev)
  195. {
  196. struct snd_event_client *c;
  197. int ret = 0;
  198. int i = 0;
  199. if (!dev) {
  200. pr_err_ratelimited("%s: dev is NULL\n", __func__);
  201. return -EINVAL;
  202. }
  203. dev_dbg(dev, "%s: removing client to SND event FW \n",
  204. __func__);
  205. mutex_lock(&snd_event_mutex);
  206. if (list_empty(&snd_event_client_list)) {
  207. dev_dbg(dev, "%s: No SND client registered\n", __func__);
  208. ret = -ENODEV;
  209. goto exit;
  210. }
  211. c = find_snd_event_client(dev);
  212. if (!c || (c->dev != dev)) {
  213. dev_dbg(dev, "%s: No matching snd dev found\n", __func__);
  214. ret = -ENODEV;
  215. goto exit;
  216. }
  217. c->state = false;
  218. if (master) {
  219. struct snd_event_client *d;
  220. bool dev_found = false;
  221. for (i = 0; i < master->clients->num_clients; i++) {
  222. d = master->clients->cl_arr[i].clnt;
  223. if (c->dev == d->dev) {
  224. dev_found = true;
  225. break;
  226. }
  227. }
  228. if (dev_found ) {
  229. if(master->clients_found) {
  230. ret = check_and_update_fwk_state();
  231. master->clients_found = false;
  232. }
  233. master->clients->cl_arr[i].dev = NULL;
  234. }
  235. }
  236. list_del(&c->node);
  237. kfree(c);
  238. exit:
  239. mutex_unlock(&snd_event_mutex);
  240. return ret;
  241. }
  242. EXPORT_SYMBOL(snd_event_client_deregister);
  243. /*
  244. * snd_event_mstr_add_client - Add a client to the master's list of clients
  245. *
  246. * @snd_clients: list of clients associated with this master
  247. * @compare: Pointer to the compare callback function that master will use to
  248. * confirm the clients
  249. * @data: Address to any additional data that the master wants to get back with
  250. * compare callback functions
  251. */
  252. void snd_event_mstr_add_client(struct snd_event_clients **snd_clients,
  253. int (*compare)(struct device *, void *),
  254. void *data)
  255. {
  256. struct snd_event_clients *client = *snd_clients;
  257. if (IS_ERR(client)) {
  258. pr_err_ratelimited("%s: snd_clients is invalid\n", __func__);
  259. return;
  260. }
  261. if (!client) {
  262. client = kzalloc(sizeof(*client), GFP_KERNEL);
  263. if (!client) {
  264. *snd_clients = ERR_PTR(-ENOMEM);
  265. return;
  266. }
  267. client->cl_arr = kzalloc(sizeof(struct snd_event_client_array),
  268. GFP_KERNEL);
  269. if (!client->cl_arr) {
  270. *snd_clients = ERR_PTR(-ENOMEM);
  271. return;
  272. }
  273. *snd_clients = client;
  274. } else {
  275. struct snd_event_client_array *new;
  276. new = krealloc(client->cl_arr,
  277. (client->num_clients + 1) * sizeof(*new),
  278. GFP_KERNEL | __GFP_ZERO);
  279. if (!new) {
  280. *snd_clients = ERR_PTR(-ENOMEM);
  281. return;
  282. }
  283. client->cl_arr = new;
  284. }
  285. client->cl_arr[client->num_clients].dev = NULL;
  286. client->cl_arr[client->num_clients].data = data;
  287. client->cl_arr[client->num_clients].compare = compare;
  288. client->num_clients++;
  289. }
  290. EXPORT_SYMBOL(snd_event_mstr_add_client);
  291. /*
  292. * snd_event_master_register - Register a master with the SND event FW
  293. *
  294. * @dev: Pointer to the "struct device" associated with the master
  295. * @ops: Pointer to the snd_event_ops struct for the master containing
  296. * callback functions
  297. * @clients: List of clients for the master
  298. * @data: Pointer to any additional data that the caller wants to get back
  299. * with callback functions
  300. *
  301. * Returns 0 on success or error on failure.
  302. *
  303. * Prerequisite:
  304. * clients list must not be empty.
  305. * All clients for the master must have to be registered by calling
  306. * snd_event_mstr_add_client() before calling this API to register a
  307. * master with SND event fwk.
  308. */
  309. int snd_event_master_register(struct device *dev,
  310. const struct snd_event_ops *ops,
  311. struct snd_event_clients *clients,
  312. void *data)
  313. {
  314. struct snd_master *new_master;
  315. int ret = 0;
  316. if (!dev) {
  317. pr_err_ratelimited("%s: dev is NULL\n", __func__);
  318. return -EINVAL;
  319. }
  320. mutex_lock(&snd_event_mutex);
  321. if (master) {
  322. dev_err(dev, "%s: master already allocated with %pK\n",
  323. __func__, master->dev);
  324. ret = -EALREADY;
  325. goto exit;
  326. }
  327. mutex_unlock(&snd_event_mutex);
  328. if (!clients || IS_ERR(clients)) {
  329. dev_err(dev, "%s: Invalid clients ptr\n", __func__);
  330. return -EINVAL;
  331. }
  332. new_master = kzalloc(sizeof(*new_master), GFP_KERNEL);
  333. if (!new_master)
  334. return -ENOMEM;
  335. new_master->dev = dev;
  336. new_master->ops = ops;
  337. new_master->data = data;
  338. new_master->clients = clients;
  339. dev_dbg(dev, "adding master to SND event FW (ops %pK)\n", ops);
  340. mutex_lock(&snd_event_mutex);
  341. master = new_master;
  342. ret = snd_event_find_clients(master);
  343. if (ret) {
  344. dev_dbg(dev, "%s: Failed to find all clients\n", __func__);
  345. ret = 0;
  346. goto exit;
  347. }
  348. master->clients_found = true;
  349. exit:
  350. mutex_unlock(&snd_event_mutex);
  351. return ret;
  352. }
  353. EXPORT_SYMBOL(snd_event_master_register);
  354. /*
  355. * snd_event_master_deregister - Remove a master from the SND event FW
  356. *
  357. * @dev: Pointer to the "struct device" associated with the master
  358. *
  359. * Returns 0 on success or error on failure.
  360. */
  361. int snd_event_master_deregister(struct device *dev)
  362. {
  363. int ret = 0;
  364. if (!dev) {
  365. pr_err_ratelimited("%s: dev is NULL\n", __func__);
  366. return -EINVAL;
  367. }
  368. mutex_lock(&snd_event_mutex);
  369. if (!master) {
  370. dev_dbg(dev, "%s: No master found\n", __func__);
  371. ret = -ENODEV;
  372. goto exit;
  373. }
  374. if (master->dev != dev) {
  375. dev_dbg(dev, "%s: device is not a Master\n", __func__);
  376. ret = -ENXIO;
  377. goto exit;
  378. }
  379. master->state = false;
  380. if (master && master->clients_found)
  381. ret = check_and_update_fwk_state();
  382. kfree(master->clients->cl_arr);
  383. kfree(master->clients);
  384. kfree(master);
  385. master = NULL;
  386. exit:
  387. mutex_unlock(&snd_event_mutex);
  388. return ret;
  389. }
  390. EXPORT_SYMBOL(snd_event_master_deregister);
  391. /*
  392. * snd_event_notify - Update the state of the Master/client in the SND event FW
  393. *
  394. * @dev: Pointer to the "struct device" associated with the master/client
  395. * @state: UP/DOWN state of the caller (master/client)
  396. *
  397. * Returns 0 on success or error on failure.
  398. */
  399. int snd_event_notify(struct device *dev, unsigned int state)
  400. {
  401. struct snd_event_client *c;
  402. int ret = 0;
  403. if (!dev) {
  404. pr_err_ratelimited("%s: dev is NULL\n", __func__);
  405. return -EINVAL;
  406. }
  407. dev_dbg(dev, "%s: snd_event_notify (state %u)\n",
  408. __func__, state);
  409. mutex_lock(&snd_event_mutex);
  410. if (list_empty(&snd_event_client_list) && !master) {
  411. dev_err_ratelimited(dev, "%s: No device registered\n", __func__);
  412. ret = -ENODEV;
  413. goto exit;
  414. }
  415. c = find_snd_event_client(dev);
  416. if (!c && (!master || (master->dev != dev))) {
  417. dev_err_ratelimited(dev, "%s: No snd dev entry found\n", __func__);
  418. ret = -ENXIO;
  419. goto exit;
  420. }
  421. if (c)
  422. c->state = !!state;
  423. else
  424. master->state = !!state;
  425. if (master && master->clients_found)
  426. ret = check_and_update_fwk_state();
  427. exit:
  428. mutex_unlock(&snd_event_mutex);
  429. return ret;
  430. }
  431. EXPORT_SYMBOL(snd_event_notify);
  432. MODULE_LICENSE("GPL v2");
  433. MODULE_DESCRIPTION("SND event module");