xref: /linux/sound/xen/xen_snd_front.c (revision 58f9d806d16a38ed3bae11e2ada83393436a4956)
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 
19*58f9d806SOleksandr Andrushchenko #include <xen/xen-front-pgdir-shbuf.h>
20cc3196aeSOleksandr Andrushchenko #include <xen/interface/io/sndif.h>
21cc3196aeSOleksandr Andrushchenko 
22cc3196aeSOleksandr Andrushchenko #include "xen_snd_front.h"
231cee5593SOleksandr Andrushchenko #include "xen_snd_front_alsa.h"
24788ef64aSOleksandr Andrushchenko #include "xen_snd_front_evtchnl.h"
251cee5593SOleksandr Andrushchenko 
261cee5593SOleksandr Andrushchenko static struct xensnd_req *
271cee5593SOleksandr Andrushchenko be_stream_prepare_req(struct xen_snd_front_evtchnl *evtchnl, u8 operation)
281cee5593SOleksandr Andrushchenko {
291cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
301cee5593SOleksandr Andrushchenko 
311cee5593SOleksandr Andrushchenko 	req = RING_GET_REQUEST(&evtchnl->u.req.ring,
321cee5593SOleksandr Andrushchenko 			       evtchnl->u.req.ring.req_prod_pvt);
331cee5593SOleksandr Andrushchenko 	req->operation = operation;
341cee5593SOleksandr Andrushchenko 	req->id = evtchnl->evt_next_id++;
351cee5593SOleksandr Andrushchenko 	evtchnl->evt_id = req->id;
361cee5593SOleksandr Andrushchenko 	return req;
371cee5593SOleksandr Andrushchenko }
381cee5593SOleksandr Andrushchenko 
391cee5593SOleksandr Andrushchenko static int be_stream_do_io(struct xen_snd_front_evtchnl *evtchnl)
401cee5593SOleksandr Andrushchenko {
411cee5593SOleksandr Andrushchenko 	if (unlikely(evtchnl->state != EVTCHNL_STATE_CONNECTED))
421cee5593SOleksandr Andrushchenko 		return -EIO;
431cee5593SOleksandr Andrushchenko 
441cee5593SOleksandr Andrushchenko 	reinit_completion(&evtchnl->u.req.completion);
451cee5593SOleksandr Andrushchenko 	xen_snd_front_evtchnl_flush(evtchnl);
461cee5593SOleksandr Andrushchenko 	return 0;
471cee5593SOleksandr Andrushchenko }
481cee5593SOleksandr Andrushchenko 
491cee5593SOleksandr Andrushchenko static int be_stream_wait_io(struct xen_snd_front_evtchnl *evtchnl)
501cee5593SOleksandr Andrushchenko {
511cee5593SOleksandr Andrushchenko 	if (wait_for_completion_timeout(&evtchnl->u.req.completion,
521cee5593SOleksandr Andrushchenko 			msecs_to_jiffies(VSND_WAIT_BACK_MS)) <= 0)
531cee5593SOleksandr Andrushchenko 		return -ETIMEDOUT;
541cee5593SOleksandr Andrushchenko 
551cee5593SOleksandr Andrushchenko 	return evtchnl->u.req.resp_status;
561cee5593SOleksandr Andrushchenko }
571cee5593SOleksandr Andrushchenko 
581cee5593SOleksandr Andrushchenko int xen_snd_front_stream_query_hw_param(struct xen_snd_front_evtchnl *evtchnl,
591cee5593SOleksandr Andrushchenko 					struct xensnd_query_hw_param *hw_param_req,
601cee5593SOleksandr Andrushchenko 					struct xensnd_query_hw_param *hw_param_resp)
611cee5593SOleksandr Andrushchenko {
621cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
631cee5593SOleksandr Andrushchenko 	int ret;
641cee5593SOleksandr Andrushchenko 
651cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
661cee5593SOleksandr Andrushchenko 
671cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
681cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_HW_PARAM_QUERY);
691cee5593SOleksandr Andrushchenko 	req->op.hw_param = *hw_param_req;
701cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
711cee5593SOleksandr Andrushchenko 
721cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
731cee5593SOleksandr Andrushchenko 
741cee5593SOleksandr Andrushchenko 	if (ret == 0)
751cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
761cee5593SOleksandr Andrushchenko 
771cee5593SOleksandr Andrushchenko 	if (ret == 0)
781cee5593SOleksandr Andrushchenko 		*hw_param_resp = evtchnl->u.req.resp.hw_param;
791cee5593SOleksandr Andrushchenko 
801cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
811cee5593SOleksandr Andrushchenko 	return ret;
821cee5593SOleksandr Andrushchenko }
831cee5593SOleksandr Andrushchenko 
841cee5593SOleksandr Andrushchenko int xen_snd_front_stream_prepare(struct xen_snd_front_evtchnl *evtchnl,
85*58f9d806SOleksandr Andrushchenko 				 struct xen_front_pgdir_shbuf *shbuf,
861cee5593SOleksandr Andrushchenko 				 u8 format, unsigned int channels,
871cee5593SOleksandr Andrushchenko 				 unsigned int rate, u32 buffer_sz,
881cee5593SOleksandr Andrushchenko 				 u32 period_sz)
891cee5593SOleksandr Andrushchenko {
901cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
911cee5593SOleksandr Andrushchenko 	int ret;
921cee5593SOleksandr Andrushchenko 
931cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
941cee5593SOleksandr Andrushchenko 
951cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
961cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_OPEN);
971cee5593SOleksandr Andrushchenko 	req->op.open.pcm_format = format;
981cee5593SOleksandr Andrushchenko 	req->op.open.pcm_channels = channels;
991cee5593SOleksandr Andrushchenko 	req->op.open.pcm_rate = rate;
1001cee5593SOleksandr Andrushchenko 	req->op.open.buffer_sz = buffer_sz;
1011cee5593SOleksandr Andrushchenko 	req->op.open.period_sz = period_sz;
102*58f9d806SOleksandr Andrushchenko 	req->op.open.gref_directory =
103*58f9d806SOleksandr Andrushchenko 		xen_front_pgdir_shbuf_get_dir_start(shbuf);
1041cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
1051cee5593SOleksandr Andrushchenko 
1061cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
1071cee5593SOleksandr Andrushchenko 
1081cee5593SOleksandr Andrushchenko 	if (ret == 0)
1091cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
1101cee5593SOleksandr Andrushchenko 
1111cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
1121cee5593SOleksandr Andrushchenko 	return ret;
1131cee5593SOleksandr Andrushchenko }
1141cee5593SOleksandr Andrushchenko 
1151cee5593SOleksandr Andrushchenko int xen_snd_front_stream_close(struct xen_snd_front_evtchnl *evtchnl)
1161cee5593SOleksandr Andrushchenko {
1171cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
1181cee5593SOleksandr Andrushchenko 	int ret;
1191cee5593SOleksandr Andrushchenko 
1201cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
1211cee5593SOleksandr Andrushchenko 
1221cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
1231cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_CLOSE);
1241cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
1251cee5593SOleksandr Andrushchenko 
1261cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
1271cee5593SOleksandr Andrushchenko 
1281cee5593SOleksandr Andrushchenko 	if (ret == 0)
1291cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
1301cee5593SOleksandr Andrushchenko 
1311cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
1321cee5593SOleksandr Andrushchenko 	return ret;
1331cee5593SOleksandr Andrushchenko }
1341cee5593SOleksandr Andrushchenko 
1351cee5593SOleksandr Andrushchenko int xen_snd_front_stream_write(struct xen_snd_front_evtchnl *evtchnl,
1361cee5593SOleksandr Andrushchenko 			       unsigned long pos, unsigned long count)
1371cee5593SOleksandr Andrushchenko {
1381cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
1391cee5593SOleksandr Andrushchenko 	int ret;
1401cee5593SOleksandr Andrushchenko 
1411cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
1421cee5593SOleksandr Andrushchenko 
1431cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
1441cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_WRITE);
1451cee5593SOleksandr Andrushchenko 	req->op.rw.length = count;
1461cee5593SOleksandr Andrushchenko 	req->op.rw.offset = pos;
1471cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
1481cee5593SOleksandr Andrushchenko 
1491cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
1501cee5593SOleksandr Andrushchenko 
1511cee5593SOleksandr Andrushchenko 	if (ret == 0)
1521cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
1531cee5593SOleksandr Andrushchenko 
1541cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
1551cee5593SOleksandr Andrushchenko 	return ret;
1561cee5593SOleksandr Andrushchenko }
1571cee5593SOleksandr Andrushchenko 
1581cee5593SOleksandr Andrushchenko int xen_snd_front_stream_read(struct xen_snd_front_evtchnl *evtchnl,
1591cee5593SOleksandr Andrushchenko 			      unsigned long pos, unsigned long count)
1601cee5593SOleksandr Andrushchenko {
1611cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
1621cee5593SOleksandr Andrushchenko 	int ret;
1631cee5593SOleksandr Andrushchenko 
1641cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
1651cee5593SOleksandr Andrushchenko 
1661cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
1671cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_READ);
1681cee5593SOleksandr Andrushchenko 	req->op.rw.length = count;
1691cee5593SOleksandr Andrushchenko 	req->op.rw.offset = pos;
1701cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
1711cee5593SOleksandr Andrushchenko 
1721cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
1731cee5593SOleksandr Andrushchenko 
1741cee5593SOleksandr Andrushchenko 	if (ret == 0)
1751cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
1761cee5593SOleksandr Andrushchenko 
1771cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
1781cee5593SOleksandr Andrushchenko 	return ret;
1791cee5593SOleksandr Andrushchenko }
1801cee5593SOleksandr Andrushchenko 
1811cee5593SOleksandr Andrushchenko int xen_snd_front_stream_trigger(struct xen_snd_front_evtchnl *evtchnl,
1821cee5593SOleksandr Andrushchenko 				 int type)
1831cee5593SOleksandr Andrushchenko {
1841cee5593SOleksandr Andrushchenko 	struct xensnd_req *req;
1851cee5593SOleksandr Andrushchenko 	int ret;
1861cee5593SOleksandr Andrushchenko 
1871cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->u.req.req_io_lock);
1881cee5593SOleksandr Andrushchenko 
1891cee5593SOleksandr Andrushchenko 	mutex_lock(&evtchnl->ring_io_lock);
1901cee5593SOleksandr Andrushchenko 	req = be_stream_prepare_req(evtchnl, XENSND_OP_TRIGGER);
1911cee5593SOleksandr Andrushchenko 	req->op.trigger.type = type;
1921cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->ring_io_lock);
1931cee5593SOleksandr Andrushchenko 
1941cee5593SOleksandr Andrushchenko 	ret = be_stream_do_io(evtchnl);
1951cee5593SOleksandr Andrushchenko 
1961cee5593SOleksandr Andrushchenko 	if (ret == 0)
1971cee5593SOleksandr Andrushchenko 		ret = be_stream_wait_io(evtchnl);
1981cee5593SOleksandr Andrushchenko 
1991cee5593SOleksandr Andrushchenko 	mutex_unlock(&evtchnl->u.req.req_io_lock);
2001cee5593SOleksandr Andrushchenko 	return ret;
2011cee5593SOleksandr Andrushchenko }
202cc3196aeSOleksandr Andrushchenko 
203cc3196aeSOleksandr Andrushchenko static void xen_snd_drv_fini(struct xen_snd_front_info *front_info)
204cc3196aeSOleksandr Andrushchenko {
2051cee5593SOleksandr Andrushchenko 	xen_snd_front_alsa_fini(front_info);
206788ef64aSOleksandr Andrushchenko 	xen_snd_front_evtchnl_free_all(front_info);
207cc3196aeSOleksandr Andrushchenko }
208cc3196aeSOleksandr Andrushchenko 
209cc3196aeSOleksandr Andrushchenko static int sndback_initwait(struct xen_snd_front_info *front_info)
210cc3196aeSOleksandr Andrushchenko {
211fd3b3604SOleksandr Andrushchenko 	int num_streams;
212fd3b3604SOleksandr Andrushchenko 	int ret;
213fd3b3604SOleksandr Andrushchenko 
214fd3b3604SOleksandr Andrushchenko 	ret = xen_snd_front_cfg_card(front_info, &num_streams);
215fd3b3604SOleksandr Andrushchenko 	if (ret < 0)
216fd3b3604SOleksandr Andrushchenko 		return ret;
217fd3b3604SOleksandr Andrushchenko 
218788ef64aSOleksandr Andrushchenko 	/* create event channels for all streams and publish */
219788ef64aSOleksandr Andrushchenko 	ret = xen_snd_front_evtchnl_create_all(front_info, num_streams);
220788ef64aSOleksandr Andrushchenko 	if (ret < 0)
221788ef64aSOleksandr Andrushchenko 		return ret;
222788ef64aSOleksandr Andrushchenko 
223788ef64aSOleksandr Andrushchenko 	return xen_snd_front_evtchnl_publish_all(front_info);
224cc3196aeSOleksandr Andrushchenko }
225cc3196aeSOleksandr Andrushchenko 
226cc3196aeSOleksandr Andrushchenko static int sndback_connect(struct xen_snd_front_info *front_info)
227cc3196aeSOleksandr Andrushchenko {
2281cee5593SOleksandr Andrushchenko 	return xen_snd_front_alsa_init(front_info);
229cc3196aeSOleksandr Andrushchenko }
230cc3196aeSOleksandr Andrushchenko 
231cc3196aeSOleksandr Andrushchenko static void sndback_disconnect(struct xen_snd_front_info *front_info)
232cc3196aeSOleksandr Andrushchenko {
233cc3196aeSOleksandr Andrushchenko 	xen_snd_drv_fini(front_info);
234cc3196aeSOleksandr Andrushchenko 	xenbus_switch_state(front_info->xb_dev, XenbusStateInitialising);
235cc3196aeSOleksandr Andrushchenko }
236cc3196aeSOleksandr Andrushchenko 
237cc3196aeSOleksandr Andrushchenko static void sndback_changed(struct xenbus_device *xb_dev,
238cc3196aeSOleksandr Andrushchenko 			    enum xenbus_state backend_state)
239cc3196aeSOleksandr Andrushchenko {
240cc3196aeSOleksandr Andrushchenko 	struct xen_snd_front_info *front_info = dev_get_drvdata(&xb_dev->dev);
241cc3196aeSOleksandr Andrushchenko 	int ret;
242cc3196aeSOleksandr Andrushchenko 
243cc3196aeSOleksandr Andrushchenko 	dev_dbg(&xb_dev->dev, "Backend state is %s, front is %s\n",
244cc3196aeSOleksandr Andrushchenko 		xenbus_strstate(backend_state),
245cc3196aeSOleksandr Andrushchenko 		xenbus_strstate(xb_dev->state));
246cc3196aeSOleksandr Andrushchenko 
247cc3196aeSOleksandr Andrushchenko 	switch (backend_state) {
248cc3196aeSOleksandr Andrushchenko 	case XenbusStateReconfiguring:
249cc3196aeSOleksandr Andrushchenko 		/* fall through */
250cc3196aeSOleksandr Andrushchenko 	case XenbusStateReconfigured:
251cc3196aeSOleksandr Andrushchenko 		/* fall through */
252cc3196aeSOleksandr Andrushchenko 	case XenbusStateInitialised:
253cc3196aeSOleksandr Andrushchenko 		/* fall through */
254cc3196aeSOleksandr Andrushchenko 		break;
255cc3196aeSOleksandr Andrushchenko 
256cc3196aeSOleksandr Andrushchenko 	case XenbusStateInitialising:
257cc3196aeSOleksandr Andrushchenko 		/* Recovering after backend unexpected closure. */
258cc3196aeSOleksandr Andrushchenko 		sndback_disconnect(front_info);
259cc3196aeSOleksandr Andrushchenko 		break;
260cc3196aeSOleksandr Andrushchenko 
261cc3196aeSOleksandr Andrushchenko 	case XenbusStateInitWait:
262cc3196aeSOleksandr Andrushchenko 		/* Recovering after backend unexpected closure. */
263cc3196aeSOleksandr Andrushchenko 		sndback_disconnect(front_info);
264cc3196aeSOleksandr Andrushchenko 
265cc3196aeSOleksandr Andrushchenko 		ret = sndback_initwait(front_info);
266cc3196aeSOleksandr Andrushchenko 		if (ret < 0)
267cc3196aeSOleksandr Andrushchenko 			xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
268cc3196aeSOleksandr Andrushchenko 		else
269cc3196aeSOleksandr Andrushchenko 			xenbus_switch_state(xb_dev, XenbusStateInitialised);
270cc3196aeSOleksandr Andrushchenko 		break;
271cc3196aeSOleksandr Andrushchenko 
272cc3196aeSOleksandr Andrushchenko 	case XenbusStateConnected:
273cc3196aeSOleksandr Andrushchenko 		if (xb_dev->state != XenbusStateInitialised)
274cc3196aeSOleksandr Andrushchenko 			break;
275cc3196aeSOleksandr Andrushchenko 
276cc3196aeSOleksandr Andrushchenko 		ret = sndback_connect(front_info);
277cc3196aeSOleksandr Andrushchenko 		if (ret < 0)
278cc3196aeSOleksandr Andrushchenko 			xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
279cc3196aeSOleksandr Andrushchenko 		else
280cc3196aeSOleksandr Andrushchenko 			xenbus_switch_state(xb_dev, XenbusStateConnected);
281cc3196aeSOleksandr Andrushchenko 		break;
282cc3196aeSOleksandr Andrushchenko 
283cc3196aeSOleksandr Andrushchenko 	case XenbusStateClosing:
284cc3196aeSOleksandr Andrushchenko 		/*
285cc3196aeSOleksandr Andrushchenko 		 * In this state backend starts freeing resources,
286cc3196aeSOleksandr Andrushchenko 		 * so let it go into closed state first, so we can also
287cc3196aeSOleksandr Andrushchenko 		 * remove ours.
288cc3196aeSOleksandr Andrushchenko 		 */
289cc3196aeSOleksandr Andrushchenko 		break;
290cc3196aeSOleksandr Andrushchenko 
291cc3196aeSOleksandr Andrushchenko 	case XenbusStateUnknown:
292cc3196aeSOleksandr Andrushchenko 		/* fall through */
293cc3196aeSOleksandr Andrushchenko 	case XenbusStateClosed:
294cc3196aeSOleksandr Andrushchenko 		if (xb_dev->state == XenbusStateClosed)
295cc3196aeSOleksandr Andrushchenko 			break;
296cc3196aeSOleksandr Andrushchenko 
297cc3196aeSOleksandr Andrushchenko 		sndback_disconnect(front_info);
298cc3196aeSOleksandr Andrushchenko 		break;
299cc3196aeSOleksandr Andrushchenko 	}
300cc3196aeSOleksandr Andrushchenko }
301cc3196aeSOleksandr Andrushchenko 
302cc3196aeSOleksandr Andrushchenko static int xen_drv_probe(struct xenbus_device *xb_dev,
303cc3196aeSOleksandr Andrushchenko 			 const struct xenbus_device_id *id)
304cc3196aeSOleksandr Andrushchenko {
305cc3196aeSOleksandr Andrushchenko 	struct xen_snd_front_info *front_info;
306cc3196aeSOleksandr Andrushchenko 
307cc3196aeSOleksandr Andrushchenko 	front_info = devm_kzalloc(&xb_dev->dev,
308cc3196aeSOleksandr Andrushchenko 				  sizeof(*front_info), GFP_KERNEL);
309cc3196aeSOleksandr Andrushchenko 	if (!front_info)
310cc3196aeSOleksandr Andrushchenko 		return -ENOMEM;
311cc3196aeSOleksandr Andrushchenko 
312cc3196aeSOleksandr Andrushchenko 	front_info->xb_dev = xb_dev;
313cc3196aeSOleksandr Andrushchenko 	dev_set_drvdata(&xb_dev->dev, front_info);
314cc3196aeSOleksandr Andrushchenko 
315cc3196aeSOleksandr Andrushchenko 	return xenbus_switch_state(xb_dev, XenbusStateInitialising);
316cc3196aeSOleksandr Andrushchenko }
317cc3196aeSOleksandr Andrushchenko 
318cc3196aeSOleksandr Andrushchenko static int xen_drv_remove(struct xenbus_device *dev)
319cc3196aeSOleksandr Andrushchenko {
320cc3196aeSOleksandr Andrushchenko 	struct xen_snd_front_info *front_info = dev_get_drvdata(&dev->dev);
321cc3196aeSOleksandr Andrushchenko 	int to = 100;
322cc3196aeSOleksandr Andrushchenko 
323cc3196aeSOleksandr Andrushchenko 	xenbus_switch_state(dev, XenbusStateClosing);
324cc3196aeSOleksandr Andrushchenko 
325cc3196aeSOleksandr Andrushchenko 	/*
326cc3196aeSOleksandr Andrushchenko 	 * On driver removal it is disconnected from XenBus,
327cc3196aeSOleksandr Andrushchenko 	 * so no backend state change events come via .otherend_changed
328cc3196aeSOleksandr Andrushchenko 	 * callback. This prevents us from exiting gracefully, e.g.
329cc3196aeSOleksandr Andrushchenko 	 * signaling the backend to free event channels, waiting for its
330cc3196aeSOleksandr Andrushchenko 	 * state to change to XenbusStateClosed and cleaning at our end.
331cc3196aeSOleksandr Andrushchenko 	 * Normally when front driver removed backend will finally go into
332cc3196aeSOleksandr Andrushchenko 	 * XenbusStateInitWait state.
333cc3196aeSOleksandr Andrushchenko 	 *
334cc3196aeSOleksandr Andrushchenko 	 * Workaround: read backend's state manually and wait with time-out.
335cc3196aeSOleksandr Andrushchenko 	 */
336cc3196aeSOleksandr Andrushchenko 	while ((xenbus_read_unsigned(front_info->xb_dev->otherend, "state",
337cc3196aeSOleksandr Andrushchenko 				     XenbusStateUnknown) != XenbusStateInitWait) &&
3380d5bcfc9SDan Carpenter 	       --to)
339cc3196aeSOleksandr Andrushchenko 		msleep(10);
340cc3196aeSOleksandr Andrushchenko 
341cc3196aeSOleksandr Andrushchenko 	if (!to) {
342cc3196aeSOleksandr Andrushchenko 		unsigned int state;
343cc3196aeSOleksandr Andrushchenko 
344cc3196aeSOleksandr Andrushchenko 		state = xenbus_read_unsigned(front_info->xb_dev->otherend,
345cc3196aeSOleksandr Andrushchenko 					     "state", XenbusStateUnknown);
346cc3196aeSOleksandr Andrushchenko 		pr_err("Backend state is %s while removing driver\n",
347cc3196aeSOleksandr Andrushchenko 		       xenbus_strstate(state));
348cc3196aeSOleksandr Andrushchenko 	}
349cc3196aeSOleksandr Andrushchenko 
350cc3196aeSOleksandr Andrushchenko 	xen_snd_drv_fini(front_info);
351cc3196aeSOleksandr Andrushchenko 	xenbus_frontend_closed(dev);
352cc3196aeSOleksandr Andrushchenko 	return 0;
353cc3196aeSOleksandr Andrushchenko }
354cc3196aeSOleksandr Andrushchenko 
355cc3196aeSOleksandr Andrushchenko static const struct xenbus_device_id xen_drv_ids[] = {
356cc3196aeSOleksandr Andrushchenko 	{ XENSND_DRIVER_NAME },
357cc3196aeSOleksandr Andrushchenko 	{ "" }
358cc3196aeSOleksandr Andrushchenko };
359cc3196aeSOleksandr Andrushchenko 
360cc3196aeSOleksandr Andrushchenko static struct xenbus_driver xen_driver = {
361cc3196aeSOleksandr Andrushchenko 	.ids = xen_drv_ids,
362cc3196aeSOleksandr Andrushchenko 	.probe = xen_drv_probe,
363cc3196aeSOleksandr Andrushchenko 	.remove = xen_drv_remove,
364cc3196aeSOleksandr Andrushchenko 	.otherend_changed = sndback_changed,
365cc3196aeSOleksandr Andrushchenko };
366cc3196aeSOleksandr Andrushchenko 
367cc3196aeSOleksandr Andrushchenko static int __init xen_drv_init(void)
368cc3196aeSOleksandr Andrushchenko {
369cc3196aeSOleksandr Andrushchenko 	if (!xen_domain())
370cc3196aeSOleksandr Andrushchenko 		return -ENODEV;
371cc3196aeSOleksandr Andrushchenko 
372cc3196aeSOleksandr Andrushchenko 	if (!xen_has_pv_devices())
373cc3196aeSOleksandr Andrushchenko 		return -ENODEV;
374cc3196aeSOleksandr Andrushchenko 
375d6e0fbb8SOleksandr Andrushchenko 	/* At the moment we only support case with XEN_PAGE_SIZE == PAGE_SIZE */
376d6e0fbb8SOleksandr Andrushchenko 	if (XEN_PAGE_SIZE != PAGE_SIZE) {
377d6e0fbb8SOleksandr Andrushchenko 		pr_err(XENSND_DRIVER_NAME ": different kernel and Xen page sizes are not supported: XEN_PAGE_SIZE (%lu) != PAGE_SIZE (%lu)\n",
378d6e0fbb8SOleksandr Andrushchenko 		       XEN_PAGE_SIZE, PAGE_SIZE);
379d6e0fbb8SOleksandr Andrushchenko 		return -ENODEV;
380d6e0fbb8SOleksandr Andrushchenko 	}
381d6e0fbb8SOleksandr Andrushchenko 
382cc3196aeSOleksandr Andrushchenko 	pr_info("Initialising Xen " XENSND_DRIVER_NAME " frontend driver\n");
383cc3196aeSOleksandr Andrushchenko 	return xenbus_register_frontend(&xen_driver);
384cc3196aeSOleksandr Andrushchenko }
385cc3196aeSOleksandr Andrushchenko 
386cc3196aeSOleksandr Andrushchenko static void __exit xen_drv_fini(void)
387cc3196aeSOleksandr Andrushchenko {
388cc3196aeSOleksandr Andrushchenko 	pr_info("Unregistering Xen " XENSND_DRIVER_NAME " frontend driver\n");
389cc3196aeSOleksandr Andrushchenko 	xenbus_unregister_driver(&xen_driver);
390cc3196aeSOleksandr Andrushchenko }
391cc3196aeSOleksandr Andrushchenko 
392cc3196aeSOleksandr Andrushchenko module_init(xen_drv_init);
393cc3196aeSOleksandr Andrushchenko module_exit(xen_drv_fini);
394cc3196aeSOleksandr Andrushchenko 
395cc3196aeSOleksandr Andrushchenko MODULE_DESCRIPTION("Xen virtual sound device frontend");
396cc3196aeSOleksandr Andrushchenko MODULE_LICENSE("GPL");
397cc3196aeSOleksandr Andrushchenko MODULE_ALIAS("xen:" XENSND_DRIVER_NAME);
398cc3196aeSOleksandr Andrushchenko MODULE_SUPPORTED_DEVICE("{{ALSA,Virtual soundcard}}");
399