blob: 277214d4fd0a55a0d1393e7700769974f6c72399 [file] [log] [blame]
Oleksandr Andrushchenkocc3196ae12018-05-14 09:27:37 +03001// 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"
Oleksandr Andrushchenko788ef642018-05-14 09:27:39 +030021#include "xen_snd_front_evtchnl.h"
Oleksandr Andrushchenkocc3196ae12018-05-14 09:27:37 +030022
23static void xen_snd_drv_fini(struct xen_snd_front_info *front_info)
24{
Oleksandr Andrushchenko788ef642018-05-14 09:27:39 +030025 xen_snd_front_evtchnl_free_all(front_info);
Oleksandr Andrushchenkocc3196ae12018-05-14 09:27:37 +030026}
27
28static int sndback_initwait(struct xen_snd_front_info *front_info)
29{
Oleksandr Andrushchenkofd3b3602018-05-14 09:27:38 +030030 int num_streams;
31 int ret;
32
33 ret = xen_snd_front_cfg_card(front_info, &num_streams);
34 if (ret < 0)
35 return ret;
36
Oleksandr Andrushchenko788ef642018-05-14 09:27:39 +030037 /* create event channels for all streams and publish */
38 ret = xen_snd_front_evtchnl_create_all(front_info, num_streams);
39 if (ret < 0)
40 return ret;
41
42 return xen_snd_front_evtchnl_publish_all(front_info);
Oleksandr Andrushchenkocc3196ae12018-05-14 09:27:37 +030043}
44
45static int sndback_connect(struct xen_snd_front_info *front_info)
46{
47 return 0;
48}
49
50static void sndback_disconnect(struct xen_snd_front_info *front_info)
51{
52 xen_snd_drv_fini(front_info);
53 xenbus_switch_state(front_info->xb_dev, XenbusStateInitialising);
54}
55
56static void sndback_changed(struct xenbus_device *xb_dev,
57 enum xenbus_state backend_state)
58{
59 struct xen_snd_front_info *front_info = dev_get_drvdata(&xb_dev->dev);
60 int ret;
61
62 dev_dbg(&xb_dev->dev, "Backend state is %s, front is %s\n",
63 xenbus_strstate(backend_state),
64 xenbus_strstate(xb_dev->state));
65
66 switch (backend_state) {
67 case XenbusStateReconfiguring:
68 /* fall through */
69 case XenbusStateReconfigured:
70 /* fall through */
71 case XenbusStateInitialised:
72 /* fall through */
73 break;
74
75 case XenbusStateInitialising:
76 /* Recovering after backend unexpected closure. */
77 sndback_disconnect(front_info);
78 break;
79
80 case XenbusStateInitWait:
81 /* Recovering after backend unexpected closure. */
82 sndback_disconnect(front_info);
83
84 ret = sndback_initwait(front_info);
85 if (ret < 0)
86 xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
87 else
88 xenbus_switch_state(xb_dev, XenbusStateInitialised);
89 break;
90
91 case XenbusStateConnected:
92 if (xb_dev->state != XenbusStateInitialised)
93 break;
94
95 ret = sndback_connect(front_info);
96 if (ret < 0)
97 xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
98 else
99 xenbus_switch_state(xb_dev, XenbusStateConnected);
100 break;
101
102 case XenbusStateClosing:
103 /*
104 * In this state backend starts freeing resources,
105 * so let it go into closed state first, so we can also
106 * remove ours.
107 */
108 break;
109
110 case XenbusStateUnknown:
111 /* fall through */
112 case XenbusStateClosed:
113 if (xb_dev->state == XenbusStateClosed)
114 break;
115
116 sndback_disconnect(front_info);
117 break;
118 }
119}
120
121static int xen_drv_probe(struct xenbus_device *xb_dev,
122 const struct xenbus_device_id *id)
123{
124 struct xen_snd_front_info *front_info;
125
126 front_info = devm_kzalloc(&xb_dev->dev,
127 sizeof(*front_info), GFP_KERNEL);
128 if (!front_info)
129 return -ENOMEM;
130
131 front_info->xb_dev = xb_dev;
132 dev_set_drvdata(&xb_dev->dev, front_info);
133
134 return xenbus_switch_state(xb_dev, XenbusStateInitialising);
135}
136
137static int xen_drv_remove(struct xenbus_device *dev)
138{
139 struct xen_snd_front_info *front_info = dev_get_drvdata(&dev->dev);
140 int to = 100;
141
142 xenbus_switch_state(dev, XenbusStateClosing);
143
144 /*
145 * On driver removal it is disconnected from XenBus,
146 * so no backend state change events come via .otherend_changed
147 * callback. This prevents us from exiting gracefully, e.g.
148 * signaling the backend to free event channels, waiting for its
149 * state to change to XenbusStateClosed and cleaning at our end.
150 * Normally when front driver removed backend will finally go into
151 * XenbusStateInitWait state.
152 *
153 * Workaround: read backend's state manually and wait with time-out.
154 */
155 while ((xenbus_read_unsigned(front_info->xb_dev->otherend, "state",
156 XenbusStateUnknown) != XenbusStateInitWait) &&
157 to--)
158 msleep(10);
159
160 if (!to) {
161 unsigned int state;
162
163 state = xenbus_read_unsigned(front_info->xb_dev->otherend,
164 "state", XenbusStateUnknown);
165 pr_err("Backend state is %s while removing driver\n",
166 xenbus_strstate(state));
167 }
168
169 xen_snd_drv_fini(front_info);
170 xenbus_frontend_closed(dev);
171 return 0;
172}
173
174static const struct xenbus_device_id xen_drv_ids[] = {
175 { XENSND_DRIVER_NAME },
176 { "" }
177};
178
179static struct xenbus_driver xen_driver = {
180 .ids = xen_drv_ids,
181 .probe = xen_drv_probe,
182 .remove = xen_drv_remove,
183 .otherend_changed = sndback_changed,
184};
185
186static int __init xen_drv_init(void)
187{
188 if (!xen_domain())
189 return -ENODEV;
190
191 if (!xen_has_pv_devices())
192 return -ENODEV;
193
194 pr_info("Initialising Xen " XENSND_DRIVER_NAME " frontend driver\n");
195 return xenbus_register_frontend(&xen_driver);
196}
197
198static void __exit xen_drv_fini(void)
199{
200 pr_info("Unregistering Xen " XENSND_DRIVER_NAME " frontend driver\n");
201 xenbus_unregister_driver(&xen_driver);
202}
203
204module_init(xen_drv_init);
205module_exit(xen_drv_fini);
206
207MODULE_DESCRIPTION("Xen virtual sound device frontend");
208MODULE_LICENSE("GPL");
209MODULE_ALIAS("xen:" XENSND_DRIVER_NAME);
210MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual soundcard}}");