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