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 struct uac_params {
23 	/* playback */
24 	int p_chmask;	/* channel mask */
25 	int p_srate;	/* rate in Hz */
26 	int p_ssize;	/* sample size */
27 
28 	/* capture */
29 	int c_chmask;	/* channel mask */
30 	int c_srate;	/* rate in Hz */
31 	int c_ssize;	/* sample size */
32 
33 	int req_number; /* number of preallocated requests */
34 	int fb_max;	/* upper frequency drift feedback limit per-mil */
35 };
36 
37 struct g_audio {
38 	struct usb_function func;
39 	struct usb_gadget *gadget;
40 
41 	struct usb_ep *in_ep;
42 
43 	struct usb_ep *out_ep;
44 	/* feedback IN endpoint corresponding to out_ep */
45 	struct usb_ep *in_ep_fback;
46 
47 	/* Max packet size for all in_ep possible speeds */
48 	unsigned int in_ep_maxpsize;
49 	/* Max packet size for all out_ep possible speeds */
50 	unsigned int out_ep_maxpsize;
51 
52 	/* The ALSA Sound Card it represents on the USB-Client side */
53 	struct snd_uac_chip *uac;
54 
55 	struct uac_params params;
56 };
57 
58 static inline struct g_audio *func_to_g_audio(struct usb_function *f)
59 {
60 	return container_of(f, struct g_audio, func);
61 }
62 
63 static inline uint num_channels(uint chanmask)
64 {
65 	uint num = 0;
66 
67 	while (chanmask) {
68 		num += (chanmask & 1);
69 		chanmask >>= 1;
70 	}
71 
72 	return num;
73 }
74 
75 /*
76  * g_audio_setup - initialize one virtual ALSA sound card
77  * @g_audio: struct with filled params, in_ep_maxpsize, out_ep_maxpsize
78  * @pcm_name: the id string for a PCM instance of this sound card
79  * @card_name: name of this soundcard
80  *
81  * This sets up the single virtual ALSA sound card that may be exported by a
82  * gadget driver using this framework.
83  *
84  * Context: may sleep
85  *
86  * Returns zero on success, or a negative error on failure.
87  */
88 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
89 					const char *card_name);
90 void g_audio_cleanup(struct g_audio *g_audio);
91 
92 int u_audio_start_capture(struct g_audio *g_audio);
93 void u_audio_stop_capture(struct g_audio *g_audio);
94 int u_audio_start_playback(struct g_audio *g_audio);
95 void u_audio_stop_playback(struct g_audio *g_audio);
96 
97 #endif /* __U_AUDIO_H */
98