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
1958f9d806SOleksandr 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 *
be_stream_prepare_req(struct xen_snd_front_evtchnl * evtchnl,u8 operation)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
be_stream_do_io(struct xen_snd_front_evtchnl * evtchnl)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
be_stream_wait_io(struct xen_snd_front_evtchnl * evtchnl)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
xen_snd_front_stream_query_hw_param(struct xen_snd_front_evtchnl * evtchnl,struct xensnd_query_hw_param * hw_param_req,struct xensnd_query_hw_param * hw_param_resp)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
xen_snd_front_stream_prepare(struct xen_snd_front_evtchnl * evtchnl,struct xen_front_pgdir_shbuf * shbuf,u8 format,unsigned int channels,unsigned int rate,u32 buffer_sz,u32 period_sz)841cee5593SOleksandr Andrushchenko int xen_snd_front_stream_prepare(struct xen_snd_front_evtchnl *evtchnl,
8558f9d806SOleksandr 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;
10258f9d806SOleksandr Andrushchenko req->op.open.gref_directory =
10358f9d806SOleksandr 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
xen_snd_front_stream_close(struct xen_snd_front_evtchnl * evtchnl)1151cee5593SOleksandr Andrushchenko int xen_snd_front_stream_close(struct xen_snd_front_evtchnl *evtchnl)
1161cee5593SOleksandr Andrushchenko {
11704b3c795SPierre-Louis Bossart __always_unused 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
xen_snd_front_stream_write(struct xen_snd_front_evtchnl * evtchnl,unsigned long pos,unsigned long count)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
xen_snd_front_stream_read(struct xen_snd_front_evtchnl * evtchnl,unsigned long pos,unsigned long count)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
xen_snd_front_stream_trigger(struct xen_snd_front_evtchnl * evtchnl,int type)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
xen_snd_drv_fini(struct xen_snd_front_info * front_info)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
sndback_initwait(struct xen_snd_front_info * front_info)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
sndback_connect(struct xen_snd_front_info * front_info)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
sndback_disconnect(struct xen_snd_front_info * front_info)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
sndback_changed(struct xenbus_device * xb_dev,enum xenbus_state backend_state)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 case XenbusStateReconfigured:
250cc3196aeSOleksandr Andrushchenko case XenbusStateInitialised:
251cc3196aeSOleksandr Andrushchenko break;
252cc3196aeSOleksandr Andrushchenko
253cc3196aeSOleksandr Andrushchenko case XenbusStateInitialising:
254cc3196aeSOleksandr Andrushchenko /* Recovering after backend unexpected closure. */
255cc3196aeSOleksandr Andrushchenko sndback_disconnect(front_info);
256cc3196aeSOleksandr Andrushchenko break;
257cc3196aeSOleksandr Andrushchenko
258cc3196aeSOleksandr Andrushchenko case XenbusStateInitWait:
259cc3196aeSOleksandr Andrushchenko /* Recovering after backend unexpected closure. */
260cc3196aeSOleksandr Andrushchenko sndback_disconnect(front_info);
261cc3196aeSOleksandr Andrushchenko
262cc3196aeSOleksandr Andrushchenko ret = sndback_initwait(front_info);
263cc3196aeSOleksandr Andrushchenko if (ret < 0)
264cc3196aeSOleksandr Andrushchenko xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
265cc3196aeSOleksandr Andrushchenko else
266cc3196aeSOleksandr Andrushchenko xenbus_switch_state(xb_dev, XenbusStateInitialised);
267cc3196aeSOleksandr Andrushchenko break;
268cc3196aeSOleksandr Andrushchenko
269cc3196aeSOleksandr Andrushchenko case XenbusStateConnected:
270cc3196aeSOleksandr Andrushchenko if (xb_dev->state != XenbusStateInitialised)
271cc3196aeSOleksandr Andrushchenko break;
272cc3196aeSOleksandr Andrushchenko
273cc3196aeSOleksandr Andrushchenko ret = sndback_connect(front_info);
274cc3196aeSOleksandr Andrushchenko if (ret < 0)
275cc3196aeSOleksandr Andrushchenko xenbus_dev_fatal(xb_dev, ret, "initializing frontend");
276cc3196aeSOleksandr Andrushchenko else
277cc3196aeSOleksandr Andrushchenko xenbus_switch_state(xb_dev, XenbusStateConnected);
278cc3196aeSOleksandr Andrushchenko break;
279cc3196aeSOleksandr Andrushchenko
280cc3196aeSOleksandr Andrushchenko case XenbusStateClosing:
281cc3196aeSOleksandr Andrushchenko /*
282cc3196aeSOleksandr Andrushchenko * In this state backend starts freeing resources,
283cc3196aeSOleksandr Andrushchenko * so let it go into closed state first, so we can also
284cc3196aeSOleksandr Andrushchenko * remove ours.
285cc3196aeSOleksandr Andrushchenko */
286cc3196aeSOleksandr Andrushchenko break;
287cc3196aeSOleksandr Andrushchenko
288cc3196aeSOleksandr Andrushchenko case XenbusStateUnknown:
289cc3196aeSOleksandr Andrushchenko case XenbusStateClosed:
290cc3196aeSOleksandr Andrushchenko if (xb_dev->state == XenbusStateClosed)
291cc3196aeSOleksandr Andrushchenko break;
292cc3196aeSOleksandr Andrushchenko
293cc3196aeSOleksandr Andrushchenko sndback_disconnect(front_info);
294cc3196aeSOleksandr Andrushchenko break;
295cc3196aeSOleksandr Andrushchenko }
296cc3196aeSOleksandr Andrushchenko }
297cc3196aeSOleksandr Andrushchenko
xen_drv_probe(struct xenbus_device * xb_dev,const struct xenbus_device_id * id)298cc3196aeSOleksandr Andrushchenko static int xen_drv_probe(struct xenbus_device *xb_dev,
299cc3196aeSOleksandr Andrushchenko const struct xenbus_device_id *id)
300cc3196aeSOleksandr Andrushchenko {
301cc3196aeSOleksandr Andrushchenko struct xen_snd_front_info *front_info;
302cc3196aeSOleksandr Andrushchenko
303cc3196aeSOleksandr Andrushchenko front_info = devm_kzalloc(&xb_dev->dev,
304cc3196aeSOleksandr Andrushchenko sizeof(*front_info), GFP_KERNEL);
305cc3196aeSOleksandr Andrushchenko if (!front_info)
306cc3196aeSOleksandr Andrushchenko return -ENOMEM;
307cc3196aeSOleksandr Andrushchenko
308cc3196aeSOleksandr Andrushchenko front_info->xb_dev = xb_dev;
309cc3196aeSOleksandr Andrushchenko dev_set_drvdata(&xb_dev->dev, front_info);
310cc3196aeSOleksandr Andrushchenko
311cc3196aeSOleksandr Andrushchenko return xenbus_switch_state(xb_dev, XenbusStateInitialising);
312cc3196aeSOleksandr Andrushchenko }
313cc3196aeSOleksandr Andrushchenko
xen_drv_remove(struct xenbus_device * dev)314*7cffcadeSDawei Li static void xen_drv_remove(struct xenbus_device *dev)
315cc3196aeSOleksandr Andrushchenko {
316cc3196aeSOleksandr Andrushchenko struct xen_snd_front_info *front_info = dev_get_drvdata(&dev->dev);
317cc3196aeSOleksandr Andrushchenko int to = 100;
318cc3196aeSOleksandr Andrushchenko
319cc3196aeSOleksandr Andrushchenko xenbus_switch_state(dev, XenbusStateClosing);
320cc3196aeSOleksandr Andrushchenko
321cc3196aeSOleksandr Andrushchenko /*
322cc3196aeSOleksandr Andrushchenko * On driver removal it is disconnected from XenBus,
323cc3196aeSOleksandr Andrushchenko * so no backend state change events come via .otherend_changed
324cc3196aeSOleksandr Andrushchenko * callback. This prevents us from exiting gracefully, e.g.
325cc3196aeSOleksandr Andrushchenko * signaling the backend to free event channels, waiting for its
326cc3196aeSOleksandr Andrushchenko * state to change to XenbusStateClosed and cleaning at our end.
327cc3196aeSOleksandr Andrushchenko * Normally when front driver removed backend will finally go into
328cc3196aeSOleksandr Andrushchenko * XenbusStateInitWait state.
329cc3196aeSOleksandr Andrushchenko *
330cc3196aeSOleksandr Andrushchenko * Workaround: read backend's state manually and wait with time-out.
331cc3196aeSOleksandr Andrushchenko */
332cc3196aeSOleksandr Andrushchenko while ((xenbus_read_unsigned(front_info->xb_dev->otherend, "state",
333cc3196aeSOleksandr Andrushchenko XenbusStateUnknown) != XenbusStateInitWait) &&
3340d5bcfc9SDan Carpenter --to)
335cc3196aeSOleksandr Andrushchenko msleep(10);
336cc3196aeSOleksandr Andrushchenko
337cc3196aeSOleksandr Andrushchenko if (!to) {
338cc3196aeSOleksandr Andrushchenko unsigned int state;
339cc3196aeSOleksandr Andrushchenko
340cc3196aeSOleksandr Andrushchenko state = xenbus_read_unsigned(front_info->xb_dev->otherend,
341cc3196aeSOleksandr Andrushchenko "state", XenbusStateUnknown);
342cc3196aeSOleksandr Andrushchenko pr_err("Backend state is %s while removing driver\n",
343cc3196aeSOleksandr Andrushchenko xenbus_strstate(state));
344cc3196aeSOleksandr Andrushchenko }
345cc3196aeSOleksandr Andrushchenko
346cc3196aeSOleksandr Andrushchenko xen_snd_drv_fini(front_info);
347cc3196aeSOleksandr Andrushchenko xenbus_frontend_closed(dev);
348cc3196aeSOleksandr Andrushchenko }
349cc3196aeSOleksandr Andrushchenko
350cc3196aeSOleksandr Andrushchenko static const struct xenbus_device_id xen_drv_ids[] = {
351cc3196aeSOleksandr Andrushchenko { XENSND_DRIVER_NAME },
352cc3196aeSOleksandr Andrushchenko { "" }
353cc3196aeSOleksandr Andrushchenko };
354cc3196aeSOleksandr Andrushchenko
355cc3196aeSOleksandr Andrushchenko static struct xenbus_driver xen_driver = {
356cc3196aeSOleksandr Andrushchenko .ids = xen_drv_ids,
357cc3196aeSOleksandr Andrushchenko .probe = xen_drv_probe,
358cc3196aeSOleksandr Andrushchenko .remove = xen_drv_remove,
359cc3196aeSOleksandr Andrushchenko .otherend_changed = sndback_changed,
360de6da33eSJuergen Gross .not_essential = true,
361cc3196aeSOleksandr Andrushchenko };
362cc3196aeSOleksandr Andrushchenko
xen_drv_init(void)363cc3196aeSOleksandr Andrushchenko static int __init xen_drv_init(void)
364cc3196aeSOleksandr Andrushchenko {
365cc3196aeSOleksandr Andrushchenko if (!xen_domain())
366cc3196aeSOleksandr Andrushchenko return -ENODEV;
367cc3196aeSOleksandr Andrushchenko
368cc3196aeSOleksandr Andrushchenko if (!xen_has_pv_devices())
369cc3196aeSOleksandr Andrushchenko return -ENODEV;
370cc3196aeSOleksandr Andrushchenko
371d6e0fbb8SOleksandr Andrushchenko /* At the moment we only support case with XEN_PAGE_SIZE == PAGE_SIZE */
372d6e0fbb8SOleksandr Andrushchenko if (XEN_PAGE_SIZE != PAGE_SIZE) {
373d6e0fbb8SOleksandr Andrushchenko pr_err(XENSND_DRIVER_NAME ": different kernel and Xen page sizes are not supported: XEN_PAGE_SIZE (%lu) != PAGE_SIZE (%lu)\n",
374d6e0fbb8SOleksandr Andrushchenko XEN_PAGE_SIZE, PAGE_SIZE);
375d6e0fbb8SOleksandr Andrushchenko return -ENODEV;
376d6e0fbb8SOleksandr Andrushchenko }
377d6e0fbb8SOleksandr Andrushchenko
378cc3196aeSOleksandr Andrushchenko pr_info("Initialising Xen " XENSND_DRIVER_NAME " frontend driver\n");
379cc3196aeSOleksandr Andrushchenko return xenbus_register_frontend(&xen_driver);
380cc3196aeSOleksandr Andrushchenko }
381cc3196aeSOleksandr Andrushchenko
xen_drv_fini(void)382cc3196aeSOleksandr Andrushchenko static void __exit xen_drv_fini(void)
383cc3196aeSOleksandr Andrushchenko {
384cc3196aeSOleksandr Andrushchenko pr_info("Unregistering Xen " XENSND_DRIVER_NAME " frontend driver\n");
385cc3196aeSOleksandr Andrushchenko xenbus_unregister_driver(&xen_driver);
386cc3196aeSOleksandr Andrushchenko }
387cc3196aeSOleksandr Andrushchenko
388cc3196aeSOleksandr Andrushchenko module_init(xen_drv_init);
389cc3196aeSOleksandr Andrushchenko module_exit(xen_drv_fini);
390cc3196aeSOleksandr Andrushchenko
391cc3196aeSOleksandr Andrushchenko MODULE_DESCRIPTION("Xen virtual sound device frontend");
392cc3196aeSOleksandr Andrushchenko MODULE_LICENSE("GPL");
393cc3196aeSOleksandr Andrushchenko MODULE_ALIAS("xen:" XENSND_DRIVER_NAME);
394