snd_event.c 11 KB

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