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