u_audio.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * u_audio.h -- interface to USB gadget "ALSA sound card" utilities
  4. *
  5. * Copyright (C) 2016
  6. * Author: Ruslan Bilovol <[email protected]>
  7. */
  8. #ifndef __U_AUDIO_H
  9. #define __U_AUDIO_H
  10. #include <linux/usb/composite.h>
  11. #include "uac_common.h"
  12. /*
  13. * Same maximum frequency deviation on the slower side as in
  14. * sound/usb/endpoint.c. Value is expressed in per-mil deviation.
  15. */
  16. #define FBACK_SLOW_MAX 250
  17. /*
  18. * Maximum frequency deviation on the faster side, default value for UAC1/2.
  19. * Value is expressed in per-mil deviation.
  20. * UAC2 provides the value as a parameter as it impacts the endpoint required
  21. * bandwidth.
  22. */
  23. #define FBACK_FAST_MAX 5
  24. /* Feature Unit parameters */
  25. struct uac_fu_params {
  26. int id; /* Feature Unit ID */
  27. bool mute_present; /* mute control enable */
  28. bool volume_present; /* volume control enable */
  29. s16 volume_min; /* min volume in 1/256 dB */
  30. s16 volume_max; /* max volume in 1/256 dB */
  31. s16 volume_res; /* volume resolution in 1/256 dB */
  32. };
  33. struct uac_params {
  34. /* playback */
  35. int p_chmask; /* channel mask */
  36. int p_srates[UAC_MAX_RATES]; /* available rates in Hz (0 terminated list) */
  37. int p_ssize; /* sample size */
  38. struct uac_fu_params p_fu; /* Feature Unit parameters */
  39. /* capture */
  40. int c_chmask; /* channel mask */
  41. int c_srates[UAC_MAX_RATES]; /* available rates in Hz (0 terminated list) */
  42. int c_ssize; /* sample size */
  43. struct uac_fu_params c_fu; /* Feature Unit parameters */
  44. /* rates are dynamic, in uac_rtd_params */
  45. int req_number; /* number of preallocated requests */
  46. int fb_max; /* upper frequency drift feedback limit per-mil */
  47. };
  48. struct g_audio {
  49. struct usb_function func;
  50. struct usb_gadget *gadget;
  51. struct usb_ep *in_ep;
  52. struct usb_ep *out_ep;
  53. /* feedback IN endpoint corresponding to out_ep */
  54. struct usb_ep *in_ep_fback;
  55. /* Max packet size for all in_ep possible speeds */
  56. unsigned int in_ep_maxpsize;
  57. /* Max packet size for all out_ep possible speeds */
  58. unsigned int out_ep_maxpsize;
  59. /* Notify UAC driver about control change */
  60. int (*notify)(struct g_audio *g_audio, int unit_id, int cs);
  61. /* The ALSA Sound Card it represents on the USB-Client side */
  62. struct snd_uac_chip *uac;
  63. struct uac_params params;
  64. };
  65. static inline struct g_audio *func_to_g_audio(struct usb_function *f)
  66. {
  67. return container_of(f, struct g_audio, func);
  68. }
  69. static inline uint num_channels(uint chanmask)
  70. {
  71. uint num = 0;
  72. while (chanmask) {
  73. num += (chanmask & 1);
  74. chanmask >>= 1;
  75. }
  76. return num;
  77. }
  78. /*
  79. * g_audio_setup - initialize one virtual ALSA sound card
  80. * @g_audio: struct with filled params, in_ep_maxpsize, out_ep_maxpsize
  81. * @pcm_name: the id string for a PCM instance of this sound card
  82. * @card_name: name of this soundcard
  83. *
  84. * This sets up the single virtual ALSA sound card that may be exported by a
  85. * gadget driver using this framework.
  86. *
  87. * Context: may sleep
  88. *
  89. * Returns zero on success, or a negative error on failure.
  90. */
  91. int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
  92. const char *card_name);
  93. void g_audio_cleanup(struct g_audio *g_audio);
  94. int u_audio_start_capture(struct g_audio *g_audio);
  95. void u_audio_stop_capture(struct g_audio *g_audio);
  96. int u_audio_start_playback(struct g_audio *g_audio);
  97. void u_audio_stop_playback(struct g_audio *g_audio);
  98. int u_audio_get_capture_srate(struct g_audio *audio_dev, u32 *val);
  99. int u_audio_set_capture_srate(struct g_audio *audio_dev, int srate);
  100. int u_audio_get_playback_srate(struct g_audio *audio_dev, u32 *val);
  101. int u_audio_set_playback_srate(struct g_audio *audio_dev, int srate);
  102. int u_audio_get_volume(struct g_audio *g_audio, int playback, s16 *val);
  103. int u_audio_set_volume(struct g_audio *g_audio, int playback, s16 val);
  104. int u_audio_get_mute(struct g_audio *g_audio, int playback, int *val);
  105. int u_audio_set_mute(struct g_audio *g_audio, int playback, int val);
  106. void u_audio_suspend(struct g_audio *g_audio);
  107. #endif /* __U_AUDIO_H */