xref: /linux/sound/xen/xen_snd_front.c (revision 1cee559351a7cb57b405554bac10a6f33c28ed09)
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 
14d6e0fbb8SOleksandr Andrushchenko #include <xen/page.h>
15cc3196aeSOleksandr Andrushchenko #include <xen/platform_pci.h>
16cc3196aeSOleksandr Andrushchenko #include <xen/xen.h>
17cc3196aeSOleksandr Andrushchenko #include <xen/xenbus.h>
18cc3196aeSOleksandr Andrushchenko 
19cc3196aeSOleksandr Andrushchenko #include <xen/interface/io/sndif.h>
20cc3196aeSOleksandr Andrushchenko 
21cc3196aeSOleksandr Andrushchenko #include "xen_snd_front.h"
22*1cee5593SOleksandr Andrushchenko #include "xen_snd_front_alsa.h"
23788ef64aSOleksandr Andrushchenko #include "xen_snd_front_evtchnl.h"
24*1cee5593SOleksandr Andrushchenko #include "xen_snd_front_shbuf.h"
25*1cee5593SOleksandr Andrushchenko 
26*1cee5593SOleksandr Andrushchenko static struct xensnd_req *
27*1cee5593SOleksandr Andrushchenko be_stream_prepare_req(struct xen_snd_front_evtchnl *evtchnl, u8 operation)
28*1cee5593SOleksandr Andrushchenko {
29*1cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
30*1cee5593SOleksandr Andrushchenko 
31*1cee5593SOleksandr Andrushchenko 	req = RING_GET_REQUEST(&evtchnl->u.req.ring,
32*1cee5593SOleksandr Andrushchenko 			       evtchnl->u.req.ring.req_prod_pvt);
33*1cee5593SOleksandr Andrushchenko 	req->operation = operation;
34*1cee5593SOleksandr Andrushchenko 	req->id = evtchnl->evt_next_id++;
35*1cee5593SOleksandr Andrushchenko 	evtchnl->evt_id = req->id;
36*1cee5593SOleksandr Andrushchenko 	return req;
37*1cee5593SOleksandr Andrushchenko }
38*1cee5593SOleksandr Andrushchenko 
39*1cee5593SOleksandr Andrushchenko static int be_stream_do_io(struct xen_snd_front_evtchnl *evtchnl)
40*1cee5593SOleksandr Andrushchenko {
41*1cee5593SOleksandr Andrushchenko 	if (unlikely(evtchnl->state != EVTCHNL_STATE_CONNECTED))
42*1cee5593SOleksandr Andrushchenko 		return -EIO;
43*1cee5593SOleksandr Andrushchenko 
44*1cee5593SOleksandr Andrushchenko 	reinit_completion(&evtchnl->u.req.completion);
45*1cee5593SOleksandr Andrushchenko 	xen_snd_front_evtchnl_flush(evtchnl);
46*1cee5593SOleksandr Andrushchenko 	return 0;
47*1cee5593SOleksandr Andrushchenko }
48*1cee5593SOleksandr Andrushchenko 
49*1cee5593SOleksandr Andrushchenko static int be_stream_wait_io(struct xen_snd_front_evtchnl *evtchnl)
50*1cee5593SOleksandr Andrushchenko {
51*1cee5593SOleksandr Andrushchenko 	if (wait_for_completion_timeout(&evtchnl->u.req.completion,
52*1cee5593SOleksandr Andrushchenko 			msecs_to_jiffies(VSND_WAIT_BACK_MS)) <= 0)
53*1cee5593SOleksandr Andrushchenko 		return -ETIMEDOUT;
54*1cee5593SOleksandr Andrushchenko 
55*1cee5593SOleksandr Andrushchenko 	return evtchnl->u.req.resp_status;
56*1cee5593SOleksandr Andrushchenko }
57*1cee5593SOleksandr Andrushchenko 
58*1cee5593SOleksandr Andrushchenko int xen_snd_front_stream_query_hw_param(struct xen_snd_front_evtchnl *evtchnl,
59*1cee5593SOleksandr Andrushchenko 					struct xensnd_query_hw_param *hw_param_req,
60*1cee5593SOleksandr Andrushchenko 					struct xensnd_query_hw_param *hw_param_resp)
61*1cee5593SOleksandr Andrushchenko {
62*1cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
63*1cee5593SOleksandr Andrushchenko 	int ret;
64*1cee5593SOleksandr Andrushchenko 
65*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
66*1cee5593SOleksandr Andrushchenko 
67*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
68*1cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_HW_PARAM_QUERY);
69*1cee5593SOleksandr Andrushchenko 	req->op.hw_param = *hw_param_req;
70*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
71*1cee5593SOleksandr Andrushchenko 
72*1cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
73*1cee5593SOleksandr Andrushchenko 
74*1cee5593SOleksandr Andrushchenko 	if (ret == 0)
75*1cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
76*1cee5593SOleksandr Andrushchenko 
77*1cee5593SOleksandr Andrushchenko 	if (ret == 0)
78*1cee5593SOleksandr Andrushchenko 		*hw_param_resp = evtchnl->u.req.resp.hw_param;
79*1cee5593SOleksandr Andrushchenko 
80*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
81*1cee5593SOleksandr Andrushchenko 	return ret;
82*1cee5593SOleksandr Andrushchenko }
83*1cee5593SOleksandr Andrushchenko 
84*1cee5593SOleksandr Andrushchenko int xen_snd_front_stream_prepare(struct xen_snd_front_evtchnl *evtchnl,
85*1cee5593SOleksandr Andrushchenko 				 struct xen_snd_front_shbuf *sh_buf,
86*1cee5593SOleksandr Andrushchenko 				 u8 format, unsigned int channels,
87*1cee5593SOleksandr Andrushchenko 				 unsigned int rate, u32 buffer_sz,
88*1cee5593SOleksandr Andrushchenko 				 u32 period_sz)
89*1cee5593SOleksandr Andrushchenko {
90*1cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
91*1cee5593SOleksandr Andrushchenko 	int ret;
92*1cee5593SOleksandr Andrushchenko 
93*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
94*1cee5593SOleksandr Andrushchenko 
95*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
96*1cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_OPEN);
97*1cee5593SOleksandr Andrushchenko 	req->op.open.pcm_format = format;
98*1cee5593SOleksandr Andrushchenko 	req->op.open.pcm_channels = channels;
99*1cee5593SOleksandr Andrushchenko 	req->op.open.pcm_rate = rate;
100*1cee5593SOleksandr Andrushchenko 	req->op.open.buffer_sz = buffer_sz;
101*1cee5593SOleksandr Andrushchenko 	req->op.open.period_sz = period_sz;
102*1cee5593SOleksandr Andrushchenko 	req->op.open.gref_directory = xen_snd_front_shbuf_get_dir_start(sh_buf);
103*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
104*1cee5593SOleksandr Andrushchenko 
105*1cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
106*1cee5593SOleksandr Andrushchenko 
107*1cee5593SOleksandr Andrushchenko 	if (ret == 0)
108*1cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
109*1cee5593SOleksandr Andrushchenko 
110*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
111*1cee5593SOleksandr Andrushchenko 	return ret;
112*1cee5593SOleksandr Andrushchenko }
113*1cee5593SOleksandr Andrushchenko 
114*1cee5593SOleksandr Andrushchenko int xen_snd_front_stream_close(struct xen_snd_front_evtchnl *evtchnl)
115*1cee5593SOleksandr Andrushchenko {
116*1cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
117*1cee5593SOleksandr Andrushchenko 	int ret;
118*1cee5593SOleksandr Andrushchenko 
119*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
120*1cee5593SOleksandr Andrushchenko 
121*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
122*1cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_CLOSE);
123*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
124*1cee5593SOleksandr Andrushchenko 
125*1cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
126*1cee5593SOleksandr Andrushchenko 
127*1cee5593SOleksandr Andrushchenko 	if (ret == 0)
128*1cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
129*1cee5593SOleksandr Andrushchenko 
130*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
131*1cee5593SOleksandr Andrushchenko 	return ret;
132*1cee5593SOleksandr Andrushchenko }
133*1cee5593SOleksandr Andrushchenko 
134*1cee5593SOleksandr Andrushchenko int xen_snd_front_stream_write(struct xen_snd_front_evtchnl *evtchnl,
135*1cee5593SOleksandr Andrushchenko 			       unsigned long pos, unsigned long count)
136*1cee5593SOleksandr Andrushchenko {
137*1cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
138*1cee5593SOleksandr Andrushchenko 	int ret;
139*1cee5593SOleksandr Andrushchenko 
140*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
141*1cee5593SOleksandr Andrushchenko 
142*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
143*1cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_WRITE);
144*1cee5593SOleksandr Andrushchenko 	req->op.rw.length = count;
145*1cee5593SOleksandr Andrushchenko 	req->op.rw.offset = pos;
146*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
147*1cee5593SOleksandr Andrushchenko 
148*1cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
149*1cee5593SOleksandr Andrushchenko 
150*1cee5593SOleksandr Andrushchenko 	if (ret == 0)
151*1cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
152*1cee5593SOleksandr Andrushchenko 
153*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
154*1cee5593SOleksandr Andrushchenko 	return ret;
155*1cee5593SOleksandr Andrushchenko }
156*1cee5593SOleksandr Andrushchenko 
157*1cee5593SOleksandr Andrushchenko int xen_snd_front_stream_read(struct xen_snd_front_evtchnl *evtchnl,
158*1cee5593SOleksandr Andrushchenko 			      unsigned long pos, unsigned long count)
159*1cee5593SOleksandr Andrushchenko {
160*1cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
161*1cee5593SOleksandr Andrushchenko 	int ret;
162*1cee5593SOleksandr Andrushchenko 
163*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
164*1cee5593SOleksandr Andrushchenko 
165*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
166*1cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_READ);
167*1cee5593SOleksandr Andrushchenko 	req->op.rw.length = count;
168*1cee5593SOleksandr Andrushchenko 	req->op.rw.offset = pos;
169*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
170*1cee5593SOleksandr Andrushchenko 
171*1cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
172*1cee5593SOleksandr Andrushchenko 
173*1cee5593SOleksandr Andrushchenko 	if (ret == 0)
174*1cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
175*1cee5593SOleksandr Andrushchenko 
176*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
177*1cee5593SOleksandr Andrushchenko 	return ret;
178*1cee5593SOleksandr Andrushchenko }
179*1cee5593SOleksandr Andrushchenko 
180*1cee5593SOleksandr Andrushchenko int xen_snd_front_stream_trigger(struct xen_snd_front_evtchnl *evtchnl,
181*1cee5593SOleksandr Andrushchenko 				 int type)
182*1cee5593SOleksandr Andrushchenko {
183*1cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
184*1cee5593SOleksandr Andrushchenko 	int ret;
185*1cee5593SOleksandr Andrushchenko 
186*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
187*1cee5593SOleksandr Andrushchenko 
188*1cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
189*1cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_TRIGGER);
190*1cee5593SOleksandr Andrushchenko 	req->op.trigger.type = type;
191*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
192*1cee5593SOleksandr Andrushchenko 
193*1cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
194*1cee5593SOleksandr Andrushchenko 
195*1cee5593SOleksandr Andrushchenko 	if (ret == 0)
196*1cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
197*1cee5593SOleksandr Andrushchenko 
198*1cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
199*1cee5593SOleksandr Andrushchenko 	return ret;
200*1cee5593SOleksandr Andrushchenko }
201cc3196aeSOleksandr Andrushchenko 
202cc3196aeSOleksandr Andrushchenko static void xen_snd_drv_fini(struct xen_snd_front_info *front_info)
203cc3196aeSOleksandr Andrushchenko {
204*1cee5593SOleksandr Andrushchenko 	xen_snd_front_alsa_fini(front_info);
205788ef64aSOleksandr Andrushchenko 	xen_snd_front_evtchnl_free_all(front_info);
206cc3196aeSOleksandr Andrushchenko }
207cc3196aeSOleksandr Andrushchenko 
208cc3196aeSOleksandr Andrushchenko static int sndback_initwait(struct xen_snd_front_info *front_info)
209cc3196aeSOleksandr Andrushchenko {
210fd3b3604SOleksandr Andrushchenko 	int num_streams;
211fd3b3604SOleksandr Andrushchenko 	int ret;
212fd3b3604SOleksandr Andrushchenko 
213fd3b3604SOleksandr Andrushchenko 	ret = xen_snd_front_cfg_card(front_info, &num_streams);
214fd3b3604SOleksandr Andrushchenko 	if (ret < 0)
215fd3b3604SOleksandr Andrushchenko 		return ret;
216fd3b3604SOleksandr Andrushchenko 
217788ef64aSOleksandr Andrushchenko 	/* create event channels for all streams and publish */
218788ef64aSOleksandr Andrushchenko 	ret = xen_snd_front_evtchnl_create_all(front_info, num_streams);
219788ef64aSOleksandr Andrushchenko 	if (ret < 0)
220788ef64aSOleksandr Andrushchenko 		return ret;
221788ef64aSOleksandr Andrushchenko 
222788ef64aSOleksandr Andrushchenko 	return xen_snd_front_evtchnl_publish_all(front_info);
223cc3196aeSOleksandr Andrushchenko }
224cc3196aeSOleksandr Andrushchenko 
225cc3196aeSOleksandr Andrushchenko static int sndback_connect(struct xen_snd_front_info *front_info)
226cc3196aeSOleksandr Andrushchenko {
227*1cee5593SOleksandr Andrushchenko 	return xen_snd_front_alsa_init(front_info);
228cc3196aeSOleksandr Andrushchenko }
229cc3196aeSOleksandr Andrushchenko 
230cc3196aeSOleksandr Andrushchenko static void sndback_disconnect(struct xen_snd_front_info *front_info)
231cc3196aeSOleksandr Andrushchenko {
232cc3196aeSOleksandr Andrushchenko 	xen_snd_drv_fini(front_info);
233cc3196aeSOleksandr Andrushchenko 	xenbus_switch_state(front_info->xb_dev, XenbusStateInitialising);
234cc3196aeSOleksandr Andrushchenko }
235cc3196aeSOleksandr Andrushchenko 
236cc3196aeSOleksandr Andrushchenko static void sndback_changed(struct xenbus_device *xb_dev,
237cc3196aeSOleksandr Andrushchenko 			    enum xenbus_state backend_state)
238cc3196aeSOleksandr Andrushchenko {
239cc3196aeSOleksandr Andrushchenko 	struct xen_snd_front_info *front_info = dev_get_drvdata(&xb_dev->dev);
240cc3196aeSOleksandr Andrushchenko 	int ret;
241cc3196aeSOleksandr Andrushchenko 
242cc3196aeSOleksandr Andrushchenko 	dev_dbg(&xb_dev->dev, "Backend state is %s, front is %s\n",
243cc3196aeSOleksandr Andrushchenko 		xenbus_strstate(backend_state),
244cc3196aeSOleksandr Andrushchenko 		xenbus_strstate(xb_dev->state));
245cc3196aeSOleksandr Andrushchenko 
246cc3196aeSOleksandr Andrushchenko 	switch (backend_state) {
247cc3196aeSOleksandr Andrushchenko 	case XenbusStateReconfiguring:
248cc3196aeSOleksandr Andrushchenko 		/* fall through */
249cc3196aeSOleksandr Andrushchenko 	case XenbusStateReconfigured:
250cc3196aeSOleksandr Andrushchenko 		/* fall through */
251cc3196aeSOleksandr Andrushchenko 	case XenbusStateInitialised:
252cc3196aeSOleksandr Andrushchenko 		/* fall through */
253cc3196aeSOleksandr Andrushchenko 		break;
254cc3196aeSOleksandr Andrushchenko 
255cc3196aeSOleksandr Andrushchenko 	case XenbusStateInitialising:
256cc3196aeSOleksandr Andrushchenko 		/* Recovering after backend unexpected closure. */
257cc3196aeSOleksandr Andrushchenko 		sndback_disconnect(front_info);
258cc3196aeSOleksandr Andrushchenko 		break;
259cc3196aeSOleksandr Andrushchenko 
260cc3196aeSOleksandr Andrushchenko 	case XenbusStateInitWait:
261cc3196aeSOleksandr Andrushchenko 		/* Recovering after backend unexpected closure. */
262cc3196aeSOleksandr Andrushchenko 		sndback_disconnect(front_info);
263cc3196aeSOleksandr Andrushchenko 
264cc3196aeSOleksandr Andrushchenko 		ret = sndback_initwait(front_info);
265cc3196aeSOleksandr Andrushchenko 		if (ret < 0)
266cc3196aeSOleksandr Andrushchenko 			xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
267cc3196aeSOleksandr Andrushchenko 		else
268cc3196aeSOleksandr Andrushchenko 			xenbus_switch_state(xb_dev, XenbusStateInitialised);
269cc3196aeSOleksandr Andrushchenko 		break;
270cc3196aeSOleksandr Andrushchenko 
271cc3196aeSOleksandr Andrushchenko 	case XenbusStateConnected:
272cc3196aeSOleksandr Andrushchenko 		if (xb_dev->state != XenbusStateInitialised)
273cc3196aeSOleksandr Andrushchenko 			break;
274cc3196aeSOleksandr Andrushchenko 
275cc3196aeSOleksandr Andrushchenko 		ret = sndback_connect(front_info);
276cc3196aeSOleksandr Andrushchenko 		if (ret < 0)
277cc3196aeSOleksandr Andrushchenko 			xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
278cc3196aeSOleksandr Andrushchenko 		else
279cc3196aeSOleksandr Andrushchenko 			xenbus_switch_state(xb_dev, XenbusStateConnected);
280cc3196aeSOleksandr Andrushchenko 		break;
281cc3196aeSOleksandr Andrushchenko 
282cc3196aeSOleksandr Andrushchenko 	case XenbusStateClosing:
283cc3196aeSOleksandr Andrushchenko 		/*
284cc3196aeSOleksandr Andrushchenko 		 * In this state backend starts freeing resources,
285cc3196aeSOleksandr Andrushchenko 		 * so let it go into closed state first, so we can also
286cc3196aeSOleksandr Andrushchenko 		 * remove ours.
287cc3196aeSOleksandr Andrushchenko 		 */
288cc3196aeSOleksandr Andrushchenko 		break;
289cc3196aeSOleksandr Andrushchenko 
290cc3196aeSOleksandr Andrushchenko 	case XenbusStateUnknown:
291cc3196aeSOleksandr Andrushchenko 		/* fall through */
292cc3196aeSOleksandr Andrushchenko 	case XenbusStateClosed:
293cc3196aeSOleksandr Andrushchenko 		if (xb_dev->state == XenbusStateClosed)
294cc3196aeSOleksandr Andrushchenko 			break;
295cc3196aeSOleksandr Andrushchenko 
296cc3196aeSOleksandr Andrushchenko 		sndback_disconnect(front_info);
297cc3196aeSOleksandr Andrushchenko 		break;
298cc3196aeSOleksandr Andrushchenko 	}
299cc3196aeSOleksandr Andrushchenko }
300cc3196aeSOleksandr Andrushchenko 
301cc3196aeSOleksandr Andrushchenko static int xen_drv_probe(struct xenbus_device *xb_dev,
302cc3196aeSOleksandr Andrushchenko 			 const struct xenbus_device_id *id)
303cc3196aeSOleksandr Andrushchenko {
304cc3196aeSOleksandr Andrushchenko 	struct xen_snd_front_info *front_info;
305cc3196aeSOleksandr Andrushchenko 
306cc3196aeSOleksandr Andrushchenko 	front_info = devm_kzalloc(&xb_dev->dev,
307cc3196aeSOleksandr Andrushchenko 				  sizeof(*front_info), GFP_KERNEL);
308cc3196aeSOleksandr Andrushchenko 	if (!front_info)
309cc3196aeSOleksandr Andrushchenko 		return -ENOMEM;
310cc3196aeSOleksandr Andrushchenko 
311cc3196aeSOleksandr Andrushchenko 	front_info->xb_dev = xb_dev;
312cc3196aeSOleksandr Andrushchenko 	dev_set_drvdata(&xb_dev->dev, front_info);
313cc3196aeSOleksandr Andrushchenko 
314cc3196aeSOleksandr Andrushchenko 	return xenbus_switch_state(xb_dev, XenbusStateInitialising);
315cc3196aeSOleksandr Andrushchenko }
316cc3196aeSOleksandr Andrushchenko 
317cc3196aeSOleksandr Andrushchenko static int xen_drv_remove(struct xenbus_device *dev)
318cc3196aeSOleksandr Andrushchenko {
319cc3196aeSOleksandr Andrushchenko 	struct xen_snd_front_info *front_info = dev_get_drvdata(&dev->dev);
320cc3196aeSOleksandr Andrushchenko 	int to = 100;
321cc3196aeSOleksandr Andrushchenko 
322cc3196aeSOleksandr Andrushchenko 	xenbus_switch_state(dev, XenbusStateClosing);
323cc3196aeSOleksandr Andrushchenko 
324cc3196aeSOleksandr Andrushchenko 	/*
325cc3196aeSOleksandr Andrushchenko 	 * On driver removal it is disconnected from XenBus,
326cc3196aeSOleksandr Andrushchenko 	 * so no backend state change events come via .otherend_changed
327cc3196aeSOleksandr Andrushchenko 	 * callback. This prevents us from exiting gracefully, e.g.
328cc3196aeSOleksandr Andrushchenko 	 * signaling the backend to free event channels, waiting for its
329cc3196aeSOleksandr Andrushchenko 	 * state to change to XenbusStateClosed and cleaning at our end.
330cc3196aeSOleksandr Andrushchenko 	 * Normally when front driver removed backend will finally go into
331cc3196aeSOleksandr Andrushchenko 	 * XenbusStateInitWait state.
332cc3196aeSOleksandr Andrushchenko 	 *
333cc3196aeSOleksandr Andrushchenko 	 * Workaround: read backend's state manually and wait with time-out.
334cc3196aeSOleksandr Andrushchenko 	 */
335cc3196aeSOleksandr Andrushchenko 	while ((xenbus_read_unsigned(front_info->xb_dev->otherend, "state",
336cc3196aeSOleksandr Andrushchenko 				     XenbusStateUnknown) != XenbusStateInitWait) &&
337cc3196aeSOleksandr Andrushchenko 				     to--)
338cc3196aeSOleksandr Andrushchenko 		msleep(10);
339cc3196aeSOleksandr Andrushchenko 
340cc3196aeSOleksandr Andrushchenko 	if (!to) {
341cc3196aeSOleksandr Andrushchenko 		unsigned int state;
342cc3196aeSOleksandr Andrushchenko 
343cc3196aeSOleksandr Andrushchenko 		state = xenbus_read_unsigned(front_info->xb_dev->otherend,
344cc3196aeSOleksandr Andrushchenko 					     "state", XenbusStateUnknown);
345cc3196aeSOleksandr Andrushchenko 		pr_err("Backend state is %s while removing driver\n",
346cc3196aeSOleksandr Andrushchenko 		       xenbus_strstate(state));
347cc3196aeSOleksandr Andrushchenko 	}
348cc3196aeSOleksandr Andrushchenko 
349cc3196aeSOleksandr Andrushchenko 	xen_snd_drv_fini(front_info);
350cc3196aeSOleksandr Andrushchenko 	xenbus_frontend_closed(dev);
351cc3196aeSOleksandr Andrushchenko 	return 0;
352cc3196aeSOleksandr Andrushchenko }
353cc3196aeSOleksandr Andrushchenko 
354cc3196aeSOleksandr Andrushchenko static const struct xenbus_device_id xen_drv_ids[] = {
355cc3196aeSOleksandr Andrushchenko 	{ XENSND_DRIVER_NAME },
356cc3196aeSOleksandr Andrushchenko 	{ "" }
357cc3196aeSOleksandr Andrushchenko };
358cc3196aeSOleksandr Andrushchenko 
359cc3196aeSOleksandr Andrushchenko static struct xenbus_driver xen_driver = {
360cc3196aeSOleksandr Andrushchenko 	.ids = xen_drv_ids,
361cc3196aeSOleksandr Andrushchenko 	.probe = xen_drv_probe,
362cc3196aeSOleksandr Andrushchenko 	.remove = xen_drv_remove,
363cc3196aeSOleksandr Andrushchenko 	.otherend_changed = sndback_changed,
364cc3196aeSOleksandr Andrushchenko };
365cc3196aeSOleksandr Andrushchenko 
366cc3196aeSOleksandr Andrushchenko static int __init xen_drv_init(void)
367cc3196aeSOleksandr Andrushchenko {
368cc3196aeSOleksandr Andrushchenko 	if (!xen_domain())
369cc3196aeSOleksandr Andrushchenko 		return -ENODEV;
370cc3196aeSOleksandr Andrushchenko 
371cc3196aeSOleksandr Andrushchenko 	if (!xen_has_pv_devices())
372cc3196aeSOleksandr Andrushchenko 		return -ENODEV;
373cc3196aeSOleksandr Andrushchenko 
374d6e0fbb8SOleksandr Andrushchenko 	/* At the moment we only support case with XEN_PAGE_SIZE == PAGE_SIZE */
375d6e0fbb8SOleksandr Andrushchenko 	if (XEN_PAGE_SIZE != PAGE_SIZE) {
376d6e0fbb8SOleksandr Andrushchenko 		pr_err(XENSND_DRIVER_NAME ": different kernel and Xen page sizes are not supported: XEN_PAGE_SIZE (%lu) != PAGE_SIZE (%lu)\n",
377d6e0fbb8SOleksandr Andrushchenko 		       XEN_PAGE_SIZE, PAGE_SIZE);
378d6e0fbb8SOleksandr Andrushchenko 		return -ENODEV;
379d6e0fbb8SOleksandr Andrushchenko 	}
380d6e0fbb8SOleksandr Andrushchenko 
381cc3196aeSOleksandr Andrushchenko 	pr_info("Initialising Xen " XENSND_DRIVER_NAME " frontend driver\n");
382cc3196aeSOleksandr Andrushchenko 	return xenbus_register_frontend(&xen_driver);
383cc3196aeSOleksandr Andrushchenko }
384cc3196aeSOleksandr Andrushchenko 
385cc3196aeSOleksandr Andrushchenko static void __exit xen_drv_fini(void)
386cc3196aeSOleksandr Andrushchenko {
387cc3196aeSOleksandr Andrushchenko 	pr_info("Unregistering Xen " XENSND_DRIVER_NAME " frontend driver\n");
388cc3196aeSOleksandr Andrushchenko 	xenbus_unregister_driver(&xen_driver);
389cc3196aeSOleksandr Andrushchenko }
390cc3196aeSOleksandr Andrushchenko 
391cc3196aeSOleksandr Andrushchenko module_init(xen_drv_init);
392cc3196aeSOleksandr Andrushchenko module_exit(xen_drv_fini);
393cc3196aeSOleksandr Andrushchenko 
394cc3196aeSOleksandr Andrushchenko MODULE_DESCRIPTION("Xen virtual sound device frontend");
395cc3196aeSOleksandr Andrushchenko MODULE_LICENSE("GPL");
396cc3196aeSOleksandr Andrushchenko MODULE_ALIAS("xen:" XENSND_DRIVER_NAME);
397cc3196aeSOleksandr Andrushchenko MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual soundcard}}");
398