xref: /linux/sound/xen/xen_snd_front.c (revision fd3b36045c2c612b5f44a47f388677af256d1d0a)
1cc3196aeSOleksandr Andrushchenko // SPDX-License-Identifier: GPL-2.0 OR MIT
2cc3196aeSOleksandr Andrushchenko 
3cc3196aeSOleksandr Andrushchenko /*
4cc3196aeSOleksandr Andrushchenko  * Xen para-virtual sound device
5cc3196aeSOleksandr Andrushchenko  *
6cc3196aeSOleksandr Andrushchenko  * Copyright (C) 2016-2018 EPAM Systems Inc.
7cc3196aeSOleksandr Andrushchenko  *
8cc3196aeSOleksandr Andrushchenko  * Author: Oleksandr Andrushchenko <oleksandr_andrushchenko@epam.com>
9cc3196aeSOleksandr Andrushchenko  */
10cc3196aeSOleksandr Andrushchenko 
11cc3196aeSOleksandr Andrushchenko #include <linux/delay.h>
12cc3196aeSOleksandr Andrushchenko #include <linux/module.h>
13cc3196aeSOleksandr Andrushchenko 
14cc3196aeSOleksandr Andrushchenko #include <xen/platform_pci.h>
15cc3196aeSOleksandr Andrushchenko #include <xen/xen.h>
16cc3196aeSOleksandr Andrushchenko #include <xen/xenbus.h>
17cc3196aeSOleksandr Andrushchenko 
18cc3196aeSOleksandr Andrushchenko #include <xen/interface/io/sndif.h>
19cc3196aeSOleksandr Andrushchenko 
20cc3196aeSOleksandr Andrushchenko #include "xen_snd_front.h"
21cc3196aeSOleksandr Andrushchenko 
22cc3196aeSOleksandr Andrushchenko static void xen_snd_drv_fini(struct xen_snd_front_info *front_info)
23cc3196aeSOleksandr Andrushchenko {
24cc3196aeSOleksandr Andrushchenko }
25cc3196aeSOleksandr Andrushchenko 
26cc3196aeSOleksandr Andrushchenko static int sndback_initwait(struct xen_snd_front_info *front_info)
27cc3196aeSOleksandr Andrushchenko {
28*fd3b3604SOleksandr Andrushchenko 	int num_streams;
29*fd3b3604SOleksandr Andrushchenko 	int ret;
30*fd3b3604SOleksandr Andrushchenko 
31*fd3b3604SOleksandr Andrushchenko 	ret = xen_snd_front_cfg_card(front_info, &num_streams);
32*fd3b3604SOleksandr Andrushchenko 	if (ret < 0)
33*fd3b3604SOleksandr Andrushchenko 		return ret;
34*fd3b3604SOleksandr Andrushchenko 
35cc3196aeSOleksandr Andrushchenko 	return 0;
36cc3196aeSOleksandr Andrushchenko }
37cc3196aeSOleksandr Andrushchenko 
38cc3196aeSOleksandr Andrushchenko static int sndback_connect(struct xen_snd_front_info *front_info)
39cc3196aeSOleksandr Andrushchenko {
40cc3196aeSOleksandr Andrushchenko 	return 0;
41cc3196aeSOleksandr Andrushchenko }
42cc3196aeSOleksandr Andrushchenko 
43cc3196aeSOleksandr Andrushchenko static void sndback_disconnect(struct xen_snd_front_info *front_info)
44cc3196aeSOleksandr Andrushchenko {
45cc3196aeSOleksandr Andrushchenko 	xen_snd_drv_fini(front_info);
46cc3196aeSOleksandr Andrushchenko 	xenbus_switch_state(front_info->xb_dev, XenbusStateInitialising);
47cc3196aeSOleksandr Andrushchenko }
48cc3196aeSOleksandr Andrushchenko 
49cc3196aeSOleksandr Andrushchenko static void sndback_changed(struct xenbus_device *xb_dev,
50cc3196aeSOleksandr Andrushchenko 			    enum xenbus_state backend_state)
51cc3196aeSOleksandr Andrushchenko {
52cc3196aeSOleksandr Andrushchenko 	struct xen_snd_front_info *front_info = dev_get_drvdata(&xb_dev->dev);
53cc3196aeSOleksandr Andrushchenko 	int ret;
54cc3196aeSOleksandr Andrushchenko 
55cc3196aeSOleksandr Andrushchenko 	dev_dbg(&xb_dev->dev, "Backend state is %s, front is %s\n",
56cc3196aeSOleksandr Andrushchenko 		xenbus_strstate(backend_state),
57cc3196aeSOleksandr Andrushchenko 		xenbus_strstate(xb_dev->state));
58cc3196aeSOleksandr Andrushchenko 
59cc3196aeSOleksandr Andrushchenko 	switch (backend_state) {
60cc3196aeSOleksandr Andrushchenko 	case XenbusStateReconfiguring:
61cc3196aeSOleksandr Andrushchenko 		/* fall through */
62cc3196aeSOleksandr Andrushchenko 	case XenbusStateReconfigured:
63cc3196aeSOleksandr Andrushchenko 		/* fall through */
64cc3196aeSOleksandr Andrushchenko 	case XenbusStateInitialised:
65cc3196aeSOleksandr Andrushchenko 		/* fall through */
66cc3196aeSOleksandr Andrushchenko 		break;
67cc3196aeSOleksandr Andrushchenko 
68cc3196aeSOleksandr Andrushchenko 	case XenbusStateInitialising:
69cc3196aeSOleksandr Andrushchenko 		/* Recovering after backend unexpected closure. */
70cc3196aeSOleksandr Andrushchenko 		sndback_disconnect(front_info);
71cc3196aeSOleksandr Andrushchenko 		break;
72cc3196aeSOleksandr Andrushchenko 
73cc3196aeSOleksandr Andrushchenko 	case XenbusStateInitWait:
74cc3196aeSOleksandr Andrushchenko 		/* Recovering after backend unexpected closure. */
75cc3196aeSOleksandr Andrushchenko 		sndback_disconnect(front_info);
76cc3196aeSOleksandr Andrushchenko 
77cc3196aeSOleksandr Andrushchenko 		ret = sndback_initwait(front_info);
78cc3196aeSOleksandr Andrushchenko 		if (ret < 0)
79cc3196aeSOleksandr Andrushchenko 			xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
80cc3196aeSOleksandr Andrushchenko 		else
81cc3196aeSOleksandr Andrushchenko 			xenbus_switch_state(xb_dev, XenbusStateInitialised);
82cc3196aeSOleksandr Andrushchenko 		break;
83cc3196aeSOleksandr Andrushchenko 
84cc3196aeSOleksandr Andrushchenko 	case XenbusStateConnected:
85cc3196aeSOleksandr Andrushchenko 		if (xb_dev->state != XenbusStateInitialised)
86cc3196aeSOleksandr Andrushchenko 			break;
87cc3196aeSOleksandr Andrushchenko 
88cc3196aeSOleksandr Andrushchenko 		ret = sndback_connect(front_info);
89cc3196aeSOleksandr Andrushchenko 		if (ret < 0)
90cc3196aeSOleksandr Andrushchenko 			xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
91cc3196aeSOleksandr Andrushchenko 		else
92cc3196aeSOleksandr Andrushchenko 			xenbus_switch_state(xb_dev, XenbusStateConnected);
93cc3196aeSOleksandr Andrushchenko 		break;
94cc3196aeSOleksandr Andrushchenko 
95cc3196aeSOleksandr Andrushchenko 	case XenbusStateClosing:
96cc3196aeSOleksandr Andrushchenko 		/*
97cc3196aeSOleksandr Andrushchenko 		 * In this state backend starts freeing resources,
98cc3196aeSOleksandr Andrushchenko 		 * so let it go into closed state first, so we can also
99cc3196aeSOleksandr Andrushchenko 		 * remove ours.
100cc3196aeSOleksandr Andrushchenko 		 */
101cc3196aeSOleksandr Andrushchenko 		break;
102cc3196aeSOleksandr Andrushchenko 
103cc3196aeSOleksandr Andrushchenko 	case XenbusStateUnknown:
104cc3196aeSOleksandr Andrushchenko 		/* fall through */
105cc3196aeSOleksandr Andrushchenko 	case XenbusStateClosed:
106cc3196aeSOleksandr Andrushchenko 		if (xb_dev->state == XenbusStateClosed)
107cc3196aeSOleksandr Andrushchenko 			break;
108cc3196aeSOleksandr Andrushchenko 
109cc3196aeSOleksandr Andrushchenko 		sndback_disconnect(front_info);
110cc3196aeSOleksandr Andrushchenko 		break;
111cc3196aeSOleksandr Andrushchenko 	}
112cc3196aeSOleksandr Andrushchenko }
113cc3196aeSOleksandr Andrushchenko 
114cc3196aeSOleksandr Andrushchenko static int xen_drv_probe(struct xenbus_device *xb_dev,
115cc3196aeSOleksandr Andrushchenko 			 const struct xenbus_device_id *id)
116cc3196aeSOleksandr Andrushchenko {
117cc3196aeSOleksandr Andrushchenko 	struct xen_snd_front_info *front_info;
118cc3196aeSOleksandr Andrushchenko 
119cc3196aeSOleksandr Andrushchenko 	front_info = devm_kzalloc(&xb_dev->dev,
120cc3196aeSOleksandr Andrushchenko 				  sizeof(*front_info), GFP_KERNEL);
121cc3196aeSOleksandr Andrushchenko 	if (!front_info)
122cc3196aeSOleksandr Andrushchenko 		return -ENOMEM;
123cc3196aeSOleksandr Andrushchenko 
124cc3196aeSOleksandr Andrushchenko 	front_info->xb_dev = xb_dev;
125cc3196aeSOleksandr Andrushchenko 	dev_set_drvdata(&xb_dev->dev, front_info);
126cc3196aeSOleksandr Andrushchenko 
127cc3196aeSOleksandr Andrushchenko 	return xenbus_switch_state(xb_dev, XenbusStateInitialising);
128cc3196aeSOleksandr Andrushchenko }
129cc3196aeSOleksandr Andrushchenko 
130cc3196aeSOleksandr Andrushchenko static int xen_drv_remove(struct xenbus_device *dev)
131cc3196aeSOleksandr Andrushchenko {
132cc3196aeSOleksandr Andrushchenko 	struct xen_snd_front_info *front_info = dev_get_drvdata(&dev->dev);
133cc3196aeSOleksandr Andrushchenko 	int to = 100;
134cc3196aeSOleksandr Andrushchenko 
135cc3196aeSOleksandr Andrushchenko 	xenbus_switch_state(dev, XenbusStateClosing);
136cc3196aeSOleksandr Andrushchenko 
137cc3196aeSOleksandr Andrushchenko 	/*
138cc3196aeSOleksandr Andrushchenko 	 * On driver removal it is disconnected from XenBus,
139cc3196aeSOleksandr Andrushchenko 	 * so no backend state change events come via .otherend_changed
140cc3196aeSOleksandr Andrushchenko 	 * callback. This prevents us from exiting gracefully, e.g.
141cc3196aeSOleksandr Andrushchenko 	 * signaling the backend to free event channels, waiting for its
142cc3196aeSOleksandr Andrushchenko 	 * state to change to XenbusStateClosed and cleaning at our end.
143cc3196aeSOleksandr Andrushchenko 	 * Normally when front driver removed backend will finally go into
144cc3196aeSOleksandr Andrushchenko 	 * XenbusStateInitWait state.
145cc3196aeSOleksandr Andrushchenko 	 *
146cc3196aeSOleksandr Andrushchenko 	 * Workaround: read backend's state manually and wait with time-out.
147cc3196aeSOleksandr Andrushchenko 	 */
148cc3196aeSOleksandr Andrushchenko 	while ((xenbus_read_unsigned(front_info->xb_dev->otherend, "state",
149cc3196aeSOleksandr Andrushchenko 				     XenbusStateUnknown) != XenbusStateInitWait) &&
150cc3196aeSOleksandr Andrushchenko 				     to--)
151cc3196aeSOleksandr Andrushchenko 		msleep(10);
152cc3196aeSOleksandr Andrushchenko 
153cc3196aeSOleksandr Andrushchenko 	if (!to) {
154cc3196aeSOleksandr Andrushchenko 		unsigned int state;
155cc3196aeSOleksandr Andrushchenko 
156cc3196aeSOleksandr Andrushchenko 		state = xenbus_read_unsigned(front_info->xb_dev->otherend,
157cc3196aeSOleksandr Andrushchenko 					     "state", XenbusStateUnknown);
158cc3196aeSOleksandr Andrushchenko 		pr_err("Backend state is %s while removing driver\n",
159cc3196aeSOleksandr Andrushchenko 		       xenbus_strstate(state));
160cc3196aeSOleksandr Andrushchenko 	}
161cc3196aeSOleksandr Andrushchenko 
162cc3196aeSOleksandr Andrushchenko 	xen_snd_drv_fini(front_info);
163cc3196aeSOleksandr Andrushchenko 	xenbus_frontend_closed(dev);
164cc3196aeSOleksandr Andrushchenko 	return 0;
165cc3196aeSOleksandr Andrushchenko }
166cc3196aeSOleksandr Andrushchenko 
167cc3196aeSOleksandr Andrushchenko static const struct xenbus_device_id xen_drv_ids[] = {
168cc3196aeSOleksandr Andrushchenko 	{ XENSND_DRIVER_NAME },
169cc3196aeSOleksandr Andrushchenko 	{ "" }
170cc3196aeSOleksandr Andrushchenko };
171cc3196aeSOleksandr Andrushchenko 
172cc3196aeSOleksandr Andrushchenko static struct xenbus_driver xen_driver = {
173cc3196aeSOleksandr Andrushchenko 	.ids = xen_drv_ids,
174cc3196aeSOleksandr Andrushchenko 	.probe = xen_drv_probe,
175cc3196aeSOleksandr Andrushchenko 	.remove = xen_drv_remove,
176cc3196aeSOleksandr Andrushchenko 	.otherend_changed = sndback_changed,
177cc3196aeSOleksandr Andrushchenko };
178cc3196aeSOleksandr Andrushchenko 
179cc3196aeSOleksandr Andrushchenko static int __init xen_drv_init(void)
180cc3196aeSOleksandr Andrushchenko {
181cc3196aeSOleksandr Andrushchenko 	if (!xen_domain())
182cc3196aeSOleksandr Andrushchenko 		return -ENODEV;
183cc3196aeSOleksandr Andrushchenko 
184cc3196aeSOleksandr Andrushchenko 	if (!xen_has_pv_devices())
185cc3196aeSOleksandr Andrushchenko 		return -ENODEV;
186cc3196aeSOleksandr Andrushchenko 
187cc3196aeSOleksandr Andrushchenko 	pr_info("Initialising Xen " XENSND_DRIVER_NAME " frontend driver\n");
188cc3196aeSOleksandr Andrushchenko 	return xenbus_register_frontend(&xen_driver);
189cc3196aeSOleksandr Andrushchenko }
190cc3196aeSOleksandr Andrushchenko 
191cc3196aeSOleksandr Andrushchenko static void __exit xen_drv_fini(void)
192cc3196aeSOleksandr Andrushchenko {
193cc3196aeSOleksandr Andrushchenko 	pr_info("Unregistering Xen " XENSND_DRIVER_NAME " frontend driver\n");
194cc3196aeSOleksandr Andrushchenko 	xenbus_unregister_driver(&xen_driver);
195cc3196aeSOleksandr Andrushchenko }
196cc3196aeSOleksandr Andrushchenko 
197cc3196aeSOleksandr Andrushchenko module_init(xen_drv_init);
198cc3196aeSOleksandr Andrushchenko module_exit(xen_drv_fini);
199cc3196aeSOleksandr Andrushchenko 
200cc3196aeSOleksandr Andrushchenko MODULE_DESCRIPTION("Xen virtual sound device frontend");
201cc3196aeSOleksandr Andrushchenko MODULE_LICENSE("GPL");
202cc3196aeSOleksandr Andrushchenko MODULE_ALIAS("xen:" XENSND_DRIVER_NAME);
203cc3196aeSOleksandr Andrushchenko MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual soundcard}}");
204