summaryrefslogtreecommitdiffstats
path: root/sound
diff options
context:
space:
mode:
authorRobert Rosengren2018-03-26 07:24:49 +0200
committerTakashi Iwai2018-03-27 08:00:28 +0200
commit306a4f3ca7f3c7dfa473ebd19d66e40e59d99734 (patch)
treec275ecc8324f5221d215670d49b8d950a15a4f94 /sound
parentALSA: usb-audio: update clock valid control (diff)
downloadkernel-qcow2-linux-306a4f3ca7f3c7dfa473ebd19d66e40e59d99734.tar.gz
kernel-qcow2-linux-306a4f3ca7f3c7dfa473ebd19d66e40e59d99734.tar.xz
kernel-qcow2-linux-306a4f3ca7f3c7dfa473ebd19d66e40e59d99734.zip
ALSA: aloop: Mark paused device as inactive
Show paused ALSA aloop device as inactive, i.e. the control "PCM Slave Active" set as false. Notification sent upon state change. This makes it possible for client capturing from aloop device to know if data is expected. Without it the client expects data even if playback is paused. Signed-off-by: Robert Rosengren <robert.rosengren@axis.com> Signed-off-by: Takashi Iwai <tiwai@suse.de>
Diffstat (limited to 'sound')
-rw-r--r--sound/drivers/aloop.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/sound/drivers/aloop.c b/sound/drivers/aloop.c
index 1063a4377502..58e349fc893f 100644
--- a/sound/drivers/aloop.c
+++ b/sound/drivers/aloop.c
@@ -296,6 +296,8 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
cable->pause |= stream;
loopback_timer_stop(dpcm);
spin_unlock(&cable->lock);
+ if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+ loopback_active_notify(dpcm);
break;
case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
case SNDRV_PCM_TRIGGER_RESUME:
@@ -304,6 +306,8 @@ static int loopback_trigger(struct snd_pcm_substream *substream, int cmd)
cable->pause &= ~stream;
loopback_timer_start(dpcm);
spin_unlock(&cable->lock);
+ if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
+ loopback_active_notify(dpcm);
break;
default:
return -EINVAL;
@@ -892,9 +896,11 @@ static int loopback_active_get(struct snd_kcontrol *kcontrol,
[kcontrol->id.subdevice][kcontrol->id.device ^ 1];
unsigned int val = 0;
- if (cable != NULL)
- val = (cable->running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ?
- 1 : 0;
+ if (cable != NULL) {
+ unsigned int running = cable->running ^ cable->pause;
+
+ val = (running & (1 << SNDRV_PCM_STREAM_PLAYBACK)) ? 1 : 0;
+ }
ucontrol->value.integer.value[0] = val;
return 0;
}
e>
90addb0218d4 ^
1ba00980f6b9 ^
12e364b9f08a ^
55c67dcaac78 ^


5f3a7e364608 ^
55c67dcaac78 ^
12e364b9f08a ^
12e364b9f08a ^
12e364b9f08a ^


2c7e1d4e4b1d ^
2ee0deec49bd ^


d5b3f1dccee4 ^






b615d628b672 ^


b615d628b672 ^
b615d628b672 ^
e3420ed66295 ^


e4feb2f25795 ^
e3420ed66295 ^












ec17f452f4e9 ^




12e364b9f08a ^
52063eca7fd0 ^
2ee0d05243a2 ^

12e364b9f08a ^
4616881022a1 ^








9232d2d62a9d ^
12e364b9f08a ^
e3420ed66295 ^

12e364b9f08a ^
c3d9a224e787 ^
12e364b9f08a ^
84982fbf0f0d ^
c1f834eb104d ^
3103dc0304fd ^
ec17f452f4e9 ^

2ee0d05243a2 ^

b3c55b13a1e4 ^
c1f834eb104d ^


c071b6f541b6 ^
12e364b9f08a ^
ec17f452f4e9 ^


12e364b9f08a ^


7166ed19a08c ^
c79b28f7332e ^
12e364b9f08a ^
ec17f452f4e9 ^
ec17f452f4e9 ^
12e364b9f08a ^




317d9614a980 ^
12e364b9f08a ^
12e364b9f08a ^



ec17f452f4e9 ^


12e364b9f08a ^





98d7b5947761 ^
12e364b9f08a ^



ec17f452f4e9 ^

12e364b9f08a ^



2ee0d05243a2 ^

12e364b9f08a ^








ec17f452f4e9 ^






12e364b9f08a ^


12e364b9f08a ^



3ab477012935 ^
12e364b9f08a ^

ddf5de536949 ^

12e364b9f08a ^
12e364b9f08a ^
ec17f452f4e9 ^
12e364b9f08a ^
19f6634f41ff ^

84efd2077c1e ^










19f6634f41ff ^
8e76e695f76c ^
84efd2077c1e ^

















19f6634f41ff ^

54b312290077 ^
1b1d463d08be ^












54b312290077 ^
1b1d463d08be ^



















54b312290077 ^

422af17c67b8 ^
8a4a8a03d3a3 ^










422af17c67b8 ^
8a4a8a03d3a3 ^
















422af17c67b8 ^


79730c7c8148 ^











422af17c67b8 ^
79730c7c8148 ^
















422af17c67b8 ^


97f792ee3cd6 ^










422af17c67b8 ^
8e76e695f76c ^
97f792ee3cd6 ^
















422af17c67b8 ^

e56fa7cd3dbc ^
8e76e695f76c ^

e56fa7cd3dbc ^


8e76e695f76c ^

e56fa7cd3dbc ^

19f6634f41ff ^

54b312290077 ^
422af17c67b8 ^


19f6634f41ff ^







e56fa7cd3dbc ^










19f6634f41ff ^

e56fa7cd3dbc ^
19f6634f41ff ^


04dacacc14b4 ^



12e364b9f08a ^
eb34e877ccbc ^
12e364b9f08a ^

19f6634f41ff ^
04dacacc14b4 ^
12e364b9f08a ^

464129edbccf ^
4616881022a1 ^



e4a3dd33e575 ^
4616881022a1 ^




ec17f452f4e9 ^


2ee0deec49bd ^








464129edbccf ^
2ee0deec49bd ^

4616881022a1 ^


e4a3dd33e575 ^
b4d4dfbcbb73 ^

4616881022a1 ^




















4616881022a1 ^

464129edbccf ^
4616881022a1 ^






464129edbccf ^
4616881022a1 ^






















8c395e740bbb ^
e4a3dd33e575 ^
4616881022a1 ^


0e7bf2f4494e ^
4616881022a1 ^


ab0592b9fb9b ^







7f44582ea772 ^
ab0592b9fb9b ^


65bd6e4607a1 ^

ab0592b9fb9b ^



d1e0863701a9 ^
ab0592b9fb9b ^


















ab0592b9fb9b ^
12e364b9f08a ^
5f2513950f1f ^





























3ab477012935 ^
12e364b9f08a ^

b9b141e8535f ^
12e364b9f08a ^



22ad57ba5eac ^
5233d1ebb6c9 ^
12e364b9f08a ^



ec17f452f4e9 ^

2ee0d05243a2 ^

12e364b9f08a ^
2ea5117b5f09 ^
12e364b9f08a ^

ec17f452f4e9 ^

2ee0d05243a2 ^

12e364b9f08a ^

5233d1ebb6c9 ^
98d7b5947761 ^
12e364b9f08a ^



b3168c70bb8d ^
12e364b9f08a ^
3ab477012935 ^
26eb2c0c5771 ^
b3168c70bb8d ^
2098dbd1b2a8 ^
12e364b9f08a ^
2098dbd1b2a8 ^
c3d9a224e787 ^
12e364b9f08a ^
12e364b9f08a ^



98d7b5947761 ^
b3168c70bb8d ^
98d7b5947761 ^
12e364b9f08a ^
3ab477012935 ^
26eb2c0c5771 ^
b3168c70bb8d ^
2ea5117b5f09 ^

c3d9a224e787 ^
12e364b9f08a ^
12e364b9f08a ^



2ee0deec49bd ^




12e364b9f08a ^
12c957dc942c ^




















































0274b5aec1b3 ^

3032aeddd85e ^
12e364b9f08a ^
e4a3dd33e575 ^
0274b5aec1b3 ^
12e364b9f08a ^
0274b5aec1b3 ^
12e364b9f08a ^
0aca78449b58 ^
0274b5aec1b3 ^
12e364b9f08a ^


fbb31f48aba3 ^
a298bc0b5913 ^
fbb31f48aba3 ^
12e364b9f08a ^
3ab477012935 ^
a298bc0b5913 ^

12e364b9f08a ^
e4a3dd33e575 ^
12e364b9f08a ^
0274b5aec1b3 ^
12e364b9f08a ^
12e364b9f08a ^
0274b5aec1b3 ^
12e364b9f08a ^
fbb31f48aba3 ^


12e364b9f08a ^
c3d9a224e787 ^
0aca78449b58 ^
12e364b9f08a ^
12e364b9f08a ^


0274b5aec1b3 ^

b4b598fdde27 ^
12e364b9f08a ^
e4a3dd33e575 ^
12e364b9f08a ^
0aca78449b58 ^
0274b5aec1b3 ^
12e364b9f08a ^
0aca78449b58 ^
0274b5aec1b3 ^
12e364b9f08a ^


d32517e392b9 ^
2836c6a8de21 ^
f4c11551e710 ^
12e364b9f08a ^
0274b5aec1b3 ^
12e364b9f08a ^
0274b5aec1b3 ^
ec17f452f4e9 ^



0274b5aec1b3 ^
87241ab85930 ^
0274b5aec1b3 ^





87241ab85930 ^
0274b5aec1b3 ^
0aca78449b58 ^
2836c6a8de21 ^
0274b5aec1b3 ^

368acb3f512b ^


87241ab85930 ^
0274b5aec1b3 ^


98d7b5947761 ^
0274b5aec1b3 ^
75c1f8b726e9 ^
12e364b9f08a ^
12e364b9f08a ^


87241ab85930 ^
12e364b9f08a ^

87241ab85930 ^
12e364b9f08a ^


368acb3f512b ^
87241ab85930 ^
4a185e54e15c ^
12e364b9f08a ^


a298bc0b5913 ^
b4b598fdde27 ^
2836c6a8de21 ^
f4c11551e710 ^
12e364b9f08a ^
0274b5aec1b3 ^
12e364b9f08a ^
0274b5aec1b3 ^
ec17f452f4e9 ^



0274b5aec1b3 ^
87241ab85930 ^
0274b5aec1b3 ^





87241ab85930 ^
0274b5aec1b3 ^

2836c6a8de21 ^
0274b5aec1b3 ^


87241ab85930 ^
0274b5aec1b3 ^


98d7b5947761 ^
0274b5aec1b3 ^
75c1f8b726e9 ^
12e364b9f08a ^
12e364b9f08a ^


87241ab85930 ^
12e364b9f08a ^


bd0d2dcc03db ^


87241ab85930 ^
12e364b9f08a ^

bd0d2dcc03db ^
3f833b54f1be ^
bd0d2dcc03db ^
ec17f452f4e9 ^

12e364b9f08a ^

87241ab85930 ^
12e364b9f08a ^


87241ab85930 ^
12e364b9f08a ^


3cb3fa3b7914 ^
87241ab85930 ^
3cb3fa3b7914 ^
12e364b9f08a ^


3ab477012935 ^
12e364b9f08a ^
2ea5117b5f09 ^
52063eca7fd0 ^
12e364b9f08a ^
d32517e392b9 ^
b32c4997c03d ^
12e364b9f08a ^
d32517e392b9 ^
6c5fed359fc7 ^

12e364b9f08a ^
22ad57ba5eac ^
9fd04060abdf ^
12e364b9f08a ^
6c5fed359fc7 ^


12e364b9f08a ^
22ad57ba5eac ^
9fd04060abdf ^
12e364b9f08a ^

4abce83dca11 ^
d32517e392b9 ^

12e364b9f08a ^
6c5fed359fc7 ^
12e364b9f08a ^
b32c4997c03d ^










9fd04060abdf ^
b32c4997c03d ^

820b11b6bb49 ^
12c957dc942c ^
12e364b9f08a ^
6c5fed359fc7 ^
12e364b9f08a ^
9fd04060abdf ^
3032aeddd85e ^
98d7b5947761 ^
12e364b9f08a ^


3ab477012935 ^
12e364b9f08a ^
2ea5117b5f09 ^
52063eca7fd0 ^
d32517e392b9 ^
12e364b9f08a ^

d32517e392b9 ^
dff54cd60567 ^
22ad57ba5eac ^
dff54cd60567 ^
22ad57ba5eac ^
12e364b9f08a ^
3032aeddd85e ^
98d7b5947761 ^
d32517e392b9 ^

12e364b9f08a ^


317d9614a980 ^

12e364b9f08a ^
2ea5117b5f09 ^
e82ba62e23bc ^
d32517e392b9 ^
12e364b9f08a ^
12e364b9f08a ^
654bada025db ^


12e364b9f08a ^
d32517e392b9 ^
654bada025db ^

12e364b9f08a ^
22ad57ba5eac ^
654bada025db ^

12e364b9f08a ^
22ad57ba5eac ^
e4a3dd33e575 ^
654bada025db ^
12e364b9f08a ^
22ad57ba5eac ^
654bada025db ^
a07d7c3858ae ^


654bada025db ^



654bada025db ^

12e364b9f08a ^
3032aeddd85e ^
98d7b5947761 ^
12e364b9f08a ^


3ab477012935 ^
12e364b9f08a ^
2ea5117b5f09 ^
52063eca7fd0 ^

a298bc0b5913 ^
d32517e392b9 ^
b32c4997c03d ^
12e364b9f08a ^

a298bc0b5913 ^

c60c8e26983f ^
12e364b9f08a ^
a298bc0b5913 ^
c6af7a9cbcfa ^
12e364b9f08a ^
a298bc0b5913 ^

c60c8e26983f ^
12e364b9f08a ^
22ad57ba5eac ^
c6af7a9cbcfa ^
12e364b9f08a ^
a298bc0b5913 ^


c60c8e26983f ^
12e364b9f08a ^
a298bc0b5913 ^
c6af7a9cbcfa ^
12e364b9f08a ^
a298bc0b5913 ^
c60c8e26983f ^


12e364b9f08a ^
22ad57ba5eac ^
c6af7a9cbcfa ^
12e364b9f08a ^
97a84f120378 ^
a298bc0b5913 ^






c60c8e26983f ^
12e364b9f08a ^

a3ef1a8e9391 ^




b32c4997c03d ^






c6af7a9cbcfa ^
b32c4997c03d ^


12c957dc942c ^



c60c8e26983f ^
12e364b9f08a ^
c6af7a9cbcfa ^
b4b598fdde27 ^
12e364b9f08a ^
4da3336c2585 ^
12e364b9f08a ^


3ab477012935 ^
12e364b9f08a ^
2ea5117b5f09 ^
52063eca7fd0 ^

2ea5117b5f09 ^
a298bc0b5913 ^
12e364b9f08a ^

a298bc0b5913 ^
0278a9055ac9 ^

12e364b9f08a ^
22ad57ba5eac ^
0278a9055ac9 ^

12e364b9f08a ^
22ad57ba5eac ^
12e364b9f08a ^
0278a9055ac9 ^
b4b598fdde27 ^
0278a9055ac9 ^
4da3336c2585 ^
12e364b9f08a ^


3ab477012935 ^
12e364b9f08a ^
2ea5117b5f09 ^
52063eca7fd0 ^

a298bc0b5913 ^
12e364b9f08a ^

a298bc0b5913 ^
61715c8b9c53 ^
22ad57ba5eac ^
61715c8b9c53 ^
22ad57ba5eac ^
12e364b9f08a ^
61715c8b9c53 ^
b4b598fdde27 ^
12e364b9f08a ^
4da3336c2585 ^
12e364b9f08a ^

ec17f452f4e9 ^







12e364b9f08a ^

ec17f452f4e9 ^


12e364b9f08a ^

d5b3f1dccee4 ^
c1f834eb104d ^
12e364b9f08a ^
3103dc0304fd ^
12e364b9f08a ^
dde29996cdb1 ^


c1f834eb104d ^
dde29996cdb1 ^


3103dc0304fd ^
dde29996cdb1 ^

12e364b9f08a ^



12e364b9f08a ^
dde29996cdb1 ^
12e364b9f08a ^


c1f834eb104d ^
12e364b9f08a ^
597c338f0a60 ^
3103dc0304fd ^
12e364b9f08a ^

c1f834eb104d ^
12e364b9f08a ^




d5b3f1dccee4 ^
cafefc0ca1a3 ^

26eb2c0c5771 ^
c3d9a224e787 ^
d19642f656c8 ^

cafefc0ca1a3 ^
12e364b9f08a ^



c3d9a224e787 ^
d19642f656c8 ^

cafefc0ca1a3 ^
12e364b9f08a ^




cafefc0ca1a3 ^
84982fbf0f0d ^
12e364b9f08a ^

ec17f452f4e9 ^





12e364b9f08a ^
d3368a58776b ^
12e364b9f08a ^

eb34e877ccbc ^
12e364b9f08a ^

12e364b9f08a ^
d3368a58776b ^
12e364b9f08a ^



26eb2c0c5771 ^
12e364b9f08a ^
eb34e877ccbc ^
12e364b9f08a ^


12e364b9f08a ^
ec17f452f4e9 ^





12e364b9f08a ^
d3368a58776b ^
12e364b9f08a ^

eb34e877ccbc ^
12e364b9f08a ^

12e364b9f08a ^

77a0449d4cf3 ^
12e364b9f08a ^

26eb2c0c5771 ^
12e364b9f08a ^

260d89929c2f ^
77a0449d4cf3 ^
12e364b9f08a ^


77a0449d4cf3 ^
12e364b9f08a ^

26eb2c0c5771 ^
12e364b9f08a ^

77a0449d4cf3 ^

12e364b9f08a ^


77a0449d4cf3 ^
12e364b9f08a ^

26eb2c0c5771 ^
12e364b9f08a ^

77a0449d4cf3 ^

12e364b9f08a ^

12e364b9f08a ^
ec17f452f4e9 ^
12e364b9f08a ^
ec17f452f4e9 ^
12e364b9f08a ^


ec17f452f4e9 ^
12e364b9f08a ^






ec17f452f4e9 ^




12e364b9f08a ^




26eb2c0c5771 ^
12e364b9f08a ^


ec17f452f4e9 ^





12e364b9f08a ^



2cc1a1b3eeee ^
12e364b9f08a ^

ec17f452f4e9 ^






12e364b9f08a ^

3ab477012935 ^
12e364b9f08a ^
ea0dcfcf6d6e ^

6a55e3c33762 ^
38f736e974e3 ^
12e364b9f08a ^








ec17f452f4e9 ^


12e364b9f08a ^






ec17f452f4e9 ^





12e364b9f08a ^



2ea5117b5f09 ^
12e364b9f08a ^








2ea5117b5f09 ^
12e364b9f08a ^
2ea5117b5f09 ^
12e364b9f08a ^
2ea5117b5f09 ^
12e364b9f08a ^
2ea5117b5f09 ^
12e364b9f08a ^
eb34e877ccbc ^
12e364b9f08a ^


ec17f452f4e9 ^
ec17f452f4e9 ^



12e364b9f08a ^
ec17f452f4e9 ^
12e364b9f08a ^
ec17f452f4e9 ^

12e364b9f08a ^

b06bdf7d5bd4 ^
12e364b9f08a ^
e82ba62e23bc ^

12e364b9f08a ^
ddf5de536949 ^
12e364b9f08a ^

ddf5de536949 ^
12e364b9f08a ^


ec17f452f4e9 ^

12e364b9f08a ^


ddf5de536949 ^
2ea5117b5f09 ^
98d7b5947761 ^
12e364b9f08a ^

2ea5117b5f09 ^
12e364b9f08a ^




ddf5de536949 ^
119296eaa8a5 ^
12e364b9f08a ^

ec17f452f4e9 ^



12e364b9f08a ^
bd5b9b32d282 ^
3ab477012935 ^
12e364b9f08a ^




38f736e974e3 ^
12e364b9f08a ^
12e364b9f08a ^
2ea5117b5f09 ^
ec17f452f4e9 ^









12e364b9f08a ^

a07d7c3858ae ^



12e364b9f08a ^

ec17f452f4e9 ^





ddf5de536949 ^


12e364b9f08a ^




5f3a7e364608 ^















d5b3f1dccee4 ^
524b0b63a185 ^
5fc0229ae5e6 ^
b3c55b13a1e4 ^
524b0b63a185 ^
0aca78449b58 ^
524b0b63a185 ^
0aca78449b58 ^
524b0b63a185 ^


12e364b9f08a ^
12e364b9f08a ^

e6bdb904c1d8 ^

3ab477012935 ^
e6bdb904c1d8 ^

12e364b9f08a ^
12e364b9f08a ^


98d7b5947761 ^
2ea5117b5f09 ^

12e364b9f08a ^


12e364b9f08a ^
c3d9a224e787 ^
d19642f656c8 ^

e6bdb904c1d8 ^
12e364b9f08a ^




e6bdb904c1d8 ^
12e364b9f08a ^
e6bdb904c1d8 ^
12e364b9f08a ^




c3d9a224e787 ^
d19642f656c8 ^

e6bdb904c1d8 ^
12e364b9f08a ^





c3d9a224e787 ^
e6bdb904c1d8 ^

3ab477012935 ^
12e364b9f08a ^





c3d9a224e787 ^
e6bdb904c1d8 ^
3ab477012935 ^
e6bdb904c1d8 ^
3ab477012935 ^
12e364b9f08a ^





ebec896762ee ^
e6bdb904c1d8 ^
75c1f8b726e9 ^
12e364b9f08a ^





ebec896762ee ^
e6bdb904c1d8 ^
75c1f8b726e9 ^
12e364b9f08a ^



12e364b9f08a ^
12e364b9f08a ^

87241ab85930 ^
d32517e392b9 ^
12e364b9f08a ^
4b4fd43a4376 ^
0274b5aec1b3 ^
0274b5aec1b3 ^





12e364b9f08a ^

87241ab85930 ^
d32517e392b9 ^
12e364b9f08a ^
0274b5aec1b3 ^




12e364b9f08a ^

87241ab85930 ^
a298bc0b5913 ^
12e364b9f08a ^
0274b5aec1b3 ^






addce19f58c5 ^
12e364b9f08a ^

87241ab85930 ^
a298bc0b5913 ^
12e364b9f08a ^
0274b5aec1b3 ^




12e364b9f08a ^

87241ab85930 ^
ea3a5aafe0fb ^

12e364b9f08a ^
12e364b9f08a ^
b4b598fdde27 ^
bd0d2dcc03db ^
0274b5aec1b3 ^


12e364b9f08a ^
12e364b9f08a ^
87241ab85930 ^
a298bc0b5913 ^
12e364b9f08a ^

b4b598fdde27 ^
bd0d2dcc03db ^
0274b5aec1b3 ^


12e364b9f08a ^

ec17f452f4e9 ^







e56fa7cd3dbc ^

ec17f452f4e9 ^

e56fa7cd3dbc ^

8e76e695f76c ^

e56fa7cd3dbc ^
9421736356cb ^
80224f064ead ^
e56fa7cd3dbc ^
ebec896762ee ^
e56fa7cd3dbc ^

80224f064ead ^


e56fa7cd3dbc ^


ec17f452f4e9 ^







e56fa7cd3dbc ^

ec17f452f4e9 ^

e56fa7cd3dbc ^

8e76e695f76c ^

e56fa7cd3dbc ^
9421736356cb ^
e56fa7cd3dbc ^
ebec896762ee ^
e56fa7cd3dbc ^





e3420ed66295 ^




780fcad38a2b ^
e3420ed66295 ^










a07d7c3858ae ^




e3420ed66295 ^

















5f3a7e364608 ^

















e3420ed66295 ^


2500276efe1c ^
e3420ed66295 ^









d5b3f1dccee4 ^
e3420ed66295 ^




















0f570fc0ae56 ^
e3420ed66295 ^







4616881022a1 ^
e3420ed66295 ^




4616881022a1 ^
e3420ed66295 ^










1366a3db3dcf ^








612b81c9315c ^

























































511474a5077b ^


















































































































8a2853279b33 ^




















a9c73937436a ^






























3d8394c86f1b ^


































































55c67dcaac78 ^

12e364b9f08a ^
1366a3db3dcf ^
d5b3f1dccee4 ^
d3368a58776b ^



1366a3db3dcf ^
12e364b9f08a ^
84982fbf0f0d ^
12e364b9f08a ^
c732623b0bb3 ^
d3368a58776b ^
c732623b0bb3 ^
1366a3db3dcf ^

d3368a58776b ^


8a1182eb34dc ^
1366a3db3dcf ^
8a1182eb34dc ^

5aa8ae57c560 ^
1366a3db3dcf ^


12e364b9f08a ^
4da3336c2585 ^






4da3336c2585 ^
4da3336c2585 ^

0bde29799a82 ^
12e364b9f08a ^
eb34e877ccbc ^

4cb005a93cee ^
1366a3db3dcf ^

4cb005a93cee ^
12e364b9f08a ^
c79b28f7332e ^
1366a3db3dcf ^


12e364b9f08a ^
1366a3db3dcf ^

















e3420ed66295 ^

55c67dcaac78 ^

12e364b9f08a ^
12e364b9f08a ^

c79b28f7332e ^

0bde29799a82 ^
4da3336c2585 ^
1783319f47c5 ^
c3d9a224e787 ^
8a1182eb34dc ^
addceb12a3ed ^
04dacacc14b4 ^
12e364b9f08a ^
55c67dcaac78 ^







55c67dcaac78 ^










1fc07f99134b ^


d5b3f1dccee4 ^



0c9f3536cc71 ^
d5b3f1dccee4 ^








55c67dcaac78 ^



35e606de5145 ^
d5b3f1dccee4 ^
55c67dcaac78 ^












12e364b9f08a ^

12e364b9f08a ^
b615d628b672 ^

b615d628b672 ^
55c67dcaac78 ^

12e364b9f08a ^





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277

                      
                                               

                       


                                                                          







                                                                      
                       
                       
                        

                     


                                  
                       
                             
 


                             
                            
 
                                                            
 


                                             
                                                


                                                          






                                                                           


                    
                              
 


                                                         
                                                  












                                                            




                                                                    
                           
                                                                      

                                                 
 








                                      
                                                   
 

                                                    
 
                                              
 
                                                          
                                     
                                                                             

                                                                     

                                                                         
                                                                     


                                                                  
                                                      
 


                                                                               


                                                                        
                                                      
                                        
 
  
                                                                            




                                                                               
                                          
                                                                        



                                              


                                                                    





                                                         
                                                         



                                                 

                                                                            



                                                                               

                                                        








                                                                        






                                                                             


                              



                                 
                                     

  

                                                                            
 
                                
                                                                  
 

                                                  










                                                                           
                                                   
                                                              

















                                                                 

                                  
                                                  












                                                                          
                                                   



















                                                                 

                                  
                                                                            










                                                                          
                                                                             
















                                                                 


                                                                             











                                                                 
                                                                              
















                                                                 


                                                       










                                                                             
                                                        
                                                                   
















                                                                   

                                       
                                                       

                                                                   


                                                      

                                                                  

                                     

                                                         
                                  


                                       







                                                            










                                                                

                                                                  
                                        


            



                                                        
                                        
                                                              

                               
                                              
                                                

  
              



                                                             
                 




                                                                      


                                                                            








                                                                       
           

                                                         


                                                             
                 

                       




















                                                                      

 
                                                   






                                                             
             






















                                                                         
                                                                      
                   


                                                   
                                             


                     







                                                                   
                                       


                                

                                               



                         
 


















                                                                         
 
           





























                                                                               
                                             

                                  
                                                



                                                                        
                                                        
                                 



                                                                       

                                                                      

                             
                  
                                    

                                                          

                                                         

                                 

                                                
            
                                               



                                                                          
                                                                         
 
                                        
 
                                                            
                                               
                       
 
                                                         
                                                                           



                       
                                                  
                                                                       
                                                
 
                                        
 
                                                            

                                                             
                                                         
                                                                           



                       




                     
           




















































                                                                               

                                                               
                           
 
                             
                                                                  
 
                                               
                       
 
                                                     


           
                                                      
                                                                  
                                                                      
 
                                        

                                       
 
                                
                                                                  
                                             
                       
 
                                                                       
 


                                                              
 
                                                         
                                                                         
                       


           

                                                                  
                              
 
                             
                                                                  
 
                                               
                       
 
                                                     


           
                                         
                                                             
                                            
 
                                                         
 
                        



                                                                            
                                   
                                 





                                                                               
                                 
         
 
                            

                                                                  


                                                                  
                               


                                         
                                                                
                                                     
         
 


                                                 
                                                     

                                           
                                                      


                              
 
            
                                               


           
                                            
                                                       
                                                                     
                                                    
 
                                                         
 
                        



                                                                            
                                   
                                 





                                                                               
                                 

         
                            


                                                                        
                                         


                                         
                                                                
                                                     
         
 


                                             
                                                        


                                                                               


                                                                         
                                                                

                                                                               
                                                                              
                                                   
                                                                   

                                                                   

                                                       
                                                               


                                              
                                                         


                              
 
            
                                                  


           
                                           
 
                                                           
                                            
                                        
                                      
                                          
 
                                                                            

                                                               
                                                        
                                                        
                                    
         


                                                               
                                                        
                                                          
                                    

         
                                            

                                                   
 
                                                                              
 










                                                                               
                                    

                                              
                                                                             
                                                     
 
                                                                             
 
               
                                                               
                                                                


           
                                            
 
                                                           
                                             
                                      

                                        
                                                                            
                      
                                                       
                                              
                                                        
 
                                                                
                                                                

                                                                         


           

                                                
 
                                                           
                   
                                      
                                        
 


                                                        
 
                                                                            

                                                                  
                                                        
                                                       

                                                                  
                                                        
                                                       
                                               
                                                                  
                                                        
                                                                         
                


                                                          



                                                                     

                                                               
         
                                                                  
                                                                


           
                                                 
 
                                                           

                                               
                                             
                                      
                                          

                                        

                                                                            
                                                                          
                                                        
                                                       
                                 
         

                                           
                                                                          
                                                        
                                                       
                                 
         


                                                                   
                                                                          
                                                        
                                                        
                                 
         
 


                                                                          
                                                        
                                                          
                                 
         
 






                                                               
                                                                

                                                 




                                                                                






                                                                          
                                 


                                                                        



                                                              
                                                               
                                                 
            
                                                      
                                                               
                                                                  


           
                                                      
 
                                                           

                                                     
                                                                         
                                      

                                        
                                                                   

                                                                               
                                                        
                                                          

                                                                               
                                                        
                                                          
         
                                                       
                                              
                                                                            
                                                                          


           
                                                  
 
                                                           

                                                
                                      

                                        
                                                                   
                      
                                                          
                                              
                                                        
 
                                                       
                                                              
                                                                        
                                                                          

 







                                                                           

                                                                    


                                                                       

          
                                                                       
                                                                            
 
                           
 


                                                             
                                                                     


                                                             
                                                                   

                                                            



                              
 
                                      


           
                                                                         
 
                        
                                    

                                 
                                                                     




                                  
                                                                     

                               
 
                                                

                                                                              
                                                                             



                                                           
                                                

                                                                              
                                                                           




                                                           
                                                                        
                                                                   

 





                                                                     
   
          

                                
                                                                            

                                      
 
          



                                              
 
                                                       
                                                                               


                                      
 





                                                                      
   
          

                                   
                                                                             

                                      

           
                                                       

                                              
 

                                         
                                             
                                               


           
                                                          

                                                 
 

                                         

                                               


           
                                                          

                                                 
 

                                         

                                               

 
  
                                                                  
                                                            
                                                                   


                                                                     
                                         






                                                                      




                                                                                




                                            
 


                                      





                                                                              



                                 
                                                                  

 






                                                                             

                                   
                                                         
 

                                        
                                                                
                 








                                                        


                                                             






                                                            





                                                                               



                                                            
                                                             








                                                                              
                                                       
                                                   
                                                 
                                                      
                                                      
                                                         
                                                       
 
                                                                               


                                 
   



                                                                         
                                                                 
                                                                 
                                                 

                                             

          
                                                
 

                              
 
                                                  

                                               
                                                                 


                                                                      

                                                                     


                                      
                                                                    
                                                                               
                                                                 

                                                                              
                                                                                




                                                         
                                                    
                       

 



                                                                              
   
           
                                                            




                                                
                 
                       
 
                                                          









                                                                       

                                                 



                                                               

                                                 





                                                                      


                                                                     




                                                 















                                                                     
                                              
 
                     
                     
 
                                                                             
                         
 


                    
           

                                                        

                                                     
                                     

                                   
 


                                                                     
                                            

                                              


                           
                                     
                                                

                                                                              
                                                                         




                                                              
                                                              
                                                            
                                                       




                                                        
                                                

                                                                              
                                                                          





                                                               
                                                

                                                     
                                                                      





                                                             
                                                
                                                      
                                                               
                                                   
                                                                      





                                                             
                                                              
                                                 
                





                                                               
                                                                 
                                                       
                



                                                               
                                                                    

 
    
                                                                
 
                          
                                            





                                                                      

 
    
                                                                 
 




                                                                       

 
    
                                                                   
 






                                                                            
                                         

 
    
                                                                    
 




                                                                             

 
    

                                                    
 
                                                                  
                                                        
                                                            


                                         
 
 
    
                                                                   

                                                                  
                                                        
                                                            


                                         

 







                                                                             

                                                                           

                                                                   

                                                       

                                                                  
 
                        
                
 
                                     

                               


                                                  


                     







                                                                            

                                                                           

                                                                   

                                                      

                                                                 
 
                        
 
                                     





                                            




                                                                
                     










                                                          




                                                                         

















                                                                

















                                                                         


                                                                   
                       









                                                                             
                         




















                                                                     
          







                                                                                
                                                                           




                                                                       
                                                                          










                                                                 








                                               

























































                                                                     


















































































































                                                                              




















                                                                        






























                                                                               


































































                                                                                

                                                  
 
                          
                 



                                                            
                           
 
                                                                           
 
                                                                  
                                                                            
                               

                           


                                                                  
                
                                           

         
                                                 


                                                                    
 






                                                                  
 

                                                         
                                                                      
 

                                                                          
                                                                                

                                       
         
                                                                          
 


                                      
 

















                                                                              

 

                                                  
 

                                                                 

                        
                                                           
                                                                
 
                                                
 
                                                                         
                                                                  
                                                                 







                                                          










                                                


                                             



                                                  
                                                   








                                                                   



                            
 
                                     












                                                               

 
                                                            

                                                                   
 

                         





                                                                          
/* visorchipset_main.c
 *
 * Copyright (C) 2010 - 2015 UNISYS CORPORATION
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 */

#include <linux/acpi.h>
#include <linux/cdev.h>
#include <linux/ctype.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/nls.h>
#include <linux/netdevice.h>
#include <linux/platform_device.h>
#include <linux/uuid.h>
#include <linux/crash_dump.h>

#include "version.h"
#include "visorbus.h"
#include "visorbus_private.h"
#include "vmcallinterface.h"

#define CURRENT_FILE_PC VISOR_CHIPSET_PC_visorchipset_main_c

#define POLLJIFFIES_CONTROLVMCHANNEL_FAST   1
#define POLLJIFFIES_CONTROLVMCHANNEL_SLOW 100

#define MAX_CONTROLVM_PAYLOAD_BYTES (1024 * 128)

#define VISORCHIPSET_MMAP_CONTROLCHANOFFSET	0x00000000

#define UNISYS_SPAR_LEAF_ID 0x40000000

/* The s-Par leaf ID returns "UnisysSpar64" encoded across ebx, ecx, edx */
#define UNISYS_SPAR_ID_EBX 0x73696e55
#define UNISYS_SPAR_ID_ECX 0x70537379
#define UNISYS_SPAR_ID_EDX 0x34367261

/*
 * Module parameters
 */
static int visorchipset_major;

static int
visorchipset_open(struct inode *inode, struct file *file)
{
	unsigned int minor_number = iminor(inode);

	if (minor_number)
		return -ENODEV;
	file->private_data = NULL;
	return 0;
}

static int
visorchipset_release(struct inode *inode, struct file *file)
{
	return 0;
}

/*
 * When the controlvm channel is idle for at least MIN_IDLE_SECONDS,
 * we switch to slow polling mode. As soon as we get a controlvm
 * message, we switch back to fast polling mode.
 */
#define MIN_IDLE_SECONDS 10
static unsigned long poll_jiffies = POLLJIFFIES_CONTROLVMCHANNEL_FAST;
/* when we got our last controlvm message */
static unsigned long most_recent_message_jiffies;

struct parser_context {
	unsigned long allocbytes;
	unsigned long param_bytes;
	u8 *curr;
	unsigned long bytes_remaining;
	bool byte_stream;
	char data[0];
};

static struct delayed_work periodic_controlvm_work;

static struct cdev file_cdev;
static struct visorchannel **file_controlvm_channel;

static struct visorchannel *controlvm_channel;

/* Manages the request payload in the controlvm channel */
struct visor_controlvm_payload_info {
	u8 *ptr;		/* pointer to base address of payload pool */
	u64 offset;		/*
				 * offset from beginning of controlvm
				 * channel to beginning of payload * pool
				 */
	u32 bytes;		/* number of bytes in payload pool */
};

static struct visor_controlvm_payload_info controlvm_payload_info;
static unsigned long controlvm_payload_bytes_buffered;

/*
 * The following globals are used to handle the scenario where we are unable to
 * offload the payload from a controlvm message due to memory requirements. In
 * this scenario, we simply stash the controlvm message, then attempt to
 * process it again the next time controlvm_periodic_work() runs.
 */
static struct controlvm_message controlvm_pending_msg;
static bool controlvm_pending_msg_valid;

/*
 * This describes a buffer and its current state of transfer (e.g., how many
 * bytes have already been supplied as putfile data, and how many bytes are
 * remaining) for a putfile_request.
 */
struct putfile_active_buffer {
	/* a payload from a controlvm message, containing a file data buffer */
	struct parser_context *parser_ctx;
	/* points within data area of parser_ctx to next byte of data */
	size_t bytes_remaining;
};

#define PUTFILE_REQUEST_SIG 0x0906101302281211
/*
 * This identifies a single remote --> local CONTROLVM_TRANSMIT_FILE
 * conversation. Structs of this type are dynamically linked into
 * <Putfile_request_list>.
 */
struct putfile_request {
	u64 sig;		/* PUTFILE_REQUEST_SIG */

	/* header from original TransmitFile request */
	struct controlvm_message_header controlvm_header;

	/* link to next struct putfile_request */
	struct list_head next_putfile_request;

	/*
	 * head of putfile_buffer_entry list, which describes the data to be
	 * supplied as putfile data;
	 * - this list is added to when controlvm messages come in that supply
	 * file data
	 * - this list is removed from via the hotplug program that is actually
	 * consuming these buffers to write as file data
	 */
	struct list_head input_buffer_list;
	spinlock_t req_list_lock;	/* lock for input_buffer_list */

	/* waiters for input_buffer_list to go non-empty */
	wait_queue_head_t input_buffer_wq;

	/* data not yet read within current putfile_buffer_entry */
	struct putfile_active_buffer active_buf;

	/*
	 * <0 = failed, 0 = in-progress, >0 = successful;
	 * note that this must be set with req_list_lock, and if you set <0,
	 * it is your responsibility to also free up all of the other objects
	 * in this struct (like input_buffer_list, active_buf.parser_ctx)
	 * before releasing the lock
	 */
	int completion_status;
};

struct parahotplug_request {
	struct list_head list;
	int id;
	unsigned long expiration;
	struct controlvm_message msg;
};

static LIST_HEAD(parahotplug_request_list);
static DEFINE_SPINLOCK(parahotplug_request_list_lock);	/* lock for above */

/* info for /dev/visorchipset */
static dev_t major_dev = -1; /*< indicates major num for device */

/* prototypes for attributes */
static ssize_t toolaction_show(struct device *dev,
			       struct device_attribute *attr,
			       char *buf)
{
	u8 tool_action = 0;

	visorchannel_read(controlvm_channel,
			  offsetof(struct spar_controlvm_channel_protocol,
				   tool_action), &tool_action, sizeof(u8));
	return scnprintf(buf, PAGE_SIZE, "%u\n", tool_action);
}

static ssize_t toolaction_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	u8 tool_action;
	int ret;

	if (kstrtou8(buf, 10, &tool_action))
		return -EINVAL;

	ret = visorchannel_write
		(controlvm_channel,
		 offsetof(struct spar_controlvm_channel_protocol,
			  tool_action),
		 &tool_action, sizeof(u8));

	if (ret)
		return ret;
	return count;
}
static DEVICE_ATTR_RW(toolaction);

static ssize_t boottotool_show(struct device *dev,
			       struct device_attribute *attr,
			       char *buf)
{
	struct efi_spar_indication efi_spar_indication;

	visorchannel_read(controlvm_channel,
			  offsetof(struct spar_controlvm_channel_protocol,
				   efi_spar_ind), &efi_spar_indication,
			  sizeof(struct efi_spar_indication));
	return scnprintf(buf, PAGE_SIZE, "%u\n",
			 efi_spar_indication.boot_to_tool);
}

static ssize_t boottotool_store(struct device *dev,
				struct device_attribute *attr,
				const char *buf, size_t count)
{
	int val, ret;
	struct efi_spar_indication efi_spar_indication;

	if (kstrtoint(buf, 10, &val))
		return -EINVAL;

	efi_spar_indication.boot_to_tool = val;
	ret = visorchannel_write
		(controlvm_channel,
		 offsetof(struct spar_controlvm_channel_protocol,
			  efi_spar_ind), &(efi_spar_indication),
		 sizeof(struct efi_spar_indication));

	if (ret)
		return ret;
	return count;
}
static DEVICE_ATTR_RW(boottotool);

static ssize_t error_show(struct device *dev, struct device_attribute *attr,
			  char *buf)
{
	u32 error = 0;

	visorchannel_read(controlvm_channel,
			  offsetof(struct spar_controlvm_channel_protocol,
				   installation_error),
			  &error, sizeof(u32));
	return scnprintf(buf, PAGE_SIZE, "%i\n", error);
}

static ssize_t error_store(struct device *dev, struct device_attribute *attr,
			   const char *buf, size_t count)
{
	u32 error;
	int ret;

	if (kstrtou32(buf, 10, &error))
		return -EINVAL;

	ret = visorchannel_write
		(controlvm_channel,
		 offsetof(struct spar_controlvm_channel_protocol,
			  installation_error),
		 &error, sizeof(u32));
	if (ret)
		return ret;
	return count;
}
static DEVICE_ATTR_RW(error);

static ssize_t textid_show(struct device *dev, struct device_attribute *attr,
			   char *buf)
{
	u32 text_id = 0;

	visorchannel_read
		(controlvm_channel,
		 offsetof(struct spar_controlvm_channel_protocol,
			  installation_text_id),
		 &text_id, sizeof(u32));
	return scnprintf(buf, PAGE_SIZE, "%i\n", text_id);
}

static ssize_t textid_store(struct device *dev, struct device_attribute *attr,
			    const char *buf, size_t count)
{
	u32 text_id;
	int ret;

	if (kstrtou32(buf, 10, &text_id))
		return -EINVAL;

	ret = visorchannel_write
		(controlvm_channel,
		 offsetof(struct spar_controlvm_channel_protocol,
			  installation_text_id),
		 &text_id, sizeof(u32));
	if (ret)
		return ret;
	return count;
}
static DEVICE_ATTR_RW(textid);

static ssize_t remaining_steps_show(struct device *dev,
				    struct device_attribute *attr, char *buf)
{
	u16 remaining_steps = 0;

	visorchannel_read(controlvm_channel,
			  offsetof(struct spar_controlvm_channel_protocol,
				   installation_remaining_steps),
			  &remaining_steps, sizeof(u16));
	return scnprintf(buf, PAGE_SIZE, "%hu\n", remaining_steps);
}

static ssize_t remaining_steps_store(struct device *dev,
				     struct device_attribute *attr,
				     const char *buf, size_t count)
{
	u16 remaining_steps;
	int ret;

	if (kstrtou16(buf, 10, &remaining_steps))
		return -EINVAL;

	ret = visorchannel_write
		(controlvm_channel,
		 offsetof(struct spar_controlvm_channel_protocol,
			  installation_remaining_steps),
		 &remaining_steps, sizeof(u16));
	if (ret)
		return ret;
	return count;
}
static DEVICE_ATTR_RW(remaining_steps);

static ssize_t devicedisabled_store(struct device *dev,
				    struct device_attribute *attr,
				    const char *buf, size_t count);
static DEVICE_ATTR_WO(devicedisabled);

static ssize_t deviceenabled_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count);
static DEVICE_ATTR_WO(deviceenabled);

static struct attribute *visorchipset_install_attrs[] = {
	&dev_attr_toolaction.attr,
	&dev_attr_boottotool.attr,
	&dev_attr_error.attr,
	&dev_attr_textid.attr,
	&dev_attr_remaining_steps.attr,
	NULL
};

static struct attribute_group visorchipset_install_group = {
	.name = "install",
	.attrs = visorchipset_install_attrs
};

static struct attribute *visorchipset_parahotplug_attrs[] = {
	&dev_attr_devicedisabled.attr,
	&dev_attr_deviceenabled.attr,
	NULL
};

static struct attribute_group visorchipset_parahotplug_group = {
	.name = "parahotplug",
	.attrs = visorchipset_parahotplug_attrs
};

static const struct attribute_group *visorchipset_dev_groups[] = {
	&visorchipset_install_group,
	&visorchipset_parahotplug_group,
	NULL
};

static void visorchipset_dev_release(struct device *dev)
{
}

/* /sys/devices/platform/visorchipset */
static struct platform_device visorchipset_platform_device = {
	.name = "visorchipset",
	.id = -1,
	.dev.groups = visorchipset_dev_groups,
	.dev.release = visorchipset_dev_release,
};

static uuid_le
parser_id_get(struct parser_context *ctx)
{
	struct spar_controlvm_parameters_header *phdr = NULL;

	if (!ctx)
		return NULL_UUID_LE;
	phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
	return phdr->id;
}

/*
 * Describes the state from the perspective of which controlvm messages have
 * been received for a bus or device.
 */

enum PARSER_WHICH_STRING {
	PARSERSTRING_INITIATOR,
	PARSERSTRING_TARGET,
	PARSERSTRING_CONNECTION,
	PARSERSTRING_NAME, /* TODO: only PARSERSTRING_NAME is used ? */
};

static void
parser_param_start(struct parser_context *ctx,
		   enum PARSER_WHICH_STRING which_string)
{
	struct spar_controlvm_parameters_header *phdr = NULL;

	if (!ctx)
		return;

	phdr = (struct spar_controlvm_parameters_header *)(ctx->data);
	switch (which_string) {
	case PARSERSTRING_INITIATOR:
		ctx->curr = ctx->data + phdr->initiator_offset;
		ctx->bytes_remaining = phdr->initiator_length;
		break;
	case PARSERSTRING_TARGET:
		ctx->curr = ctx->data + phdr->target_offset;
		ctx->bytes_remaining = phdr->target_length;
		break;
	case PARSERSTRING_CONNECTION:
		ctx->curr = ctx->data + phdr->connection_offset;
		ctx->bytes_remaining = phdr->connection_length;
		break;
	case PARSERSTRING_NAME:
		ctx->curr = ctx->data + phdr->name_offset;
		ctx->bytes_remaining = phdr->name_length;
		break;
	default:
		break;
	}
}

static void parser_done(struct parser_context *ctx)
{
	if (!ctx)
		return;
	controlvm_payload_bytes_buffered -= ctx->param_bytes;
	kfree(ctx);
}

static void *
parser_string_get(struct parser_context *ctx)
{
	u8 *pscan;
	unsigned long nscan;
	int value_length = -1;
	void *value = NULL;
	int i;

	if (!ctx)
		return NULL;
	pscan = ctx->curr;
	nscan = ctx->bytes_remaining;
	if (nscan == 0)
		return NULL;
	if (!pscan)
		return NULL;
	for (i = 0, value_length = -1; i < nscan; i++)
		if (pscan[i] == '\0') {
			value_length = i;
			break;
		}
	if (value_length < 0)	/* '\0' was not included in the length */
		value_length = nscan;
	value = kmalloc(value_length + 1, GFP_KERNEL | __GFP_NORETRY);
	if (!value)
		return NULL;
	if (value_length > 0)
		memcpy(value, pscan, value_length);
	((u8 *)(value))[value_length] = '\0';
	return value;
}

struct visor_busdev {
	u32 bus_no;
	u32 dev_no;
};

static int match_visorbus_dev_by_id(struct device *dev, void *data)
{
	struct visor_device *vdev = to_visor_device(dev);
	struct visor_busdev *id = data;
	u32 bus_no = id->bus_no;
	u32 dev_no = id->dev_no;

	if ((vdev->chipset_bus_no == bus_no) &&
	    (vdev->chipset_dev_no == dev_no))
		return 1;

	return 0;
}

struct visor_device *visorbus_get_device_by_id(u32 bus_no, u32 dev_no,
					       struct visor_device *from)
{
	struct device *dev;
	struct device *dev_start = NULL;
	struct visor_device *vdev = NULL;
	struct visor_busdev id = {
			.bus_no = bus_no,
			.dev_no = dev_no
		};

	if (from)
		dev_start = &from->device;
	dev = bus_find_device(&visorbus_type, dev_start, (void *)&id,
			      match_visorbus_dev_by_id);
	if (dev)
		vdev = to_visor_device(dev);
	return vdev;
}

static void
controlvm_init_response(struct controlvm_message *msg,
			struct controlvm_message_header *msg_hdr, int response)
{
	memset(msg, 0, sizeof(struct controlvm_message));
	memcpy(&msg->hdr, msg_hdr, sizeof(struct controlvm_message_header));
	msg->hdr.payload_bytes = 0;
	msg->hdr.payload_vm_offset = 0;
	msg->hdr.payload_max_bytes = 0;
	if (response < 0) {
		msg->hdr.flags.failed = 1;
		msg->hdr.completion_status = (u32)(-response);
	}
}

static void
controlvm_respond_chipset_init(struct controlvm_message_header *msg_hdr,
			       int response,
			       enum ultra_chipset_feature features)
{
	struct controlvm_message outmsg;

	controlvm_init_response(&outmsg, msg_hdr, response);
	outmsg.cmd.init_chipset.features = features;
	if (!visorchannel_signalinsert(controlvm_channel,
				       CONTROLVM_QUEUE_REQUEST, &outmsg)) {
		return;
	}
}

static void
chipset_init(struct controlvm_message *inmsg)
{
	static int chipset_inited;
	enum ultra_chipset_feature features = 0;
	int rc = CONTROLVM_RESP_SUCCESS;

	POSTCODE_LINUX_2(CHIPSET_INIT_ENTRY_PC, POSTCODE_SEVERITY_INFO);
	if (chipset_inited) {
		rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
		goto out_respond;
	}
	chipset_inited = 1;
	POSTCODE_LINUX_2(CHIPSET_INIT_EXIT_PC, POSTCODE_SEVERITY_INFO);

	/*
	 * Set features to indicate we support parahotplug (if Command
	 * also supports it).
	 */
	features =
	    inmsg->cmd.init_chipset.
	    features & ULTRA_CHIPSET_FEATURE_PARA_HOTPLUG;

	/*
	 * Set the "reply" bit so Command knows this is a
	 * features-aware driver.
	 */
	features |= ULTRA_CHIPSET_FEATURE_REPLY;

out_respond:
	if (inmsg->hdr.flags.response_expected)
		controlvm_respond_chipset_init(&inmsg->hdr, rc, features);
}

static void
controlvm_respond(struct controlvm_message_header *msg_hdr, int response)
{
	struct controlvm_message outmsg;

	controlvm_init_response(&outmsg, msg_hdr, response);
	if (outmsg.hdr.flags.test_message == 1)
		return;

	if (!visorchannel_signalinsert(controlvm_channel,
				       CONTROLVM_QUEUE_REQUEST, &outmsg)) {
		return;
	}
}

static void controlvm_respond_physdev_changestate(
		struct controlvm_message_header *msg_hdr, int response,
		struct spar_segment_state state)
{
	struct controlvm_message outmsg;

	controlvm_init_response(&outmsg, msg_hdr, response);
	outmsg.cmd.device_change_state.state = state;
	outmsg.cmd.device_change_state.flags.phys_device = 1;
	if (!visorchannel_signalinsert(controlvm_channel,
				       CONTROLVM_QUEUE_REQUEST, &outmsg)) {
		return;
	}
}

enum crash_obj_type {
	CRASH_DEV,
	CRASH_BUS,
};

static void
save_crash_message(struct controlvm_message *msg, enum crash_obj_type typ)
{
	u32 local_crash_msg_offset;
	u16 local_crash_msg_count;

	if (visorchannel_read(controlvm_channel,
			      offsetof(struct spar_controlvm_channel_protocol,
				       saved_crash_message_count),
			      &local_crash_msg_count, sizeof(u16)) < 0) {
		POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}

	if (local_crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
		POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
				 local_crash_msg_count,
				 POSTCODE_SEVERITY_ERR);
		return;
	}

	if (visorchannel_read(controlvm_channel,
			      offsetof(struct spar_controlvm_channel_protocol,
				       saved_crash_message_offset),
			      &local_crash_msg_offset, sizeof(u32)) < 0) {
		POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}

	if (typ == CRASH_BUS) {
		if (visorchannel_write(controlvm_channel,
				       local_crash_msg_offset,
				       msg,
				       sizeof(struct controlvm_message)) < 0) {
			POSTCODE_LINUX_2(SAVE_MSG_BUS_FAILURE_PC,
					 POSTCODE_SEVERITY_ERR);
			return;
		}
	} else {
		local_crash_msg_offset += sizeof(struct controlvm_message);
		if (visorchannel_write(controlvm_channel,
				       local_crash_msg_offset,
				       msg,
				       sizeof(struct controlvm_message)) < 0) {
			POSTCODE_LINUX_2(SAVE_MSG_DEV_FAILURE_PC,
					 POSTCODE_SEVERITY_ERR);
			return;
		}
	}
}

static void
bus_responder(enum controlvm_id cmd_id,
	      struct controlvm_message_header *pending_msg_hdr,
	      int response)
{
	if (!pending_msg_hdr)
		return;		/* no controlvm response needed */

	if (pending_msg_hdr->id != (u32)cmd_id)
		return;

	controlvm_respond(pending_msg_hdr, response);
}

static void
device_changestate_responder(enum controlvm_id cmd_id,
			     struct visor_device *p, int response,
			     struct spar_segment_state response_state)
{
	struct controlvm_message outmsg;
	u32 bus_no = p->chipset_bus_no;
	u32 dev_no = p->chipset_dev_no;

	if (!p->pending_msg_hdr)
		return;		/* no controlvm response needed */
	if (p->pending_msg_hdr->id != cmd_id)
		return;

	controlvm_init_response(&outmsg, p->pending_msg_hdr, response);

	outmsg.cmd.device_change_state.bus_no = bus_no;
	outmsg.cmd.device_change_state.dev_no = dev_no;
	outmsg.cmd.device_change_state.state = response_state;

	if (!visorchannel_signalinsert(controlvm_channel,
				       CONTROLVM_QUEUE_REQUEST, &outmsg))
		return;
}

static void
device_responder(enum controlvm_id cmd_id,
		 struct controlvm_message_header *pending_msg_hdr,
		 int response)
{
	if (!pending_msg_hdr)
		return;		/* no controlvm response needed */

	if (pending_msg_hdr->id != (u32)cmd_id)
		return;

	controlvm_respond(pending_msg_hdr, response);
}

static void
bus_epilog(struct visor_device *bus_info,
	   u32 cmd, struct controlvm_message_header *msg_hdr,
	   int response, bool need_response)
{
	struct controlvm_message_header *pmsg_hdr = NULL;

	if (!bus_info) {
		/*
		 * relying on a valid passed in response code
		 * be lazy and re-use msg_hdr for this failure, is this ok??
		 */
		pmsg_hdr = msg_hdr;
		goto out_respond;
	}

	if (bus_info->pending_msg_hdr) {
		/* only non-NULL if dev is still waiting on a response */
		response = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
		pmsg_hdr = bus_info->pending_msg_hdr;
		goto out_respond;
	}

	if (need_response) {
		pmsg_hdr = kzalloc(sizeof(*pmsg_hdr), GFP_KERNEL);
		if (!pmsg_hdr) {
			POSTCODE_LINUX_4(MALLOC_FAILURE_PC, cmd,
					 bus_info->chipset_bus_no,
					 POSTCODE_SEVERITY_ERR);
			return;
		}

		memcpy(pmsg_hdr, msg_hdr,
		       sizeof(struct controlvm_message_header));
		bus_info->pending_msg_hdr = pmsg_hdr;
	}

	if (response == CONTROLVM_RESP_SUCCESS) {
		switch (cmd) {
		case CONTROLVM_BUS_CREATE:
			chipset_bus_create(bus_info);
			break;
		case CONTROLVM_BUS_DESTROY:
			chipset_bus_destroy(bus_info);
			break;
		}
	}

out_respond:
	bus_responder(cmd, pmsg_hdr, response);
}

static void
device_epilog(struct visor_device *dev_info,
	      struct spar_segment_state state, u32 cmd,
	      struct controlvm_message_header *msg_hdr, int response,
	      bool need_response, bool for_visorbus)
{
	struct controlvm_message_header *pmsg_hdr = NULL;

	if (!dev_info) {
		/*
		 * relying on a valid passed in response code
		 * be lazy and re-use msg_hdr for this failure, is this ok??
		 */
		pmsg_hdr = msg_hdr;
		goto out_respond;
	}

	if (dev_info->pending_msg_hdr) {
		/* only non-NULL if dev is still waiting on a response */
		response = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
		pmsg_hdr = dev_info->pending_msg_hdr;
		goto out_respond;
	}

	if (need_response) {
		pmsg_hdr = kzalloc(sizeof(*pmsg_hdr), GFP_KERNEL);
		if (!pmsg_hdr) {
			response = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
			goto out_respond;
		}

		memcpy(pmsg_hdr, msg_hdr,
		       sizeof(struct controlvm_message_header));
		dev_info->pending_msg_hdr = pmsg_hdr;
	}

	if (response >= 0) {
		switch (cmd) {
		case CONTROLVM_DEVICE_CREATE:
			chipset_device_create(dev_info);
			break;
		case CONTROLVM_DEVICE_CHANGESTATE:
			/* ServerReady / ServerRunning / SegmentStateRunning */
			if (state.alive == segment_state_running.alive &&
			    state.operating ==
				segment_state_running.operating) {
				chipset_device_resume(dev_info);
			}
			/* ServerNotReady / ServerLost / SegmentStateStandby */
			else if (state.alive == segment_state_standby.alive &&
				 state.operating ==
				 segment_state_standby.operating) {
				/*
				 * technically this is standby case
				 * where server is lost
				 */
				chipset_device_pause(dev_info);
			}
			break;
		case CONTROLVM_DEVICE_DESTROY:
			chipset_device_destroy(dev_info);
			break;
		}
	}

out_respond:
	device_responder(cmd, pmsg_hdr, response);
}

static void
bus_create(struct controlvm_message *inmsg)
{
	struct controlvm_message_packet *cmd = &inmsg->cmd;
	u32 bus_no = cmd->create_bus.bus_no;
	int rc = CONTROLVM_RESP_SUCCESS;
	struct visor_device *bus_info;
	struct visorchannel *visorchannel;

	bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
	if (bus_info && (bus_info->state.created == 1)) {
		POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
		goto out_bus_epilog;
	}
	bus_info = kzalloc(sizeof(*bus_info), GFP_KERNEL);
	if (!bus_info) {
		POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
		goto out_bus_epilog;
	}

	INIT_LIST_HEAD(&bus_info->list_all);
	bus_info->chipset_bus_no = bus_no;
	bus_info->chipset_dev_no = BUS_ROOT_DEVICE;

	POSTCODE_LINUX_3(BUS_CREATE_ENTRY_PC, bus_no, POSTCODE_SEVERITY_INFO);

	visorchannel = visorchannel_create(cmd->create_bus.channel_addr,
					   cmd->create_bus.channel_bytes,
					   GFP_KERNEL,
					   cmd->create_bus.bus_data_type_uuid);

	if (!visorchannel) {
		POSTCODE_LINUX_3(BUS_CREATE_FAILURE_PC, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
		kfree(bus_info);
		bus_info = NULL;
		goto out_bus_epilog;
	}
	bus_info->visorchannel = visorchannel;
	if (uuid_le_cmp(cmd->create_bus.bus_inst_uuid, spar_siovm_uuid) == 0)
		save_crash_message(inmsg, CRASH_BUS);

	POSTCODE_LINUX_3(BUS_CREATE_EXIT_PC, bus_no, POSTCODE_SEVERITY_INFO);

out_bus_epilog:
	bus_epilog(bus_info, CONTROLVM_BUS_CREATE, &inmsg->hdr,
		   rc, inmsg->hdr.flags.response_expected == 1);
}

static void
bus_destroy(struct controlvm_message *inmsg)
{
	struct controlvm_message_packet *cmd = &inmsg->cmd;
	u32 bus_no = cmd->destroy_bus.bus_no;
	struct visor_device *bus_info;
	int rc = CONTROLVM_RESP_SUCCESS;

	bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
	if (!bus_info)
		rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
	else if (bus_info->state.created == 0)
		rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;

	bus_epilog(bus_info, CONTROLVM_BUS_DESTROY, &inmsg->hdr,
		   rc, inmsg->hdr.flags.response_expected == 1);

	/* bus_info is freed as part of the busdevice_release function */
}

static void
bus_configure(struct controlvm_message *inmsg,
	      struct parser_context *parser_ctx)
{
	struct controlvm_message_packet *cmd = &inmsg->cmd;
	u32 bus_no;
	struct visor_device *bus_info;
	int rc = CONTROLVM_RESP_SUCCESS;

	bus_no = cmd->configure_bus.bus_no;
	POSTCODE_LINUX_3(BUS_CONFIGURE_ENTRY_PC, bus_no,
			 POSTCODE_SEVERITY_INFO);

	bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
	if (!bus_info) {
		POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
	} else if (bus_info->state.created == 0) {
		POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
	} else if (bus_info->pending_msg_hdr) {
		POSTCODE_LINUX_3(BUS_CONFIGURE_FAILURE_PC, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_MESSAGE_ID_INVALID_FOR_CLIENT;
	} else {
		visorchannel_set_clientpartition
			(bus_info->visorchannel,
			 cmd->configure_bus.guest_handle);
		bus_info->partition_uuid = parser_id_get(parser_ctx);
		parser_param_start(parser_ctx, PARSERSTRING_NAME);
		bus_info->name = parser_string_get(parser_ctx);

		POSTCODE_LINUX_3(BUS_CONFIGURE_EXIT_PC, bus_no,
				 POSTCODE_SEVERITY_INFO);
	}
	bus_epilog(bus_info, CONTROLVM_BUS_CONFIGURE, &inmsg->hdr,
		   rc, inmsg->hdr.flags.response_expected == 1);
}

static void
my_device_create(struct controlvm_message *inmsg)
{
	struct controlvm_message_packet *cmd = &inmsg->cmd;
	u32 bus_no = cmd->create_device.bus_no;
	u32 dev_no = cmd->create_device.dev_no;
	struct visor_device *dev_info = NULL;
	struct visor_device *bus_info;
	struct visorchannel *visorchannel;
	int rc = CONTROLVM_RESP_SUCCESS;

	bus_info = visorbus_get_device_by_id(bus_no, BUS_ROOT_DEVICE, NULL);
	if (!bus_info) {
		POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
		goto out_respond;
	}

	if (bus_info->state.created == 0) {
		POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_BUS_INVALID;
		goto out_respond;
	}

	dev_info = visorbus_get_device_by_id(bus_no, dev_no, NULL);
	if (dev_info && (dev_info->state.created == 1)) {
		POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;
		goto out_respond;
	}

	dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
	if (!dev_info) {
		POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
		goto out_respond;
	}

	dev_info->chipset_bus_no = bus_no;
	dev_info->chipset_dev_no = dev_no;
	dev_info->inst = cmd->create_device.dev_inst_uuid;

	/* not sure where the best place to set the 'parent' */
	dev_info->device.parent = &bus_info->device;

	POSTCODE_LINUX_4(DEVICE_CREATE_ENTRY_PC, dev_no, bus_no,
			 POSTCODE_SEVERITY_INFO);

	visorchannel =
	       visorchannel_create_with_lock(cmd->create_device.channel_addr,
					     cmd->create_device.channel_bytes,
					     GFP_KERNEL,
					     cmd->create_device.data_type_uuid);

	if (!visorchannel) {
		POSTCODE_LINUX_4(DEVICE_CREATE_FAILURE_PC, dev_no, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_KMALLOC_FAILED;
		kfree(dev_info);
		dev_info = NULL;
		goto out_respond;
	}
	dev_info->visorchannel = visorchannel;
	dev_info->channel_type_guid = cmd->create_device.data_type_uuid;
	if (uuid_le_cmp(cmd->create_device.data_type_uuid,
			spar_vhba_channel_protocol_uuid) == 0)
		save_crash_message(inmsg, CRASH_DEV);

	POSTCODE_LINUX_4(DEVICE_CREATE_EXIT_PC, dev_no, bus_no,
			 POSTCODE_SEVERITY_INFO);
out_respond:
	device_epilog(dev_info, segment_state_running,
		      CONTROLVM_DEVICE_CREATE, &inmsg->hdr, rc,
		      inmsg->hdr.flags.response_expected == 1, 1);
}

static void
my_device_changestate(struct controlvm_message *inmsg)
{
	struct controlvm_message_packet *cmd = &inmsg->cmd;
	u32 bus_no = cmd->device_change_state.bus_no;
	u32 dev_no = cmd->device_change_state.dev_no;
	struct spar_segment_state state = cmd->device_change_state.state;
	struct visor_device *dev_info;
	int rc = CONTROLVM_RESP_SUCCESS;

	dev_info = visorbus_get_device_by_id(bus_no, dev_no, NULL);
	if (!dev_info) {
		POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
	} else if (dev_info->state.created == 0) {
		POSTCODE_LINUX_4(DEVICE_CHANGESTATE_FAILURE_PC, dev_no, bus_no,
				 POSTCODE_SEVERITY_ERR);
		rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
	}
	if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
		device_epilog(dev_info, state,
			      CONTROLVM_DEVICE_CHANGESTATE, &inmsg->hdr, rc,
			      inmsg->hdr.flags.response_expected == 1, 1);
}

static void
my_device_destroy(struct controlvm_message *inmsg)
{
	struct controlvm_message_packet *cmd = &inmsg->cmd;
	u32 bus_no = cmd->destroy_device.bus_no;
	u32 dev_no = cmd->destroy_device.dev_no;
	struct visor_device *dev_info;
	int rc = CONTROLVM_RESP_SUCCESS;

	dev_info = visorbus_get_device_by_id(bus_no, dev_no, NULL);
	if (!dev_info)
		rc = -CONTROLVM_RESP_ERROR_DEVICE_INVALID;
	else if (dev_info->state.created == 0)
		rc = -CONTROLVM_RESP_ERROR_ALREADY_DONE;

	if ((rc >= CONTROLVM_RESP_SUCCESS) && dev_info)
		device_epilog(dev_info, segment_state_running,
			      CONTROLVM_DEVICE_DESTROY, &inmsg->hdr, rc,
			      inmsg->hdr.flags.response_expected == 1, 1);
}

/**
 * initialize_controlvm_payload_info() - init controlvm_payload_info struct
 * @phys_addr: the physical address of controlvm channel
 * @offset:    the offset to payload
 * @bytes:     the size of the payload in bytes
 * @info:      the returning valid struct
 *
 * When provided with the physical address of the controlvm channel
 * (phys_addr), the offset to the payload area we need to manage
 * (offset), and the size of this payload area (bytes), fills in the
 * controlvm_payload_info struct.
 *
 * Return: CONTROLVM_RESP_SUCCESS for success or a negative for failure
 */
static int
initialize_controlvm_payload_info(u64 phys_addr, u64 offset, u32 bytes,
				  struct visor_controlvm_payload_info *info)
{
	u8 *payload = NULL;

	if (!info)
		return -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;

	memset(info, 0, sizeof(struct visor_controlvm_payload_info));
	if ((offset == 0) || (bytes == 0))
		return -CONTROLVM_RESP_ERROR_PAYLOAD_INVALID;

	payload = memremap(phys_addr + offset, bytes, MEMREMAP_WB);
	if (!payload)
		return -CONTROLVM_RESP_ERROR_IOREMAP_FAILED;

	info->offset = offset;
	info->bytes = bytes;
	info->ptr = payload;

	return CONTROLVM_RESP_SUCCESS;
}

static void
destroy_controlvm_payload_info(struct visor_controlvm_payload_info *info)
{
	if (info->ptr) {
		memunmap(info->ptr);
		info->ptr = NULL;
	}
	memset(info, 0, sizeof(struct visor_controlvm_payload_info));
}

static void
initialize_controlvm_payload(void)
{
	u64 phys_addr = visorchannel_get_physaddr(controlvm_channel);
	u64 payload_offset = 0;
	u32 payload_bytes = 0;

	if (visorchannel_read(controlvm_channel,
			      offsetof(struct spar_controlvm_channel_protocol,
				       request_payload_offset),
			      &payload_offset, sizeof(payload_offset)) < 0) {
		POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}
	if (visorchannel_read(controlvm_channel,
			      offsetof(struct spar_controlvm_channel_protocol,
				       request_payload_bytes),
			      &payload_bytes, sizeof(payload_bytes)) < 0) {
		POSTCODE_LINUX_2(CONTROLVM_INIT_FAILURE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}
	initialize_controlvm_payload_info(phys_addr,
					  payload_offset, payload_bytes,
					  &controlvm_payload_info);
}

/**
 * visorchipset_chipset_ready() - sends chipset_ready action
 *
 * Send ACTION=online for DEVPATH=/sys/devices/platform/visorchipset.
 *
 * Return: CONTROLVM_RESP_SUCCESS
 */
static int
visorchipset_chipset_ready(void)
{
	kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_ONLINE);
	return CONTROLVM_RESP_SUCCESS;
}

static int
visorchipset_chipset_selftest(void)
{
	char env_selftest[20];
	char *envp[] = { env_selftest, NULL };

	sprintf(env_selftest, "SPARSP_SELFTEST=%d", 1);
	kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
			   envp);
	return CONTROLVM_RESP_SUCCESS;
}

/**
 * visorchipset_chipset_notready() - sends chipset_notready action
 *
 * Send ACTION=offline for DEVPATH=/sys/devices/platform/visorchipset.
 *
 * Return: CONTROLVM_RESP_SUCCESS
 */
static int
visorchipset_chipset_notready(void)
{
	kobject_uevent(&visorchipset_platform_device.dev.kobj, KOBJ_OFFLINE);
	return CONTROLVM_RESP_SUCCESS;
}

static void
chipset_ready(struct controlvm_message_header *msg_hdr)
{
	int rc = visorchipset_chipset_ready();

	if (rc != CONTROLVM_RESP_SUCCESS)
		rc = -rc;
	if (msg_hdr->flags.response_expected)
		controlvm_respond(msg_hdr, rc);
}

static void
chipset_selftest(struct controlvm_message_header *msg_hdr)
{
	int rc = visorchipset_chipset_selftest();

	if (rc != CONTROLVM_RESP_SUCCESS)
		rc = -rc;
	if (msg_hdr->flags.response_expected)
		controlvm_respond(msg_hdr, rc);
}

static void
chipset_notready(struct controlvm_message_header *msg_hdr)
{
	int rc = visorchipset_chipset_notready();

	if (rc != CONTROLVM_RESP_SUCCESS)
		rc = -rc;
	if (msg_hdr->flags.response_expected)
		controlvm_respond(msg_hdr, rc);
}

/*
 * The general parahotplug flow works as follows. The visorchipset
 * driver receives a DEVICE_CHANGESTATE message from Command
 * specifying a physical device to enable or disable. The CONTROLVM
 * message handler calls parahotplug_process_message, which then adds
 * the message to a global list and kicks off a udev event which
 * causes a user level script to enable or disable the specified
 * device. The udev script then writes to
 * /proc/visorchipset/parahotplug, which causes parahotplug_proc_write
 * to get called, at which point the appropriate CONTROLVM message is
 * retrieved from the list and responded to.
 */

#define PARAHOTPLUG_TIMEOUT_MS 2000

/**
 * parahotplug_next_id() - generate unique int to match an outstanding CONTROLVM
 *                         message with a udev script /proc response
 *
 * Return: a unique integer value
 */
static int
parahotplug_next_id(void)
{
	static atomic_t id = ATOMIC_INIT(0);

	return atomic_inc_return(&id);
}

/**
 * parahotplug_next_expiration() - returns the time (in jiffies) when a
 *                                 CONTROLVM message on the list should expire
 *                                 -- PARAHOTPLUG_TIMEOUT_MS in the future
 *
 * Return: expected expiration time (in jiffies)
 */
static unsigned long
parahotplug_next_expiration(void)
{
	return jiffies + msecs_to_jiffies(PARAHOTPLUG_TIMEOUT_MS);
}

/**
 * parahotplug_request_create() - create a parahotplug_request, which is
 *                                basically a wrapper for a CONTROLVM_MESSAGE
 *                                that we can stick on a list
 * @msg: the message to insert in the request
 *
 * Return: the request containing the provided message
 */
static struct parahotplug_request *
parahotplug_request_create(struct controlvm_message *msg)
{
	struct parahotplug_request *req;

	req = kmalloc(sizeof(*req), GFP_KERNEL | __GFP_NORETRY);
	if (!req)
		return NULL;

	req->id = parahotplug_next_id();
	req->expiration = parahotplug_next_expiration();
	req->msg = *msg;

	return req;
}

/**
 * parahotplug_request_destroy() - free a parahotplug_request
 * @req: the request to deallocate
 */
static void
parahotplug_request_destroy(struct parahotplug_request *req)
{
	kfree(req);
}

/**
 * parahotplug_request_kickoff() - initiate parahotplug request
 * @req: the request to initiate
 *
 * Cause uevent to run the user level script to do the disable/enable specified
 * in the parahotplug_request.
 */
static void
parahotplug_request_kickoff(struct parahotplug_request *req)
{
	struct controlvm_message_packet *cmd = &req->msg.cmd;
	char env_cmd[40], env_id[40], env_state[40], env_bus[40], env_dev[40],
	    env_func[40];
	char *envp[] = {
		env_cmd, env_id, env_state, env_bus, env_dev, env_func, NULL
	};

	sprintf(env_cmd, "SPAR_PARAHOTPLUG=1");
	sprintf(env_id, "SPAR_PARAHOTPLUG_ID=%d", req->id);
	sprintf(env_state, "SPAR_PARAHOTPLUG_STATE=%d",
		cmd->device_change_state.state.active);
	sprintf(env_bus, "SPAR_PARAHOTPLUG_BUS=%d",
		cmd->device_change_state.bus_no);
	sprintf(env_dev, "SPAR_PARAHOTPLUG_DEVICE=%d",
		cmd->device_change_state.dev_no >> 3);
	sprintf(env_func, "SPAR_PARAHOTPLUG_FUNCTION=%d",
		cmd->device_change_state.dev_no & 0x7);

	kobject_uevent_env(&visorchipset_platform_device.dev.kobj, KOBJ_CHANGE,
			   envp);
}

/**
 * parahotplug_request_complete() - mark request as complete
 * @id:     the id of the request
 * @active: indicates whether the request is assigned to active partition
 *
 * Called from the /proc handler, which means the user script has
 * finished the enable/disable. Find the matching identifier, and
 * respond to the CONTROLVM message with success.
 *
 * Return: 0 on success or -EINVAL on failure
 */
static int
parahotplug_request_complete(int id, u16 active)
{
	struct list_head *pos;
	struct list_head *tmp;

	spin_lock(&parahotplug_request_list_lock);

	/* Look for a request matching "id". */
	list_for_each_safe(pos, tmp, &parahotplug_request_list) {
		struct parahotplug_request *req =
		    list_entry(pos, struct parahotplug_request, list);
		if (req->id == id) {
			/*
			 * Found a match. Remove it from the list and
			 * respond.
			 */
			list_del(pos);
			spin_unlock(&parahotplug_request_list_lock);
			req->msg.cmd.device_change_state.state.active = active;
			if (req->msg.hdr.flags.response_expected)
				controlvm_respond_physdev_changestate(
					&req->msg.hdr, CONTROLVM_RESP_SUCCESS,
					req->msg.cmd.device_change_state.state);
			parahotplug_request_destroy(req);
			return 0;
		}
	}

	spin_unlock(&parahotplug_request_list_lock);
	return -EINVAL;
}

/**
 * parahotplug_process_message() - enables or disables a PCI device by kicking
 *                                 off a udev script
 * @inmsg: the message indicating whether to enable or disable
 */
static void
parahotplug_process_message(struct controlvm_message *inmsg)
{
	struct parahotplug_request *req;

	req = parahotplug_request_create(inmsg);

	if (!req)
		return;

	if (inmsg->cmd.device_change_state.state.active) {
		/*
		 * For enable messages, just respond with success
		 * right away. This is a bit of a hack, but there are
		 * issues with the early enable messages we get (with
		 * either the udev script not detecting that the device
		 * is up, or not getting called at all). Fortunately
		 * the messages that get lost don't matter anyway, as
		 *
		 * devices are automatically enabled at
		 * initialization.
		*/
		parahotplug_request_kickoff(req);
		controlvm_respond_physdev_changestate
			(&inmsg->hdr,
			 CONTROLVM_RESP_SUCCESS,
			 inmsg->cmd.device_change_state.state);
		parahotplug_request_destroy(req);
	} else {
		/*
		 * For disable messages, add the request to the
		 * request list before kicking off the udev script. It
		 * won't get responded to until the script has
		 * indicated it's done.
		 */
		spin_lock(&parahotplug_request_list_lock);
		list_add_tail(&req->list, &parahotplug_request_list);
		spin_unlock(&parahotplug_request_list_lock);

		parahotplug_request_kickoff(req);
	}
}

static inline unsigned int
issue_vmcall_io_controlvm_addr(u64 *control_addr, u32 *control_bytes)
{
	struct vmcall_io_controlvm_addr_params params;
	int result = VMCALL_SUCCESS;
	u64 physaddr;

	physaddr = virt_to_phys(&params);
	ISSUE_IO_VMCALL(VMCALL_IO_CONTROLVM_ADDR, physaddr, result);
	if (VMCALL_SUCCESSFUL(result)) {
		*control_addr = params.address;
		*control_bytes = params.channel_bytes;
	}
	return result;
}

static u64 controlvm_get_channel_address(void)
{
	u64 addr = 0;
	u32 size = 0;

	if (!VMCALL_SUCCESSFUL(issue_vmcall_io_controlvm_addr(&addr, &size)))
		return 0;

	return addr;
}

static void
setup_crash_devices_work_queue(struct work_struct *work)
{
	struct controlvm_message local_crash_bus_msg;
	struct controlvm_message local_crash_dev_msg;
	struct controlvm_message msg;
	u32 local_crash_msg_offset;
	u16 local_crash_msg_count;

	POSTCODE_LINUX_2(CRASH_DEV_ENTRY_PC, POSTCODE_SEVERITY_INFO);

	/* send init chipset msg */
	msg.hdr.id = CONTROLVM_CHIPSET_INIT;
	msg.cmd.init_chipset.bus_count = 23;
	msg.cmd.init_chipset.switch_count = 0;

	chipset_init(&msg);

	/* get saved message count */
	if (visorchannel_read(controlvm_channel,
			      offsetof(struct spar_controlvm_channel_protocol,
				       saved_crash_message_count),
			      &local_crash_msg_count, sizeof(u16)) < 0) {
		POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}

	if (local_crash_msg_count != CONTROLVM_CRASHMSG_MAX) {
		POSTCODE_LINUX_3(CRASH_DEV_COUNT_FAILURE_PC,
				 local_crash_msg_count,
				 POSTCODE_SEVERITY_ERR);
		return;
	}

	/* get saved crash message offset */
	if (visorchannel_read(controlvm_channel,
			      offsetof(struct spar_controlvm_channel_protocol,
				       saved_crash_message_offset),
			      &local_crash_msg_offset, sizeof(u32)) < 0) {
		POSTCODE_LINUX_2(CRASH_DEV_CTRL_RD_FAILURE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}

	/* read create device message for storage bus offset */
	if (visorchannel_read(controlvm_channel,
			      local_crash_msg_offset,
			      &local_crash_bus_msg,
			      sizeof(struct controlvm_message)) < 0) {
		POSTCODE_LINUX_2(CRASH_DEV_RD_BUS_FAIULRE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}

	/* read create device message for storage device */
	if (visorchannel_read(controlvm_channel,
			      local_crash_msg_offset +
			      sizeof(struct controlvm_message),
			      &local_crash_dev_msg,
			      sizeof(struct controlvm_message)) < 0) {
		POSTCODE_LINUX_2(CRASH_DEV_RD_DEV_FAIULRE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}

	/* reuse IOVM create bus message */
	if (local_crash_bus_msg.cmd.create_bus.channel_addr) {
		bus_create(&local_crash_bus_msg);
	} else {
		POSTCODE_LINUX_2(CRASH_DEV_BUS_NULL_FAILURE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}

	/* reuse create device message for storage device */
	if (local_crash_dev_msg.cmd.create_device.channel_addr) {
		my_device_create(&local_crash_dev_msg);
	} else {
		POSTCODE_LINUX_2(CRASH_DEV_DEV_NULL_FAILURE_PC,
				 POSTCODE_SEVERITY_ERR);
		return;
	}
	POSTCODE_LINUX_2(CRASH_DEV_EXIT_PC, POSTCODE_SEVERITY_INFO);
}

void
bus_create_response(struct visor_device *bus_info, int response)
{
	if (response >= 0)
		bus_info->state.created = 1;

	bus_responder(CONTROLVM_BUS_CREATE, bus_info->pending_msg_hdr,
		      response);

	kfree(bus_info->pending_msg_hdr);
	bus_info->pending_msg_hdr = NULL;
}

void
bus_destroy_response(struct visor_device *bus_info, int response)
{
	bus_responder(CONTROLVM_BUS_DESTROY, bus_info->pending_msg_hdr,
		      response);

	kfree(bus_info->pending_msg_hdr);
	bus_info->pending_msg_hdr = NULL;
}

void
device_create_response(struct visor_device *dev_info, int response)
{
	if (response >= 0)
		dev_info->state.created = 1;

	device_responder(CONTROLVM_DEVICE_CREATE, dev_info->pending_msg_hdr,
			 response);

	kfree(dev_info->pending_msg_hdr);
	dev_info->pending_msg_hdr = NULL;
}

void
device_destroy_response(struct visor_device *dev_info, int response)
{
	device_responder(CONTROLVM_DEVICE_DESTROY, dev_info->pending_msg_hdr,
			 response);

	kfree(dev_info->pending_msg_hdr);
	dev_info->pending_msg_hdr = NULL;
}

void
device_pause_response(struct visor_device *dev_info,
		      int response)
{
	device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
				     dev_info, response,
				     segment_state_standby);

	kfree(dev_info->pending_msg_hdr);
	dev_info->pending_msg_hdr = NULL;
}

void
device_resume_response(struct visor_device *dev_info, int response)
{
	device_changestate_responder(CONTROLVM_DEVICE_CHANGESTATE,
				     dev_info, response,
				     segment_state_running);

	kfree(dev_info->pending_msg_hdr);
	dev_info->pending_msg_hdr = NULL;
}

/**
 * devicedisabled_store() - disables the hotplug device
 * @dev:   sysfs interface variable not utilized in this function
 * @attr:  sysfs interface variable not utilized in this function
 * @buf:   buffer containing the device id
 * @count: the size of the buffer
 *
 * The parahotplug/devicedisabled interface gets called by our support script
 * when an SR-IOV device has been shut down. The ID is passed to the script
 * and then passed back when the device has been removed.
 *
 * Return: the size of the buffer for success or negative for error
 */
static ssize_t devicedisabled_store(struct device *dev,
				    struct device_attribute *attr,
				    const char *buf, size_t count)
{
	unsigned int id;
	int err;

	if (kstrtouint(buf, 10, &id))
		return -EINVAL;

	err = parahotplug_request_complete(id, 0);
	if (err < 0)
		return err;
	return count;
}

/**
 * deviceenabled_store() - enables the hotplug device
 * @dev:   sysfs interface variable not utilized in this function
 * @attr:  sysfs interface variable not utilized in this function
 * @buf:   buffer containing the device id
 * @count: the size of the buffer
 *
 * The parahotplug/deviceenabled interface gets called by our support script
 * when an SR-IOV device has been recovered. The ID is passed to the script
 * and then passed back when the device has been brought back up.
 *
 * Return: the size of the buffer for success or negative for error
 */
static ssize_t deviceenabled_store(struct device *dev,
				   struct device_attribute *attr,
				   const char *buf, size_t count)
{
	unsigned int id;

	if (kstrtouint(buf, 10, &id))
		return -EINVAL;

	parahotplug_request_complete(id, 1);
	return count;
}

static int
visorchipset_mmap(struct file *file, struct vm_area_struct *vma)
{
	unsigned long physaddr = 0;
	unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
	u64 addr = 0;

	/* sv_enable_dfp(); */
	if (offset & (PAGE_SIZE - 1))
		return -ENXIO;	/* need aligned offsets */

	switch (offset) {
	case VISORCHIPSET_MMAP_CONTROLCHANOFFSET:
		vma->vm_flags |= VM_IO;
		if (!*file_controlvm_channel)
			return -ENXIO;

		visorchannel_read
			(*file_controlvm_channel,
			 offsetof(struct spar_controlvm_channel_protocol,
				  gp_control_channel),
			 &addr, sizeof(addr));
		if (!addr)
			return -ENXIO;

		physaddr = (unsigned long)addr;
		if (remap_pfn_range(vma, vma->vm_start,
				    physaddr >> PAGE_SHIFT,
				    vma->vm_end - vma->vm_start,
				    /*pgprot_noncached */
				    (vma->vm_page_prot))) {
			return -EAGAIN;
		}
		break;
	default:
		return -ENXIO;
	}
	return 0;
}

static inline s64 issue_vmcall_query_guest_virtual_time_offset(void)
{
	u64 result = VMCALL_SUCCESS;
	u64 physaddr = 0;

	ISSUE_IO_VMCALL(VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET, physaddr,
			result);
	return result;
}

static inline int issue_vmcall_update_physical_time(u64 adjustment)
{
	int result = VMCALL_SUCCESS;

	ISSUE_IO_VMCALL(VMCALL_UPDATE_PHYSICAL_TIME, adjustment, result);
	return result;
}

static long visorchipset_ioctl(struct file *file, unsigned int cmd,
			       unsigned long arg)
{
	u64 adjustment;
	s64 vrtc_offset;

	switch (cmd) {
	case VMCALL_QUERY_GUEST_VIRTUAL_TIME_OFFSET:
		/* get the physical rtc offset */
		vrtc_offset = issue_vmcall_query_guest_virtual_time_offset();
		if (copy_to_user((void __user *)arg, &vrtc_offset,
				 sizeof(vrtc_offset))) {
			return -EFAULT;
		}
		return 0;
	case VMCALL_UPDATE_PHYSICAL_TIME:
		if (copy_from_user(&adjustment, (void __user *)arg,
				   sizeof(adjustment))) {
			return -EFAULT;
		}
		return issue_vmcall_update_physical_time(adjustment);
	default:
		return -EFAULT;
	}
}

static const struct file_operations visorchipset_fops = {
	.owner = THIS_MODULE,
	.open = visorchipset_open,
	.read = NULL,
	.write = NULL,
	.unlocked_ioctl = visorchipset_ioctl,
	.release = visorchipset_release,
	.mmap = visorchipset_mmap,
};

static int
visorchipset_file_init(dev_t major_dev, struct visorchannel **controlvm_channel)
{
	int rc = 0;

	file_controlvm_channel = controlvm_channel;
	cdev_init(&file_cdev, &visorchipset_fops);
	file_cdev.owner = THIS_MODULE;
	if (MAJOR(major_dev) == 0) {
		rc = alloc_chrdev_region(&major_dev, 0, 1, "visorchipset");
		/* dynamic major device number registration required */
		if (rc < 0)
			return rc;
	} else {
		/* static major device number registration required */
		rc = register_chrdev_region(major_dev, 1, "visorchipset");
		if (rc < 0)
			return rc;
	}
	rc = cdev_add(&file_cdev, MKDEV(MAJOR(major_dev), 0), 1);
	if (rc < 0) {
		unregister_chrdev_region(major_dev, 1);
		return rc;
	}
	return 0;
}

static void
visorchipset_file_cleanup(dev_t major_dev)
{
	if (file_cdev.ops)