snd_event.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2018, 2020 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. dev_dbg(dev, "%s: removing client to SND event FW \n",
  203. __func__);
  204. mutex_lock(&snd_event_mutex);
  205. if (list_empty(&snd_event_client_list)) {
  206. dev_dbg(dev, "%s: No SND client registered\n", __func__);
  207. ret = -ENODEV;
  208. goto exit;
  209. }
  210. c = find_snd_event_client(dev);
  211. if (!c || (c->dev != dev)) {
  212. dev_dbg(dev, "%s: No matching snd dev found\n", __func__);
  213. ret = -ENODEV;
  214. goto exit;
  215. }
  216. c->state = false;
  217. if (master) {
  218. struct snd_event_client *d;
  219. bool dev_found = false;
  220. for (i = 0; i < master->clients->num_clients; i++) {
  221. d = master->clients->cl_arr[i].clnt;
  222. if (c->dev == d->dev) {
  223. dev_found = true;
  224. break;
  225. }
  226. }
  227. if (dev_found ) {
  228. if(master->clients_found) {
  229. ret = check_and_update_fwk_state();
  230. master->clients_found = false;
  231. }
  232. master->clients->cl_arr[i].dev = NULL;
  233. }
  234. }
  235. list_del(&c->node);
  236. kfree(c);
  237. exit:
  238. mutex_unlock(&snd_event_mutex);
  239. return ret;
  240. }
  241. EXPORT_SYMBOL(snd_event_client_deregister);
  242. /*
  243. * snd_event_mstr_add_client - Add a client to the master's list of clients
  244. *
  245. * @snd_clients: list of clients associated with this master
  246. * @compare: Pointer to the compare callback function that master will use to
  247. * confirm the clients
  248. * @data: Address to any additional data that the master wants to get back with
  249. * compare callback functions
  250. */
  251. void snd_event_mstr_add_client(struct snd_event_clients **snd_clients,
  252. int (*compare)(struct device *, void *),
  253. void *data)
  254. {
  255. struct snd_event_clients *client = *snd_clients;
  256. if (IS_ERR(client)) {
  257. pr_err("%s: snd_clients is invalid\n", __func__);
  258. return;
  259. }
  260. if (!client) {
  261. client = kzalloc(sizeof(*client), GFP_KERNEL);
  262. if (!client) {
  263. *snd_clients = ERR_PTR(-ENOMEM);
  264. return;
  265. }
  266. client->cl_arr = kzalloc(sizeof(struct snd_event_client_array),
  267. GFP_KERNEL);
  268. if (!client->cl_arr) {
  269. *snd_clients = ERR_PTR(-ENOMEM);
  270. return;
  271. }
  272. *snd_clients = client;
  273. } else {
  274. struct snd_event_client_array *new;
  275. new = krealloc(client->cl_arr,
  276. (client->num_clients + 1) * sizeof(*new),
  277. GFP_KERNEL | __GFP_ZERO);
  278. if (!new) {
  279. *snd_clients = ERR_PTR(-ENOMEM);
  280. return;
  281. }
  282. client->cl_arr = new;
  283. }
  284. client->cl_arr[client->num_clients].dev = NULL;
  285. client->cl_arr[client->num_clients].data = data;
  286. client->cl_arr[client->num_clients].compare = compare;
  287. client->num_clients++;
  288. }
  289. EXPORT_SYMBOL(snd_event_mstr_add_client);
  290. /*
  291. * snd_event_master_register - Register a master with the SND event FW
  292. *
  293. * @dev: Pointer to the "struct device" associated with the master
  294. * @ops: Pointer to the snd_event_ops struct for the master containing
  295. * callback functions
  296. * @clients: List of clients for the master
  297. * @data: Pointer to any additional data that the caller wants to get back
  298. * with callback functions
  299. *
  300. * Returns 0 on success or error on failure.
  301. *
  302. * Prerequisite:
  303. * clients list must not be empty.
  304. * All clients for the master must have to be registered by calling
  305. * snd_event_mstr_add_client() before calling this API to register a
  306. * master with SND event fwk.
  307. */
  308. int snd_event_master_register(struct device *dev,
  309. const struct snd_event_ops *ops,
  310. struct snd_event_clients *clients,
  311. void *data)
  312. {
  313. struct snd_master *new_master;
  314. int ret = 0;
  315. if (!dev) {
  316. pr_err("%s: dev is NULL\n", __func__);
  317. return -EINVAL;
  318. }
  319. mutex_lock(&snd_event_mutex);
  320. if (master) {
  321. dev_err(dev, "%s: master already allocated with %pK\n",
  322. __func__, master->dev);
  323. ret = -EALREADY;
  324. goto exit;
  325. }
  326. mutex_unlock(&snd_event_mutex);
  327. if (!clients || IS_ERR(clients)) {
  328. dev_err(dev, "%s: Invalid clients ptr\n", __func__);
  329. return -EINVAL;
  330. }
  331. new_master = kzalloc(sizeof(*new_master), GFP_KERNEL);
  332. if (!new_master)
  333. return -ENOMEM;
  334. new_master->dev = dev;
  335. new_master->ops = ops;
  336. new_master->data = data;
  337. new_master->clients = clients;
  338. dev_dbg(dev, "adding master to SND event FW (ops %pK)\n", ops);
  339. mutex_lock(&snd_event_mutex);
  340. master = new_master;
  341. ret = snd_event_find_clients(master);
  342. if (ret) {
  343. dev_dbg(dev, "%s: Failed to find all clients\n", __func__);
  344. ret = 0;
  345. goto exit;
  346. }
  347. master->clients_found = true;
  348. exit:
  349. mutex_unlock(&snd_event_mutex);
  350. return ret;
  351. }
  352. EXPORT_SYMBOL(snd_event_master_register);
  353. /*
  354. * snd_event_master_deregister - Remove a master from the SND event FW
  355. *
  356. * @dev: Pointer to the "struct device" associated with the master
  357. *
  358. * Returns 0 on success or error on failure.
  359. */
  360. int snd_event_master_deregister(struct device *dev)
  361. {
  362. int ret = 0;
  363. if (!dev) {
  364. pr_err("%s: dev is NULL\n", __func__);
  365. return -EINVAL;
  366. }
  367. mutex_lock(&snd_event_mutex);
  368. if (!master) {
  369. dev_dbg(dev, "%s: No master found\n", __func__);
  370. ret = -ENODEV;
  371. goto exit;
  372. }
  373. if (master->dev != dev) {
  374. dev_dbg(dev, "%s: device is not a Master\n", __func__);
  375. ret = -ENXIO;
  376. goto exit;
  377. }
  378. master->state = false;
  379. if (master && master->clients_found)
  380. ret = check_and_update_fwk_state();
  381. kfree(master->clients->cl_arr);
  382. kfree(master->clients);
  383. kfree(master);
  384. master = NULL;
  385. exit:
  386. mutex_unlock(&snd_event_mutex);
  387. return ret;
  388. }
  389. EXPORT_SYMBOL(snd_event_master_deregister);
  390. /*
  391. * snd_event_notify - Update the state of the Master/client in the SND event FW
  392. *
  393. * @dev: Pointer to the "struct device" associated with the master/client
  394. * @state: UP/DOWN state of the caller (master/client)
  395. *
  396. * Returns 0 on success or error on failure.
  397. */
  398. int snd_event_notify(struct device *dev, unsigned int state)
  399. {
  400. struct snd_event_client *c;
  401. int ret = 0;
  402. if (!dev) {
  403. pr_err("%s: dev is NULL\n", __func__);
  404. return -EINVAL;
  405. }
  406. dev_dbg(dev, "%s: snd_event_notify (state %u)\n",
  407. __func__, state);
  408. mutex_lock(&snd_event_mutex);
  409. if (list_empty(&snd_event_client_list) && !master) {
  410. dev_err(dev, "%s: No device registered\n", __func__);
  411. ret = -ENODEV;
  412. goto exit;
  413. }
  414. c = find_snd_event_client(dev);
  415. if (!c && (!master || (master->dev != dev))) {
  416. dev_err(dev, "%s: No snd dev entry found\n", __func__);
  417. ret = -ENXIO;
  418. goto exit;
  419. }
  420. if (c)
  421. c->state = !!state;
  422. else
  423. master->state = !!state;
  424. if (master && master->clients_found)
  425. ret = check_and_update_fwk_state();
  426. exit:
  427. mutex_unlock(&snd_event_mutex);
  428. return ret;
  429. }
  430. EXPORT_SYMBOL(snd_event_notify);
  431. MODULE_LICENSE("GPL v2");
  432. MODULE_DESCRIPTION("SND event module");