Merge remote-tracking branch 'staging/staging-next'
[deliverable/linux.git] / drivers / staging / unisys / visorbus / vbuschannel.h
CommitLineData
6f14cc18 1/* Copyright (C) 2010 - 2015 UNISYS CORPORATION
12e364b9
KC
2 * All rights reserved.
3 *
6f14cc18
BR
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
12e364b9
KC
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for more
12 * details.
13 */
14
15#ifndef __VBUSCHANNEL_H__
16#define __VBUSCHANNEL_H__
17
18/* The vbus channel is the channel area provided via the BUS_CREATE controlvm
19 * message for each virtual bus. This channel area is provided to both server
20 * and client ends of the bus. The channel header area is initialized by
21 * the server, and the remaining information is filled in by the client.
22 * We currently use this for the client to provide various information about
23 * the client devices and client drivers for the server end to see.
24 */
90addb02 25#include <linux/uuid.h>
12e364b9
KC
26#include "channel.h"
27
28/* {193b331b-c58f-11da-95a9-00e08161165f} */
8bde4043 29#define SPAR_VBUS_CHANNEL_PROTOCOL_UUID \
90addb02
BR
30 UUID_LE(0x193b331b, 0xc58f, 0x11da, \
31 0x95, 0xa9, 0x0, 0xe0, 0x81, 0x61, 0x16, 0x5f)
8bde4043
BR
32static const uuid_le spar_vbus_channel_protocol_uuid =
33 SPAR_VBUS_CHANNEL_PROTOCOL_UUID;
12e364b9 34
8bde4043 35#define SPAR_VBUS_CHANNEL_PROTOCOL_SIGNATURE ULTRA_CHANNEL_PROTOCOL_SIGNATURE
12e364b9
KC
36
37/* Must increment this whenever you insert or delete fields within this channel
e3f8f77c
EA
38 * struct. Also increment whenever you change the meaning of fields within this
39 * channel struct so as to break pre-existing software. Note that you can
40 * usually add fields to the END of the channel struct withOUT needing to
41 * increment this.
42 */
8bde4043 43#define SPAR_VBUS_CHANNEL_PROTOCOL_VERSIONID 1
12e364b9 44
1aa46ce4
BR
45#define SPAR_VBUS_CHANNEL_OK_CLIENT(ch) \
46 spar_check_channel_client(ch, \
a02fd66e
BR
47 spar_vbus_channel_protocol_uuid, \
48 "vbus", \
49 sizeof(struct spar_vbus_channel_protocol),\
50 SPAR_VBUS_CHANNEL_PROTOCOL_VERSIONID, \
51 SPAR_VBUS_CHANNEL_PROTOCOL_SIGNATURE)
12e364b9 52
9b8a8a96 53#define SPAR_VBUS_CHANNEL_OK_SERVER(actual_bytes) \
8bde4043
BR
54 (spar_check_channel_server(spar_vbus_channel_protocol_uuid, \
55 "vbus", \
3703987c 56 sizeof(struct spar_vbus_channel_protocol),\
9b8a8a96 57 actual_bytes))
10c5ef69 58
12e364b9 59#pragma pack(push, 1) /* both GCC and VC now allow this pragma */
b6d0fa15
DK
60
61/*
62 * An array of this struct is present in the channel area for each vbus.
63 * (See vbuschannel.h.)
64 * It is filled in by the client side to provide info about the device
65 * and driver from the client's perspective.
66 */
67struct ultra_vbus_deviceinfo {
68 u8 devtype[16]; /* short string identifying the device type */
69 u8 drvname[16]; /* driver .sys file name */
70 u8 infostrs[96]; /* sequence of tab-delimited id strings: */
71 /* <DRIVER_REV> <DRIVER_VERTAG> <DRIVER_COMPILETIME> */
72 u8 reserved[128]; /* pad size to 256 bytes */
73};
74
75/**
76 * vbuschannel_sanitize_buffer() - remove non-printable chars from buffer
77 * @p: destination buffer where chars are written to
78 * @remain: number of bytes that can be written starting at #p
79 * @src: pointer to source buffer
80 * @srcmax: number of valid characters at #src
81 *
82 * Reads chars from the buffer at @src for @srcmax bytes, and writes to
83 * the buffer at @p, which is @remain bytes long, ensuring never to
84 * overflow the buffer at @p, using the following rules:
85 * - printable characters are simply copied from the buffer at @src to the
86 * buffer at @p
87 * - intervening streaks of non-printable characters in the buffer at @src
88 * are replaced with a single space in the buffer at @p
89 * Note that we pay no attention to '\0'-termination.
90 *
91 * Pass @p == NULL and @remain == 0 for this special behavior -- In this
92 * case, we simply return the number of bytes that WOULD HAVE been written
93 * to a buffer at @p, had it been infinitely big.
94 *
95 * Return: the number of bytes written to @p (or WOULD HAVE been written to
96 * @p, as described in the previous paragraph)
97 */
98static inline int
99vbuschannel_sanitize_buffer(char *p, int remain, char *src, int srcmax)
100{
101 int chars = 0;
102 int nonprintable_streak = 0;
103
104 while (srcmax > 0) {
105 if ((*src >= ' ') && (*src < 0x7f)) {
106 if (nonprintable_streak) {
107 if (remain > 0) {
108 *p = ' ';
109 p++;
110 remain--;
111 chars++;
112 } else if (!p) {
113 chars++;
114 }
115 nonprintable_streak = 0;
116 }
117 if (remain > 0) {
118 *p = *src;
119 p++;
120 remain--;
121 chars++;
122 } else if (!p) {
123 chars++;
124 }
125 } else {
126 nonprintable_streak = 1;
127 }
128 src++;
129 srcmax--;
130 }
131 return chars;
132}
133
134#define VBUSCHANNEL_ADDACHAR(ch, p, remain, chars) \
135 do { \
136 if (remain <= 0) \
137 break; \
138 *p = ch; \
139 p++; chars++; remain--; \
140 } while (0)
141
142/**
143 * vbuschannel_itoa() - convert non-negative int to string
144 * @p: destination string
145 * @remain: max number of bytes that can be written to @p
146 * @num: input int to convert
147 *
148 * Converts the non-negative value at @num to an ascii decimal string
149 * at @p, writing at most @remain bytes. Note there is NO '\0' termination
150 * written to @p.
151 *
152 * Return: number of bytes written to @p
153 *
154 */
155static inline int
156vbuschannel_itoa(char *p, int remain, int num)
157{
158 int digits = 0;
159 char s[32];
160 int i;
161
162 if (num == 0) {
163 /* '0' is a special case */
164 if (remain <= 0)
165 return 0;
166 *p = '0';
167 return 1;
168 }
169 /* form a backwards decimal ascii string in <s> */
170 while (num > 0) {
171 if (digits >= (int)sizeof(s))
172 return 0;
173 s[digits++] = (num % 10) + '0';
174 num = num / 10;
175 }
176 if (remain < digits) {
177 /* not enough room left at <p> to hold number, so fill with
178 * '?'
179 */
180 for (i = 0; i < remain; i++, p++)
181 *p = '?';
182 return remain;
183 }
184 /* plug in the decimal ascii string representing the number, by */
185 /* reversing the string we just built in <s> */
186 i = digits;
187 while (i > 0) {
188 i--;
189 *p = s[i];
190 p++;
191 }
192 return digits;
193}
194
195/**
196 * vbuschannel_devinfo_to_string() - format a struct ultra_vbus_deviceinfo
197 * to a printable string
198 * @devinfo: the struct ultra_vbus_deviceinfo to format
199 * @p: destination string area
200 * @remain: size of destination string area in bytes
201 * @devix: the device index to be included in the output data, or -1 if no
202 * device index is to be included
203 *
204 * Reads @devInfo, and converts its contents to a printable string at @p,
205 * writing at most @remain bytes. Note there is NO '\0' termination
206 * written to @p.
207 *
208 * Return: number of bytes written to @p
209 */
210static inline int
211vbuschannel_devinfo_to_string(struct ultra_vbus_deviceinfo *devinfo,
212 char *p, int remain, int devix)
213{
214 char *psrc;
215 int nsrc, x, i, pad;
216 int chars = 0;
217
218 psrc = &devinfo->devtype[0];
219 nsrc = sizeof(devinfo->devtype);
220 if (vbuschannel_sanitize_buffer(NULL, 0, psrc, nsrc) <= 0)
221 return 0;
222
223 /* emit device index */
224 if (devix >= 0) {
225 VBUSCHANNEL_ADDACHAR('[', p, remain, chars);
226 x = vbuschannel_itoa(p, remain, devix);
227 p += x;
228 remain -= x;
229 chars += x;
230 VBUSCHANNEL_ADDACHAR(']', p, remain, chars);
231 } else {
232 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
233 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
234 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
235 }
236
237 /* emit device type */
238 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
239 p += x;
240 remain -= x;
241 chars += x;
242 pad = 15 - x; /* pad device type to be exactly 15 chars */
243 for (i = 0; i < pad; i++)
244 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
245 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
246
247 /* emit driver name */
248 psrc = &devinfo->drvname[0];
249 nsrc = sizeof(devinfo->drvname);
250 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
251 p += x;
252 remain -= x;
253 chars += x;
254 pad = 15 - x; /* pad driver name to be exactly 15 chars */
255 for (i = 0; i < pad; i++)
256 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
257 VBUSCHANNEL_ADDACHAR(' ', p, remain, chars);
258
259 /* emit strings */
260 psrc = &devinfo->infostrs[0];
261 nsrc = sizeof(devinfo->infostrs);
262 x = vbuschannel_sanitize_buffer(p, remain, psrc, nsrc);
263 p += x;
264 remain -= x;
265 chars += x;
266 VBUSCHANNEL_ADDACHAR('\n', p, remain, chars);
267
268 return chars;
269}
270
1cb3b9de
BR
271struct spar_vbus_headerinfo {
272 u32 struct_bytes; /* size of this struct in bytes */
273 u32 device_info_struct_bytes; /* sizeof(ULTRA_VBUS_DEVICEINFO) */
274 u32 dev_info_count; /* num of items in DevInfo member */
12e364b9 275 /* (this is the allocated size) */
1cb3b9de 276 u32 chp_info_offset; /* byte offset from beginning of this struct */
682adec4 277 /* to the ChpInfo struct (below) */
1cb3b9de 278 u32 bus_info_offset; /* byte offset from beginning of this struct */
682adec4 279 /* to the BusInfo struct (below) */
1cb3b9de 280 u32 dev_info_offset; /* byte offset from beginning of this struct */
682adec4 281 /* to the DevInfo array (below) */
c242233e 282 u8 reserved[104];
1cb3b9de 283};
12e364b9 284
a02fd66e
BR
285struct spar_vbus_channel_protocol {
286 struct channel_header channel_header; /* initialized by server */
287 struct spar_vbus_headerinfo hdr_info; /* initialized by server */
12e364b9 288 /* the remainder of this channel is filled in by the client */
a02fd66e 289 struct ultra_vbus_deviceinfo chp_info;
32b6b291 290 /* describes client chipset device and driver */
a02fd66e 291 struct ultra_vbus_deviceinfo bus_info;
32b6b291 292 /* describes client bus device and driver */
a02fd66e 293 struct ultra_vbus_deviceinfo dev_info[0];
32b6b291 294 /* describes client device and driver for each device on the bus */
51121ffc 295};
12e364b9
KC
296
297#define VBUS_CH_SIZE_EXACT(MAXDEVICES) \
298 (sizeof(ULTRA_VBUS_CHANNEL_PROTOCOL) + ((MAXDEVICES) * \
299 sizeof(ULTRA_VBUS_DEVICEINFO)))
300#define VBUS_CH_SIZE(MAXDEVICES) COVER(VBUS_CH_SIZE_EXACT(MAXDEVICES), 4096)
301
12e364b9
KC
302#pragma pack(pop)
303
304#endif
This page took 0.379101 seconds and 5 git commands to generate.