Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Oleksandr Andrushchenko and committed by
Takashi Iwai
cc3196ae 6d08b06e

+232 -1
+2
sound/Kconfig
··· 96 96 97 97 source "sound/synth/Kconfig" 98 98 99 + source "sound/xen/Kconfig" 100 + 99 101 endif # SND 100 102 101 103 endif # !UML
+1 -1
sound/Makefile
··· 5 5 obj-$(CONFIG_SOUND) += soundcore.o 6 6 obj-$(CONFIG_DMASOUND) += oss/dmasound/ 7 7 obj-$(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/ 9 9 obj-$(CONFIG_SND_AOA) += aoa/ 10 10 11 11 # This one must be compilable even if sound is configured out
+10
sound/xen/Kconfig
··· 1 + # ALSA Xen drivers 2 + 3 + config 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.
+5
sound/xen/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0 OR MIT 2 + 3 + snd_xen_front-objs := xen_snd_front.o 4 + 5 + obj-$(CONFIG_SND_XEN_FRONTEND) += snd_xen_front.o
+196
sound/xen/xen_snd_front.c
··· 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 + 22 + static void xen_snd_drv_fini(struct xen_snd_front_info *front_info) 23 + { 24 + } 25 + 26 + static int sndback_initwait(struct xen_snd_front_info *front_info) 27 + { 28 + return 0; 29 + } 30 + 31 + static int sndback_connect(struct xen_snd_front_info *front_info) 32 + { 33 + return 0; 34 + } 35 + 36 + static 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 + 42 + static 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 + 107 + static 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 + 123 + static 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 + 160 + static const struct xenbus_device_id xen_drv_ids[] = { 161 + { XENSND_DRIVER_NAME }, 162 + { "" } 163 + }; 164 + 165 + static 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 + 172 + static 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 + 184 + static 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 + 190 + module_init(xen_drv_init); 191 + module_exit(xen_drv_fini); 192 + 193 + MODULE_DESCRIPTION("Xen virtual sound device frontend"); 194 + MODULE_LICENSE("GPL"); 195 + MODULE_ALIAS("xen:" XENSND_DRIVER_NAME); 196 + MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual soundcard}}");
+18
sound/xen/xen_snd_front.h
··· 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 + 14 + struct xen_snd_front_info { 15 + struct xenbus_device *xb_dev; 16 + }; 17 + 18 + #endif /* __XEN_SND_FRONT_H */