soc-usb.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2022-2025 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/of.h>
  6. #include <linux/usb.h>
  7. #include <sound/jack.h>
  8. #include <sound/soc-usb.h>
  9. #include "../usb/card.h"
  10. static DEFINE_MUTEX(ctx_mutex);
  11. static LIST_HEAD(usb_ctx_list);
  12. static struct device_node *snd_soc_find_phandle(struct device *dev)
  13. {
  14. struct device_node *node;
  15. node = of_parse_phandle(dev->of_node, "usb-soc-be", 0);
  16. if (!node)
  17. return ERR_PTR(-ENODEV);
  18. return node;
  19. }
  20. static struct snd_soc_usb *snd_soc_usb_ctx_lookup(struct device_node *node)
  21. {
  22. struct snd_soc_usb *ctx;
  23. if (!node)
  24. return NULL;
  25. list_for_each_entry(ctx, &usb_ctx_list, list) {
  26. if (ctx->component->dev->of_node == node)
  27. return ctx;
  28. }
  29. return NULL;
  30. }
  31. static struct snd_soc_usb *snd_soc_find_usb_ctx(struct device *dev)
  32. {
  33. struct snd_soc_usb *ctx;
  34. struct device_node *node;
  35. node = snd_soc_find_phandle(dev);
  36. if (!IS_ERR(node)) {
  37. ctx = snd_soc_usb_ctx_lookup(node);
  38. of_node_put(node);
  39. } else {
  40. ctx = snd_soc_usb_ctx_lookup(dev->of_node);
  41. }
  42. return ctx ? ctx : NULL;
  43. }
  44. /* SOC USB sound kcontrols */
  45. /**
  46. * snd_soc_usb_setup_offload_jack() - Create USB offloading jack
  47. * @component: USB DPCM backend DAI component
  48. * @jack: jack structure to create
  49. *
  50. * Creates a jack device for notifying userspace of the availability
  51. * of an offload capable device.
  52. *
  53. * Returns 0 on success, negative on error.
  54. *
  55. */
  56. int snd_soc_usb_setup_offload_jack(struct snd_soc_component *component,
  57. struct snd_soc_jack *jack)
  58. {
  59. int ret;
  60. ret = snd_soc_card_jack_new(component->card, "USB Offload Jack",
  61. SND_JACK_USB, jack);
  62. if (ret < 0) {
  63. dev_err(component->card->dev, "Unable to add USB offload jack: %d\n",
  64. ret);
  65. return ret;
  66. }
  67. ret = snd_soc_component_set_jack(component, jack, NULL);
  68. if (ret) {
  69. dev_err(component->card->dev, "Failed to set jack: %d\n", ret);
  70. return ret;
  71. }
  72. return 0;
  73. }
  74. EXPORT_SYMBOL_GPL(snd_soc_usb_setup_offload_jack);
  75. /**
  76. * snd_soc_usb_update_offload_route - Find active USB offload path
  77. * @dev: USB device to get offload status
  78. * @card: USB card index
  79. * @pcm: USB PCM device index
  80. * @direction: playback or capture direction
  81. * @path: pcm or card index
  82. * @route: pointer to route output array
  83. *
  84. * Fetch the current status for the USB SND card and PCM device indexes
  85. * specified. The "route" argument should be an array of integers being
  86. * used for a kcontrol output. The first element should have the selected
  87. * card index, and the second element should have the selected pcm device
  88. * index.
  89. */
  90. int snd_soc_usb_update_offload_route(struct device *dev, int card, int pcm,
  91. int direction, enum snd_soc_usb_kctl path,
  92. long *route)
  93. {
  94. struct snd_soc_usb *ctx;
  95. int ret = -ENODEV;
  96. mutex_lock(&ctx_mutex);
  97. ctx = snd_soc_find_usb_ctx(dev);
  98. if (!ctx)
  99. goto exit;
  100. if (ctx->update_offload_route_info)
  101. ret = ctx->update_offload_route_info(ctx->component, card, pcm,
  102. direction, path, route);
  103. exit:
  104. mutex_unlock(&ctx_mutex);
  105. return ret;
  106. }
  107. EXPORT_SYMBOL_GPL(snd_soc_usb_update_offload_route);
  108. /**
  109. * snd_soc_usb_find_priv_data() - Retrieve private data stored
  110. * @usbdev: device reference
  111. *
  112. * Fetch the private data stored in the USB SND SoC structure.
  113. *
  114. */
  115. void *snd_soc_usb_find_priv_data(struct device *usbdev)
  116. {
  117. struct snd_soc_usb *ctx;
  118. mutex_lock(&ctx_mutex);
  119. ctx = snd_soc_find_usb_ctx(usbdev);
  120. mutex_unlock(&ctx_mutex);
  121. return ctx ? ctx->priv_data : NULL;
  122. }
  123. EXPORT_SYMBOL_GPL(snd_soc_usb_find_priv_data);
  124. /**
  125. * snd_soc_usb_find_supported_format() - Check if audio format is supported
  126. * @card_idx: USB sound chip array index
  127. * @params: PCM parameters
  128. * @direction: capture or playback
  129. *
  130. * Ensure that a requested audio profile from the ASoC side is able to be
  131. * supported by the USB device.
  132. *
  133. * Return 0 on success, negative on error.
  134. *
  135. */
  136. int snd_soc_usb_find_supported_format(int card_idx,
  137. struct snd_pcm_hw_params *params,
  138. int direction)
  139. {
  140. struct snd_usb_stream *as;
  141. as = snd_usb_find_suppported_substream(card_idx, params, direction);
  142. if (!as)
  143. return -EOPNOTSUPP;
  144. return 0;
  145. }
  146. EXPORT_SYMBOL_GPL(snd_soc_usb_find_supported_format);
  147. /**
  148. * snd_soc_usb_allocate_port() - allocate a SoC USB port for offloading support
  149. * @component: USB DPCM backend DAI component
  150. * @data: private data
  151. *
  152. * Allocate and initialize a SoC USB port. The SoC USB port is used to communicate
  153. * different USB audio devices attached, in order to start audio offloading handled
  154. * by an ASoC entity. USB device plug in/out events are signaled with a
  155. * notification, but don't directly impact the memory allocated for the SoC USB
  156. * port.
  157. *
  158. */
  159. struct snd_soc_usb *snd_soc_usb_allocate_port(struct snd_soc_component *component,
  160. void *data)
  161. {
  162. struct snd_soc_usb *usb;
  163. usb = kzalloc_obj(*usb);
  164. if (!usb)
  165. return ERR_PTR(-ENOMEM);
  166. usb->component = component;
  167. usb->priv_data = data;
  168. return usb;
  169. }
  170. EXPORT_SYMBOL_GPL(snd_soc_usb_allocate_port);
  171. /**
  172. * snd_soc_usb_free_port() - free a SoC USB port used for offloading support
  173. * @usb: allocated SoC USB port
  174. *
  175. * Free and remove the SoC USB port from the available list of ports. This will
  176. * ensure that the communication between USB SND and ASoC is halted.
  177. *
  178. */
  179. void snd_soc_usb_free_port(struct snd_soc_usb *usb)
  180. {
  181. snd_soc_usb_remove_port(usb);
  182. kfree(usb);
  183. }
  184. EXPORT_SYMBOL_GPL(snd_soc_usb_free_port);
  185. /**
  186. * snd_soc_usb_add_port() - Add a USB backend port
  187. * @usb: soc usb port to add
  188. *
  189. * Register a USB backend DAI link to the USB SoC framework. Memory is allocated
  190. * as part of the USB backend DAI link.
  191. *
  192. */
  193. void snd_soc_usb_add_port(struct snd_soc_usb *usb)
  194. {
  195. mutex_lock(&ctx_mutex);
  196. list_add_tail(&usb->list, &usb_ctx_list);
  197. mutex_unlock(&ctx_mutex);
  198. snd_usb_rediscover_devices();
  199. }
  200. EXPORT_SYMBOL_GPL(snd_soc_usb_add_port);
  201. /**
  202. * snd_soc_usb_remove_port() - Remove a USB backend port
  203. * @usb: soc usb port to remove
  204. *
  205. * Remove a USB backend DAI link from USB SoC. Memory is freed when USB backend
  206. * DAI is removed, or when snd_soc_usb_free_port() is called.
  207. *
  208. */
  209. void snd_soc_usb_remove_port(struct snd_soc_usb *usb)
  210. {
  211. struct snd_soc_usb *ctx, *tmp;
  212. mutex_lock(&ctx_mutex);
  213. list_for_each_entry_safe(ctx, tmp, &usb_ctx_list, list) {
  214. if (ctx == usb) {
  215. list_del(&ctx->list);
  216. break;
  217. }
  218. }
  219. mutex_unlock(&ctx_mutex);
  220. }
  221. EXPORT_SYMBOL_GPL(snd_soc_usb_remove_port);
  222. /**
  223. * snd_soc_usb_connect() - Notification of USB device connection
  224. * @usbdev: USB bus device
  225. * @sdev: USB SND device to add
  226. *
  227. * Notify of a new USB SND device connection. The sdev->card_idx can be used to
  228. * handle how the DPCM backend selects, which device to enable USB offloading
  229. * on.
  230. *
  231. */
  232. int snd_soc_usb_connect(struct device *usbdev, struct snd_soc_usb_device *sdev)
  233. {
  234. struct snd_soc_usb *ctx;
  235. if (!usbdev)
  236. return -ENODEV;
  237. mutex_lock(&ctx_mutex);
  238. ctx = snd_soc_find_usb_ctx(usbdev);
  239. if (!ctx)
  240. goto exit;
  241. if (ctx->connection_status_cb)
  242. ctx->connection_status_cb(ctx, sdev, true);
  243. exit:
  244. mutex_unlock(&ctx_mutex);
  245. return 0;
  246. }
  247. EXPORT_SYMBOL_GPL(snd_soc_usb_connect);
  248. /**
  249. * snd_soc_usb_disconnect() - Notification of USB device disconnection
  250. * @usbdev: USB bus device
  251. * @sdev: USB SND device to remove
  252. *
  253. * Notify of a new USB SND device disconnection to the USB backend.
  254. *
  255. */
  256. int snd_soc_usb_disconnect(struct device *usbdev, struct snd_soc_usb_device *sdev)
  257. {
  258. struct snd_soc_usb *ctx;
  259. if (!usbdev)
  260. return -ENODEV;
  261. mutex_lock(&ctx_mutex);
  262. ctx = snd_soc_find_usb_ctx(usbdev);
  263. if (!ctx)
  264. goto exit;
  265. if (ctx->connection_status_cb)
  266. ctx->connection_status_cb(ctx, sdev, false);
  267. exit:
  268. mutex_unlock(&ctx_mutex);
  269. return 0;
  270. }
  271. EXPORT_SYMBOL_GPL(snd_soc_usb_disconnect);
  272. MODULE_LICENSE("GPL");
  273. MODULE_DESCRIPTION("SoC USB driver for offloading");