xref: /linux/drivers/usb/gadget/function/u_audio.h (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
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  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  */
19 
20 #ifndef __U_AUDIO_H
21 #define __U_AUDIO_H
22 
23 #include <linux/usb/composite.h>
24 
25 struct uac_params {
26 	/* playback */
27 	int p_chmask;	/* channel mask */
28 	int p_srate;	/* rate in Hz */
29 	int p_ssize;	/* sample size */
30 
31 	/* capture */
32 	int c_chmask;	/* channel mask */
33 	int c_srate;	/* rate in Hz */
34 	int c_ssize;	/* sample size */
35 
36 	int req_number; /* number of preallocated requests */
37 };
38 
39 struct g_audio {
40 	struct usb_function func;
41 	struct usb_gadget *gadget;
42 
43 	struct usb_ep *in_ep;
44 	struct usb_ep *out_ep;
45 
46 	/* Max packet size for all in_ep possible speeds */
47 	unsigned int in_ep_maxpsize;
48 	/* Max packet size for all out_ep possible speeds */
49 	unsigned int out_ep_maxpsize;
50 
51 	/* The ALSA Sound Card it represents on the USB-Client side */
52 	struct snd_uac_chip *uac;
53 
54 	struct uac_params params;
55 };
56 
57 static inline struct g_audio *func_to_g_audio(struct usb_function *f)
58 {
59 	return container_of(f, struct g_audio, func);
60 }
61 
62 static inline uint num_channels(uint chanmask)
63 {
64 	uint num = 0;
65 
66 	while (chanmask) {
67 		num += (chanmask & 1);
68 		chanmask >>= 1;
69 	}
70 
71 	return num;
72 }
73 
74 /*
75  * g_audio_setup - initialize one virtual ALSA sound card
76  * @g_audio: struct with filled params, in_ep_maxpsize, out_ep_maxpsize
77  * @pcm_name: the id string for a PCM instance of this sound card
78  * @card_name: name of this soundcard
79  *
80  * This sets up the single virtual ALSA sound card that may be exported by a
81  * gadget driver using this framework.
82  *
83  * Context: may sleep
84  *
85  * Returns zero on success, or a negative error on failure.
86  */
87 int g_audio_setup(struct g_audio *g_audio, const char *pcm_name,
88 					const char *card_name);
89 void g_audio_cleanup(struct g_audio *g_audio);
90 
91 int u_audio_start_capture(struct g_audio *g_audio);
92 void u_audio_stop_capture(struct g_audio *g_audio);
93 int u_audio_start_playback(struct g_audio *g_audio);
94 void u_audio_stop_playback(struct g_audio *g_audio);
95 
96 #endif /* __U_AUDIO_H */
97