summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>2018-05-14 02:27:37 -0400
committerTakashi Iwai <tiwai@suse.de>2018-05-16 06:58:36 -0400
commitcc3196ae197c28cd6db0a2e9ddddc2e0aa1e694f (patch)
tree561d3805088ea7ab71c0ef0ad32f4458f6905807
parent6d08b06e67cd117f6992c46611dfb4ce267cd71e (diff)
ALSA: xen-front: Introduce Xen para-virtualized sound frontend driver
Introduce skeleton of the para-virtualized Xen sound frontend driver. Initial handling for Xen bus states: implement Xen bus state machine for the frontend driver according to the state diagram and recovery flow from sound para-virtualized protocol: xen/interface/io/sndif.h. Signed-off-by: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com> Reviewed-by: Juergen Gross <jgross@suse.com> Signed-off-by: Takashi Iwai <tiwai@suse.de>
-rw-r--r--sound/Kconfig2
-rw-r--r--sound/Makefile2
-rw-r--r--sound/xen/Kconfig10
-rw-r--r--sound/xen/Makefile5
-rw-r--r--sound/xen/xen_snd_front.c196
-rw-r--r--sound/xen/xen_snd_front.h18
6 files changed, 232 insertions, 1 deletions
diff --git a/sound/Kconfig b/sound/Kconfig
index 6833db9002ec..1140e9988fc5 100644
--- a/sound/Kconfig
+++ b/sound/Kconfig
@@ -96,6 +96,8 @@ source "sound/x86/Kconfig"
96 96
97source "sound/synth/Kconfig" 97source "sound/synth/Kconfig"
98 98
99source "sound/xen/Kconfig"
100
99endif # SND 101endif # SND
100 102
101endif # !UML 103endif # !UML
diff --git a/sound/Makefile b/sound/Makefile
index 99d8c31262c8..797ecdcd35e2 100644
--- a/sound/Makefile
+++ b/sound/Makefile
@@ -5,7 +5,7 @@
5obj-$(CONFIG_SOUND) += soundcore.o 5obj-$(CONFIG_SOUND) += soundcore.o
6obj-$(CONFIG_DMASOUND) += oss/dmasound/ 6obj-$(CONFIG_DMASOUND) += oss/dmasound/
7obj-$(CONFIG_SND) += core/ i2c/ drivers/ isa/ pci/ ppc/ arm/ sh/ synth/ usb/ \ 7obj-$(CONFIG_SND) += core/ i2c/ drivers/ isa/ pci/ ppc/ arm/ sh/ synth/ usb/ \
8 firewire/ sparc/ spi/ parisc/ pcmcia/ mips/ soc/ atmel/ hda/ x86/ 8 firewire/ sparc/ spi/ parisc/ pcmcia/ mips/ soc/ atmel/ hda/ x86/ xen/
9obj-$(CONFIG_SND_AOA) += aoa/ 9obj-$(CONFIG_SND_AOA) += aoa/
10 10
11# This one must be compilable even if sound is configured out 11# This one must be compilable even if sound is configured out
diff --git a/sound/xen/Kconfig b/sound/xen/Kconfig
new file mode 100644
index 000000000000..4f1fceea82d2
--- /dev/null
+++ b/sound/xen/Kconfig
@@ -0,0 +1,10 @@
1# ALSA Xen drivers
2
3config SND_XEN_FRONTEND
4 tristate "Xen para-virtualized sound frontend driver"
5 depends on XEN
6 select SND_PCM
7 select XEN_XENBUS_FRONTEND
8 help
9 Choose this option if you want to enable a para-virtualized
10 frontend sound driver for Xen guest OSes.
diff --git a/sound/xen/Makefile b/sound/xen/Makefile
new file mode 100644
index 000000000000..4507ef3c27fd
--- /dev/null
+++ b/sound/xen/Makefile
@@ -0,0 +1,5 @@
1# SPDX-License-Identifier: GPL-2.0 OR MIT
2
3snd_xen_front-objs := xen_snd_front.o
4
5obj-$(CONFIG_SND_XEN_FRONTEND) += snd_xen_front.o
diff --git a/sound/xen/xen_snd_front.c b/sound/xen/xen_snd_front.c
new file mode 100644
index 000000000000..bbbe2767b565
--- /dev/null
+++ b/sound/xen/xen_snd_front.c
@@ -0,0 +1,196 @@
1// SPDX-License-Identifier: GPL-2.0 OR MIT
2
3/*
4 * Xen para-virtual sound device
5 *
6 * Copyright (C) 2016-2018 EPAM Systems Inc.
7 *
8 * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
9 */
10
11#include <linux/delay.h>
12#include <linux/module.h>
13
14#include <xen/platform_pci.h>
15#include <xen/xen.h>
16#include <xen/xenbus.h>
17
18#include <xen/interface/io/sndif.h>
19
20#include "xen_snd_front.h"
21
22static void xen_snd_drv_fini(struct xen_snd_front_info *front_info)
23{
24}
25
26static int sndback_initwait(struct xen_snd_front_info *front_info)
27{
28 return 0;
29}
30
31static int sndback_connect(struct xen_snd_front_info *front_info)
32{
33 return 0;
34}
35
36static void sndback_disconnect(struct xen_snd_front_info *front_info)
37{
38 xen_snd_drv_fini(front_info);
39 xenbus_switch_state(front_info->xb_dev, XenbusStateInitialising);
40}
41
42static void sndback_changed(struct xenbus_device *xb_dev,
43 enum xenbus_state backend_state)
44{
45 struct xen_snd_front_info *front_info = dev_get_drvdata(&xb_dev->dev);
46 int ret;
47
48 dev_dbg(&xb_dev->dev, "Backend state is %s, front is %s\n",
49 xenbus_strstate(backend_state),
50 xenbus_strstate(xb_dev->state));
51
52 switch (backend_state) {
53 case XenbusStateReconfiguring:
54 /* fall through */
55 case XenbusStateReconfigured:
56 /* fall through */
57 case XenbusStateInitialised:
58 /* fall through */
59 break;
60
61 case XenbusStateInitialising:
62 /* Recovering after backend unexpected closure. */
63 sndback_disconnect(front_info);
64 break;
65
66 case XenbusStateInitWait:
67 /* Recovering after backend unexpected closure. */
68 sndback_disconnect(front_info);
69
70 ret = sndback_initwait(front_info);
71 if (ret < 0)
72 xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
73 else
74 xenbus_switch_state(xb_dev, XenbusStateInitialised);
75 break;
76
77 case XenbusStateConnected:
78 if (xb_dev->state != XenbusStateInitialised)
79 break;
80
81 ret = sndback_connect(front_info);
82 if (ret < 0)
83 xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
84 else
85 xenbus_switch_state(xb_dev, XenbusStateConnected);
86 break;
87
88 case XenbusStateClosing:
89 /*
90 * In this state backend starts freeing resources,
91 * so let it go into closed state first, so we can also
92 * remove ours.
93 */
94 break;
95
96 case XenbusStateUnknown:
97 /* fall through */
98 case XenbusStateClosed:
99 if (xb_dev->state == XenbusStateClosed)
100 break;
101
102 sndback_disconnect(front_info);
103 break;
104 }
105}
106
107static int xen_drv_probe(struct xenbus_device *xb_dev,
108 const struct xenbus_device_id *id)
109{
110 struct xen_snd_front_info *front_info;
111
112 front_info = devm_kzalloc(&xb_dev->dev,
113 sizeof(*front_info), GFP_KERNEL);
114 if (!front_info)
115 return -ENOMEM;
116
117 front_info->xb_dev = xb_dev;
118 dev_set_drvdata(&xb_dev->dev, front_info);
119
120 return xenbus_switch_state(xb_dev, XenbusStateInitialising);
121}
122
123static int xen_drv_remove(struct xenbus_device *dev)
124{
125 struct xen_snd_front_info *front_info = dev_get_drvdata(&dev->dev);
126 int to = 100;
127
128 xenbus_switch_state(dev, XenbusStateClosing);
129
130 /*
131 * On driver removal it is disconnected from XenBus,
132 * so no backend state change events come via .otherend_changed
133 * callback. This prevents us from exiting gracefully, e.g.
134 * signaling the backend to free event channels, waiting for its
135 * state to change to XenbusStateClosed and cleaning at our end.
136 * Normally when front driver removed backend will finally go into
137 * XenbusStateInitWait state.
138 *
139 * Workaround: read backend's state manually and wait with time-out.
140 */
141 while ((xenbus_read_unsigned(front_info->xb_dev->otherend, "state",
142 XenbusStateUnknown) != XenbusStateInitWait) &&
143 to--)
144 msleep(10);
145
146 if (!to) {
147 unsigned int state;
148
149 state = xenbus_read_unsigned(front_info->xb_dev->otherend,
150 "state", XenbusStateUnknown);
151 pr_err("Backend state is %s while removing driver\n",
152 xenbus_strstate(state));
153 }
154
155 xen_snd_drv_fini(front_info);
156 xenbus_frontend_closed(dev);
157 return 0;
158}
159
160static const struct xenbus_device_id xen_drv_ids[] = {
161 { XENSND_DRIVER_NAME },
162 { "" }
163};
164
165static struct xenbus_driver xen_driver = {
166 .ids = xen_drv_ids,
167 .probe = xen_drv_probe,
168 .remove = xen_drv_remove,
169 .otherend_changed = sndback_changed,
170};
171
172static int __init xen_drv_init(void)
173{
174 if (!xen_domain())
175 return -ENODEV;
176
177 if (!xen_has_pv_devices())
178 return -ENODEV;
179
180 pr_info("Initialising Xen " XENSND_DRIVER_NAME " frontend driver\n");
181 return xenbus_register_frontend(&xen_driver);
182}
183
184static void __exit xen_drv_fini(void)
185{
186 pr_info("Unregistering Xen " XENSND_DRIVER_NAME " frontend driver\n");
187 xenbus_unregister_driver(&xen_driver);
188}
189
190module_init(xen_drv_init);
191module_exit(xen_drv_fini);
192
193MODULE_DESCRIPTION("Xen virtual sound device frontend");
194MODULE_LICENSE("GPL");
195MODULE_ALIAS("xen:" XENSND_DRIVER_NAME);
196MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual soundcard}}");
diff --git a/sound/xen/xen_snd_front.h b/sound/xen/xen_snd_front.h
new file mode 100644
index 000000000000..4ae204b23d32
--- /dev/null
+++ b/sound/xen/xen_snd_front.h
@@ -0,0 +1,18 @@
1/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2
3/*
4 * Xen para-virtual sound device
5 *
6 * Copyright (C) 2016-2018 EPAM Systems Inc.
7 *
8 * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
9 */
10
11#ifndef __XEN_SND_FRONT_H
12#define __XEN_SND_FRONT_H
13
14struct xen_snd_front_info {
15 struct xenbus_device *xb_dev;
16};
17
18#endif /* __XEN_SND_FRONT_H */